-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv8.cpp
More file actions
362 lines (268 loc) · 9.49 KB
/
v8.cpp
File metadata and controls
362 lines (268 loc) · 9.49 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: metagoto <runpac314 gmail com> |
+----------------------------------------------------------------------+
*/
#include <v8.h>
#include "php_v8.h"
extern "C" {
#include "zend_closures.h"
}
#include "v8_v8context.h"
#include "v8_conv.h"
extern "C" {
#ifdef COMPILE_DL_V8
ZEND_GET_MODULE(v8)
#endif
} // extern "C"
static zend_class_entry* v8context_ce;
static zend_object_handlers v8context_object_handlers;
struct v8option
{
enum attribute_type {
ATTR_ERRMODE
};
enum error_mode_type {
ERRMODE_SILENT,
ERRMODE_WARNING,
ERRMODE_EXCEPTION
};
enum php_type {
PHP_VALUE,
PHP_FUNCTION,
PHP_CLASS
};
};
struct v8context_object {
zend_object std;
v8context* intern;
};
static void v8_object_dtor(void* object, zend_object_handle handle TSRMLS_DC)
{
v8context_object* obj = (v8context_object*)object;
delete obj->intern;
zend_object_std_dtor(&obj->std TSRMLS_CC);
efree(obj);
}
zend_object_value v8_create_object(zend_class_entry* class_type TSRMLS_DC)
{
zend_object_value retval;
v8context_object* obj = (v8context_object*)emalloc(sizeof(v8context_object));
memset(obj, 0, sizeof(v8context_object));
zend_object_std_init(&obj->std, class_type TSRMLS_CC);
zend_hash_copy(obj->std.properties, &class_type->default_properties
,reinterpret_cast<copy_ctor_func_t>(zval_add_ref), static_cast<void*>(0), sizeof(zval*));
obj->intern = new v8context;
retval.handle = zend_objects_store_put(obj, reinterpret_cast<zend_objects_store_dtor_t>(zend_objects_destroy_object)
, reinterpret_cast<zend_objects_free_object_storage_t>(v8_object_dtor)
, NULL TSRMLS_CC);
retval.handlers = &v8context_object_handlers;
return retval;
}
PHP_METHOD(V8Context, __construct)
{
}
PHP_METHOD(V8Context, run)
{
char* src = NULL;
int src_len = 0;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &src, &src_len)) {
return;
}
v8context_object* obj = (v8context_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
v8context* v8c = obj->intern;
v8::HandleScope handle_scope;
v8::Context::Scope context_scope(v8c->get_context());
v8::Handle<v8::Value> result = v8c->run(src, src_len);
to_zval(return_value, result TSRMLS_CC);
}
PHP_METHOD(V8Context, set)
{
char* name = NULL;
int name_len = 0;
zval* val = NULL;
long val_type = v8option::PHP_VALUE;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &name, &name_len, &val, &val_type)
|| !name_len) {
RETURN_FALSE;
}
v8context_object* obj = (v8context_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
v8context* v8c = obj->intern;
v8::HandleScope handle_scope;
v8::Context::Scope context_scope(v8c->get_context());
if (Z_TYPE_P(val) == IS_OBJECT && Z_OBJCE_P(val) == zend_ce_closure) {
val_type = v8option::PHP_FUNCTION;
}
switch (val_type) {
case v8option::PHP_VALUE: {
v8::Handle<v8::Value> jsval = to_jsval(v8c, val TSRMLS_CC);
v8c->set(name, name_len, jsval);
RETURN_TRUE;
}
case v8option::PHP_FUNCTION: {
zend_fcall_info fci;
zend_fcall_info_cache fcc;
if (SUCCESS == zend_fcall_info_init(val, 0, &fci, &fcc, NULL, NULL TSRMLS_CC)) {
if (fci.function_name) {
Z_ADDREF_P(fci.function_name);
}
v8c->register_func(name, name_len, fci, fcc);
RETURN_TRUE;
}
break;
}
case v8option::PHP_CLASS: {
//stub
}
default:
;
}
RETURN_FALSE; // throw, warn... ?
}
PHP_METHOD(V8Context, registerFunction)
{
zend_fcall_info fci;
zend_fcall_info_cache fcc;
char* name = NULL;
int name_len = 0;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f|s", &fci, &fcc, &name, &name_len)) {
RETURN_FALSE;
}
if (!name_len) {
name = Z_STRVAL_P(fci.function_name);
name_len = Z_STRLEN_P(fci.function_name);
}
v8context_object* obj = (v8context_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
v8context* v8c = obj->intern;
if (fci.function_name) {
Z_ADDREF_P(fci.function_name);
}
v8c->register_func(name, name_len, fci, fcc);
RETURN_TRUE;
}
PHP_METHOD(V8Context, registerClass)
{
char* name = NULL;
int name_len = 0;
char* js_name = NULL;
int js_name_len = 0;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &name, &name_len, &js_name, &js_name_len)
|| !name_len) {
RETURN_FALSE; // should throw if !name_len ?
}
zend_class_entry** ce = NULL;
if (zend_lookup_class(name, name_len, &ce TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
v8context_object* obj = (v8context_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
v8context* v8c = obj->intern;
if (js_name_len) {
v8c->register_class(js_name, js_name_len, *ce);
}
else {
v8c->register_class(name, name_len, *ce);
}
RETURN_TRUE;
}
PHP_METHOD(V8Context, get)
{
char* name = NULL;
int name_len = 0;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len)
|| !name_len) {
RETURN_FALSE;
}
v8context_object* obj = (v8context_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
v8context* v8c = obj->intern;
v8::HandleScope handle_scope;
v8::Context::Scope context_scope(v8c->get_context());
to_zval(return_value, v8c->get(name, name_len) TSRMLS_CC);
}
PHP_METHOD(V8Context, exists)
{
char* name = NULL;
int name_len = 0;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len)
|| !name_len) {
RETURN_FALSE;
}
v8context_object* obj = (v8context_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
v8context* v8c = obj->intern;
if (v8c->has(name, name_len)) {
RETURN_TRUE;
}
RETURN_FALSE;
}
PHP_METHOD(V8Context, unset)
{
char* name = NULL;
int name_len = 0;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len)
|| !name_len) {
RETURN_FALSE;
}
v8context_object* obj = (v8context_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
v8context* v8c = obj->intern;
if (v8c->unset(name, name_len)) {
RETURN_TRUE;
}
RETURN_FALSE;
}
zend_function_entry v8context_methods[] = {
PHP_ME(V8Context, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(V8Context, run, NULL, ZEND_ACC_PUBLIC)
PHP_ME(V8Context, set, NULL, ZEND_ACC_PUBLIC)
PHP_ME(V8Context, get, NULL, ZEND_ACC_PUBLIC)
PHP_ME(V8Context, exists, NULL, ZEND_ACC_PUBLIC)
PHP_ME(V8Context, unset, NULL, ZEND_ACC_PUBLIC)
PHP_ME(V8Context, registerFunction, NULL, ZEND_ACC_PUBLIC)
PHP_ME(V8Context, registerClass, NULL, ZEND_ACC_PUBLIC)
{ NULL, NULL, NULL }
};
/*const zend_function_entry splclassloader_functions[] = {
{NULL, NULL, NULL}
};*/
PHP_MINIT_FUNCTION(v8)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "V8Context", v8context_methods);
v8context_ce = zend_register_internal_class(&ce TSRMLS_CC);
v8context_ce->create_object = v8_create_object;
memcpy(&v8context_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
v8context_object_handlers.clone_obj = NULL; /* no cloning! */
zend_declare_class_constant_long(v8context_ce, "PHP_VALUE", sizeof("PHP_VALUE")-1, static_cast<long>(v8option::PHP_VALUE) TSRMLS_CC);
zend_declare_class_constant_long(v8context_ce, "PHP_FUNCTION",sizeof("PHP_FUNCTION")-1,static_cast<long>(v8option::PHP_FUNCTION) TSRMLS_CC);
zend_declare_class_constant_long(v8context_ce, "PHP_CLASS", sizeof("PHP_CLASS")-1, static_cast<long>(v8option::PHP_CLASS) TSRMLS_CC);
return SUCCESS;
}
PHP_MINFO_FUNCTION(v8)
{
php_info_print_table_start ();
php_info_print_table_header(2, "v8 support", "enabled");
php_info_print_table_row (2, "libv8 version", v8::V8::GetVersion());
php_info_print_table_end ();
}
zend_module_entry v8_module_entry = {
STANDARD_MODULE_HEADER,
"v8",
NULL, /*functions, */
PHP_MINIT(v8),
NULL,
NULL,
NULL,
PHP_MINFO(v8),
"0.1",
STANDARD_MODULE_PROPERTIES
};