Commit 68aa1ab3 authored by Jochen Eisinger's avatar Jochen Eisinger Committed by Commit Bot

Update module APIs to return Maybe<bool>

All APIs that can throw exceptions should return Maybe<> values

BUG=none
R=neis@chromium.org,gsathya@chromium.org

Change-Id: I6a6e5888cd71257bb02bdcfcc587c909d0c1d8f4
Reviewed-on: https://chromium-review.googlesource.com/517785
Commit-Queue: Jochen Eisinger <jochen@chromium.org>
Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Reviewed-by: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#45557}
parent e011e7ef
...@@ -1032,7 +1032,6 @@ class ScriptOrigin { ...@@ -1032,7 +1032,6 @@ class ScriptOrigin {
Local<Value> source_map_url_; Local<Value> source_map_url_;
}; };
/** /**
* A compiled JavaScript script, not yet tied to a Context. * A compiled JavaScript script, not yet tied to a Context.
*/ */
...@@ -1095,10 +1094,14 @@ class V8_EXPORT Module { ...@@ -1095,10 +1094,14 @@ class V8_EXPORT Module {
/** /**
* ModuleDeclarationInstantiation * ModuleDeclarationInstantiation
* *
* Returns false if an exception occurred during instantiation. (In the case * Returns an empty Maybe<bool> if an exception occurred during
* where the callback throws an exception, that exception is propagated.) * instantiation. (In the case where the callback throws an exception, that
* exception is propagated.)
*/ */
V8_WARN_UNUSED_RESULT bool Instantiate(Local<Context> context, V8_DEPRECATE_SOON("Use Maybe<bool> version",
bool Instantiate(Local<Context> context,
ResolveCallback callback));
V8_WARN_UNUSED_RESULT Maybe<bool> InstantiateModule(Local<Context> context,
ResolveCallback callback); ResolveCallback callback);
/** /**
...@@ -1121,14 +1124,14 @@ class V8_EXPORT DynamicImportResult { ...@@ -1121,14 +1124,14 @@ class V8_EXPORT DynamicImportResult {
* Resolves the promise with the namespace object of the given * Resolves the promise with the namespace object of the given
* module. * module.
*/ */
V8_WARN_UNUSED_RESULT bool FinishDynamicImportSuccess(Local<Context> context, V8_WARN_UNUSED_RESULT Maybe<bool> FinishDynamicImportSuccess(
Local<Module> module); Local<Context> context, Local<Module> module);
/** /**
* Rejects the promise with the given exception. * Rejects the promise with the given exception.
*/ */
V8_WARN_UNUSED_RESULT bool FinishDynamicImportFailure(Local<Context> context, V8_WARN_UNUSED_RESULT Maybe<bool> FinishDynamicImportFailure(
Local<Value> exception); Local<Context> context, Local<Value> exception);
}; };
/** /**
......
...@@ -148,10 +148,6 @@ namespace v8 { ...@@ -148,10 +148,6 @@ namespace v8 {
PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \ PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
Nothing<T>(), i::HandleScope, false) Nothing<T>(), i::HandleScope, false)
#define PREPARE_FOR_EXECUTION_BOOL(context, class_name, function_name) \
PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
false, i::HandleScope, false)
#ifdef DEBUG #ifdef DEBUG
#define ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate) \ #define ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate) \
i::VMState<v8::OTHER> __state__((isolate)); \ i::VMState<v8::OTHER> __state__((isolate)); \
...@@ -185,9 +181,6 @@ namespace v8 { ...@@ -185,9 +181,6 @@ namespace v8 {
#define RETURN_ON_FAILED_EXECUTION_PRIMITIVE(T) \ #define RETURN_ON_FAILED_EXECUTION_PRIMITIVE(T) \
EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, Nothing<T>()) EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, Nothing<T>())
#define RETURN_ON_FAILED_EXECUTION_BOOL() \
EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, false)
#define RETURN_TO_LOCAL_UNCHECKED(maybe_local, T) \ #define RETURN_TO_LOCAL_UNCHECKED(maybe_local, T) \
return maybe_local.FromMaybe(Local<T>()); return maybe_local.FromMaybe(Local<T>());
...@@ -2059,9 +2052,10 @@ Local<UnboundScript> Script::GetUnboundScript() { ...@@ -2059,9 +2052,10 @@ Local<UnboundScript> Script::GetUnboundScript() {
i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared())); i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared()));
} }
bool DynamicImportResult::FinishDynamicImportSuccess(Local<Context> context, Maybe<bool> DynamicImportResult::FinishDynamicImportSuccess(
Local<Module> module) { Local<Context> context, Local<Module> module) {
PREPARE_FOR_EXECUTION_BOOL(context, Module, FinishDynamicImportSuccess); PREPARE_FOR_EXECUTION_PRIMITIVE(context, Module, FinishDynamicImportSuccess,
bool);
auto promise = Utils::OpenHandle(this); auto promise = Utils::OpenHandle(this);
i::Handle<i::Module> module_obj = Utils::OpenHandle(*module); i::Handle<i::Module> module_obj = Utils::OpenHandle(*module);
i::Handle<i::JSModuleNamespace> module_namespace = i::Handle<i::JSModuleNamespace> module_namespace =
...@@ -2072,13 +2066,14 @@ bool DynamicImportResult::FinishDynamicImportSuccess(Local<Context> context, ...@@ -2072,13 +2066,14 @@ bool DynamicImportResult::FinishDynamicImportSuccess(Local<Context> context,
isolate->factory()->undefined_value(), arraysize(argv), isolate->factory()->undefined_value(), arraysize(argv),
argv) argv)
.is_null(); .is_null();
RETURN_ON_FAILED_EXECUTION_BOOL(); RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
return true; return Just(true);
} }
bool DynamicImportResult::FinishDynamicImportFailure(Local<Context> context, Maybe<bool> DynamicImportResult::FinishDynamicImportFailure(
Local<Value> exception) { Local<Context> context, Local<Value> exception) {
PREPARE_FOR_EXECUTION_BOOL(context, Module, FinishDynamicImportFailure); PREPARE_FOR_EXECUTION_PRIMITIVE(context, Module, FinishDynamicImportFailure,
bool);
auto promise = Utils::OpenHandle(this); auto promise = Utils::OpenHandle(this);
// We pass true to trigger the debugger's on exception handler. // We pass true to trigger the debugger's on exception handler.
i::Handle<i::Object> argv[] = {promise, Utils::OpenHandle(*exception), i::Handle<i::Object> argv[] = {promise, Utils::OpenHandle(*exception),
...@@ -2088,8 +2083,8 @@ bool DynamicImportResult::FinishDynamicImportFailure(Local<Context> context, ...@@ -2088,8 +2083,8 @@ bool DynamicImportResult::FinishDynamicImportFailure(Local<Context> context,
isolate->factory()->undefined_value(), arraysize(argv), isolate->factory()->undefined_value(), arraysize(argv),
argv) argv)
.is_null(); .is_null();
RETURN_ON_FAILED_EXECUTION_BOOL(); RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
return true; return Just(true);
} }
int Module::GetModuleRequestsLength() const { int Module::GetModuleRequestsLength() const {
...@@ -2111,11 +2106,16 @@ int Module::GetIdentityHash() const { return Utils::OpenHandle(this)->hash(); } ...@@ -2111,11 +2106,16 @@ int Module::GetIdentityHash() const { return Utils::OpenHandle(this)->hash(); }
bool Module::Instantiate(Local<Context> context, bool Module::Instantiate(Local<Context> context,
Module::ResolveCallback callback) { Module::ResolveCallback callback) {
PREPARE_FOR_EXECUTION_BOOL(context, Module, Instantiate); return InstantiateModule(context, callback).FromMaybe(false);
}
Maybe<bool> Module::InstantiateModule(Local<Context> context,
Module::ResolveCallback callback) {
PREPARE_FOR_EXECUTION_PRIMITIVE(context, Module, InstantiateModule, bool);
has_pending_exception = has_pending_exception =
!i::Module::Instantiate(Utils::OpenHandle(this), context, callback); !i::Module::Instantiate(Utils::OpenHandle(this), context, callback);
RETURN_ON_FAILED_EXECUTION_BOOL(); RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
return true; return Just(true);
} }
MaybeLocal<Value> Module::Evaluate(Local<Context> context) { MaybeLocal<Value> Module::Evaluate(Local<Context> context) {
......
...@@ -621,7 +621,7 @@ class RuntimeCallTimer final { ...@@ -621,7 +621,7 @@ class RuntimeCallTimer final {
V(Module_FinishDynamicImportSuccess) \ V(Module_FinishDynamicImportSuccess) \
V(Module_FinishDynamicImportFailure) \ V(Module_FinishDynamicImportFailure) \
V(Module_Evaluate) \ V(Module_Evaluate) \
V(Module_Instantiate) \ V(Module_InstantiateModule) \
V(NumberObject_New) \ V(NumberObject_New) \
V(NumberObject_NumberValue) \ V(NumberObject_NumberValue) \
V(Object_CallAsConstructor) \ V(Object_CallAsConstructor) \
......
...@@ -826,12 +826,14 @@ void Shell::DoHostImportModuleDynamically(void* import_data) { ...@@ -826,12 +826,14 @@ void Shell::DoHostImportModuleDynamically(void* import_data) {
root_module = module_it->second.Get(isolate); root_module = module_it->second.Get(isolate);
} else if (!FetchModuleTree(realm, absolute_path).ToLocal(&root_module)) { } else if (!FetchModuleTree(realm, absolute_path).ToLocal(&root_module)) {
CHECK(try_catch.HasCaught()); CHECK(try_catch.HasCaught());
CHECK(result->FinishDynamicImportFailure(realm, try_catch.Exception())); CHECK(result->FinishDynamicImportFailure(realm, try_catch.Exception())
.FromJust());
return; return;
} }
MaybeLocal<Value> maybe_result; MaybeLocal<Value> maybe_result;
if (root_module->Instantiate(realm, ResolveModuleCallback)) { if (root_module->InstantiateModule(realm, ResolveModuleCallback)
.FromMaybe(false)) {
maybe_result = root_module->Evaluate(realm); maybe_result = root_module->Evaluate(realm);
EmptyMessageQueues(isolate); EmptyMessageQueues(isolate);
} }
...@@ -839,12 +841,13 @@ void Shell::DoHostImportModuleDynamically(void* import_data) { ...@@ -839,12 +841,13 @@ void Shell::DoHostImportModuleDynamically(void* import_data) {
Local<Value> module; Local<Value> module;
if (!maybe_result.ToLocal(&module)) { if (!maybe_result.ToLocal(&module)) {
DCHECK(try_catch.HasCaught()); DCHECK(try_catch.HasCaught());
CHECK(result->FinishDynamicImportFailure(realm, try_catch.Exception())); CHECK(result->FinishDynamicImportFailure(realm, try_catch.Exception())
.FromJust());
return; return;
} }
DCHECK(!try_catch.HasCaught()); DCHECK(!try_catch.HasCaught());
CHECK(result->FinishDynamicImportSuccess(realm, root_module)); CHECK(result->FinishDynamicImportSuccess(realm, root_module).FromJust());
} }
bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) { bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) {
...@@ -869,7 +872,8 @@ bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) { ...@@ -869,7 +872,8 @@ bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) {
} }
MaybeLocal<Value> maybe_result; MaybeLocal<Value> maybe_result;
if (root_module->Instantiate(realm, ResolveModuleCallback)) { if (root_module->InstantiateModule(realm, ResolveModuleCallback)
.FromMaybe(false)) {
maybe_result = root_module->Evaluate(realm); maybe_result = root_module->Evaluate(realm);
EmptyMessageQueues(isolate); EmptyMessageQueues(isolate);
} }
......
...@@ -3353,9 +3353,11 @@ void Isolate::RunHostImportModuleDynamicallyCallback( ...@@ -3353,9 +3353,11 @@ void Isolate::RunHostImportModuleDynamicallyCallback(
if (host_import_module_dynamically_callback_ == nullptr) { if (host_import_module_dynamically_callback_ == nullptr) {
Handle<Object> exception = Handle<Object> exception =
factory()->NewError(error_function(), MessageTemplate::kUnsupported); factory()->NewError(error_function(), MessageTemplate::kUnsupported);
CHECK(result->FinishDynamicImportFailure( CHECK(result
->FinishDynamicImportFailure(
v8::Utils::ToLocal(handle(context(), this)), v8::Utils::ToLocal(handle(context(), this)),
v8::Utils::ToLocal(exception))); v8::Utils::ToLocal(exception))
.FromJust());
return; return;
} }
......
...@@ -70,7 +70,8 @@ TEST(ModuleInstantiationFailures) { ...@@ -70,7 +70,8 @@ TEST(ModuleInstantiationFailures) {
// Instantiation should fail. // Instantiation should fail.
{ {
v8::TryCatch inner_try_catch(isolate); v8::TryCatch inner_try_catch(isolate);
CHECK(!module->Instantiate(env.local(), FailAlwaysResolveCallback)); CHECK(module->InstantiateModule(env.local(), FailAlwaysResolveCallback)
.IsNothing());
CHECK(inner_try_catch.HasCaught()); CHECK(inner_try_catch.HasCaught());
CHECK(inner_try_catch.Exception()->StrictEquals(v8_str("boom"))); CHECK(inner_try_catch.Exception()->StrictEquals(v8_str("boom")));
} }
...@@ -82,7 +83,9 @@ TEST(ModuleInstantiationFailures) { ...@@ -82,7 +83,9 @@ TEST(ModuleInstantiationFailures) {
g_count = 0; g_count = 0;
{ {
v8::TryCatch inner_try_catch(isolate); v8::TryCatch inner_try_catch(isolate);
CHECK(!module->Instantiate(env.local(), FailOnSecondCallResolveCallback)); CHECK(
module->InstantiateModule(env.local(), FailOnSecondCallResolveCallback)
.IsNothing());
CHECK(inner_try_catch.HasCaught()); CHECK(inner_try_catch.HasCaught());
CHECK(inner_try_catch.Exception()->StrictEquals(v8_str("booom"))); CHECK(inner_try_catch.Exception()->StrictEquals(v8_str("booom")));
} }
...@@ -111,8 +114,10 @@ TEST(ModuleEvaluation) { ...@@ -111,8 +114,10 @@ TEST(ModuleEvaluation) {
ScriptCompiler::Source source(source_text, origin); ScriptCompiler::Source source(source_text, origin);
Local<Module> module = Local<Module> module =
ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked(); ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
CHECK(module->Instantiate(env.local(), CHECK(module
CompileSpecifierAsModuleResolveCallback)); ->InstantiateModule(env.local(),
CompileSpecifierAsModuleResolveCallback)
.FromJust());
CHECK(!module->Evaluate(env.local()).IsEmpty()); CHECK(!module->Evaluate(env.local()).IsEmpty());
ExpectInt32("Object.expando", 10); ExpectInt32("Object.expando", 10);
...@@ -151,8 +156,10 @@ TEST(ModuleEvaluationCompletion1) { ...@@ -151,8 +156,10 @@ TEST(ModuleEvaluationCompletion1) {
ScriptCompiler::Source source(source_text, origin); ScriptCompiler::Source source(source_text, origin);
Local<Module> module = Local<Module> module =
ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked(); ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
CHECK(module->Instantiate(env.local(), CHECK(module
CompileSpecifierAsModuleResolveCallback)); ->InstantiateModule(env.local(),
CompileSpecifierAsModuleResolveCallback)
.FromJust());
CHECK(module->Evaluate(env.local()).ToLocalChecked()->IsUndefined()); CHECK(module->Evaluate(env.local()).ToLocalChecked()->IsUndefined());
} }
...@@ -190,8 +197,10 @@ TEST(ModuleEvaluationCompletion2) { ...@@ -190,8 +197,10 @@ TEST(ModuleEvaluationCompletion2) {
ScriptCompiler::Source source(source_text, origin); ScriptCompiler::Source source(source_text, origin);
Local<Module> module = Local<Module> module =
ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked(); ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
CHECK(module->Instantiate(env.local(), CHECK(module
CompileSpecifierAsModuleResolveCallback)); ->InstantiateModule(env.local(),
CompileSpecifierAsModuleResolveCallback)
.FromJust());
CHECK(module->Evaluate(env.local()) CHECK(module->Evaluate(env.local())
.ToLocalChecked() .ToLocalChecked()
->StrictEquals(v8_str("gaga"))); ->StrictEquals(v8_str("gaga")));
......
...@@ -100,8 +100,10 @@ void IsolateData::RegisterModule(v8::Local<v8::Context> context, ...@@ -100,8 +100,10 @@ void IsolateData::RegisterModule(v8::Local<v8::Context> context,
v8::Local<v8::Module> module; v8::Local<v8::Module> module;
if (!v8::ScriptCompiler::CompileModule(isolate(), source).ToLocal(&module)) if (!v8::ScriptCompiler::CompileModule(isolate(), source).ToLocal(&module))
return; return;
if (!module->Instantiate(context, &IsolateData::ModuleResolveCallback)) if (!module->InstantiateModule(context, &IsolateData::ModuleResolveCallback)
.FromMaybe(false)) {
return; return;
}
v8::Local<v8::Value> result; v8::Local<v8::Value> result;
if (!module->Evaluate(context).ToLocal(&result)) return; if (!module->Evaluate(context).ToLocal(&result)) return;
modules_[name] = v8::Global<v8::Module>(isolate_, module); modules_[name] = v8::Global<v8::Module>(isolate_, module);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment