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

Commit 6e48845

Browse files
committed
Add FunctionObject::getScriptId() method
1 parent 241d0f1 commit 6e48845

File tree

5 files changed

+62
-15
lines changed

5 files changed

+62
-15
lines changed

Diff for: src/php_v8_function.cc

+25
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,27 @@ static PHP_METHOD(Function, getScriptColumnNumber) {
541541
RETURN_LONG((zend_long) column_number);
542542
}
543543

544+
static PHP_METHOD(Function, getScriptId) {
545+
546+
if (zend_parse_parameters_none() == FAILURE) {
547+
return;
548+
}
549+
550+
PHP_V8_VALUE_FETCH_WITH_CHECK(getThis(), php_v8_value);
551+
PHP_V8_ENTER_STORED_ISOLATE(php_v8_value);
552+
PHP_V8_ENTER_STORED_CONTEXT(php_v8_value);
553+
554+
v8::Local<v8::Function> local_function = php_v8_value_get_local_as<v8::Function>(php_v8_value);
555+
556+
int script_id = local_function->ScriptId();
557+
558+
if (script_id == v8::Message::kNoScriptIdInfo) {
559+
RETURN_NULL();
560+
}
561+
562+
RETURN_LONG((zend_long) script_id);
563+
}
564+
544565
static PHP_METHOD(Function, getBoundFunction) {
545566
if (zend_parse_parameters_none() == FAILURE) {
546567
return;
@@ -610,6 +631,9 @@ ZEND_END_ARG_INFO()
610631
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_getScriptColumnNumber, ZEND_RETURN_VALUE, 0, IS_LONG, 1)
611632
ZEND_END_ARG_INFO()
612633

634+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_getScriptId, ZEND_RETURN_VALUE, 0, IS_LONG, 1)
635+
ZEND_END_ARG_INFO()
636+
613637
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_getBoundFunction, ZEND_RETURN_VALUE, 0, V8\\Value, 0)
614638
ZEND_END_ARG_INFO()
615639

@@ -626,6 +650,7 @@ static const zend_function_entry php_v8_object_methods[] = {
626650
PHP_V8_ME(Function, getDisplayName, ZEND_ACC_PUBLIC)
627651
PHP_V8_ME(Function, getScriptLineNumber, ZEND_ACC_PUBLIC)
628652
PHP_V8_ME(Function, getScriptColumnNumber, ZEND_ACC_PUBLIC)
653+
PHP_V8_ME(Function, getScriptId, ZEND_ACC_PUBLIC)
629654
PHP_V8_ME(Function, getBoundFunction, ZEND_ACC_PUBLIC)
630655
PHP_V8_ME(Function, getScriptOrigin, ZEND_ACC_PUBLIC)
631656

Diff for: stubs/src/FunctionObject.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,19 @@ public function getScriptColumnNumber(): ?int
113113
}
114114

115115

116-
///**
117-
// * Returns scriptId.
118-
// */
119-
//int ScriptId() const;
116+
/**
117+
* Returns script id where function was created and null if no information available.
118+
*
119+
* @return int
120+
*/
121+
public function getScriptId(): ?int
122+
{
123+
}
120124

