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

Commit 0753966

Browse files
committed
Remove Obj::ForceSet(), add CreateDataProperty and DefineOwnProperty
1 parent dc12f46 commit 0753966

File tree

7 files changed

+152
-39
lines changed

7 files changed

+152
-39
lines changed

src/php_v8_object.cc

Lines changed: 96 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,83 @@ static PHP_METHOD(V8Object, SetIndex) {
243243
RETURN_BOOL(maybe_res.FromJust());
244244
}
245245

246-
static PHP_METHOD(V8Object, ForceSet) {
246+
static PHP_METHOD(V8Object, CreateDataProperty) {
247+
zval *php_v8_context_zv;
248+
zval *php_v8_key_or_index_zv;
249+
zval *php_v8_value_zv;
250+
251+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ooo", &php_v8_context_zv, &php_v8_key_or_index_zv, &php_v8_value_zv) == FAILURE) {
252+
return;
253+
}
254+
255+
PHP_V8_VALUE_FETCH_WITH_CHECK(getThis(), php_v8_value);
256+
PHP_V8_VALUE_FETCH_WITH_CHECK(php_v8_key_or_index_zv, php_v8_key_or_index);
257+
PHP_V8_VALUE_FETCH_WITH_CHECK(php_v8_value_zv, php_v8_value_value_to_set);
258+
PHP_V8_CONTEXT_FETCH_WITH_CHECK(php_v8_context_zv, php_v8_context);
259+
260+
PHP_V8_DATA_ISOLATES_CHECK(php_v8_value, php_v8_context);
261+
PHP_V8_DATA_ISOLATES_CHECK(php_v8_value, php_v8_key_or_index);
262+
PHP_V8_DATA_ISOLATES_CHECK(php_v8_value, php_v8_value_value_to_set);
263+
264+
PHP_V8_ENTER_STORED_ISOLATE(php_v8_value);
265+
PHP_V8_ENTER_CONTEXT(php_v8_context);
266+
267+
v8::Local<v8::Object> local_obj = php_v8_value_get_object_local(isolate, php_v8_value);
268+
v8::Local<v8::Name> local_key_or_index = php_v8_value_get_name_local(isolate, php_v8_key_or_index);
269+
v8::Local<v8::Value> local_value_to_set = php_v8_value_get_value_local(isolate, php_v8_value_value_to_set);
270+
271+
PHP_V8_TRY_CATCH(isolate);
272+
PHP_V8_INIT_ISOLATE_LIMITS_ON_OBJECT_VALUE(php_v8_value);
273+
274+
v8::Maybe<bool> maybe_res = local_obj->CreateDataProperty(context, local_key_or_index, local_value_to_set);
275+
276+
PHP_V8_MAYBE_CATCH(php_v8_context, try_catch);
277+
PHP_V8_THROW_EXCEPTION_WHEN_NOTHING(maybe_res, "Failed to create data property");
278+
279+
RETURN_BOOL(maybe_res.FromJust());
280+
}
281+
282+
static PHP_METHOD(V8Object, CreateDataPropertyIndex) {
283+
zval *php_v8_context_zv;
284+
zend_long index;
285+
zval *php_v8_value_zv;
286+
287+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "olo", &php_v8_context_zv, &index, &php_v8_value_zv) == FAILURE) {
288+
return;
289+
}
290+
291+
PHP_V8_CHECK_UINT32_RANGE(index, "Index is out of range (should be valid uint32 value)");
292+
293+
PHP_V8_VALUE_FETCH_WITH_CHECK(getThis(), php_v8_value);
294+
PHP_V8_VALUE_FETCH_WITH_CHECK(php_v8_value_zv, php_v8_value_value_to_set);
295+
PHP_V8_CONTEXT_FETCH_WITH_CHECK(php_v8_context_zv, php_v8_context);
296+
297+
PHP_V8_DATA_ISOLATES_CHECK(php_v8_value, php_v8_context);
298+
PHP_V8_DATA_ISOLATES_CHECK(php_v8_value, php_v8_value_value_to_set);
299+
300+
PHP_V8_ENTER_STORED_ISOLATE(php_v8_value);
301+
PHP_V8_ENTER_CONTEXT(php_v8_context);
302+
303+
v8::Local<v8::Object> local_obj = php_v8_value_get_object_local(isolate, php_v8_value);
304+
v8::Local<v8::Value> local_value_to_set = php_v8_value_get_value_local(isolate, php_v8_value_value_to_set);
305+
306+
PHP_V8_TRY_CATCH(isolate);
307+
PHP_V8_INIT_ISOLATE_LIMITS_ON_OBJECT_VALUE(php_v8_value);
308+
309+
v8::Maybe<bool> maybe_res = local_obj->CreateDataProperty(context, static_cast<uint32_t>(index), local_value_to_set);
310+
311+
PHP_V8_MAYBE_CATCH(php_v8_context, try_catch);
312+
PHP_V8_THROW_EXCEPTION_WHEN_NOTHING(maybe_res, "Failed to create data property");
313+
314+
RETURN_BOOL(maybe_res.FromJust());
315+
}
316+
317+
static PHP_METHOD(V8Object, DefineOwnProperty) {
247318
zval *php_v8_context_zv;
248319
zval *php_v8_value_zv;
249320
zval *php_v8_key_zv;
250321

251-
zend_long attributes;
322+
zend_long attributes = 0;
252323

253324
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ooo|l", &php_v8_context_zv, &php_v8_key_zv, &php_v8_value_zv, &attributes) == FAILURE) {
254325
return;
@@ -270,20 +341,20 @@ static PHP_METHOD(V8Object, ForceSet) {
270341
v8::Local<v8::Value> local_value_to_set = php_v8_value_get_value_local(isolate, php_v8_value_value_to_set);
271342
v8::Local<v8::Object> local_obj = php_v8_value_get_object_local(isolate, php_v8_value);
272343

273-
v8::Local<v8::String> local_key = php_v8_value_get_string_local(isolate, php_v8_key);
344+
v8::Local<v8::Name> local_name = php_v8_value_get_name_local(isolate, php_v8_key);
274345

275346
attributes = attributes ? attributes & PHP_V8_PROPERTY_ATTRIBUTE_FLAGS : attributes;
276347

277348
PHP_V8_TRY_CATCH(isolate);
278349
PHP_V8_INIT_ISOLATE_LIMITS_ON_OBJECT_VALUE(php_v8_value);
279350

280-
v8::Maybe<bool> maybe = local_obj->ForceSet(context,
281-
local_key,
282-
local_value_to_set,
283-
static_cast<v8::PropertyAttribute>(attributes));
351+
v8::Maybe<bool> maybe = local_obj->DefineOwnProperty(context,
352+
local_name,
353+
local_value_to_set,
354+
static_cast<v8::PropertyAttribute>(attributes));
284355

285356
PHP_V8_MAYBE_CATCH(php_v8_context, try_catch);
286-
PHP_V8_THROW_EXCEPTION_WHEN_NOTHING(maybe, "Failed to set persistent");
357+
PHP_V8_THROW_EXCEPTION_WHEN_NOTHING(maybe, "Failed to define own property");
287358

288359
RETURN_BOOL(maybe.FromJust())
289360
}
@@ -1334,10 +1405,21 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_object_SetIndex, ZEND_RETURN_
13341405
ZEND_ARG_OBJ_INFO(0, value, V8\\Value, 0)
13351406
ZEND_END_ARG_INFO()
13361407

1337-
// TODO: subject of change due to deprecateion
1338-
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_object_ForceSet, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 3)
1408+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_object_CreateDataProperty, ZEND_RETURN_VALUE, 3, _IS_BOOL, NULL, 0)
1409+
ZEND_ARG_OBJ_INFO(0, context, V8\\Context, 0)
1410+
ZEND_ARG_OBJ_INFO(0, key, V8\\NameValue, 0)
1411+
ZEND_ARG_OBJ_INFO(0, value, V8\\Value, 0)
1412+
ZEND_END_ARG_INFO()
1413+
1414+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_object_CreateDataPropertyIndex, ZEND_RETURN_VALUE, 3, _IS_BOOL, NULL, 0)
1415+
ZEND_ARG_OBJ_INFO(0, context, V8\\Context, 0)
1416+
ZEND_ARG_TYPE_INFO(0, key, IS_LONG, 0)
1417+
ZEND_ARG_OBJ_INFO(0, value, V8\\Value, 0)
1418+
ZEND_END_ARG_INFO()
1419+
1420+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_object_DefineOwnProperty, ZEND_RETURN_VALUE, 3, _IS_BOOL, NULL, 0)
13391421
ZEND_ARG_OBJ_INFO(0, context, V8\\Context, 0)
1340-
ZEND_ARG_INFO(0, key_or_index)
1422+
ZEND_ARG_OBJ_INFO(0, key, V8\\NameValue, 0)
13411423
ZEND_ARG_OBJ_INFO(0, value, V8\\Value, 0)
13421424
ZEND_ARG_INFO(0, attributes)
13431425
ZEND_END_ARG_INFO()
@@ -1510,7 +1592,9 @@ static const zend_function_entry php_v8_object_methods[] = {
15101592
PHP_ME(V8Object, GetContext, arginfo_v8_object_GetContext, ZEND_ACC_PUBLIC)
15111593
PHP_ME(V8Object, Set, arginfo_v8_object_Set, ZEND_ACC_PUBLIC)
15121594
PHP_ME(V8Object, SetIndex, arginfo_v8_object_SetIndex, ZEND_ACC_PUBLIC)
1513-
PHP_ME(V8Object, ForceSet, arginfo_v8_object_ForceSet, ZEND_ACC_PUBLIC)
1595+
PHP_ME(V8Object, CreateDataProperty, arginfo_v8_object_CreateDataProperty, ZEND_ACC_PUBLIC)
1596+
PHP_ME(V8Object, CreateDataPropertyIndex, arginfo_v8_object_CreateDataPropertyIndex, ZEND_ACC_PUBLIC)
1597+
PHP_ME(V8Object, DefineOwnProperty, arginfo_v8_object_DefineOwnProperty, ZEND_ACC_PUBLIC)
15141598
PHP_ME(V8Object, Get, arginfo_v8_object_Get, ZEND_ACC_PUBLIC)
15151599
PHP_ME(V8Object, GetIndex, arginfo_v8_object_GetIndex, ZEND_ACC_PUBLIC)
15161600
PHP_ME(V8Object, GetPropertyAttributes, arginfo_v8_object_GetPropertyAttributes, ZEND_ACC_PUBLIC)

stubs/src/ObjectValue.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,52 @@ public function SetIndex(Context $context, int $index, Value $value) : bool
5858
}
5959

