Skip to content

Commit

Permalink
compat: switch dyn import error class
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Nov 3, 2021
1 parent 8e31bbb commit 22b06f8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 5 deletions.
14 changes: 12 additions & 2 deletions cli/proc_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,24 @@ impl ProcState {
} else {
if maybe_referrer.is_some() && !is_dynamic {
if let Some(span) = graph_data.resolved_map.get(&specifier) {
let error_class = if self.flags.compat {
"Deno.compat.errors.ERR_MODULE_NOT_FOUND"
} else {
"TypeError"
};
return Err(custom_error(
"NotFound",
error_class,
format!("Cannot load module \"{}\".\n at {}", specifier, span),
));
}
}
let error_class = if self.flags.compat {
"Deno.compat.errors.ERR_MODULE_NOT_FOUND"
} else {
"TypeError"
};
return Err(custom_error(
"NotFound",
error_class,
format!("Cannot load module \"{}\".", specifier),
));
}
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/integration/compat_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ itest!(compat_with_import_map_and_https_imports {
output: "compat/import_map_https_imports.out",
});

itest!(compat_dyn_import_rejects_with_node_compatible_error {
args: "run --quiet --compat --unstable -A compat/dyn_import_reject.js",
output: "compat/dyn_import_reject.out",
});

#[test]
fn globals_in_repl() {
let (out, _err) = util::run_and_collect_output_with_args(
Expand Down
4 changes: 4 additions & 0 deletions cli/tests/testdata/compat/dyn_import_reject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import("./foobar.js").catch((e) => {
console.log(e);
console.log(e.code);
});
2 changes: 2 additions & 0 deletions cli/tests/testdata/compat/dyn_import_reject.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Error: Cannot load module "[WILDCARD]/testdata/compat/foobar.js".
ERR_MODULE_NOT_FOUND
18 changes: 15 additions & 3 deletions core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,10 @@ impl JsRuntime {

fn dynamic_import_reject(&mut self, id: ModuleLoadId, err: AnyError) {
let module_map_rc = Self::module_map(self.v8_isolate());
let scope = &mut self.handle_scope();
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("<anonymous>", err_class).ok();
let mut scope = &mut self.handle_scope();

let resolver_handle = module_map_rc
.borrow_mut()
Expand All @@ -1141,13 +1144,22 @@ impl JsRuntime {
.expect("Invalid dynamic import id");
let resolver = resolver_handle.open(scope);

let exception = err
let exception: v8::Local<v8::Value> = err
.downcast_ref::<ErrWithV8Handle>()
.map(|err| err.get_handle(scope))
.unwrap_or_else(|| {
let message = err.to_string();
let message = v8::String::new(scope, &message).unwrap();
v8::Exception::type_error(scope, message)
let err_class =
v8::Local::<v8::Value>::new(&mut scope, err_class.unwrap());
let maybe_err_class =
v8::Local::<v8::Function>::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)
}
});

// IMPORTANT: No borrows to `ModuleMap` can be held at this point because
Expand Down
23 changes: 23 additions & 0 deletions runtime/js/07_compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
"use strict";

((window) => {
const {
Error,
} = window.__bootstrap.primordials;

class ERR_MODULE_NOT_FOUND extends Error {
constructor(msg) {
super(msg);
this.code = "ERR_MODULE_NOT_FOUND";
}
}

const errors = {
ERR_MODULE_NOT_FOUND,
};

window.__bootstrap.compat = {
errors,
};
})(this);
1 change: 1 addition & 0 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,6 @@
flockSync: __bootstrap.fs.flockSync,
funlock: __bootstrap.fs.funlock,
funlockSync: __bootstrap.fs.funlockSync,
compat: __bootstrap.compat,
};
})(this);

0 comments on commit 22b06f8

Please sign in to comment.