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

Merge branch 'array-access'

Conflicts:
	v8js_convert.cc
This commit is contained in:
Stefan Siegl 2014-12-10 20:02:33 +01:00
commit 2b4750748e
20 changed files with 1972 additions and 893 deletions

View File

@ -222,3 +222,34 @@ $v8->executeString('print(PHP.foo.$bar, "\n");');
$v8->executeString('PHP.foo.__call("bar", ["function"]);');
?>
```
Mapping Rules
=============
Native Arrays
-------------
Despite the common name the concept of arrays is very different between PHP and JavaScript. In JavaScript an array is a contiguous collection of elements indexed by integral numbers from zero on upwards. In PHP arrays can be sparse, i.e. integral keys need not be contiguous and may even be negative. Besides PHP arrays may not only use integral numbers as keys but also strings (so-called associative arrays). Contrary JavaScript arrays allow for properties to be attached to arrays, which isn't supported by PHP. Those properties are not part of the arrays collection, for example `Array.prototype.forEach` method doesn't "see" these.
Generally PHP arrays are mapped to JavaScript "native" arrays if this is possible, i.e. the PHP array uses contiguous numeric keys from zero on upwards. Both associative and sparse arrays are mapped to JavaScript objects. Those objects have a constructor also called "Array", but they are not native arrays and don't share the Array.prototype, hence they don't (directly) support the typical array functions like `join`, `forEach`, etc.
PHP arrays are immediately exported value by value without live binding. This is if you change a value on JavaScript side or push further values onto the array, this change is *not* reflected on PHP side.
If JavaScript arrays are passed back to PHP the JavaScript array is always converted to a PHP array. If the JavaScript array has (own) properties attached, these are also converted to keys of the PHP array.
Native Objects
--------------
PHP objects passed to JavaScript are mapped to native JavaScript objects which have a "virtual" constructor function with the name of the PHP object's class. This constructor function can be used to create new instances of the PHP class as long as the PHP class doesn't have a non-public `__construct` method.
All public methods and properties are visible to JavaScript code and the properties are live-bound, i.e. if a property's value is changed by JavaScript code, the PHP object is also affected.
If a native JavaScript object is passed to PHP the JavaScript object is mapped to a PHP object of `V8Object` class. This object has all properties the JavaScript object has and is fully mutable. If a function is assigned to one of those properties, it's also callable by PHP code.
The `executeString` function can be configured to always map JavaScript objects to PHP arrays by setting the `V8Js::FLAG_FORCE_ARRAY` flag. Then the standard array behaviour applies that values are not live-bound, i.e. if you change values of the resulting PHP array, the JavaScript object is *not* affected.
PHP Objects implementing ArrayAccess, Countable
-----------------------------------------------
The above rule that PHP objects are generally converted to JavaScript objects also applies to PHP objects of `ArrayObject` type or other classes, that implement both the `ArrayAccess` and the `Countable` interface -- even so they behave like PHP arrays.
This behaviour can be changed by enabling the php.ini flag `v8js.use_array_access`. If set, objects of PHP classes that implement the aforementioned interfaces are converted to JavaScript Array-like objects. This is by-index access of this object results in immediate calls to the `offsetGet` or `offsetSet` PHP methods (effectively this is live-binding of JavaScript against the PHP object). Such an Array-esque object also supports calling every attached public method of the PHP object + methods of JavaScript's native Array.prototype methods (as long as they are not overloaded by PHP methods).

View File

@ -123,7 +123,7 @@ AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <v8-debug.h>]],
CPPFLAGS=$old_CPPFLAGS
]);
PHP_NEW_EXTENSION(v8js, v8js.cc v8js_convert.cc v8js_methods.cc v8js_variables.cc v8js_commonjs.cc, $ext_shared, , "-std="$ac_cv_v8_cstd)
PHP_NEW_EXTENSION(v8js, v8js.cc v8js_array_access.cc v8js_convert.cc v8js_methods.cc v8js_object_export.cc v8js_variables.cc v8js_commonjs.cc, $ext_shared, , "-std="$ac_cv_v8_cstd)
PHP_ADD_MAKEFILE_FRAGMENT
fi

View File

@ -13,7 +13,7 @@ if (PHP_V8JS != "no") {
AC_DEFINE("PHP_V8_API_VERSION", "3017015", "", false);
AC_DEFINE("PHP_V8_VERSION", "3.17.15", "", true);
EXTENSION("v8js", "v8js.cc v8js_commonjs.cc v8js_convert.cc v8js_methods.cc v8js_variables.cc", "yes");
EXTENSION("v8js", "v8js.cc v8js_array_access.cc v8js_commonjs.cc v8js_convert.cc v8js_methods.cc v8js_object_export.cc v8js_variables.cc", "yes");
} else {
WARNING("v8js not enabled, headers or libs not found");

View File

@ -220,6 +220,7 @@ ZEND_BEGIN_MODULE_GLOBALS(v8js)
/* Ini globals */
char *v8_flags; /* V8 command line flags */
bool use_date; /* Generate JS Date objects instead of PHP DateTime */
bool use_array_access; /* Convert ArrayAccess, Countable objects to array-like objects */
// Timer thread globals
std::deque<php_v8js_timer_ctx *> timer_stack;

59
tests/array_access.phpt Normal file
View File

@ -0,0 +1,59 @@
--TEST--
Test V8::executeString() : Check ArrayAccess interface wrapping
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--INI--
v8js.use_array_access = 1
--FILE--
<?php
class MyArray implements ArrayAccess, Countable {
public function offsetExists($offset) {
return $offset >= 0 && $offset <= 20;
}
public function offsetGet($offset) {
return 19 - $offset;
}
public function offsetSet($offset, $value) {
throw new Exception('Not implemented');
}
public function offsetUnset($offset) {
throw new Exception('Not implemented');
}
public function count() {
return 20;
}
}
$myarr = new MyArray();
var_dump(count($myarr));
var_dump($myarr[5]);
$js = <<<EOJS
var_dump(PHP.myarr.constructor.name);
var_dump(PHP.myarr.length);
var_dump(PHP.myarr[5]);
var_dump(PHP.myarr.join(', '));
var_dump(PHP.myarr.slice(5, 10).join(', '));
EOJS;
$v8 = new V8Js();
$v8->myarr = $myarr;
$v8->executeString($js);
?>
===EOF===
--EXPECT--
int(20)
int(14)
string(5) "Array"
int(20)
int(14)
string(68) "19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0"
string(18) "14, 13, 12, 11, 10"
===EOF===

View File

@ -0,0 +1,52 @@
--TEST--
Test V8::executeString() : Check ArrayAccess live binding
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--INI--
v8js.use_array_access = 1
--FILE--
<?php
class MyArray implements ArrayAccess, Countable {
private $data = Array('one', 'two', 'three');
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function offsetGet($offset) {
return $this->data[$offset];
}
public function offsetSet($offset, $value) {
$this->data[$offset] = $value;
}
public function offsetUnset($offset) {
throw new Exception('Not implemented');
}
public function count() {
return count($this->data);
}
public function push($value) {
$this->data[] = $value;
}
}
$v8 = new V8Js();
$v8->myarr = new MyArray();
$v8->executeString('var_dump(PHP.myarr.join(","));');
/* array is "live bound", i.e. new elements just pop up on js side. */
$v8->myarr->push('new');
$v8->executeString('var_dump(PHP.myarr.join(","));');
?>
===EOF===
--EXPECT--
string(13) "one,two,three"
string(17) "one,two,three,new"
===EOF===

View File

@ -0,0 +1,57 @@
--TEST--
Test V8::executeString() : Use ArrayAccess with JavaScript native push method
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--INI--
v8js.use_array_access = 1
--FILE--
<?php
class MyArray implements ArrayAccess, Countable {
private $data = Array('one', 'two', 'three');
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function offsetGet($offset) {
return $this->data[$offset];
}
public function offsetSet($offset, $value) {
echo "set[$offset] = $value\n";
$this->data[$offset] = $value;
}
public function offsetUnset($offset) {
throw new Exception('Not implemented');
}
public function count() {
return count($this->data);
}
}
$v8 = new V8Js();
$v8->myarr = new MyArray();
/* Call native JavaScript push method, this should cause a count
* offsetSet call. */
$v8->executeString('PHP.myarr.push(23);');
/* Access array from PHP side, should work if JavaScript called
* the write accessor functions. */
var_dump(count($v8->myarr));
var_dump($v8->myarr[3]);
/* And JS side of course should see it too. */
$v8->executeString('var_dump(PHP.myarr.join(","));');
?>
===EOF===
--EXPECT--
set[3] = 23
int(4)
int(23)
string(16) "one,two,three,23"
===EOF===

View File

@ -0,0 +1,83 @@
--TEST--
Test V8::executeString() : Export PHP methods on ArrayAccess objects
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--INI--
v8js.use_array_access = 1
--FILE--
<?php
class MyArray implements ArrayAccess, Countable {
private $data = Array('one', 'two', 'three');
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function offsetGet($offset) {
return $this->data[$offset];
}
public function offsetSet($offset, $value) {
echo "set[$offset] = $value\n";
$this->data[$offset] = $value;
}
public function offsetUnset($offset) {
throw new Exception('Not implemented');
}
public function count() {
echo 'count() = ', count($this->data), "\n";
return count($this->data);
}
public function phpSidePush($value) {
echo "push << $value\n";
$this->data[] = $value;
}
public function push($value) {
echo "php-side-push << $value\n";
$this->data[] = $value;
}
}
$v8 = new V8Js();
$v8->myarr = new MyArray();
/* Call PHP method to modify the array. */
$v8->executeString('PHP.myarr.phpSidePush(23);');
var_dump(count($v8->myarr));
var_dump($v8->myarr[3]);
/* And JS should see the changes due to live binding. */
$v8->executeString('var_dump(PHP.myarr.join(","));');
/* Call `push' method, this should trigger the PHP method. */
$v8->executeString('PHP.myarr.push(42);');
var_dump(count($v8->myarr));
var_dump($v8->myarr[4]);
/* And JS should see the changes due to live binding. */
$v8->executeString('var_dump(PHP.myarr.join(","));');
?>
===EOF===
--EXPECT--
push << 23
count() = 4
int(4)
int(23)
count() = 4
string(16) "one,two,three,23"
php-side-push << 42
count() = 5
int(5)
int(42)
count() = 5
string(19) "one,two,three,23,42"
===EOF===

View File

@ -0,0 +1,65 @@
--TEST--
Test V8::executeString() : Export PHP properties on ArrayAccess objects
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--INI--
v8js.use_array_access = 1
--FILE--
<?php
class MyArray implements ArrayAccess, Countable {
private $data = Array('one', 'two', 'three');
private $privFoo = 23;
protected $protFoo = 23;
public $pubFoo = 42;
/* We can have a length property on the PHP object, but the length property
* of the JS object will still call count() method. Anyways it should be
* accessibly as $length. */
public $length = 42;
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function offsetGet($offset) {
return $this->data[$offset];
}
public function offsetSet($offset, $value) {
echo "set[$offset] = $value\n";
$this->data[$offset] = $value;
}
public function offsetUnset($offset) {
throw new Exception('Not implemented');
}
public function count() {
return count($this->data);
}
}
$v8 = new V8Js();
$v8->myarr = new MyArray();
$v8->executeString('var_dump(PHP.myarr.privFoo);');
$v8->executeString('var_dump(PHP.myarr.protFoo);');
$v8->executeString('var_dump(PHP.myarr.pubFoo);');
/* This should call count(), i.e. return 3 */
$v8->executeString('var_dump(PHP.myarr.length);');
/* This should print the value of the $length property */
$v8->executeString('var_dump(PHP.myarr.$length);');
?>
===EOF===
--EXPECT--
NULL
NULL
int(42)
int(3)
int(42)
===EOF===

