From f9b167deb07a650590b7f1eef8fe86bf9e22d211 Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Fri, 18 Jan 2019 12:15:09 -0800 Subject: [PATCH] Avoid crashes on ES module resolution when module not found (#1546) --- libdeno/binding.cc | 8 +++++++- src/isolate.rs | 11 +++++++++-- tests/error_009_missing_js_module.js | 1 + tests/error_009_missing_js_module.js.out | 1 + tests/error_009_missing_js_module.test | 4 ++++ 5 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/error_009_missing_js_module.js create mode 100644 tests/error_009_missing_js_module.js.out create mode 100644 tests/error_009_missing_js_module.test diff --git a/libdeno/binding.cc b/libdeno/binding.cc index d292f2638d..0005293040 100644 --- a/libdeno/binding.cc +++ b/libdeno/binding.cc @@ -558,7 +558,11 @@ v8::MaybeLocal ResolveCallback(v8::Local context, if (d->resolve_module_.IsEmpty()) { // Resolution Error. - isolate->ThrowException(v8_str("module resolution error")); + std::stringstream err_ss; + err_ss << "NotFound: Cannot resolve module \"" << specifier_c + << "\" from \"" << referrer_filename << "\""; + auto resolve_error = v8_str(err_ss.str().c_str()); + isolate->ThrowException(resolve_error); return v8::MaybeLocal(); } else { auto module = d->resolve_module_.Get(isolate); @@ -612,6 +616,8 @@ bool ExecuteMod(v8::Local context, const char* js_filename, auto module = maybe_module.ToLocalChecked(); auto maybe_ok = module->InstantiateModule(context, ResolveCallback); if (maybe_ok.IsNothing()) { + DCHECK(try_catch.HasCaught()); + HandleException(context, try_catch.Exception()); return false; } diff --git a/src/isolate.rs b/src/isolate.rs index bbd52c64d6..5100ac4907 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -403,8 +403,15 @@ extern "C" fn resolve_cb( debug!("module_resolve callback {} {}", specifier, referrer); let isolate = unsafe { Isolate::from_raw_ptr(user_data) }; - let out = - code_fetch_and_maybe_compile(&isolate.state, specifier, referrer).unwrap(); + let maybe_out = + code_fetch_and_maybe_compile(&isolate.state, specifier, referrer); + + if maybe_out.is_err() { + // Resolution failure + return; + } + + let out = maybe_out.unwrap(); let filename = CString::new(out.filename.clone()).unwrap(); let filename_ptr = filename.as_ptr() as *const i8; diff --git a/tests/error_009_missing_js_module.js b/tests/error_009_missing_js_module.js new file mode 100644 index 0000000000..e6ca88934b --- /dev/null +++ b/tests/error_009_missing_js_module.js @@ -0,0 +1 @@ +import "./bad-module.js"; diff --git a/tests/error_009_missing_js_module.js.out b/tests/error_009_missing_js_module.js.out new file mode 100644 index 0000000000..1bcd9099ed --- /dev/null +++ b/tests/error_009_missing_js_module.js.out @@ -0,0 +1 @@ +NotFound: Cannot resolve module "./bad-module.js" from "[WILDCARD]/tests/error_009_missing_js_module.js" diff --git a/tests/error_009_missing_js_module.test b/tests/error_009_missing_js_module.test new file mode 100644 index 0000000000..b16bb232b5 --- /dev/null +++ b/tests/error_009_missing_js_module.test @@ -0,0 +1,4 @@ +args: tests/error_009_missing_js_module.js +check_stderr: true +exit_code: 1 +output: tests/error_009_missing_js_module.js.out \ No newline at end of file