6060
/**
61-
* Sets an own property on this object bypassing interceptors and
62-
* overriding accessors or read-only properties.
61+
* Implements CreateDataProperty (ECMA-262, 7.3.4).
6362
*
64-
* Note that if the object has an interceptor the property will be set
65-
* locally, but since the interceptor takes precedence the local property
66-
* will only be returned if the interceptor doesn't return a value.
63+
* Defines a configurable, writable, enumerable property with the given value
64+
* on the object unless the property already exists and is not configurable
65+
* or the object is not extensible.
6766
*
68-
* Note also that this only works for named properties.
67+
* @param Context $context
68+
* @param NameValue $key
69+
* @param Value $value
70+
*
71+
* @return bool
72+
*/
73+
public function CreateDataProperty(Context $context, NameValue $key, Value $value) : bool
74+
{
75+
}
76+
77+
/**
78+
* Implements CreateDataProperty (ECMA-262, 7.3.4).
79+
*
80+
* Defines a configurable, writable, enumerable property with the given value
81+
* on the object unless the property already exists and is not configurable
82+
* or the object is not extensible.
6983
*
7084
* @param Context $context
71-
* @param string $key
85+
* @param int $index
7286
* @param Value $value
73-
* @param int $attributes
7487
*
7588
* @return bool
7689
*/
77-
public function ForceSet(Context $context, $key, Value $value, $attributes = PropertyAttribute::None)
90+
public function CreateDataPropertyIndex(Context $context, int $index, Value $value) : bool
91+
{
92+
}
93+
94+
/**
95+
* Implements DefineOwnProperty.
96+
*
97+
* In general, CreateDataProperty will be faster, however, does not allow for specifying attributes.
98+
*
99+
* @param Context $context
100+
* @param NameValue $key
101+
* @param Value $value
102+
* @param int $attributes
103+
*
104+
* @return bool
105+
*/
106+
public function DefineOwnProperty(Context $context, NameValue $key, Value $value, int $attributes = PropertyAttribute::None) : bool
78107
{
79108
}
80109