View File

@ -0,0 +1,53 @@
--TEST--
Test V8::executeString() : Export __invoke method on ArrayAccess objects
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--INI--
v8js.use_array_access = 1
--FILE--
<?php
class MyArray implements ArrayAccess, Countable {
private $data = Array('one', 'two', 'three');
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function offsetGet($offset) {
return $this->data[$offset];
}
public function offsetSet($offset, $value) {
echo "set[$offset] = $value\n";
$this->data[$offset] = $value;
}
public function offsetUnset($offset) {
throw new Exception('Not implemented');
}
public function count() {
return count($this->data);
}
public function __invoke() {
echo "__invoke called!\n";
}
}
$v8 = new V8Js();
$v8->myarr = new MyArray();
$v8->executeString('PHP.myarr();');
$v8->executeString('var_dump(PHP.myarr.length);');
$v8->executeString('var_dump(PHP.myarr.join(", "));');
?>
===EOF===
--EXPECT--
__invoke called!
int(3)
string(15) "one, two, three"
===EOF===

View File

@ -0,0 +1,63 @@
--TEST--
Test V8::executeString() : Enumerate ArrayAccess keys
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--INI--
v8js.use_array_access = 1
--FILE--
<?php
class MyArray implements ArrayAccess, Countable {
private $data = Array('one', 'two', 'three', null, 'five');
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function offsetGet($offset) {
return $this->data[$offset];
}
public function offsetSet($offset, $value) {
echo "set[$offset] = $value\n";
$this->data[$offset] = $value;
}
public function offsetUnset($offset) {
throw new Exception('Not implemented');
}
public function count() {
return count($this->data);
}
}
$v8 = new V8Js();
$v8->myarr = new MyArray();
$js = <<<EOF
var jsarr = [ "one", "two", "three", , "five" ];
for(var i in jsarr) {
var_dump(i);
}
for(var i in PHP.myarr) {
var_dump(i);
}
EOF;
$v8->executeString($js);
?>
===EOF===
--EXPECT--
string(1) "0"
string(1) "1"
string(1) "2"
string(1) "4"
string(1) "0"
string(1) "1"
string(1) "2"
string(1) "4"
===EOF===

View File

@ -0,0 +1,57 @@
--TEST--
Test V8::executeString() : Delete (unset) ArrayAccess keys
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--INI--
v8js.use_array_access = 1
--FILE--
<?php
class MyArray implements ArrayAccess, Countable {
private $data = Array('one', 'two', 'three');
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function offsetGet($offset) {
if(!$this->offsetExists($offset)) {
return null;
}
return $this->data[$offset];
}
public function offsetSet($offset, $value) {
$this->data[$offset] = $value;
}
public function offsetUnset($offset) {
unset($this->data[$offset]);
}
public function count() {
return max(array_keys($this->data)) + 1;
}
}
$v8 = new V8Js();
$v8->myarr = new MyArray();
$js = <<<EOF
var jsarr = [ "one", "two", "three" ];
delete jsarr[1];
var_dump(jsarr.join(","));
delete PHP.myarr[1];
var_dump(PHP.myarr.join(","));
EOF;
$v8->executeString($js);
?>
===EOF===
--EXPECT--
string(10) "one,,three"
string(10) "one,,three"
===EOF===

View File

@ -0,0 +1,56 @@
--TEST--
Test V8::executeString() : in array (isset) behaviour of ArrayAccess
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--INI--
v8js.use_array_access = 1
--FILE--
<?php
class MyArray implements ArrayAccess, Countable {
private $data = Array('one', null, 'three');
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function offsetGet($offset) {
return $this->data[$offset];
}
public function offsetSet($offset, $value) {
$this->data[$offset] = $value;
}
public function offsetUnset($offset) {
unset($this->data[$offset]);
}
public function count() {
return max(array_keys($this->data)) + 1;
}
}
$v8 = new V8Js();
$v8->myarr = new MyArray();
$js = <<<EOF
var jsarr = [ "one", , "three" ];
var_dump(0 in jsarr);
var_dump(1 in jsarr);
var_dump(0 in PHP.myarr);
var_dump(1 in PHP.myarr);
EOF;
$v8->executeString($js);
?>
===EOF===
--EXPECT--
bool(true)
bool(false)
bool(true)
bool(false)
===EOF===

View File

@ -0,0 +1,57 @@
--TEST--
Test V8::executeString() : Check array access setter behaviour
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--INI--
v8js.use_array_access = 1
--FILE--
<?php
class MyArray implements ArrayAccess, Countable {
private $data = array('one', 'two', 'three');
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function offsetGet($offset) {
return $this->data[$offset];
}
public function offsetSet($offset, $value) {
$this->data[$offset] = $value;
}
public function offsetUnset($offset) {
throw new Exception('Not implemented');
}
public function count() {
return count($this->data);
}
}
$myarr = new MyArray();
$myarr[0] = 'three';
$js = <<<EOJS
var_dump(PHP.myarr[2]);
PHP.myarr[2] = 'one';
var_dump(PHP.myarr[2]);
var_dump(PHP.myarr.join(','));
EOJS;
$v8 = new V8Js();
$v8->myarr = $myarr;
$v8->executeString($js);
var_dump($myarr[2]);
?>
===EOF===
--EXPECT--
string(5) "three"
string(3) "one"
string(13) "three,two,one"
string(3) "one"
===EOF===

18
v8js.cc
View File

@ -85,9 +85,27 @@ static ZEND_INI_MH(v8js_OnUpdateUseDate) /* {{{ */
}
/* }}} */
static ZEND_INI_MH(v8js_OnUpdateUseArrayAccess) /* {{{ */
{
bool value;
if (new_value_length==2 && strcasecmp("on", new_value)==0) {
value = (bool) 1;
} else if (new_value_length==3 && strcasecmp("yes", new_value)==0) {
value = (bool) 1;
} else if (new_value_length==4 && strcasecmp("true", new_value)==0) {
value = (bool) 1;
} else {
value = (bool) atoi(new_value);
}
V8JSG(use_array_access) = value;
return SUCCESS;
}
/* }}} */
ZEND_INI_BEGIN() /* {{{ */
ZEND_INI_ENTRY("v8js.flags", NULL, ZEND_INI_ALL, v8js_OnUpdateV8Flags)
ZEND_INI_ENTRY("v8js.use_date", "0", ZEND_INI_ALL, v8js_OnUpdateUseDate)
ZEND_INI_ENTRY("v8js.use_array_access", "0", ZEND_INI_ALL, v8js_OnUpdateUseArrayAccess)
ZEND_INI_END()
/* }}} */

267
v8js_array_access.cc Normal file
View File

@ -0,0 +1,267 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| http://www.opensource.org/licenses/mit-license.php MIT License |
+----------------------------------------------------------------------+
| Author: Stefan Siegl <stesie@brokenpipe.de> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
extern "C" {
#include "php.h"
#include "ext/date/php_date.h"
#include "ext/standard/php_string.h"
#include "zend_interfaces.h"
#include "zend_closures.h"
}
#include "php_v8js_macros.h"
#include "v8js_array_access.h"
#include "v8js_object_export.h"
static zval *php_v8js_array_access_dispatch(zval *object, const char *method_name, int param_count,
uint32_t index, zval *zvalue_ptr TSRMLS_DC) /* {{{ */
{
zend_class_entry *ce = Z_OBJCE_P(object);
/* Okay, let's call offsetGet. */
zend_fcall_info fci;
zval *php_value;
zval fmember;
INIT_ZVAL(fmember);
ZVAL_STRING(&fmember, method_name, 0);
zval zindex;
INIT_ZVAL(zindex);
ZVAL_LONG(&zindex, index);
fci.size = sizeof(fci);
fci.function_table = &ce->function_table;
fci.function_name = &fmember;
fci.symbol_table = NULL;
fci.retval_ptr_ptr = &php_value;
zval *zindex_ptr = &zindex;
zval **params[2] = { &zindex_ptr, &zvalue_ptr };
fci.param_count = param_count;
fci.params = params;
fci.object_ptr = object;
fci.no_separation = 0;
zend_call_function(&fci, NULL TSRMLS_CC);
return php_value;
}
/* }}} */
void php_v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
V8JS_TSRMLS_FETCH();
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
zval *php_value = php_v8js_array_access_dispatch(object, "offsetGet", 1, index, NULL TSRMLS_CC);
v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
zval_ptr_dtor(&php_value);
info.GetReturnValue().Set(ret_value);
}
/* }}} */
void php_v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
V8JS_TSRMLS_FETCH();
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
zval *zvalue_ptr;
MAKE_STD_ZVAL(zvalue_ptr);
if (v8js_to_zval(value, zvalue_ptr, 0, isolate TSRMLS_CC) != SUCCESS) {
info.GetReturnValue().Set(v8::Handle<v8::Value>());
return;
}
zval *php_value = php_v8js_array_access_dispatch(object, "offsetSet", 2, index, zvalue_ptr TSRMLS_CC);
zval_ptr_dtor(&php_value);
/* simply pass back the value to tell we intercepted the call
* as the offsetSet function returns void. */
info.GetReturnValue().Set(value);
/* if PHP wanted to hold on to this value, zend_call_function would
* have bumped the refcount. */
zval_ptr_dtor(&zvalue_ptr);
}
/* }}} */
static int php_v8js_array_access_get_count_result(zval *object TSRMLS_DC) /* {{{ */
{
zval *php_value = php_v8js_array_access_dispatch(object, "count", 0, 0, NULL TSRMLS_CC);
if(Z_TYPE_P(php_value) != IS_LONG) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-numeric return value from count() method");
return 0;
}
int result = Z_LVAL_P(php_value);
zval_ptr_dtor(&php_value);
return result;
}
/* }}} */
static bool php_v8js_array_access_isset_p(zval *object, int index TSRMLS_DC) /* {{{ */
{
zval *php_value = php_v8js_array_access_dispatch(object, "offsetExists", 1, index, NULL TSRMLS_CC);
if(Z_TYPE_P(php_value) != IS_BOOL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-boolean return value from offsetExists() method");
return false;
}
bool result = Z_LVAL_P(php_value);
zval_ptr_dtor(&php_value);
return result;
}
/* }}} */
static void php_v8js_array_access_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
V8JS_TSRMLS_FETCH();
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
int length = php_v8js_array_access_get_count_result(object TSRMLS_CC);
info.GetReturnValue().Set(V8JS_INT(length));
}
/* }}} */
void php_v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
V8JS_TSRMLS_FETCH();
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
zval *php_value = php_v8js_array_access_dispatch(object, "offsetUnset", 1, index, NULL TSRMLS_CC);
zval_ptr_dtor(&php_value);
info.GetReturnValue().Set(V8JS_BOOL(true));
}
/* }}} */
void php_v8js_array_access_query(uint32_t index, const v8::PropertyCallbackInfo<v8::Integer>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
V8JS_TSRMLS_FETCH();
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
/* If index is set, then return an integer encoding a v8::PropertyAttribute;
* otherwise we're expected to return an empty handle. */
if(php_v8js_array_access_isset_p(object, index TSRMLS_CC)) {
info.GetReturnValue().Set(V8JS_UINT(v8::PropertyAttribute::None));
}
}
/* }}} */
void php_v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
V8JS_TSRMLS_FETCH();
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
int length = php_v8js_array_access_get_count_result(object TSRMLS_CC);
v8::Local<v8::Array> result = v8::Array::New(isolate, length);
int i = 0;
for(int j = 0; j < length; j ++) {
if(php_v8js_array_access_isset_p(object, j TSRMLS_CC)) {
result->Set(i ++, V8JS_INT(j));
}
}
result->Set(V8JS_STR("length"), V8JS_INT(i));
info.GetReturnValue().Set(result);
}
/* }}} */
void php_v8js_array_access_named_getter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
{
v8::String::Utf8Value cstr(property);
const char *name = ToCString(cstr);
if(strcmp(name, "length") == 0) {
php_v8js_array_access_length(property, info);
return;
}
v8::Local<v8::Value> ret_value = php_v8js_named_property_callback(property, info, V8JS_PROP_GETTER);
if(ret_value.IsEmpty()) {
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Array> arr = v8::Array::New(isolate);
v8::Local<v8::Value> prototype = arr->GetPrototype();
if(!prototype->IsObject()) {
/* ehh? Array.prototype not an object? strange, stop. */
info.GetReturnValue().Set(ret_value);
}
ret_value = prototype->ToObject()->Get(property);
}
info.GetReturnValue().Set(ret_value);
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/

31
v8js_array_access.h Normal file
View File

@ -0,0 +1,31 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| http://www.opensource.org/licenses/mit-license.php MIT License |
+----------------------------------------------------------------------+
| Author: Stefan Siegl <stesie@brokenpipe.de> |
+----------------------------------------------------------------------+
*/
#ifndef V8JS_ARRAY_ACCESS_H
#define V8JS_ARRAY_ACCESS_H
/* Indexed Property Handlers */
void php_v8js_array_access_getter(uint32_t index,
const v8::PropertyCallbackInfo<v8::Value>& info);
void php_v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<v8::Value>& info);
void php_v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& info);
void php_v8js_array_access_deleter(uint32_t index,
const v8::PropertyCallbackInfo<v8::Boolean>& info);
void php_v8js_array_access_query(uint32_t index,
const v8::PropertyCallbackInfo<v8::Integer>& info);
/* Named Property Handlers */
void php_v8js_array_access_named_getter(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value> &info);
#endif /* V8JS_ARRAY_ACCESS_H */

