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

Commit c2fe6e2

Browse files
committed
Add Value::TypeOf()
1 parent 25c9f06 commit c2fe6e2

35 files changed

+194
-54
lines changed

src/php_v8_value.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,26 @@ static PHP_METHOD(V8Value, SameValue) {
10501050
RETVAL_BOOL(val);
10511051
}
10521052

1053+
static PHP_METHOD(V8Value, TypeOf) {
1054+
zval *php_v8_isolate_zv;
1055+
1056+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &php_v8_isolate_zv) == FAILURE) {
1057+
return;
1058+
}
1059+
1060+
PHP_V8_ISOLATE_FETCH_WITH_CHECK(php_v8_isolate_zv, php_v8_isolate);
1061+
PHP_V8_VALUE_FETCH_WITH_CHECK(getThis(), php_v8_value);
1062+
1063+
PHP_V8_DATA_ISOLATES_CHECK_USING(php_v8_value, php_v8_isolate);
1064+
1065+
PHP_V8_ENTER_STORED_ISOLATE(php_v8_value);
1066+
1067+
v8::Local<v8::String> local_string = php_v8_value_get_value_local(isolate, php_v8_value)->TypeOf(isolate);
1068+
1069+
PHP_V8_THROW_EXCEPTION_WHEN_EMPTY(local_string, "Failed to get type of value");
1070+
1071+
php_v8_get_or_create_value(return_value, local_string, isolate);
1072+
}
10531073

10541074

10551075
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_value___construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
@@ -1175,6 +1195,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_value_SameValue, ZEND_SEND_BY_VAL, ZEND_RETURN
11751195
ZEND_ARG_OBJ_INFO(0, that, V8\\Value, 0)
11761196
ZEND_END_ARG_INFO()
11771197

1198+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_value_TypeOf, ZEND_RETURN_VALUE, 0, IS_OBJECT, PHP_V8_NS "\\StringValue", 0)
1199+
ZEND_ARG_OBJ_INFO(0, isolate, V8\\Isolate, 0)
1200+
ZEND_END_ARG_INFO()
1201+
11781202

11791203
static const zend_function_entry php_v8_value_methods[] = {
11801204
PHP_ME(V8Value, __construct, arginfo_v8_value___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
@@ -1222,6 +1246,8 @@ static const zend_function_entry php_v8_value_methods[] = {
12221246
PHP_ME(V8Value, Equals, arginfo_v8_value_Equals, ZEND_ACC_PUBLIC)
12231247
PHP_ME(V8Value, StrictEquals, arginfo_v8_value_StrictEquals, ZEND_ACC_PUBLIC)
12241248
PHP_ME(V8Value, SameValue, arginfo_v8_value_SameValue, ZEND_ACC_PUBLIC)
1249+
PHP_ME(V8Value, TypeOf, arginfo_v8_value_TypeOf, ZEND_ACC_PUBLIC)
1250+
12251251
PHP_FE_END
12261252
};
12271253

stubs/src/Value.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,21 @@ public function SameValue(Value $that)
406406
{
407407
}
408408

409+
409410
// /**
410411
// * @param Value $value
411412
// * @return Value
412413
// */
413414
////template <class T> V8_INLINE static Value* Cast(T* value);
414415
// public static function Cast(Value $value) {}
416+
// Local<String> TypeOf(v8::Isolate*);
417+
418+
/**
419+
* @param Isolate $isolate
420+
*
421+
* @return StringValue
422+
*/
423+
public function TypeOf(Isolate $isolate) : StringValue
424+
{
425+
}
415426
}

tests/.testsuite.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function header($title)
3434
echo str_repeat('-', strlen($title) + 1), PHP_EOL;
3535
}
3636

