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

Commit d63cf3d

Browse files
committed
Enforce Value() method on all V8\Primitive values
1 parent ed7f151 commit d63cf3d

8 files changed

+83
-43
lines changed

src/php_v8_string.cc

+5-6
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,24 @@ static PHP_METHOD(V8String, ContainsOnlyOneByte)
127127
}
128128

129129

130-
131130
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_string___construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 2)
132131
ZEND_ARG_OBJ_INFO(0, isolate, V8\\Isolate, 0)
133132
ZEND_ARG_INFO(0, data)
134133
ZEND_END_ARG_INFO()
135134

136-
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_string_Value, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
135+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_string_Value, ZEND_RETURN_VALUE, 0, IS_STRING, 0)
137136
ZEND_END_ARG_INFO()
138137

139-
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_string_Length, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
138+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_string_Length, ZEND_RETURN_VALUE, 0, IS_LONG, 0)
140139
ZEND_END_ARG_INFO()
141140

142-
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_string_Utf8Length, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
141+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_string_Utf8Length, ZEND_RETURN_VALUE, 0, IS_LONG, 0)
143142
ZEND_END_ARG_INFO()
144143

145-
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_string_IsOneByte, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
144+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_string_IsOneByte, ZEND_RETURN_VALUE, 0, _IS_BOOL, 0)
146145
ZEND_END_ARG_INFO()
147146

148-
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_string_ContainsOnlyOneByte, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
147+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_string_ContainsOnlyOneByte, ZEND_RETURN_VALUE, 0, _IS_BOOL, 0)
149148
ZEND_END_ARG_INFO()
150149

151150

src/php_v8_symbol.cc

+28
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,29 @@ static PHP_METHOD(V8Symbol, __construct) {
5353
php_v8_value->persistent->Reset(isolate, local_symbol);
5454
}
5555

56+
static PHP_METHOD(V8Symbol, Value)
57+
{
58+
if (zend_parse_parameters_none() == FAILURE) {
59+
return;
60+
}
61+
62+
PHP_V8_VALUE_FETCH_WITH_CHECK(getThis(), php_v8_value);
63+
PHP_V8_ENTER_STORED_ISOLATE(php_v8_value);
64+
65+
v8::Local<v8::Symbol> local_symbol = php_v8_value_get_local_as<v8::Symbol>(php_v8_value);
66+
v8::Local<v8::Value> local_name = local_symbol->Name();
67+
68+
if (local_name->IsUndefined()) {
69+
RETURN_EMPTY_STRING();
70+
}
71+
72+
v8::String::Utf8Value str(local_name);
73+
74+
PHP_V8_CONVERT_UTF8VALUE_TO_STRING_WITH_CHECK(str, cstr);
75+
76+
RETVAL_STRINGL(cstr, str.length());
77+
}
78+
5679
static PHP_METHOD(V8Symbol, Name)
5780
{
5881
if (zend_parse_parameters_none() == FAILURE) {
@@ -154,6 +177,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_symbol___construct, ZEND_SEND_BY_VAL, ZEND_RET
154177
ZEND_ARG_OBJ_INFO(0, name, V8\\StringValue, 1)
155178
ZEND_END_ARG_INFO()
156179

180+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_symbol_Value, ZEND_RETURN_VALUE, 0, IS_STRING, 0)
181+
ZEND_END_ARG_INFO()
182+
157183
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_v8_symbol_Name, ZEND_RETURN_VALUE, 0, V8\\Value, 0)
158184
ZEND_END_ARG_INFO()
159185

