Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 41d01fb

Browse files
committed
Do not use Object::CreationContext() for API
Ther rationale for that as it may hold older context which zval was already destroyed and we do not want to cover this case at this time as it adds more ambiguity then practical benefit
1 parent 84bc40b commit 41d01fb

11 files changed

+124
-74
lines changed

src/php_v8_object.cc

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,25 +1071,6 @@ static PHP_METHOD(V8Object, Clone) {
10711071
php_v8_get_or_create_value(return_value, local_cloned_object, php_v8_value->php_v8_isolate);
10721072
}
10731073

1074-
static PHP_METHOD(V8Object, CreationContext) {
1075-
if (zend_parse_parameters_none() == FAILURE) {
1076-
return;
1077-
}
1078-
1079-
PHP_V8_VALUE_FETCH_WITH_CHECK(getThis(), php_v8_value);
1080-
PHP_V8_ENTER_ISOLATE(php_v8_value->php_v8_isolate);
1081-
1082-
v8::Local<v8::Value> local_value = php_v8_value_get_local(php_v8_value);
1083-
v8::Local<v8::Object> local_object = v8::Local<v8::Object>::Cast(local_value);
1084-
1085-
v8::Local<v8::Context> local_context= local_object->CreationContext();
1086-
1087-
php_v8_context_t *php_v8_context = php_v8_context_get_reference(local_context);
1088-
1089-
ZVAL_OBJ(return_value, &php_v8_context->std);
1090-
Z_ADDREF_P(return_value);
1091-
}
1092-
10931074
static PHP_METHOD(V8Object, IsCallable) {
10941075
if (zend_parse_parameters_none() == FAILURE) {
10951076
return;
@@ -1377,9 +1358,6 @@ ZEND_END_ARG_INFO()
13771358
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_v8_object_Clone, ZEND_RETURN_VALUE, 0, V8\\ObjectValue, 0)
13781359
ZEND_END_ARG_INFO()
13791360

1380-
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_v8_object_CreationContext, ZEND_RETURN_VALUE, 0, V8\\Context, 0)
1381-
ZEND_END_ARG_INFO()
1382-
13831361
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_object_IsCallable, ZEND_RETURN_VALUE, 0, _IS_BOOL, 0)
13841362
ZEND_END_ARG_INFO()
13851363

