Skip to content

Commit 4d4eccb

Browse files
committed
support both V8 10.x and 12.x
1 parent d0c4a36 commit 4d4eccb

6 files changed

+71
-33
lines changed

v8js_array_access.cc

+21-15
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static zval v8js_array_access_dispatch(zend_object *object, const char *method_n
5656

5757

5858

59-
v8::Intercepted v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
59+
V8JS_INTERCEPTED v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
6060
{
6161
v8::Isolate *isolate = info.GetIsolate();
6262
v8::Local<v8::Object> self = info.Holder();
@@ -71,16 +71,16 @@ v8::Intercepted v8js_array_access_getter(uint32_t index, const v8::PropertyCallb
7171
zval_ptr_dtor(&php_value);
7272

7373
if (ret_value.IsEmpty()) {
74-
return v8::Intercepted::kNo;
74+
return V8JS_INTERCEPTED_NO;
7575
} else {
7676
info.GetReturnValue().Set(ret_value);
77-
return v8::Intercepted::kYes;
77+
return V8JS_INTERCEPTED_YES;
7878
}
7979
}
8080
/* }}} */
8181

82-
v8::Intercepted v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
83-
const v8::PropertyCallbackInfo<void>& info) /* {{{ */
82+
V8JS_INTERCEPTED v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
83+
const V8JS_SETTER_PROPERTY_CALLBACK_INFO &info) /* {{{ */
8484
{
8585
v8::Isolate *isolate = info.GetIsolate();
8686
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
9191
ZVAL_UNDEF(&zvalue);
9292

9393
if (v8js_to_zval(value, &zvalue, 0, isolate) != SUCCESS) {
94-
return v8::Intercepted::kNo;
94+
return V8JS_INTERCEPTED_NO;
9595
}
9696

9797
zval php_value = v8js_array_access_dispatch(object, "offsetSet", 2, index, zvalue);
9898
zval_ptr_dtor(&php_value);
9999

100+
#if !PHP_V8_HAS_INTERCEPTED
101+
/* simply pass back the value to tell we intercepted the call
102+
* as the offsetSet function returns void. */
103+
info.GetReturnValue().Set(value);
104+
#endif
105+
100106
/* if PHP wanted to hold on to this value, zend_call_function would
101107
* have bumped the refcount. */
102108
zval_ptr_dtor(&zvalue);
103-
return v8::Intercepted::kYes;
109+
return V8JS_INTERCEPTED_YES;
104110
}
105111
/* }}} */
106112

@@ -160,7 +166,7 @@ static void v8js_array_access_length(v8::Local<v8::String> property, const v8::P
160166
}
161167
/* }}} */
162168

