Преглед изворни кода

Refactor PHP method call code

Stefan Siegl пре 10 година
родитељ
комит
dba4da9b8b
2 измењених фајлова са 30 додато и 123 уклоњено
  1. 30 121
      v8js_array_access.cc
  2. 0 2
      v8js_array_access.h

+ 30 - 121
v8js_array_access.cc

@@ -26,15 +26,10 @@ extern "C" {
 #include "v8js_array_access.h"
 #include "v8js_object_export.h"
 
-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());
+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. */
@@ -43,7 +38,7 @@ void php_v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo
 
 	zval fmember;
 	INIT_ZVAL(fmember);
-	ZVAL_STRING(&fmember, "offsetGet", 0);
+	ZVAL_STRING(&fmember, method_name, 0);
 
 	zval zindex;
 	INIT_ZVAL(zindex);
@@ -56,15 +51,32 @@ void php_v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo
 	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;
+	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);
 	v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
 	zval_ptr_dtor(&php_value);
 
@@ -82,19 +94,6 @@ void php_v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
 
 	v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
 	zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
-	zend_class_entry *ce = Z_OBJCE_P(object);
-
-	/* Okay, let's call offsetSet. */
-	zend_fcall_info fci;
-	zval *php_value;
-
-	zval fmember;
-	INIT_ZVAL(fmember);
-	ZVAL_STRING(&fmember, "offsetSet", 0);
-
-	zval zindex;
-	INIT_ZVAL(zindex);
-	ZVAL_LONG(&zindex, index);
 
 	zval *zvalue_ptr;
 	MAKE_STD_ZVAL(zvalue_ptr);
@@ -103,22 +102,7 @@ void php_v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
 		return;
 	}
 
-	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 = 2;
-	fci.params = params;
-
-	fci.object_ptr = object;
-	fci.no_separation = 0;
-
-	zend_call_function(&fci, NULL TSRMLS_CC);
+	zval *php_value = php_v8js_array_access_dispatch(object, "offsetSet", 2, index, zvalue_ptr);
 	zval_ptr_dtor(&php_value);
 
 	/* simply pass back the value to tell we intercepted the call
@@ -134,28 +118,7 @@ void php_v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
 
 static int php_v8js_array_access_get_count_result(zval *object TSRMLS_DC) /* {{{ */
 {
-	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);
+	zval *php_value = php_v8js_array_access_dispatch(object, "count", 0, 0, NULL);
 
 	if(Z_TYPE_P(php_value) != IS_LONG) {
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-numeric return value from count() method");
@@ -171,35 +134,7 @@ static int php_v8js_array_access_get_count_result(zval *object TSRMLS_DC) /* {{{
 
 static bool php_v8js_array_access_isset_p(zval *object, int index TSRMLS_DC) /* {{{ */
 {
-	zend_class_entry *ce = Z_OBJCE_P(object);
-
-	/* Okay, let's call offsetExists. */
-	zend_fcall_info fci;
-	zval *php_value;
-
-	zval fmember;
-	INIT_ZVAL(fmember);
-	ZVAL_STRING(&fmember, "offsetExists", 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);
+	zval *php_value = php_v8js_array_access_dispatch(object, "offsetExists", 1, index, NULL);
 
 	if(Z_TYPE_P(php_value) != IS_BOOL) {
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-boolean return value from offsetExists() method");
@@ -214,7 +149,7 @@ static bool php_v8js_array_access_isset_p(zval *object, int index TSRMLS_DC) /*
 /* }}} */
 
 
-void php_v8js_array_access_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
+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();
@@ -238,36 +173,10 @@ void php_v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInf
 
 	v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
 	zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
-	zend_class_entry *ce = Z_OBJCE_P(object);
 
-	/* Okay, let's call offsetUnset. */
-	zend_fcall_info fci;
-	zval *php_value;
-
-	zval fmember;
-	INIT_ZVAL(fmember);
-	ZVAL_STRING(&fmember, "offsetUnset", 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);
+	zval *php_value = php_v8js_array_access_dispatch(object, "offsetUnset", 1, index, NULL);
 	zval_ptr_dtor(&php_value);
+
 	info.GetReturnValue().Set(V8JS_BOOL(true));
 }
 /* }}} */

+ 0 - 2
v8js_array_access.h

@@ -18,8 +18,6 @@ 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_length(v8::Local<v8::String> property,
-				  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);