@@ -188,6 +214,8 @@ PHP_V8_SYMBOL_WELL_KNOWN_ARGS(arginfo_v8_symbol_GetUnscopables);
188214
static const zend_function_entry php_v8_symbol_methods[] = {
189215
PHP_ME(V8Symbol, __construct, arginfo_v8_symbol___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
190216

217+
PHP_ME(V8Symbol, Value, arginfo_v8_symbol_Value, ZEND_ACC_PUBLIC)
218+
191219
PHP_ME(V8Symbol, Name, arginfo_v8_symbol_Name, ZEND_ACC_PUBLIC)
192220

193221
PHP_ME(V8Symbol, For, arginfo_v8_symbol_For, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)

src/php_v8_undefined.cc

+15
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,29 @@ static PHP_METHOD(V8Undefined, __construct) {
3535
php_v8_value->persistent->Reset(isolate, v8::Undefined(isolate));
3636
}
3737

38+
static PHP_METHOD(V8Undefined, Value)
39+
{
40+
if (zend_parse_parameters_none() == FAILURE) {
41+
return;
42+
}
43+
PHP_V8_VALUE_FETCH_WITH_CHECK(getThis(), php_v8_value);
44+
45+
RETURN_NULL()
46+
}
47+
3848

3949
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_undefined___construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
4050
ZEND_ARG_OBJ_INFO(0, isolate, V8\\Isolate, 0)
4151
ZEND_END_ARG_INFO()
4252

53+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_undefined_Value, ZEND_RETURN_VALUE, 0, IS_NULL, 0)
54+
ZEND_END_ARG_INFO()
55+
4356

4457
static const zend_function_entry php_v8_undefined_methods[] = {
4558
PHP_ME(V8Undefined, __construct, arginfo_v8_undefined___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
59+
PHP_ME(V8Undefined, Value, arginfo_v8_undefined_Value, ZEND_ACC_PUBLIC)
60+
4661
PHP_FE_END
4762
};
4863

stubs/src/PrimitiveValue.php

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
*/
2222
abstract class PrimitiveValue extends Value
2323
{
24+
abstract function Value();
2425
}

stubs/src/StringValue.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,35 @@ public function __construct(Isolate $isolate, $data = '')
3434
/**
3535
* @return string
3636
*/
37-
public function Value()
37+
public function Value(): string
3838
{
3939
}
4040

4141
/**
4242
* @return int
4343
*/
44-
public function Length()
44+
public function Length(): int
4545
{
4646
}
4747

4848
/**
4949
* @return int
5050
*/
51-
public function Utf8Length()
51+
public function Utf8Length(): int
5252
{
5353
}
5454

5555
/**
5656
* @return bool
5757
*/
58-
public function IsOneByte()
58+
public function IsOneByte(): bool
5959
{
6060
}
6161

6262
/**
6363
* @return bool
6464
*/
65-
public function ContainsOnlyOneByte()
65+
public function ContainsOnlyOneByte():bool
6666
{
6767
}
6868
}

stubs/src/SymbolValue.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@ public function __construct(Isolate $isolate, StringValue $name = null)
3333
{
3434
}
3535

36+
/**
37+
* @return string
38+
*/
39+
public function Value(): string
40+
{
41+
}
42+
3643
/**
3744
* Returns the print name string of the symbol, or undefined if none.
3845
*
39-
* @return StringValue | Value
46+
* @return StringValue | UndefinedValue | Value
4047
*/
4148
public function Name(): Value
4249
{

tests/V8SymbolValue.phpt

+18-30
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ $helper->line();
3030

3131
$helper->header('Accessors');
3232
$helper->method_matches($value, 'GetIsolate', $isolate);
33-
$helper->method_export($value, 'Name');
33+
$helper->method_export($value, 'Value');
34+
$helper->assert('Name() is undefined', $value->Name() instanceof \V8\UndefinedValue);
3435
$helper->assert('GetIdentityHash is integer', gettype($value->GetIdentityHash()), 'integer');
3536
$helper->space();
3637

@@ -50,7 +51,8 @@ $helper->line();
5051

5152
$helper->header('Accessors');
5253
$helper->method_matches($value, 'GetIsolate', $isolate);
53-
$helper->method_export($value, 'Name');
54+
$helper->method_export($value, 'Value');
55+
$helper->assert('Name() is undefined', $value->Name() instanceof \V8\UndefinedValue);
5456
$helper->assert('GetIdentityHash is integer', gettype($value->GetIdentityHash()), 'integer');
5557
$helper->space();
5658

@@ -69,7 +71,8 @@ $helper->line();
6971

7072
$helper->header('Accessors');
7173
$helper->method_matches($value, 'GetIsolate', $isolate);
72-
$helper->method_export($value, 'Name');
74+
$helper->method_export($value, 'Value');
75+
$helper->assert('Name() is String', $value->Name() instanceof \V8\StringValue);
7376
$helper->assert('GetIdentityHash is integer', gettype($value->GetIdentityHash()), 'integer');
7477
$helper->space();
7578

@@ -93,7 +96,8 @@ $helper->line();
9396

9497
$helper->header('Accessors');
9598
$helper->method_matches($value, 'GetIsolate', $isolate);
96-
$helper->method_export($value, 'Name');
99+
$helper->method_export($value, 'Value');
100+
$helper->assert('Name() is String', $value->Name() instanceof \V8\StringValue);
97101
$helper->assert('GetIdentityHash is integer', gettype($value->GetIdentityHash()), 'integer');
98102
$helper->space();
99103

@@ -186,12 +190,8 @@ SymbolValue extends Value: ok
186190
Accessors:
187191
----------
188192
V8\SymbolValue::GetIsolate() matches expected value
189-
V8\SymbolValue->Name():
190-
object(V8\UndefinedValue)#92 (1) {
191-
["isolate":"V8\Value":private]=>
192-
object(V8\Isolate)#3 (0) {
193-
}
194-
}
193+
V8\SymbolValue->Value(): string(0) ""
194+
Name() is undefined: ok
195195
GetIdentityHash is integer: ok
196196