163-
v8::Intercepted v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean>& info) /* {{{ */
169+
V8JS_INTERCEPTED v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean>& info) /* {{{ */
164170
{
165171
v8::Isolate *isolate = info.GetIsolate();
166172
v8::Local<v8::Object> self = info.Holder();
@@ -174,11 +180,11 @@ v8::Intercepted v8js_array_access_deleter(uint32_t index, const v8::PropertyCall
174180
zval_ptr_dtor(&php_value);
175181

176182
info.GetReturnValue().Set(V8JS_BOOL(true));
177-
return v8::Intercepted::kYes;
183+
return V8JS_INTERCEPTED_YES;
178184
}
179185
/* }}} */
180186

181-
v8::Intercepted v8js_array_access_query(uint32_t index, const v8::PropertyCallbackInfo<v8::Integer>& info) /* {{{ */
187+
V8JS_INTERCEPTED v8js_array_access_query(uint32_t index, const v8::PropertyCallbackInfo<v8::Integer>& info) /* {{{ */
182188
{
183189
v8::Isolate *isolate = info.GetIsolate();
184190
v8::Local<v8::Object> self = info.Holder();
@@ -189,10 +195,10 @@ v8::Intercepted v8js_array_access_query(uint32_t index, const v8::PropertyCallba
189195
* otherwise we're expected to return an empty handle. */
190196
if(v8js_array_access_isset_p(object, index)) {
191197
info.GetReturnValue().Set(V8JS_UINT(v8::PropertyAttribute::None));
192-
return v8::Intercepted::kYes;
198+
return V8JS_INTERCEPTED_YES;
193199
}
194200

195-
return v8::Intercepted::kNo;
201+
return V8JS_INTERCEPTED_NO;
196202
}
197203
/* }}} */
198204

@@ -222,7 +228,7 @@ void v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& inf
222228

223229

224230

225-
v8::Intercepted v8js_array_access_named_getter(v8::Local<v8::Name> property_name, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
231+
V8JS_INTERCEPTED v8js_array_access_named_getter(v8::Local<v8::Name> property_name, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
226232
{
227233
v8::Local<v8::String> property = v8::Local<v8::String>::Cast(property_name);
228234
v8::Isolate *isolate = info.GetIsolate();
@@ -231,7 +237,7 @@ v8::Intercepted v8js_array_access_named_getter(v8::Local<v8::Name> property_name
231237

232238
if(strcmp(name, "length") == 0) {
233239
v8js_array_access_length(property, info);
234-
return v8::Intercepted::kYes;
240+
return V8JS_INTERCEPTED_YES;
235241
}
236242

237243
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
255261
}
256262

257263
info.GetReturnValue().Set(ret_value);
258-
return v8::Intercepted::kYes;
264+
return V8JS_INTERCEPTED_YES;
259265
}
260266
/* }}} */
261267

v8js_array_access.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
#define V8JS_ARRAY_ACCESS_H
1515

1616
/* Indexed Property Handlers */
17-
v8::Intercepted v8js_array_access_getter(uint32_t index,
17+
V8JS_INTERCEPTED v8js_array_access_getter(uint32_t index,
1818
const v8::PropertyCallbackInfo<v8::Value>& info);
19-
v8::Intercepted v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
20-
const v8::PropertyCallbackInfo<void>& info);
19+
V8JS_INTERCEPTED v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
20+
const V8JS_SETTER_PROPERTY_CALLBACK_INFO &info);
2121
void v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& info);
22-
v8::Intercepted v8js_array_access_deleter(uint32_t index,
22+
V8JS_INTERCEPTED v8js_array_access_deleter(uint32_t index,
2323
const v8::PropertyCallbackInfo<v8::Boolean>& info);
24-
v8::Intercepted v8js_array_access_query(uint32_t index,
24+
V8JS_INTERCEPTED v8js_array_access_query(uint32_t index,
2525
const v8::PropertyCallbackInfo<v8::Integer>& info);
2626

2727
/* Named Property Handlers */
28-
v8::Intercepted v8js_array_access_named_getter(v8::Local<v8::Name> property,
28+
V8JS_INTERCEPTED v8js_array_access_named_getter(v8::Local<v8::Name> property,
2929
const v8::PropertyCallbackInfo<v8::Value> &info);
3030

3131
#endif /* V8JS_ARRAY_ACCESS_H */

v8js_class.cc

+4
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,11 @@ static void v8js_compile_script(zval *this_ptr, const zend_string *str, const ze
524524
v8::Local<v8::String> sname = identifier
525525
? V8JS_ZSTR(identifier)
526526
: V8JS_SYM("V8Js::compileString()");
527+
#if PHP_V8_API_VERSION >= 12002000
527528
v8::ScriptOrigin origin(sname);
529+
#else
530+
v8::ScriptOrigin origin(c->isolate, sname);
531+
#endif
528532

529533
if (ZSTR_LEN(str) > std::numeric_limits<int>::max()) {
530534
zend_throw_exception(php_ce_v8js_exception,

v8js_methods.cc

+4
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,11 @@ V8JS_METHOD(require)
502502

503503
// Set script identifier
504504
v8::Local<v8::String> sname = V8JS_STR(normalised_module_id);
505+
#if PHP_V8_API_VERSION >= 12002000
505506
v8::ScriptOrigin origin(sname);
507+
#else
508+
v8::ScriptOrigin origin(c->isolate, sname);
509+
#endif
506510

507511
if (Z_STRLEN(module_code) > std::numeric_limits<int>::max()) {
508512
zend_throw_exception(php_ce_v8js_exception,

v8js_object_export.cc

+20-12
Original file line numberDiff line numberDiff line change
@@ -857,58 +857,62 @@ v8::Local<v8::Value> v8js_named_property_callback(v8::Isolate *isolate, v8::Loca
857857
}
858858
/* }}} */
859859

860-
static v8::Intercepted v8js_named_property_getter(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
860+
static V8JS_INTERCEPTED v8js_named_property_getter(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
861861
{
862862
v8::Local<v8::Value> r = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_GETTER);
863863

864864
if (r.IsEmpty()) {
865-
return v8::Intercepted::kNo;
865+
return V8JS_INTERCEPTED_NO;
866866
} else {
867867
info.GetReturnValue().Set(r);
868-
return v8::Intercepted::kYes;
868+
return V8JS_INTERCEPTED_YES;
869869
}
870870
}
871871
/* }}} */
872872

873-
static v8::Intercepted v8js_named_property_setter(v8::Local<v8::Name> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void> &info) /* {{{ */
873+
static V8JS_INTERCEPTED v8js_named_property_setter(v8::Local<v8::Name> property, v8::Local<v8::Value> value, const V8JS_SETTER_PROPERTY_CALLBACK_INFO &info) /* {{{ */
874874
{
875875
v8::Local<v8::Value> r = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_SETTER, value);
876+
#if PHP_V8_HAS_INTERCEPTED
876877
return r.IsEmpty() ? v8::Intercepted::kNo : v8::Intercepted::kYes;
878+
#else
879+
info.GetReturnValue().Set(r);
880+
#endif
877881
}
878882
/* }}} */
879883

880-
static v8::Intercepted v8js_named_property_query(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Integer> &info) /* {{{ */
884+
static V8JS_INTERCEPTED v8js_named_property_query(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Integer> &info) /* {{{ */
881885
{
882886
v8::Local<v8::Value> r = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_QUERY);
883887
if (r.IsEmpty()) {
884-
return v8::Intercepted::kNo;
888+
return V8JS_INTERCEPTED_NO;
885889
}
886890

887891
v8::Isolate *isolate = info.GetIsolate();
888892
v8::MaybeLocal<v8::Integer> value = r->ToInteger(isolate->GetEnteredOrMicrotaskContext());
889893
if (value.IsEmpty()) {
890-
return v8::Intercepted::kNo;
894+
return V8JS_INTERCEPTED_NO;
891895
} else {
892896
info.GetReturnValue().Set(value.ToLocalChecked());
893-
return v8::Intercepted::kYes;
897+
return V8JS_INTERCEPTED_YES;
894898
}
895899
}
896900
/* }}} */
897901

898-
static v8::Intercepted v8js_named_property_deleter(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Boolean> &info) /* {{{ */
902+
static V8JS_INTERCEPTED v8js_named_property_deleter(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Boolean> &info) /* {{{ */
899903
{
900904
v8::Local<v8::Value> r = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_DELETER);
901905
if (r.IsEmpty()) {
902-
return v8::Intercepted::kNo;
906+
return V8JS_INTERCEPTED_NO;
903907
}
904908

905909
v8::Isolate *isolate = info.GetIsolate();
906910
v8::Local<v8::Boolean> value = r->ToBoolean(isolate);
907911
if (value.IsEmpty()) {
908-
return v8::Intercepted::kNo;
912+
return V8JS_INTERCEPTED_NO;
909913
} else {
910914
info.GetReturnValue().Set(value);
911-
return v8::Intercepted::kYes;
915+
return V8JS_INTERCEPTED_YES;
912916
}
913917
}
914918
/* }}} */
@@ -951,7 +955,11 @@ static v8::MaybeLocal<v8::Object> v8js_wrap_object(v8::Isolate *isolate, zend_cl
951955
/* We'll free persist_tpl_ when template_cache is destroyed */
952956

953957
v8::Local<v8::ObjectTemplate> inst_tpl = new_tpl->InstanceTemplate();
958+
#if PHP_V8_HAS_INTERCEPTED
954959
v8::NamedPropertyGetterCallback getter = v8js_named_property_getter;
960+
#else
961+
v8::GenericNamedPropertyGetterCallback getter = v8js_named_property_getter;
962+
#endif
955963
v8::GenericNamedPropertyEnumeratorCallback enumerator = v8js_named_property_enumerator;
956964

957965
/* Check for ArrayAccess object */

v8js_v8.h

+16
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ int v8js_get_properties_hash(v8::Local<v8::Value> jsValue, HashTable *retval, in
113113
#define IS_LONG 99
114114
#endif
115115

116+
#define PHP_V8_HAS_INTERCEPTED PHP_V8_API_VERSION >= 12005000
117+
118+
#if PHP_V8_HAS_INTERCEPTED
119+
#define V8JS_INTERCEPTED v8::Intercepted
120+
#define V8JS_INTERCEPTED_YES v8::Intercepted::kYes
121+
#define V8JS_INTERCEPTED_NO v8::Intercepted::kNo
122+
#define V8JS_SETTER_PROPERTY_CALLBACK_INFO v8::PropertyCallbackInfo<void>
123+
124+
#else
125+
#define V8JS_INTERCEPTED void
126+
#define V8JS_INTERCEPTED_YES
127+
#define V8JS_INTERCEPTED_NO
128+
#define V8JS_SETTER_PROPERTY_CALLBACK_INFO v8::PropertyCallbackInfo<v8::Value>
129+
130+
#endif
131+
116132

117133
#endif /* V8JS_V8_H */
118134

0 commit comments

Comments
 (0)