0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-11-09 16:28:41 +00:00

Add isolate parameter to V8JS_* macros.

This avoids unnecessary calls to Isolate::GetCurrent() in the implementation.
By standardizing on the V8JS_SYM and V8JS_STR macros we also standardize on
UTF-8 encoding for v8 strings.
This commit is contained in:
C. Scott Ananian 2013-10-25 14:16:02 -04:00
parent 27a140c9fb
commit 52d8946b89
4 changed files with 117 additions and 99 deletions

View File

@ -35,15 +35,15 @@ extern "C" {
#define V8JS_VERSION "0.1.3"
/* Helper macros */
#define V8JS_SYM(v) v8::String::NewSymbol(v, sizeof(v) - 1)
#define V8JS_SYML(v, l) v8::String::NewSymbol(v, l)
#define V8JS_STR(v) v8::String::New(v)
#define V8JS_STRL(v, l) v8::String::New(v, l)
#define V8JS_INT(v) v8::Integer::New(v)
#define V8JS_FLOAT(v) v8::Number::New(v)
#define V8JS_BOOL(v) v8::Boolean::New(v)
#define V8JS_NULL v8::Null()
#define V8JS_UNDEFINED v8::Undefined()
#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)
#define V8JS_INT(v) v8::Integer::New(v, isolate)
#define V8JS_FLOAT(v) v8::Number::New(isolate, v)
#define V8JS_BOOL(v) ((v)?v8::True(isolate):v8::False(isolate))
#define V8JS_NULL v8::Null(isolate)
#define V8JS_UNDEFINED v8::Undefined(isolate)
#define V8JS_MN(name) v8js_method_##name
#define V8JS_METHOD(name) void V8JS_MN(name)(const v8::FunctionCallbackInfo<v8::Value>& info)
#define V8JS_THROW(type, message, message_len) v8::ThrowException(v8::Exception::type(V8JS_STRL(message, message_len)))

134
v8js.cc
View File

@ -128,13 +128,14 @@ static int php_v8js_v8_has_property(zval *object, zval *member, int has_set_exis
int retval = false;
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
v8::Locker locker(obj->isolate);
v8::Isolate::Scope isolate_scope(obj->isolate);
v8::HandleScope local_scope(obj->isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
v8::Isolate *isolate = obj->isolate;
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope local_scope(isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
v8::Context::Scope temp_scope(temp_context);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject() && !v8obj->IsFunction())
{
@ -186,13 +187,14 @@ static zval *php_v8js_v8_read_property(zval *object, zval *member, int type ZEND
zval *retval = NULL;
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
v8::Locker locker(obj->isolate);
v8::Isolate::Scope isolate_scope(obj->isolate);
v8::HandleScope local_scope(obj->isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
v8::Isolate *isolate = obj->isolate;
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope local_scope(isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
v8::Context::Scope temp_scope(temp_context);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject() && !v8obj->IsFunction())
{
@ -212,7 +214,7 @@ static zval *php_v8js_v8_read_property(zval *object, zval *member, int type ZEND
MAKE_STD_ZVAL(retval);
}
if (v8js_to_zval(jsVal, retval, obj->flags, obj->isolate TSRMLS_CC) == SUCCESS) {
if (v8js_to_zval(jsVal, retval, obj->flags, isolate TSRMLS_CC) == SUCCESS) {
return retval;
}
}
@ -228,16 +230,17 @@ static void php_v8js_v8_write_property(zval *object, zval *member, zval *value Z
{
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
v8::Locker locker(obj->isolate);
v8::Isolate::Scope isolate_scope(obj->isolate);
v8::HandleScope local_scope(obj->isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
v8::Isolate *isolate = obj->isolate;
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope local_scope(isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
v8::Context::Scope temp_scope(temp_context);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (v8obj->IsObject() && !v8obj->IsFunction()) {
v8obj->ToObject()->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, obj->isolate TSRMLS_CC));
v8obj->ToObject()->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, isolate TSRMLS_CC));
}
}
/* }}} */
@ -246,13 +249,14 @@ static void php_v8js_v8_unset_property(zval *object, zval *member ZEND_HASH_KEY_
{
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
v8::Locker locker(obj->isolate);
v8::Isolate::Scope isolate_scope(obj->isolate);
v8::HandleScope local_scope(obj->isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
v8::Isolate *isolate = obj->isolate;
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope local_scope(isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
v8::Context::Scope temp_scope(temp_context);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (v8obj->IsObject() && !v8obj->IsFunction()) {
v8obj->ToObject()->ForceDelete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)));
@ -317,14 +321,15 @@ static HashTable *php_v8js_v8_get_properties(zval *object TSRMLS_DC) /* {{{ */
ALLOC_HASHTABLE(retval);
zend_hash_init(retval, 0, NULL, ZVAL_PTR_DTOR, 0);
v8::Locker locker(obj->isolate);
v8::Isolate::Scope isolate_scope(obj->isolate);
v8::HandleScope local_scope(obj->isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
v8::Isolate *isolate = obj->isolate;
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope local_scope(isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
v8::Context::Scope temp_scope(temp_context);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (php_v8js_v8_get_properties_hash(v8obj, retval, obj->flags, obj->isolate TSRMLS_CC) == SUCCESS) {
if (php_v8js_v8_get_properties_hash(v8obj, retval, obj->flags, isolate TSRMLS_CC) == SUCCESS) {
return retval;
}
@ -344,13 +349,14 @@ static zend_function *php_v8js_v8_get_method(zval **object_ptr, char *method, in
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(*object_ptr TSRMLS_CC);
zend_function *f;
v8::Locker locker(obj->isolate);
v8::Isolate::Scope isolate_scope(obj->isolate);
v8::HandleScope local_scope(obj->isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
v8::Isolate *isolate = obj->isolate;
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope local_scope(isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
v8::Context::Scope temp_scope(temp_context);
v8::Local<v8::String> jsKey = V8JS_STRL(method, method_len);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (!obj->v8obj.IsEmpty() && v8obj->IsObject() && !v8obj->IsFunction()) {
v8::Local<v8::Object> jsObj = v8obj->ToObject();
@ -389,14 +395,15 @@ static int php_v8js_v8_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS) /
zend_get_parameters_array_ex(argc, argv);
}
v8::Locker locker(obj->isolate);
v8::Isolate::Scope isolate_scope(obj->isolate);
v8::HandleScope local_scope(obj->isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
v8::Isolate *isolate = obj->isolate;
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope local_scope(isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
v8::Context::Scope temp_scope(temp_context);
v8::Local<v8::String> method_name = V8JS_SYML(method, strlen(method));
v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj)->ToObject();
v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj)->ToObject();
v8::Local<v8::Function> cb;
if (method_name->Equals(V8JS_SYM(V8JS_V8_INVOKE_FUNC_NAME))) {
@ -409,7 +416,7 @@ static int php_v8js_v8_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS) /
v8::Local<v8::Value> js_retval;
for (i = 0; i < argc; i++) {
jsArgv[i] = v8::Local<v8::Value>::New(obj->isolate, zval_to_v8js(*argv[i], obj->isolate TSRMLS_CC));
jsArgv[i] = v8::Local<v8::Value>::New(isolate, zval_to_v8js(*argv[i], isolate TSRMLS_CC));
}
js_retval = cb->Call(V8JS_GLOBAL, argc, jsArgv);
@ -421,7 +428,7 @@ static int php_v8js_v8_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS) /
}
if (return_value_used) {
return v8js_to_zval(js_retval, return_value, obj->flags, obj->isolate TSRMLS_CC);
return v8js_to_zval(js_retval, return_value, obj->flags, isolate TSRMLS_CC);
}
return SUCCESS;
@ -434,12 +441,13 @@ static int php_v8js_v8_get_closure(zval *object, zend_class_entry **ce_ptr, zend
php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
v8::Locker locker(obj->isolate);
v8::Isolate::Scope isolate_scope(obj->isolate);
v8::HandleScope local_scope(obj->isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
v8::Isolate *isolate = obj->isolate;
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope local_scope(isolate);
v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
v8::Context::Scope temp_scope(temp_context);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (!v8obj->IsFunction()) {
return FAILURE;
@ -722,11 +730,12 @@ static PHP_METHOD(V8Js, __construct)
v8::ExtensionConfiguration extension_conf(exts_count, exts);
// Isolate execution
v8::Locker locker(c->isolate);
v8::Isolate::Scope isolate_scope(c->isolate);
v8::Isolate *isolate = c->isolate;
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
/* Handle scope */
v8::HandleScope handle_scope(c->isolate);
v8::HandleScope handle_scope(isolate);
/* Redirect fatal errors to PHP error handler */
// This needs to be done within the context isolate
@ -737,15 +746,15 @@ static PHP_METHOD(V8Js, __construct)
v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New();
tpl->SetClassName(V8JS_SYM("V8Js"));
c->global_template.Reset(c->isolate, tpl);
c->global_template.Reset(isolate, tpl);
/* Register builtin methods */
php_v8js_register_methods(tpl->InstanceTemplate(), c);
/* Create context */
v8::Local<v8::Context> context = v8::Context::New(c->isolate, &extension_conf, tpl->InstanceTemplate());
v8::Local<v8::Context> context = v8::Context::New(isolate, &extension_conf, tpl->InstanceTemplate());
context->SetAlignedPointerInEmbedderData(1, c);
c->context.Reset(c->isolate, context);
c->context.Reset(isolate, context);
if (exts) {
_php_v8js_free_ext_strarr(exts, exts_count);
@ -778,12 +787,12 @@ static PHP_METHOD(V8Js, __construct)
/* Register Get accessor for passed variables */
if (vars_arr && zend_hash_num_elements(Z_ARRVAL_P(vars_arr)) > 0) {
php_v8js_register_accessors(php_obj_t->InstanceTemplate(), vars_arr, c->isolate TSRMLS_CC);
php_v8js_register_accessors(php_obj_t->InstanceTemplate(), vars_arr, isolate TSRMLS_CC);
}
/* Set name for the PHP JS object */
v8::Local<v8::String> object_name_js = (object_name_len) ? V8JS_SYML(object_name, object_name_len) : V8JS_SYM("PHP");
c->object_name.Reset(c->isolate, object_name_js);
c->object_name.Reset(isolate, object_name_js);
/* Add the PHP object into global object */
v8::Local<v8::Object> php_obj = php_obj_t->InstanceTemplate()->NewInstance();
@ -810,7 +819,7 @@ static PHP_METHOD(V8Js, __construct)
zend_property_info *property_info = zend_get_property_info(c->std.ce, &zmember, 1 TSRMLS_CC);
if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
/* Write value to PHP JS object */
php_obj->ForceSet(V8JS_SYML(member, member_len - 1), zval_to_v8js(*value, c->isolate TSRMLS_CC), v8::ReadOnly);
php_obj->ForceSet(V8JS_SYML(member, member_len - 1), zval_to_v8js(*value, isolate TSRMLS_CC), v8::ReadOnly);
}
}
@ -827,10 +836,11 @@ static PHP_METHOD(V8Js, __construct)
} \
\
(ctx) = (php_v8js_ctx *) zend_object_store_get_object(object TSRMLS_CC); \
v8::Locker locker((ctx)->isolate); \
v8::Isolate::Scope isolate_scope((ctx)->isolate); \
v8::HandleScope handle_scope((ctx)->isolate); \
v8::Context::Scope context_scope((ctx)->isolate, (ctx)->context);
v8::Isolate *isolate = (ctx)->isolate; \
v8::Locker locker(isolate); \
v8::Isolate::Scope isolate_scope(isolate); \
v8::HandleScope handle_scope(isolate); \
v8::Context::Scope context_scope(isolate, (ctx)->context);
static void php_v8js_timer_push(long time_limit, long memory_limit, php_v8js_ctx *c TSRMLS_DC)
{
@ -941,7 +951,7 @@ static PHP_METHOD(V8Js, executeString)
v8::Local<v8::String> sname = identifier_len ? V8JS_SYML(identifier, identifier_len) : V8JS_SYM("V8Js::executeString()");
/* Compiles a string context independently. TODO: Add a php function which calls this and returns the result as resource which can be executed later. */
v8::Local<v8::String> source = v8::String::New(str, str_len);
v8::Local<v8::String> source = V8JS_STRL(str, str_len);
v8::Local<v8::Script> script = v8::Script::New(source, sname);
/* Compile errors? */
@ -1353,11 +1363,11 @@ static void php_v8js_write_property(zval *object, zval *member, zval *value ZEND
zend_property_info *property_info = zend_get_property_info(c->std.ce, member, 1 TSRMLS_CC);
if(property_info->flags & ZEND_ACC_PUBLIC) {
/* Global PHP JS object */
v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(c->isolate, c->object_name);
v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
v8::Local<v8::Object> jsobj = V8JS_GLOBAL->Get(object_name_js)->ToObject();
/* Write value to PHP JS object */
jsobj->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, c->isolate TSRMLS_CC), v8::ReadOnly);
jsobj->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, isolate TSRMLS_CC), v8::ReadOnly);
}
/* Write value to PHP object */
@ -1370,7 +1380,7 @@ static void php_v8js_unset_property(zval *object, zval *member ZEND_HASH_KEY_DC
V8JS_BEGIN_CTX(c, object)
/* Global PHP JS object */
v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(c->isolate, c->object_name);
v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
v8::Local<v8::Object> jsobj = V8JS_GLOBAL->Get(object_name_js)->ToObject();
/* Delete value from PHP JS object */

View File

@ -196,7 +196,7 @@ static void php_v8js_php_callback(const v8::FunctionCallbackInfo<v8::Value>& inf
/* Callback for PHP constructor calls */
static void php_v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::Isolate *isolate = info.GetIsolate();
info.GetReturnValue().Set(V8JS_UNDEFINED);
// @todo assert constructor call
@ -218,7 +218,7 @@ static void php_v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value
// Check access on __construct function, if any
if (ctor_ptr != NULL && (ctor_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Call to protected __construct() not allowed")));
info.GetReturnValue().Set(v8::ThrowException(V8JS_SYM("Call to protected __construct() not allowed")));
return;
}
@ -233,7 +233,7 @@ static void php_v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value
newobj->SetAlignedPointerInInternalField(0, value);
newobj->SetAlignedPointerInInternalField(1, (void *) isolate);
newobj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), v8::True(isolate));
newobj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), V8JS_BOOL(true));
}
/* }}} */
@ -262,6 +262,7 @@ static int _php_v8js_is_assoc_array(HashTable *myht TSRMLS_DC) /* {{{ */
static void php_v8js_property_caller(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Local<v8::Object> self = info.Holder();
v8::Isolate *isolate = reinterpret_cast<v8::Isolate *>(self->GetAlignedPointerFromInternalField(1));
v8::Local<v8::String> cname = info.Callee()->GetName()->ToString();
v8::Local<v8::Value> value;
v8::Local<v8::String> cb_func = v8::Local<v8::String>::Cast(info.Data());
@ -271,7 +272,7 @@ static void php_v8js_property_caller(const v8::FunctionCallbackInfo<v8::Value>&
if (!value.IsEmpty() && value->IsFunction())
{
int argc = info.Length(), i = 0;
v8::Local<v8::Value> *argv = new v8::Local<v8::Value>[argc];
v8::Local<v8::Value> argv[argc];
v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(value);
if (cb_func->Equals(V8JS_SYM(ZEND_INVOKE_FUNC_NAME))) {
@ -319,6 +320,8 @@ static void php_v8js_property_getter(v8::Local<v8::String> property, const v8::P
return;
}
v8::Isolate *isolate = reinterpret_cast<v8::Isolate *>(self->GetAlignedPointerFromInternalField(1));
/* If __get() is set for PHP object, call it */
value = self->GetHiddenValue(V8JS_SYM(ZEND_GET_FUNC_NAME));
if (!value.IsEmpty() && value->IsFunction()) {
@ -343,6 +346,7 @@ static void php_v8js_property_getter(v8::Local<v8::String> property, const v8::P
static void php_v8js_property_query(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Integer> &info) /* {{{ */
{
v8::Local<v8::Object> self = info.Holder();
v8::Isolate *isolate = reinterpret_cast<v8::Isolate *>(self->GetAlignedPointerFromInternalField(1));
v8::Local<v8::Value> value;
/* Return early if property is set in JS object */

View File

@ -43,6 +43,7 @@ V8JS_METHOD(sleep) /* {{{ */
/* global.print - php print() */
V8JS_METHOD(print) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
int ret = 0;
TSRMLS_FETCH();
@ -55,7 +56,7 @@ V8JS_METHOD(print) /* {{{ */
}
/* }}} */
static void _php_v8js_dumper(v8::Local<v8::Value> var, int level TSRMLS_DC) /* {{{ */
static void _php_v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int level TSRMLS_DC) /* {{{ */
{
if (level > 1) {
php_printf("%*c", (level - 1) * 2, ' ');
@ -125,7 +126,7 @@ static void _php_v8js_dumper(v8::Local<v8::Value> var, int level TSRMLS_DC) /* {
for (unsigned i = 0; i < length; i++) {
php_printf("%*c[%d] =>\n", level * 2, ' ', i);
_php_v8js_dumper(array->Get(i), level + 1 TSRMLS_CC);
_php_v8js_dumper(isolate, array->Get(i), level + 1 TSRMLS_CC);
}
if (level > 1) {
@ -162,7 +163,7 @@ static void _php_v8js_dumper(v8::Local<v8::Value> var, int level TSRMLS_DC) /* {
v8::Local<v8::String> key = keys->Get(i)->ToString();
v8::String::Utf8Value kname(key);
php_printf("%*c[\"%s\"] =>\n", level * 2, ' ', ToCString(kname));
_php_v8js_dumper(object->Get(key), level + 1 TSRMLS_CC);
_php_v8js_dumper(isolate, object->Get(key), level + 1 TSRMLS_CC);
}
}
@ -182,10 +183,11 @@ static void _php_v8js_dumper(v8::Local<v8::Value> var, int level TSRMLS_DC) /* {
/* global.var_dump - Dump JS values */
V8JS_METHOD(var_dump) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
TSRMLS_FETCH();
for (int i = 0; i < info.Length(); i++) {
_php_v8js_dumper(info[i], 1 TSRMLS_CC);
_php_v8js_dumper(isolate, info[i], 1 TSRMLS_CC);
}
info.GetReturnValue().Set(V8JS_NULL);
@ -194,6 +196,7 @@ V8JS_METHOD(var_dump) /* {{{ */
V8JS_METHOD(require)
{
v8::Isolate *isolate = info.GetIsolate();
TSRMLS_FETCH();
// Get the extension context
@ -202,7 +205,7 @@ V8JS_METHOD(require)
// Check that we have a module loader
if (c->module_loader == NULL) {
info.GetReturnValue().Set(v8::ThrowException(v8::String::New("No module loader")));
info.GetReturnValue().Set(v8::ThrowException(V8JS_SYM("No module loader")));
return;
}
@ -216,7 +219,7 @@ V8JS_METHOD(require)
php_v8js_commonjs_normalise_identifier(c->modules_base.back(), module_id, normalised_path, module_name);
efree(module_id);
char *normalised_module_id = (char *)emalloc(strlen(module_id));
char *normalised_module_id = (char *)emalloc(strlen(normalised_path)+1+strlen(module_name)+1);
*normalised_module_id = 0;
if (strlen(normalised_path) > 0) {
@ -233,7 +236,7 @@ V8JS_METHOD(require)
if (!strcmp(*it, normalised_module_id)) {
efree(normalised_path);
info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Module cyclic dependency")));
info.GetReturnValue().Set(v8::ThrowException(V8JS_SYM("Module cyclic dependency")));
return;
}
}
@ -258,7 +261,7 @@ V8JS_METHOD(require)
if (FAILURE == call_user_function(EG(function_table), NULL, c->module_loader, &module_code, 1, params TSRMLS_CC)) {
efree(normalised_path);
info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Module loader callback failed")));
info.GetReturnValue().Set(v8::ThrowException(V8JS_SYM("Module loader callback failed")));
return;
}
@ -268,7 +271,7 @@ V8JS_METHOD(require)
// Clear the PHP exception and throw it in V8 instead
zend_clear_exception(TSRMLS_C);
info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Module loader callback exception")));
info.GetReturnValue().Set(v8::ThrowException(V8JS_SYM("Module loader callback exception")));
return;
}
@ -281,44 +284,44 @@ V8JS_METHOD(require)
if (!strlen(Z_STRVAL(module_code))) {
efree(normalised_path);
info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Module loader callback did not return code")));
info.GetReturnValue().Set(v8::ThrowException(V8JS_SYM("Module loader callback did not return code")));
return;
}
// Create a template for the global object and set the built-in global functions
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
global->Set(v8::String::New("print"), v8::FunctionTemplate::New(V8JS_MN(print)), v8::ReadOnly);
global->Set(V8JS_SYM("print"), v8::FunctionTemplate::New(V8JS_MN(print)), v8::ReadOnly);
global->Set(V8JS_SYM("sleep"), v8::FunctionTemplate::New(V8JS_MN(sleep)), v8::ReadOnly);
global->Set(v8::String::New("require"), v8::FunctionTemplate::New(V8JS_MN(require), v8::External::New(c)), v8::ReadOnly);
global->Set(V8JS_SYM("require"), v8::FunctionTemplate::New(V8JS_MN(require), v8::External::New(c)), v8::ReadOnly);
// Add the exports object in which the module can return its API
v8::Handle<v8::ObjectTemplate> exports_template = v8::ObjectTemplate::New();
v8::Handle<v8::Object> exports = exports_template->NewInstance();
global->Set(v8::String::New("exports"), exports);
global->Set(V8JS_SYM("exports"), exports);
// Add the module object in which the module can have more fine-grained control over what it can return
v8::Handle<v8::ObjectTemplate> module_template = v8::ObjectTemplate::New();
v8::Handle<v8::Object> module = module_template->NewInstance();
module->Set(v8::String::New("id"), v8::String::New(normalised_module_id));
global->Set(v8::String::New("module"), module);
module->Set(V8JS_SYM("id"), V8JS_STR(normalised_module_id));
global->Set(V8JS_SYM("module"), module);
// Each module gets its own context so different modules do not affect each other
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(c->isolate, v8::Context::New(c->isolate, NULL, global));
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, v8::Context::New(isolate, NULL, global));
// Catch JS exceptions
v8::TryCatch try_catch;
v8::Locker locker(c->isolate);
v8::Isolate::Scope isolate_scope(c->isolate);
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(c->isolate);
v8::HandleScope handle_scope(isolate);
// Enter the module context
v8::Context::Scope scope(context);
// Set script identifier
v8::Local<v8::String> sname = V8JS_SYM("require");
v8::Local<v8::String> source = v8::String::New(Z_STRVAL(module_code));
v8::Local<v8::String> source = V8JS_STR(Z_STRVAL(module_code));
// Create and compile script
v8::Local<v8::Script> script = v8::Script::New(source, sname);
@ -326,7 +329,7 @@ V8JS_METHOD(require)
// The script will be empty if there are compile errors
if (script.IsEmpty()) {
efree(normalised_path);
info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Module script compile failed")));
info.GetReturnValue().Set(v8::ThrowException(V8JS_SYM("Module script compile failed")));
return;
}
@ -346,7 +349,7 @@ V8JS_METHOD(require)
// Script possibly terminated, return immediately
if (!try_catch.CanContinue()) {
info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Module script compile failed")));
info.GetReturnValue().Set(v8::ThrowException(V8JS_SYM("Module script compile failed")));
return;
}
@ -359,9 +362,9 @@ V8JS_METHOD(require)
// Cache the module so it doesn't need to be compiled and run again
// Ensure compatibility with CommonJS implementations such as NodeJS by playing nicely with module.exports and exports
if (module->Has(v8::String::New("exports")) && !module->Get(v8::String::New("exports"))->IsUndefined()) {
if (module->Has(V8JS_SYM("exports")) && !module->Get(V8JS_SYM("exports"))->IsUndefined()) {
// If module.exports has been set then we cache this arbitrary value...
V8JSG(modules_loaded)[normalised_module_id] = handle_scope.Close(module->Get(v8::String::New("exports"))->ToObject());
V8JSG(modules_loaded)[normalised_module_id] = handle_scope.Close(module->Get(V8JS_SYM("exports"))->ToObject());
} else {
// ...otherwise we cache the exports object itself
V8JSG(modules_loaded)[normalised_module_id] = handle_scope.Close(exports);
@ -372,6 +375,7 @@ V8JS_METHOD(require)
void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate> global, php_v8js_ctx *c) /* {{{ */
{
v8::Isolate *isolate = c->isolate;
global->Set(V8JS_SYM("exit"), v8::FunctionTemplate::New(V8JS_MN(exit)), v8::ReadOnly);
global->Set(V8JS_SYM("sleep"), v8::FunctionTemplate::New(V8JS_MN(sleep)), v8::ReadOnly);
global->Set(V8JS_SYM("print"), v8::FunctionTemplate::New(V8JS_MN(print)), v8::ReadOnly);