Skip to content

Commit 05b96a9

Browse files
committed
Use internal fields instead of GetHiddenValue/SetHiddenValue
The latter were deprecated and removed in V8 5.2
1 parent 48cb242 commit 05b96a9

File tree

7 files changed

+37
-59
lines changed

7 files changed

+37
-59
lines changed

php_v8js_macros.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ extern "C" {
5555
/* V8Js Version */
5656
#define PHP_V8JS_VERSION "0.6.2"
5757

58-
/* Hidden field name used to link JS wrappers with underlying PHP object */
59-
#define PHPJS_OBJECT_KEY "phpjs::object"
60-
6158
/* Helper macros */
6259
#define V8JS_GET_CLASS_NAME(var, obj) \
6360
v8::String::Utf8Value var(obj->GetConstructorName());

v8js_array_access.cc

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
+----------------------------------------------------------------------+
33
| PHP Version 5 |
44
+----------------------------------------------------------------------+
5-
| Copyright (c) 1997-2013 The PHP Group |
5+
| Copyright (c) 1997-2016 The PHP Group |
66
+----------------------------------------------------------------------+
77
| http://www.opensource.org/licenses/mit-license.php MIT License |
88
+----------------------------------------------------------------------+
@@ -73,8 +73,7 @@ void v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8:
7373

7474
V8JS_TSRMLS_FETCH();
7575

76-
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
77-
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
76+
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
7877

7978
zval *php_value = v8js_array_access_dispatch(object, "offsetGet", 1, index, NULL TSRMLS_CC);
8079
v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
@@ -92,8 +91,7 @@ void v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
9291

9392
V8JS_TSRMLS_FETCH();
9493

95-
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
96-
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
94+
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
9795

9896
zval *zvalue_ptr;
9997
MAKE_STD_ZVAL(zvalue_ptr);
@@ -156,8 +154,7 @@ static void v8js_array_access_length(v8::Local<v8::String> property, const v8::P
156154

157155
V8JS_TSRMLS_FETCH();
158156

159-
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
160-
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
157+
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
161158

162159
int length = v8js_array_access_get_count_result(object TSRMLS_CC);
163160
info.GetReturnValue().Set(V8JS_INT(length));
@@ -171,8 +168,7 @@ void v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInfo<v8
171168

172169
V8JS_TSRMLS_FETCH();
173170

174-
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
175-
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
171+
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
176172

177173
zval *php_value = v8js_array_access_dispatch(object, "offsetUnset", 1, index, NULL TSRMLS_CC);
178174
zval_ptr_dtor(&php_value);
@@ -188,8 +184,7 @@ void v8js_array_access_query(uint32_t index, const v8::PropertyCallbackInfo<v8::
188184

189185
V8JS_TSRMLS_FETCH();
190186

191-
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
192-
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
187+
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
193188

194189
/* If index is set, then return an integer encoding a v8::PropertyAttribute;
195190
* otherwise we're expected to return an empty handle. */
@@ -207,8 +202,7 @@ void v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& inf
207202

208203
V8JS_TSRMLS_FETCH();
209204

210-
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
211-
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
205+
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
212206

213207
int length = v8js_array_access_get_count_result(object TSRMLS_CC);
214208
v8::Local<v8::Array> result = v8::Array::New(isolate, length);

v8js_class.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ static PHP_METHOD(V8Js, __construct)
475475
c->object_name.Reset(isolate, object_name_js);
476476

477477
/* Add the PHP object into global object */
478+
php_obj_t->InstanceTemplate()->SetInternalFieldCount(2);
478479
v8::Local<v8::Object> php_obj = php_obj_t->InstanceTemplate()->NewInstance();
479480
V8JS_GLOBAL(isolate)->ForceSet(object_name_js, php_obj, v8::ReadOnly);
480481

@@ -505,7 +506,7 @@ static PHP_METHOD(V8Js, __construct)
505506
}
506507

507508
/* Add pointer to zend object */
508-
php_obj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), v8::External::New(isolate, getThis()));
509+
php_obj->SetAlignedPointerInInternalField(1, getThis());
509510

510511
/* Export public methods */
511512
zend_function *method_ptr;

v8js_convert.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
+----------------------------------------------------------------------+
33
| PHP Version 5 |
44
+----------------------------------------------------------------------+
5-
| Copyright (c) 1997-2013 The PHP Group |
5+
| Copyright (c) 1997-2016 The PHP Group |
66
+----------------------------------------------------------------------+
77
| http://www.opensource.org/licenses/mit-license.php MIT License |
88
+----------------------------------------------------------------------+
99
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
1010
| Author: Patrick Reilly <preilly@php.net> |
11+
| Author: Stefan Siegl <stesie@php.net> |
1112
+----------------------------------------------------------------------+
1213
*/
1314

