Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2025-04-05 09:48:45 -07:00
parent 13aa178734
commit 8ce175f006
2 changed files with 36 additions and 43 deletions

View File

@ -156,44 +156,41 @@ impl ExecutorContext {
exec_state: &mut ExecState, exec_state: &mut ExecState,
) -> Result<(), KclError> { ) -> Result<(), KclError> {
for statement in &program.body { for statement in &program.body {
match statement { if let BodyItem::ImportStatement(import_stmt) = statement {
BodyItem::ImportStatement(import_stmt) => { let path_str = import_stmt.path.to_string();
let path_str = import_stmt.path.to_string();
if modules.contains_key(&path_str) { if modules.contains_key(&path_str) {
// don't waste our time if we've already loaded the // don't waste our time if we've already loaded the
// module. // module.
continue; continue;
}
let source_range = SourceRange::from(import_stmt);
let attrs = &import_stmt.outer_attrs;
let module_id = self
.open_module(&import_stmt.path, attrs, exec_state, source_range)
.await?;
let Some(module) = exec_state.get_module(module_id) else {
crate::log::log("we got back a module id that doesn't exist");
unreachable!();
};
let progn = {
// this dance is to avoid taking out a mut borrow
// below on exec_state after borrowing here. As a
// result, we need to clone (ugh) the program for
// now.
let ModuleRepr::Kcl(ref progn, _) = module.repr else {
// not a kcl file, we can skip this
continue;
};
progn.clone()
};
modules.insert(path_str, progn.clone().inner);
self.preload_all_modules(modules, &progn, exec_state).await?;
} }
_ => {}
let source_range = SourceRange::from(import_stmt);
let attrs = &import_stmt.outer_attrs;
let module_id = self
.open_module(&import_stmt.path, attrs, exec_state, source_range)
.await?;
let Some(module) = exec_state.get_module(module_id) else {
crate::log::log("we got back a module id that doesn't exist");
unreachable!();
};
let progn = {
// this dance is to avoid taking out a mut borrow
// below on exec_state after borrowing here. As a
// result, we need to clone (ugh) the program for
// now.
let ModuleRepr::Kcl(ref progn, _) = module.repr else {
// not a kcl file, we can skip this
continue;
};
progn.clone()
};
modules.insert(path_str, progn.clone().inner);
self.preload_all_modules(modules, &progn, exec_state).await?;
}; };
} }
Ok(()) Ok(())
@ -2571,8 +2568,6 @@ a = foo()
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn load_all_modules() { async fn load_all_modules() {
let mut universe = HashMap::<String, Node<Program>>::new();
// program a.kcl // program a.kcl
let programa_kcl = r#" let programa_kcl = r#"
export a = 1 export a = 1
@ -2598,7 +2593,7 @@ import c from 'c.kcl'
d = b + c d = b + c
"#; "#;
let main = crate::parsing::parse_str(&main_kcl, ModuleId::default()) let main = crate::parsing::parse_str(main_kcl, ModuleId::default())
.parse_errs_as_err() .parse_errs_as_err()
.unwrap(); .unwrap();

View File

@ -735,12 +735,10 @@ impl ExecutorContext {
for module in modules { for module in modules {
let program = universe.get(&module).unwrap().clone(); let program = universe.get(&module).unwrap().clone();
let module = module.clone(); let exec_state = exec_state.clone();
let mut exec_state = exec_state.clone();
let exec_ctxt = self.clone(); let exec_ctxt = self.clone();
set.spawn(async move { set.spawn(async move {
let module = module;
let mut exec_state = exec_state; let mut exec_state = exec_state;
let exec_ctxt = exec_ctxt; let exec_ctxt = exec_ctxt;
let program = program; let program = program;
@ -761,7 +759,7 @@ impl ExecutorContext {
set.join_all().await; set.join_all().await;
} }
self.inner_run(&program, exec_state, false).await self.inner_run(program, exec_state, false).await
} }
/// Perform the execution of a program. Accept all possible parameters and /// Perform the execution of a program. Accept all possible parameters and