0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-10-18 11:18:40 +00:00

support both V8 10.x and 12.x

This commit is contained in:
Stefan Siegl 2024-09-28 18:09:43 +02:00
parent d0c4a3614d
commit 4d4eccb3ad
6 changed files with 71 additions and 33 deletions

View File

@ -56,7 +56,7 @@ static zval v8js_array_access_dispatch(zend_object *object, const char *method_n
v8::Intercepted v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */ V8JS_INTERCEPTED v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
{ {
v8::Isolate *isolate = info.GetIsolate(); v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder(); v8::Local<v8::Object> self = info.Holder();
@ -71,16 +71,16 @@ v8::Intercepted v8js_array_access_getter(uint32_t index, const v8::PropertyCallb
zval_ptr_dtor(&php_value); zval_ptr_dtor(&php_value);
if (ret_value.IsEmpty()) { if (ret_value.IsEmpty()) {
return v8::Intercepted::kNo; return V8JS_INTERCEPTED_NO;
} else { } else {
info.GetReturnValue().Set(ret_value); info.GetReturnValue().Set(ret_value);
return v8::Intercepted::kYes; return V8JS_INTERCEPTED_YES;
} }
} }
/* }}} */ /* }}} */
v8::Intercepted v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value, V8JS_INTERCEPTED v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info) /* {{{ */ const V8JS_SETTER_PROPERTY_CALLBACK_INFO &info) /* {{{ */
{ {
v8::Isolate *isolate = info.GetIsolate(); v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder(); v8::Local<v8::Object> self = info.Holder();
@ -91,16 +91,22 @@ v8::Intercepted v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> va
ZVAL_UNDEF(&zvalue); ZVAL_UNDEF(&zvalue);
if (v8js_to_zval(value, &zvalue, 0, isolate) != SUCCESS) { if (v8js_to_zval(value, &zvalue, 0, isolate) != SUCCESS) {
return v8::Intercepted::kNo; return V8JS_INTERCEPTED_NO;
} }
zval php_value = v8js_array_access_dispatch(object, "offsetSet", 2, index, zvalue); zval php_value = v8js_array_access_dispatch(object, "offsetSet", 2, index, zvalue);
zval_ptr_dtor(&php_value); zval_ptr_dtor(&php_value);
#if !PHP_V8_HAS_INTERCEPTED
/* simply pass back the value to tell we intercepted the call
* as the offsetSet function returns void. */
info.GetReturnValue().Set(value);
#endif
/* if PHP wanted to hold on to this value, zend_call_function would /* if PHP wanted to hold on to this value, zend_call_function would
* have bumped the refcount. */ * have bumped the refcount. */
zval_ptr_dtor(&zvalue); zval_ptr_dtor(&zvalue);
return v8::Intercepted::kYes; return V8JS_INTERCEPTED_YES;
} }
/* }}} */ /* }}} */
@ -160,7 +166,7 @@ static void v8js_array_access_length(v8::Local<v8::String> property, const v8::P
} }
/* }}} */ /* }}} */
v8::Intercepted v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean>& info) /* {{{ */ V8JS_INTERCEPTED v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean>& info) /* {{{ */
{ {
v8::Isolate *isolate = info.GetIsolate(); v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder(); v8::Local<v8::Object> self = info.Holder();
@ -174,11 +180,11 @@ v8::Intercepted v8js_array_access_deleter(uint32_t index, const v8::PropertyCall
zval_ptr_dtor(&php_value); zval_ptr_dtor(&php_value);
info.GetReturnValue().Set(V8JS_BOOL(true)); info.GetReturnValue().Set(V8JS_BOOL(true));
return v8::Intercepted::kYes; return V8JS_INTERCEPTED_YES;
} }
/* }}} */ /* }}} */
v8::Intercepted v8js_array_access_query(uint32_t index, const v8::PropertyCallbackInfo<v8::Integer>& info) /* {{{ */ V8JS_INTERCEPTED v8js_array_access_query(uint32_t index, const v8::PropertyCallbackInfo<v8::Integer>& info) /* {{{ */
{ {
v8::Isolate *isolate = info.GetIsolate(); v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder(); v8::Local<v8::Object> self = info.Holder();
@ -189,10 +195,10 @@ v8::Intercepted v8js_array_access_query(uint32_t index, const v8::PropertyCallba
* otherwise we're expected to return an empty handle. */ * otherwise we're expected to return an empty handle. */
if(v8js_array_access_isset_p(object, index)) { if(v8js_array_access_isset_p(object, index)) {
info.GetReturnValue().Set(V8JS_UINT(v8::PropertyAttribute::None)); info.GetReturnValue().Set(V8JS_UINT(v8::PropertyAttribute::None));
return v8::Intercepted::kYes; return V8JS_INTERCEPTED_YES;
} }
return v8::Intercepted::kNo; return V8JS_INTERCEPTED_NO;
} }
/* }}} */ /* }}} */
@ -222,7 +228,7 @@ void v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& inf
v8::Intercepted v8js_array_access_named_getter(v8::Local<v8::Name> property_name, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */ V8JS_INTERCEPTED v8js_array_access_named_getter(v8::Local<v8::Name> property_name, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
{ {
v8::Local<v8::String> property = v8::Local<v8::String>::Cast(property_name); v8::Local<v8::String> property = v8::Local<v8::String>::Cast(property_name);
v8::Isolate *isolate = info.GetIsolate(); v8::Isolate *isolate = info.GetIsolate();
@ -231,7 +237,7 @@ v8::Intercepted v8js_array_access_named_getter(v8::Local<v8::Name> property_name
if(strcmp(name, "length") == 0) { if(strcmp(name, "length") == 0) {
v8js_array_access_length(property, info); v8js_array_access_length(property, info);
return v8::Intercepted::kYes; return V8JS_INTERCEPTED_YES;
} }
v8::Local<v8::Value> ret_value = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_GETTER); v8::Local<v8::Value> ret_value = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_GETTER);
@ -255,7 +261,7 @@ v8::Intercepted v8js_array_access_named_getter(v8::Local<v8::Name> property_name
} }
info.GetReturnValue().Set(ret_value); info.GetReturnValue().Set(ret_value);
return v8::Intercepted::kYes; return V8JS_INTERCEPTED_YES;
} }
/* }}} */ /* }}} */

View File

@ -14,18 +14,18 @@
#define V8JS_ARRAY_ACCESS_H #define V8JS_ARRAY_ACCESS_H
/* Indexed Property Handlers */ /* Indexed Property Handlers */
v8::Intercepted v8js_array_access_getter(uint32_t index, V8JS_INTERCEPTED v8js_array_access_getter(uint32_t index,
const v8::PropertyCallbackInfo<v8::Value>& info); const v8::PropertyCallbackInfo<v8::Value>& info);
v8::Intercepted v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value, V8JS_INTERCEPTED v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info); const V8JS_SETTER_PROPERTY_CALLBACK_INFO &info);
void v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& info); void v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& info);
v8::Intercepted v8js_array_access_deleter(uint32_t index, V8JS_INTERCEPTED v8js_array_access_deleter(uint32_t index,
const v8::PropertyCallbackInfo<v8::Boolean>& info); const v8::PropertyCallbackInfo<v8::Boolean>& info);
v8::Intercepted v8js_array_access_query(uint32_t index, V8JS_INTERCEPTED v8js_array_access_query(uint32_t index,
const v8::PropertyCallbackInfo<v8::Integer>& info); const v8::PropertyCallbackInfo<v8::Integer>& info);
/* Named Property Handlers */ /* Named Property Handlers */
v8::Intercepted v8js_array_access_named_getter(v8::Local<v8::Name> property, V8JS_INTERCEPTED v8js_array_access_named_getter(v8::Local<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Value> &info); const v8::PropertyCallbackInfo<v8::Value> &info);
#endif /* V8JS_ARRAY_ACCESS_H */ #endif /* V8JS_ARRAY_ACCESS_H */

