Skip to content

Commit

Permalink
fix: handle module states in op_import_sync
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Oct 18, 2024
1 parent e6e58db commit 60a682a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
74 changes: 66 additions & 8 deletions core/ops_builtin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use crate::error::exception_to_err_result;
use crate::error::format_file_name;
use crate::error::generic_error;
use crate::error::type_error;
use crate::io::AdaptiveBufferStrategy;
use crate::io::BufMutView;
Expand Down Expand Up @@ -441,12 +442,43 @@ async fn do_load_job<'s>(
}

let root_id = load.root_module_id.expect("Root module should be loaded");
module_map_rc
.instantiate_module(scope, root_id)
.map_err(|e| {
let exception = v8::Local::new(scope, e);
exception_to_err_result::<()>(scope, exception, false, false).unwrap_err()
})?;

let module = module_map_rc
.get_module(scope, root_id)
.expect("Module must exist");

match module.get_status() {
v8::ModuleStatus::Uninstantiated => {
module_map_rc
.instantiate_module(scope, root_id)
.map_err(|e| {
let exception = v8::Local::new(scope, e);
exception_to_err_result::<()>(scope, exception, false, false)
.unwrap_err()
})?;
}
v8::ModuleStatus::Instantiated
| v8::ModuleStatus::Instantiating
| v8::ModuleStatus::Evaluating => {
return Err(generic_error(format!(
"Cannot require() ES Module {specifier} in a cycle."
)));
}
v8::ModuleStatus::Evaluated => {
// OK
}
v8::ModuleStatus::Errored => {
return Err(
exception_to_err_result::<()>(
scope,
module.get_exception(),
false,
false,
)
.unwrap_err(),
);
}
}

Ok(root_id)
}
Expand Down Expand Up @@ -517,11 +549,37 @@ fn op_import_sync<'s>(
code,
))?;

module_map_rc.mod_evaluate_sync(scope, module_id)?;

let module = module_map_rc
.get_module(scope, module_id)
.expect("Module must exist");

match module.get_status() {
v8::ModuleStatus::Uninstantiated
| v8::ModuleStatus::Instantiating
| v8::ModuleStatus::Evaluating => {
return Err(generic_error(format!(
"Cannot require() ES Module {specifier} in a cycle."
)));
}
v8::ModuleStatus::Instantiated => {
module_map_rc.mod_evaluate_sync(scope, module_id)?;
}
v8::ModuleStatus::Evaluated => {
// OK
}
v8::ModuleStatus::Errored => {
return Err(
exception_to_err_result::<()>(
scope,
module.get_exception(),
false,
false,
)
.unwrap_err(),
);
}
}

let namespace = module.get_module_namespace().cast::<v8::Object>();

let scope = &mut v8::TryCatch::new(scope);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

const { op_import_sync, op_path_to_url } = Deno.core.ops;

const resolve = (p: string) => op_path_to_url(p);

await import(resolve("./integration/import_sync/sync.js"));
console.log(op_import_sync(resolve("./integration/import_sync/sync.js")));
3 changes: 3 additions & 0 deletions testing/integration/import_sync_existing/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

export const a = 1;
1 change: 1 addition & 0 deletions testing/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ integration_test!(
error_get_file_name_to_string,
error_get_script_name_or_source_url,
import_sync,
import_sync_existing,
main_module_handler,
module_types,
pending_unref_op_tla,
Expand Down

0 comments on commit 60a682a

Please sign in to comment.