diff --git a/php_v8js_macros.h b/php_v8js_macros.h index 87f15ef..f91a574 100644 --- a/php_v8js_macros.h +++ b/php_v8js_macros.h @@ -215,6 +215,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::stack timer_stack; diff --git a/tests/array_access.phpt b/tests/array_access.phpt new file mode 100644 index 0000000..75c0b91 --- /dev/null +++ b/tests/array_access.phpt @@ -0,0 +1,54 @@ +--TEST-- +Test V8::executeString() : Check ArrayAccess interface wrapping +--SKIPIF-- + +--INI-- +v8js.use_array_access = 1 +--FILE-- += 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 = <<myarr = (object) $myarr; +$v8->executeString($js); + +?> +===EOF=== +--EXPECT-- +int(20) +int(14) +string(11) "ArrayAccess" +int(20) +int(14) +===EOF=== diff --git a/v8js.cc b/v8js.cc index 6b22b0a..2777b63 100644 --- a/v8js.cc +++ b/v8js.cc @@ -79,9 +79,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() /* }}} */ diff --git a/v8js_convert.cc b/v8js_convert.cc index 76e5944..c5bea76 100644 --- a/v8js_convert.cc +++ b/v8js_convert.cc @@ -866,6 +866,105 @@ static void php_v8js_named_property_deleter(v8::Local property, cons } /* }}} */ +static void php_v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo& info) /* {{{ */ +{ + v8::Isolate *isolate = info.GetIsolate(); + v8::Local self = info.Holder(); + + zval *object = reinterpret_cast(self->GetAlignedPointerFromInternalField(0)); + 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, "offsetGet", 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 **zindex_ptr_ptr = &zindex_ptr; + fci.param_count = 1; + fci.params = &zindex_ptr_ptr; + + fci.object_ptr = object; + fci.no_separation = 0; + + zend_call_function(&fci, NULL TSRMLS_CC); + + v8::Local ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC); + zval_ptr_dtor(&php_value); + + info.GetReturnValue().Set(ret_value); +} +/* }}} */ + +static void php_v8js_array_access_length(v8::Local property, const v8::PropertyCallbackInfo& info) /* {{{ */ +{ + v8::Isolate *isolate = info.GetIsolate(); + v8::Local self = info.Holder(); + + zval *object = reinterpret_cast(self->GetAlignedPointerFromInternalField(0)); + zend_class_entry *ce = Z_OBJCE_P(object); + + zend_fcall_info fci; + zval *php_value; + + zval fmember; + INIT_ZVAL(fmember); + ZVAL_STRING(&fmember, "count", 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; + + fci.param_count = 0; + fci.params = NULL; + + fci.object_ptr = object; + fci.no_separation = 0; + + zend_call_function(&fci, NULL TSRMLS_CC); + + v8::Local ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC); + zval_ptr_dtor(&php_value); + + info.GetReturnValue().Set(ret_value); +} +/* }}} */ + + +static v8::Handle php_v8js_array_access_to_jsobj(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */ +{ + v8::Local aa_tpl = v8::FunctionTemplate::New(isolate, 0); + aa_tpl->SetClassName(V8JS_SYM("ArrayAccess")); + + v8::Local inst_tpl = aa_tpl->InstanceTemplate(); + inst_tpl->SetIndexedPropertyHandler(php_v8js_array_access_getter); + inst_tpl->SetAccessor(V8JS_STR("length"), php_v8js_array_access_length); + inst_tpl->SetInternalFieldCount(1); + + + v8::Handle newobj = inst_tpl->NewInstance(); + newobj->SetAlignedPointerInInternalField(0, value); + + return newobj; +} +/* }}} */ + + static v8::Handle php_v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */ { v8::Handle newobj; @@ -889,6 +988,25 @@ static v8::Handle php_v8js_hash_to_jsobj(zval *value, v8::Isolate *is return V8JS_NULL; } + /* 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) { + return php_v8js_array_access_to_jsobj(value, isolate TSRMLS_CC); + } + } + /* Object methods */ if (ce == php_ce_v8_function) { php_v8js_object *c = (php_v8js_object *) zend_object_store_get_object(value TSRMLS_CC);