@@ -1444,7 +1422,6 @@ static const zend_function_entry php_v8_object_methods[] = {
14441422
PHP_ME(V8Object, HasIndexedLookupInterceptor, arginfo_v8_object_HasIndexedLookupInterceptor, ZEND_ACC_PUBLIC)
14451423
PHP_ME(V8Object, GetIdentityHash, arginfo_v8_object_GetIdentityHash, ZEND_ACC_PUBLIC)
14461424
PHP_ME(V8Object, Clone, arginfo_v8_object_Clone, ZEND_ACC_PUBLIC)
1447-
PHP_ME(V8Object, CreationContext, arginfo_v8_object_CreationContext, ZEND_ACC_PUBLIC)
14481425

14491426
PHP_ME(V8Object, IsCallable, arginfo_v8_object_IsCallable, ZEND_ACC_PUBLIC)
14501427
PHP_ME(V8Object, IsConstructor, arginfo_v8_object_IsConstructor, ZEND_ACC_PUBLIC)

src/php_v8_value.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ php_v8_value_t *php_v8_create_value(zval *return_value, v8::Local<v8::Value> loc
280280
if (local_value->IsObject()) {
281281
assert(php_v8_isolate->isolate->InContext());
282282

283-
php_v8_context_t *php_v8_context = php_v8_context_get_reference(local_value.As<v8::Object>()->CreationContext());
283+
php_v8_context_t *php_v8_context = php_v8_context_get_reference(php_v8_isolate->isolate->GetEnteredContext());
284284

285285
ZVAL_OBJ(&context_zv, &php_v8_context->std);
286286
PHP_V8_OBJECT_STORE_CONTEXT(return_value, &context_zv);

stubs/src/ObjectValue.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -411,16 +411,6 @@ public function Clone(): ObjectValue
411411
{
412412
}
413413

414-
/**
415-
* Returns the context in which the object was created.
416-
* Note: Object may be created outside context!
417-
*
418-
* @return \V8\Context
419-
*/
420-
public function CreationContext(): Context
421-
{
422-
}
423-
424414
/**
425415
* Checks whether a callback is set by the
426416
* ObjectTemplate::SetCallAsFunctionHandler method.

tests/V8ArrayObject.phpt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ $helper->line();
2929
$helper->header('Accessors');
3030
$helper->method_matches($value, 'GetIsolate', $isolate);
3131
$helper->method_matches($value, 'GetContext', $context);
32-
$helper->method_matches($value, 'CreationContext', $context);
3332
$helper->space();
3433

3534
$v8_helper->run_checks($value, 'Checkers');
@@ -122,7 +121,6 @@ Accessors:
122121
----------
123122
V8\ArrayObject::GetIsolate() matches expected value
124123
V8\ArrayObject::GetContext() matches expected value
125-
V8\ArrayObject::CreationContext() matches expected value
126124

127125

128126
Checkers:
@@ -184,7 +182,7 @@ V8\ArrayObject(V8\Value)->IsProxy(): bool(false)
184182
Converters:
185183
-----------
186184
V8\ArrayObject(V8\Value)->ToBoolean():
187-
object(V8\BooleanValue)#118 (1) {
185+
object(V8\BooleanValue)#117 (1) {
188186
["isolate":"V8\Value":private]=>
189187
object(V8\Isolate)#3 (5) {
190188
["snapshot":"V8\Isolate":private]=>
@@ -200,7 +198,7 @@ V8\ArrayObject(V8\Value)->ToBoolean():
200198
}
201199
}
202200
V8\ArrayObject(V8\Value)->ToNumber():
203-
object(V8\Int32Value)#118 (1) {
201+
object(V8\Int32Value)#117 (1) {
204202
["isolate":"V8\Value":private]=>
205203
object(V8\Isolate)#3 (5) {
206204
["snapshot":"V8\Isolate":private]=>
@@ -216,7 +214,7 @@ V8\ArrayObject(V8\Value)->ToNumber():
216214
}
217215
}
218216
V8\ArrayObject(V8\Value)->ToString():
219-
object(V8\StringValue)#118 (1) {
217+
object(V8\StringValue)#117 (1) {
220218
["isolate":"V8\Value":private]=>
221219
object(V8\Isolate)#3 (5) {
222220
["snapshot":"V8\Isolate":private]=>
@@ -232,7 +230,7 @@ V8\ArrayObject(V8\Value)->ToString():
232230
}
233231
}
234232
V8\ArrayObject(V8\Value)->ToDetailString():
235-
object(V8\StringValue)#118 (1) {
233+
object(V8\StringValue)#117 (1) {
236234
["isolate":"V8\Value":private]=>
237235
object(V8\Isolate)#3 (5) {
238236
["snapshot":"V8\Isolate":private]=>
@@ -298,7 +296,7 @@ V8\ArrayObject(V8\Value)->ToObject():
298296
}
299297
}
300298
V8\ArrayObject(V8\Value)->ToInteger():
301-
object(V8\Int32Value)#118 (1) {
299+
object(V8\Int32Value)#117 (1) {
302300
["isolate":"V8\Value":private]=>
303301
object(V8\Isolate)#3 (5) {
304302
["snapshot":"V8\Isolate":private]=>
@@ -314,7 +312,7 @@ V8\ArrayObject(V8\Value)->ToInteger():
314312
}
315313
}
316314
V8\ArrayObject(V8\Value)->ToUint32():
317-
object(V8\Int32Value)#118 (1) {
315+
object(V8\Int32Value)#117 (1) {
318316
["isolate":"V8\Value":private]=>
319317
object(V8\Isolate)#3 (5) {
320318
["snapshot":"V8\Isolate":private]=>
@@ -330,7 +328,7 @@ V8\ArrayObject(V8\Value)->ToUint32():
330328
}
331329
}
332330
V8\ArrayObject(V8\Value)->ToInt32():
333-
object(V8\Int32Value)#118 (1) {
331+
object(V8\Int32Value)#117 (1) {
334332
["isolate":"V8\Value":private]=>
335333
object(V8\Isolate)#3 (5) {
336334
["snapshot":"V8\Isolate":private]=>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
--TEST--
2+
V8\Context reference lifecycle
3+
--SKIPIF--
4+
<?php if (!extension_loaded("v8")) {
5+
print "skip";
6+
} ?>
7+
--FILE--
8+
<?php
9+
10+
/** @var \Phpv8Testsuite $helper */
11+
$helper = require '.testsuite.php';
12+
13+
require '.v8-helpers.php';
14+
$v8_helper = new PhpV8Helpers($helper);
15+
16+
class Context extends V8\Context {
17+
public function __destruct() {
18+
echo 'Context dies now', PHP_EOL;
19+
}
20+
}
21+
22+
$isolate = new \V8\Isolate();
23+
24+
25+
$obj = $v8_helper->CompileRun(new Context($isolate), 'var obj = {}; obj');
26+
27+
//$helper->dump($obj);
28+
$helper->dump($obj->GetContext());
29+
30+
31+
$context = new Context($isolate);
32+
$context->GlobalObject()->Set($context, new \V8\StringValue($isolate, 'obj'), $obj);
33+
34+
$helper->line();
35+
$obj = null;
36+
$helper->line();
37+
38+
$helper->message('Previous context should be dead, creating zval for object from old context');
39+
$helper->line();
40+
41+
$obj = $v8_helper->CompileRun($context, 'var obj2 = obj; obj2');
42+
43+
//$helper->dump($obj);
44+
$helper->dump($obj->GetContext());
45+
$obj = null;
46+
?>
47+
--EXPECT--
48+
object(Context)#4 (3) {
49+
["isolate":"V8\Context":private]=>
50+
object(V8\Isolate)#3 (5) {
51+
["snapshot":"V8\Isolate":private]=>
52+
NULL
53+
["time_limit":"V8\Isolate":private]=>
54+
float(0)
55+
["time_limit_hit":"V8\Isolate":private]=>
56+
bool(false)
57+
["memory_limit":"V8\Isolate":private]=>
58+
int(0)
59+
["memory_limit_hit":"V8\Isolate":private]=>
60+
bool(false)
61+
}
62+
["global_template":"V8\Context":private]=>
63+
NULL
64+
["global_object":"V8\Context":private]=>
65+
NULL
66+
}
67+
68+
Context dies now
69+
70+
Previous context should be dead, creating zval for object from old context
71+
72+
object(Context)#6 (3) {
73+
["isolate":"V8\Context":private]=>
74+
object(V8\Isolate)#3 (5) {
75+
["snapshot":"V8\Isolate":private]=>
76+
NULL
77+
["time_limit":"V8\Isolate":private]=>
78+
float(0)
79+
["time_limit_hit":"V8\Isolate":private]=>
80+
bool(false)
81+
["memory_limit":"V8\Isolate":private]=>
82+
int(0)
83+
["memory_limit_hit":"V8\Isolate":private]=>
84+
bool(false)
85+
}
86+
["global_template":"V8\Context":private]=>
87+
NULL
88+
["global_object":"V8\Context":private]=>
89+
NULL
90+
}
91+
Context dies now

