From d63cf3dc86678fb8dc0ee64cee7dbe5d0720dee0 Mon Sep 17 00:00:00 2001 From: Bogdan Padalko Date: Wed, 3 May 2017 19:31:58 +0300 Subject: [PATCH] Enforce Value() method on all V8\Primitive values --- src/php_v8_string.cc | 11 ++++----- src/php_v8_symbol.cc | 28 +++++++++++++++++++++ src/php_v8_undefined.cc | 15 +++++++++++ stubs/src/PrimitiveValue.php | 1 + stubs/src/StringValue.php | 10 ++++---- stubs/src/SymbolValue.php | 9 ++++++- tests/V8SymbolValue.phpt | 48 ++++++++++++++---------------------- tests/V8Undefined.phpt | 4 ++- 8 files changed, 83 insertions(+), 43 deletions(-) diff --git a/src/php_v8_string.cc b/src/php_v8_string.cc index 465907e..c2412a0 100644 --- a/src/php_v8_string.cc +++ b/src/php_v8_string.cc @@ -127,25 +127,24 @@ static PHP_METHOD(V8String, ContainsOnlyOneByte) } - ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_string___construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 2) ZEND_ARG_OBJ_INFO(0, isolate, V8\\Isolate, 0) ZEND_ARG_INFO(0, data) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_string_Value, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0) +PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_string_Value, ZEND_RETURN_VALUE, 0, IS_STRING, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_string_Length, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0) +PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_string_Length, ZEND_RETURN_VALUE, 0, IS_LONG, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_string_Utf8Length, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0) +PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_string_Utf8Length, ZEND_RETURN_VALUE, 0, IS_LONG, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_string_IsOneByte, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0) +PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_string_IsOneByte, ZEND_RETURN_VALUE, 0, _IS_BOOL, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_string_ContainsOnlyOneByte, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0) +PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_string_ContainsOnlyOneByte, ZEND_RETURN_VALUE, 0, _IS_BOOL, 0) ZEND_END_ARG_INFO() diff --git a/src/php_v8_symbol.cc b/src/php_v8_symbol.cc index c68730c..7bfae52 100644 --- a/src/php_v8_symbol.cc +++ b/src/php_v8_symbol.cc @@ -53,6 +53,29 @@ static PHP_METHOD(V8Symbol, __construct) { php_v8_value->persistent->Reset(isolate, local_symbol); } +static PHP_METHOD(V8Symbol, Value) +{ + if (zend_parse_parameters_none() == FAILURE) { + return; + } + + PHP_V8_VALUE_FETCH_WITH_CHECK(getThis(), php_v8_value); + PHP_V8_ENTER_STORED_ISOLATE(php_v8_value); + + v8::Local local_symbol = php_v8_value_get_local_as(php_v8_value); + v8::Local local_name = local_symbol->Name(); + + if (local_name->IsUndefined()) { + RETURN_EMPTY_STRING(); + } + + v8::String::Utf8Value str(local_name); + + PHP_V8_CONVERT_UTF8VALUE_TO_STRING_WITH_CHECK(str, cstr); + + RETVAL_STRINGL(cstr, str.length()); +} + static PHP_METHOD(V8Symbol, Name) { 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 ZEND_ARG_OBJ_INFO(0, name, V8\\StringValue, 1) ZEND_END_ARG_INFO() +PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_symbol_Value, ZEND_RETURN_VALUE, 0, IS_STRING, 0) +ZEND_END_ARG_INFO() + PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_v8_symbol_Name, ZEND_RETURN_VALUE, 0, V8\\Value, 0) ZEND_END_ARG_INFO() @@ -188,6 +214,8 @@ PHP_V8_SYMBOL_WELL_KNOWN_ARGS(arginfo_v8_symbol_GetUnscopables); static const zend_function_entry php_v8_symbol_methods[] = { PHP_ME(V8Symbol, __construct, arginfo_v8_symbol___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) + PHP_ME(V8Symbol, Value, arginfo_v8_symbol_Value, ZEND_ACC_PUBLIC) + PHP_ME(V8Symbol, Name, arginfo_v8_symbol_Name, ZEND_ACC_PUBLIC) PHP_ME(V8Symbol, For, arginfo_v8_symbol_For, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) diff --git a/src/php_v8_undefined.cc b/src/php_v8_undefined.cc index 1a8b1ae..4f28e11 100644 --- a/src/php_v8_undefined.cc +++ b/src/php_v8_undefined.cc @@ -35,14 +35,29 @@ static PHP_METHOD(V8Undefined, __construct) { php_v8_value->persistent->Reset(isolate, v8::Undefined(isolate)); } +static PHP_METHOD(V8Undefined, Value) +{ + if (zend_parse_parameters_none() == FAILURE) { + return; + } + PHP_V8_VALUE_FETCH_WITH_CHECK(getThis(), php_v8_value); + + RETURN_NULL() +} + ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_undefined___construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1) ZEND_ARG_OBJ_INFO(0, isolate, V8\\Isolate, 0) ZEND_END_ARG_INFO() +PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_undefined_Value, ZEND_RETURN_VALUE, 0, IS_NULL, 0) +ZEND_END_ARG_INFO() + static const zend_function_entry php_v8_undefined_methods[] = { PHP_ME(V8Undefined, __construct, arginfo_v8_undefined___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) + PHP_ME(V8Undefined, Value, arginfo_v8_undefined_Value, ZEND_ACC_PUBLIC) + PHP_FE_END }; diff --git a/stubs/src/PrimitiveValue.php b/stubs/src/PrimitiveValue.php index d13b3fb..d38bd02 100644 --- a/stubs/src/PrimitiveValue.php +++ b/stubs/src/PrimitiveValue.php @@ -21,4 +21,5 @@ */ abstract class PrimitiveValue extends Value { + abstract function Value(); } diff --git a/stubs/src/StringValue.php b/stubs/src/StringValue.php index da067c3..0f3ae23 100644 --- a/stubs/src/StringValue.php +++ b/stubs/src/StringValue.php @@ -34,35 +34,35 @@ public function __construct(Isolate $isolate, $data = '') /** * @return string */ - public function Value() + public function Value(): string { } /** * @return int */ - public function Length() + public function Length(): int { } /** * @return int */ - public function Utf8Length() + public function Utf8Length(): int { } /** * @return bool */ - public function IsOneByte() + public function IsOneByte(): bool { } /** * @return bool */ - public function ContainsOnlyOneByte() + public function ContainsOnlyOneByte():bool { } } diff --git a/stubs/src/SymbolValue.php b/stubs/src/SymbolValue.php index 3951e71..c9438c7 100644 --- a/stubs/src/SymbolValue.php +++ b/stubs/src/SymbolValue.php @@ -33,10 +33,17 @@ public function __construct(Isolate $isolate, StringValue $name = null) { } + /** + * @return string + */ + public function Value(): string + { + } + /** * Returns the print name string of the symbol, or undefined if none. * - * @return StringValue | Value + * @return StringValue | UndefinedValue | Value */ public function Name(): Value { diff --git a/tests/V8SymbolValue.phpt b/tests/V8SymbolValue.phpt index bb0aecc..13b2495 100644 --- a/tests/V8SymbolValue.phpt +++ b/tests/V8SymbolValue.phpt @@ -30,7 +30,8 @@ $helper->line(); $helper->header('Accessors'); $helper->method_matches($value, 'GetIsolate', $isolate); -$helper->method_export($value, 'Name'); +$helper->method_export($value, 'Value'); +$helper->assert('Name() is undefined', $value->Name() instanceof \V8\UndefinedValue); $helper->assert('GetIdentityHash is integer', gettype($value->GetIdentityHash()), 'integer'); $helper->space(); @@ -50,7 +51,8 @@ $helper->line(); $helper->header('Accessors'); $helper->method_matches($value, 'GetIsolate', $isolate); -$helper->method_export($value, 'Name'); +$helper->method_export($value, 'Value'); +$helper->assert('Name() is undefined', $value->Name() instanceof \V8\UndefinedValue); $helper->assert('GetIdentityHash is integer', gettype($value->GetIdentityHash()), 'integer'); $helper->space(); @@ -69,7 +71,8 @@ $helper->line(); $helper->header('Accessors'); $helper->method_matches($value, 'GetIsolate', $isolate); -$helper->method_export($value, 'Name'); +$helper->method_export($value, 'Value'); +$helper->assert('Name() is String', $value->Name() instanceof \V8\StringValue); $helper->assert('GetIdentityHash is integer', gettype($value->GetIdentityHash()), 'integer'); $helper->space(); @@ -93,7 +96,8 @@ $helper->line(); $helper->header('Accessors'); $helper->method_matches($value, 'GetIsolate', $isolate); -$helper->method_export($value, 'Name'); +$helper->method_export($value, 'Value'); +$helper->assert('Name() is String', $value->Name() instanceof \V8\StringValue); $helper->assert('GetIdentityHash is integer', gettype($value->GetIdentityHash()), 'integer'); $helper->space(); @@ -186,12 +190,8 @@ SymbolValue extends Value: ok Accessors: ---------- V8\SymbolValue::GetIsolate() matches expected value -V8\SymbolValue->Name(): - object(V8\UndefinedValue)#92 (1) { - ["isolate":"V8\Value":private]=> - object(V8\Isolate)#3 (0) { - } - } +V8\SymbolValue->Value(): string(0) "" +Name() is undefined: ok GetIdentityHash is integer: ok @@ -266,12 +266,8 @@ SymbolValue extends NameValue: ok Accessors: ---------- V8\SymbolValue::GetIsolate() matches expected value -V8\SymbolValue->Name(): - object(V8\UndefinedValue)#7 (1) { - ["isolate":"V8\Value":private]=> - object(V8\Isolate)#3 (0) { - } - } +V8\SymbolValue->Value(): string(0) "" +Name() is undefined: ok GetIdentityHash is integer: ok @@ -334,7 +330,7 @@ Empty StringValue constructor: Object representation: ---------------------- -object(V8\SymbolValue)#7 (1) { +object(V8\SymbolValue)#5 (1) { ["isolate":"V8\Value":private]=> object(V8\Isolate)#3 (0) { } @@ -346,12 +342,8 @@ SymbolValue extends NameValue: ok Accessors: ---------- V8\SymbolValue::GetIsolate() matches expected value -V8\SymbolValue->Name(): - object(V8\StringValue)#8 (1) { - ["isolate":"V8\Value":private]=> - object(V8\Isolate)#3 (0) { - } - } +V8\SymbolValue->Value(): string(0) "" +Name() is String: ok GetIdentityHash is integer: ok @@ -418,7 +410,7 @@ Non-empty StringValue constructor: Object representation: ---------------------- -object(V8\SymbolValue)#8 (1) { +object(V8\SymbolValue)#4 (1) { ["isolate":"V8\Value":private]=> object(V8\Isolate)#3 (0) { } @@ -430,12 +422,8 @@ SymbolValue extends NameValue: ok Accessors: ---------- V8\SymbolValue::GetIsolate() matches expected value -V8\SymbolValue->Name(): - object(V8\StringValue)#93 (1) { - ["isolate":"V8\Value":private]=> - object(V8\Isolate)#3 (0) { - } - } +V8\SymbolValue->Value(): string(4) "test" +Name() is String: ok GetIdentityHash is integer: ok diff --git a/tests/V8Undefined.phpt b/tests/V8Undefined.phpt index dfe4751..b3ebeab 100644 --- a/tests/V8Undefined.phpt +++ b/tests/V8Undefined.phpt @@ -39,6 +39,7 @@ $helper->line(); $helper->header('Accessors'); $helper->method_matches($value, 'GetIsolate', $isolate); +$helper->method_export($value, 'Value'); $helper->space(); $v8_helper->run_checks($value); @@ -94,6 +95,7 @@ Default Value is not an instance of Function: ok Accessors: ---------- V8\UndefinedValue::GetIsolate() matches expected value +V8\UndefinedValue->Value(): NULL Checks on V8\UndefinedValue: @@ -158,7 +160,7 @@ V8\UndefinedValue(V8\Value)->NumberValue(): float(NAN) V8\UndefinedValue::ToString() converting: ----------------------------------------- -object(V8\StringValue)#88 (1) { +object(V8\StringValue)#89 (1) { ["isolate":"V8\Value":private]=> object(V8\Isolate)#3 (0) { }