From dba4da9b8bdcf418c6736338608f0aa55216fb9e Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sun, 30 Nov 2014 23:30:28 +0100 Subject: [PATCH] Refactor PHP method call code --- v8js_array_access.cc | 151 +++++++++---------------------------------- v8js_array_access.h | 2 - 2 files changed, 30 insertions(+), 123 deletions(-) diff --git a/v8js_array_access.cc b/v8js_array_access.cc index 35d7713..2f95f33 100644 --- a/v8js_array_access.cc +++ b/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& info) /* {{{ */ + +static zval *php_v8js_array_access_dispatch(zval *object, const char *method_name, int param_count, + uint32_t index, zval *zvalue_ptr TSRMLS_DC) /* {{{ */ { - v8::Isolate *isolate = info.GetIsolate(); - v8::Local self = info.Holder(); - - V8JS_TSRMLS_FETCH(); - - v8::Local php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)); - zval *object = reinterpret_cast(v8::External::Cast(*php_object)->Value()); 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& info) /* {{{ */ +{ + v8::Isolate *isolate = info.GetIsolate(); + v8::Local self = info.Holder(); + + V8JS_TSRMLS_FETCH(); + + v8::Local php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)); + zval *object = reinterpret_cast(v8::External::Cast(*php_object)->Value()); + + zval *php_value = php_v8js_array_access_dispatch(object, "offsetGet", 1, index, NULL); v8::Local 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 value, v8::Local php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)); zval *object = reinterpret_cast(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 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 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 property, const v8::PropertyCallbackInfo& info) /* {{{ */ +static void php_v8js_array_access_length(v8::Local property, const v8::PropertyCallbackInfo& info) /* {{{ */ { v8::Isolate *isolate = info.GetIsolate(); v8::Local self = info.Holder(); @@ -238,36 +173,10 @@ void php_v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInf v8::Local php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)); zval *object = reinterpret_cast(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)); } /* }}} */ diff --git a/v8js_array_access.h b/v8js_array_access.h index 0d3f36d..58bd63e 100644 --- a/v8js_array_access.h +++ b/v8js_array_access.h @@ -18,8 +18,6 @@ void php_v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo& info); void php_v8js_array_access_setter(uint32_t index, v8::Local value, const v8::PropertyCallbackInfo& info); -void php_v8js_array_access_length(v8::Local property, - const v8::PropertyCallbackInfo& info); void php_v8js_array_access_enumerator(const v8::PropertyCallbackInfo& info); void php_v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInfo& info);