37+
public function inline($message, $value)
38+
{
39+
echo $message, ': ', $value, PHP_EOL;
40+
}
41+
3742
public function space($new_lines_number = 2)
3843
{
3944
echo str_repeat(PHP_EOL, max(1, $new_lines_number));
@@ -448,11 +453,16 @@ class CallChainFinalizer extends DefaultFinalizer {
448453
* @var array
449454
*/
450455
private $args;
456+
/**
457+
* @var bool
458+
*/
459+
private $expanded;
451460

452-
public function __construct(array $map = [], array $args = [])
461+
public function __construct(array $map = [], array $args = [], $expanded = true)
453462
{
454463
$this->map = $map;
455464
$this->args = $args;
465+
$this->expanded = $expanded;
456466
}
457467

458468
public function finalize($res, Throwable $exception = null) : string
@@ -484,7 +494,11 @@ public function finalize($res, Throwable $exception = null) : string
484494
// TODO: at this place exception possible
485495
$res = $res->$method(...$args);
486496

487-
return PHP_EOL . ' ' . '@@' .$cls . '->'. $method .'(): ' . parent::finalize($res, $exception);
497+
if ($this->expanded) {
498+
return PHP_EOL . ' ' . '@@' .$cls . '->'. $method .'(): ' . parent::finalize($res, $exception);
499+
}
500+
501+
return $cls.'->'. $method .'(): ' . parent::finalize($res, $exception);
488502
}
489503
}
490504

tests/.v8-helpers.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ public function run_checks(\V8\Value $value, $title=null) {
115115
$title = $title ?: 'Checks on ' . get_class($value);
116116
$this->testsuite->header($title);
117117

118+
$filter = new ArrayListFilter(['TypeOf'], false);
119+
$finalizer = new CallChainFinalizer([\V8\StringValue::class => 'Value'], [], false);
120+
$this->testsuite->dump_object_methods($value, ['@@default' => [$value->GetIsolate()]], $filter, $finalizer);
121+
$this->testsuite->line();
122+
118123
$filter = new RegexpFilter('/^Is/');
119124
$this->testsuite->dump_object_methods($value, [], $filter);
120125
$this->testsuite->space();

tests/004-ICU-Intl_DateTimeFormat.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--TEST--
22
ICU - Intl.DateTimeFormat()
33
--SKIPIF--
4-
<?php if (!extension_loaded("v8")) console.log "skip"; ?>
4+
<?php if (!extension_loaded("v8")) print "skip"; ?>
55
--ENV--
66
TZ=UTC
77
--INI--

tests/004-ICU-Intl_NumberFormat.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--TEST--
22
ICU - Intl.NumberFormat()
33
--SKIPIF--
4-
<?php if (!extension_loaded("v8")) console.log "skip"; ?>
4+
<?php if (!extension_loaded("v8")) print "skip"; ?>
55
--FILE--
66
<?php
77
/** @var \Phpv8Testsuite $helper */

tests/V8ArrayObject.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ V8\ArrayObject::CreationContext() matches expected value
129129
Converters:
130130
-----------
131131
V8\ArrayObject(V8\Value)->ToBoolean():
132-
object(V8\BooleanValue)#93 (1) {
132+
object(V8\BooleanValue)#94 (1) {
133133
["isolate":"V8\Value":private]=>
134134
object(V8\Isolate)#3 (5) {
135135
["snapshot":"V8\Isolate":private]=>
@@ -145,7 +145,7 @@ V8\ArrayObject(V8\Value)->ToBoolean():
145145
}
146146
}
147147
V8\ArrayObject(V8\Value)->ToNumber():
148-
object(V8\NumberValue)#93 (1) {
148+
object(V8\NumberValue)#94 (1) {
149149
["isolate":"V8\Value":private]=>
150150
object(V8\Isolate)#3 (5) {
151151
["snapshot":"V8\Isolate":private]=>
@@ -161,7 +161,7 @@ V8\ArrayObject(V8\Value)->ToNumber():
161161
}
162162
}
163163
V8\ArrayObject(V8\Value)->ToString():
164-
object(V8\StringValue)#93 (1) {
164+
object(V8\StringValue)#94 (1) {
165165
["isolate":"V8\Value":private]=>
166166
object(V8\Isolate)#3 (5) {
167167
["snapshot":"V8\Isolate":private]=>
@@ -177,7 +177,7 @@ V8\ArrayObject(V8\Value)->ToString():
177177
}
178178
}
179179
V8\ArrayObject(V8\Value)->ToDetailString():
180-
object(V8\StringValue)#93 (1) {
180+
object(V8\StringValue)#94 (1) {
181181
["isolate":"V8\Value":private]=>
182182
object(V8\Isolate)#3 (5) {
183183
["snapshot":"V8\Isolate":private]=>
@@ -246,7 +246,7 @@ V8\ArrayObject(V8\Value)->ToObject():
246246
}
247247
}
248248
V8\ArrayObject(V8\Value)->ToInteger():
249-
object(V8\NumberValue)#93 (1) {
249+
object(V8\NumberValue)#94 (1) {
250250
["isolate":"V8\Value":private]=>
251251
object(V8\Isolate)#3 (5) {
252252
["snapshot":"V8\Isolate":private]=>
@@ -262,7 +262,7 @@ V8\ArrayObject(V8\Value)->ToInteger():
262262
}
263263
}
264264
V8\ArrayObject(V8\Value)->ToUint32():
265-
object(V8\NumberValue)#93 (1) {
265+
object(V8\NumberValue)#94 (1) {
266266
["isolate":"V8\Value":private]=>
267267
object(V8\Isolate)#3 (5) {
268268
["snapshot":"V8\Isolate":private]=>
@@ -278,7 +278,7 @@ V8\ArrayObject(V8\Value)->ToUint32():
278278
}
279279
}
280280
V8\ArrayObject(V8\Value)->ToInt32():
281-
object(V8\NumberValue)#93 (1) {
281+
object(V8\NumberValue)#94 (1) {
282282
["isolate":"V8\Value":private]=>
283283
object(V8\Isolate)#3 (5) {
284284
["snapshot":"V8\Isolate":private]=>