tests/V8ArrayObject.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ V8\ArrayObject::CreationContext() matches expected value
129129
Converters:
130130
-----------
131131
V8\ArrayObject(V8\Value)->ToBoolean():
132-
object(V8\BooleanValue)#91 (1) {
132+
object(V8\BooleanValue)#93 (1) {
133133
["isolate":"V8\Value":private]=>
134134
object(V8\Isolate)#3 (5) {
135135
["snapshot":"V8\Isolate":private]=>
@@ -145,7 +145,7 @@ V8\ArrayObject(V8\Value)->ToBoolean():
145145
}
146146
}
147147
V8\ArrayObject(V8\Value)->ToNumber():
148-
object(V8\NumberValue)#91 (1) {
148+
object(V8\NumberValue)#93 (1) {
149149
["isolate":"V8\Value":private]=>
150150
object(V8\Isolate)#3 (5) {
151151
["snapshot":"V8\Isolate":private]=>
@@ -161,7 +161,7 @@ V8\ArrayObject(V8\Value)->ToNumber():
161161
}
162162
}
163163
V8\ArrayObject(V8\Value)->ToString():
164-
object(V8\StringValue)#91 (1) {
164+
object(V8\StringValue)#93 (1) {
165165
["isolate":"V8\Value":private]=>
166166
object(V8\Isolate)#3 (5) {
167167
["snapshot":"V8\Isolate":private]=>
@@ -177,7 +177,7 @@ V8\ArrayObject(V8\Value)->ToString():
177177
}
178178
}
179179
V8\ArrayObject(V8\Value)->ToDetailString():
180-
object(V8\StringValue)#91 (1) {
180+
object(V8\StringValue)#93 (1) {
181181
["isolate":"V8\Value":private]=>
182182
object(V8\Isolate)#3 (5) {
183183
["snapshot":"V8\Isolate":private]=>
@@ -246,7 +246,7 @@ V8\ArrayObject(V8\Value)->ToObject():
246246
}
247247
}
248248
V8\ArrayObject(V8\Value)->ToInteger():
249-
object(V8\NumberValue)#91 (1) {
249+
object(V8\NumberValue)#93 (1) {
250250
["isolate":"V8\Value":private]=>
251251
object(V8\Isolate)#3 (5) {
252252
["snapshot":"V8\Isolate":private]=>
@@ -262,7 +262,7 @@ V8\ArrayObject(V8\Value)->ToInteger():
262262
}
263263
}
264264
V8\ArrayObject(V8\Value)->ToUint32():
265-
object(V8\NumberValue)#91 (1) {
265+
object(V8\NumberValue)#93 (1) {
266266
["isolate":"V8\Value":private]=>
267267
object(V8\Isolate)#3 (5) {
268268
["snapshot":"V8\Isolate":private]=>
@@ -278,7 +278,7 @@ V8\ArrayObject(V8\Value)->ToUint32():
278278
}
279279
}
280280
V8\ArrayObject(V8\Value)->ToInt32():
281-
object(V8\NumberValue)#91 (1) {
281+
object(V8\NumberValue)#93 (1) {
282282
["isolate":"V8\Value":private]=>
283283
object(V8\Isolate)#3 (5) {
284284
["snapshot":"V8\Isolate":private]=>

