/
v8.cpp
166 lines (152 loc) · 4.72 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
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
/**
* @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 "Common.h"
#include "Extension.h"
#include "SilkJS.h"
#include <v8/v8-debug.h>
#include "VirtualMachine.h"
struct ScriptWrapper {
Persistent<Script> script;
};
/**
* @function v8.gc
*
* ### Synopsis
*
* v8.gc();
*
* Call v8's garbage collector.
*/
static void gc (const v8::FunctionCallbackInfo<v8::Value>& args)
{
for (int i=0; i<10000; i++) {
if (V8::IdleNotification()) {
break;
}
}
args.GetReturnValue().Set(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 void compileScript (const v8::FunctionCallbackInfo<v8::Value>& args)
{
// Persistent<Context>context = Context::New(NULL, ObjectTemplate::New());
// Context::Scope context_scope(context);
TryCatch tryCatch;
v8::Persistent<Script>s = v8::Persistent<Script>::New(::Extension::Get().isolate, v8::Script::New(args[0]->ToString(), args[1]->ToString()));
if (s.IsEmpty()) {
String::Utf8Value error(tryCatch.Exception());
args.GetReturnValue().Set(ThrowException(String::New(*error))); // String::Concat(String::New("Error compiling "), args[1]->ToString()));
// delete wrapper;
// return Opaque::New(NULL);
}
ScriptWrapper *wrapper = new ScriptWrapper;
wrapper->script = s; // Persistent<Script>::New(Script::New(args[0]->ToString(), args[1]->ToString()));
// context.Dispose();
args.GetReturnValue().Set(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 void runScript (const v8::FunctionCallbackInfo<v8::Value>& args)
{
ScriptWrapper *wrapper = (ScriptWrapper *) JSOPAQUE(args[0]); // wrap->Value();
if (!wrapper) {
args.GetReturnValue().Set(Null());
return;
}
// Persistent<Context>context = Context::New(NULL, ObjectTemplate::New());
// Context::Scope context_scope(context);
Handle<Value>v = wrapper->script->Run();
// context.Dispose();
args.GetReturnValue().Set(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 void freeScript (const v8::FunctionCallbackInfo<v8::Value>& args)
{
ScriptWrapper *wrapper = (ScriptWrapper *) JSOPAQUE(args[0]);
wrapper->script.Dispose();
delete wrapper;
args.GetReturnValue().Set(Undefined());
}
static void debugger ()
{
//TODO
Persistent<Context> context = ::Extension::Get().context;
v8::Isolate* isolate = ::Extension::Get().isolate;
v8::Locker locker(isolate); // Critical section
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
v8::Context::Scope contextScope(v8::Local<v8::Context>::New(isolate, context));
Debug::ProcessDebugMessages();
}
/**
* @function v8.enableDebugger
*
* ### Synopsis
*
* This function enables the internal v8 debugger on port 5858.
*/
static void enableDebugger (const v8::FunctionCallbackInfo<v8::Value>& args)
{
HandleScope scope;
Debug::SetDebugMessageDispatchHandler(debugger, true);
Debug::EnableAgent("silkjs", 5858, true);
Debug::DebugBreak();
args.GetReturnValue().Set(Undefined());
}
void VirtualMachine::Register (v8::Handle<v8::ObjectTemplate> global)
{
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));
global->Set(String::New("v8"), v8);
}