-
Notifications
You must be signed in to change notification settings - Fork 200
/
v8js_convert.cc
296 lines (252 loc) · 7.34 KB
/
v8js_convert.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2017 The PHP Group |
+----------------------------------------------------------------------+
| http://www.opensource.org/licenses/mit-license.php MIT License |
+----------------------------------------------------------------------+
| Author: Jani Taskinen <[email protected]> |
| Author: Patrick Reilly <[email protected]> |
| Author: Stefan Siegl <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdexcept>
#include <limits>
#include "php_v8js_macros.h"
#include "v8js_exceptions.h"
#include "v8js_object_export.h"
#include "v8js_v8object_class.h"
#include "v8js_v8.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 int v8js_is_assoc_array(HashTable *myht) /* {{{ */
{
zend_string *key;
zend_ulong index, idx = 0;
ZEND_HASH_FOREACH_KEY(myht, index, key) {
if(key) {
// HASH_KEY_IS_STRING
return 1;
}
if(index != idx) {
return 1;
}
idx ++;
} ZEND_HASH_FOREACH_END();
return 0;
}
/* }}} */
static v8::Local<v8::Value> v8js_hash_to_jsarr(zval *value, v8::Isolate *isolate) /* {{{ */
{
HashTable *myht = HASH_OF(value);
int i = myht ? zend_hash_num_elements(myht) : 0;
/* Return object if dealing with assoc array */
if (i > 0 && v8js_is_assoc_array(myht)) {
return v8js_hash_to_jsobj(value, isolate);
}
v8::Local<v8::Array> newarr;
/* Prevent recursion */
if (myht && GC_IS_RECURSIVE(myht)) {
return V8JS_NULL;
}
v8::Local<v8::Context> v8_context = isolate->GetEnteredOrMicrotaskContext();
newarr = v8::Array::New(isolate, i);
if (i > 0)
{
zval *data;
zend_ulong index = 0;
if (myht && !(GC_FLAGS(myht) & GC_IMMUTABLE)) {
GC_PROTECT_RECURSION(myht);
}
ZEND_HASH_FOREACH_VAL(myht, data) {
newarr->Set(v8_context, index++, zval_to_v8js(data, isolate));
} ZEND_HASH_FOREACH_END();
if (myht && !(GC_FLAGS(myht) & GC_IMMUTABLE)) {
GC_UNPROTECT_RECURSION(myht);
}
}
return newarr;
}
/* }}} */
v8::Local<v8::Value> zend_long_to_v8js(zend_long v, v8::Isolate *isolate) /* {{{ */
{
if (v < std::numeric_limits<int32_t>::min() || v > std::numeric_limits<int32_t>::max()) {
return V8JS_FLOAT(static_cast<double>(v));
} else {
return V8JS_INT(static_cast<int32_t>(v));
}
}
/* }}} */
v8::Local<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate) /* {{{ */
{
v8::Local<v8::Value> jsValue;
zend_string *value_str;
zend_class_entry *ce;
switch (Z_TYPE_P(value))
{
case IS_INDIRECT:
jsValue = zval_to_v8js(Z_INDIRECT_P(value), isolate);
break;
case IS_REFERENCE:
jsValue = zval_to_v8js(Z_REFVAL_P(value), isolate);
break;
case IS_ARRAY:
jsValue = v8js_hash_to_jsarr(value, isolate);
break;
case IS_OBJECT:
if (V8JSG(use_date)) {
ce = php_date_get_date_ce();
if (instanceof_function(Z_OBJCE_P(value), ce)) {
zval dtval;
zend_call_method_with_0_params(Z_OBJ_P(value), NULL, NULL, "getTimestamp", &dtval);
v8::Date::New(isolate->GetEnteredOrMicrotaskContext(), ((double)Z_LVAL(dtval) * 1000.0)).ToLocal(&jsValue);
zval_dtor(&dtval);
} else
jsValue = v8js_hash_to_jsobj(value, isolate);
} else
jsValue = v8js_hash_to_jsobj(value, isolate);
break;
case IS_STRING:
value_str = Z_STR_P(value);
if (ZSTR_LEN(value_str) > std::numeric_limits<int>::max()) {
zend_throw_exception(php_ce_v8js_exception,
"String exceeds maximum string length", 0);
break;
}
jsValue = V8JS_ZSTR(value_str);
break;
case IS_LONG:
jsValue = zend_long_to_v8js(Z_LVAL_P(value), isolate);
break;
case IS_DOUBLE:
jsValue = V8JS_FLOAT(Z_DVAL_P(value));
break;
case IS_TRUE:
jsValue = V8JS_TRUE();
break;
case IS_FALSE:
jsValue = V8JS_FALSE();
break;
case IS_NULL:
jsValue = V8JS_NULL;
break;
case IS_UNDEF:
default:
/* undefined -> return v8::Value left empty */
jsValue = v8::Undefined(isolate);
break;
}
return jsValue;
}
/* }}} */
int v8js_to_zval(v8::Local<v8::Value> jsValue, zval *return_value, int flags, v8::Isolate *isolate) /* {{{ */
{
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, ctx->context);
if (jsValue->IsString())
{
v8::String::Utf8Value str(isolate, jsValue);
const char *cstr = ToCString(str);
RETVAL_STRINGL(cstr, str.length());
}
else if (jsValue->IsBoolean())
{
bool value = jsValue->BooleanValue(isolate);
RETVAL_BOOL(value);
}
else if (jsValue->IsInt32() || jsValue->IsUint32())
{
v8::Maybe<int64_t> value = jsValue->IntegerValue(v8_context);
if (value.IsNothing())
{
return FAILURE;
}
RETVAL_LONG((long) value.ToChecked());
}
else if (jsValue->IsNumber())
{
v8::Maybe<double> value = jsValue->NumberValue(v8_context);
if (value.IsNothing())
{
return FAILURE;
}
RETVAL_DOUBLE(value.ToChecked());
}
else if (jsValue->IsDate()) /* Return as a PHP DateTime object */
{
v8::String::Utf8Value str(isolate, jsValue);
const char *cstr = ToCString(str);
/* cstr has two timezone specifications:
*
* example from Linux:
* Mon Sep 08 1975 09:00:00 GMT+0000 (UTC)
*
* example from Windows:
* Mon Sep 08 1975 11:00:00 GMT+0200 (W. Europe Daylight Time)
*
* ... problem is, that PHP can't parse the second timezone
* specification as returned by v8 running on Windows. And as a
* matter of that fails due to inconsistent second timezone spec
*/
char *date_str = estrdup(cstr);
char *paren_ptr = strchr(date_str, '(');
if (paren_ptr != NULL) {
*paren_ptr = 0;
}
zend_class_entry *ce = php_date_get_date_ce();
php_date_instantiate(ce, return_value);
if (!php_date_initialize(Z_PHPDATE_P(return_value), date_str, strlen(date_str), NULL, NULL, 0)) {
efree(date_str);
return FAILURE;
}
efree(date_str);
}
else if (jsValue->IsObject())
{
v8::Local<v8::Object> self;
if (!jsValue->ToObject(v8_context).ToLocal(&self))
{
return FAILURE;
}
// if this is a wrapped PHP object, then just unwrap it.
if ((self->InternalFieldCount() == 2) && !jsValue->IsArrayBufferView() && !jsValue->IsArrayBuffer()) {
zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
zval zval_object;
ZVAL_OBJ(&zval_object, object);
RETVAL_ZVAL(&zval_object, 1, 0);
return SUCCESS;
}
if ((flags & V8JS_FLAG_FORCE_ARRAY && !jsValue->IsFunction()) || jsValue->IsArray()) {
array_init(return_value);
return v8js_get_properties_hash(jsValue, Z_ARRVAL_P(return_value), flags, isolate);
} else {
v8js_v8object_create(return_value, jsValue, flags, isolate);
return SUCCESS;
}
}
else /* types External, RegExp, Undefined and Null are considered NULL */
{
RETVAL_NULL();
}
return SUCCESS;
}
/* }}} */
/*
* 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
*/