tests/V8FunctionObject.phpt

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

116116
v8Tests\TrackingDtors\FunctionObject(V8\FunctionObject)->GetScriptOrigin():
117-
object(V8\ScriptOrigin)#105 (6) {
117+
object(V8\ScriptOrigin)#107 (6) {
118118
["resource_name":"V8\ScriptOrigin":private]=>
119119
string(0) ""
120120
["resource_line_offset":"V8\ScriptOrigin":private]=>
121121
int(0)
122122
["resource_column_offset":"V8\ScriptOrigin":private]=>
123123
int(0)
124124
["options":"V8\ScriptOrigin":private]=>
125-
object(V8\ScriptOriginOptions)#106 (3) {
125+
object(V8\ScriptOriginOptions)#108 (3) {
126126
["is_embedder_debug_script":"V8\ScriptOriginOptions":private]=>
127127
bool(false)
128128
["is_shared_cross_origin":"V8\ScriptOriginOptions":private]=>

tests/V8ObjectValue.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ V8\ObjectValue->GetIdentityHash(): int(%d)
114114
Converters:
115115
-----------
116116
V8\ObjectValue(V8\Value)->ToBoolean():
117-
object(V8\BooleanValue)#88 (1) {
117+
object(V8\BooleanValue)#90 (1) {
118118
["isolate":"V8\Value":private]=>
119119
object(V8\Isolate)#2 (5) {
120120
["snapshot":"V8\Isolate":private]=>
@@ -130,7 +130,7 @@ V8\ObjectValue(V8\Value)->ToBoolean():
130130
}
131131
}
132132
V8\ObjectValue(V8\Value)->ToNumber():
133-
object(V8\NumberValue)#88 (1) {
133+
object(V8\NumberValue)#90 (1) {
134134
["isolate":"V8\Value":private]=>
135135
object(V8\Isolate)#2 (5) {
136136
["snapshot":"V8\Isolate":private]=>
@@ -146,7 +146,7 @@ V8\ObjectValue(V8\Value)->ToNumber():
146146
}
147147
}
148148
V8\ObjectValue(V8\Value)->ToString():
149-
object(V8\StringValue)#88 (1) {
149+
object(V8\StringValue)#90 (1) {
150150
["isolate":"V8\Value":private]=>
151151
object(V8\Isolate)#2 (5) {
152152
["snapshot":"V8\Isolate":private]=>
@@ -162,7 +162,7 @@ V8\ObjectValue(V8\Value)->ToString():
162162
}
163163
}
164164
V8\ObjectValue(V8\Value)->ToDetailString():
165-
object(V8\StringValue)#88 (1) {
165+
object(V8\StringValue)#90 (1) {
166166
["isolate":"V8\Value":private]=>
167167
object(V8\Isolate)#2 (5) {
168168
["snapshot":"V8\Isolate":private]=>
@@ -231,7 +231,7 @@ V8\ObjectValue(V8\Value)->ToObject():
231231
}
232232
}
233233
V8\ObjectValue(V8\Value)->ToInteger():
234-
object(V8\NumberValue)#88 (1) {
234+
object(V8\NumberValue)#90 (1) {
235235
["isolate":"V8\Value":private]=>
236236
object(V8\Isolate)#2 (5) {
237237
["snapshot":"V8\Isolate":private]=>
@@ -247,7 +247,7 @@ V8\ObjectValue(V8\Value)->ToInteger():
247247
}
248248
}
249249
V8\ObjectValue(V8\Value)->ToUint32():
250-
object(V8\NumberValue)#88 (1) {
250+
object(V8\NumberValue)#90 (1) {
251251
["isolate":"V8\Value":private]=>
252252
object(V8\Isolate)#2 (5) {
253253
["snapshot":"V8\Isolate":private]=>
@@ -263,7 +263,7 @@ V8\ObjectValue(V8\Value)->ToUint32():
263263
}
264264
}
265265
V8\ObjectValue(V8\Value)->ToInt32():
266-
object(V8\NumberValue)#88 (1) {
266+
object(V8\NumberValue)#90 (1) {
267267
["isolate":"V8\Value":private]=>
268268
object(V8\Isolate)#2 (5) {
269269
["snapshot":"V8\Isolate":private]=>

tests/V8StringObject.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ StringObject extends ObjectValue: ok
127127
Getters:
128128
--------
129129
V8\StringObject->ValueOf():
130-
object(V8\StringValue)#91 (1) {
130+
object(V8\StringValue)#93 (1) {
131131
["isolate":"V8\Value":private]=>
132132
object(V8\Isolate)#3 (5) {
133133
["snapshot":"V8\Isolate":private]=>

tests/V8SymbolObject.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ SymbolObject extends ObjectValue: ok
130130
Getters:
131131
--------
132132
V8\SymbolObject->ValueOf():
133-
object(V8\SymbolValue)#91 (1) {
133+
object(V8\SymbolValue)#93 (1) {
134134
["isolate":"V8\Value":private]=>
135135
object(V8\Isolate)#3 (5) {
136136
["snapshot":"V8\Isolate":private]=>

0 commit comments

Comments
 (0)