Forráskód Böngészése

Add basic ArrayAccess wrapping

Stefan Siegl 10 éve
szülő
commit
f1dd5ad23b
4 módosított fájl, 191 hozzáadás és 0 törlés
  1. 1 0
      php_v8js_macros.h
  2. 54 0
      tests/array_access.phpt
  3. 18 0
      v8js.cc
  4. 118 0
      v8js_convert.cc

+ 1 - 0
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<php_v8js_timer_ctx *> timer_stack;

+ 54 - 0
tests/array_access.phpt

@@ -0,0 +1,54 @@
+--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]);
+EOJS;
+
+$v8 = new V8Js();
+$v8->myarr = (object) $myarr;
+$v8->executeString($js);
+
+?>
+===EOF===
+--EXPECT--
+int(20)
+int(14)
+string(11) "ArrayAccess"
+int(20)
+int(14)
+===EOF===

+ 18 - 0
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()
 /* }}} */
 

+ 118 - 0
v8js_convert.cc

@@ -866,6 +866,105 @@ static void php_v8js_named_property_deleter(v8::Local<v8::String> property, cons
 }
 /* }}} */
 
+static 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();
+
+	zval *object = reinterpret_cast<zval *>(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<v8::Value> 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<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
+{
+	v8::Isolate *isolate = info.GetIsolate();
+	v8::Local<v8::Object> self = info.Holder();
+
+	zval *object = reinterpret_cast<zval *>(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<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
+	zval_ptr_dtor(&php_value);
+
+	info.GetReturnValue().Set(ret_value);
+}
+/* }}} */
+
+
+static v8::Handle<v8::Value> php_v8js_array_access_to_jsobj(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
+{
+	v8::Local<v8::FunctionTemplate> aa_tpl = v8::FunctionTemplate::New(isolate, 0);
+	aa_tpl->SetClassName(V8JS_SYM("ArrayAccess"));
+
+	v8::Local<v8::ObjectTemplate> 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<v8::Object> newobj = inst_tpl->NewInstance();
+	newobj->SetAlignedPointerInInternalField(0, value);
+
+	return newobj;
+}
+/* }}} */
+
+
 static v8::Handle<v8::Value> php_v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
 {
 	v8::Handle<v8::Object> newobj;
@@ -889,6 +988,25 @@ static v8::Handle<v8::Value> 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);