197197

@@ -266,12 +266,8 @@ SymbolValue extends NameValue: ok
266266
Accessors:
267267
----------
268268
V8\SymbolValue::GetIsolate() matches expected value
269-
V8\SymbolValue->Name():
270-
object(V8\UndefinedValue)#7 (1) {
271-
["isolate":"V8\Value":private]=>
272-
object(V8\Isolate)#3 (0) {
273-
}
274-
}
269+
V8\SymbolValue->Value(): string(0) ""
270+
Name() is undefined: ok
275271
GetIdentityHash is integer: ok
276272

277273

@@ -334,7 +330,7 @@ Empty StringValue constructor:
334330

335331
Object representation:
336332
----------------------
337-
object(V8\SymbolValue)#7 (1) {
333+
object(V8\SymbolValue)#5 (1) {
338334
["isolate":"V8\Value":private]=>
339335
object(V8\Isolate)#3 (0) {
340336
}
@@ -346,12 +342,8 @@ SymbolValue extends NameValue: ok
346342
Accessors:
347343
----------
348344
V8\SymbolValue::GetIsolate() matches expected value
349-
V8\SymbolValue->Name():
350-
object(V8\StringValue)#8 (1) {
351-
["isolate":"V8\Value":private]=>
352-
object(V8\Isolate)#3 (0) {
353-
}
354-
}
345+
V8\SymbolValue->Value(): string(0) ""
346+
Name() is String: ok
355347
GetIdentityHash is integer: ok
356348

357349

@@ -418,7 +410,7 @@ Non-empty StringValue constructor:
418410

419411
Object representation:
420412
----------------------
421-
object(V8\SymbolValue)#8 (1) {
413+
object(V8\SymbolValue)#4 (1) {
422414
["isolate":"V8\Value":private]=>
423415
object(V8\Isolate)#3 (0) {
424416
}
@@ -430,12 +422,8 @@ SymbolValue extends NameValue: ok
430422
Accessors:
431423
----------
432424
V8\SymbolValue::GetIsolate() matches expected value
433-
V8\SymbolValue->Name():
434-
object(V8\StringValue)#93 (1) {
435-
["isolate":"V8\Value":private]=>
436-
object(V8\Isolate)#3 (0) {
437-
}
438-
}
425+
V8\SymbolValue->Value(): string(4) "test"
426+
Name() is String: ok
439427
GetIdentityHash is integer: ok
440428

441429

tests/V8Undefined.phpt

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ $helper->line();
3939

4040
$helper->header('Accessors');
4141
$helper->method_matches($value, 'GetIsolate', $isolate);
42+
$helper->method_export($value, 'Value');
4243
$helper->space();
4344

4445
$v8_helper->run_checks($value);
@@ -94,6 +95,7 @@ Default Value is not an instance of Function: ok
9495
Accessors:
9596
----------
9697
V8\UndefinedValue::GetIsolate() matches expected value
98+
V8\UndefinedValue->Value(): NULL
9799

98100

99101
Checks on V8\UndefinedValue:
@@ -158,7 +160,7 @@ V8\UndefinedValue(V8\Value)->NumberValue(): float(NAN)
158160

159161
V8\UndefinedValue::ToString() converting:
160162
-----------------------------------------
161-
object(V8\StringValue)#88 (1) {
163+
object(V8\StringValue)#89 (1) {
162164
["isolate":"V8\Value":private]=>
163165
object(V8\Isolate)#3 (0) {
164166
}

0 commit comments

Comments
 (0)