121125
/**
122-
* Returns the original function if this function is bound, else returns
123-
* v8::Undefined.
126+
* Returns the original function if this function is bound, else returns UndefinedValue.
124127
*
125-
* @return Value
128+
* @return FunctionObject|UndefinedValue|Value
126129
*/
127130
public function getBoundFunction(): Value
128131
{

Diff for: tests/.testsuite.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,17 @@ public function method_matches_instanceof($object, $method, $expected, array $ar
197197
echo get_class($object), '::', $method, '() result', ($object->$method(...$args) instanceof $expected ? ' is' : ' not an'), ' instance of ', $expected, PHP_EOL;
198198
}
199199

200-
201200
public function method_matches_with_output($object, $method, $expected, array $args = [])
202201
{
203-
echo get_class($object), '::', $method, '()', ' ', ($expected === $object->$method(...$args) ? 'matches' : 'doesn\'t match'), ' expected ', var_export($expected,
204-
true), PHP_EOL;
202+
echo get_class($object), '::', $method, '()', ' ';
203+
echo ($expected === ($res = $object->$method(...$args)) ? 'matches' : 'doesn\'t match');
204+
echo ' expected ', var_export($expected, true);
205+
206+
if ($expected !== $res) {
207+
echo ', given ', var_export($res, true);
208+
}
209+
210+
echo PHP_EOL;
205211
}
206212

207213
public function method_matches_with_dump($object, $method, $expected, array $args = [])

Diff for: tests/001-verify_extension_entities.phpt

+1
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ class V8\FunctionObject
721721
public function getDisplayName(): V8\Value
722722
public function getScriptLineNumber(): ?int
723723
public function getScriptColumnNumber(): ?int
724+
public function getScriptId(): ?int
724725
public function getBoundFunction(): V8\Value
725726
public function getScriptOrigin(): V8\ScriptOrigin
726727

Diff for: tests/FunctionObject.phpt

+17-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ $v8_helper = new PhpV8Helpers($helper);
1515

1616
require '.tracking_dtors.php';
1717

18-
$isolate = new v8Tests\TrackingDtors\Isolate();
18+
$isolate = new v8Tests\TrackingDtors\Isolate();
1919
$global_template = new V8\ObjectTemplate($isolate);
20-
$context = new V8\Context($isolate, $global_template);
20+
$context = new V8\Context($isolate, $global_template);
2121

2222

2323
$func = new v8Tests\TrackingDtors\FunctionObject($context, function (\V8\FunctionCallbackInfo $info) {
@@ -33,24 +33,32 @@ $helper->space();
3333
$helper->assert('FunctionObject extends ObjectValue', $func instanceof \V8\ObjectValue);
3434
$helper->assert('FunctionObject implements AdjustableExternalMemoryInterface', $func instanceof \V8\AdjustableExternalMemoryInterface);
3535
$helper->assert('FunctionObject is instanceof Function', $func->instanceOf($context, $context->globalObject()->get($context, new \V8\StringValue($isolate, 'Function'))));
36+
$helper->assert('Function created from php holds no script id', $func->getScriptId() === null);
3637
$helper->line();
3738

3839
$v8_helper->run_checks($func, 'Checkers');
3940

4041
$context->globalObject()->set($context, new \V8\StringValue($isolate, 'print'), $func);
4142

42-
$source = 'print("Hello, world"); delete print; "Script done"';
43+
$source = 'print("Hello, world"); delete print; "Script done"';
4344
$file_name = 'test.js';
4445

4546

4647
$script = new V8\Script($context, new \V8\StringValue($isolate, $source), new \V8\ScriptOrigin($file_name));
4748

4849
$helper->dump($script->run($context)->toString($context)->value());
50+
51+
$helper->assert('Function created from php still holds no script id after been passed to script', $func->getScriptId() === null);
52+
4953
$helper->line();
5054

5155
$helper->dump_object_methods($func, [], new ArrayMapFilter(['getScriptOrigin' => true]));
5256
$helper->line();
5357

58+
$helper->line();
59+
$fnc2 = $v8_helper->CompileRun($context, 'function test() {}; test');
60+
$helper->assert('Function from script holds script id', $fnc2->getScriptId() !== null);
61+
5462
echo 'We are done for now', PHP_EOL;
5563

5664
?>
@@ -73,6 +81,7 @@ object(v8Tests\TrackingDtors\FunctionObject)#6 (2) {
7381
FunctionObject extends ObjectValue: ok
7482
FunctionObject implements AdjustableExternalMemoryInterface: ok
7583
FunctionObject is instanceof Function: ok
84+
Function created from php holds no script id: ok
7685

7786
Checkers:
7887
---------
@@ -132,17 +141,18 @@ v8Tests\TrackingDtors\FunctionObject(V8\Value)->isProxy(): bool(false)
132141

133142
Should output Hello World string
134143
string(11) "Script done"
144+
Function created from php still holds no script id after been passed to script: ok
135145

136146
v8Tests\TrackingDtors\FunctionObject(V8\FunctionObject)->getScriptOrigin():
137-
object(V8\ScriptOrigin)#128 (6) {
147+
object(V8\ScriptOrigin)#129 (6) {
138148
["resource_name":"V8\ScriptOrigin":private]=>
139149
string(0) ""
140150
["resource_line_offset":"V8\ScriptOrigin":private]=>
141151
int(0)
142152
["resource_column_offset":"V8\ScriptOrigin":private]=>
143153
int(0)
144154
["options":"V8\ScriptOrigin":private]=>
145-
object(V8\ScriptOriginOptions)#132 (4) {
155+
object(V8\ScriptOriginOptions)#133 (4) {
146156
["is_shared_cross_origin":"V8\ScriptOriginOptions":private]=>
147157
bool(false)
148158
["is_opaque":"V8\ScriptOriginOptions":private]=>
@@ -158,6 +168,8 @@ v8Tests\TrackingDtors\FunctionObject(V8\FunctionObject)->getScriptOrigin():
158168
string(0) ""
159169
}
160170

171+
172+
Function from script holds script id: ok
161173
We are done for now
162174
FunctionObject dies now!
163175
Isolate dies now!

0 commit comments

Comments
 (0)