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

Commit

Permalink
Enforce Value() method on all V8\Primitive values
Browse files Browse the repository at this point in the history
  • Loading branch information
pinepain committed May 3, 2017
1 parent ed7f151 commit d63cf3d
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 43 deletions.
11 changes: 5 additions & 6 deletions src/php_v8_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
28 changes: 28 additions & 0 deletions src/php_v8_symbol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Symbol> local_symbol = php_v8_value_get_local_as<v8::Symbol>(php_v8_value);
v8::Local<v8::Value> 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) {
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions src/php_v8_undefined.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
1 change: 1 addition & 0 deletions stubs/src/PrimitiveValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
*/
abstract class PrimitiveValue extends Value
{
abstract function Value();
}
10 changes: 5 additions & 5 deletions stubs/src/StringValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
}
9 changes: 8 additions & 1 deletion stubs/src/SymbolValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
48 changes: 18 additions & 30 deletions tests/V8SymbolValue.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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) {
}
Expand All @@ -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


Expand Down Expand Up @@ -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) {
}
Expand All @@ -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


Expand Down
4 changes: 3 additions & 1 deletion tests/V8Undefined.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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) {
}
Expand Down

0 comments on commit d63cf3d

Please sign in to comment.