瀏覽代碼

Implement isset behaviour on ArrayAccess objects

Stefan Siegl 10 年之前
父節點
當前提交
901268994a
共有 4 個文件被更改,包括 77 次插入1 次删除
  1. 56 0
      tests/array_access_008.phpt
  2. 18 0
      v8js_array_access.cc
  3. 2 0
      v8js_array_access.h
  4. 1 1
      v8js_object_export.cc

+ 56 - 0
tests/array_access_008.phpt

@@ -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===

+ 18 - 0
v8js_array_access.cc

@@ -272,6 +272,24 @@ void php_v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInf
 }
 /* }}} */
 
+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) /* {{{ */
 {

+ 2 - 0
v8js_array_access.h

@@ -23,6 +23,8 @@ void php_v8js_array_access_length(v8::Local<v8::String> property,
 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,

+ 1 - 1
v8js_object_export.cc

@@ -885,7 +885,7 @@ static v8::Handle<v8::Object> php_v8js_wrap_object(v8::Isolate *isolate, zend_cl
 				if(has_array_access && has_countable) {
 					inst_tpl->SetIndexedPropertyHandler(php_v8js_array_access_getter,
 														php_v8js_array_access_setter,
-														0, /* query */
+														php_v8js_array_access_query,
 														php_v8js_array_access_deleter,
 														php_v8js_array_access_enumerator);