Compare commits

...

1 Commits

Author SHA1 Message Date
495727d617 hacky shit
Signed-off-by: Jess Frazelle <github@jessfraz.com>
2025-04-05 13:05:04 -07:00
6 changed files with 125 additions and 43 deletions

View File

@ -460,7 +460,7 @@ impl ExecutorContext {
Ok(last_expr)
}
pub(super) async fn open_module(
pub async fn open_module(
&self,
path: &ImportPath,
attrs: &[Node<Annotation>],
@ -468,6 +468,7 @@ impl ExecutorContext {
source_range: SourceRange,
) -> Result<ModuleId, KclError> {
let resolved_path = ModulePath::from_import_path(path, &self.settings.project_directory);
match path {
ImportPath::Kcl { .. } => {
exec_state.global.mod_loader.cycle_check(&resolved_path, source_range)?;
@ -597,7 +598,7 @@ impl ExecutorContext {
result
}
async fn exec_module_from_ast(
pub async fn exec_module_from_ast(
&self,
program: &Node<Program>,
module_id: ModuleId,
@ -605,6 +606,7 @@ impl ExecutorContext {
exec_state: &mut ExecState,
source_range: SourceRange,
) -> Result<(Option<KclValue>, EnvironmentRef, Vec<String>), KclError> {
println!("exec_module_from_ast {path}");
exec_state.global.mod_loader.enter_module(path);
let result = self.exec_module_body(program, exec_state, false, module_id, path).await;
exec_state.global.mod_loader.leave_module(path);
@ -2658,6 +2660,7 @@ d = b + c
original_file_contents: "".to_owned(),
},
&mut exec_state,
false,
)
.await
.unwrap();

View File

@ -31,14 +31,14 @@ use tokio::task::JoinSet;
use crate::{
engine::EngineManager,
errors::KclError,
errors::{KclError, KclErrorDetails},
execution::{
artifact::build_artifact_graph,
cache::{CacheInformation, CacheResult},
types::{UnitAngle, UnitLen},
},
fs::FileManager,
modules::{ModuleId, ModulePath},
modules::{ModuleId, ModulePath, ModuleRepr},
parsing::ast::types::{Expr, ImportPath, NodeRef},
source_range::SourceRange,
std::StdLib,
@ -671,7 +671,7 @@ impl ExecutorContext {
(program, exec_state, false)
};
let result = self.inner_run(&program, &mut exec_state, preserve_mem).await;
let result = self.run_concurrent(&program, &mut exec_state, preserve_mem).await;
if result.is_err() {
cache::bust_cache().await;
@ -704,7 +704,7 @@ impl ExecutorContext {
program: &crate::Program,
exec_state: &mut ExecState,
) -> Result<(EnvironmentRef, Option<ModelingSessionData>), KclErrorWithOutputs> {
self.run_concurrent(program, exec_state).await
self.run_concurrent(program, exec_state, false).await
}
/// Perform the execution of a program using an (experimental!) concurrent
@ -721,46 +721,107 @@ impl ExecutorContext {
&self,
program: &crate::Program,
exec_state: &mut ExecState,
preserve_mem: bool,
) -> Result<(EnvironmentRef, Option<ModelingSessionData>), KclErrorWithOutputs> {
self.prepare_mem(exec_state).await.unwrap();
let mut universe = std::collections::HashMap::new();
let mut out_id_map = std::collections::HashMap::new();
crate::walk::import_universe(self, &program.ast, &mut universe)
crate::walk::import_universe(self, &program.ast, &mut universe, &mut out_id_map, exec_state)
.await
.unwrap();
for modules in crate::walk::import_graph(&universe).unwrap().into_iter() {
let mut set = JoinSet::new();
println!("Running module {modules:?}");
//let mut set = JoinSet::new();
println!("AFTER Running module {modules:?}");
let (results_tx, mut results_rx): (
tokio::sync::mpsc::Sender<(
String,
Result<(Option<KclValue>, EnvironmentRef, Vec<String>), KclError>,
)>,
tokio::sync::mpsc::Receiver<_>,
) = tokio::sync::mpsc::channel(1);
for module in modules {
let program = universe.get(&module).unwrap().clone();
let Some(module_id) = out_id_map.get(&module) else {
panic!("Module {module} not found in exec_state");
};
let module_id = module_id.clone();
let module_path = {
let module_info = exec_state.get_module(module_id).unwrap();
let module_path = module_info.path.clone();
module_path
};
let exec_state = exec_state.clone();
let exec_ctxt = self.clone();
let results_tx = results_tx.clone();
set.spawn(async move {
println!("Running module {module} from run_concurrent");
let mut exec_state = exec_state;
let exec_ctxt = exec_ctxt;
let program = program;
println!("before spawn");
#[cfg(target_arch = "wasm32")]
{
wasm_bindgen_futures::spawn_local(async move {
//set.spawn(async move {
println!("Running module {module} from run_concurrent");
let mut exec_state = exec_state;
let exec_ctxt = exec_ctxt;
let program = program;
exec_ctxt
.inner_run(
&crate::Program {
ast: program.clone(),
original_file_contents: "".to_owned(),
},
&mut exec_state,
false,
)
.await
});
let result = exec_ctxt
.exec_module_from_ast(
&program,
module_id,
&module_path,
&mut exec_state,
Default::default(),
)
.await;
results_tx.send((module, result)).await.unwrap();
});
}
}
set.join_all().await;
drop(results_tx);
while let Some((module, result)) = results_rx.recv().await {
match result {
Ok((env_ref, session_data, variables)) => {
println!("{module} {:?}", variables);
let Some(module_id) = out_id_map.get(&module) else {
//let snapshot_png_bytes = self.prepare_snapshot().await.unwrap().contents.0;
// Save to a file.
//tokio::fs::write("snapshot.png", snapshot_png_bytes).await.unwrap();
return Err(KclErrorWithOutputs::no_outputs(KclError::Internal(KclErrorDetails {
message: format!("Module {module} not found in exec_state"),
source_ranges: Default::default(),
})));
};
let path = exec_state.global.module_infos[module_id].path.clone();
let mut repr = exec_state.global.module_infos[module_id].take_repr();
let ModuleRepr::Kcl(program, cache) = &mut repr else {
continue;
};
*cache = Some((session_data, variables.clone()));
exec_state.global.module_infos[module_id].restore_repr(repr);
}
Err(e) => {
//let snapshot_png_bytes = self.prepare_snapshot().await.unwrap().contents.0;
// Save to a file.
//tokio::fs::write("snapshot.png", snapshot_png_bytes).await.unwrap();
return Err(KclErrorWithOutputs::no_outputs(e));
}
}
}
}
self.inner_run(program, exec_state, false).await
self.inner_run(program, exec_state, preserve_mem).await
}
/// Perform the execution of a program. Accept all possible parameters and

View File

@ -228,7 +228,7 @@ impl ExecState {
self.global.module_infos.insert(id, module_info);
}
pub(super) fn get_module(&mut self, id: ModuleId) -> Option<&ModuleInfo> {
pub fn get_module(&mut self, id: ModuleId) -> Option<&ModuleInfo> {
self.global.module_infos.get(&id)
}

View File

@ -6,10 +6,10 @@ use std::{
use anyhow::Result;
use crate::{
fs::FileSystem,
modules::ModuleRepr,
parsing::ast::types::{ImportPath, Node as AstNode, NodeRef, Program},
walk::{Node, Visitable},
ExecutorContext, SourceRange,
ExecState, ExecutorContext, ModuleId,
};
/// Specific dependency between two modules. The 0th element of this tuple
@ -43,6 +43,9 @@ pub fn import_graph(progs: &HashMap<String, AstNode<Program>>) -> Result<Vec<Vec
#[allow(clippy::iter_over_hash_type)]
fn topsort(all_modules: &[&str], graph: Graph) -> Result<Vec<Vec<String>>> {
if all_modules.is_empty() {
return Ok(vec![]);
}
let mut dep_map = HashMap::<String, Vec<String>>::new();
for (dependent, dependency) in graph.iter() {
@ -59,6 +62,7 @@ fn topsort(all_modules: &[&str], graph: Graph) -> Result<Vec<Vec<String>>> {
let mut order = vec![];
loop {
println!("waiting_modules: {:?}", waiting_modules);
// Each pass through we need to find any modules which have nothing
// "pointing at it" -- so-called reverse dependencies. This is an entry
// that is either not in the dep_map OR an empty list.
@ -131,30 +135,42 @@ pub(crate) async fn import_universe<'prog>(
ctx: &ExecutorContext,
prog: NodeRef<'prog, Program>,
out: &mut HashMap<String, AstNode<Program>>,
out_id_map: &mut HashMap<String, ModuleId>,
exec_state: &mut ExecState,
) -> Result<()> {
for module in import_dependencies(prog)? {
let modules = import_dependencies(prog)?;
for module in modules {
eprintln!("{:?}", module);
if out.contains_key(&module) {
continue;
}
// TODO: use open_module and find a way to pass attrs cleanly
let kcl = ctx
.fs
.read_to_string(
ctx.settings
.project_directory
.clone()
.unwrap_or("".into())
.join(&module),
SourceRange::default(),
let module_id = ctx
.open_module(
&ImportPath::Kcl {
filename: module.to_string(),
},
&[],
exec_state,
Default::default(),
)
.await?;
let program = crate::parsing::parse_str(&kcl, crate::ModuleId::default()).parse_errs_as_err()?;
out_id_map.insert(module.clone(), module_id);
let program = {
let Some(module_info) = exec_state.get_module(module_id) else {
// We should never get here we just fucking added it.
anyhow::bail!("Module {} not found", module);
};
let ModuleRepr::Kcl(program, _) = &module_info.repr else {
anyhow::bail!("Module {} is not a KCL program", module);
};
program.clone()
};
out.insert(module.clone(), program.clone());
Box::pin(import_universe(ctx, &program, out)).await?;
Box::pin(import_universe(ctx, &program, out, out_id_map, exec_state)).await?;
}
Ok(())

View File

@ -77,7 +77,8 @@ impl Context {
let program: Program = serde_json::from_str(program_ast_json).map_err(|e| e.to_string())?;
let ctx = self.create_executor_ctx(settings, path, false)?;
match ctx.run_with_caching(program).await {
let mut exec_state = kcl_lib::ExecState::new(&ctx);
match ctx.run(&program, &mut exec_state).await {
// The serde-wasm-bindgen does not work here because of weird HashMap issues.
// DO NOT USE serde_wasm_bindgen::to_value it will break the frontend.
Ok(outcome) => JsValue::from_serde(&outcome).map_err(|e| e.to_string()),

View File

@ -1965,6 +1965,7 @@ export class EngineCommandManager extends EventTarget {
range,
idToRangeMap,
})
console.log("responose to wasm", resp)
return BSON.serialize(resp[0])
}
/**