The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / src / node_script.cc @ 6f92da2d
History | View | Annotate | Download (14.3 KB)
1 | 55048cdf | Ryan Dahl | // Copyright Joyent, Inc. and other Node contributors.
|
---|---|---|---|
2 | //
|
||
3 | // Permission is hereby granted, free of charge, to any person obtaining a
|
||
4 | // copy of this software and associated documentation files (the
|
||
5 | // "Software"), to deal in the Software without restriction, including
|
||
6 | // without limitation the rights to use, copy, modify, merge, publish,
|
||
7 | // distribute, sublicense, and/or sell copies of the Software, and to permit
|
||
8 | // persons to whom the Software is furnished to do so, subject to the
|
||
9 | // following conditions:
|
||
10 | //
|
||
11 | // The above copyright notice and this permission notice shall be included
|
||
12 | // in all copies or substantial portions of the Software.
|
||
13 | //
|
||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||
15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||
16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||
17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||
18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||
19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||
20 | // USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||
21 | |||
22 | ff4a9d38 | Ben Noordhuis | #include "node.h" |
23 | #include "node_script.h" |
||
24 | c0818093 | Andrew Paprocki | #include "node_watchdog.h" |
25 | c2a06725 | Herbert Vojcik | #include <assert.h> |
26 | |||
27 | 40f29dd4 | Ryan Dahl | namespace node {
|
28 | c2a06725 | Herbert Vojcik | |
29 | b9165252 | Ben Noordhuis | using v8::Array;
|
30 | 40f29dd4 | Ryan Dahl | using v8::Context;
|
31 | b9165252 | Ben Noordhuis | using v8::Exception;
|
32 | using v8::Function;
|
||
33 | 110a9cd8 | Ben Noordhuis | using v8::FunctionCallbackInfo;
|
34 | b9165252 | Ben Noordhuis | using v8::FunctionTemplate;
|
35 | 40f29dd4 | Ryan Dahl | using v8::Handle;
|
36 | using v8::HandleScope;
|
||
37 | b9165252 | Ben Noordhuis | using v8::Local;
|
38 | 40f29dd4 | Ryan Dahl | using v8::Object;
|
39 | b9165252 | Ben Noordhuis | using v8::Persistent;
|
40 | using v8::Script;
|
||
41 | using v8::String;
|
||
42 | 40f29dd4 | Ryan Dahl | using v8::TryCatch;
|
43 | c0818093 | Andrew Paprocki | using v8::V8;
|
44 | b9165252 | Ben Noordhuis | using v8::Value;
|
45 | f123a1ab | Ryan Dahl | |
46 | c2a06725 | Herbert Vojcik | |
47 | 40f29dd4 | Ryan Dahl | class WrappedContext : ObjectWrap { |
48 | public:
|
||
49 | static void Initialize(Handle<Object> target); |
||
50 | 110a9cd8 | Ben Noordhuis | static void New(const FunctionCallbackInfo<Value>& args); |
51 | 23172c5d | Ryan Dahl | |
52 | 110a9cd8 | Ben Noordhuis | Local<Context> GetV8Context(); |
53 | 40f29dd4 | Ryan Dahl | static Local<Object> NewInstance();
|
54 | f0872060 | Ben Noordhuis | static bool InstanceOf(Handle<Value> value); |
55 | 242161be | Ruben Rodriguez | |
56 | 40f29dd4 | Ryan Dahl | protected:
|
57 | 23172c5d | Ryan Dahl | |
58 | 74a8215a | Ben Noordhuis | static Persistent<FunctionTemplate> constructor_template;
|
59 | |||
60 | 40f29dd4 | Ryan Dahl | WrappedContext(); |
61 | ~WrappedContext(); |
||
62 | |||
63 | Persistent<Context> context_; |
||
64 | }; |
||
65 | |||
66 | |||
67 | 74a8215a | Ben Noordhuis | Persistent<FunctionTemplate> WrappedContext::constructor_template; |
68 | |||
69 | |||
70 | 40f29dd4 | Ryan Dahl | class WrappedScript : ObjectWrap { |
71 | public:
|
||
72 | static void Initialize(Handle<Object> target); |
||
73 | |||
74 | enum EvalInputFlags { compileCode, unwrapExternal };
|
||
75 | enum EvalContextFlags { thisContext, newContext, userContext };
|
||
76 | enum EvalOutputFlags { returnResult, wrapExternal };
|
||
77 | c0818093 | Andrew Paprocki | enum EvalTimeoutFlags { noTimeout, useTimeout };
|
78 | 40f29dd4 | Ryan Dahl | |
79 | template <EvalInputFlags input_flag,
|
||
80 | EvalContextFlags context_flag, |
||
81 | c0818093 | Andrew Paprocki | EvalOutputFlags output_flag, |
82 | EvalTimeoutFlags timeout_flag> |
||
83 | 110a9cd8 | Ben Noordhuis | static void EvalMachine(const FunctionCallbackInfo<Value>& args); |
84 | 40f29dd4 | Ryan Dahl | |
85 | protected:
|
||
86 | WrappedScript() : ObjectWrap() {} |
||
87 | ~WrappedScript(); |
||
88 | |||
89 | 110a9cd8 | Ben Noordhuis | static void New(const FunctionCallbackInfo<Value>& args); |
90 | static void CreateContext(const FunctionCallbackInfo<Value>& args); |
||
91 | static void RunInContext(const FunctionCallbackInfo<Value>& args); |
||
92 | static void RunInThisContext(const FunctionCallbackInfo<Value>& args); |
||
93 | static void RunInNewContext(const FunctionCallbackInfo<Value>& args); |
||
94 | static void CompileRunInContext(const FunctionCallbackInfo<Value>& args); |
||
95 | static void CompileRunInThisContext(const FunctionCallbackInfo<Value>& args); |
||
96 | static void CompileRunInNewContext(const FunctionCallbackInfo<Value>& args); |
||
97 | 40f29dd4 | Ryan Dahl | |
98 | Persistent<Script> script_; |
||
99 | }; |
||
100 | |||
101 | |||
102 | 8c2c7bb8 | Fedor Indutny | void CloneObject(Handle<Object> recv,
|
103 | 110a9cd8 | Ben Noordhuis | Handle<Value> source, |
104 | Handle<Value> target) { |
||
105 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
106 | 8c2c7bb8 | Fedor Indutny | |
107 | 110a9cd8 | Ben Noordhuis | const char raw_script_source[] = |
108 | "(function(source, target) { \n"
|
||
109 | " Object.getOwnPropertyNames(source).forEach(function(key) { \n"
|
||
110 | " try { \n"
|
||
111 | " var desc = Object.getOwnPropertyDescriptor(source, key); \n"
|
||
112 | " if (desc.value === source) desc.value = target; \n"
|
||
113 | " Object.defineProperty(target, key, desc); \n"
|
||
114 | " } catch (e) { \n"
|
||
115 | " // Catch sealed properties errors \n"
|
||
116 | " } \n"
|
||
117 | " }); \n"
|
||
118 | "}); \n";
|
||
119 | |||
120 | Local<String> script_source = |
||
121 | String::New(raw_script_source, sizeof(raw_script_source) - 1); |
||
122 | Local<Script> script = |
||
123 | Script::Compile(script_source, String::New("binding:script"));
|
||
124 | |||
125 | Local<Function> fun = script->Run().As<Function>(); |
||
126 | assert(fun.IsEmpty() == false);
|
||
127 | assert(fun->IsFunction() == true);
|
||
128 | |||
129 | Handle<Value> argv[] = { source, target }; |
||
130 | Handle<Value> rc = fun->Call(recv, ARRAY_SIZE(argv), argv); |
||
131 | assert(rc.IsEmpty() == false);
|
||
132 | 8c2c7bb8 | Fedor Indutny | } |
133 | |||
134 | |||
135 | 40f29dd4 | Ryan Dahl | void WrappedContext::Initialize(Handle<Object> target) {
|
136 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
137 | 242161be | Ruben Rodriguez | |
138 | 40f29dd4 | Ryan Dahl | Local<FunctionTemplate> t = FunctionTemplate::New(WrappedContext::New); |
139 | 110a9cd8 | Ben Noordhuis | t->InstanceTemplate()->SetInternalFieldCount(1);
|
140 | t->SetClassName(String::NewSymbol("Context"));
|
||
141 | 242161be | Ruben Rodriguez | |
142 | 110a9cd8 | Ben Noordhuis | target->Set(String::NewSymbol("Context"), t->GetFunction());
|
143 | constructor_template.Reset(node_isolate, t); |
||
144 | 242161be | Ruben Rodriguez | } |
145 | |||
146 | 23172c5d | Ryan Dahl | |
147 | f0872060 | Ben Noordhuis | bool WrappedContext::InstanceOf(Handle<Value> value) {
|
148 | 110a9cd8 | Ben Noordhuis | return !value.IsEmpty() && HasInstance(constructor_template, value);
|
149 | f0872060 | Ben Noordhuis | } |
150 | |||
151 | |||
152 | 110a9cd8 | Ben Noordhuis | void WrappedContext::New(const FunctionCallbackInfo<Value>& args) { |
153 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
154 | 40f29dd4 | Ryan Dahl | WrappedContext *t = new WrappedContext();
|
155 | 242161be | Ruben Rodriguez | t->Wrap(args.This()); |
156 | } |
||
157 | |||
158 | 23172c5d | Ryan Dahl | |
159 | 40f29dd4 | Ryan Dahl | WrappedContext::WrappedContext() : ObjectWrap() { |
160 | 110a9cd8 | Ben Noordhuis | context_.Reset(node_isolate, Context::New(node_isolate)); |
161 | 23172c5d | Ryan Dahl | } |
162 | |||
163 | |||
164 | 40f29dd4 | Ryan Dahl | WrappedContext::~WrappedContext() { |
165 | 110a9cd8 | Ben Noordhuis | context_.Dispose(); |
166 | 242161be | Ruben Rodriguez | } |
167 | |||
168 | 23172c5d | Ryan Dahl | |
169 | 40f29dd4 | Ryan Dahl | Local<Object> WrappedContext::NewInstance() { |
170 | 110a9cd8 | Ben Noordhuis | Local<FunctionTemplate> constructor_template_handle = |
171 | 78d90945 | Ben Noordhuis | PersistentToLocal(node_isolate, constructor_template); |
172 | 110a9cd8 | Ben Noordhuis | return constructor_template_handle->GetFunction()->NewInstance();
|
173 | 242161be | Ruben Rodriguez | } |
174 | |||
175 | 23172c5d | Ryan Dahl | |
176 | 110a9cd8 | Ben Noordhuis | Local<Context> WrappedContext::GetV8Context() { |
177 | d4cc30f1 | Ben Noordhuis | return PersistentToLocal(node_isolate, context_);
|
178 | 242161be | Ruben Rodriguez | } |
179 | |||
180 | |||
181 | 40f29dd4 | Ryan Dahl | void WrappedScript::Initialize(Handle<Object> target) {
|
182 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
183 | c2a06725 | Herbert Vojcik | |
184 | 40f29dd4 | Ryan Dahl | Local<FunctionTemplate> t = FunctionTemplate::New(WrappedScript::New); |
185 | 110a9cd8 | Ben Noordhuis | t->InstanceTemplate()->SetInternalFieldCount(1);
|
186 | 75db1995 | Ryan Dahl | // Note: We use 'NodeScript' instead of 'Script' so that we do not
|
187 | // conflict with V8's Script class defined in v8/src/messages.js
|
||
188 | // See GH-203 https://github.com/joyent/node/issues/203
|
||
189 | 110a9cd8 | Ben Noordhuis | t->SetClassName(String::NewSymbol("NodeScript"));
|
190 | c2a06725 | Herbert Vojcik | |
191 | 110a9cd8 | Ben Noordhuis | NODE_SET_PROTOTYPE_METHOD(t, |
192 | 40f29dd4 | Ryan Dahl | "createContext",
|
193 | WrappedScript::CreateContext); |
||
194 | c2a06725 | Herbert Vojcik | |
195 | 110a9cd8 | Ben Noordhuis | NODE_SET_PROTOTYPE_METHOD(t, |
196 | 40f29dd4 | Ryan Dahl | "runInContext",
|
197 | WrappedScript::RunInContext); |
||
198 | |||
199 | 110a9cd8 | Ben Noordhuis | NODE_SET_PROTOTYPE_METHOD(t, |
200 | 40f29dd4 | Ryan Dahl | "runInThisContext",
|
201 | WrappedScript::RunInThisContext); |
||
202 | |||
203 | 110a9cd8 | Ben Noordhuis | NODE_SET_PROTOTYPE_METHOD(t, |
204 | 40f29dd4 | Ryan Dahl | "runInNewContext",
|
205 | WrappedScript::RunInNewContext); |
||
206 | |||
207 | 110a9cd8 | Ben Noordhuis | NODE_SET_METHOD(t, |
208 | 40f29dd4 | Ryan Dahl | "createContext",
|
209 | WrappedScript::CreateContext); |
||
210 | |||
211 | 110a9cd8 | Ben Noordhuis | NODE_SET_METHOD(t, |
212 | 40f29dd4 | Ryan Dahl | "runInContext",
|
213 | WrappedScript::CompileRunInContext); |
||
214 | |||
215 | 110a9cd8 | Ben Noordhuis | NODE_SET_METHOD(t, |
216 | 40f29dd4 | Ryan Dahl | "runInThisContext",
|
217 | WrappedScript::CompileRunInThisContext); |
||
218 | |||
219 | 110a9cd8 | Ben Noordhuis | NODE_SET_METHOD(t, |
220 | 40f29dd4 | Ryan Dahl | "runInNewContext",
|
221 | WrappedScript::CompileRunInNewContext); |
||
222 | |||
223 | 110a9cd8 | Ben Noordhuis | target->Set(String::NewSymbol("NodeScript"), t->GetFunction());
|
224 | c2a06725 | Herbert Vojcik | } |
225 | |||
226 | 23172c5d | Ryan Dahl | |
227 | 110a9cd8 | Ben Noordhuis | void WrappedScript::New(const FunctionCallbackInfo<Value>& args) { |
228 | assert(args.IsConstructCall() == true);
|
||
229 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
230 | 40f29dd4 | Ryan Dahl | WrappedScript *t = new WrappedScript();
|
231 | 0a4ebc3d | Trevor Norris | t->Wrap(args.This()); |
232 | 110a9cd8 | Ben Noordhuis | WrappedScript::EvalMachine< |
233 | c0818093 | Andrew Paprocki | compileCode, thisContext, wrapExternal, noTimeout>(args); |
234 | c2a06725 | Herbert Vojcik | } |
235 | |||
236 | 23172c5d | Ryan Dahl | |
237 | 40f29dd4 | Ryan Dahl | WrappedScript::~WrappedScript() { |
238 | 110a9cd8 | Ben Noordhuis | script_.Dispose(); |
239 | c2a06725 | Herbert Vojcik | } |
240 | |||
241 | |||
242 | 110a9cd8 | Ben Noordhuis | void WrappedScript::CreateContext(const FunctionCallbackInfo<Value>& args) { |
243 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
244 | 242161be | Ruben Rodriguez | |
245 | 40f29dd4 | Ryan Dahl | Local<Object> context = WrappedContext::NewInstance(); |
246 | 242161be | Ruben Rodriguez | |
247 | if (args.Length() > 0) { |
||
248 | 9f9c333c | Fedor Indutny | if (args[0]->IsObject()) { |
249 | Local<Object> sandbox = args[0].As<Object>();
|
||
250 | |||
251 | CloneObject(args.This(), sandbox, context); |
||
252 | } else {
|
||
253 | 110a9cd8 | Ben Noordhuis | return ThrowTypeError(
|
254 | "createContext() accept only object as first argument.");
|
||
255 | 9f9c333c | Fedor Indutny | } |
256 | 242161be | Ruben Rodriguez | } |
257 | |||
258 | 110a9cd8 | Ben Noordhuis | args.GetReturnValue().Set(context); |
259 | 242161be | Ruben Rodriguez | } |
260 | |||
261 | 23172c5d | Ryan Dahl | |
262 | 110a9cd8 | Ben Noordhuis | void WrappedScript::RunInContext(const FunctionCallbackInfo<Value>& args) { |
263 | WrappedScript::EvalMachine< |
||
264 | c0818093 | Andrew Paprocki | unwrapExternal, userContext, returnResult, useTimeout>(args); |
265 | 242161be | Ruben Rodriguez | } |
266 | |||
267 | |||
268 | 110a9cd8 | Ben Noordhuis | void WrappedScript::RunInThisContext(const FunctionCallbackInfo<Value>& args) { |
269 | WrappedScript::EvalMachine< |
||
270 | c0818093 | Andrew Paprocki | unwrapExternal, thisContext, returnResult, useTimeout>(args); |
271 | c2a06725 | Herbert Vojcik | } |
272 | |||
273 | |||
274 | 110a9cd8 | Ben Noordhuis | void WrappedScript::RunInNewContext(const FunctionCallbackInfo<Value>& args) { |
275 | WrappedScript::EvalMachine< |
||
276 | c0818093 | Andrew Paprocki | unwrapExternal, newContext, returnResult, useTimeout>(args); |
277 | c2a06725 | Herbert Vojcik | } |
278 | |||
279 | |||
280 | 110a9cd8 | Ben Noordhuis | void WrappedScript::CompileRunInContext(
|
281 | const FunctionCallbackInfo<Value>& args) {
|
||
282 | WrappedScript::EvalMachine< |
||
283 | c0818093 | Andrew Paprocki | compileCode, userContext, returnResult, useTimeout>(args); |
284 | 242161be | Ruben Rodriguez | } |
285 | |||
286 | |||
287 | 110a9cd8 | Ben Noordhuis | void WrappedScript::CompileRunInThisContext(
|
288 | const FunctionCallbackInfo<Value>& args) {
|
||
289 | WrappedScript::EvalMachine< |
||
290 | c0818093 | Andrew Paprocki | compileCode, thisContext, returnResult, useTimeout>(args); |
291 | c2a06725 | Herbert Vojcik | } |
292 | |||
293 | |||
294 | 110a9cd8 | Ben Noordhuis | void WrappedScript::CompileRunInNewContext(
|
295 | const FunctionCallbackInfo<Value>& args) {
|
||
296 | WrappedScript::EvalMachine< |
||
297 | c0818093 | Andrew Paprocki | compileCode, newContext, returnResult, useTimeout>(args); |
298 | c2a06725 | Herbert Vojcik | } |
299 | |||
300 | |||
301 | 40f29dd4 | Ryan Dahl | template <WrappedScript::EvalInputFlags input_flag,
|
302 | WrappedScript::EvalContextFlags context_flag, |
||
303 | c0818093 | Andrew Paprocki | WrappedScript::EvalOutputFlags output_flag, |
304 | WrappedScript::EvalTimeoutFlags timeout_flag> |
||
305 | 110a9cd8 | Ben Noordhuis | void WrappedScript::EvalMachine(const FunctionCallbackInfo<Value>& args) { |
306 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
307 | c2a06725 | Herbert Vojcik | |
308 | 40f29dd4 | Ryan Dahl | if (input_flag == compileCode && args.Length() < 1) { |
309 | 110a9cd8 | Ben Noordhuis | return ThrowTypeError("needs at least 'code' argument."); |
310 | c2a06725 | Herbert Vojcik | } |
311 | |||
312 | 40f29dd4 | Ryan Dahl | const int sandbox_index = input_flag == compileCode ? 1 : 0; |
313 | 8e29ce9f | Fedor Indutny | if (context_flag == userContext &&
|
314 | !WrappedContext::InstanceOf(args[sandbox_index])) { |
||
315 | 110a9cd8 | Ben Noordhuis | return ThrowTypeError("needs a 'context' argument."); |
316 | 242161be | Ruben Rodriguez | } |
317 | |||
318 | c2a06725 | Herbert Vojcik | Local<String> code; |
319 | 40f29dd4 | Ryan Dahl | if (input_flag == compileCode) code = args[0]->ToString(); |
320 | c2a06725 | Herbert Vojcik | |
321 | Local<Object> sandbox; |
||
322 | 40f29dd4 | Ryan Dahl | if (context_flag == newContext) {
|
323 | sandbox = args[sandbox_index]->IsObject() ? args[sandbox_index]->ToObject() |
||
324 | : Object::New(); |
||
325 | } else if (context_flag == userContext) { |
||
326 | sandbox = args[sandbox_index]->ToObject(); |
||
327 | 242161be | Ruben Rodriguez | } |
328 | 23172c5d | Ryan Dahl | |
329 | 40f29dd4 | Ryan Dahl | const int filename_index = sandbox_index + |
330 | c05936ca | Antranig Basman | (context_flag == thisContext? 0 : 1); |
331 | 40f29dd4 | Ryan Dahl | Local<String> filename = args.Length() > filename_index |
332 | ? args[filename_index]->ToString() |
||
333 | 23172c5d | Ryan Dahl | : String::New("evalmachine.<anonymous>");
|
334 | c2a06725 | Herbert Vojcik | |
335 | c0818093 | Andrew Paprocki | uint64_t timeout = 0;
|
336 | const int timeout_index = filename_index + 1; |
||
337 | if (timeout_flag == useTimeout && args.Length() > timeout_index) {
|
||
338 | if (!args[timeout_index]->IsUint32()) {
|
||
339 | 110a9cd8 | Ben Noordhuis | return ThrowTypeError("needs an unsigned integer 'ms' argument."); |
340 | c0818093 | Andrew Paprocki | } |
341 | timeout = args[timeout_index]->Uint32Value(); |
||
342 | } |
||
343 | |||
344 | const int display_error_index = timeout_index + |
||
345 | (timeout_flag == noTimeout ? 0 : 1); |
||
346 | 1c7cd4aa | Ryan Dahl | bool display_error = false; |
347 | if (args.Length() > display_error_index &&
|
||
348 | args[display_error_index]->IsBoolean() && |
||
349 | args[display_error_index]->BooleanValue() == true) {
|
||
350 | display_error = true;
|
||
351 | } |
||
352 | |||
353 | 110a9cd8 | Ben Noordhuis | Local<Context> context = Context::GetCurrent(); |
354 | 23172c5d | Ryan Dahl | |
355 | c2a06725 | Herbert Vojcik | Local<Array> keys; |
356 | 40f29dd4 | Ryan Dahl | if (context_flag == newContext) {
|
357 | c2a06725 | Herbert Vojcik | // Create the new context
|
358 | 2868bf94 | Trevor Norris | context = Context::New(node_isolate); |
359 | c2a06725 | Herbert Vojcik | |
360 | 40f29dd4 | Ryan Dahl | } else if (context_flag == userContext) { |
361 | 242161be | Ruben Rodriguez | // Use the passed in context
|
362 | 40f29dd4 | Ryan Dahl | WrappedContext *nContext = ObjectWrap::Unwrap<WrappedContext>(sandbox); |
363 | 242161be | Ruben Rodriguez | context = nContext->GetV8Context(); |
364 | } |
||
365 | |||
366 | 70635753 | Marcel Laverdet | Context::Scope context_scope(context); |
367 | |||
368 | 242161be | Ruben Rodriguez | // New and user context share code. DRY it up.
|
369 | 40f29dd4 | Ryan Dahl | if (context_flag == userContext || context_flag == newContext) {
|
370 | 9d27faa2 | Ryan Dahl | // Copy everything from the passed in sandbox (either the persistent
|
371 | // context for runInContext(), or the sandbox arg to runInNewContext()).
|
||
372 | CloneObject(args.This(), sandbox, context->Global()->GetPrototype()); |
||
373 | c2a06725 | Herbert Vojcik | } |
374 | |||
375 | // Catch errors
|
||
376 | TryCatch try_catch; |
||
377 | |||
378 | c16963b9 | Miroslav Bajtos | // TryCatch must not be verbose to prevent duplicate logging
|
379 | // of uncaught exceptions (we are rethrowing them)
|
||
380 | try_catch.SetVerbose(false);
|
||
381 | |||
382 | c2a06725 | Herbert Vojcik | Handle<Value> result; |
383 | 110a9cd8 | Ben Noordhuis | Local<Script> script; |
384 | c2a06725 | Herbert Vojcik | |
385 | 40f29dd4 | Ryan Dahl | if (input_flag == compileCode) {
|
386 | // well, here WrappedScript::New would suffice in all cases, but maybe
|
||
387 | 23172c5d | Ryan Dahl | // Compile has a little better performance where possible
|
388 | 40f29dd4 | Ryan Dahl | script = output_flag == returnResult ? Script::Compile(code, filename) |
389 | : Script::New(code, filename); |
||
390 | c2a06725 | Herbert Vojcik | if (script.IsEmpty()) {
|
391 | b57c1f51 | Ryan Dahl | // FIXME UGLY HACK TO DISPLAY SYNTAX ERRORS.
|
392 | c16963b9 | Miroslav Bajtos | if (display_error) DisplayExceptionLine(try_catch.Message());
|
393 | b57c1f51 | Ryan Dahl | |
394 | c2a06725 | Herbert Vojcik | // Hack because I can't get a proper stacktrace on SyntaxError
|
395 | 110a9cd8 | Ben Noordhuis | try_catch.ReThrow(); |
396 | return;
|
||
397 | c2a06725 | Herbert Vojcik | } |
398 | } else {
|
||
399 | 0a4ebc3d | Trevor Norris | WrappedScript *n_script = ObjectWrap::Unwrap<WrappedScript>(args.This()); |
400 | 40f29dd4 | Ryan Dahl | if (!n_script) {
|
401 | 110a9cd8 | Ben Noordhuis | return ThrowError("Must be called as a method of Script."); |
402 | 40f29dd4 | Ryan Dahl | } else if (n_script->script_.IsEmpty()) { |
403 | 110a9cd8 | Ben Noordhuis | return ThrowError(
|
404 | "'this' must be a result of previous new Script(code) call.");
|
||
405 | c2a06725 | Herbert Vojcik | } |
406 | 23172c5d | Ryan Dahl | |
407 | d4cc30f1 | Ben Noordhuis | script = PersistentToLocal(node_isolate, n_script->script_); |
408 | c2a06725 | Herbert Vojcik | } |
409 | |||
410 | 40f29dd4 | Ryan Dahl | if (output_flag == returnResult) {
|
411 | c0818093 | Andrew Paprocki | if (timeout) {
|
412 | Watchdog wd(timeout); |
||
413 | result = script->Run(); |
||
414 | } else {
|
||
415 | result = script->Run(); |
||
416 | } |
||
417 | if (try_catch.HasCaught() && try_catch.HasTerminated()) {
|
||
418 | V8::CancelTerminateExecution(args.GetIsolate()); |
||
419 | 110a9cd8 | Ben Noordhuis | return ThrowError("Script execution timed out."); |
420 | c0818093 | Andrew Paprocki | } |
421 | 1f50d711 | Ryan Dahl | if (result.IsEmpty()) {
|
422 | c16963b9 | Miroslav Bajtos | if (display_error) DisplayExceptionLine(try_catch.Message());
|
423 | 110a9cd8 | Ben Noordhuis | try_catch.ReThrow(); |
424 | return;
|
||
425 | 1f50d711 | Ryan Dahl | } |
426 | 23172c5d | Ryan Dahl | } else {
|
427 | 0a4ebc3d | Trevor Norris | WrappedScript *n_script = ObjectWrap::Unwrap<WrappedScript>(args.This()); |
428 | 40f29dd4 | Ryan Dahl | if (!n_script) {
|
429 | 110a9cd8 | Ben Noordhuis | return ThrowError("Must be called as a method of Script."); |
430 | 8c853404 | Blake Mizerany | } |
431 | 110a9cd8 | Ben Noordhuis | n_script->script_.Reset(node_isolate, script); |
432 | 23172c5d | Ryan Dahl | result = args.This(); |
433 | } |
||
434 | |||
435 | 40f29dd4 | Ryan Dahl | if (context_flag == userContext || context_flag == newContext) {
|
436 | 23172c5d | Ryan Dahl | // success! copy changes back onto the sandbox object.
|
437 | 8c2c7bb8 | Fedor Indutny | CloneObject(args.This(), context->Global()->GetPrototype(), sandbox); |
438 | c2a06725 | Herbert Vojcik | } |
439 | |||
440 | 110a9cd8 | Ben Noordhuis | args.GetReturnValue().Set(result); |
441 | c2a06725 | Herbert Vojcik | } |
442 | 870aa3d9 | Paul Querna | |
443 | 40f29dd4 | Ryan Dahl | |
444 | void InitEvals(Handle<Object> target) {
|
||
445 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
446 | 40f29dd4 | Ryan Dahl | WrappedContext::Initialize(target); |
447 | WrappedScript::Initialize(target); |
||
448 | 870aa3d9 | Paul Querna | } |
449 | |||
450 | 40f29dd4 | Ryan Dahl | |
451 | } // namespace node
|
||
452 | |||
453 | cdcb1118 | Ben Noordhuis | NODE_MODULE(node_evals, node::InitEvals) |