summaryrefslogtreecommitdiff
path: root/core/bindings.rs
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2023-02-07 20:22:46 +0100
committerGitHub <noreply@github.com>2023-02-07 20:22:46 +0100
commitb4aa1530970f7b9cc4e6f2f27e077852c4e178d3 (patch)
tree3d008912affe8550692183bd2697a386db5e3c79 /core/bindings.rs
parent65500f36e870b4ada3996b06aa287e30177d21a3 (diff)
refactor: Use ES modules for internal runtime code (#17648)
This PR refactors all internal js files (except core) to be written as ES modules. `__bootstrap`has been mostly replaced with static imports in form in `internal:[path to file from repo root]`. To specify if files are ESM, an `esm` method has been added to `Extension`, similar to the `js` method. A new ModuleLoader called `InternalModuleLoader` has been added to enable the loading of internal specifiers, which is used in all situations except when a snapshot is only loaded, and not a new one is created from it. --------- Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'core/bindings.rs')
-rw-r--r--core/bindings.rs72
1 files changed, 41 insertions, 31 deletions
diff --git a/core/bindings.rs b/core/bindings.rs
index d5f38d3c2..dce97000c 100644
--- a/core/bindings.rs
+++ b/core/bindings.rs
@@ -267,45 +267,47 @@ pub fn host_import_module_dynamically_callback<'s>(
.unwrap()
.to_rust_string_lossy(scope);
+ let is_internal_module = specifier_str.starts_with("internal:");
let resolver = v8::PromiseResolver::new(scope).unwrap();
let promise = resolver.get_promise(scope);
- let assertions = parse_import_assertions(
- scope,
- import_assertions,
- ImportAssertionsKind::DynamicImport,
- );
+ if !is_internal_module {
+ let assertions = parse_import_assertions(
+ scope,
+ import_assertions,
+ ImportAssertionsKind::DynamicImport,
+ );
- {
- let tc_scope = &mut v8::TryCatch::new(scope);
- validate_import_assertions(tc_scope, &assertions);
- if tc_scope.has_caught() {
- let e = tc_scope.exception().unwrap();
- resolver.reject(tc_scope, e);
+ {
+ let tc_scope = &mut v8::TryCatch::new(scope);
+ validate_import_assertions(tc_scope, &assertions);
+ if tc_scope.has_caught() {
+ let e = tc_scope.exception().unwrap();
+ resolver.reject(tc_scope, e);
+ }
}
- }
- let asserted_module_type =
- get_asserted_module_type_from_assertions(&assertions);
+ let asserted_module_type =
+ get_asserted_module_type_from_assertions(&assertions);
- let resolver_handle = v8::Global::new(scope, resolver);
- {
- let state_rc = JsRuntime::state(scope);
- let module_map_rc = JsRuntime::module_map(scope);
+ let resolver_handle = v8::Global::new(scope, resolver);
+ {
+ let state_rc = JsRuntime::state(scope);
+ let module_map_rc = JsRuntime::module_map(scope);
- debug!(
- "dyn_import specifier {} referrer {} ",
- specifier_str, referrer_name_str
- );
- ModuleMap::load_dynamic_import(
- module_map_rc,
- &specifier_str,
- &referrer_name_str,
- asserted_module_type,
- resolver_handle,
- );
- state_rc.borrow_mut().notify_new_dynamic_import();
+ debug!(
+ "dyn_import specifier {} referrer {} ",
+ specifier_str, referrer_name_str
+ );
+ ModuleMap::load_dynamic_import(
+ module_map_rc,
+ &specifier_str,
+ &referrer_name_str,
+ asserted_module_type,
+ resolver_handle,
+ );
+ state_rc.borrow_mut().notify_new_dynamic_import();
+ }
}
-
// Map errors from module resolution (not JS errors from module execution) to
// ones rethrown from this scope, so they include the call stack of the
// dynamic import site. Error objects without any stack frames are assumed to
@@ -317,6 +319,14 @@ pub fn host_import_module_dynamically_callback<'s>(
let promise = promise.catch(scope, map_err).unwrap();
+ if is_internal_module {
+ let message =
+ v8::String::new(scope, "Cannot load internal module from external code")
+ .unwrap();
+ let exception = v8::Exception::type_error(scope, message);
+ resolver.reject(scope, exception);
+ }
+
Some(promise)
}