2010-12-30 14:04:51 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP Version 5 |
|
|
|
|
+----------------------------------------------------------------------+
|
2013-09-28 17:17:33 +00:00
|
|
|
| Copyright (c) 1997-2013 The PHP Group |
|
2010-12-30 14:04:51 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2013-09-28 17:17:33 +00:00
|
|
|
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
2010-12-30 14:04:51 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
2012-04-27 16:41:32 +00:00
|
|
|
| Author: Patrick Reilly <preilly@php.net> |
|
2010-12-30 14:04:51 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2013-05-15 19:21:38 +00:00
|
|
|
/* $Id$ */
|
2010-12-30 14:04:51 +00:00
|
|
|
|
|
|
|
#ifndef PHP_V8JS_MACROS_H
|
|
|
|
#define PHP_V8JS_MACROS_H
|
|
|
|
|
2014-03-19 14:24:24 +00:00
|
|
|
#if __GNUC__ == 4 && __GNUC_MINOR__ == 4
|
|
|
|
#define _GLIBCXX_USE_NANOSLEEP 1
|
|
|
|
#endif
|
|
|
|
|
2013-04-12 23:00:27 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "php.h"
|
|
|
|
#include "php_v8js.h"
|
|
|
|
}
|
|
|
|
|
2010-12-30 14:04:51 +00:00
|
|
|
#include <v8.h>
|
|
|
|
|
2013-04-12 23:00:27 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <stack>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <map>
|
2014-08-10 21:48:54 +00:00
|
|
|
#include <list>
|
2013-04-12 23:00:27 +00:00
|
|
|
#include <vector>
|
2013-05-15 19:21:38 +00:00
|
|
|
#include <mutex>
|
2013-04-12 23:00:27 +00:00
|
|
|
|
2010-12-30 14:04:51 +00:00
|
|
|
/* V8Js Version */
|
2014-04-23 12:49:39 +00:00
|
|
|
#define V8JS_VERSION "0.1.5"
|
2010-12-30 14:04:51 +00:00
|
|
|
|
|
|
|
/* Helper macros */
|
2013-10-25 18:16:02 +00:00
|
|
|
#define V8JS_SYM(v) v8::String::NewFromUtf8(isolate, v, v8::String::kInternalizedString, sizeof(v) - 1)
|
|
|
|
#define V8JS_SYML(v, l) v8::String::NewFromUtf8(isolate, v, v8::String::kInternalizedString, l)
|
|
|
|
#define V8JS_STR(v) v8::String::NewFromUtf8(isolate, v)
|
|
|
|
#define V8JS_STRL(v, l) v8::String::NewFromUtf8(isolate, v, v8::String::kNormalString, l)
|
2014-03-21 23:07:53 +00:00
|
|
|
#define V8JS_INT(v) v8::Integer::New(isolate, v)
|
|
|
|
#define V8JS_UINT(v) v8::Integer::NewFromUnsigned(isolate, v)
|
2013-10-25 18:16:02 +00:00
|
|
|
#define V8JS_FLOAT(v) v8::Number::New(isolate, v)
|
|
|
|
#define V8JS_BOOL(v) ((v)?v8::True(isolate):v8::False(isolate))
|
2014-03-20 14:06:18 +00:00
|
|
|
#define V8JS_DATE(v) v8::Date::New(isolate, v)
|
2013-10-25 18:16:02 +00:00
|
|
|
#define V8JS_NULL v8::Null(isolate)
|
|
|
|
#define V8JS_UNDEFINED v8::Undefined(isolate)
|
2010-12-30 14:04:51 +00:00
|
|
|
#define V8JS_MN(name) v8js_method_##name
|
2013-07-07 22:58:19 +00:00
|
|
|
#define V8JS_METHOD(name) void V8JS_MN(name)(const v8::FunctionCallbackInfo<v8::Value>& info)
|
2013-12-21 00:18:08 +00:00
|
|
|
#define V8JS_THROW(isolate, type, message, message_len) (isolate)->ThrowException(v8::Exception::type(V8JS_STRL(message, message_len)))
|
|
|
|
#define V8JS_GLOBAL(isolate) ((isolate)->GetCurrentContext()->Global())
|
2010-12-30 14:04:51 +00:00
|
|
|
|
2013-10-25 18:05:51 +00:00
|
|
|
/* Abbreviate long type names */
|
|
|
|
typedef v8::Persistent<v8::FunctionTemplate, v8::CopyablePersistentTraits<v8::FunctionTemplate> > v8js_tmpl_t;
|
2014-03-22 16:41:00 +00:00
|
|
|
typedef v8::Persistent<v8::Object, v8::CopyablePersistentTraits<v8::Object> > v8js_persistent_obj_t;
|
2013-10-25 18:05:51 +00:00
|
|
|
|
|
|
|
/* Hidden field name used to link JS wrappers with underlying PHP object */
|
|
|
|
#define PHPJS_OBJECT_KEY "phpjs::object"
|
|
|
|
|
2010-12-30 14:04:51 +00:00
|
|
|
/* Helper macros */
|
2014-10-19 12:09:53 +00:00
|
|
|
#define V8JS_GET_CLASS_NAME(var, obj) \
|
2010-12-30 14:04:51 +00:00
|
|
|
v8::String::Utf8Value var(obj->GetConstructorName());
|
|
|
|
|
2012-06-13 02:24:48 +00:00
|
|
|
#if ZEND_MODULE_API_NO >= 20100409
|
|
|
|
# define ZEND_HASH_KEY_DC , const zend_literal *key
|
|
|
|
# define ZEND_HASH_KEY_CC , key
|
2013-10-25 21:40:20 +00:00
|
|
|
# define ZEND_HASH_KEY_NULL , NULL
|
2012-06-13 02:24:48 +00:00
|
|
|
#else
|
|
|
|
# define ZEND_HASH_KEY_DC
|
|
|
|
# define ZEND_HASH_KEY_CC
|
2013-10-25 21:40:20 +00:00
|
|
|
# define ZEND_HASH_KEY_NULL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* method signatures of zend_update_property and zend_read_property were
|
|
|
|
* declared as 'char *' instead of 'const char *' before PHP 5.4 */
|
|
|
|
#if ZEND_MODULE_API_NO >= 20100525
|
|
|
|
# define V8JS_CONST
|
|
|
|
#else
|
|
|
|
# define V8JS_CONST (char *)
|
2012-06-13 02:24:48 +00:00
|
|
|
#endif
|
|
|
|
|
2010-12-30 14:04:51 +00:00
|
|
|
/* Global flags */
|
2013-12-21 00:18:08 +00:00
|
|
|
#define V8JS_GLOBAL_SET_FLAGS(isolate,flags) V8JS_GLOBAL(isolate)->SetHiddenValue(V8JS_SYM("__php_flags__"), V8JS_INT(flags))
|
|
|
|
#define V8JS_GLOBAL_GET_FLAGS(isolate) V8JS_GLOBAL(isolate)->GetHiddenValue(V8JS_SYM("__php_flags__"))->IntegerValue();
|
2010-12-30 14:04:51 +00:00
|
|
|
|
|
|
|
/* Options */
|
|
|
|
#define V8JS_FLAG_NONE (1<<0)
|
|
|
|
#define V8JS_FLAG_FORCE_ARRAY (1<<1)
|
|
|
|
|
2013-10-12 18:44:15 +00:00
|
|
|
#define V8JS_DEBUG_AUTO_BREAK_NEVER 0
|
|
|
|
#define V8JS_DEBUG_AUTO_BREAK_ONCE 1
|
|
|
|
#define V8JS_DEBUG_AUTO_BREAK_ALWAYS 2
|
|
|
|
|
2010-12-30 14:04:51 +00:00
|
|
|
/* Extracts a C string from a V8 Utf8Value. */
|
2013-10-21 19:40:29 +00:00
|
|
|
static inline const char * ToCString(const v8::String::Utf8Value &value) /* {{{ */
|
2010-12-30 14:04:51 +00:00
|
|
|
{
|
|
|
|
return *value ? *value : "<string conversion failed>";
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* Extern Class entries */
|
|
|
|
extern zend_class_entry *php_ce_v8_object;
|
|
|
|
extern zend_class_entry *php_ce_v8_function;
|
|
|
|
|
|
|
|
/* Create PHP V8 object */
|
2013-04-13 23:36:05 +00:00
|
|
|
void php_v8js_create_v8(zval *, v8::Handle<v8::Value>, int, v8::Isolate * TSRMLS_DC);
|
2010-12-30 14:04:51 +00:00
|
|
|
|
|
|
|
/* Fetch V8 object properties */
|
2013-04-13 23:36:05 +00:00
|
|
|
int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value>, HashTable *, int, v8::Isolate * TSRMLS_DC);
|
2010-12-30 14:04:51 +00:00
|
|
|
|
|
|
|
/* Convert zval into V8 value */
|
2013-04-13 23:36:05 +00:00
|
|
|
v8::Handle<v8::Value> zval_to_v8js(zval *, v8::Isolate * TSRMLS_DC);
|
2010-12-30 14:04:51 +00:00
|
|
|
|
|
|
|
/* Convert V8 value into zval */
|
2013-04-13 23:36:05 +00:00
|
|
|
int v8js_to_zval(v8::Handle<v8::Value>, zval *, int, v8::Isolate * TSRMLS_DC);
|
2010-12-30 14:04:51 +00:00
|
|
|
|
2013-10-27 06:34:06 +00:00
|
|
|
struct php_v8js_accessor_ctx
|
|
|
|
{
|
|
|
|
char *variable_name_string;
|
|
|
|
uint variable_name_string_len;
|
|
|
|
v8::Isolate *isolate;
|
|
|
|
};
|
|
|
|
|
|
|
|
void php_v8js_accessor_ctx_dtor(php_v8js_accessor_ctx * TSRMLS_DC);
|
|
|
|
|
2010-12-30 14:04:51 +00:00
|
|
|
/* Register accessors into passed object */
|
2013-10-27 06:34:06 +00:00
|
|
|
void php_v8js_register_accessors(std::vector<php_v8js_accessor_ctx*> *accessor_list, v8::Local<v8::FunctionTemplate>, zval *, v8::Isolate * TSRMLS_DC);
|
|
|
|
|
2014-08-11 01:11:02 +00:00
|
|
|
struct php_v8js_object;
|
2012-04-27 16:26:15 +00:00
|
|
|
|
2013-03-24 16:27:13 +00:00
|
|
|
/* {{{ Context container */
|
|
|
|
struct php_v8js_ctx {
|
|
|
|
zend_object std;
|
|
|
|
v8::Persistent<v8::String> object_name;
|
|
|
|
v8::Persistent<v8::Context> context;
|
|
|
|
zend_bool report_uncaught;
|
|
|
|
zval *pending_exception;
|
|
|
|
int in_execution;
|
2013-04-11 16:15:55 +00:00
|
|
|
v8::Isolate *isolate;
|
|
|
|
bool time_limit_hit;
|
|
|
|
bool memory_limit_hit;
|
|
|
|
v8::Persistent<v8::FunctionTemplate> global_template;
|
2013-03-24 16:27:13 +00:00
|
|
|
zval *module_loader;
|
2013-04-12 23:00:27 +00:00
|
|
|
std::vector<char *> modules_stack;
|
|
|
|
std::vector<char *> modules_base;
|
2014-09-19 22:36:22 +00:00
|
|
|
std::map<char *, v8js_persistent_obj_t> modules_loaded;
|
2013-10-25 18:05:51 +00:00
|
|
|
std::map<const char *,v8js_tmpl_t> template_cache;
|
2014-03-22 16:41:00 +00:00
|
|
|
|
|
|
|
std::map<zval *, v8js_persistent_obj_t> weak_objects;
|
|
|
|
std::map<v8js_tmpl_t *, v8js_persistent_obj_t> weak_closures;
|
|
|
|
|
2014-08-11 01:11:02 +00:00
|
|
|
std::list<php_v8js_object *> php_v8js_objects;
|
|
|
|
|
2013-10-27 06:34:06 +00:00
|
|
|
std::vector<php_v8js_accessor_ctx *> accessor_list;
|
2014-03-20 09:20:59 +00:00
|
|
|
char *tz;
|
2013-10-26 03:46:03 +00:00
|
|
|
#ifdef ZTS
|
|
|
|
void ***zts_ctx;
|
|
|
|
#endif
|
2013-03-24 16:27:13 +00:00
|
|
|
};
|
|
|
|
/* }}} */
|
|
|
|
|
2013-10-26 03:46:03 +00:00
|
|
|
#ifdef ZTS
|
2014-10-19 12:09:53 +00:00
|
|
|
# define V8JS_TSRMLS_FETCH() TSRMLS_FETCH_FROM_CTX(((php_v8js_ctx *) isolate->GetData(0))->zts_ctx);
|
2013-10-26 03:46:03 +00:00
|
|
|
#else
|
|
|
|
# define V8JS_TSRMLS_FETCH()
|
|
|
|
#endif
|
|
|
|
|
2013-04-12 23:00:27 +00:00
|
|
|
// Timer context
|
|
|
|
struct php_v8js_timer_ctx
|
|
|
|
{
|
|
|
|
long time_limit;
|
|
|
|
long memory_limit;
|
|
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> time_point;
|
|
|
|
php_v8js_ctx *v8js_ctx;
|
2013-10-27 02:57:35 +00:00
|
|
|
bool killed;
|
2013-04-12 23:00:27 +00:00
|
|
|
};
|
|
|
|
|
2013-10-02 22:34:50 +00:00
|
|
|
/* {{{ Object container */
|
|
|
|
struct php_v8js_object {
|
|
|
|
zend_object std;
|
|
|
|
v8::Persistent<v8::Value> v8obj;
|
|
|
|
int flags;
|
2014-08-11 01:11:02 +00:00
|
|
|
struct php_v8js_ctx *ctx;
|
2013-10-26 13:37:33 +00:00
|
|
|
HashTable *properties;
|
2013-10-02 22:34:50 +00:00
|
|
|
};
|
|
|
|
/* }}} */
|
|
|
|
|
2014-09-16 14:09:58 +00:00
|
|
|
/* Resource declaration */
|
2013-10-02 22:34:50 +00:00
|
|
|
|
2013-04-12 23:00:27 +00:00
|
|
|
/* Module globals */
|
|
|
|
ZEND_BEGIN_MODULE_GLOBALS(v8js)
|
|
|
|
int v8_initialized;
|
|
|
|
HashTable *extensions;
|
|
|
|
|
|
|
|
/* Ini globals */
|
|
|
|
char *v8_flags; /* V8 command line flags */
|
2014-03-20 14:06:18 +00:00
|
|
|
bool use_date; /* Generate JS Date objects instead of PHP DateTime */
|
2013-04-12 23:00:27 +00:00
|
|
|
|
|
|
|
// Timer thread globals
|
|
|
|
std::stack<php_v8js_timer_ctx *> timer_stack;
|
|
|
|
std::thread *timer_thread;
|
|
|
|
std::mutex timer_mutex;
|
|
|
|
bool timer_stop;
|
|
|
|
|
2014-04-12 07:22:58 +00:00
|
|
|
// fatal error unwinding
|
|
|
|
bool fatal_error_abort;
|
|
|
|
int error_num;
|
|
|
|
char *error_message;
|
|
|
|
jmp_buf *unwind_env;
|
2014-05-22 08:17:38 +00:00
|
|
|
void (*old_error_handler)(int, const char *, const uint, const char*, va_list);
|
2013-04-12 23:00:27 +00:00
|
|
|
ZEND_END_MODULE_GLOBALS(v8js)
|
|
|
|
|
|
|
|
extern zend_v8js_globals v8js_globals;
|
|
|
|
|
2013-10-19 20:11:17 +00:00
|
|
|
ZEND_EXTERN_MODULE_GLOBALS(v8js)
|
|
|
|
|
2013-04-12 23:00:27 +00:00
|
|
|
#ifdef ZTS
|
|
|
|
# define V8JSG(v) TSRMG(v8js_globals_id, zend_v8js_globals *, v)
|
|
|
|
#else
|
|
|
|
# define V8JSG(v) (v8js_globals.v)
|
|
|
|
#endif
|
|
|
|
|
2013-03-24 16:27:13 +00:00
|
|
|
/* Register builtin methods into passed object */
|
|
|
|
void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate>, php_v8js_ctx *c);
|
2010-12-30 14:04:51 +00:00
|
|
|
|
2014-09-16 14:09:58 +00:00
|
|
|
typedef struct _php_v8js_script {
|
|
|
|
char *name;
|
|
|
|
v8::Isolate *isolate;
|
|
|
|
v8::Persistent<v8::Script, v8::CopyablePersistentTraits<v8::Script>> *script;
|
|
|
|
} php_v8js_script;
|
|
|
|
|
2014-07-01 08:25:37 +00:00
|
|
|
static void php_v8js_script_free(php_v8js_script *res, bool dispose_persistent);
|
2014-09-16 14:09:58 +00:00
|
|
|
|
2010-12-30 14:04:51 +00:00
|
|
|
#endif /* PHP_V8JS_MACROS_H */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
*/
|