Skip to content

Commit 98507d8

Browse files
committed
fix(android): cleanup
1 parent ab61c00 commit 98507d8

File tree

2 files changed

+21
-122
lines changed

2 files changed

+21
-122
lines changed

Diff for: android/runtime/v8/src/native/modules/EvaluateModule.cpp

+20-120
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace titanium {
2020
Persistent<String> EvaluateModule::REQUIRE_STRING;
2121
Persistent<String> EvaluateModule::MODULE_REF_STRING;
2222

23-
std::vector<Persistent<Context, CopyablePersistentTraits<Context>>> EvaluateModule::moduleContexts;
23+
std::vector<Persistent<Context, CopyablePersistentTraits<Context>>> EvaluateModule::module_contexts;
2424

2525
static void assign(Local<Object> source, Local<Data> destination)
2626
{
@@ -65,15 +65,14 @@ namespace titanium {
6565
}
6666

6767
SetMethod(context, isolate, target, "runAsModule", EvaluateModule::RunAsModule);
68-
SetMethod(context, isolate, target, "runAsScript", EvaluateModule::RunAsScript);
6968
}
7069

7170
void EvaluateModule::GlobalSetterCallback(Local<String> key, Local<Value> value, const PropertyCallbackInfo<Value>& info)
7271
{
7372
Isolate* isolate = info.GetIsolate();
7473

7574
// Iterate through current module contexts.
76-
for (const auto persistentModuleContext : moduleContexts) {
75+
for (const auto persistentModuleContext : EvaluateModule::module_contexts) {
7776

7877
if (!persistentModuleContext.IsEmpty()) {
7978

@@ -172,8 +171,7 @@ namespace titanium {
172171
}
173172

174173
if (tryCatch.HasCaught()) {
175-
V8Util::openJSErrorDialog(isolate, tryCatch);
176-
V8Util::reportException(isolate, tryCatch, true);
174+
tryCatch.ReThrow();
177175
}
178176

179177
return Undefined(isolate);
@@ -190,24 +188,20 @@ namespace titanium {
190188
void EvaluateModule::RunAsModule(const FunctionCallbackInfo<Value>& args)
191189
{
192190
Isolate* isolate = args.GetIsolate();
193-
Local<Context> context = isolate->GetCurrentContext();
194191

195192
if (args.Length() == 0) {
196193
isolate->ThrowException(
197-
Exception::TypeError(
194+
Exception::SyntaxError(
198195
STRING_NEW(isolate, "Missing arguments, requires at least 'code' argument.")));
199196
return;
200197
}
201198

202199
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()
205201
? 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()
208203
? args[2].As<Object>() : Local<Object>();
209204

210-
TryCatch tryCatch(isolate);
211205
ScriptOrigin origin(filename,
212206
Local<Integer>(),
213207
Local<Integer>(),
@@ -219,12 +213,16 @@ namespace titanium {
219213
Boolean::New(isolate, true));
220214
ScriptCompiler::Source source(code, origin);
221215

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+
}
228226
}
229227
if (maybeModule.IsEmpty()) {
230228
args.GetReturnValue().Set(v8::Undefined(isolate));
@@ -246,7 +244,7 @@ namespace titanium {
246244
Persistent<Context> persistentModuleContext;
247245
persistentModuleContext.Reset(isolate, moduleContext);
248246
persistentModuleContext.SetWeak();
249-
moduleContexts.emplace_back(persistentModuleContext);
247+
EvaluateModule::module_contexts.emplace_back(persistentModuleContext);
250248

251249
// Obtain module global context.
252250
Local<Object> moduleGlobal = moduleContext->Global();
@@ -265,30 +263,20 @@ namespace titanium {
265263
}
266264

267265
// Instantiate module and process imports via `ModuleCallback`.
268-
module->InstantiateModule(moduleContext, ModuleCallback).FromJust();
269-
266+
module->InstantiateModule(moduleContext, ModuleCallback);
270267
if (module->GetStatus() == Module::kErrored) {
271268

272269
// Throw any exceptions from module instantiation.
273270
isolate->ThrowException(module->GetException());
274-
}
275-
if (tryCatch.HasCaught()) {
276-
V8Util::openJSErrorDialog(isolate, tryCatch);
277-
V8Util::reportException(isolate, tryCatch, true);
278271
return;
279272
}
280273

281274
// Execute module, obtaining a result.
282275
MaybeLocal<Value> maybeResult = module->Evaluate(moduleContext);
283-
284276
if (module->GetStatus() == Module::kErrored) {
285277

286278
// Throw any exceptions from module evaluation.
287279
isolate->ThrowException(module->GetException());
288-
}
289-
if (tryCatch.HasCaught()) {
290-
V8Util::openJSErrorDialog(isolate, tryCatch);
291-
V8Util::reportException(isolate, tryCatch, true);
292280
return;
293281
}
294282
if (maybeResult.IsEmpty()) {
@@ -307,20 +295,10 @@ namespace titanium {
307295

308296
// When top-level-await is enabled, modules return a `Promise`.
309297
// Wait for the promise to fulfill before obtaining module exports.
310-
if (promise->State() == Promise::kPending) {
298+
while (promise->State() == Promise::kPending) {
311299

312300
// 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();
324302
}
325303

326304
// Obtain module exports as result.
@@ -330,82 +308,4 @@ namespace titanium {
330308
// Return result.
331309
args.GetReturnValue().Set(result);
332310
}
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-
}
411311
}

Diff for: android/runtime/v8/src/native/modules/EvaluateModule.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ namespace titanium {
2020
static MaybeLocal<Module> ModuleCallback(Local<Context> context, Local<String> specifier, Local<Module> referrer);
2121

2222
static void RunAsModule(const FunctionCallbackInfo<Value> &args);
23-
static void RunAsScript(const FunctionCallbackInfo<Value> &args);
2423

2524
private:
2625
static Persistent<String> DEFAULT_STRING;
2726
static Persistent<String> REQUIRE_STRING;
2827
static Persistent<String> MODULE_REF_STRING;
2928

30-
static std::vector<Persistent<Context, CopyablePersistentTraits<Context>>> moduleContexts;
29+
static std::vector<Persistent<Context, CopyablePersistentTraits<Context>>> module_contexts;
3130
};
3231
}
3332
#endif

0 commit comments

Comments
 (0)