-
Notifications
You must be signed in to change notification settings - Fork 200
/
v8js_array_access.cc
276 lines (211 loc) · 7.95 KB
/
v8js_array_access.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2017 The PHP Group |
+----------------------------------------------------------------------+
| http://www.opensource.org/licenses/mit-license.php MIT License |
+----------------------------------------------------------------------+
| Author: Stefan Siegl <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php_v8js_macros.h"
#include "v8js_array_access.h"
#include "v8js_exceptions.h"
#include "v8js_object_export.h"
extern "C" {
#include "php.h"
#include "ext/date/php_date.h"
#include "ext/standard/php_string.h"
#include "zend_interfaces.h"
#include "zend_closures.h"
#include "zend_exceptions.h"
}
static zval v8js_array_access_dispatch(zend_object *object, const char *method_name, int param_count,
uint32_t index, zval zvalue) /* {{{ */
{
zend_fcall_info fci;
zval php_value;
fci.size = sizeof(fci);
ZVAL_STRING(&fci.function_name, method_name);
fci.retval = &php_value;
zval params[2];
ZVAL_LONG(¶ms[0], index);
params[1] = zvalue;
fci.param_count = param_count;
fci.params = params;
fci.object = object;
fci.named_params = NULL;
zend_call_function(&fci, NULL);
zval_dtor(&fci.function_name);
return php_value;
}
/* }}} */
V8JS_INTERCEPTED 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();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
zval zvalue;
ZVAL_UNDEF(&zvalue);
zval php_value = v8js_array_access_dispatch(object, "offsetGet", 1, index, zvalue);
v8::Local<v8::Value> ret_value = zval_to_v8js(&php_value, isolate);
zval_ptr_dtor(&php_value);
if (ret_value.IsEmpty()) {
return V8JS_INTERCEPTED_NO;
} else {
info.GetReturnValue().Set(ret_value);
return V8JS_INTERCEPTED_YES;
}
}
/* }}} */
V8JS_INTERCEPTED v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
const V8JS_SETTER_PROPERTY_CALLBACK_INFO &info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
zval zvalue;
ZVAL_UNDEF(&zvalue);
if (v8js_to_zval(value, &zvalue, 0, isolate) != SUCCESS) {
return V8JS_INTERCEPTED_NO;
}
zval php_value = v8js_array_access_dispatch(object, "offsetSet", 2, index, zvalue);
zval_ptr_dtor(&php_value);
#if !PHP_V8_HAS_INTERCEPTED
/* simply pass back the value to tell we intercepted the call
* as the offsetSet function returns void. */
info.GetReturnValue().Set(value);
#endif
/* if PHP wanted to hold on to this value, zend_call_function would
* have bumped the refcount. */
zval_ptr_dtor(&zvalue);
return V8JS_INTERCEPTED_YES;
}
/* }}} */
static int v8js_array_access_get_count_result(zend_object *object) /* {{{ */
{
zval zvalue;
ZVAL_UNDEF(&zvalue);
zval php_value = v8js_array_access_dispatch(object, "count", 0, 0, zvalue);
if(Z_TYPE(php_value) != IS_LONG) {
php_error_docref(NULL, E_WARNING, "Non-numeric return value from count() method");
zval_ptr_dtor(&php_value);
return 0;
}
zend_long result = Z_LVAL(php_value);
if (result > std::numeric_limits<int>::max()) {
zend_throw_exception(php_ce_v8js_exception,
"Array size/offset exceeds maximum supported length", 0);
return 0;
}
return static_cast<int>(result);
}
/* }}} */
static bool v8js_array_access_isset_p(zend_object *object, int index) /* {{{ */
{
zval zvalue;
ZVAL_UNDEF(&zvalue);
zval php_value = v8js_array_access_dispatch(object, "offsetExists", 1, index, zvalue);
if(Z_TYPE(php_value) != IS_TRUE && Z_TYPE(php_value) != IS_FALSE) {
php_error_docref(NULL, E_WARNING, "Non-boolean return value from offsetExists() method");
zval_ptr_dtor(&php_value);
return false;
}
return Z_TYPE(php_value) == IS_TRUE;
}
/* }}} */
static void 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();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
int length = v8js_array_access_get_count_result(object);
info.GetReturnValue().Set(V8JS_INT(length));
}
/* }}} */
V8JS_INTERCEPTED v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
zval zvalue;
ZVAL_UNDEF(&zvalue);
zval php_value = v8js_array_access_dispatch(object, "offsetUnset", 1, index, zvalue);
zval_ptr_dtor(&php_value);
info.GetReturnValue().Set(V8JS_BOOL(true));
return V8JS_INTERCEPTED_YES;
}
/* }}} */
V8JS_INTERCEPTED 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();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
/* If index is set, then return an integer encoding a v8::PropertyAttribute;
* otherwise we're expected to return an empty handle. */
if(v8js_array_access_isset_p(object, index)) {
info.GetReturnValue().Set(V8JS_UINT(v8::PropertyAttribute::None));
return V8JS_INTERCEPTED_YES;
}
return V8JS_INTERCEPTED_NO;
}
/* }}} */
void v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& info) /* {{{ */
{
v8::Isolate *isolate = info.GetIsolate();
v8::Local<v8::Object> self = info.Holder();
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
int length = v8js_array_access_get_count_result(object);
v8::Local<v8::Array> result = v8::Array::New(isolate, length);
int i = 0;
for(int j = 0; j < length; j ++) {
if(v8js_array_access_isset_p(object, j)) {
result->Set(isolate->GetEnteredOrMicrotaskContext(), i ++, V8JS_INT(j));
}
}
result->Set(isolate->GetEnteredOrMicrotaskContext(), V8JS_SYM("length"), V8JS_INT(i));
info.GetReturnValue().Set(result);
}
/* }}} */
V8JS_INTERCEPTED v8js_array_access_named_getter(v8::Local<v8::Name> property_name, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
{
v8::Local<v8::String> property = v8::Local<v8::String>::Cast(property_name);
v8::Isolate *isolate = info.GetIsolate();
v8::String::Utf8Value cstr(isolate, property);
const char *name = ToCString(cstr);
if(strcmp(name, "length") == 0) {
v8js_array_access_length(property, info);
return V8JS_INTERCEPTED_YES;
}
v8::Local<v8::Value> ret_value = v8js_named_property_callback(info.GetIsolate(), info.Holder(), property, V8JS_PROP_GETTER);
if(ret_value.IsEmpty()) {
v8::Local<v8::Array> arr = v8::Array::New(isolate);
v8::Local<v8::Value> prototype = arr->GetPrototype();
if(!prototype->IsObject()) {
/* ehh? Array.prototype not an object? strange, stop. */
info.GetReturnValue().Set(ret_value);
}
v8::Local<v8::Object> prototype_object;
if(!prototype->ToObject(isolate->GetEnteredOrMicrotaskContext()).ToLocal(&prototype_object)) {
/* ehh? Array.prototype not an object? strange, stop. */
info.GetReturnValue().Set(ret_value);
}
prototype_object->Get(isolate->GetEnteredOrMicrotaskContext(), property).ToLocal(&ret_value);
}
info.GetReturnValue().Set(ret_value);
return V8JS_INTERCEPTED_YES;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/