@@ -236,14 +237,15 @@ int v8js_to_zval(v8::Handle<v8::Value> jsValue, zval *return_value, int flags, v
236237
}
237238
else if (jsValue->IsObject())
238239
{
239-
v8::Handle<v8::Object> self = v8::Handle<v8::Object>::Cast(jsValue);
240+
v8::Local<v8::Object> self = jsValue->ToObject();
241+
240242
// if this is a wrapped PHP object, then just unwrap it.
241-
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
242-
if (!php_object.IsEmpty()) {
243-
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
243+
if (self->InternalFieldCount()) {
244+
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
244245
RETVAL_ZVAL(object, 1, 0);
245246
return SUCCESS;
246247
}
248+
247249
if ((flags & V8JS_FLAG_FORCE_ARRAY && !jsValue->IsFunction()) || jsValue->IsArray()) {
248250
array_init(return_value);
249251
return v8js_get_properties_hash(jsValue, Z_ARRVAL_P(return_value), flags, isolate TSRMLS_CC);

v8js_exceptions.cc

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
+----------------------------------------------------------------------+
33
| PHP Version 5 |
44
+----------------------------------------------------------------------+
5-
| Copyright (c) 1997-2015 The PHP Group |
5+
| Copyright (c) 1997-2016 The PHP Group |
66
+----------------------------------------------------------------------+
77
| http://www.opensource.org/licenses/mit-license.php MIT License |
88
+----------------------------------------------------------------------+
@@ -83,18 +83,13 @@ void v8js_create_script_exception(zval *return_value, v8::Isolate *isolate, v8::
8383
PHPV8_EXPROP(_string, JsTrace, stacktrace_string);
8484
}
8585

86-
if(try_catch->Exception()->IsObject()) {
87-
v8::Local<v8::Value> php_ref = try_catch->Exception()->ToObject()->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
86+
if(try_catch->Exception()->IsObject() && try_catch->Exception()->ToObject()->InternalFieldCount()) {
87+
zval *php_exception = reinterpret_cast<zval *>(try_catch->Exception()->ToObject()->GetAlignedPointerFromInternalField(1));
8888

89-
if(!php_ref.IsEmpty()) {
90-
assert(php_ref->IsExternal());
91-
zval *php_exception = reinterpret_cast<zval *>(v8::External::Cast(*php_ref)->Value());
92-
93-
zend_class_entry *exception_ce = zend_exception_get_default(TSRMLS_C);
94-
if (Z_TYPE_P(php_exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(php_exception), exception_ce TSRMLS_CC)) {
95-
Z_ADDREF_P(php_exception);
96-
zend_exception_set_previous(return_value, php_exception TSRMLS_CC);
97-
}
89+
zend_class_entry *exception_ce = zend_exception_get_default(TSRMLS_C);
90+
if (Z_TYPE_P(php_exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(php_exception), exception_ce TSRMLS_CC)) {
91+
Z_ADDREF_P(php_exception);
92+
zend_exception_set_previous(return_value, php_exception TSRMLS_CC);
9893
}
9994
}
10095

v8js_object_export.cc

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
+----------------------------------------------------------------------+
33
| PHP Version 5 |
44
+----------------------------------------------------------------------+
5-
| Copyright (c) 1997-2013 The PHP Group |
5+
| Copyright (c) 1997-2016 The PHP Group |
66
+----------------------------------------------------------------------+
77
| http://www.opensource.org/licenses/mit-license.php MIT License |
88
+----------------------------------------------------------------------+
99
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
1010
| Author: Patrick Reilly <preilly@php.net> |
11+
| Author: Stefan Siegl <stesie@php.net> |
1112
+----------------------------------------------------------------------+
1213
*/
1314

@@ -88,13 +89,9 @@ static void v8js_call_php_func(zval *value, zend_class_entry *ce, zend_function
8889
fci.params = (zval ***) safe_emalloc(argc, sizeof(zval **), 0);
8990
argv = (zval **) safe_emalloc(argc, sizeof(zval *), 0);
9091
for (i = 0; i < argc; i++) {
91-
v8::Local<v8::Value> php_object;
92-
if (info[i]->IsObject()) {
93-
php_object = v8::Local<v8::Object>::Cast(info[i])->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
94-
}
95-
if (!php_object.IsEmpty()) {
92+
if (info[i]->IsObject() && info[i]->ToObject()->InternalFieldCount()) {
9693
/* This is a PHP object, passed to JS and back. */
97-
argv[i] = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
94+
argv[i] = reinterpret_cast<zval *>(info[i]->ToObject()->GetAlignedPointerFromInternalField(1));
9895
Z_ADDREF_P(argv[i]);
9996
} else {
10097
MAKE_STD_ZVAL(argv[i]);
@@ -178,7 +175,7 @@ void v8js_php_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ *
178175
v8::Local<v8::Object> self = info.Holder();
179176

180177
V8JS_TSRMLS_FETCH();
181-
zval *value = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
178+
zval *value = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
182179
zend_function *method_ptr;
183180
zend_class_entry *ce = Z_OBJCE_P(value);
184181

@@ -198,9 +195,7 @@ static void v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value>& i
198195
v8::Isolate *isolate = info.GetIsolate();
199196
info.GetReturnValue().Set(V8JS_UNDEFINED);
200197

201-
// @todo assert constructor call
202198
v8::Handle<v8::Object> newobj = info.This();
203-
v8::Local<v8::External> php_object;
204199
zval *value;
205200

206201
if (!info.IsConstructCall()) {
@@ -215,14 +210,14 @@ static void v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value>& i
215210

216211
if (info[0]->IsExternal()) {
217212
// Object created by v8js in v8js_hash_to_jsobj, PHP object passed as v8::External.
218-
php_object = v8::Local<v8::External>::Cast(info[0]);
213+
v8::Local<v8::External> php_object = v8::Local<v8::External>::Cast(info[0]);
219214
value = reinterpret_cast<zval *>(php_object->Value());
220215

221216
if(ctx->weak_objects.count(value)) {
222217
// We already exported this object, hence no need to add another
223218
// ref, v8 won't give us a second weak-object callback anyways.
224219
newobj->SetAlignedPointerInInternalField(0, ext_tmpl->Value());
225-
newobj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), php_object);
220+
newobj->SetAlignedPointerInInternalField(1, value);
226221
return;
227222
}
228223

@@ -248,11 +243,10 @@ static void v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value>& i
248243
if (ctor_ptr != NULL) {
249244
v8js_call_php_func(value, ce, ctor_ptr, isolate, info TSRMLS_CC);
250245
}
251-
php_object = v8::External::New(isolate, value);
252246
}
253247

254248
newobj->SetAlignedPointerInInternalField(0, ext_tmpl->Value());
255-
newobj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), php_object);
249+
newobj->SetAlignedPointerInInternalField(1, value);
256250

257251
// Since we got to decrease the reference count again, in case v8 garbage collector
258252
// decides to dispose the JS object, we add a weak persistent handle and register
@@ -319,7 +313,7 @@ static void v8js_named_property_enumerator(const v8::PropertyCallbackInfo<v8::Ar
319313
uint key_len;
320314
ulong index;
321315

322-
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
316+
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
323317
ce = Z_OBJCE_P(object);
324318

325319
/* enumerate all methods */
@@ -449,7 +443,7 @@ static void v8js_fake_call_impl(const v8::FunctionCallbackInfo<v8::Value>& info)
449443

450444
V8JS_TSRMLS_FETCH();
451445
zend_class_entry *ce;
452-
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
446+
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
453447
ce = Z_OBJCE_P(object);
454448

455449
// first arg is method name, second arg is array of args.
@@ -541,7 +535,7 @@ inline v8::Local<v8::Value> v8js_named_property_callback(v8::Local<v8::String> p
541535
zend_function *method_ptr = NULL;
542536
zval *php_value;
543537

544-
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
538+
zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
545539
v8js_tmpl_t *tmpl_ptr = reinterpret_cast<v8js_tmpl_t *>(self->GetAlignedPointerFromInternalField(0));
546540
v8::Local<v8::FunctionTemplate> tmpl = v8::Local<v8::FunctionTemplate>::New(isolate, *tmpl_ptr);
547541
ce = scope = Z_OBJCE_P(object);
@@ -809,7 +803,7 @@ static v8::Handle<v8::Object> v8js_wrap_object(v8::Isolate *isolate, zend_class_
809803
new_tpl = v8::FunctionTemplate::New(isolate, 0);
810804

811805
new_tpl->SetClassName(V8JS_STRL(ce->name, ce->name_length));
812-
new_tpl->InstanceTemplate()->SetInternalFieldCount(1);
806+
new_tpl->InstanceTemplate()->SetInternalFieldCount(2);
813807

814808
if (ce == zend_ce_closure) {
815809
/* Got a closure, mustn't cache ... */

v8js_v8.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,8 @@ int v8js_get_properties_hash(v8::Handle<v8::Value> jsValue, HashTable *retval, i
276276
const char *key = ToCString(cstr);
277277
zval *value = NULL;
278278

279-
v8::Local<v8::Value> php_object;
280-
if (jsVal->IsObject()) {
281-
php_object = v8::Local<v8::Object>::Cast(jsVal)->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
282-
}
283-
if (!php_object.IsEmpty()) {
284-
/* This is a PHP object, passed to JS and back. */
285-
value = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
279+
if (jsVal->IsObject() && jsVal->ToObject()->InternalFieldCount()) {
280+
value = reinterpret_cast<zval *>(jsVal->ToObject()->GetAlignedPointerFromInternalField(1));
286281
Z_ADDREF_P(value);
287282
}
288283
else {

0 commit comments

Comments
 (0)