summaryrefslogtreecommitdiff
path: root/core/runtime/bindings.rs
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-06-14 10:45:59 -0600
committerGitHub <noreply@github.com>2023-06-14 16:45:59 +0000
commit88e6e9c1e6263f326d30b7fcc33465e0428faf33 (patch)
tree956e60abea884d89e4c76cc792e2b3159317278e /core/runtime/bindings.rs
parentcd27757184e683575c2540f73a96dc96e268943a (diff)
refactor(core): some runtime methods should live on the module map (#19502)
A few easy migrations of module code from the runtime to the module map. The module map already has a few places where it needs a handle scope, so we're not coupling it any further with the v8 runtime. `init_runtime_module_map` is replaced with an option to reduce API surface of JsRuntime. `module_resolve_callback` now lives in the `ModuleMap` and we use a annex data to avoid having to go through the `Rc<RefCell<...>>` stored in the `JsRuntime`'s isolate.
Diffstat (limited to 'core/runtime/bindings.rs')
-rw-r--r--core/runtime/bindings.rs55
1 files changed, 1 insertions, 54 deletions
diff --git a/core/runtime/bindings.rs b/core/runtime/bindings.rs
index 4cc27592f..2eb804e73 100644
--- a/core/runtime/bindings.rs
+++ b/core/runtime/bindings.rs
@@ -7,6 +7,7 @@ use std::os::raw::c_void;
use v8::MapFnTo;
use crate::error::is_instance_of_error;
+use crate::error::throw_type_error;
use crate::error::JsStackFrame;
use crate::modules::get_asserted_module_type_from_assertions;
use crate::modules::parse_import_assertions;
@@ -542,57 +543,3 @@ fn call_console(
inspector_console_method.call(scope, receiver.into(), &call_args);
deno_console_method.call(scope, receiver.into(), &call_args);
}
-
-/// Called by V8 during `JsRuntime::instantiate_module`.
-///
-/// This function borrows `ModuleMap` from the isolate slot,
-/// so it is crucial to ensure there are no existing borrows
-/// of `ModuleMap` when `JsRuntime::instantiate_module` is called.
-pub fn module_resolve_callback<'s>(
- context: v8::Local<'s, v8::Context>,
- specifier: v8::Local<'s, v8::String>,
- import_assertions: v8::Local<'s, v8::FixedArray>,
- referrer: v8::Local<'s, v8::Module>,
-) -> Option<v8::Local<'s, v8::Module>> {
- // SAFETY: `CallbackScope` can be safely constructed from `Local<Context>`
- let scope = &mut unsafe { v8::CallbackScope::new(context) };
-
- let module_map_rc = JsRuntime::module_map_from(scope);
- let module_map = module_map_rc.borrow();
-
- let referrer_global = v8::Global::new(scope, referrer);
-
- let referrer_info = module_map
- .get_info(&referrer_global)
- .expect("ModuleInfo not found");
- let referrer_name = referrer_info.name.as_str();
-
- let specifier_str = specifier.to_rust_string_lossy(scope);
-
- let assertions = parse_import_assertions(
- scope,
- import_assertions,
- ImportAssertionsKind::StaticImport,
- );
- let maybe_module = module_map.resolve_callback(
- scope,
- &specifier_str,
- referrer_name,
- assertions,
- );
- if let Some(module) = maybe_module {
- return Some(module);
- }
-
- let msg = format!(
- r#"Cannot resolve module "{specifier_str}" from "{referrer_name}""#
- );
- throw_type_error(scope, msg);
- None
-}
-
-pub fn throw_type_error(scope: &mut v8::HandleScope, message: impl AsRef<str>) {
- let message = v8::String::new(scope, message.as_ref()).unwrap();
- let exception = v8::Exception::type_error(scope, message);
- scope.throw_exception(exception);
-}