From 7cda7c315e1bf1bf14e1b7443935a6235bb96035 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 3 Nov 2021 21:27:28 +0900 Subject: [PATCH] fix --- .../testdata/compat/dyn_import_reject.out | 2 +- core/runtime.rs | 35 +++++++++++-------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/cli/tests/testdata/compat/dyn_import_reject.out b/cli/tests/testdata/compat/dyn_import_reject.out index 4da17924524306..510957829581ea 100644 --- a/cli/tests/testdata/compat/dyn_import_reject.out +++ b/cli/tests/testdata/compat/dyn_import_reject.out @@ -1,2 +1,2 @@ -Error: Cannot load module "[WILDCARD]/testdata/compat/foobar.js". +Error: Cannot load module "file:///[WILDCARD]/testdata/compat/foobar.js". ERR_MODULE_NOT_FOUND diff --git a/core/runtime.rs b/core/runtime.rs index c0210d2f8d2e91..7df753d9857231 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -561,7 +561,14 @@ impl JsRuntime { source_code: &str, ) -> Result, AnyError> { let scope = &mut self.handle_scope(); + Self::execute_script_with_scope(scope, name, source_code) + } + fn execute_script_with_scope( + scope: &mut v8::HandleScope, + name: &str, + source_code: &str, + ) -> Result, AnyError> { let source = v8::String::new(scope, source_code).unwrap(); let name = v8::String::new(scope, name).unwrap(); let origin = bindings::script_origin(scope, name); @@ -1133,9 +1140,7 @@ impl JsRuntime { fn dynamic_import_reject(&mut self, id: ModuleLoadId, err: AnyError) { let module_map_rc = Self::module_map(self.v8_isolate()); let get_error_class_fn = self.op_state().borrow().get_error_class_fn; - let err_class = get_error_class_fn(&err); - let err_class = self.execute_script("", err_class).ok(); - let mut scope = &mut self.handle_scope(); + let scope = &mut self.handle_scope(); let resolver_handle = module_map_rc .borrow_mut() @@ -1144,22 +1149,24 @@ impl JsRuntime { .expect("Invalid dynamic import id"); let resolver = resolver_handle.open(scope); - let exception: v8::Local = err + let exception = err .downcast_ref::() .map(|err| err.get_handle(scope)) .unwrap_or_else(|| { let message = err.to_string(); - let message = v8::String::new(scope, &message).unwrap(); - let err_class = - v8::Local::::new(&mut scope, err_class.unwrap()); - let maybe_err_class = - v8::Local::::try_from(err_class).ok(); - if let Some(err_class) = maybe_err_class { - let err = err_class.new_instance(scope, &[message.into()]).unwrap(); - err.into() - } else { - v8::Exception::type_error(scope, message) + let message = v8::String::new(scope, message).unwrap(); + if let Ok(err_class) = Self::execute_script_with_scope( + scope, + "", + get_error_class_fn(&err), + ) { + if let Ok(err_class) = v8::Local::::try_from( + v8::Local::::new(scope, err_class), + ) { + return err_class.new_instance(scope, &[message.into()]).unwrap().into(); + } } + v8::Exception::type_error(scope, message) }); // IMPORTANT: No borrows to `ModuleMap` can be held at this point because