-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv8_v8context.cpp
More file actions
320 lines (236 loc) · 8.79 KB
/
v8_v8context.cpp
File metadata and controls
320 lines (236 loc) · 8.79 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
/*
+----------------------------------------------------------------------+
| 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_v8context.h"
#include "v8_conv.h"
#include <iostream>
#include <fstream>
using namespace v8;
v8context::v8context()
{
HandleScope handle_scope;
Local<ObjectTemplate> global = ObjectTemplate::New();
context = Context::New(NULL, global);
}
v8context::~v8context()
{
std::for_each(fcall_list.begin(), fcall_list.end(), del_fcall_info());
std::for_each(ze_obj_list.begin(), ze_obj_list.end(), del_ze_obj());
context.Dispose();
}
Handle<Value> v8context::run(const char* src, const int len)
{
HandleScope handle_scope;
Context::Scope context_scope(context);
TryCatch trycatch;
Handle<Script> script = Script::Compile(String::New(src, len));
if (script.IsEmpty()) {
return handle_scope.Close(trycatch.Exception()->ToString());
}
Local<Value> result = script->Run();
if (result.IsEmpty()) {
String::AsciiValue exception(trycatch.Exception());
return handle_scope.Close(trycatch.Exception()->ToString());
}
return handle_scope.Close(result);
}
void v8context::set(const char* name, const int name_len, Handle<Value> value)
{
HandleScope handle_scope;
Context::Scope context_scope(context);
context->Global()->Set(String::New(name, name_len), value);
}
Handle<Value> v8context::get(const char* name, const int name_len) const
{
HandleScope handle_scope;
Context::Scope context_scope(context);
return handle_scope.Close(context->Global()->Get(String::New(name, name_len)));
}
bool v8context::has(const char* name, const int name_len) const
{
HandleScope handle_scope;
Context::Scope context_scope(context);
return context->Global()->Has(String::New(name, name_len));
}
bool v8context::unset(const char* name, const int name_len)
{
HandleScope handle_scope;
Context::Scope context_scope(context);
Local<Value> prop = context->Global()->Get(String::New(name, name_len));
if (prop.IsEmpty() || !prop->IsFunction()) {
return false;
}
Local<Function> func = Local<Function>::Cast(prop);
Local<Value> val = func->GetHiddenValue(String::New("p"));
if (!val.IsEmpty()) {
Handle<External> field = Handle<External>::Cast(val);
fcall_info* fc = static_cast<fcall_info*>(field->Value());
fcall_list_t::iterator it = std::find(fcall_list.begin(), fcall_list.end(), fc);
if (it != fcall_list.end()) {
fcall_list.erase(it);
del_fcall_info()(fc);
context->Global()->ForceDelete(String::New(name, name_len));
return true;
}
}
return false;
}
const Persistent<Context>& v8context::get_context() const
{
return context;
}
void v8context::register_ze_obj(Handle<Object> obj, zval* val TSRMLS_DC)
{
ze_obj* zeo = 0;
ze_obj_list_t::iterator it = std::find_if(ze_obj_list.begin(), ze_obj_list.end(), ze_obj_zval_pred(val));
if (it == ze_obj_list.end()) {
Z_ADDREF_P(val);
zeo = new ze_obj(Z_OBJCE_P(val), this, val);
ze_obj_list.push_back(zeo);
}
else {
zeo = *it;
}
obj->SetInternalField(0, External::New(zeo));
}
void v8context::register_func(const char* name, const int name_len, const zend_fcall_info& fci, const zend_fcall_info_cache& fcc)
{
HandleScope handle_scope;
Context::Scope context_scope(context);
fcall_info* fc = new fcall_info(fci, fcc, this);
fcall_list.push_back(fc);
Handle<FunctionTemplate> temp = FunctionTemplate::New(func_callback);
Local<Function> func = temp->GetFunction();
func->SetHiddenValue(String::New("p"), External::New(fc));
context->Global()->Set(String::New(name, name_len), func);
}
/*Handle<Value> interceptor_get(Local<String> name, const AccessorInfo& args)
{
php_printf("INTER_GET\n");
return Null();
}
Handle<Value> interceptor_set(Local<String> name, Local<Value> value, const AccessorInfo& args)
{
php_printf("INTER_SET\n");
return Null();
}
*/
void v8context::register_class(const char* name, const int name_len, zend_class_entry* ce)
{
HandleScope handle_scope;
Context::Scope context_scope(context);
reg_class_pack* pack = new reg_class_pack(ce, this);
Local<FunctionTemplate> func = FunctionTemplate::New(&ctor_callback, External::New(static_cast<void*>(pack)));
func->SetClassName(String::New(name, name_len));
Local<ObjectTemplate> obj_tpl = func->InstanceTemplate();
obj_tpl->SetNamedPropertyHandler(&interceptor_get, interceptor_set); // see the rest of args!
obj_tpl->SetInternalFieldCount(1);
context->Global()->Set(v8::String::New(name, name_len), func->GetFunction());
}
Handle<Value> v8context::ctor_callback(const Arguments& args)
{
TSRMLS_FETCH();
HandleScope handle_scope;
reg_class_pack* pack = static_cast<reg_class_pack*>(Handle<External>::Cast(args.Data())->Value());
v8context* self = pack->context;
zend_class_entry* ce = pack->ce;
zval* obj;
MAKE_STD_ZVAL(obj);
//if (ce->constructor) {
//}
//else {
object_init_ex(obj, ce);
//}
//Z_ADDREF_P(obj); // ???
ze_obj* zeo = new ze_obj(ce, self, obj);
self->ze_obj_list.push_back(zeo);
args.This()->SetInternalField(0, External::New(static_cast<void*>(zeo)));
delete pack;
return args.This();
}
Handle<Value> v8context::getter(Local<String> name, const AccessorInfo& args, ze_obj* zeo)
{
TSRMLS_FETCH();
Handle<Object> obj = args.This();
if (obj->HasRealNamedProperty(name)) {
return obj->GetRealNamedProperty(name);
}
String::AsciiValue prop(name);
zval* val = zend_read_property(zeo->ce, zeo->obj, *prop, prop.length(), 1 TSRMLS_CC);
if (val) {
Handle<Value> jsval = to_jsval(this, val TSRMLS_CC);
zval_ptr_dtor(&val);
return jsval;
}
return Null();
}
Handle<Value> v8context::setter(Local<String> name, Local<Value> value, const AccessorInfo& args, ze_obj* zeo)
{
TSRMLS_FETCH();
Handle<Object> obj = args.This();
zval* data;
MAKE_STD_ZVAL(data);
to_zval(data, value TSRMLS_CC);
String::AsciiValue ascii(name);
PHPWRITE(*ascii, ascii.length());
zend_update_property(zeo->ce, zeo->obj, *ascii, ascii.length(), data TSRMLS_CC);
//args.This()->GetRealNamedProperty(String::New("test"));
/////////args.This()->Set(name, value);
/*zval* val = zend_read_property(zeo->ce, zeo->obj, *prop, prop.length(), 1 TSRMLS_CC);
if (val) {
v8::Handle<v8::Value> jsval = to_jsval(this, val TSRMLS_CC);
zval_ptr_dtor(&val);
return jsval;
}
php_printf("RETURN_NULL\n");
*/
return value; //Null();
}
Handle<Value> v8context::func_callback(const Arguments& args)
{
TSRMLS_FETCH();
HandleScope handle_scope;
Handle<External> field = Handle<External>::Cast(args.Callee()->GetHiddenValue(String::New("p")));
fcall_info* fc = static_cast<fcall_info*>(field->Value());
v8context* self = fc->context;
Context::Scope context_scope(self->context);
int argc = args.Length();
zval*** params = (zval***)emalloc(argc * sizeof(zval**));
for (int i = 0; i < argc; ++i) {
zval** val = (zval**)emalloc(sizeof(zval*));
MAKE_STD_ZVAL(*val);
to_zval(*val, args[i] TSRMLS_CC);
params[i] = val;
}
zval* retval = 0;
fc->fci.params = params;
fc->fci.param_count = argc;
fc->fci.retval_ptr_ptr = &retval;
zend_call_function(&fc->fci, NULL TSRMLS_CC);
for (int i = 0; i < argc; ++i) {
zval** val = params[i];
zval_ptr_dtor(val);
efree(val);
}
efree(params);
if (retval != NULL) {
Handle<Value> val = to_jsval(self, retval TSRMLS_CC);
zval_ptr_dtor(&retval);
return handle_scope.Close(val);
}
return Null();
}