View File

@ -26,237 +26,10 @@ extern "C" {
}
#include "php_v8js_macros.h"
#include <v8.h>
#include "v8js_object_export.h"
#include <stdexcept>
#include <limits>
#include <iostream>
static void php_v8js_weak_object_callback(const v8::WeakCallbackData<v8::Object, zval> &data);
/* Callback for PHP methods and functions */
static void php_v8js_call_php_func(zval *value, zend_class_entry *ce, zend_function *method_ptr, v8::Isolate *isolate, const v8::FunctionCallbackInfo<v8::Value>& info TSRMLS_DC) /* {{{ */
{
v8::Handle<v8::Value> return_value;
zend_fcall_info fci;
zend_fcall_info_cache fcc;
zval fname, *retval_ptr = NULL, **argv = NULL;
zend_uint argc = info.Length(), min_num_args = 0, max_num_args = 0;
char *error;
int error_len, i, flags = V8JS_FLAG_NONE;
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
/* Set parameter limits */
min_num_args = method_ptr->common.required_num_args;
max_num_args = method_ptr->common.num_args;
/* Function name to call */
INIT_ZVAL(fname);
ZVAL_STRING(&fname, method_ptr->common.function_name, 0);
/* zend_fcall_info */
fci.size = sizeof(fci);
fci.function_table = &ce->function_table;
fci.function_name = &fname;
fci.symbol_table = NULL;
fci.object_ptr = value;
fci.retval_ptr_ptr = &retval_ptr;
fci.param_count = 0;
/* Check for passed vs required number of arguments */
if (argc < min_num_args)
{
error_len = spprintf(&error, 0,
"%s::%s() expects %s %d parameter%s, %d given",
ce->name,
method_ptr->common.function_name,
min_num_args == max_num_args ? "exactly" : argc < min_num_args ? "at least" : "at most",
argc < min_num_args ? min_num_args : max_num_args,
(argc < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
argc);
return_value = V8JS_THROW(isolate, TypeError, error, error_len);
if (ce == zend_ce_closure) {
efree(const_cast<char*>(method_ptr->internal_function.function_name));
efree(method_ptr);
}
efree(error);
info.GetReturnValue().Set(return_value);
return;
}
/* Convert parameters passed from V8 */
if (argc) {
flags = V8JS_GLOBAL_GET_FLAGS(isolate);
fci.params = (zval ***) safe_emalloc(argc, sizeof(zval **), 0);
argv = (zval **) safe_emalloc(argc, sizeof(zval *), 0);
for (i = 0; i < argc; i++) {
v8::Local<v8::Value> php_object;
if (info[i]->IsObject()) {
php_object = v8::Local<v8::Object>::Cast(info[i])->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
}
if (!php_object.IsEmpty()) {
/* This is a PHP object, passed to JS and back. */
argv[i] = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
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++;
error_len = spprintf(&error, 0, "converting parameter #%d passed to %s() failed", i + 1, method_ptr->common.function_name);
return_value = V8JS_THROW(isolate, Error, error, error_len);
efree(error);
goto failure;
}
}
fci.params[fci.param_count++] = &argv[i];
}
} else {
fci.params = NULL;
}
fci.no_separation = 1;
info.GetReturnValue().Set(V8JS_NULL);
zend_try {
{
isolate->Exit();
v8::Unlocker unlocker(isolate);
/* zend_fcall_info_cache */
fcc.initialized = 1;
fcc.function_handler = method_ptr;
fcc.calling_scope = ce;
fcc.called_scope = ce;
fcc.object_ptr = value;
zend_call_function(&fci, &fcc TSRMLS_CC);
}
isolate->Enter();
}
zend_catch {
v8::V8::TerminateExecution(isolate);
V8JSG(fatal_error_abort) = 1;
}
zend_end_try();
failure:
/* Cleanup */
if (argc) {
for (i = 0; i < fci.param_count; i++) {
zval_ptr_dtor(&argv[i]);
}
efree(argv);
efree(fci.params);
}
if (retval_ptr != NULL) {
return_value = zval_to_v8js(retval_ptr, isolate TSRMLS_CC);
zval_ptr_dtor(&retval_ptr);
} else {
return_value = V8JS_NULL;
}
info.GetReturnValue().Set(return_value);
}
/* }}} */
/* Callback for PHP methods and functions */
static void php_v8js_php_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
V8JS_TSRMLS_FETCH();
zval *value = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
zend_function *method_ptr;
zend_class_entry *ce = Z_OBJCE_P(value);
/* Set method_ptr from v8::External or fetch the closure invoker */
if (!info.Data().IsEmpty() && info.Data()->IsExternal()) {
method_ptr = static_cast<zend_function *>(v8::External::Cast(*info.Data())->Value());
} else {
method_ptr = zend_get_closure_invoke_method(value TSRMLS_CC);
}
return php_v8js_call_php_func(value, ce, method_ptr, isolate, info TSRMLS_CC);
}
/* Callback for PHP constructor calls */
static void php_v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
info.GetReturnValue().Set(V8JS_UNDEFINED);
// @todo assert constructor call
v8::Handle<v8::Object> newobj = info.This();
v8::Local<v8::External> php_object;
zval *value;
if (!info.IsConstructCall()) {
return;
}
v8::Local<v8::Array> cons_data = v8::Local<v8::Array>::Cast(info.Data());
v8::Local<v8::External> ext_tmpl = v8::Local<v8::External>::Cast(cons_data->Get(0));
v8::Local<v8::External> ext_ce = v8::Local<v8::External>::Cast(cons_data->Get(1));
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
if (info[0]->IsExternal()) {
// Object created by v8js in php_v8js_hash_to_jsobj, PHP object passed as v8::External.
php_object = v8::Local<v8::External>::Cast(info[0]);
value = reinterpret_cast<zval *>(php_object->Value());
if(ctx->weak_objects.count(value)) {
// We already exported this object, hence no need to add another
// ref, v8 won't give us a second weak-object callback anyways.
newobj->SetAlignedPointerInInternalField(0, ext_tmpl->Value());
newobj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), php_object);
return;
}
// Increase the reference count of this value because we're storing it internally for use later
// See https://github.com/preillyme/v8js/issues/6
Z_ADDREF_P(value);
} else {
// Object created from JavaScript context. Need to create PHP object first.
V8JS_TSRMLS_FETCH();
zend_class_entry *ce = static_cast<zend_class_entry *>(ext_ce->Value());
zend_function *ctor_ptr = ce->constructor;
// Check access on __construct function, if any
if (ctor_ptr != NULL && (ctor_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Call to protected __construct() not allowed")));
return;
}
MAKE_STD_ZVAL(value);
object_init_ex(value, ce);
// Call __construct function
if (ctor_ptr != NULL) {
php_v8js_call_php_func(value, ce, ctor_ptr, isolate, info TSRMLS_CC);
}
php_object = v8::External::New(isolate, value);
}
newobj->SetAlignedPointerInInternalField(0, ext_tmpl->Value());
newobj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), php_object);
// Since we got to decrease the reference count again, in case v8 garbage collector
// decides to dispose the JS object, we add a weak persistent handle and register
// a callback function that removes the reference.
ctx->weak_objects[value].Reset(isolate, newobj);
ctx->weak_objects[value].SetWeak(value, php_v8js_weak_object_callback);
// Just tell v8 that we're allocating some external memory
// (for the moment we just always tell 1k instead of trying to find out actual values)
isolate->AdjustAmountOfExternalAllocatedMemory(1024);
}
/* }}} */
static int _php_v8js_is_assoc_array(HashTable *myht TSRMLS_DC) /* {{{ */
{
@ -280,668 +53,6 @@ static int _php_v8js_is_assoc_array(HashTable *myht TSRMLS_DC) /* {{{ */
}
/* }}} */
static void php_v8js_weak_object_callback(const v8::WeakCallbackData<v8::Object, zval> &data) {
v8::Isolate *isolate = data.GetIsolate();
zval *value = data.GetParameter();
zval_ptr_dtor(&value);
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
ctx->weak_objects.at(value).Reset();
ctx->weak_objects.erase(value);
isolate->AdjustAmountOfExternalAllocatedMemory(-1024);
}
static void php_v8js_weak_closure_callback(const v8::WeakCallbackData<v8::Object, v8js_tmpl_t> &data) {
v8::Isolate *isolate = data.GetIsolate();
v8js_tmpl_t *persist_tpl_ = data.GetParameter();
persist_tpl_->Reset();
delete persist_tpl_;
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
ctx->weak_closures.at(persist_tpl_).Reset();
ctx->weak_closures.erase(persist_tpl_);
};
/* These are not defined by Zend */
#define ZEND_WAKEUP_FUNC_NAME "__wakeup"
#define ZEND_SLEEP_FUNC_NAME "__sleep"
#define ZEND_SET_STATE_FUNC_NAME "__set_state"
#define IS_MAGIC_FUNC(mname) \
((key_len == sizeof(mname)) && \
!strncasecmp(key, mname, key_len - 1))
#define PHP_V8JS_CALLBACK(isolate, mptr, tmpl) \
v8::FunctionTemplate::New((isolate), php_v8js_php_callback, v8::External::New((isolate), mptr), v8::Signature::New((isolate), tmpl))->GetFunction()
static void php_v8js_named_property_enumerator(const v8::PropertyCallbackInfo<v8::Array> &info) /* {{{ */
{
// note: 'special' properties like 'constructor' are not enumerated.
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
v8::Local<v8::Array> result = v8::Array::New(isolate, 0);
uint32_t result_len = 0;
V8JS_TSRMLS_FETCH();
zend_class_entry *ce;
zend_function *method_ptr;
HashTable *proptable;
HashPosition pos;
char *key = NULL;
uint key_len;
ulong index;
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
ce = Z_OBJCE_P(object);
/* enumerate all methods */
zend_hash_internal_pointer_reset_ex(&ce->function_table, &pos);
for (;; zend_hash_move_forward_ex(&ce->function_table, &pos)) {
if (zend_hash_get_current_key_ex(&ce->function_table, &key, &key_len, &index, 0, &pos) != HASH_KEY_IS_STRING ||
zend_hash_get_current_data_ex(&ce->function_table, (void **) &method_ptr, &pos) == FAILURE
) {
break;
}
if ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
/* Allow only public methods */
continue;
}
if ((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) != 0) {
/* no __construct, __destruct(), or __clone() functions */
continue;
}
// hide (do not enumerate) other PHP magic functions
if (IS_MAGIC_FUNC(ZEND_CALLSTATIC_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_SLEEP_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_WAKEUP_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_SET_STATE_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_GET_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_SET_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_UNSET_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_CALL_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_INVOKE_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_ISSET_FUNC_NAME)) {
continue;
}
v8::Local<v8::String> method_name = V8JS_STR(method_ptr->common.function_name);
// rename PHP special method names to JS equivalents.
if (IS_MAGIC_FUNC(ZEND_TOSTRING_FUNC_NAME)) {
method_name = V8JS_SYM("toString");
}
result->Set(result_len++, method_name);
}
/* enumerate all properties */
/* Z_OBJPROP uses the get_properties handler */
proptable = Z_OBJPROP_P(object);
zend_hash_internal_pointer_reset_ex(proptable, &pos);
for (;; zend_hash_move_forward_ex(proptable, &pos)) {
int i = zend_hash_get_current_key_ex(proptable, &key, &key_len, &index, 0, &pos);
if (i == HASH_KEY_NON_EXISTANT)
break;
// for consistency with the 'in' operator, skip properties whose
// value IS_NULL (like isset does)
zval **data;
if (zend_hash_get_current_data_ex(proptable, (void **) &data, &pos) == SUCCESS &&
ZVAL_IS_NULL(*data))
continue;
if (i == HASH_KEY_IS_STRING) {
/* skip protected and private members */
if (key[0] == '\0') {
continue;
}
// prefix enumerated property names with '$' so they can be
// dereferenced unambiguously (ie, don't conflict with method
// names)
char *prefixed = static_cast<char *>(emalloc(key_len + 1));
prefixed[0] = '$';
strncpy(prefixed + 1, key, key_len);
result->Set(result_len++, V8JS_STRL(prefixed, key_len));
efree(prefixed);
} else {
// even numeric indices are enumerated as strings in JavaScript
result->Set(result_len++, V8JS_FLOAT((double) index)->ToString());
}
}
/* done */
info.GetReturnValue().Set(result);
}
/* }}} */
static void php_v8js_invoke_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(info.Data());
int argc = info.Length(), i;
v8::Local<v8::Value> *argv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
v8::Local<v8::Value> result;
V8JS_TSRMLS_FETCH();
for (i=0; i<argc; i++) {
new(&argv[i]) v8::Local<v8::Value>;
argv[i] = info[i];
}
if (info.IsConstructCall()) {
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
v8::String::Utf8Value str(self->GetConstructorName()->ToString());
const char *constructor_name = ToCString(str);
zend_class_entry **pce;
zend_lookup_class(constructor_name, str.length(), &pce TSRMLS_CC);
v8::Local<v8::FunctionTemplate> new_tpl;
new_tpl = v8::Local<v8::FunctionTemplate>::New
(isolate, ctx->template_cache.at((*pce)->name));
result = new_tpl->GetFunction()->NewInstance(argc, argv);
} else {
result = cb->Call(self, argc, argv);
}
info.GetReturnValue().Set(result);
}
/* }}} */
// this is a magic '__call' implementation for PHP classes which don't actually
// have a '__call' magic function. This way we can always force a method
// call (as opposed to a property get) from JavaScript using __call.
static void php_v8js_fake_call_impl(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
v8::Handle<v8::Value> return_value;
char *error;
int error_len;
V8JS_TSRMLS_FETCH();
zend_class_entry *ce;
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
ce = Z_OBJCE_P(object);
// first arg is method name, second arg is array of args.
if (info.Length() < 2) {
error_len = spprintf(&error, 0,
"%s::__call expects 2 parameters, %d given",
ce->name, (int) info.Length());
return_value = V8JS_THROW(isolate, TypeError, error, error_len);
efree(error);
info.GetReturnValue().Set(return_value);
return;
}
if (!info[1]->IsArray()) {
error_len = spprintf(&error, 0,
"%s::__call expects 2nd parameter to be an array",
ce->name);
return_value = V8JS_THROW(isolate, TypeError, error, error_len);
efree(error);
info.GetReturnValue().Set(return_value);
return;
}
v8::String::Utf8Value str(info[0]->ToString());
const char *method_name = ToCString(str);
uint method_name_len = strlen(method_name);
v8::Local<v8::Array> args = v8::Local<v8::Array>::Cast(info[1]);
if (args->Length() > 1000000) {
// prevent overflow, since args->Length() is a uint32_t and args
// in the Function->Call method below is a (signed) int.
error_len = spprintf(&error, 0,
"%s::__call expects fewer than a million arguments",
ce->name);
return_value = V8JS_THROW(isolate, TypeError, error, error_len);
efree(error);
info.GetReturnValue().Set(return_value);
return;
}
// okay, look up the method name and manually invoke it.
const zend_object_handlers *h = Z_OBJ_HT_P(object);
zend_function *method_ptr =
h->get_method(&object, (char*)method_name, method_name_len
ZEND_HASH_KEY_NULL TSRMLS_CC);
if (method_ptr == NULL ||
(method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0 ||
(method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) != 0) {
error_len = spprintf(&error, 0,
"%s::__call to %s method %s", ce->name,
(method_ptr == NULL) ? "undefined" : "non-public", method_name);
return_value = V8JS_THROW(isolate, TypeError, error, error_len);
efree(error);
info.GetReturnValue().Set(return_value);
return;
}
v8::Local<v8::FunctionTemplate> tmpl =
v8::Local<v8::FunctionTemplate>::New
(isolate, *reinterpret_cast<v8js_tmpl_t *>(self->GetAlignedPointerFromInternalField(0)));
// use php_v8js_php_callback to actually execute the method
v8::Local<v8::Function> cb = PHP_V8JS_CALLBACK(isolate, method_ptr, tmpl);
uint32_t i, argc = args->Length();
v8::Local<v8::Value> *argv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
for (i=0; i<argc; i++) {
new(&argv[i]) v8::Local<v8::Value>;
argv[i] = args->Get(i);
}
return_value = cb->Call(info.This(), (int) argc, argv);
info.GetReturnValue().Set(return_value);
}
/* }}} */
typedef enum {
V8JS_PROP_GETTER,
V8JS_PROP_SETTER,
V8JS_PROP_QUERY,
V8JS_PROP_DELETER
} property_op_t;
/* This method handles named property and method get/set/query/delete. */
template<typename T>
static inline v8::Local<v8::Value> php_v8js_named_property_callback(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<T> &info, property_op_t callback_type, v8::Local<v8::Value> set_value = v8::Local<v8::Value>()) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::String::Utf8Value cstr(property);
const char *name = ToCString(cstr);
uint name_len = strlen(name);
char *lower = estrndup(name, name_len);
const char *method_name;
uint method_name_len;
v8::Local<v8::Object> self = info.Holder();
v8::Local<v8::Value> ret_value;
v8::Local<v8::Function> cb;
V8JS_TSRMLS_FETCH();
zend_class_entry *scope, *ce;
zend_function *method_ptr = NULL;
zval *php_value;
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
v8::Local<v8::FunctionTemplate> tmpl =
v8::Local<v8::FunctionTemplate>::New
(isolate, *reinterpret_cast<v8js_tmpl_t *>(self->GetAlignedPointerFromInternalField(0)));
ce = scope = Z_OBJCE_P(object);
/* First, check the (case-insensitive) method table */
php_strtolower(lower, name_len);
method_name = lower;
method_name_len = name_len;
// toString() -> __tostring()
if (name_len == 8 && strcmp(name, "toString") == 0) {
method_name = ZEND_TOSTRING_FUNC_NAME;
method_name_len = sizeof(ZEND_TOSTRING_FUNC_NAME) - 1;
}
bool is_constructor = (name_len == 11 && strcmp(name, "constructor") == 0);
bool is_magic_call = (method_name_len == 6 && strcmp(method_name, "__call") == 0);
if (is_constructor ||
(name[0] != '$' /* leading '$' means property, not method */ &&
zend_hash_find(&ce->function_table, method_name, method_name_len + 1, (void**)&method_ptr) == SUCCESS &&
((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) != 0) && /* Allow only public methods */
((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) == 0) /* no __construct, __destruct(), or __clone() functions */
) || (method_ptr=NULL, is_magic_call)
) {
if (callback_type == V8JS_PROP_GETTER) {
if (is_constructor) {
// Don't set a return value here, i.e. indicate that we don't
// have a special value. V8 "knows" the constructor anyways
// (from the template) and will use that.
} else {
if (is_magic_call && method_ptr==NULL) {
// Fake __call implementation
// (only use this if method_ptr==NULL, which means
// there is no actual PHP __call() implementation)
v8::Local<v8::Function> cb =
v8::FunctionTemplate::New(isolate,
php_v8js_fake_call_impl, V8JS_NULL,
v8::Signature::New(isolate, tmpl))->GetFunction();
cb->SetName(property);
ret_value = cb;
} else {
ret_value = PHP_V8JS_CALLBACK(isolate, method_ptr, tmpl);
}
}
} else if (callback_type == V8JS_PROP_QUERY) {
// methods are not enumerable
ret_value = V8JS_UINT(v8::ReadOnly|v8::DontEnum|v8::DontDelete);
} else if (callback_type == V8JS_PROP_SETTER) {
ret_value = set_value; // lie. this field is read-only.
} else if (callback_type == V8JS_PROP_DELETER) {
ret_value = V8JS_BOOL(false);
} else {
/* shouldn't reach here! but bail safely */
ret_value = v8::Handle<v8::Value>();
}
} else {
if (name[0]=='$') {
// this is a property (not a method)
name++; name_len--;
}
if (callback_type == V8JS_PROP_GETTER) {
/* Nope, not a method -- must be a (case-sensitive) property */
zval zname;
INIT_ZVAL(zname);
ZVAL_STRINGL(&zname, name, name_len, 0);
zend_property_info *property_info = zend_get_property_info(ce, &zname, 1 TSRMLS_CC);
if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
php_value = zend_read_property(NULL, object, V8JS_CONST name, name_len, true TSRMLS_CC);
// special case uninitialized_zval_ptr and return an empty value
// (indicating that we don't intercept this property) if the
// property doesn't exist.
if (php_value == EG(uninitialized_zval_ptr)) {
ret_value = v8::Handle<v8::Value>();
} else {
// wrap it
ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
/* We don't own the reference to php_value... unless the
* returned refcount was 0, in which case the below code
* will free it. */
zval_add_ref(&php_value);
zval_ptr_dtor(&php_value);
}
}
else if (zend_hash_find(&ce->function_table, "__get", 6, (void**)&method_ptr) == SUCCESS
/* Allow only public methods */
&& ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) != 0)) {
/* Okay, let's call __get. */
zend_fcall_info fci;
zval fmember;
INIT_ZVAL(fmember);
ZVAL_STRING(&fmember, "__get", 0);
fci.size = sizeof(fci);
fci.function_table = &ce->function_table;
fci.function_name = &fmember;
fci.symbol_table = NULL;
fci.retval_ptr_ptr = &php_value;
zval *zname_ptr = &zname;
zval **zname_ptr_ptr = &zname_ptr;
fci.param_count = 1;
fci.params = &zname_ptr_ptr;
fci.object_ptr = object;
fci.no_separation = 0;
zend_call_function(&fci, NULL TSRMLS_CC);
ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
zval_ptr_dtor(&php_value);
}
} else if (callback_type == V8JS_PROP_SETTER) {
MAKE_STD_ZVAL(php_value);
if (v8js_to_zval(set_value, php_value, 0, isolate TSRMLS_CC) != SUCCESS) {
ret_value = v8::Handle<v8::Value>();
}
else {
zval zname;
INIT_ZVAL(zname);
ZVAL_STRINGL(&zname, name, name_len, 0);
zend_property_info *property_info = zend_get_property_info(ce, &zname, 1 TSRMLS_CC);
if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
zend_update_property(scope, object, V8JS_CONST name, name_len, php_value TSRMLS_CC);
ret_value = set_value;
}
else if (zend_hash_find(&ce->function_table, "__set", 6, (void**)&method_ptr) == SUCCESS
/* Allow only public methods */
&& ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) != 0)) {
/* Okay, let's call __set. */
zend_fcall_info fci;
zval fmember;
INIT_ZVAL(fmember);
ZVAL_STRING(&fmember, "__set", 0);
zval *php_ret_value;
fci.size = sizeof(fci);
fci.function_table = &ce->function_table;
fci.function_name = &fmember;
fci.symbol_table = NULL;
fci.retval_ptr_ptr = &php_ret_value;
zval *zname_ptr = &zname;
zval **params[2];
fci.param_count = 2;
fci.params = params;
fci.params[0] = &zname_ptr;
fci.params[1] = &php_value;
fci.object_ptr = object;
fci.no_separation = 1;
zend_call_function(&fci, NULL TSRMLS_CC);
ret_value = zval_to_v8js(php_ret_value, isolate TSRMLS_CC);
zval_ptr_dtor(&php_ret_value);
}
}
// if PHP wanted to hold on to this value, update_property would
// have bumped the refcount
zval_ptr_dtor(&php_value);
} else if (callback_type == V8JS_PROP_QUERY ||
callback_type == V8JS_PROP_DELETER) {
const zend_object_handlers *h = Z_OBJ_HT_P(object);
zval *prop;
MAKE_STD_ZVAL(prop);
ZVAL_STRINGL(prop, name, name_len, 1);
if (callback_type == V8JS_PROP_QUERY) {
if (h->has_property(object, prop, 0 ZEND_HASH_KEY_NULL TSRMLS_CC)) {
ret_value = V8JS_UINT(v8::None);
} else {
ret_value = v8::Handle<v8::Value>(); // empty handle
}
} else {
zend_property_info *property_info = zend_get_property_info(ce, prop, 1 TSRMLS_CC);
if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
h->unset_property(object, prop ZEND_HASH_KEY_NULL TSRMLS_CC);
ret_value = V8JS_BOOL(true);
}
else {
ret_value = v8::Handle<v8::Value>(); // empty handle
}
}
zval_ptr_dtor(&prop);
} else {
/* shouldn't reach here! but bail safely */
ret_value = v8::Handle<v8::Value>();
}
}
efree(lower);
return ret_value;
}
/* }}} */
static void php_v8js_named_property_getter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
{
info.GetReturnValue().Set(php_v8js_named_property_callback(property, info, V8JS_PROP_GETTER));
}
/* }}} */
static void php_v8js_named_property_setter(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
{
info.GetReturnValue().Set(php_v8js_named_property_callback(property, info, V8JS_PROP_SETTER, value));
}
/* }}} */
static void php_v8js_named_property_query(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Integer> &info) /* {{{ */
{
v8::Local<v8::Value> r = php_v8js_named_property_callback(property, info, V8JS_PROP_QUERY);
if (!r.IsEmpty()) {
info.GetReturnValue().Set(r->ToInteger());
}
}
/* }}} */
static void php_v8js_named_property_deleter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Boolean> &info) /* {{{ */
{
v8::Local<v8::Value> r = php_v8js_named_property_callback(property, info, V8JS_PROP_DELETER);
if (!r.IsEmpty()) {
info.GetReturnValue().Set(r->ToBoolean());
}
}
/* }}} */
static v8::Handle<v8::Value> php_v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
{
v8::Handle<v8::Object> newobj;
int i;
char *key = NULL;
ulong index;
uint key_len;
HashTable *myht;
HashPosition pos;
zend_class_entry *ce = NULL;
if (Z_TYPE_P(value) == IS_ARRAY) {
myht = HASH_OF(value);
} else {
myht = Z_OBJPROP_P(value);
ce = Z_OBJCE_P(value);
}
/* Prevent recursion */
if (myht && myht->nApplyCount > 1) {
return V8JS_NULL;
}
/* Object methods */
if (ce == php_ce_v8_function) {
php_v8js_object *c = (php_v8js_object *) zend_object_store_get_object(value TSRMLS_CC);
if(isolate != c->ctx->isolate) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "V8Function object passed to wrong V8Js instance");
return V8JS_NULL;
}
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, c->v8obj);
return v8obj;
} else if (ce) {
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
v8::Local<v8::FunctionTemplate> new_tpl;
v8js_tmpl_t *persist_tpl_;
try {
new_tpl = v8::Local<v8::FunctionTemplate>::New
(isolate, ctx->template_cache.at(ce->name));
}
catch (const std::out_of_range &) {
/* No cached v8::FunctionTemplate available as of yet, create one. */
new_tpl = v8::FunctionTemplate::New(isolate, 0);
new_tpl->SetClassName(V8JS_STRL(ce->name, ce->name_length));
new_tpl->InstanceTemplate()->SetInternalFieldCount(1);
if (ce == zend_ce_closure) {
/* Got a closure, mustn't cache ... */
persist_tpl_ = new v8js_tmpl_t(isolate, new_tpl);
/* We'll free persist_tpl_ via php_v8js_weak_closure_callback, below */
new_tpl->InstanceTemplate()->SetCallAsFunctionHandler(php_v8js_php_callback);
} else {
/* Add new v8::FunctionTemplate to tpl_map, as long as it is not a closure. */
persist_tpl_ = &ctx->template_cache[ce->name];
persist_tpl_->Reset(isolate, new_tpl);
/* We'll free persist_tpl_ when template_cache is destroyed */
// Finish setup of new_tpl
new_tpl->InstanceTemplate()->SetNamedPropertyHandler
(php_v8js_named_property_getter, /* getter */
php_v8js_named_property_setter, /* setter */
php_v8js_named_property_query, /* query */
php_v8js_named_property_deleter, /* deleter */
php_v8js_named_property_enumerator, /* enumerator */
V8JS_NULL /* data */
);
// add __invoke() handler
zend_function *invoke_method_ptr;
if (zend_hash_find(&ce->function_table, ZEND_INVOKE_FUNC_NAME,
sizeof(ZEND_INVOKE_FUNC_NAME),
(void**)&invoke_method_ptr) == SUCCESS &&
invoke_method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) {
new_tpl->InstanceTemplate()->SetCallAsFunctionHandler(php_v8js_invoke_callback, PHP_V8JS_CALLBACK(isolate, invoke_method_ptr, new_tpl));
}
}
v8::Local<v8::Array> call_handler_data = v8::Array::New(isolate, 2);
call_handler_data->Set(0, v8::External::New(isolate, persist_tpl_));
call_handler_data->Set(1, v8::External::New(isolate, ce));
new_tpl->SetCallHandler(php_v8js_construct_callback, call_handler_data);
}
// Create v8 wrapper object
v8::Handle<v8::Value> external = v8::External::New(isolate, value);
newobj = new_tpl->GetFunction()->NewInstance(1, &external);
if (ce == zend_ce_closure) {
// free uncached function template when object is freed
ctx->weak_closures[persist_tpl_].Reset(isolate, newobj);
ctx->weak_closures[persist_tpl_].SetWeak(persist_tpl_, php_v8js_weak_closure_callback);
}
} else {
// @todo re-use template likewise
v8::Local<v8::FunctionTemplate> new_tpl = v8::FunctionTemplate::New(isolate, 0);
new_tpl->SetClassName(V8JS_SYM("Array"));
newobj = new_tpl->InstanceTemplate()->NewInstance();
}
/* Object properties */
i = myht ? zend_hash_num_elements(myht) : 0;
if (i > 0 && !ce)
{
zval **data;
HashTable *tmp_ht;
zend_hash_internal_pointer_reset_ex(myht, &pos);
for (;; zend_hash_move_forward_ex(myht, &pos)) {
i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
if (i == HASH_KEY_NON_EXISTANT)
break;
if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS)
{
tmp_ht = HASH_OF(*data);
if (tmp_ht) {
tmp_ht->nApplyCount++;
}
if (i == HASH_KEY_IS_STRING)
{
if (key[0] == '\0' && Z_TYPE_P(value) == IS_OBJECT) {
/* Skip protected and private members. */
if (tmp_ht) {
tmp_ht->nApplyCount--;
}
continue;
}
newobj->Set(V8JS_STRL(key, key_len - 1), zval_to_v8js(*data, isolate TSRMLS_CC));
} else {
newobj->Set(index, zval_to_v8js(*data, isolate TSRMLS_CC));
}
if (tmp_ht) {
tmp_ht->nApplyCount--;
}
}
}
}
return newobj;
}
/* }}} */
static v8::Handle<v8::Value> php_v8js_hash_to_jsarr(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
{

985
v8js_object_export.cc Normal file
View File

@ -0,0 +1,985 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| http://www.opensource.org/licenses/mit-license.php MIT License |
+----------------------------------------------------------------------+
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
| Author: Patrick Reilly <preilly@php.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
extern "C" {
#include "php.h"
#include "ext/date/php_date.h"
#include "ext/standard/php_string.h"
#include "zend_interfaces.h"
#include "zend_closures.h"
}
#include "php_v8js_macros.h"
#include "v8js_array_access.h"
#include "v8js_object_export.h"
static void php_v8js_weak_object_callback(const v8::WeakCallbackData<v8::Object, zval> &data);
/* Callback for PHP methods and functions */
static void php_v8js_call_php_func(zval *value, zend_class_entry *ce, zend_function *method_ptr, v8::Isolate *isolate, const v8::FunctionCallbackInfo<v8::Value>& info TSRMLS_DC) /* {{{ */
{
v8::Handle<v8::Value> return_value;
zend_fcall_info fci;
zend_fcall_info_cache fcc;
zval fname, *retval_ptr = NULL, **argv = NULL;
zend_uint argc = info.Length(), min_num_args = 0, max_num_args = 0;
char *error;
int error_len, i, flags = V8JS_FLAG_NONE;
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
/* Set parameter limits */
min_num_args = method_ptr->common.required_num_args;
max_num_args = method_ptr->common.num_args;
/* Function name to call */
INIT_ZVAL(fname);
ZVAL_STRING(&fname, method_ptr->common.function_name, 0);
/* zend_fcall_info */
fci.size = sizeof(fci);
fci.function_table = &ce->function_table;
fci.function_name = &fname;
fci.symbol_table = NULL;
fci.object_ptr = value;
fci.retval_ptr_ptr = &retval_ptr;
fci.param_count = 0;
/* Check for passed vs required number of arguments */
if (argc < min_num_args)
{
error_len = spprintf(&error, 0,
"%s::%s() expects %s %d parameter%s, %d given",
ce->name,
method_ptr->common.function_name,
min_num_args == max_num_args ? "exactly" : argc < min_num_args ? "at least" : "at most",
argc < min_num_args ? min_num_args : max_num_args,
(argc < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
argc);
return_value = V8JS_THROW(isolate, TypeError, error, error_len);
if (ce == zend_ce_closure) {
efree(const_cast<char*>(method_ptr->internal_function.function_name));
efree(method_ptr);
}
efree(error);
info.GetReturnValue().Set(return_value);
return;
}
/* Convert parameters passed from V8 */
if (argc) {
flags = V8JS_GLOBAL_GET_FLAGS(isolate);
fci.params = (zval ***) safe_emalloc(argc, sizeof(zval **), 0);
argv = (zval **) safe_emalloc(argc, sizeof(zval *), 0);
for (i = 0; i < argc; i++) {
v8::Local<v8::Value> php_object;
if (info[i]->IsObject()) {
php_object = v8::Local<v8::Object>::Cast(info[i])->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
}
if (!php_object.IsEmpty()) {
/* This is a PHP object, passed to JS and back. */
argv[i] = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
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++;
error_len = spprintf(&error, 0, "converting parameter #%d passed to %s() failed", i + 1, method_ptr->common.function_name);
return_value = V8JS_THROW(isolate, Error, error, error_len);
efree(error);
goto failure;
}
}
fci.params[fci.param_count++] = &argv[i];
}
} else {
fci.params = NULL;
}
fci.no_separation = 1;
info.GetReturnValue().Set(V8JS_NULL);
zend_try {
{
isolate->Exit();
v8::Unlocker unlocker(isolate);
/* zend_fcall_info_cache */
fcc.initialized = 1;
fcc.function_handler = method_ptr;
fcc.calling_scope = ce;
fcc.called_scope = ce;
fcc.object_ptr = value;
zend_call_function(&fci, &fcc TSRMLS_CC);
}
isolate->Enter();
}
zend_catch {
v8::V8::TerminateExecution(isolate);
V8JSG(fatal_error_abort) = 1;
}
zend_end_try();
failure:
/* Cleanup */
if (argc) {
for (i = 0; i < fci.param_count; i++) {
zval_ptr_dtor(&argv[i]);
}
efree(argv);
efree(fci.params);
}
if (retval_ptr != NULL) {
return_value = zval_to_v8js(retval_ptr, isolate TSRMLS_CC);
zval_ptr_dtor(&retval_ptr);
} else {
return_value = V8JS_NULL;
}
info.GetReturnValue().Set(return_value);
}
/* }}} */
/* Callback for PHP methods and functions */
static void php_v8js_php_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
V8JS_TSRMLS_FETCH();
zval *value = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
zend_function *method_ptr;
zend_class_entry *ce = Z_OBJCE_P(value);
/* Set method_ptr from v8::External or fetch the closure invoker */
if (!info.Data().IsEmpty() && info.Data()->IsExternal()) {
method_ptr = static_cast<zend_function *>(v8::External::Cast(*info.Data())->Value());
} else {
method_ptr = zend_get_closure_invoke_method(value TSRMLS_CC);
}
return php_v8js_call_php_func(value, ce, method_ptr, isolate, info TSRMLS_CC);
}
/* Callback for PHP constructor calls */
static void php_v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
info.GetReturnValue().Set(V8JS_UNDEFINED);
// @todo assert constructor call
v8::Handle<v8::Object> newobj = info.This();
v8::Local<v8::External> php_object;
zval *value;
if (!info.IsConstructCall()) {
return;
}
v8::Local<v8::Array> cons_data = v8::Local<v8::Array>::Cast(info.Data());
v8::Local<v8::External> ext_tmpl = v8::Local<v8::External>::Cast(cons_data->Get(0));
v8::Local<v8::External> ext_ce = v8::Local<v8::External>::Cast(cons_data->Get(1));
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
if (info[0]->IsExternal()) {
// Object created by v8js in php_v8js_hash_to_jsobj, PHP object passed as v8::External.
php_object = v8::Local<v8::External>::Cast(info[0]);
value = reinterpret_cast<zval *>(php_object->Value());
if(ctx->weak_objects.count(value)) {
// We already exported this object, hence no need to add another
// ref, v8 won't give us a second weak-object callback anyways.
newobj->SetAlignedPointerInInternalField(0, ext_tmpl->Value());
newobj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), php_object);
return;
}
// Increase the reference count of this value because we're storing it internally for use later
// See https://github.com/preillyme/v8js/issues/6
Z_ADDREF_P(value);
} else {
// Object created from JavaScript context. Need to create PHP object first.
V8JS_TSRMLS_FETCH();
zend_class_entry *ce = static_cast<zend_class_entry *>(ext_ce->Value());
zend_function *ctor_ptr = ce->constructor;
// Check access on __construct function, if any
if (ctor_ptr != NULL && (ctor_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Call to protected __construct() not allowed")));
return;
}
MAKE_STD_ZVAL(value);
object_init_ex(value, ce);
// Call __construct function
if (ctor_ptr != NULL) {
php_v8js_call_php_func(value, ce, ctor_ptr, isolate, info TSRMLS_CC);
}
php_object = v8::External::New(isolate, value);
}
newobj->SetAlignedPointerInInternalField(0, ext_tmpl->Value());
newobj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), php_object);
// Since we got to decrease the reference count again, in case v8 garbage collector
// decides to dispose the JS object, we add a weak persistent handle and register
// a callback function that removes the reference.
ctx->weak_objects[value].Reset(isolate, newobj);
ctx->weak_objects[value].SetWeak(value, php_v8js_weak_object_callback);
// Just tell v8 that we're allocating some external memory
// (for the moment we just always tell 1k instead of trying to find out actual values)
isolate->AdjustAmountOfExternalAllocatedMemory(1024);
}
/* }}} */
static void php_v8js_weak_object_callback(const v8::WeakCallbackData<v8::Object, zval> &data) {
v8::Isolate *isolate = data.GetIsolate();
zval *value = data.GetParameter();
zval_ptr_dtor(&value);
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
ctx->weak_objects.at(value).Reset();
ctx->weak_objects.erase(value);
isolate->AdjustAmountOfExternalAllocatedMemory(-1024);
}
static void php_v8js_weak_closure_callback(const v8::WeakCallbackData<v8::Object, v8js_tmpl_t> &data) {
v8::Isolate *isolate = data.GetIsolate();
v8js_tmpl_t *persist_tpl_ = data.GetParameter();
persist_tpl_->Reset();
delete persist_tpl_;
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
ctx->weak_closures.at(persist_tpl_).Reset();
ctx->weak_closures.erase(persist_tpl_);
};
/* These are not defined by Zend */
#define ZEND_WAKEUP_FUNC_NAME "__wakeup"
#define ZEND_SLEEP_FUNC_NAME "__sleep"
#define ZEND_SET_STATE_FUNC_NAME "__set_state"
#define IS_MAGIC_FUNC(mname) \
((key_len == sizeof(mname)) && \
!strncasecmp(key, mname, key_len - 1))
#define PHP_V8JS_CALLBACK(isolate, mptr, tmpl) \
v8::FunctionTemplate::New((isolate), php_v8js_php_callback, v8::External::New((isolate), mptr), v8::Signature::New((isolate), tmpl))->GetFunction()
static void php_v8js_named_property_enumerator(const v8::PropertyCallbackInfo<v8::Array> &info) /* {{{ */
{
// note: 'special' properties like 'constructor' are not enumerated.
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
v8::Local<v8::Array> result = v8::Array::New(isolate, 0);
uint32_t result_len = 0;
V8JS_TSRMLS_FETCH();
zend_class_entry *ce;
zend_function *method_ptr;
HashTable *proptable;
HashPosition pos;
char *key = NULL;
uint key_len;
ulong index;
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
ce = Z_OBJCE_P(object);
/* enumerate all methods */
zend_hash_internal_pointer_reset_ex(&ce->function_table, &pos);
for (;; zend_hash_move_forward_ex(&ce->function_table, &pos)) {
if (zend_hash_get_current_key_ex(&ce->function_table, &key, &key_len, &index, 0, &pos) != HASH_KEY_IS_STRING ||
zend_hash_get_current_data_ex(&ce->function_table, (void **) &method_ptr, &pos) == FAILURE
) {
break;
}
if ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
/* Allow only public methods */
continue;
}
if ((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) != 0) {
/* no __construct, __destruct(), or __clone() functions */
continue;
}
// hide (do not enumerate) other PHP magic functions
if (IS_MAGIC_FUNC(ZEND_CALLSTATIC_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_SLEEP_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_WAKEUP_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_SET_STATE_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_GET_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_SET_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_UNSET_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_CALL_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_INVOKE_FUNC_NAME) ||
IS_MAGIC_FUNC(ZEND_ISSET_FUNC_NAME)) {
continue;
}
v8::Local<v8::String> method_name = V8JS_STR(method_ptr->common.function_name);
// rename PHP special method names to JS equivalents.
if (IS_MAGIC_FUNC(ZEND_TOSTRING_FUNC_NAME)) {
method_name = V8JS_SYM("toString");
}
result->Set(result_len++, method_name);
}
/* enumerate all properties */
/* Z_OBJPROP uses the get_properties handler */
proptable = Z_OBJPROP_P(object);
zend_hash_internal_pointer_reset_ex(proptable, &pos);
for (;; zend_hash_move_forward_ex(proptable, &pos)) {
int i = zend_hash_get_current_key_ex(proptable, &key, &key_len, &index, 0, &pos);
if (i == HASH_KEY_NON_EXISTANT)
break;
// for consistency with the 'in' operator, skip properties whose
// value IS_NULL (like isset does)
zval **data;
if (zend_hash_get_current_data_ex(proptable, (void **) &data, &pos) == SUCCESS &&
ZVAL_IS_NULL(*data))
continue;
if (i == HASH_KEY_IS_STRING) {
/* skip protected and private members */
if (key[0] == '\0') {
continue;
}
// prefix enumerated property names with '$' so they can be
// dereferenced unambiguously (ie, don't conflict with method
// names)
char *prefixed = static_cast<char *>(emalloc(key_len + 1));
prefixed[0] = '$';
strncpy(prefixed + 1, key, key_len);
result->Set(result_len++, V8JS_STRL(prefixed, key_len));
efree(prefixed);
} else {
// even numeric indices are enumerated as strings in JavaScript
result->Set(result_len++, V8JS_FLOAT((double) index)->ToString());
}
}
/* done */
info.GetReturnValue().Set(result);
}
/* }}} */
static void php_v8js_invoke_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(info.Data());
int argc = info.Length(), i;
v8::Local<v8::Value> *argv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
v8::Local<v8::Value> result;
V8JS_TSRMLS_FETCH();
for (i=0; i<argc; i++) {
new(&argv[i]) v8::Local<v8::Value>;
argv[i] = info[i];
}
if (info.IsConstructCall()) {
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
v8::String::Utf8Value str(self->GetConstructorName()->ToString());
const char *constructor_name = ToCString(str);
zend_class_entry **pce;
zend_lookup_class(constructor_name, str.length(), &pce TSRMLS_CC);
v8::Local<v8::FunctionTemplate> new_tpl;
new_tpl = v8::Local<v8::FunctionTemplate>::New
(isolate, ctx->template_cache.at((*pce)->name));
result = new_tpl->GetFunction()->NewInstance(argc, argv);
} else {
result = cb->Call(self, argc, argv);
}
info.GetReturnValue().Set(result);
}
/* }}} */
// this is a magic '__call' implementation for PHP classes which don't actually
// have a '__call' magic function. This way we can always force a method
// call (as opposed to a property get) from JavaScript using __call.
static void php_v8js_fake_call_impl(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
v8::Handle<v8::Value> return_value;
char *error;
int error_len;
V8JS_TSRMLS_FETCH();
zend_class_entry *ce;
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
ce = Z_OBJCE_P(object);
// first arg is method name, second arg is array of args.
if (info.Length() < 2) {
error_len = spprintf(&error, 0,
"%s::__call expects 2 parameters, %d given",
ce->name, (int) info.Length());
return_value = V8JS_THROW(isolate, TypeError, error, error_len);
efree(error);
info.GetReturnValue().Set(return_value);
return;
}
if (!info[1]->IsArray()) {
error_len = spprintf(&error, 0,
"%s::__call expects 2nd parameter to be an array",
ce->name);
return_value = V8JS_THROW(isolate, TypeError, error, error_len);
efree(error);
info.GetReturnValue().Set(return_value);
return;
}
v8::String::Utf8Value str(info[0]->ToString());
const char *method_name = ToCString(str);
uint method_name_len = strlen(method_name);
v8::Local<v8::Array> args = v8::Local<v8::Array>::Cast(info[1]);
if (args->Length() > 1000000) {
// prevent overflow, since args->Length() is a uint32_t and args
// in the Function->Call method below is a (signed) int.
error_len = spprintf(&error, 0,
"%s::__call expects fewer than a million arguments",
ce->name);
return_value = V8JS_THROW(isolate, TypeError, error, error_len);
efree(error);
info.GetReturnValue().Set(return_value);
return;
}
// okay, look up the method name and manually invoke it.
const zend_object_handlers *h = Z_OBJ_HT_P(object);
zend_function *method_ptr =
h->get_method(&object, (char*)method_name, method_name_len
ZEND_HASH_KEY_NULL TSRMLS_CC);
if (method_ptr == NULL ||
(method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0 ||
(method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) != 0) {
error_len = spprintf(&error, 0,
"%s::__call to %s method %s", ce->name,
(method_ptr == NULL) ? "undefined" : "non-public", method_name);
return_value = V8JS_THROW(isolate, TypeError, error, error_len);
efree(error);
info.GetReturnValue().Set(return_value);
return;
}
v8::Local<v8::FunctionTemplate> tmpl =
v8::Local<v8::FunctionTemplate>::New
(isolate, *reinterpret_cast<v8js_tmpl_t *>(self->GetAlignedPointerFromInternalField(0)));
// use php_v8js_php_callback to actually execute the method
v8::Local<v8::Function> cb = PHP_V8JS_CALLBACK(isolate, method_ptr, tmpl);
uint32_t i, argc = args->Length();
v8::Local<v8::Value> *argv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
for (i=0; i<argc; i++) {
new(&argv[i]) v8::Local<v8::Value>;
argv[i] = args->Get(i);
}
return_value = cb->Call(info.This(), (int) argc, argv);
info.GetReturnValue().Set(return_value);
}
/* }}} */
/* This method handles named property and method get/set/query/delete. */
template<typename T>
inline v8::Local<v8::Value> php_v8js_named_property_callback(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<T> &info, property_op_t callback_type, v8::Local<v8::Value> set_value) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::String::Utf8Value cstr(property);
const char *name = ToCString(cstr);
uint name_len = strlen(name);
char *lower = estrndup(name, name_len);
const char *method_name;
uint method_name_len;
v8::Local<v8::Object> self = info.Holder();
v8::Local<v8::Value> ret_value;
v8::Local<v8::Function> cb;
V8JS_TSRMLS_FETCH();
zend_class_entry *scope, *ce;
zend_function *method_ptr = NULL;
zval *php_value;
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
v8::Local<v8::FunctionTemplate> tmpl =
v8::Local<v8::FunctionTemplate>::New
(isolate, *reinterpret_cast<v8js_tmpl_t *>(self->GetAlignedPointerFromInternalField(0)));
ce = scope = Z_OBJCE_P(object);
/* First, check the (case-insensitive) method table */
php_strtolower(lower, name_len);
method_name = lower;
method_name_len = name_len;
// toString() -> __tostring()
if (name_len == 8 && strcmp(name, "toString") == 0) {
method_name = ZEND_TOSTRING_FUNC_NAME;
method_name_len = sizeof(ZEND_TOSTRING_FUNC_NAME) - 1;
}
bool is_constructor = (name_len == 11 && strcmp(name, "constructor") == 0);
bool is_magic_call = (method_name_len == 6 && strcmp(method_name, "__call") == 0);
if (is_constructor ||
(name[0] != '$' /* leading '$' means property, not method */ &&
zend_hash_find(&ce->function_table, method_name, method_name_len + 1, (void**)&method_ptr) == SUCCESS &&
((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) != 0) && /* Allow only public methods */
((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) == 0) /* no __construct, __destruct(), or __clone() functions */
) || (method_ptr=NULL, is_magic_call)
) {
if (callback_type == V8JS_PROP_GETTER) {
if (is_constructor) {
// Don't set a return value here, i.e. indicate that we don't
// have a special value. V8 "knows" the constructor anyways
// (from the template) and will use that.
} else {
if (is_magic_call && method_ptr==NULL) {
// Fake __call implementation
// (only use this if method_ptr==NULL, which means
// there is no actual PHP __call() implementation)
v8::Local<v8::Function> cb =
v8::FunctionTemplate::New(isolate,
php_v8js_fake_call_impl, V8JS_NULL,
v8::Signature::New(isolate, tmpl))->GetFunction();
cb->SetName(property);
ret_value = cb;
} else {
ret_value = PHP_V8JS_CALLBACK(isolate, method_ptr, tmpl);
}
}
} else if (callback_type == V8JS_PROP_QUERY) {
// methods are not enumerable
ret_value = V8JS_UINT(v8::ReadOnly|v8::DontEnum|v8::DontDelete);
} else if (callback_type == V8JS_PROP_SETTER) {
ret_value = set_value; // lie. this field is read-only.
} else if (callback_type == V8JS_PROP_DELETER) {
ret_value = V8JS_BOOL(false);
} else {
/* shouldn't reach here! but bail safely */
ret_value = v8::Handle<v8::Value>();
}
} else {
if (name[0]=='$') {
// this is a property (not a method)
name++; name_len--;
}
if (callback_type == V8JS_PROP_GETTER) {
/* Nope, not a method -- must be a (case-sensitive) property */
zval zname;
INIT_ZVAL(zname);
ZVAL_STRINGL(&zname, name, name_len, 0);
zend_property_info *property_info = zend_get_property_info(ce, &zname, 1 TSRMLS_CC);
if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
php_value = zend_read_property(NULL, object, V8JS_CONST name, name_len, true TSRMLS_CC);
// special case uninitialized_zval_ptr and return an empty value
// (indicating that we don't intercept this property) if the
// property doesn't exist.
if (php_value == EG(uninitialized_zval_ptr)) {
ret_value = v8::Handle<v8::Value>();
} else {
// wrap it
ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
/* We don't own the reference to php_value... unless the
* returned refcount was 0, in which case the below code
* will free it. */
zval_add_ref(&php_value);
zval_ptr_dtor(&php_value);
}
}
else if (zend_hash_find(&ce->function_table, "__get", 6, (void**)&method_ptr) == SUCCESS
/* Allow only public methods */
&& ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) != 0)) {
/* Okay, let's call __get. */
zend_fcall_info fci;
zval fmember;
INIT_ZVAL(fmember);
ZVAL_STRING(&fmember, "__get", 0);
fci.size = sizeof(fci);
fci.function_table = &ce->function_table;
fci.function_name = &fmember;
fci.symbol_table = NULL;
fci.retval_ptr_ptr = &php_value;
zval *zname_ptr = &zname;
zval **zname_ptr_ptr = &zname_ptr;
fci.param_count = 1;
fci.params = &zname_ptr_ptr;
fci.object_ptr = object;
fci.no_separation = 0;
zend_call_function(&fci, NULL TSRMLS_CC);
ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
zval_ptr_dtor(&php_value);
}
} else if (callback_type == V8JS_PROP_SETTER) {
MAKE_STD_ZVAL(php_value);
if (v8js_to_zval(set_value, php_value, 0, isolate TSRMLS_CC) != SUCCESS) {
ret_value = v8::Handle<v8::Value>();
}
else {
zval zname;
INIT_ZVAL(zname);
ZVAL_STRINGL(&zname, name, name_len, 0);
zend_property_info *property_info = zend_get_property_info(ce, &zname, 1 TSRMLS_CC);
if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
zend_update_property(scope, object, V8JS_CONST name, name_len, php_value TSRMLS_CC);
ret_value = set_value;
}
else if (zend_hash_find(&ce->function_table, "__set", 6, (void**)&method_ptr) == SUCCESS
/* Allow only public methods */
&& ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) != 0)) {
/* Okay, let's call __set. */
zend_fcall_info fci;
zval fmember;
INIT_ZVAL(fmember);
ZVAL_STRING(&fmember, "__set", 0);
zval *php_ret_value;
fci.size = sizeof(fci);
fci.function_table = &ce->function_table;
fci.function_name = &fmember;
fci.symbol_table = NULL;
fci.retval_ptr_ptr = &php_ret_value;
zval *zname_ptr = &zname;
zval **params[2];
fci.param_count = 2;
fci.params = params;
fci.params[0] = &zname_ptr;
fci.params[1] = &php_value;
fci.object_ptr = object;
fci.no_separation = 1;
zend_call_function(&fci, NULL TSRMLS_CC);
ret_value = zval_to_v8js(php_ret_value, isolate TSRMLS_CC);
zval_ptr_dtor(&php_ret_value);
}
}
// if PHP wanted to hold on to this value, update_property would
// have bumped the refcount
zval_ptr_dtor(&php_value);
} else if (callback_type == V8JS_PROP_QUERY ||
callback_type == V8JS_PROP_DELETER) {
const zend_object_handlers *h = Z_OBJ_HT_P(object);
zval *prop;
MAKE_STD_ZVAL(prop);
ZVAL_STRINGL(prop, name, name_len, 1);
if (callback_type == V8JS_PROP_QUERY) {
if (h->has_property(object, prop, 0 ZEND_HASH_KEY_NULL TSRMLS_CC)) {
ret_value = V8JS_UINT(v8::None);
} else {
ret_value = v8::Handle<v8::Value>(); // empty handle
}
} else {
zend_property_info *property_info = zend_get_property_info(ce, prop, 1 TSRMLS_CC);
if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
h->unset_property(object, prop ZEND_HASH_KEY_NULL TSRMLS_CC);
ret_value = V8JS_BOOL(true);
}
else {
ret_value = v8::Handle<v8::Value>(); // empty handle
}
}
zval_ptr_dtor(&prop);
} else {
/* shouldn't reach here! but bail safely */
ret_value = v8::Handle<v8::Value>();
}
}
efree(lower);
return ret_value;
}
/* }}} */
static void php_v8js_named_property_getter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
{
info.GetReturnValue().Set(php_v8js_named_property_callback(property, info, V8JS_PROP_GETTER));
}
/* }}} */
static void php_v8js_named_property_setter(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
{
info.GetReturnValue().Set(php_v8js_named_property_callback(property, info, V8JS_PROP_SETTER, value));
}
/* }}} */
static void php_v8js_named_property_query(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Integer> &info) /* {{{ */
{
v8::Local<v8::Value> r = php_v8js_named_property_callback(property, info, V8JS_PROP_QUERY);
if (!r.IsEmpty()) {
info.GetReturnValue().Set(r->ToInteger());
}
}
/* }}} */
static void php_v8js_named_property_deleter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Boolean> &info) /* {{{ */
{
v8::Local<v8::Value> r = php_v8js_named_property_callback(property, info, V8JS_PROP_DELETER);
if (!r.IsEmpty()) {
info.GetReturnValue().Set(r->ToBoolean());
}
}
/* }}} */
static v8::Handle<v8::Object> php_v8js_wrap_object(v8::Isolate *isolate, zend_class_entry *ce, zval *value TSRMLS_DC) /* {{{ */
{
php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
v8::Local<v8::FunctionTemplate> new_tpl;
v8js_tmpl_t *persist_tpl_;
try {
new_tpl = v8::Local<v8::FunctionTemplate>::New
(isolate, ctx->template_cache.at(ce->name));
}
catch (const std::out_of_range &) {
/* No cached v8::FunctionTemplate available as of yet, create one. */
new_tpl = v8::FunctionTemplate::New(isolate, 0);
new_tpl->SetClassName(V8JS_STRL(ce->name, ce->name_length));
new_tpl->InstanceTemplate()->SetInternalFieldCount(1);
if (ce == zend_ce_closure) {
/* Got a closure, mustn't cache ... */
persist_tpl_ = new v8js_tmpl_t(isolate, new_tpl);
/* We'll free persist_tpl_ via php_v8js_weak_closure_callback, below */
new_tpl->InstanceTemplate()->SetCallAsFunctionHandler(php_v8js_php_callback);
} else {
/* Add new v8::FunctionTemplate to tpl_map, as long as it is not a closure. */
persist_tpl_ = &ctx->template_cache[ce->name];
persist_tpl_->Reset(isolate, new_tpl);
/* We'll free persist_tpl_ when template_cache is destroyed */
v8::Local<v8::ObjectTemplate> inst_tpl = new_tpl->InstanceTemplate();
v8::NamedPropertyGetterCallback getter = php_v8js_named_property_getter;
v8::NamedPropertyEnumeratorCallback enumerator = php_v8js_named_property_enumerator;
/* Check for ArrayAccess object */
if (V8JSG(use_array_access) && ce) {
bool has_array_access = false;
bool has_countable = false;
for (int i = 0; i < ce->num_interfaces; i ++) {
if (strcmp (ce->interfaces[i]->name, "ArrayAccess") == 0) {
has_array_access = true;
}
else if (strcmp (ce->interfaces[i]->name, "Countable") == 0) {
has_countable = true;
}
}
if(has_array_access && has_countable) {
inst_tpl->SetIndexedPropertyHandler(php_v8js_array_access_getter,
php_v8js_array_access_setter,
php_v8js_array_access_query,
php_v8js_array_access_deleter,
php_v8js_array_access_enumerator);
/* Switch to special ArrayAccess getter, which falls back to
* php_v8js_named_property_getter, but possibly bridges the
* call to Array.prototype functions. */
getter = php_v8js_array_access_named_getter;
/* Overwrite enumerator, since for(... in ...) loop should
* not see the methods but iterate over the elements. */
enumerator = 0;
}
}
// Finish setup of new_tpl
inst_tpl->SetNamedPropertyHandler
(getter, /* getter */
php_v8js_named_property_setter, /* setter */
php_v8js_named_property_query, /* query */
php_v8js_named_property_deleter, /* deleter */
enumerator, /* enumerator */
V8JS_NULL /* data */
);
// add __invoke() handler
zend_function *invoke_method_ptr;
if (zend_hash_find(&ce->function_table, ZEND_INVOKE_FUNC_NAME,
sizeof(ZEND_INVOKE_FUNC_NAME),
(void**)&invoke_method_ptr) == SUCCESS &&
invoke_method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) {
new_tpl->InstanceTemplate()->SetCallAsFunctionHandler(php_v8js_invoke_callback, PHP_V8JS_CALLBACK(isolate, invoke_method_ptr, new_tpl));
}
}
v8::Local<v8::Array> call_handler_data = v8::Array::New(isolate, 2);
call_handler_data->Set(0, v8::External::New(isolate, persist_tpl_));
call_handler_data->Set(1, v8::External::New(isolate, ce));
new_tpl->SetCallHandler(php_v8js_construct_callback, call_handler_data);
}
// Create v8 wrapper object
v8::Handle<v8::Value> external = v8::External::New(isolate, value);
v8::Handle<v8::Object> newobj = new_tpl->GetFunction()->NewInstance(1, &external);
if (ce == zend_ce_closure) {
// free uncached function template when object is freed
ctx->weak_closures[persist_tpl_].Reset(isolate, newobj);
ctx->weak_closures[persist_tpl_].SetWeak(persist_tpl_, php_v8js_weak_closure_callback);
}
return newobj;
}
/* }}} */
static v8::Handle<v8::Object> php_v8js_wrap_array_to_object(v8::Isolate *isolate, zval *value TSRMLS_DC) /* {{{ */
{
int i;
HashPosition pos;
char *key = NULL;
uint key_len;
ulong index;
// @todo re-use template likewise
v8::Local<v8::FunctionTemplate> new_tpl = v8::FunctionTemplate::New(isolate, 0);
/* Call it Array, but it is not a native array, especially it doesn't have
* have the typical Array.prototype functions. */
new_tpl->SetClassName(V8JS_SYM("Array"));
v8::Handle<v8::Object> newobj = new_tpl->InstanceTemplate()->NewInstance();
HashTable *myht = HASH_OF(value);
i = myht ? zend_hash_num_elements(myht) : 0;
if (i > 0)
{
zval **data;
HashTable *tmp_ht;
zend_hash_internal_pointer_reset_ex(myht, &pos);
for (;; zend_hash_move_forward_ex(myht, &pos)) {
i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
if (i == HASH_KEY_NON_EXISTANT)
break;
if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS)
{
tmp_ht = HASH_OF(*data);
if (tmp_ht) {
tmp_ht->nApplyCount++;
}
if (i == HASH_KEY_IS_STRING)
{
if (key[0] == '\0' && Z_TYPE_P(value) == IS_OBJECT) {
/* Skip protected and private members. */
if (tmp_ht) {
tmp_ht->nApplyCount--;
}
continue;
}
newobj->Set(V8JS_STRL(key, key_len - 1), zval_to_v8js(*data, isolate TSRMLS_CC));
} else {
newobj->Set(index, zval_to_v8js(*data, isolate TSRMLS_CC));
}
if (tmp_ht) {
tmp_ht->nApplyCount--;
}
}
}
}
return newobj;
}
/* }}} */
v8::Handle<v8::Value> php_v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
{
HashTable *myht;
zend_class_entry *ce = NULL;
if (Z_TYPE_P(value) == IS_ARRAY) {
myht = HASH_OF(value);
} else {
myht = Z_OBJPROP_P(value);
ce = Z_OBJCE_P(value);
}
/* Prevent recursion */
if (myht && myht->nApplyCount > 1) {
return V8JS_NULL;
}
/* Special case, passing back object originating from JS to JS */
if (ce == php_ce_v8_function) {
php_v8js_object *c = (php_v8js_object *) zend_object_store_get_object(value TSRMLS_CC);
if(isolate != c->ctx->isolate) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "V8Function object passed to wrong V8Js instance");
return V8JS_NULL;
}
return v8::Local<v8::Value>::New(isolate, c->v8obj);
}
/* If it's a PHP object, wrap it */
if (ce) {
return php_v8js_wrap_object(isolate, ce, value TSRMLS_CC);
}
/* Associative PHP arrays cannot be wrapped to JS arrays, convert them to
* JS objects and attach all their array keys as properties. */
return php_v8js_wrap_array_to_object(isolate, value TSRMLS_CC);
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/

33
v8js_object_export.h Normal file
View File

@ -0,0 +1,33 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| http://www.opensource.org/licenses/mit-license.php MIT License |
+----------------------------------------------------------------------+
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
| Author: Patrick Reilly <preilly@php.net> |
+----------------------------------------------------------------------+
*/
#ifndef V8JS_OBJECT_EXPORT_H
#define V8JS_OBJECT_EXPORT_H
v8::Handle<v8::Value> php_v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate TSRMLS_DC);
typedef enum {
V8JS_PROP_GETTER,
V8JS_PROP_SETTER,
V8JS_PROP_QUERY,
V8JS_PROP_DELETER
} property_op_t;
template<typename T>
v8::Local<v8::Value> php_v8js_named_property_callback(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<T> &info,
property_op_t callback_type,
v8::Local<v8::Value> set_value = v8::Local<v8::Value>());
#endif /* V8JS_OBJECT_EXPORT_H */