-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinterface.c
300 lines (234 loc) · 7.14 KB
/
interface.c
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "duktape.h"
#include "php_phpjs.h"
PHPAPI zend_class_entry *phpjs_JSException_ptr;
PHPAPI zend_class_entry *phpjs_JS_ptr;
typedef struct {
zend_object zo;
duk_context * ctx;
} php_js_t;
ZEND_METHOD(JS, evaluate)
{
FETCH_THIS;
char * str;
int len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) == FAILURE)
return;
if (duk_peval_lstring(ctx, str, len) != 0) {
duk_php_throw(ctx, -1 TSRMLS_CC);
RETURN_FALSE;
}
duk_to_zval(&return_value, ctx, -1);
duk_pop(ctx);
}
// public function load($path)
ZEND_METHOD(JS, __construct)
{
FETCH_THIS_EX(0);
obj->ctx = duk_create_heap(NULL, NULL, NULL, getThis(), NULL);
duk_php_init(obj->ctx);
}
ZEND_METHOD(JS, load)
{
FETCH_THIS;
char * varname;
int varname_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &varname, &varname_len) == FAILURE) {
return;
}
if (duk_peval_file(ctx, varname) != 0) {
duk_php_throw(ctx, -1 TSRMLS_CC);
RETURN_FALSE;
}
duk_to_zval(&return_value, ctx, -1);
duk_pop(ctx);
}
ZEND_METHOD(JS, offsetUnset)
{
}
ZEND_METHOD(JS, offsetExists)
{
FETCH_THIS;
char * varname;
int varname_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &varname, &varname_len) == FAILURE)
return;
duk_push_global_object(ctx);
duk_get_prop_string(ctx, -1, varname);
if (duk_get_type_mask(ctx, -3) & DUK_TYPE_UNDEFINED) {
RETURN_FALSE;
} else {
RETURN_TRUE;
}
}
ZEND_METHOD(JS, offsetGet)
{
FETCH_THIS;
char * varname;
int varname_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &varname, &varname_len) == FAILURE)
return;
duk_push_global_object(ctx);
duk_get_prop_string(ctx, -1, varname);
duk_to_zval(&return_value, ctx, -1);
}
// public function __get($name)
ZEND_METHOD(JS, __get)
{
FETCH_THIS;
char * varname;
int varname_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &varname, &varname_len) == FAILURE)
return;
duk_push_global_object(ctx);
duk_get_prop_string(ctx, -1, varname);
duk_to_zval(&return_value, ctx, -1);
php_duk_free_return(ctx);
}
// public function __set($name, $value)
ZEND_METHOD(JS, __set)
{
FETCH_THIS;
zval* a_value;
char * name;
int len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name, &len, &a_value) == FAILURE)
return;
zval_to_duk(ctx, name, a_value);
}
ZEND_METHOD(JS, offsetSet)
{
FETCH_THIS;
zval* a_value;
char * name;
int len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name, &len, &a_value) == FAILURE)
return;
zval_to_duk(ctx, name, a_value);
}
// public function __call($name, $args)
ZEND_METHOD(JS, __call)
{
FETCH_THIS;
zval* a_args;
char * fnc;
int lfnc;
int ind;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &fnc, &lfnc, &a_args) == FAILURE)
return;
duk_push_global_object(ctx);
phpjs_php__call(ctx, fnc, a_args, return_value TSRMLS_CC);
}
ZEND_BEGIN_ARG_INFO_EX(ai_phpjs_JS_evaluate, 0, 0, 1)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(ai_phpjs_JS_offsetExists, 0, 0, 1)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(ai_phpjs_JS_offsetGet, 0, 0, 1)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(ai_phpjs_JS_offsetSet, 0, 0, 2)
ZEND_ARG_INFO(0, offset)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(ai_phpjs_JS_offsetUnset, 0, 0, 1)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(ai_phpjs_JS_load, 0, 0, 1)
ZEND_ARG_INFO(0, path)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(ai_phpjs_JS_export, 0, 0, 1)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(ai_phpjs_JS___get, 0, 0, 1)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(ai_phpjs_JS___set, 0, 0, 2)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(ai_phpjs_JS___call, 0, 0, 2)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, args)
ZEND_END_ARG_INFO()
static zend_function_entry phpjs_JS_functions[] = {
ZEND_ME(JS, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
ZEND_ME(JS, evaluate, ai_phpjs_JS_evaluate, ZEND_ACC_PUBLIC)
ZEND_ME(JS, load, ai_phpjs_JS_load, ZEND_ACC_PUBLIC)
ZEND_ME(JS, offsetExists, ai_phpjs_JS_offsetExists, ZEND_ACC_PUBLIC)
ZEND_ME(JS, offsetGet, ai_phpjs_JS_offsetGet, ZEND_ACC_PUBLIC)
ZEND_ME(JS, offsetSet, ai_phpjs_JS_offsetSet, ZEND_ACC_PUBLIC)
ZEND_ME(JS, offsetUnset, ai_phpjs_JS_offsetUnset, ZEND_ACC_PUBLIC)
ZEND_ME(JS, __get, ai_phpjs_JS___get, ZEND_ACC_PUBLIC)
ZEND_ME(JS, __set, ai_phpjs_JS___set, ZEND_ACC_PUBLIC)
ZEND_ME(JS, __call, ai_phpjs_JS___call, ZEND_ACC_PUBLIC)
ZEND_FE_END
};
static zend_function_entry phpjs_functions[] = {
{NULL, NULL, NULL}
};
static void php_js_free_storage(php_js_t *obj TSRMLS_DC)
{
zend_object_std_dtor(&obj->zo TSRMLS_CC);
if (obj->ctx) {
duk_destroy_heap(obj->ctx);
}
efree(obj);
}
zend_object_value phpjs_new_vm(zend_class_entry *ce TSRMLS_DC)
{
zend_object_value retval;
php_js_t * obj;
obj = (php_js_t *) emalloc(sizeof(php_js_t));
memset(obj, 0, sizeof(php_js_t));
zend_object_std_init(&obj->zo, ce TSRMLS_CC);
#if PHP_VERSION_ID < 50399
zval * tmp;
zend_hash_copy(obj->zo.properties, &ce->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
#else
object_properties_init((zend_object*) &(obj->zo), ce);
#endif
retval.handle = zend_objects_store_put(obj, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t)php_js_free_storage, NULL TSRMLS_CC);
retval.handlers = zend_get_std_object_handlers();
return retval;
}
PHP_MINIT_FUNCTION(phpjs)
{
zend_class_entry _ce, *_if;
zval* _val;
INIT_CLASS_ENTRY(_ce, "JSException", NULL);
phpjs_JSException_ptr = zend_register_internal_class_ex(&_ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
zend_declare_property_string(phpjs_JSException_ptr, _S("js_stack"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
INIT_CLASS_ENTRY(_ce, "JS", phpjs_JS_functions);
phpjs_JS_ptr = zend_register_internal_class(&_ce TSRMLS_CC);
phpjs_JS_ptr->create_object = phpjs_new_vm;
zend_do_implement_interface(phpjs_JS_ptr, zend_ce_arrayaccess TSRMLS_CC);
php_register_object_handler(TSRMLS_C);
php_register_function_handler(TSRMLS_C);
return SUCCESS;
}
PHP_MINFO_FUNCTION(phpjs)
{
php_info_print_table_start();
php_info_print_table_header(2, "phpjs", "enabled");
php_info_print_table_row(2, "Version", "1.0");
php_info_print_table_end();
}
zend_module_entry phpjs_module_entry = {
STANDARD_MODULE_HEADER,
"phpjs",
phpjs_functions,
PHP_MINIT(phpjs),
NULL,
NULL,
NULL,
PHP_MINFO(phpjs),
"1.0",
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_PHPJS
ZEND_GET_MODULE(phpjs)
#endif