tests/V8Boolean.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ V8\BooleanValue->Value(): bool(true)
8181

8282
Checkers:
8383
---------
84+
V8\BooleanValue(V8\Value)->TypeOf(): V8\StringValue->Value(): string(7) "boolean"
85+
8486
V8\BooleanValue(V8\Value)->IsUndefined(): bool(false)
8587
V8\BooleanValue(V8\Value)->IsNull(): bool(false)
8688
V8\BooleanValue(V8\Value)->IsTrue(): bool(true)

tests/V8BooleanObject.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ V8\BooleanObject->ValueOf(): bool(true)
121121

122122
Checkers:
123123
---------
124+
V8\BooleanObject(V8\Value)->TypeOf(): V8\StringValue->Value(): string(6) "object"
125+
124126
V8\BooleanObject(V8\ObjectValue)->IsCallable(): bool(false)
125127
V8\BooleanObject(V8\Value)->IsUndefined(): bool(false)
126128
V8\BooleanObject(V8\Value)->IsNull(): bool(false)
@@ -152,6 +154,8 @@ typeof val: object
152154

153155
Checkers on boxed from script:
154156
------------------------------
157+
V8\BooleanObject(V8\Value)->TypeOf(): V8\StringValue->Value(): string(6) "object"
158+
155159
V8\BooleanObject(V8\ObjectValue)->IsCallable(): bool(false)
156160
V8\BooleanObject(V8\Value)->IsUndefined(): bool(false)
157161
V8\BooleanObject(V8\Value)->IsNull(): bool(false)

tests/V8DateObject.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ V8\DateObject->ValueOf(): float(1445444940000)
179179

180180
Checkers:
181181
---------
182+
V8\DateObject(V8\Value)->TypeOf(): V8\StringValue->Value(): string(6) "object"
183+
182184
V8\DateObject(V8\ObjectValue)->IsCallable(): bool(false)
183185
V8\DateObject(V8\Value)->IsUndefined(): bool(false)
184186
V8\DateObject(V8\Value)->IsNull(): bool(false)

0 commit comments

Comments
 (0)