0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-12-21 16:31:53 +00:00

omit use of deprecated apis

This commit is contained in:
Stefan Siegl 2018-09-13 14:01:15 +02:00
parent ab6e2267af
commit 9c45508183
No known key found for this signature in database
GPG Key ID: 73942AF5642F3DDA
10 changed files with 351 additions and 239 deletions

View File

@ -210,11 +210,11 @@ void v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& inf
for(int j = 0; j < length; j ++) {
if(v8js_array_access_isset_p(object, j)) {
result->Set(i ++, V8JS_INT(j));
result->Set(isolate->GetEnteredContext(), i ++, V8JS_INT(j));
}
}
result->Set(V8JS_STR("length"), V8JS_INT(i));
result->Set(isolate->GetEnteredContext(), V8JS_SYM("length"), V8JS_INT(i));
info.GetReturnValue().Set(result);
}
/* }}} */
@ -244,7 +244,13 @@ void v8js_array_access_named_getter(v8::Local<v8::Name> property_name, const v8:
info.GetReturnValue().Set(ret_value);
}
ret_value = prototype->ToObject(isolate)->Get(property);
v8::Local<v8::Object> prototype_object;
if(!prototype->ToObject(isolate->GetEnteredContext()).ToLocal(&prototype_object)) {
/* ehh? Array.prototype not an object? strange, stop. */
info.GetReturnValue().Set(ret_value);
}
prototype_object->Get(isolate->GetEnteredContext(), property).ToLocal(&ret_value);
}
info.GetReturnValue().Set(ret_value);

View File

