Skip to content

Commit dba4da9

Browse files
committed
Refactor PHP method call code
1 parent 9012689 commit dba4da9

File tree

2 files changed

+30
-123
lines changed

2 files changed

+30
-123
lines changed

v8js_array_access.cc

Lines changed: 30 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,10 @@ extern "C" {
2626
#include "v8js_array_access.h"
2727
#include "v8js_object_export.h"
2828

29-
void php_v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
30-
{
31-
v8::Isolate *isolate = info.GetIsolate();
32-
v8::Local<v8::Object> self = info.Holder();
3329

34-
V8JS_TSRMLS_FETCH();
35-
36-
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
37-
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
30+
static zval *php_v8js_array_access_dispatch(zval *object, const char *method_name, int param_count,
31+
uint32_t index, zval *zvalue_ptr TSRMLS_DC) /* {{{ */
32+
{
3833
zend_class_entry *ce = Z_OBJCE_P(object);
3934

4035
/* Okay, let's call offsetGet. */
@@ -43,7 +38,7 @@ void php_v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo
4338

4439
zval fmember;
4540
INIT_ZVAL(fmember);
46-
ZVAL_STRING(&fmember, "offsetGet", 0);
41+
ZVAL_STRING(&fmember, method_name, 0);
4742

4843
zval zindex;
4944
INIT_ZVAL(zindex);
@@ -56,15 +51,32 @@ void php_v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo
5651
fci.retval_ptr_ptr = &php_value;
5752

5853
zval *zindex_ptr = &zindex;
59-
zval **zindex_ptr_ptr = &zindex_ptr;
60-
fci.param_count = 1;
61-
fci.params = &zindex_ptr_ptr;
54+
zval **params[2] = { &zindex_ptr, &zvalue_ptr };
55+
56+
fci.param_count = param_count;
57+
fci.params = params;
6258

6359
fci.object_ptr = object;
6460
fci.no_separation = 0;
6561

6662
zend_call_function(&fci, NULL TSRMLS_CC);
63+
return php_value;
64+
}
65+
/* }}} */
66+
6767

68+
69+
void php_v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
70+
{
71+
v8::Isolate *isolate = info.GetIsolate();
72+
v8::Local<v8::Object> self = info.Holder();
73+
74+
V8JS_TSRMLS_FETCH();
75+
76+
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
77+
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
78+
79+
zval *php_value = php_v8js_array_access_dispatch(object, "offsetGet", 1, index, NULL);
6880
v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
6981
zval_ptr_dtor(&php_value);
7082

@@ -82,19 +94,6 @@ void php_v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
8294

8395
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
8496
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
85-
zend_class_entry *ce = Z_OBJCE_P(object);
86-
87-
/* Okay, let's call offsetSet. */
88-
zend_fcall_info fci;
89-
zval *php_value;
90-
91-
zval fmember;
92-
INIT_ZVAL(fmember);
93-
ZVAL_STRING(&fmember, "offsetSet", 0);
94-
95-
zval zindex;
96-
INIT_ZVAL(zindex);
97-
ZVAL_LONG(&zindex, index);
9897

9998
zval *zvalue_ptr;
10099
MAKE_STD_ZVAL(zvalue_ptr);
@@ -103,22 +102,7 @@ void php_v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
103102
return;
104103
}
105104

106-
fci.size = sizeof(fci);
107-
fci.function_table = &ce->function_table;
108-
fci.function_name = &fmember;
109-
fci.symbol_table = NULL;
110-
fci.retval_ptr_ptr = &php_value;
111-
112-
zval *zindex_ptr = &zindex;
113-
zval **params[2] = { &zindex_ptr, &zvalue_ptr };
114-
115-
fci.param_count = 2;
116-
fci.params = params;
117-
118-
fci.object_ptr = object;
119-
fci.no_separation = 0;
120-
121-
zend_call_function(&fci, NULL TSRMLS_CC);
105+
zval *php_value = php_v8js_array_access_dispatch(object, "offsetSet", 2, index, zvalue_ptr);
122106
zval_ptr_dtor(&php_value);
123107

124108
/* 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,
134118

135119
static int php_v8js_array_access_get_count_result(zval *object TSRMLS_DC) /* {{{ */
136120
{
137-
zend_class_entry *ce = Z_OBJCE_P(object);
138-
139-
zend_fcall_info fci;
140-
zval *php_value;
141-
142-
zval fmember;
143-
INIT_ZVAL(fmember);
144-
ZVAL_STRING(&fmember, "count", 0);
145-
146-
fci.size = sizeof(fci);
147-
fci.function_table = &ce->function_table;
148-
fci.function_name = &fmember;
149-
fci.symbol_table = NULL;
150-
fci.retval_ptr_ptr = &php_value;
151-
152-
fci.param_count = 0;
153-
fci.params = NULL;
154-
155-
fci.object_ptr = object;
156-
fci.no_separation = 0;
157-
158-
zend_call_function(&fci, NULL TSRMLS_CC);
121+
zval *php_value = php_v8js_array_access_dispatch(object, "count", 0, 0, NULL);
159122

160123
if(Z_TYPE_P(php_value) != IS_LONG) {
161124
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) /* {{{
171134

172135
static bool php_v8js_array_access_isset_p(zval *object, int index TSRMLS_DC) /* {{{ */
173136
{
174-
zend_class_entry *ce = Z_OBJCE_P(object);
175-
176-
/* Okay, let's call offsetExists. */
177-
zend_fcall_info fci;
178-
zval *php_value;
179-
180-
zval fmember;
181-
INIT_ZVAL(fmember);
182-
ZVAL_STRING(&fmember, "offsetExists", 0);
183-
184-
zval zindex;
185-
INIT_ZVAL(zindex);
186-
ZVAL_LONG(&zindex, index);
187-
188-
fci.size = sizeof(fci);
189-
fci.function_table = &ce->function_table;
190-
fci.function_name = &fmember;
191-
fci.symbol_table = NULL;
192-
fci.retval_ptr_ptr = &php_value;
193-
194-
zval *zindex_ptr = &zindex;
195-
zval **zindex_ptr_ptr = &zindex_ptr;
196-
fci.param_count = 1;
197-
fci.params = &zindex_ptr_ptr;
198-
199-
fci.object_ptr = object;
200-
fci.no_separation = 0;
201-
202-
zend_call_function(&fci, NULL TSRMLS_CC);
137+
zval *php_value = php_v8js_array_access_dispatch(object, "offsetExists", 1, index, NULL);
203138

204139
if(Z_TYPE_P(php_value) != IS_BOOL) {
205140
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) /*
214149
/* }}} */
215150

216151

217-
void php_v8js_array_access_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
152+
static void php_v8js_array_access_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
218153
{
219154
v8::Isolate *isolate = info.GetIsolate();
220155
v8::Local<v8::Object> self = info.Holder();
@@ -238,36 +173,10 @@ void php_v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInf
238173

239174
v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
240175
zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
241-
zend_class_entry *ce = Z_OBJCE_P(object);
242176

243-
/* Okay, let's call offsetUnset. */
244-
zend_fcall_info fci;
245-
zval *php_value;
246-
247-
zval fmember;
248-
INIT_ZVAL(fmember);
249-
ZVAL_STRING(&fmember, "offsetUnset", 0);
250-
251-
zval zindex;
252-
INIT_ZVAL(zindex);
253-
ZVAL_LONG(&zindex, index);
254-
255-
fci.size = sizeof(fci);
256-
fci.function_table = &ce->function_table;
257-
fci.function_name = &fmember;
258-
fci.symbol_table = NULL;
259-
fci.retval_ptr_ptr = &php_value;
260-
261-
zval *zindex_ptr = &zindex;
262-
zval **zindex_ptr_ptr = &zindex_ptr;
263-
fci.param_count = 1;
264-
fci.params = &zindex_ptr_ptr;
265-
266-
fci.object_ptr = object;
267-
fci.no_separation = 0;
268-
269-
zend_call_function(&fci, NULL TSRMLS_CC);
177+
zval *php_value = php_v8js_array_access_dispatch(object, "offsetUnset", 1, index, NULL);
270178
zval_ptr_dtor(&php_value);
179+
271180
info.GetReturnValue().Set(V8JS_BOOL(true));
272181
}
273182
/* }}} */

v8js_array_access.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ void php_v8js_array_access_getter(uint32_t index,
1818
const v8::PropertyCallbackInfo<v8::Value>& info);
1919
void php_v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
2020
const v8::PropertyCallbackInfo<v8::Value>& info);
21-
void php_v8js_array_access_length(v8::Local<v8::String> property,
22-
const v8::PropertyCallbackInfo<v8::Value>& info);
2321
void php_v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& info);
2422
void php_v8js_array_access_deleter(uint32_t index,
2523
const v8::PropertyCallbackInfo<v8::Boolean>& info);

0 commit comments

Comments
 (0)