tests/V8FunctionObject.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ Should output Hello World string
170170
string(11) "Script done"
171171

172172
v8Tests\TrackingDtors\FunctionObject(V8\FunctionObject)->GetScriptOrigin():
173-
object(V8\ScriptOrigin)#127 (6) {
173+
object(V8\ScriptOrigin)#126 (6) {
174174
["resource_name":"V8\ScriptOrigin":private]=>
175175
string(0) ""
176176
["resource_line_offset":"V8\ScriptOrigin":private]=>
177177
int(0)
178178
["resource_column_offset":"V8\ScriptOrigin":private]=>
179179
int(0)
180180
["options":"V8\ScriptOrigin":private]=>
181-
object(V8\ScriptOriginOptions)#131 (4) {
181+
object(V8\ScriptOriginOptions)#130 (4) {
182182
["is_shared_cross_origin":"V8\ScriptOriginOptions":private]=>
183183
bool(false)
184184
["is_opaque":"V8\ScriptOriginOptions":private]=>

tests/V8MapObject.phpt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ $helper->line();
3131
$helper->header('Accessors');
3232
$helper->method_matches($value, 'GetIsolate', $isolate);
3333
$helper->method_matches($value, 'GetContext', $context);
34-
$helper->method_matches($value, 'CreationContext', $context);
3534
$helper->space();
3635

