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

Commit d0e07c7

Browse files
committed
Add immutable prototype chains to ObjectTemplate, closes #58
1 parent e5ac940 commit d0e07c7

4 files changed

+76
-12
lines changed

src/php_v8_object_template.cc

+47-12
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,33 @@ static PHP_METHOD(ObjectTemplate, setCallAsFunctionHandler) {
372372
local_template->SetCallAsFunctionHandler(callback, data);
373373
}
374374

375+
static PHP_METHOD(ObjectTemplate, isImmutableProto) {
376+
if (zend_parse_parameters_none() == FAILURE) {
377+
return;
378+
}
379+
380+
PHP_V8_FETCH_OBJECT_TEMPLATE_WITH_CHECK(getThis(), php_v8_object_template);
381+
PHP_V8_ENTER_STORED_ISOLATE(php_v8_object_template);
382+
383+
v8::Local<v8::ObjectTemplate> local_obj_tpl = php_v8_object_template_get_local(php_v8_object_template);
384+
385+
RETURN_BOOL(static_cast<zend_bool>(local_obj_tpl->IsImmutableProto()));
386+
}
387+
388+
static PHP_METHOD(ObjectTemplate, setImmutableProto) {
389+
if (zend_parse_parameters_none() == FAILURE) {
390+
return;
391+
}
392+
393+
PHP_V8_FETCH_OBJECT_TEMPLATE_WITH_CHECK(getThis(), php_v8_object_template);
394+
PHP_V8_ENTER_STORED_ISOLATE(php_v8_object_template);
395+
396+
v8::Local<v8::ObjectTemplate> local_obj_tpl = php_v8_object_template_get_local(php_v8_object_template);
397+
398+
local_obj_tpl->SetImmutableProto();
399+
}
400+
401+
375402
/* Non-standard, implementations of AdjustableExternalMemoryInterface::AdjustExternalAllocatedMemory */
376403
static PHP_METHOD(ObjectTemplate, adjustExternalAllocatedMemory) {
377404
php_v8_ext_mem_interface_object_template_AdjustExternalAllocatedMemory(INTERNAL_FUNCTION_PARAM_PASSTHRU);
@@ -443,6 +470,12 @@ PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_VOID_INFO_EX(arginfo_setCallAsFunctionHandler,
443470
ZEND_ARG_INFO(0, callback)
444471
ZEND_END_ARG_INFO()
445472

473+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_isImmutableProto, ZEND_RETURN_VALUE, 0, _IS_BOOL, 0)
474+
ZEND_END_ARG_INFO()
475+
476+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_VOID_INFO_EX(arginfo_setImmutableProto, 0)
477+
ZEND_END_ARG_INFO()
478+
446479
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_adjustExternalAllocatedMemory, ZEND_RETURN_VALUE, 1, IS_LONG, 0)
447480
ZEND_ARG_TYPE_INFO(0, change_in_bytes, IS_LONG, 0)
448481
ZEND_END_ARG_INFO()
@@ -453,18 +486,20 @@ ZEND_END_ARG_INFO()
453486

454487