@ -102,7 +102,7 @@ static void v8js_free_storage(zend_object *object) /* {{{ */
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(c->isolate, c->context);
v8::Context::Scope context_scope(v8_context);
v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(c->isolate, c->object_name);
V8JS_GLOBAL(c->isolate)->Delete(object_name_js);
V8JS_GLOBAL(c->isolate)->Delete(v8_context, object_name_js);
}
c->object_name.Reset();
@ -494,8 +494,7 @@ static PHP_METHOD(V8Js, __construct)
return;
}
object_name_js = v8::String::NewFromUtf8(isolate, ZSTR_VAL(object_name),
v8::String::kInternalizedString, static_cast<int>(ZSTR_LEN(object_name)));
object_name_js = V8JS_ZSYM(object_name);
}
else {
object_name_js = V8JS_SYM("PHP");
@ -505,7 +504,7 @@ static PHP_METHOD(V8Js, __construct)
/* Add the PHP object into global object */
php_obj_t->InstanceTemplate()->SetInternalFieldCount(2);
v8::Local<v8::Object> php_obj = php_obj_t->InstanceTemplate()->NewInstance();
v8::Local<v8::Object> php_obj = php_obj_t->InstanceTemplate()->NewInstance(context).ToLocalChecked();
V8JS_GLOBAL(isolate)->DefineOwnProperty(context, object_name_js, php_obj, v8::ReadOnly);
/* Export public property values */
@ -524,8 +523,7 @@ static PHP_METHOD(V8Js, __construct)
return;
}
v8::Local<v8::Name> key = v8::String::NewFromUtf8(isolate, ZSTR_VAL(member),
v8::String::kInternalizedString, static_cast<int>(ZSTR_LEN(member)));
v8::Local<v8::Name> key = V8JS_ZSYM(member);
/* Write value to PHP JS object */
value = OBJ_PROP(Z_OBJ_P(getThis()), property_info->offset);
@ -587,24 +585,16 @@ static PHP_METHOD(V8Js, __construct)
return;
}
v8::Local<v8::String> method_name = v8::String::NewFromUtf8(isolate,
ZSTR_VAL(method_ptr->common.function_name), v8::String::kInternalizedString,
static_cast<int>(ZSTR_LEN(method_ptr->common.function_name)));
v8::Local<v8::String> method_name = V8JS_ZSYM(method_ptr->common.function_name);
v8::Local<v8::FunctionTemplate> ft;
/*try {
ft = v8::Local<v8::FunctionTemplate>::New
(isolate, c->method_tmpls.at(method_ptr));
}
catch (const std::out_of_range &) */ {
ft = v8::FunctionTemplate::New(isolate, v8js_php_callback,
v8::External::New((isolate), method_ptr));
// @fixme add/check Signature v8::Signature::New((isolate), tmpl));
v8js_function_tmpl_t *persistent_ft = &c->method_tmpls[method_ptr];
persistent_ft->Reset(isolate, ft);
}
ft = v8::FunctionTemplate::New(isolate, v8js_php_callback,
v8::External::New((isolate), method_ptr));
// @fixme add/check Signature v8::Signature::New((isolate), tmpl));
v8js_function_tmpl_t *persistent_ft = &c->method_tmpls[method_ptr];
persistent_ft->Reset(isolate, ft);
php_obj->CreateDataProperty(context, method_name, ft->GetFunction());
php_obj->CreateDataProperty(context, method_name, ft->GetFunction(context).ToLocalChecked());
} ZEND_HASH_FOREACH_END();
}
/* }}} */
@ -646,7 +636,7 @@ static void v8js_compile_script(zval *this_ptr, const zend_string *str, const ze
}
v8::Local<v8::String> sname = identifier
? v8::String::NewFromUtf8(isolate, ZSTR_VAL(identifier), v8::String::kNormalString, static_cast<int>(ZSTR_LEN(identifier)))
? V8JS_ZSTR(identifier)
: V8JS_SYM("V8Js::compileString()");
v8::ScriptOrigin origin(sname);
@ -656,7 +646,7 @@ static void v8js_compile_script(zval *this_ptr, const zend_string *str, const ze
return;
}
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, ZSTR_VAL(str), v8::String::kNormalString, static_cast<int>(ZSTR_LEN(str)));
v8::Local<v8::String> source = V8JS_ZSTR(str);
v8::MaybeLocal<v8::Script> script = v8::Script::Compile(v8::Local<v8::Context>::New(isolate, c->context), source, &origin);
/* Compile errors? */
@ -1164,7 +1154,7 @@ static v8::StartupData createSnapshotDataBlob(v8::SnapshotCreator *snapshot_crea
v8::Context::Scope context_scope(context);
v8::TryCatch try_catch(isolate);
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, ZSTR_VAL(str), v8::String::kNormalString, static_cast<int>(ZSTR_LEN(str)));
v8::Local<v8::String> source = V8JS_ZSTR(str);
v8::MaybeLocal<v8::Script> script = v8::Script::Compile(context, source);
if (script.IsEmpty() || script.ToLocalChecked()->Run(context).IsEmpty())
@ -1330,7 +1320,7 @@ static void v8js_write_property(zval *object, zval *member, zval *value, void **
(property_info->flags & ZEND_ACC_PUBLIC))) {
/* Global PHP JS object */
v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(object_name_js)->ToObject(isolate);
v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(v8_context, object_name_js).ToLocalChecked()->ToObject(v8_context).ToLocalChecked();
if (Z_STRLEN_P(member) > std::numeric_limits<int>::max()) {
zend_throw_exception(php_ce_v8js_exception,
@ -1354,7 +1344,7 @@ static void v8js_unset_property(zval *object, zval *member, void **cache_slot) /
/* Global PHP JS object */
v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(object_name_js)->ToObject(isolate);
v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(v8_context, object_name_js).ToLocalChecked()->ToObject(v8_context).ToLocalChecked();
if (Z_STRLEN_P(member) > std::numeric_limits<int>::max()) {
zend_throw_exception(php_ce_v8js_exception,
@ -1364,7 +1354,7 @@ static void v8js_unset_property(zval *object, zval *member, void **cache_slot) /
/* Delete value from PHP JS object */
v8::Local<v8::Value> key = V8JS_SYML(Z_STRVAL_P(member), static_cast<int>(Z_STRLEN_P(member)));
jsobj->Delete(key);
jsobj->Delete(v8_context, key);
/* Unset from PHP object */
std_object_handlers.unset_property(object, member, NULL);

View File

@ -79,6 +79,7 @@ static v8::Local<v8::Value> v8js_hash_to_jsarr(zval *value, v8::Isolate *isolate
return V8JS_NULL;
}
v8::Local<v8::Context> v8_context = isolate->GetEnteredContext();
newarr = v8::Array::New(isolate, i);
if (i > 0)
@ -96,7 +97,7 @@ static v8::Local<v8::Value> v8js_hash_to_jsarr(zval *value, v8::Isolate *isolate
}
ZEND_HASH_FOREACH_VAL(myht, data) {
newarr->Set(index++, zval_to_v8js(data, isolate));
newarr->Set(v8_context, index++, zval_to_v8js(data, isolate));
} ZEND_HASH_FOREACH_END();
#if PHP_VERSION_ID >= 70300
@ -114,7 +115,7 @@ static v8::Local<v8::Value> v8js_hash_to_jsarr(zval *value, v8::Isolate *isolate
v8::Local<v8::Value> zend_long_to_v8js(zend_long v, v8::Isolate *isolate) /* {{{ */
{
if (v < - std::numeric_limits<int32_t>::min() || v > std::numeric_limits<int32_t>::max()) {
if (v < std::numeric_limits<int32_t>::min() || v > std::numeric_limits<int32_t>::max()) {
return V8JS_FLOAT(static_cast<double>(v));
} else {
return V8JS_INT(static_cast<int32_t>(v));
@ -148,7 +149,7 @@ v8::Local<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate) /* {{{ */
if (instanceof_function(Z_OBJCE_P(value), ce)) {
zval dtval;
zend_call_method_with_0_params(value, NULL, NULL, "getTimestamp", &dtval);
jsValue = V8JS_DATE(((double)Z_LVAL(dtval) * 1000.0));
v8::Date::New(isolate->GetEnteredContext(), ((double)Z_LVAL(dtval) * 1000.0)).ToLocal(&jsValue);
zval_dtor(&dtval);
} else
jsValue = v8js_hash_to_jsobj(value, isolate);
@ -164,7 +165,7 @@ v8::Local<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate) /* {{{ */
break;
}
jsValue = v8::String::NewFromUtf8(isolate, ZSTR_VAL(value_str), v8::String::kNormalString, static_cast<int>(ZSTR_LEN(value_str)));
jsValue = V8JS_ZSTR(value_str);
break;
case IS_LONG:
@ -201,33 +202,37 @@ v8::Local<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate) /* {{{ */
int v8js_to_zval(v8::Local<v8::Value> jsValue, zval *return_value, int flags, v8::Isolate *isolate) /* {{{ */
{
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, ctx->context);
if (jsValue->IsString())
{
v8::String::Utf8Value str(isolate, jsValue);
const char *cstr = ToCString(str);
RETVAL_STRINGL(cstr, jsValue->ToString(isolate)->Utf8Length(isolate));
RETVAL_STRINGL(cstr, str.length());
}
else if (jsValue->IsBoolean())
{
v8::Maybe<bool> value = jsValue->BooleanValue(v8::Local<v8::Context>::New(isolate, ctx->context));
if (value.IsNothing()) {
v8::Maybe<bool> value = jsValue->BooleanValue(v8_context);
if (value.IsNothing())
{
return FAILURE;
}
RETVAL_BOOL(value.ToChecked());
}
else if (jsValue->IsInt32() || jsValue->IsUint32())
{
v8::Maybe<int64_t> value = jsValue->IntegerValue(v8::Local<v8::Context>::New(isolate, ctx->context));
if (value.IsNothing()) {
v8::Maybe<int64_t> value = jsValue->IntegerValue(v8_context);
if (value.IsNothing())
{
return FAILURE;
}
RETVAL_LONG((long) value.ToChecked());
}
else if (jsValue->IsNumber())
{
v8::Maybe<double> value = jsValue->NumberValue(v8::Local<v8::Context>::New(isolate, ctx->context));
if (value.IsNothing()) {
v8::Maybe<double> value = jsValue->NumberValue(v8_context);
if (value.IsNothing())
{
return FAILURE;
}
RETVAL_DOUBLE(value.ToChecked());
@ -267,7 +272,11 @@ int v8js_to_zval(v8::Local<v8::Value> jsValue, zval *return_value, int flags, v8
}
else if (jsValue->IsObject())
{
v8::Local<v8::Object> self = jsValue->ToObject(isolate);
v8::Local<v8::Object> self;
if (!jsValue->ToObject(v8_context).ToLocal(&self))
{
return FAILURE;
}
// if this is a wrapped PHP object, then just unwrap it.
if (self->InternalFieldCount() == 2) {

View File

@ -92,8 +92,9 @@ void v8js_create_script_exception(zval *return_value, v8::Isolate *isolate, v8::
PHPV8_EXPROP(_string, JsTrace, ToCString(stacktrace));
}
if(try_catch->Exception()->IsObject() && try_catch->Exception()->ToObject(isolate)->InternalFieldCount() == 2) {
zend_object *php_exception = reinterpret_cast<zend_object *>(try_catch->Exception()->ToObject(isolate)->GetAlignedPointerFromInternalField(1));
v8::Local<v8::Object> error_object;
if(try_catch->Exception()->IsObject() && try_catch->Exception()->ToObject(context).ToLocal(&error_object) && error_object->InternalFieldCount() == 2) {
zend_object *php_exception = reinterpret_cast<zend_object *>(error_object->GetAlignedPointerFromInternalField(1));
zend_class_entry *exception_ce = zend_exception_get_default();
if (instanceof_function(php_exception->ce, exception_ce)) {

View File

@ -25,10 +25,10 @@ v8::Local<v8::Value> v8js_wrap_generator(v8::Isolate *isolate, v8::Local<v8::Val
assert(wrapped_object->IsObject());
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, ctx->context);
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, ctx->context);
v8::TryCatch try_catch(isolate);
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, "(\
v8::Local<v8::String> source = V8JS_SYM("(\
function(wrapped_object) { \
return (function*() { \
for(;;) { \
@ -40,14 +40,14 @@ function(wrapped_object) { \
} \
})(); \
})");
v8::MaybeLocal<v8::Script> script = v8::Script::Compile(context, source);
v8::MaybeLocal<v8::Script> script = v8::Script::Compile(v8_context, source);
if(script.IsEmpty()) {
zend_error(E_ERROR, "Failed to compile Generator object wrapper");
return result;
}
v8::MaybeLocal<v8::Value> wrapper_fn_val = script.ToLocalChecked()->Run(context);
v8::MaybeLocal<v8::Value> wrapper_fn_val = script.ToLocalChecked()->Run(v8_context);
if(wrapper_fn_val.IsEmpty() || !wrapper_fn_val.ToLocalChecked()->IsFunction()) {
zend_error(E_ERROR, "Failed to create Generator object wrapper function");
@ -60,7 +60,7 @@ function(wrapped_object) { \
new(&jsArgv[0]) v8::Local<v8::Value>;
jsArgv[0] = v8::Local<v8::Value>::New(isolate, wrapped_object);
result = wrapper_fn->Call(V8JS_GLOBAL(isolate), 1, jsArgv);
wrapper_fn->Call(v8_context, V8JS_GLOBAL(isolate), 1, jsArgv).ToLocal(&result);
return result;
}
/* }}} */

View File

@ -65,7 +65,7 @@ V8JS_METHOD(print) /* {{{ */
static void v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int level) /* {{{ */
{
v8js_ctx *c = (v8js_ctx *) isolate->GetData(0);
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, c->context);
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, c->context);
if (level > 1) {
php_printf("%*c", (level - 1) * 2, ' ');
@ -83,7 +83,7 @@ static void v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int leve
}
if (var->IsInt32())
{
v8::Maybe<int64_t> value = var->IntegerValue(context);
v8::Maybe<int64_t> value = var->IntegerValue(v8_context);
if (value.IsNothing())
{
php_printf("<empty>\n");
@ -96,7 +96,7 @@ static void v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int leve
}
if (var->IsUint32())
{
v8::Maybe<uint32_t> value = var->Uint32Value(context);
v8::Maybe<uint32_t> value = var->Uint32Value(v8_context);
if (value.IsNothing())
{
php_printf("<empty>\n");
@ -109,7 +109,7 @@ static void v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int leve
}
if (var->IsNumber())
{
v8::Maybe<double> value = var->NumberValue(context);
v8::Maybe<double> value = var->NumberValue(v8_context);
if (value.IsNothing())
{
php_printf("<empty>\n");
@ -122,7 +122,7 @@ static void v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int leve
}
if (var->IsBoolean())
{
v8::Maybe<bool> value = var->BooleanValue(context);
v8::Maybe<bool> value = var->BooleanValue(v8_context);
if (value.IsNothing())
{
php_printf("<empty>\n");
@ -142,7 +142,7 @@ static void v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int leve
details = re->GetSource();
}
else {
details = var->ToDetailString(context).FromMaybe(v8::Local<v8::String>());
details = var->ToDetailString(v8_context).FromMaybe(v8::Local<v8::String>());
if (try_catch.HasCaught()) {
details = V8JS_SYM("<toString threw exception>");
@ -151,7 +151,7 @@ static void v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int leve
v8::String::Utf8Value str(isolate, details);
const char *valstr = ToCString(str);
size_t valstr_len = details->ToString(isolate)->Utf8Length(isolate);
size_t valstr_len = str.length();
if (var->IsString())
{
@ -177,7 +177,16 @@ static void v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int leve
for (unsigned i = 0; i < length; i++) {
php_printf("%*c[%d] =>\n", level * 2, ' ', i);
v8js_dumper(isolate, array->Get(i), level + 1);
v8::MaybeLocal<v8::Value> value = array->Get(v8_context, i);
if (value.IsEmpty())
{
php_printf("<empty>\n");
}
else
{
v8js_dumper(isolate, value.ToLocalChecked(), level + 1);
}
}
if (level > 1) {
@ -194,13 +203,23 @@ static void v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int leve
if (var->IsFunction() && strcmp(ToCString(cname), "Closure") != 0)
{
v8::String::Utf8Value csource(isolate, object->ToString(isolate));
php_printf("object(Closure)#%d {\n%*c%s\n", hash, level * 2 + 2, ' ', ToCString(csource));
php_printf("object(Closure)#%d {\n%*c", hash, level * 2 + 2, ' ');
v8::Local<v8::String> source;
if (object->ToString(v8_context).ToLocal(&source))
{
v8::String::Utf8Value csource(isolate, source);
php_printf("%s\n", ToCString(csource));
}
else
{
php_printf("<empty>\n");
}
}
else
{
v8::Local<v8::Array> keys = object->GetOwnPropertyNames();
uint32_t length = keys->Length();
v8::MaybeLocal<v8::Array> keys = object->GetOwnPropertyNames(v8_context);
uint32_t length = keys.IsEmpty() ? 0 : keys.ToLocalChecked()->Length();
if (strcmp(ToCString(cname), "Array") == 0 ||
strcmp(ToCString(cname), "V8Object") == 0) {
@ -211,10 +230,26 @@ static void v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int leve
php_printf(" (%d) {\n", length);
for (unsigned i = 0; i < length; i++) {
v8::Local<v8::String> key = keys->Get(i)->ToString(isolate);
v8::String::Utf8Value kname(isolate, key);
php_printf("%*c[\"%s\"] =>\n", level * 2, ' ', ToCString(kname));
v8js_dumper(isolate, object->Get(key), level + 1);
v8::MaybeLocal<v8::Value> key_slot = keys.ToLocalChecked()->Get(v8_context, i);
v8::Local<v8::String> key;
if (key_slot.IsEmpty() || !key_slot.ToLocalChecked()->ToString(v8_context).ToLocal(&key))
{
key = V8JS_SYM("<empty>");
}
v8::String::Utf8Value key_name(isolate, key);
php_printf("%*c[\"%s\"] =>\n", level * 2, ' ', ToCString(key_name));
v8::MaybeLocal<v8::Value> value = object->Get(v8_context, key);
if (value.IsEmpty())
{
php_printf("<empty>\n");
}
else
{
v8js_dumper(isolate, value.ToLocalChecked(), level + 1);
}
}
}
@ -385,7 +420,9 @@ V8JS_METHOD(require)
if (c->modules_loaded.count(normalised_module_id) > 0) {
v8::Persistent<v8::Value> newobj;
newobj.Reset(isolate, c->modules_loaded[normalised_module_id]);
info.GetReturnValue().Set(newobj);
// TODO store v8::Global in c->modules_loaded directly!?
info.GetReturnValue().Set(v8::Global<v8::Value>(isolate, newobj));
efree(normalised_module_id);
efree(normalised_path);
@ -449,7 +486,7 @@ V8JS_METHOD(require)
}
if(Z_TYPE(module_code) == IS_OBJECT) {
v8::Local<v8::Object> newobj = zval_to_v8js(&module_code, isolate)->ToObject(isolate);
v8::Local<v8::Object> newobj = zval_to_v8js(&module_code, isolate)->ToObject(isolate->GetEnteredContext()).ToLocalChecked();
c->modules_loaded[normalised_module_id].Reset(isolate, newobj);
info.GetReturnValue().Set(newobj);
@ -503,7 +540,7 @@ V8JS_METHOD(require)
}
v8::Local<v8::String> base_path = V8JS_STR(normalised_path);
v8::MaybeLocal<v8::Function> require_fn = v8::FunctionTemplate::New(isolate, V8JS_MN(require), base_path)->GetFunction();
v8::MaybeLocal<v8::Function> require_fn = v8::FunctionTemplate::New(isolate, V8JS_MN(require), base_path)->GetFunction(context);
if (require_fn.IsEmpty()) {
efree(normalised_path);
@ -523,8 +560,8 @@ V8JS_METHOD(require)
v8::Local<v8::Object> exports = v8::Object::New(isolate);
v8::Local<v8::Object> module = v8::Object::New(isolate);
module->Set(V8JS_SYM("id"), V8JS_STR(normalised_module_id));
module->Set(V8JS_SYM("exports"), exports);
module->Set(context, V8JS_SYM("id"), V8JS_STR(normalised_module_id));
module->Set(context, V8JS_SYM("exports"), exports);
if (!module_function.IsEmpty() && module_function.ToLocalChecked()->IsFunction()) {
v8::Local<v8::Value> *jsArgv = static_cast<v8::Local<v8::Value> *>(alloca(3 * sizeof(v8::Local<v8::Value>)));
@ -538,7 +575,7 @@ V8JS_METHOD(require)
jsArgv[2] = require_fn.ToLocalChecked();
// actually call the module
v8::Local<v8::Function>::Cast(module_function.ToLocalChecked())->Call(exports, 3, jsArgv);
v8::Local<v8::Function>::Cast(module_function.ToLocalChecked())->Call(context, exports, 3, jsArgv);
}
// Remove this module and path from the stack
@ -571,8 +608,10 @@ 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(V8JS_SYM("exports"))) {
newobj = module->Get(V8JS_SYM("exports"));
v8::Local<v8::String> sym_exports = V8JS_SYM("exports");
if (module->Has(context, sym_exports).FromMaybe(false))
{
module->Get(context, sym_exports).ToLocal(&newobj);
}
c->modules_loaded[normalised_module_id].Reset(isolate, newobj);

View File

@ -35,8 +35,10 @@ extern "C" {
static void v8js_weak_object_callback(const v8::WeakCallbackInfo<zend_object> &data);
/* Callback for PHP methods and functions */
static void v8js_call_php_func(zend_object *object, zend_function *method_ptr, v8::Isolate *isolate, const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
static void v8js_call_php_func(zend_object *object, zend_function *method_ptr, const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Context> v8_context = isolate->GetEnteredContext();
v8::Local<v8::Value> return_value = V8JS_NULL;
zend_fcall_info fci;
zend_fcall_info_cache fcc;
@ -97,16 +99,24 @@ static void v8js_call_php_func(zend_object *object, zend_function *method_ptr, v
}
/* Convert parameters passed from V8 */
if (argc) {
if (argc)
{
fci.params = (zval *) safe_emalloc(argc, sizeof(zval), 0);
for (i = 0; i < argc; i++) {
if (info[i]->IsObject() && info[i]->ToObject(isolate)->InternalFieldCount() == 2) {
for (i = 0; i < argc; i++)
{
v8::Local<v8::Object> param_object;
if (info[i]->IsObject() && info[i]->ToObject(v8_context).ToLocal(&param_object) && param_object->InternalFieldCount() == 2)
{
/* This is a PHP object, passed to JS and back. */
zend_object *object = reinterpret_cast<zend_object *>(info[i]->ToObject(isolate)->GetAlignedPointerFromInternalField(1));
zend_object *object = reinterpret_cast<zend_object *>(param_object->GetAlignedPointerFromInternalField(1));
ZVAL_OBJ(&fci.params[i], object);
Z_ADDREF_P(&fci.params[i]);
} else {
if (v8js_to_zval(info[i], &fci.params[i], ctx->flags, isolate) == FAILURE) {
}
else
{
if (v8js_to_zval(info[i], &fci.params[i], ctx->flags, isolate) == FAILURE)
{
error_len = spprintf(&error, 0, "converting parameter #%d passed to %s() failed", i + 1, method_ptr->common.function_name);
if (error_len > std::numeric_limits<int>::max()) {
@ -190,7 +200,6 @@ failure:
/* Callback for PHP methods and functions */
void v8js_php_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
@ -203,7 +212,7 @@ void v8js_php_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ *
method_ptr = zend_get_closure_invoke_method(object);
}
return v8js_call_php_func(object, method_ptr, isolate, info);
return v8js_call_php_func(object, method_ptr, info);
}
/* Callback for PHP constructor calls */
@ -220,8 +229,16 @@ static void v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value>& i
}
v8::Local<v8::Array> cons_data = v8::Local<v8::Array>::Cast(info.Data());
v8::Local<v8::External> ext_tmpl = v8::Local<v8::External>::Cast(cons_data->Get(0));
v8::Local<v8::External> ext_ce = v8::Local<v8::External>::Cast(cons_data->Get(1));
v8::Local<v8::Value> cons_tmpl, cons_ce;
if (!cons_data->Get(isolate->GetEnteredContext(), 0).ToLocal(&cons_tmpl)
||!cons_data->Get(isolate->GetEnteredContext(), 1).ToLocal(&cons_ce))
{
return;
}
v8::Local<v8::External> ext_tmpl = v8::Local<v8::External>::Cast(cons_tmpl);
v8::Local<v8::External> ext_ce = v8::Local<v8::External>::Cast(cons_ce);
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
@ -257,7 +274,7 @@ static void v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value>& i
// Call __construct function
if (ctor_ptr != NULL) {
v8js_call_php_func(Z_OBJ(value), ctor_ptr, isolate, info);
v8js_call_php_func(Z_OBJ(value), ctor_ptr, info);
}
}
@ -311,13 +328,15 @@ static void v8js_weak_closure_callback(const v8::WeakCallbackInfo<v8js_function_
!strncasecmp(ZSTR_VAL(key), mname, ZSTR_LEN(key)))
#define PHP_V8JS_CALLBACK(isolate, mptr, tmpl) \
v8::FunctionTemplate::New((isolate), v8js_php_callback, v8::External::New((isolate), mptr), v8::Signature::New((isolate), tmpl))->GetFunction()
(v8::FunctionTemplate::New((isolate), v8js_php_callback, v8::External::New((isolate), mptr), v8::Signature::New((isolate), tmpl))->GetFunction(isolate->GetEnteredContext()).ToLocalChecked())
static void v8js_named_property_enumerator(const v8::PropertyCallbackInfo<v8::Array> &info) /* {{{ */
{
// note: 'special' properties like 'constructor' are not enumerated.
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Context> v8_context = isolate->GetEnteredContext();
v8::Local<v8::Object> self = info.Holder();
v8::Local<v8::Array> result = v8::Array::New(isolate, 0);
uint32_t result_len = 0;
@ -372,7 +391,7 @@ static void v8js_named_property_enumerator(const v8::PropertyCallbackInfo<v8::Ar
method_name = V8JS_STRL(ZSTR_VAL(method_ptr->common.function_name), static_cast<int>(ZSTR_LEN(method_ptr->common.function_name)));
}
result->Set(result_len++, method_name);
result->Set(v8_context, result_len++, method_name);
} ZEND_HASH_FOREACH_END();
/* enumerate all properties */
@ -400,11 +419,11 @@ static void v8js_named_property_enumerator(const v8::PropertyCallbackInfo<v8::Ar
char *prefixed = static_cast<char *>(emalloc(ZSTR_LEN(key) + 2));
prefixed[0] = '$';
strncpy(prefixed + 1, ZSTR_VAL(key), ZSTR_LEN(key) + 1);
result->Set(result_len++, V8JS_STRL(prefixed, static_cast<int>(ZSTR_LEN(key) + 1)));
result->Set(v8_context, result_len++, V8JS_STRL(prefixed, static_cast<int>(ZSTR_LEN(key) + 1)));
efree(prefixed);
} else {
// even numeric indices are enumerated as strings in JavaScript
result->Set(result_len++, V8JS_FLOAT((double) index)->ToString(isolate));
result->Set(v8_context, result_len++, V8JS_FLOAT((double) index)->ToString(v8_context).ToLocalChecked());
}
} ZEND_HASH_FOREACH_END();
@ -416,6 +435,8 @@ static void v8js_named_property_enumerator(const v8::PropertyCallbackInfo<v8::Ar
static void v8js_invoke_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Context> v8_context = isolate->GetEnteredContext();
v8::Local<v8::Object> self = info.Holder();
v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(info.Data());
int argc = info.Length(), i;
@ -430,9 +451,9 @@ static void v8js_invoke_callback(const v8::FunctionCallbackInfo<v8::Value>& info
if (info.IsConstructCall()) {
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
v8::MaybeLocal<v8::String> str = self->GetConstructorName()->ToString(isolate);
v8::String::Utf8Value str_value(isolate, str.ToLocalChecked());
zend_string *constructor_name = zend_string_init(ToCString(str_value), str.ToLocalChecked()->Utf8Length(isolate), 0);
v8::Local<v8::String> str = self->GetConstructorName()->ToString(v8_context).ToLocalChecked();
v8::String::Utf8Value str_value(isolate, str);
zend_string *constructor_name = zend_string_init(ToCString(str_value), str_value.length(), 0);
zend_class_entry *ce = zend_lookup_class(constructor_name);
zend_string_release(constructor_name);
@ -440,15 +461,13 @@ static void v8js_invoke_callback(const v8::FunctionCallbackInfo<v8::Value>& info
new_tpl = v8::Local<v8::FunctionTemplate>::New
(isolate, ctx->template_cache.at(ce->name));
v8::MaybeLocal<v8::Object> maybeResult = new_tpl->GetFunction()->NewInstance(isolate->GetEnteredContext(), argc, argv);
if (!maybeResult.IsEmpty()) {
result = maybeResult.ToLocalChecked();
} else {
v8::Local<v8::Function> fn;
if (!new_tpl->GetFunction(v8_context).ToLocal(&fn) || !fn->NewInstance(v8_context, argc, argv).ToLocal(&result))
{
result = V8JS_UNDEFINED;
}
} else {
result = cb->Call(self, argc, argv);
cb->Call(v8_context, self, argc, argv).ToLocal(&result);
}
info.GetReturnValue().Set(result);
@ -461,6 +480,8 @@ static void v8js_invoke_callback(const v8::FunctionCallbackInfo<v8::Value>& info
static void v8js_fake_call_impl(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Context> v8_context = isolate->GetEnteredContext();
v8::Local<v8::Object> self = info.Holder();
v8::Local<v8::Value> return_value = V8JS_NULL;
@ -529,7 +550,7 @@ static void v8js_fake_call_impl(const v8::FunctionCallbackInfo<v8::Value>& info)
return;
}
v8::MaybeLocal<v8::String> str = info[0]->ToString(isolate);
v8::MaybeLocal<v8::String> str = info[0]->ToString(v8_context);
if (str.IsEmpty())
{
@ -552,7 +573,7 @@ static void v8js_fake_call_impl(const v8::FunctionCallbackInfo<v8::Value>& info)
v8::String::Utf8Value str_value(isolate, str.ToLocalChecked());
const char *method_c_name = ToCString(str_value);
zend_string *method_name = zend_string_init(method_c_name, str.ToLocalChecked()->Utf8Length(isolate), 0);
zend_string *method_name = zend_string_init(method_c_name, str_value.length(), 0);
// okay, look up the method name and manually invoke it.
const zend_object_handlers *h = object->handlers;
@ -588,9 +609,9 @@ static void v8js_fake_call_impl(const v8::FunctionCallbackInfo<v8::Value>& info)
v8::Local<v8::Value> *argv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
for (i=0; i<argc; i++) {
new(&argv[i]) v8::Local<v8::Value>;
argv[i] = args->Get(i);
args->Get(v8_context, i).ToLocal(&argv[i]);
}
return_value = cb->Call(info.This(), (int) argc, argv);
cb->Call(v8_context, info.This(), (int) argc, argv).ToLocal(&return_value);
info.GetReturnValue().Set(return_value);
}
/* }}} */
@ -602,10 +623,11 @@ v8::Local<v8::Value> v8js_named_property_callback(v8::Local<v8::Name> property_n
v8::Local<v8::String> property = v8::Local<v8::String>::Cast(property_name);
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Context> v8_context = isolate->GetEnteredContext();
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
v8::String::Utf8Value cstr(isolate, property);
const char *name = ToCString(cstr);
uint name_len = property->Utf8Length(isolate);
uint name_len = cstr.length();
char *lower = estrndup(name, name_len);
zend_string *method_name;
@ -668,9 +690,13 @@ v8::Local<v8::Value> v8js_named_property_callback(v8::Local<v8::Name> property_n
v8js_function_tmpl_t *persistent_ft = &ctx->call_impls[tmpl_ptr];
persistent_ft->Reset(isolate, ft);
}
v8::Local<v8::Function> cb = ft->GetFunction();
cb->SetName(property);
ret_value = cb;
v8::Local<v8::Function> fn;
if (ft->GetFunction(v8_context).ToLocal(&fn))
{
fn->SetName(property);
ret_value = fn;
}
} else {
v8::Local<v8::FunctionTemplate> ft;
try {
@ -684,7 +710,7 @@ v8::Local<v8::Value> v8js_named_property_callback(v8::Local<v8::Name> property_n
v8js_function_tmpl_t *persistent_ft = &ctx->method_tmpls[method_ptr];
persistent_ft->Reset(isolate, ft);
}
ret_value = ft->GetFunction();
ft->GetFunction(v8_context).ToLocal(&ret_value);
}
}
} else if (callback_type == V8JS_PROP_QUERY) {
@ -849,6 +875,7 @@ static void v8js_named_property_deleter(v8::Local<v8::Name> property, const v8::
static v8::MaybeLocal<v8::Object> v8js_wrap_object(v8::Isolate *isolate, zend_class_entry *ce, zval *value) /* {{{ */
{
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, ctx->context);
v8::Local<v8::FunctionTemplate> new_tpl;
v8js_function_tmpl_t *persist_tpl_;
@ -938,14 +965,20 @@ static v8::MaybeLocal<v8::Object> v8js_wrap_object(v8::Isolate *isolate, zend_cl
zend_string_release(invoke_str);
}
v8::Local<v8::Array> call_handler_data = v8::Array::New(isolate, 2);
call_handler_data->Set(0, v8::External::New(isolate, persist_tpl_));
call_handler_data->Set(1, v8::External::New(isolate, ce));
call_handler_data->Set(v8_context, 0, v8::External::New(isolate, persist_tpl_));
call_handler_data->Set(v8_context, 1, v8::External::New(isolate, ce));
new_tpl->SetCallHandler(v8js_construct_callback, call_handler_data);
}
// Create v8 wrapper object
v8::Local<v8::Value> external = v8::External::New(isolate, Z_OBJ_P(value));
v8::MaybeLocal<v8::Object> newobj = new_tpl->GetFunction()->NewInstance(isolate->GetEnteredContext(), 1, &external);
v8::Local<v8::Function> constr;
if (!new_tpl->GetFunction(v8_context).ToLocal(&constr)) {
return v8::MaybeLocal<v8::Object>();
}
v8::MaybeLocal<v8::Object> newobj = constr->NewInstance(v8_context, 1, &external);
if (ce == zend_ce_closure && !newobj.IsEmpty()) {
// free uncached function template when object is freed
@ -965,6 +998,7 @@ static v8::Local<v8::Object> v8js_wrap_array_to_object(v8::Isolate *isolate, zva
zend_ulong index;
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, ctx->context);
v8::Local<v8::FunctionTemplate> new_tpl;
if(ctx->array_tmpl.IsEmpty()) {
@ -981,7 +1015,7 @@ static v8::Local<v8::Object> v8js_wrap_array_to_object(v8::Isolate *isolate, zva
new_tpl = v8::Local<v8::FunctionTemplate>::New(isolate, ctx->array_tmpl);
}
v8::Local<v8::Object> newobj = new_tpl->InstanceTemplate()->NewInstance();
v8::Local<v8::Object> newobj = new_tpl->InstanceTemplate()->NewInstance(v8_context).ToLocalChecked();
HashTable *myht = HASH_OF(value);
i = myht ? zend_hash_num_elements(myht) : 0;
@ -1012,7 +1046,7 @@ static v8::Local<v8::Object> v8js_wrap_array_to_object(v8::Isolate *isolate, zva
continue;
}
newobj->Set(V8JS_STRL(ZSTR_VAL(key), static_cast<int>(ZSTR_LEN(key))),
newobj->Set(v8_context, V8JS_STRL(ZSTR_VAL(key), static_cast<int>(ZSTR_LEN(key))),
zval_to_v8js(data, isolate));
} else {
if (index > std::numeric_limits<uint32_t>::max()) {
@ -1021,7 +1055,7 @@ static v8::Local<v8::Object> v8js_wrap_array_to_object(v8::Isolate *isolate, zva
continue;
}
newobj->Set(static_cast<uint32_t>(index), zval_to_v8js(data, isolate));
newobj->Set(v8_context, static_cast<uint32_t>(index), zval_to_v8js(data, isolate));
}
} ZEND_HASH_FOREACH_END();

View File

@ -275,53 +275,67 @@ void v8js_terminate_execution(v8::Isolate *isolate) /* {{{ */
int v8js_get_properties_hash(v8::Local<v8::Value> jsValue, HashTable *retval, int flags, v8::Isolate *isolate) /* {{{ */
{
v8::Local<v8::Object> jsObj = jsValue->ToObject(isolate);
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, ctx->context);
if (!jsObj.IsEmpty()) {
v8::Local<v8::Array> jsKeys = jsObj->GetPropertyNames();
for (unsigned i = 0; i < jsKeys->Length(); i++)
{
v8::Local<v8::String> jsKey = jsKeys->Get(i)->ToString(isolate);
/* Skip any prototype properties */
if (!jsObj->HasOwnProperty(isolate->GetEnteredContext(), jsKey).FromMaybe(false)
&& !jsObj->HasRealNamedProperty(jsKey)
&& !jsObj->HasRealNamedCallbackProperty(jsKey)) {
continue;
}
v8::Local<v8::Value> jsVal = jsObj->Get(jsKey);
v8::String::Utf8Value cstr(isolate, jsKey);
const char *c_key = ToCString(cstr);
zend_string *key = zend_string_init(c_key, jsKey->ToString(isolate)->Utf8Length(isolate), 0);
zval value;
ZVAL_UNDEF(&value);
if (jsVal->IsObject() && jsVal->ToObject(isolate)->InternalFieldCount() == 2) {
/* This is a PHP object, passed to JS and back. */
zend_object *object = reinterpret_cast<zend_object *>(jsVal->ToObject(isolate)->GetAlignedPointerFromInternalField(1));
ZVAL_OBJ(&value, object);
Z_ADDREF_P(&value);
}
else {
if (v8js_to_zval(jsVal, &value, flags, isolate) == FAILURE) {
zval_ptr_dtor(&value);
return FAILURE;
}
}
if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
zend_symtable_update(retval, key, &value);
} else {
zend_hash_update(retval, key, &value);
}
zend_string_release(key);
}
return SUCCESS;
v8::Local<v8::Object> jsObj;
v8::Local<v8::Array> jsKeys;
if (!jsValue->ToObject(v8_context).ToLocal(&jsObj)
|| !jsObj->GetPropertyNames(v8_context).ToLocal(&jsKeys)) {
return FAILURE;
}
return FAILURE;
for (unsigned i = 0; i < jsKeys->Length(); i++)
{
v8::Local<v8::Value> jsKeySlot;
v8::Local<v8::String> jsKey;
if (!jsKeys->Get(v8_context, i).ToLocal(&jsKeySlot)
|| !jsKeySlot->ToString(v8_context).ToLocal(&jsKey)) {
continue;
}
/* Skip any prototype properties */
if (!jsObj->HasOwnProperty(isolate->GetEnteredContext(), jsKey).FromMaybe(false)
&& !jsObj->HasRealNamedProperty(v8_context, jsKey).FromMaybe(false)
&& !jsObj->HasRealNamedCallbackProperty(v8_context, jsKey).FromMaybe(false)) {
continue;
}
v8::Local<v8::Value> jsVal;
if (!jsObj->Get(v8_context, jsKey).ToLocal(&jsVal)) {
continue;
}
v8::String::Utf8Value cstr(isolate, jsKey);
zend_string *key = zend_string_init(ToCString(cstr), cstr.length(), 0);
zval value;
ZVAL_UNDEF(&value);
v8::Local<v8::Object> jsValObject;
if (jsVal->IsObject() && jsVal->ToObject(v8_context).ToLocal(&jsValObject) && jsValObject->InternalFieldCount() == 2) {
/* This is a PHP object, passed to JS and back. */
zend_object *object = reinterpret_cast<zend_object *>(jsValObject->GetAlignedPointerFromInternalField(1));
ZVAL_OBJ(&value, object);
Z_ADDREF_P(&value);
}
else {
if (v8js_to_zval(jsVal, &value, flags, isolate) == FAILURE) {
zval_ptr_dtor(&value);
return FAILURE;
}
}
if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
zend_symtable_update(retval, key, &value);
} else {
zend_hash_update(retval, key, &value);
}
zend_string_release(key);
}
return SUCCESS;
}
/* }}} */

View File

@ -17,19 +17,18 @@
#include <functional>
/* Helper macros */
#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_ZSYM(v) v8::String::NewFromUtf8(isolate, ZSTR_VAL(v), v8::String::kInternalizedString, ZSTR_LEN(v))
#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_ZSTR(v) v8::String::NewFromUtf8(isolate, ZSTR_VAL(v), v8::String::kNormalString, ZSTR_LEN(v))
#define V8JS_SYM(v) (v8::String::NewFromUtf8(isolate, v, v8::NewStringType::kInternalized, sizeof(v) - 1).ToLocalChecked())
#define V8JS_SYML(v, l) (v8::String::NewFromUtf8(isolate, v, v8::NewStringType::kInternalized, l).ToLocalChecked())
#define V8JS_ZSYM(v) (v8::String::NewFromUtf8(isolate, ZSTR_VAL(v), v8::NewStringType::kInternalized, ZSTR_LEN(v)).ToLocalChecked())
#define V8JS_STR(v) (v8::String::NewFromUtf8(isolate, v, v8::NewStringType::kNormal).ToLocalChecked())
#define V8JS_STRL(v, l) (v8::String::NewFromUtf8(isolate, v, v8::NewStringType::kNormal, l).ToLocalChecked())
#define V8JS_ZSTR(v) (v8::String::NewFromUtf8(isolate, ZSTR_VAL(v), v8::NewStringType::kNormal, ZSTR_LEN(v)).ToLocalChecked())
#define V8JS_INT(v) v8::Integer::New(isolate, v)
#define V8JS_UINT(v) v8::Integer::NewFromUnsigned(isolate, v)
#define V8JS_FLOAT(v) v8::Number::New(isolate, v)
#define V8JS_BOOL(v) ((v)?v8::True(isolate):v8::False(isolate))
#define V8JS_TRUE() v8::True(isolate)
#define V8JS_FALSE() v8::False(isolate)
#define V8JS_DATE(v) v8::Date::New(isolate, v)
#define V8JS_NULL v8::Null(isolate)
#define V8JS_UNDEFINED v8::Undefined(isolate)
#define V8JS_MN(name) v8js_method_##name

View File

@ -59,58 +59,64 @@ static int v8js_v8object_has_property(zval *object, zval *member, int has_set_ex
if (!obj->ctx) {
zend_throw_exception(php_ce_v8js_exception,
"Can't access V8Object after V8Js instance is destroyed!", 0);
return retval;
return false;
}
V8JS_CTX_PROLOGUE_EX(obj->ctx, retval);
V8JS_CTX_PROLOGUE_EX(obj->ctx, false);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
v8::Local<v8::Object> jsObj;
if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject())
{
if (Z_STRLEN_P(member) > std::numeric_limits<int>::max()) {
zend_throw_exception(php_ce_v8js_exception,
"Member name length exceeds maximum supported length", 0);
return retval;
}
if (Z_TYPE_P(member) != IS_STRING || !v8obj->IsObject() || !v8obj->ToObject(v8_context).ToLocal(&jsObj)) {
return false;
}
v8::Local<v8::Object> jsObj = v8obj->ToObject(isolate);
v8::Local<v8::String> jsKey = V8JS_STRL(Z_STRVAL_P(member), static_cast<int>(Z_STRLEN_P(member)));
v8::Local<v8::Value> jsVal;
if (Z_STRLEN_P(member) > std::numeric_limits<int>::max()) {
zend_throw_exception(php_ce_v8js_exception,
"Member name length exceeds maximum supported length", 0);
return false;
}
/* Skip any prototype properties */
if (jsObj->HasRealNamedProperty(jsKey) || jsObj->HasRealNamedCallbackProperty(jsKey)) {
if (has_set_exists == 2) {
/* property_exists(), that's enough! */
retval = true;
} else {
/* We need to look at the value. */
jsVal = jsObj->Get(jsKey);
if (has_set_exists == 0 ) {
/* isset(): We make 'undefined' equivalent to 'null' */
retval = !( jsVal->IsNull() || jsVal->IsUndefined() );
} else {
/* empty() */
retval = jsVal->BooleanValue(v8_context).FromMaybe(false);
/* for PHP compatibility, [] should also be empty */
if (jsVal->IsArray() && retval) {
v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(jsVal);
retval = (array->Length() != 0);
}
/* for PHP compatibility, '0' should also be empty */
if (jsVal->IsString() && retval) {
v8::MaybeLocal<v8::String> str = jsVal->ToString(isolate);
if (!str.IsEmpty() && str.ToLocalChecked()->Length() == 1) {
uint16_t c = 0;
str.ToLocalChecked()->Write(isolate, &c, 0, 1);
if (c == '0') {
retval = false;
}
}
}
}
}
v8::Local<v8::String> jsKey = V8JS_ZSYM(Z_STR_P(member));
/* Skip any prototype properties */
if (!jsObj->HasRealNamedProperty(v8_context, jsKey).FromMaybe(false)
&& !jsObj->HasRealNamedCallbackProperty(v8_context, jsKey).FromMaybe(false)) {
return false;
}
if (has_set_exists == 2) {
/* property_exists(), that's enough! */
return true;
}
/* We need to look at the value. */
v8::Local<v8::Value> jsVal = jsObj->Get(v8_context, jsKey).ToLocalChecked();
if (has_set_exists == 0 ) {
/* isset(): We make 'undefined' equivalent to 'null' */
return !( jsVal->IsNull() || jsVal->IsUndefined() );
}
/* empty() */
retval = jsVal->BooleanValue(v8_context).FromMaybe(false);
/* for PHP compatibility, [] should also be empty */
if (jsVal->IsArray() && retval) {
v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(jsVal);
retval = (array->Length() != 0);
}
/* for PHP compatibility, '0' should also be empty */
v8::Local<v8::String> str;
if (jsVal->IsString() && retval && jsVal->ToString(v8_context).ToLocal(&str)
&& str->Length() == 1) {
uint16_t c = 0;
str->Write(isolate, &c, 0, 1);
if (c == '0') {
retval = false;
}
}
return retval;
}
/* }}} */
@ -137,15 +143,15 @@ static zval *v8js_v8object_read_property(zval *object, zval *member, int type, v
return retval;
}
v8::Local<v8::Object> jsObj = v8obj->ToObject(isolate);
v8::Local<v8::String> jsKey = V8JS_STRL(Z_STRVAL_P(member), static_cast<int>(Z_STRLEN_P(member)));
v8::Local<v8::Value> jsVal;
v8::Local<v8::String> jsKey = V8JS_ZSYM(Z_STR_P(member));
v8::Local<v8::Object> jsObj = v8obj->ToObject(v8_context).ToLocalChecked();
/* Skip any prototype properties */
if (jsObj->HasRealNamedProperty(jsKey) || jsObj->HasRealNamedCallbackProperty(jsKey)) {
jsVal = jsObj->Get(jsKey);
if (jsObj->HasRealNamedProperty(v8_context, jsKey).FromMaybe(false)
|| jsObj->HasRealNamedCallbackProperty(v8_context, jsKey).FromMaybe(false)) {
v8::MaybeLocal<v8::Value> jsVal = jsObj->Get(v8_context, jsKey);
if (v8js_to_zval(jsVal, retval, obj->flags, isolate) == SUCCESS) {
if (!jsVal.IsEmpty() && v8js_to_zval(jsVal.ToLocalChecked(), retval, obj->flags, isolate) == SUCCESS) {
return retval;
}
}
@ -166,7 +172,7 @@ static void v8js_v8object_write_property(zval *object, zval *member, zval *value
}
V8JS_CTX_PROLOGUE(obj->ctx);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
v8::Local<v8::Value> v8objHandle = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (Z_STRLEN_P(member) > std::numeric_limits<int>::max()) {
zend_throw_exception(php_ce_v8js_exception,
@ -174,8 +180,9 @@ static void v8js_v8object_write_property(zval *object, zval *member, zval *value
return;
}
if (v8obj->IsObject()) {
v8obj->ToObject(isolate)->CreateDataProperty(v8_context, V8JS_SYML(Z_STRVAL_P(member), static_cast<int>(Z_STRLEN_P(member))), zval_to_v8js(value, isolate));
v8::Local<v8::Object> v8obj;
if (v8objHandle->IsObject() && v8objHandle->ToObject(v8_context).ToLocal(&v8obj)) {
v8obj->CreateDataProperty(v8_context, V8JS_ZSYM(Z_STR_P(member)), zval_to_v8js(value, isolate));
}
}
/* }}} */
@ -191,7 +198,7 @@ static void v8js_v8object_unset_property(zval *object, zval *member, void **cach
}
V8JS_CTX_PROLOGUE(obj->ctx);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
v8::Local<v8::Value> v8objHandle = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (Z_STRLEN_P(member) > std::numeric_limits<int>::max()) {
zend_throw_exception(php_ce_v8js_exception,
@ -199,8 +206,9 @@ static void v8js_v8object_unset_property(zval *object, zval *member, void **cach
return;
}
if (v8obj->IsObject()) {
v8obj->ToObject(isolate)->Delete(V8JS_SYML(Z_STRVAL_P(member), static_cast<int>(Z_STRLEN_P(member))));
v8::Local<v8::Object> v8obj;
if (v8objHandle->IsObject() && v8objHandle->ToObject(v8_context).ToLocal(&v8obj)) {
v8obj->Delete(v8_context, V8JS_ZSYM(Z_STR_P(member)));
}
}
/* }}} */
@ -276,9 +284,13 @@ static zend_function *v8js_v8object_get_method(zend_object **object_ptr, zend_st
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(isolate);
if (jsObj->Has(jsKey) && jsObj->Get(jsKey)->IsFunction()) {
v8::Local<v8::Object> jsObj;
v8::Local<v8::Value> jsObjSlot;
if (v8obj->ToObject(v8_context).ToLocal(&jsObj)
&& jsObj->Has(v8_context, jsKey).FromMaybe(false)
&& jsObj->Get(v8_context, jsKey).ToLocal(&jsObjSlot)
&& jsObjSlot->IsFunction()) {
f = (zend_function *) ecalloc(1, sizeof(*f));
f->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
f->common.function_name = zend_string_copy(method);
@ -324,16 +336,22 @@ static int v8js_v8object_call_method(zend_string *method, zend_object *object, I
std::function< v8::MaybeLocal<v8::Value>(v8::Isolate *) > v8_call = [obj, method, argc, argv, object, &return_value](v8::Isolate *isolate) {
int i = 0;
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, obj->ctx->context);
v8::Local<v8::Context> v8_context = isolate->GetEnteredContext();
v8::Local<v8::String> method_name = V8JS_SYML(ZSTR_VAL(method), static_cast<int>(ZSTR_LEN(method)));
v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj)->ToObject(isolate);
v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj)->ToObject(v8_context).ToLocalChecked();
v8::Local<v8::Object> thisObj;
v8::Local<v8::Function> cb;
if (method_name->Equals(context, V8JS_SYM(V8JS_V8_INVOKE_FUNC_NAME)).FromMaybe(false)) {
if (method_name->Equals(v8_context, V8JS_SYM(V8JS_V8_INVOKE_FUNC_NAME)).FromMaybe(false)) {
cb = v8::Local<v8::Function>::Cast(v8obj);
} else {
cb = v8::Local<v8::Function>::Cast(v8obj->Get(method_name));
v8::Local<v8::Value> slot;
if (!v8obj->Get(v8_context, method_name).ToLocal(&slot)) {
return v8::MaybeLocal<v8::Value>();
}
cb = v8::Local<v8::Function>::Cast(slot);
}
// If a method is invoked on V8Object, then set the object itself as
@ -352,13 +370,13 @@ static int v8js_v8object_call_method(zend_string *method, zend_object *object, I
jsArgv[i] = v8::Local<v8::Value>::New(isolate, zval_to_v8js(&argv[i], isolate));
}
v8::Local<v8::Value> result = cb->Call(thisObj, argc, jsArgv);
v8::MaybeLocal<v8::Value> result = cb->Call(v8_context, thisObj, argc, jsArgv);
if (obj->std.ce == php_ce_v8object && result->StrictEquals(thisObj)) {
if (obj->std.ce == php_ce_v8object && !result.IsEmpty() && result.ToLocalChecked()->StrictEquals(thisObj)) {
/* JS code did "return this", retain object identity */
ZVAL_OBJ(return_value, object);
zval_copy_ctor(return_value);
result.Clear();
result = v8::MaybeLocal<v8::Value>();
}
return result;
@ -547,26 +565,28 @@ static void v8js_v8generator_next(v8js_v8generator *g) /* {{{ */
* some memory on bailout. */
{
std::function< v8::MaybeLocal<v8::Value>(v8::Isolate *) > v8_call = [g](v8::Isolate *isolate) {
v8::Local<v8::String> method_name = V8JS_STR("next");
v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, g->v8obj.v8obj)->ToObject(isolate);
v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(v8obj->Get(method_name));;
v8::Local<v8::Context> v8_context = isolate->GetEnteredContext();
v8::Local<v8::String> method_name = V8JS_SYM("next");
v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, g->v8obj.v8obj)->ToObject(v8_context).ToLocalChecked();
v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(v8obj->Get(v8_context, method_name).ToLocalChecked());;
v8::Local<v8::Value> result = cb->Call(v8obj, 0, NULL);
v8::MaybeLocal<v8::Value> result = cb->Call(v8_context, v8obj, 0, NULL);
if(result.IsEmpty()) {
/* cb->Call probably threw (and already threw a zend exception), just return */
return V8JS_NULL;
}
if(!result->IsObject()) {
if(!result.ToLocalChecked()->IsObject()) {
zend_throw_exception(php_ce_v8js_exception,
"V8Generator returned non-object on next()", 0);
return V8JS_NULL;
}
v8::Local<v8::Object> resultObj = result->ToObject(isolate);
v8::Local<v8::Value> val = resultObj->Get(V8JS_STR("value"));
v8::Local<v8::Value> done = resultObj->Get(V8JS_STR("done"));
v8::Local<v8::Object> resultObj = result.ToLocalChecked()->ToObject(v8_context).ToLocalChecked();
v8::Local<v8::Value> val = resultObj->Get(v8_context, V8JS_SYM("value")).ToLocalChecked();
v8::Local<v8::Value> done = resultObj->Get(v8_context, V8JS_SYM("done")).ToLocalChecked();
zval_ptr_dtor(&g->value);
v8js_to_zval(val, &g->value, 0, isolate);