3736
$helper->header('Getters');
@@ -159,7 +158,6 @@ Accessors:
159158
----------
160159
V8\MapObject::GetIsolate() matches expected value
161160
V8\MapObject::GetContext() matches expected value
162-
V8\MapObject::CreationContext() matches expected value
163161

164162

165163
Getters:
@@ -226,7 +224,7 @@ V8\MapObject(V8\Value)->IsProxy(): bool(false)
226224
Converters:
227225
-----------
228226
V8\MapObject(V8\Value)->ToBoolean():
229-
object(V8\BooleanValue)#119 (1) {
227+
object(V8\BooleanValue)#118 (1) {
230228
["isolate":"V8\Value":private]=>
231229
object(V8\Isolate)#3 (5) {
232230
["snapshot":"V8\Isolate":private]=>
@@ -242,7 +240,7 @@ V8\MapObject(V8\Value)->ToBoolean():
242240
}
243241
}
244242
V8\MapObject(V8\Value)->ToNumber():
245-
object(V8\NumberValue)#119 (1) {
243+
object(V8\NumberValue)#118 (1) {
246244
["isolate":"V8\Value":private]=>
247245
object(V8\Isolate)#3 (5) {
248246
["snapshot":"V8\Isolate":private]=>
@@ -258,7 +256,7 @@ V8\MapObject(V8\Value)->ToNumber():
258256
}
259257
}
260258
V8\MapObject(V8\Value)->ToString():
261-
object(V8\StringValue)#119 (1) {
259+
object(V8\StringValue)#118 (1) {
262260
["isolate":"V8\Value":private]=>
263261
object(V8\Isolate)#3 (5) {
264262
["snapshot":"V8\Isolate":private]=>
@@ -274,7 +272,7 @@ V8\MapObject(V8\Value)->ToString():
274272
}
275273
}
276274
V8\MapObject(V8\Value)->ToDetailString():
277-
object(V8\StringValue)#119 (1) {
275+
object(V8\StringValue)#118 (1) {
278276
["isolate":"V8\Value":private]=>
279277
object(V8\Isolate)#3 (5) {
280278
["snapshot":"V8\Isolate":private]=>
@@ -340,7 +338,7 @@ V8\MapObject(V8\Value)->ToObject():
340338
}
341339
}
342340
V8\MapObject(V8\Value)->ToInteger():
343-
object(V8\Int32Value)#119 (1) {
341+
object(V8\Int32Value)#118 (1) {
344342
["isolate":"V8\Value":private]=>
345343
object(V8\Isolate)#3 (5) {
346344
["snapshot":"V8\Isolate":private]=>
@@ -356,7 +354,7 @@ V8\MapObject(V8\Value)->ToInteger():
356354
}
357355
}
358356
V8\MapObject(V8\Value)->ToUint32():
359-
object(V8\Int32Value)#119 (1) {
357+
object(V8\Int32Value)#118 (1) {
360358
["isolate":"V8\Value":private]=>
361359
object(V8\Isolate)#3 (5) {
362360
["snapshot":"V8\Isolate":private]=>
@@ -372,7 +370,7 @@ V8\MapObject(V8\Value)->ToUint32():
372370
}
373371
}
374372
V8\MapObject(V8\Value)->ToInt32():
375-
object(V8\Int32Value)#119 (1) {
373+
object(V8\Int32Value)#118 (1) {
376374
["isolate":"V8\Value":private]=>
377375
object(V8\Isolate)#3 (5) {
378376
["snapshot":"V8\Isolate":private]=>

0 commit comments

Comments
 (0)