455488
static const zend_function_entry php_v8_object_template_methods[] = {
456-
PHP_V8_ME(ObjectTemplate, __construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
457-
PHP_V8_ME(ObjectTemplate, getIsolate, ZEND_ACC_PUBLIC)
458-
PHP_V8_ME(ObjectTemplate, set, ZEND_ACC_PUBLIC)
459-
PHP_V8_ME(ObjectTemplate, setAccessorProperty, ZEND_ACC_PUBLIC)
460-
PHP_V8_ME(ObjectTemplate, setNativeDataProperty, ZEND_ACC_PUBLIC)
461-
PHP_V8_ME(ObjectTemplate, newInstance, ZEND_ACC_PUBLIC)
462-
PHP_V8_ME(ObjectTemplate, setAccessor, ZEND_ACC_PUBLIC)
463-
PHP_V8_ME(ObjectTemplate, setHandlerForNamedProperty, ZEND_ACC_PUBLIC)
464-
PHP_V8_ME(ObjectTemplate, setHandlerForIndexedProperty, ZEND_ACC_PUBLIC)
465-
PHP_V8_ME(ObjectTemplate, setCallAsFunctionHandler, ZEND_ACC_PUBLIC)
466-
PHP_V8_ME(ObjectTemplate, adjustExternalAllocatedMemory, ZEND_ACC_PUBLIC)
467-
PHP_V8_ME(ObjectTemplate, getExternalAllocatedMemory, ZEND_ACC_PUBLIC)
489+
PHP_V8_ME(ObjectTemplate, __construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
490+
PHP_V8_ME(ObjectTemplate, getIsolate, ZEND_ACC_PUBLIC)
491+
PHP_V8_ME(ObjectTemplate, set, ZEND_ACC_PUBLIC)
492+
PHP_V8_ME(ObjectTemplate, setAccessorProperty, ZEND_ACC_PUBLIC)
493+
PHP_V8_ME(ObjectTemplate, setNativeDataProperty, ZEND_ACC_PUBLIC)
494+
PHP_V8_ME(ObjectTemplate, newInstance, ZEND_ACC_PUBLIC)
495+
PHP_V8_ME(ObjectTemplate, setAccessor, ZEND_ACC_PUBLIC)
496+
PHP_V8_ME(ObjectTemplate, setHandlerForNamedProperty, ZEND_ACC_PUBLIC)
497+
PHP_V8_ME(ObjectTemplate, setHandlerForIndexedProperty, ZEND_ACC_PUBLIC)
498+
PHP_V8_ME(ObjectTemplate, setCallAsFunctionHandler, ZEND_ACC_PUBLIC)
499+
PHP_V8_ME(ObjectTemplate, isImmutableProto, ZEND_ACC_PUBLIC)
500+
PHP_V8_ME(ObjectTemplate, setImmutableProto, ZEND_ACC_PUBLIC)
501+
PHP_V8_ME(ObjectTemplate, adjustExternalAllocatedMemory, ZEND_ACC_PUBLIC)
502+
PHP_V8_ME(ObjectTemplate, getExternalAllocatedMemory, ZEND_ACC_PUBLIC)
468503

469504
PHP_FE_END
470505
};

stubs/src/ObjectTemplate.php

+20
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ public function setCallAsFunctionHandler(callable $callback)
120120
{
121121
}
122122

123+
124+
/**
125+
* Returns true if the object will be an immutable prototype exotic object.
126+
*
127+
* @return bool
128+
*/
129+
public function isImmutableProto(): bool
130+
{
131+
}
132+
133+
/**
134+
* Makes the ObjectTempate for an immutable prototype exotic object, with an
135+
* immutable __proto__.
136+
*
137+
* @return bool
138+
*/
139+
public function setImmutableProto(): bool
140+
{
141+
}
142+
123143
/**
124144
* {@inheritdoc}
125145
*/

tests/001-verify_extension_entities.phpt

+2
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,8 @@ class V8\ObjectTemplate
843843
public function setHandlerForNamedProperty(V8\NamedPropertyHandlerConfiguration $configuration)
844844
public function setHandlerForIndexedProperty(V8\IndexedPropertyHandlerConfiguration $configuration)
845845
public function setCallAsFunctionHandler($callback)
846+
public function isImmutableProto(): bool
847+
public function setImmutableProto(): bool
846848
public function adjustExternalAllocatedMemory(int $change_in_bytes): int
847849
public function getExternalAllocatedMemory(): int
848850

tests/ObjectTemplate.phpt

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ $instance = $value->newInstance($context);
4444

4545
$helper->assert('ObjectTemplate instance has name from constructor', $instance->getConstructorName()->value() == 'TestConstructor');
4646

47+
$helper->line();
48+
$helper->assert('Object template is not immutable prototype by default', $value->isImmutableProto(), false);
49+
$value->setImmutableProto();
50+
$helper->assert('Object template is now set to be immutable prototype', $value->isImmutableProto(), true);
4751

4852
?>
4953
--EXPECT--
@@ -64,3 +68,6 @@ Accessors:
6468
V8\ObjectTemplate::getIsolate() matches expected value
6569

6670
ObjectTemplate instance has name from constructor: ok
71+
72+
Object template is not immutable prototype by default: ok
73+
Object template is now set to be immutable prototype: ok

0 commit comments

Comments
 (0)