@@ -20,7 +20,7 @@ namespace titanium {
20
20
Persistent<String> EvaluateModule::REQUIRE_STRING;
21
21
Persistent<String> EvaluateModule::MODULE_REF_STRING;
22
22
23
- std::vector<Persistent<Context, CopyablePersistentTraits<Context>>> EvaluateModule::moduleContexts ;
23
+ std::vector<Persistent<Context, CopyablePersistentTraits<Context>>> EvaluateModule::module_contexts ;
24
24
25
25
static void assign (Local<Object> source, Local<Data> destination)
26
26
{
@@ -65,15 +65,14 @@ namespace titanium {
65
65
}
66
66
67
67
SetMethod (context, isolate, target, " runAsModule" , EvaluateModule::RunAsModule);
68
- SetMethod (context, isolate, target, " runAsScript" , EvaluateModule::RunAsScript);
69
68
}
70
69
71
70
void EvaluateModule::GlobalSetterCallback (Local<String> key, Local<Value> value, const PropertyCallbackInfo<Value>& info)
72
71
{
73
72
Isolate* isolate = info.GetIsolate ();
74
73
75
74
// Iterate through current module contexts.
76
- for (const auto persistentModuleContext : moduleContexts ) {
75
+ for (const auto persistentModuleContext : EvaluateModule::module_contexts ) {
77
76
78
77
if (!persistentModuleContext.IsEmpty ()) {
79
78
@@ -172,8 +171,7 @@ namespace titanium {
172
171
}
173
172
174
173
if (tryCatch.HasCaught ()) {
175
- V8Util::openJSErrorDialog (isolate, tryCatch);
176
- V8Util::reportException (isolate, tryCatch, true );
174
+ tryCatch.ReThrow ();
177
175
}
178
176
179
177
return Undefined (isolate);
@@ -190,24 +188,20 @@ namespace titanium {
190
188
void EvaluateModule::RunAsModule (const FunctionCallbackInfo<Value>& args)
191
189
{
192
190
Isolate* isolate = args.GetIsolate ();
193
- Local<Context> context = isolate->GetCurrentContext ();
194
191
195
192
if (args.Length () == 0 ) {
196
193
isolate->ThrowException (
197
- Exception::TypeError (
194
+ Exception::SyntaxError (
198
195
STRING_NEW (isolate, " Missing arguments, requires at least 'code' argument." )));
199
196
return ;
200
197
}
201
198
202
199
Local<String> code = args[0 ].As <String>();
203
- Local<String> filename = args.Length () > 1
204
- && args[1 ]->IsString ()
200
+ Local<String> filename = args.Length () > 1 && args[1 ]->IsString ()
205
201
? args[1 ].As <String>() : STRING_NEW (isolate, " <anonymous>" );
206
- Local<Object> contextObj = args.Length () > 2
207
- && args[2 ]->IsObject ()
202
+ Local<Object> contextObj = args.Length () > 2 && args[2 ]->IsObject ()
208
203
? args[2 ].As <Object>() : Local<Object>();
209
204
210
- TryCatch tryCatch (isolate);
211
205
ScriptOrigin origin (filename,
212
206
Local<Integer>(),
213
207
Local<Integer>(),
@@ -219,12 +213,16 @@ namespace titanium {
219
213
Boolean::New (isolate, true ));
220
214
ScriptCompiler::Source source (code, origin);
221
215
222
- // Attempt to compile module source code.
223
- MaybeLocal<Module> maybeModule = ScriptCompiler::CompileModule (isolate, &source);
224
- if (tryCatch.HasCaught ()) {
225
- V8Util::openJSErrorDialog (isolate, tryCatch);
226
- V8Util::reportException (isolate, tryCatch, true );
227
- return ;
216
+ MaybeLocal<Module> maybeModule;
217
+ {
218
+ TryCatch tryCatch (isolate);
219
+
220
+ // Attempt to compile module source code.
221
+ maybeModule = ScriptCompiler::CompileModule (isolate, &source);
222
+ if (tryCatch.HasCaught ()) {
223
+ tryCatch.ReThrow ();
224
+ return ;
225
+ }
228
226
}
229
227
if (maybeModule.IsEmpty ()) {
230
228
args.GetReturnValue ().Set (v8::Undefined (isolate));
@@ -246,7 +244,7 @@ namespace titanium {
246
244
Persistent<Context> persistentModuleContext;
247
245
persistentModuleContext.Reset (isolate, moduleContext);
248
246
persistentModuleContext.SetWeak ();
249
- moduleContexts .emplace_back (persistentModuleContext);
247
+ EvaluateModule::module_contexts .emplace_back (persistentModuleContext);
250
248
251
249
// Obtain module global context.
252
250
Local<Object> moduleGlobal = moduleContext->Global ();
@@ -265,30 +263,20 @@ namespace titanium {
265
263
}
266
264
267
265
// Instantiate module and process imports via `ModuleCallback`.
268
- module->InstantiateModule (moduleContext, ModuleCallback).FromJust ();
269
-
266
+ module->InstantiateModule (moduleContext, ModuleCallback);
270
267
if (module->GetStatus () == Module::kErrored ) {
271
268
272
269
// Throw any exceptions from module instantiation.
273
270
isolate->ThrowException (module->GetException ());
274
- }
275
- if (tryCatch.HasCaught ()) {
276
- V8Util::openJSErrorDialog (isolate, tryCatch);
277
- V8Util::reportException (isolate, tryCatch, true );
278
271
return ;
279
272
}
280
273
281
274
// Execute module, obtaining a result.
282
275
MaybeLocal<Value> maybeResult = module->Evaluate (moduleContext);
283
-
284
276
if (module->GetStatus () == Module::kErrored ) {
285
277
286
278
// Throw any exceptions from module evaluation.
287
279
isolate->ThrowException (module->GetException ());
288
- }
289
- if (tryCatch.HasCaught ()) {
290
- V8Util::openJSErrorDialog (isolate, tryCatch);
291
- V8Util::reportException (isolate, tryCatch, true );
292
280
return ;
293
281
}
294
282
if (maybeResult.IsEmpty ()) {
@@ -307,20 +295,10 @@ namespace titanium {
307
295
308
296
// When top-level-await is enabled, modules return a `Promise`.
309
297
// Wait for the promise to fulfill before obtaining module exports.
310
- if (promise->State () == Promise::kPending ) {
298
+ while (promise->State () == Promise::kPending ) {
311
299
312
300
// Allow promise to fulfill.
313
- isolate->PerformMicrotaskCheckpoint ();
314
- }
315
- if (module->GetStatus () == Module::kErrored ) {
316
-
317
- // Throw any exceptions from promise.
318
- isolate->ThrowException (module->GetException ());
319
- }
320
- if (tryCatch.HasCaught ()) {
321
- V8Util::openJSErrorDialog (isolate, tryCatch);
322
- V8Util::reportException (isolate, tryCatch, true );
323
- return ;
301
+ isolate->RunMicrotasks ();
324
302
}
325
303
326
304
// Obtain module exports as result.
@@ -330,82 +308,4 @@ namespace titanium {
330
308
// Return result.
331
309
args.GetReturnValue ().Set (result);
332
310
}
333
-
334
- void EvaluateModule::RunAsScript (const FunctionCallbackInfo<Value>& args)
335
- {
336
- Isolate* isolate = args.GetIsolate ();
337
- Local<Context> context = isolate->GetCurrentContext ();
338
-
339
- if (args.Length () < 1 ) {
340
- isolate->ThrowException (
341
- Exception::TypeError (
342
- STRING_NEW (isolate, " Missing arguments, requires at least 'code' argument." )));
343
- return ;
344
- }
345
-
346
- Local<String> code = args[0 ].As <String>();
347
- Local<String> filename = args.Length () > 1
348
- && args[1 ]->IsString ()
349
- ? args[1 ].As <String>() : STRING_NEW (isolate, " <anonymous>" );
350
- Local<Object> contextObj = args.Length () > 2
351
- && args[2 ]->IsObject ()
352
- ? args[2 ].As <Object>() : Local<Object>();
353
-
354
- TryCatch tryCatch (isolate);
355
-
356
- // Obtain runtime global context.
357
- Local<Context> runtimeContext = V8Runtime::GlobalContext ();
358
- Local<Object> runtimeGlobal = runtimeContext->Global ();
359
-
360
- // Create new context for script to run in.
361
- Local<Context> scriptContext = Context::New (isolate);
362
- Context::Scope scope (scriptContext);
363
-
364
- // Obtain script global context.
365
- Local<Object> scriptGlobal = scriptContext->Global ();
366
-
367
- // Set security token from previous context to access properties.
368
- scriptContext->SetSecurityToken (context->GetSecurityToken ());
369
-
370
- // Set runtime global properties in script global context.
371
- assign (runtimeGlobal, scriptGlobal);
372
-
373
- if (!contextObj.IsEmpty ()) {
374
-
375
- // Context object has been provided, set context properties in script global context.
376
- assign (contextObj, scriptGlobal);
377
- }
378
-
379
- ScriptOrigin origin (filename);
380
-
381
- // Compile script.
382
- MaybeLocal<Script> maybeScript = Script::Compile (scriptContext, code, &origin);
383
- if (tryCatch.HasCaught ()) {
384
- V8Util::openJSErrorDialog (isolate, tryCatch);
385
- V8Util::reportException (isolate, tryCatch, true );
386
- return ;
387
- }
388
- if (maybeScript.IsEmpty ()) {
389
- isolate->ThrowException (Exception::TypeError (STRING_NEW (isolate, " Failed to compile script." )));
390
- args.GetReturnValue ().Set (v8::Undefined (isolate));
391
- return ;
392
- }
393
- Local<Script> script = maybeScript.ToLocalChecked ();
394
-
395
- // Execute script.
396
- MaybeLocal<Value> maybeResult = script->Run (scriptContext);
397
- if (tryCatch.HasCaught ()) {
398
- V8Util::openJSErrorDialog (isolate, tryCatch);
399
- V8Util::reportException (isolate, tryCatch, true );
400
- return ;
401
- }
402
- if (maybeResult.IsEmpty ()) {
403
-
404
- // No result found, return undefined.
405
- args.GetReturnValue ().Set (v8::Undefined (isolate));
406
- return ;
407
- }
408
-
409
- args.GetReturnValue ().Set (maybeResult.ToLocalChecked ());
410
- }
411
311
}
0 commit comments