/
v8.cpp
140 lines (125 loc) · 3.57 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
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
/**
* @module builtin/v8
*
* ### Synopsis
* SilkJS builtin v8 object.
*
* ### Description
* The builtin/v8 object provides low-level access to some v8 JavaScript engine methods.
*
* ### Usage
* var net = require('builtin/v8');
*
* ### See Also:
* Google's v8 documentation and the source code
*/
#include "SilkJS.h"
#include <v8-debug.h>
struct ScriptWrapper {
Persistent<Script> script;
};
/**
* @function v8.gc
*
* ### Synopsis
*
* v8.gc();
*
* Call v8's garbage collector.
*/
static JSVAL gc (const Arguments& args) {
for (int i=0; i<10000; i++) {
if (V8::IdleNotification()) {
break;
}
}
return Undefined();
}
/**
* @function v8.compileScript
*
* ### Synopsis
* var script v8.compileScript(source);
*
* Compile a JavaScript source (string) to a v8::Script object. The returned script object should be treated as opaque by JavaScript code.
*
* @param {string} source - JavaScript source code to compile.
* @return {object} script - Compiled JavaScript (opaque handle)
*
* ### Notes
* You should free any scripts you compile with v8.freeScript().
*/
static JSVAL compileScript (JSARGS args) {
// 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 Opaque::New(wrapper);
}
/**
* @function v8.runScript
*
* ### Synopsis
*
* var ret = v8.runScript(script);
*
* Run a previously compiled v8::Script - see v8.compileScript above.
* @param {object} script - opaque handle to previously compiled v8::Script.
* @return whatever the script returns.
*/
// runScript(context, script)
static JSVAL runScript (JSARGS args) {
ScriptWrapper *wrapper = (ScriptWrapper *) JSOPAQUE(args[0]); // wrap->Value();
// Persistent<Context>context = Context::New(NULL, ObjectTemplate::New());
// Context::Scope context_scope(context);
Handle<Value>v = wrapper->script->Run();
// context.Dispose();
return v;
}
/**
* @function v8.freeScript
*
* ### Synopsis
*
* v8.freeScript(script);
*
* Free a previously compiled v8::Script. Each compiled script consumes some amount of memory until it is freed.
*
* @param {object} script - opaque handle to script to be freed.
*/
static JSVAL freeScript (JSARGS args) {
ScriptWrapper *wrapper = (ScriptWrapper *) JSOPAQUE(args[0]);
wrapper->script.Dispose();
delete wrapper;
return Undefined();
}
static void debugger () {
extern Persistent<Context> context;
Context::Scope scope(context);
Debug::ProcessDebugMessages();
}
/**
* @function v8.enableDebugger
*
* ### Synopsis
*
* This function enables the internal v8 debugger on port 5858.
*/
static JSVAL enableDebugger (JSARGS args) {
HandleScope scope;
Debug::SetDebugMessageDispatchHandler(debugger, true);
Debug::EnableAgent("silkjs", 5858, true);
Debug::DebugBreak();
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));
v8->Set(String::New("enableDebugger"), FunctionTemplate::New(enableDebugger));
builtinObject->Set(String::New("v8"), v8);
}