Skip to content

Commit

Permalink
Handle dyn imports exceeding call stack size (#1058)
Browse files Browse the repository at this point in the history
Fixes #1050
  • Loading branch information
littledivy authored Jan 24, 2025
1 parent f94773e commit cce028f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
21 changes: 21 additions & 0 deletions core/modules/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,27 @@ async fn dyn_import_err() {
.await;
}

#[tokio::test]
async fn dyn_import_recurse_err() {
let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::default()));
let mut runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(loader.clone()),
..Default::default()
});

runtime
.execute_script(
"file:///dyn_import2.js",
r#"
function bar(v) {
bar(import(0));
}
bar("foo");
"#,
)
.expect_err("should throw range error");
}

#[tokio::test]
async fn dyn_import_ok() {
let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::with(
Expand Down
25 changes: 16 additions & 9 deletions core/runtime/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,8 @@ pub fn host_import_module_dynamically_callback<'s>(
ImportAttributesKind::DynamicImport,
);

let tc_scope = &mut v8::TryCatch::new(scope);
{
let tc_scope = &mut v8::TryCatch::new(scope);
{
let state = JsRuntime::state_from(tc_scope);
if let Some(validate_import_attributes_cb) =
Expand All @@ -686,15 +686,15 @@ pub fn host_import_module_dynamically_callback<'s>(
let requested_module_type =
get_requested_module_type_from_attributes(&assertions);

let resolver_handle = v8::Global::new(scope, resolver);
let cped_handle = v8::Global::new(scope, cped);
let resolver_handle = v8::Global::new(tc_scope, resolver);
let cped_handle = v8::Global::new(tc_scope, cped);
{
let state = JsRuntime::state_from(scope);
let module_map_rc = JsRealm::module_map_from(scope);
let state = JsRuntime::state_from(tc_scope);
let module_map_rc = JsRealm::module_map_from(tc_scope);

if !ModuleMap::load_dynamic_import(
module_map_rc,
scope,
tc_scope,
&specifier_str,
&referrer_name_str,
requested_module_type,
Expand All @@ -714,11 +714,18 @@ pub fn host_import_module_dynamically_callback<'s>(
let builder = v8::FunctionBuilder::new(catch_dynamic_import_promise_error);

let map_err =
v8::FunctionBuilder::<v8::Function>::build(builder, scope).unwrap();
v8::FunctionBuilder::<v8::Function>::build(builder, tc_scope).unwrap();

let Some(promise_new) = promise.catch(tc_scope, map_err) else {
if tc_scope.has_caught() {
let e = tc_scope.exception().unwrap();
resolver.reject(tc_scope, e);
}

let promise = promise.catch(scope, map_err).unwrap();
return Some(promise);
};

Some(promise)
Some(promise_new)
}

pub extern "C" fn host_initialize_import_meta_object_callback(
Expand Down

0 comments on commit cce028f

Please sign in to comment.