View File

@ -524,7 +524,11 @@ static void v8js_compile_script(zval *this_ptr, const zend_string *str, const ze
v8::Local<v8::String> sname = identifier v8::Local<v8::String> sname = identifier
? V8JS_ZSTR(identifier) ? V8JS_ZSTR(identifier)
: V8JS_SYM("V8Js::compileString()"); : V8JS_SYM("V8Js::compileString()");
#if PHP_V8_API_VERSION >= 12002000
v8::ScriptOrigin origin(sname); v8::ScriptOrigin origin(sname);
#else
v8::ScriptOrigin origin(c->isolate, sname);
#endif
if (ZSTR_LEN(str) > std::numeric_limits<int>::max()) { if (ZSTR_LEN(str) > std::numeric_limits<int>::max()) {
zend_throw_exception(php_ce_v8js_exception, zend_throw_exception(php_ce_v8js_exception,

View File

@ -502,7 +502,11 @@ V8JS_METHOD(require)
// Set script identifier // Set script identifier
v8::Local<v8::String> sname = V8JS_STR(normalised_module_id); v8::Local<v8::String> sname = V8JS_STR(normalised_module_id);
#if PHP_V8_API_VERSION >= 12002000
v8::ScriptOrigin origin(sname); v8::ScriptOrigin origin(sname);
#else
v8::ScriptOrigin origin(c->isolate, sname);
#endif
if (Z_STRLEN(module_code) > std::numeric_limits<int>::max()) { if (Z_STRLEN(module_code) > std::numeric_limits<int>::max()) {
zend_throw_exception(php_ce_v8js_exception, zend_throw_exception(php_ce_v8js_exception,

View File

@ -857,58 +857,62 @@ v8::Local<v8::Value> v8js_named_property_callback(v8::Isolate *isolate, v8::Loca
} }
/* }}} */ /* }}} */
static v8::Intercepted v8js_named_property_getter(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */ static V8JS_INTERCEPTED v8js_named_property_getter(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
{ {
v8::Local<v8::Value> r = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_GETTER); v8::Local<v8::Value> r = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_GETTER);
if (r.IsEmpty()) { if (r.IsEmpty()) {
return v8::Intercepted::kNo; return V8JS_INTERCEPTED_NO;
} else { } else {
info.GetReturnValue().Set(r); info.GetReturnValue().Set(r);
return v8::Intercepted::kYes; return V8JS_INTERCEPTED_YES;
} }
} }
/* }}} */ /* }}} */
static v8::Intercepted v8js_named_property_setter(v8::Local<v8::Name> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void> &info) /* {{{ */ static V8JS_INTERCEPTED v8js_named_property_setter(v8::Local<v8::Name> property, v8::Local<v8::Value> value, const V8JS_SETTER_PROPERTY_CALLBACK_INFO &info) /* {{{ */
{ {
v8::Local<v8::Value> r = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_SETTER, value); v8::Local<v8::Value> r = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_SETTER, value);
#if PHP_V8_HAS_INTERCEPTED
return r.IsEmpty() ? v8::Intercepted::kNo : v8::Intercepted::kYes; return r.IsEmpty() ? v8::Intercepted::kNo : v8::Intercepted::kYes;
#else
info.GetReturnValue().Set(r);
#endif
} }
/* }}} */ /* }}} */
static v8::Intercepted v8js_named_property_query(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Integer> &info) /* {{{ */ static V8JS_INTERCEPTED v8js_named_property_query(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Integer> &info) /* {{{ */
{ {
v8::Local<v8::Value> r = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_QUERY); v8::Local<v8::Value> r = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_QUERY);
if (r.IsEmpty()) { if (r.IsEmpty()) {
return v8::Intercepted::kNo; return V8JS_INTERCEPTED_NO;
} }
v8::Isolate *isolate = info.GetIsolate(); v8::Isolate *isolate = info.GetIsolate();
v8::MaybeLocal<v8::Integer> value = r->ToInteger(isolate->GetEnteredOrMicrotaskContext()); v8::MaybeLocal<v8::Integer> value = r->ToInteger(isolate->GetEnteredOrMicrotaskContext());
if (value.IsEmpty()) { if (value.IsEmpty()) {
return v8::Intercepted::kNo; return V8JS_INTERCEPTED_NO;
} else { } else {
info.GetReturnValue().Set(value.ToLocalChecked()); info.GetReturnValue().Set(value.ToLocalChecked());
return v8::Intercepted::kYes; return V8JS_INTERCEPTED_YES;
} }
} }
/* }}} */ /* }}} */
static v8::Intercepted v8js_named_property_deleter(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Boolean> &info) /* {{{ */ static V8JS_INTERCEPTED v8js_named_property_deleter(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Boolean> &info) /* {{{ */
{ {
v8::Local<v8::Value> r = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_DELETER); v8::Local<v8::Value> r = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_DELETER);
if (r.IsEmpty()) { if (r.IsEmpty()) {
return v8::Intercepted::kNo; return V8JS_INTERCEPTED_NO;
} }
v8::Isolate *isolate = info.GetIsolate(); v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Boolean> value = r->ToBoolean(isolate); v8::Local<v8::Boolean> value = r->ToBoolean(isolate);
if (value.IsEmpty()) { if (value.IsEmpty()) {
return v8::Intercepted::kNo; return V8JS_INTERCEPTED_NO;
} else { } else {
info.GetReturnValue().Set(value); info.GetReturnValue().Set(value);
return v8::Intercepted::kYes; return V8JS_INTERCEPTED_YES;
} }
} }
/* }}} */ /* }}} */
@ -951,7 +955,11 @@ static v8::MaybeLocal<v8::Object> v8js_wrap_object(v8::Isolate *isolate, zend_cl
/* We'll free persist_tpl_ when template_cache is destroyed */ /* We'll free persist_tpl_ when template_cache is destroyed */
v8::Local<v8::ObjectTemplate> inst_tpl = new_tpl->InstanceTemplate(); v8::Local<v8::ObjectTemplate> inst_tpl = new_tpl->InstanceTemplate();
#if PHP_V8_HAS_INTERCEPTED
v8::NamedPropertyGetterCallback getter = v8js_named_property_getter; v8::NamedPropertyGetterCallback getter = v8js_named_property_getter;
#else
v8::GenericNamedPropertyGetterCallback getter = v8js_named_property_getter;
#endif
v8::GenericNamedPropertyEnumeratorCallback enumerator = v8js_named_property_enumerator; v8::GenericNamedPropertyEnumeratorCallback enumerator = v8js_named_property_enumerator;
/* Check for ArrayAccess object */ /* Check for ArrayAccess object */

View File

@ -113,6 +113,22 @@ int v8js_get_properties_hash(v8::Local<v8::Value> jsValue, HashTable *retval, in
#define IS_LONG 99 #define IS_LONG 99
#endif #endif
#define PHP_V8_HAS_INTERCEPTED PHP_V8_API_VERSION >= 12005000
#if PHP_V8_HAS_INTERCEPTED
#define V8JS_INTERCEPTED v8::Intercepted
#define V8JS_INTERCEPTED_YES v8::Intercepted::kYes
#define V8JS_INTERCEPTED_NO v8::Intercepted::kNo
#define V8JS_SETTER_PROPERTY_CALLBACK_INFO v8::PropertyCallbackInfo<void>
#else
#define V8JS_INTERCEPTED void
#define V8JS_INTERCEPTED_YES
#define V8JS_INTERCEPTED_NO
#define V8JS_SETTER_PROPERTY_CALLBACK_INFO v8::PropertyCallbackInfo<v8::Value>
#endif
#endif /* V8JS_V8_H */ #endif /* V8JS_V8_H */