mirror of
https://github.com/phpv8/v8js.git
synced 2024-12-22 08:11:52 +00:00
When passing objects from JS to PHP with mixed keys, some of the keys
might get lost or "re-indexed". This fixes it and adds two tests.
This commit is contained in:
parent
54621e18f3
commit
dd20670546
99
tests/array_pass.phpt
Normal file
99
tests/array_pass.phpt
Normal file
@ -0,0 +1,99 @@
|
||||
--TEST--
|
||||
Test V8::executeString() : Check passing array from JS to PHP
|
||||
--SKIPIF--
|
||||
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
$v8 = new V8Js();
|
||||
$v8->test = function ($arr) { var_dump($arr); };
|
||||
try {
|
||||
$v8->executeString('PHP.test([0, 1, 2]);');
|
||||
} catch (V8JsScriptException $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
$v8->executeString('PHP.test({ "0" : "0", "1" : "1", "2" : "2" });');
|
||||
} catch (V8JsScriptException $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
$v8->executeString('PHP.test({ "0" : "foo", "1" : "bar", "2" : "baz" });');
|
||||
} catch (V8JsScriptException $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
$v8->executeString('PHP.test({ "foo" : "0", "bar" : "1", "baz" : "2" });');
|
||||
} catch (V8JsScriptException $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
// includes gap
|
||||
$v8->executeString('PHP.test({ "0" : "0", "2" : "2", "3" : "3" });');
|
||||
} catch (V8JsScriptException $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
// mixed key types
|
||||
$v8->executeString('PHP.test({ "0" : "0", "bar" : "1", "2" : "2" });');
|
||||
} catch (V8JsScriptException $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
||||
?>
|
||||
===EOF===
|
||||
--EXPECT--
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(0)
|
||||
[1]=>
|
||||
int(1)
|
||||
[2]=>
|
||||
int(2)
|
||||
}
|
||||
object(V8Object)#3 (3) {
|
||||
["0"]=>
|
||||
string(1) "0"
|
||||
["1"]=>
|
||||
string(1) "1"
|
||||
["2"]=>
|
||||
string(1) "2"
|
||||
}
|
||||
object(V8Object)#3 (3) {
|
||||
["0"]=>
|
||||
string(3) "foo"
|
||||
["1"]=>
|
||||
string(3) "bar"
|
||||
["2"]=>
|
||||
string(3) "baz"
|
||||
}
|
||||
object(V8Object)#3 (3) {
|
||||
["foo"]=>
|
||||
string(1) "0"
|
||||
["bar"]=>
|
||||
string(1) "1"
|
||||
["baz"]=>
|
||||
string(1) "2"
|
||||
}
|
||||
object(V8Object)#3 (3) {
|
||||
["0"]=>
|
||||
string(1) "0"
|
||||
["2"]=>
|
||||
string(1) "2"
|
||||
["3"]=>
|
||||
string(1) "3"
|
||||
}
|
||||
object(V8Object)#3 (3) {
|
||||
["0"]=>
|
||||
string(1) "0"
|
||||
["2"]=>
|
||||
string(1) "2"
|
||||
["bar"]=>
|
||||
string(1) "1"
|
||||
}
|
||||
===EOF===
|
99
tests/array_pass_flags.phpt
Normal file
99
tests/array_pass_flags.phpt
Normal file
@ -0,0 +1,99 @@
|
||||
--TEST--
|
||||
Test V8::executeString() : Check passing array from JS to PHP (using force array flag)
|
||||
--SKIPIF--
|
||||
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
$v8 = new V8Js();
|
||||
$v8->test = function ($arr) { var_dump($arr); };
|
||||
try {
|
||||
$v8->executeString('PHP.test([0, 1, 2]);', "test", \V8Js::FLAG_FORCE_ARRAY);
|
||||
} catch (V8JsScriptException $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
$v8->executeString('PHP.test({ "0" : "0", "1" : "1", "2" : "2" });', "test", \V8Js::FLAG_FORCE_ARRAY);
|
||||
} catch (V8JsScriptException $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
$v8->executeString('PHP.test({ "0" : "foo", "1" : "bar", "2" : "baz" });', "test", \V8Js::FLAG_FORCE_ARRAY);
|
||||
} catch (V8JsScriptException $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
$v8->executeString('PHP.test({ "foo" : "0", "bar" : "1", "baz" : "2" });', "test", \V8Js::FLAG_FORCE_ARRAY);
|
||||
} catch (V8JsScriptException $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
// includes gap
|
||||
$v8->executeString('PHP.test({ "0" : "0", "2" : "2", "3" : "3" });', "test", \V8Js::FLAG_FORCE_ARRAY);
|
||||
} catch (V8JsScriptException $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
// mixed key types
|
||||
$v8->executeString('PHP.test({ "0" : "0", "bar" : "1", "2" : "2" });', "test", \V8Js::FLAG_FORCE_ARRAY);
|
||||
} catch (V8JsScriptException $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
||||
?>
|
||||
===EOF===
|
||||
--EXPECT--
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(0)
|
||||
[1]=>
|
||||
int(1)
|
||||
[2]=>
|
||||
int(2)
|
||||
}
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(1) "0"
|
||||
[1]=>
|
||||
string(1) "1"
|
||||
[2]=>
|
||||
string(1) "2"
|
||||
}
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(3) "foo"
|
||||
[1]=>
|
||||
string(3) "bar"
|
||||
[2]=>
|
||||
string(3) "baz"
|
||||
}
|
||||
array(3) {
|
||||
["foo"]=>
|
||||
string(1) "0"
|
||||
["bar"]=>
|
||||
string(1) "1"
|
||||
["baz"]=>
|
||||
string(1) "2"
|
||||
}
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(1) "0"
|
||||
[2]=>
|
||||
string(1) "2"
|
||||
[3]=>
|
||||
string(1) "3"
|
||||
}
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(1) "0"
|
||||
[2]=>
|
||||
string(1) "2"
|
||||
["bar"]=>
|
||||
string(1) "1"
|
||||
}
|
||||
===EOF===
|
2
v8js.cc
2
v8js.cc
@ -261,7 +261,7 @@ int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value> jsValue, HashTable *re
|
||||
v8::Local<v8::String> jsKey = jsKeys->Get(i)->ToString();
|
||||
|
||||
/* Skip any prototype properties */
|
||||
if (!jsObj->HasRealNamedProperty(jsKey) && !jsObj->HasRealNamedCallbackProperty(jsKey) && !jsObj->HasRealIndexedProperty(i)) {
|
||||
if (!jsObj->HasOwnProperty(jsKey) && !jsObj->HasRealNamedProperty(jsKey) && !jsObj->HasRealNamedCallbackProperty(jsKey)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user