0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-09-19 00:25:18 +00:00

Don't re-wrap PHP objects from V8 to V8Object

This commit is contained in:
Stefan Siegl 2013-10-21 20:00:58 +02:00
parent 0051c77700
commit bd9483080d
4 changed files with 113 additions and 25 deletions

View File

@ -113,15 +113,13 @@ Mon, 08 Sep 1975 09:00:00 +0000
string(3) "foo"
array(3) {
[0]=>
object(V8Object)#4 (3) {
["mytest"]=>
object(V8Function)#6 (0) {
}
["mydatetest"]=>
object(V8Function)#7 (0) {
}
object(Testing)#2 (3) {
["foo"]=>
string(8) "ORIGINAL"
["my_private":"Testing":private]=>
string(3) "arf"
["my_protected":protected]=>
string(4) "argh"
}
[1]=>
array(3) {
@ -139,15 +137,13 @@ array(3) {
[1]=>
string(3) "bar"
[2]=>
object(V8Object)#5 (3) {
["mytest"]=>
object(V8Function)#7 (0) {
}
["mydatetest"]=>
object(V8Function)#6 (0) {
}
object(Testing)#2 (3) {
["foo"]=>
string(8) "ORIGINAL"
["my_private":"Testing":private]=>
string(3) "arf"
["my_protected":protected]=>
string(4) "argh"
}
}
}

View File

@ -0,0 +1,74 @@
--TEST--
Test V8::executeString() : Object passing PHP > JS > PHP
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
class Bar {
function sayHello() {
echo "Hello\n";
}
}
class Foo {
function getBar() {
return new Bar();
}
function callMulti($list) {
foreach($list as $x) {
echo get_class($x)."\n"; // V8Object vs. Bar
$x->sayHello();
}
}
function callSingle($inst) {
echo get_class($inst)."\n";
$inst->sayHello();
}
}
$v8 = new V8Js();
$v8->foo = new Foo();
$JS = <<< EOF
var obj = PHP.foo.getBar();
PHP.foo.callMulti([obj]);
PHP.foo.callMulti([obj]);
PHP.foo.callSingle(obj);
PHP.foo.callSingle(obj);
obj = {};
obj.sayHello = function() {
print("JavaScript Hello\\n");
};
PHP.foo.callMulti([obj]);
PHP.foo.callMulti([obj]);
PHP.foo.callSingle(obj);
PHP.foo.callSingle(obj);
EOF;
$v8->executeString($JS);
?>
===EOF===
--EXPECT--
Bar
Hello
Bar
Hello
Bar
Hello
Bar
Hello
V8Object
JavaScript Hello
V8Object
JavaScript Hello
V8Object
JavaScript Hello
V8Object
JavaScript Hello
===EOF===

View File

@ -281,12 +281,21 @@ int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value> jsValue, HashTable *re
const char *key = ToCString(cstr);
zval *value = NULL;
if(jsVal->IsObject()
&& !jsVal->IsFunction()
&& jsVal->ToObject()->InternalFieldCount() == 2) {
/* This is a PHP object, passed to JS and back. */
value = reinterpret_cast<zval *>(jsVal->ToObject()->GetAlignedPointerFromInternalField(0));
Z_ADDREF_P(value);
}
else {
MAKE_STD_ZVAL(value);
if (v8js_to_zval(jsVal, value, flags, isolate TSRMLS_CC) == FAILURE) {
zval_ptr_dtor(&value);
return FAILURE;
}
}
if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
zend_symtable_update(retval, key, strlen(key) + 1, (void *)&value, sizeof(zval *), NULL);

View File

@ -110,6 +110,13 @@ static void php_v8js_call_php_func(zval *value, zend_class_entry *ce, zend_funct
fci.params = (zval ***) safe_emalloc(argc, sizeof(zval **), 0);
argv = (zval **) safe_emalloc(argc, sizeof(zval *), 0);
for (i = 0; i < argc; i++) {
if(info[i]->IsObject()
&& !info[i]->IsFunction()
&& info[i]->ToObject()->InternalFieldCount() == 2) {
/* This is a PHP object, passed to JS and back. */
argv[i] = reinterpret_cast<zval *>(info[i]->ToObject()->GetAlignedPointerFromInternalField(0));
Z_ADDREF_P(argv[i]);
} else {
MAKE_STD_ZVAL(argv[i]);
if (v8js_to_zval(info[i], argv[i], flags, isolate TSRMLS_CC) == FAILURE) {
fci.param_count++;
@ -118,6 +125,8 @@ static void php_v8js_call_php_func(zval *value, zend_class_entry *ce, zend_funct
efree(error);
goto failure;
}
}
fci.params[fci.param_count++] = &argv[i];
}
} else {