forked from mschwartz/SilkJS
/
v8.cpp
60 lines (47 loc) · 1.63 KB
/
v8.cpp
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
#include "SilkJS.h"
struct ScriptWrapper {
Persistent<Script> script;
};
static JSVAL gc(const Arguments& args) {
HandleScope scope;
while (!V8::IdleNotification()) {}
return Undefined();
}
static JSVAL compileScript(JSARGS args) {
HandleScope scope;
Persistent<Context>context = Context::New(NULL, ObjectTemplate::New());
Context::Scope context_scope(context);
ScriptWrapper *wrapper = new ScriptWrapper;
wrapper->script = Persistent<Script>::New(Script::New(args[0]->ToString(), args[1]->ToString()));
context.Dispose();
return scope.Close(External::New(wrapper));
}
// runScript(context, script)
static JSVAL runScript(JSARGS args) {
HandleScope scope;
Local<External>wrap = Local<External>::Cast(args[0]);
ScriptWrapper *wrapper = (ScriptWrapper *)wrap->Value();
Persistent<Context>context = Context::New(NULL, ObjectTemplate::New());
Context::Scope context_scope(context);
Handle<Value>v = wrapper->script->Run();
context.Dispose();
JSVAL ret = scope.Close(v);
return ret;
}
static JSVAL freeScript(JSARGS args) {
HandleScope scope;
Local<External>wrap = Local<External>::Cast(args[0]);
ScriptWrapper *wrapper = (ScriptWrapper *)wrap->Value();
wrapper->script.Dispose();
delete wrapper;
return Undefined();
}
void init_v8_object() {
HandleScope scope;
Handle<ObjectTemplate>v8 = ObjectTemplate::New();
v8->Set(String::New("gc"), FunctionTemplate::New(gc));
v8->Set(String::New("compileScript"), FunctionTemplate::New(compileScript));
v8->Set(String::New("runScript"), FunctionTemplate::New(runScript));
v8->Set(String::New("freeScript"), FunctionTemplate::New(freeScript));
globalObject->Set(String::New("v8"), v8);
}