parallelized modules from kcl (#6206)
* wip * sketch a bit more; going to pull this out of tests next * wip * lock start things * this was a bad idea * Revert "this was a bad idea" This reverts commita2092e7ed6
. * prepare prelude before spawning * error * poop * yike * :( * ok * Reapply "this was a bad idea" This reverts commitfafdf41093
. * chip away more * man this is bad * fix rebase add feature flag Signed-off-by: Jess Frazelle <github@jessfraz.com> * get rid of execution kind Signed-off-by: Jess Frazelle <github@jessfraz.com> * clippy Signed-off-by: Jess Frazelle <github@jessfraz.com> * logs Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * no extra executes Signed-off-by: Jess Frazelle <github@jessfraz.com> * race w batch Signed-off-by: Jess Frazelle <github@jessfraz.com> * cluppy Signed-off-by: Jess Frazelle <github@jessfraz.com> * no printlns Signed-off-by: Jess Frazelle <github@jessfraz.com> * no printlns Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix source ranges Signed-off-by: Jess Frazelle <github@jessfraz.com> * batch shit Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix some bugs Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix error Signed-off-by: Jess Frazelle <github@jessfraz.com> * cut 1 * preserve mem * re-ad deep_clone the helper we were calling was pushing a new call, which was hanging out. we can skip the middleman since we already have something properly prepared, just without a stdlib in some cases. * skip non-kcl * clean up source range bug * error message changed the uuids also changed because the error is hit before execute even starts. * typo * rensnapshot a few * order things * MAYBE REVERT LATER: attempt at an ordering * snapsnap * Revert "snapsnap" This reverts commit7350b32c7d
. * Revert "MAYBE REVERT LATER:" This reverts commitab49f3e85f
. * ugh * poop * poop2 * lint * tranche 1 * more * more snaps * snap * more * update * MAYBE REVERT THIS * cache multi-file Signed-off-by: Jess Frazelle <github@jessfraz.com> * addd tests Signed-off-by: Jess Frazelle <github@jessfraz.com> * set to false Signed-off-by: Jess Frazelle <github@jessfraz.com> * add test outputs Signed-off-by: Jess Frazelle <github@jessfraz.com> * clippy Signed-off-by: Jess Frazelle <github@jessfraz.com> * kcl-py-bindings uses carwheel Signed-off-by: Jess Frazelle <github@jessfraz.com> * update snapshots Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: Paul R. Tagliamonte <paul@zoo.dev> Co-authored-by: Paul Tagliamonte <paultag@gmail.com>
This commit is contained in:
@ -243,7 +243,7 @@ fn generate_changed_program(old_ast: Node<Program>, mut new_ast: Node<Program>,
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::execution::{parse_execute, ExecTestResults};
|
||||
use crate::execution::{parse_execute, parse_execute_with_project_dir, ExecTestResults};
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code() {
|
||||
@ -600,4 +600,64 @@ startSketchOn('XY')
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_multi_file_no_changes_does_not_reexecute() {
|
||||
let code = r#"import "toBeImported.kcl" as importedCube
|
||||
|
||||
importedCube
|
||||
|
||||
sketch001 = startSketchOn(XZ)
|
||||
profile001 = startProfileAt([-134.53, -56.17], sketch001)
|
||||
|> angledLine(angle = 0, length = 79.05, tag = $rectangleSegmentA001)
|
||||
|> angledLine(angle = segAng(rectangleSegmentA001) - 90, length = 76.28)
|
||||
|> angledLine(angle = segAng(rectangleSegmentA001), length = -segLen(rectangleSegmentA001), tag = $seg01)
|
||||
|> line(endAbsolute = [profileStartX(%), profileStartY(%)], tag = $seg02)
|
||||
|> close()
|
||||
extrude001 = extrude(profile001, length = 100)
|
||||
sketch003 = startSketchOn(extrude001, face = seg02)
|
||||
sketch002 = startSketchOn(extrude001, face = seg01)
|
||||
"#;
|
||||
|
||||
let other_file = (
|
||||
std::path::PathBuf::from("toBeImported.kcl"),
|
||||
r#"sketch001 = startSketchOn(XZ)
|
||||
profile001 = startProfileAt([281.54, 305.81], sketch001)
|
||||
|> angledLine(angle = 0, length = 123.43, tag = $rectangleSegmentA001)
|
||||
|> angledLine(angle = segAng(rectangleSegmentA001) - 90, length = 85.99)
|
||||
|> angledLine(angle = segAng(rectangleSegmentA001), length = -segLen(rectangleSegmentA001))
|
||||
|> line(endAbsolute = [profileStartX(%), profileStartY(%)])
|
||||
|> close()
|
||||
extrude(profile001, length = 100)"#
|
||||
.to_string(),
|
||||
);
|
||||
|
||||
let tmp_dir = std::env::temp_dir();
|
||||
let tmp_dir = tmp_dir.join(uuid::Uuid::new_v4().to_string());
|
||||
|
||||
// Create a temporary file for each of the other files.
|
||||
let tmp_file = tmp_dir.join(other_file.0);
|
||||
std::fs::create_dir_all(tmp_file.parent().unwrap()).unwrap();
|
||||
std::fs::write(tmp_file, other_file.1).unwrap();
|
||||
|
||||
let ExecTestResults { program, exec_ctxt, .. } =
|
||||
parse_execute_with_project_dir(code, Some(tmp_dir)).await.unwrap();
|
||||
|
||||
let mut new_program = crate::Program::parse_no_errs(code).unwrap();
|
||||
new_program.compute_digest();
|
||||
|
||||
let result = get_changed_program(
|
||||
CacheInformation {
|
||||
ast: &program.ast,
|
||||
settings: &exec_ctxt.settings,
|
||||
},
|
||||
CacheInformation {
|
||||
ast: &new_program.ast,
|
||||
settings: &exec_ctxt.settings,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(result, CacheResult::NoAction(false));
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ use super::{
|
||||
types::{PrimitiveType, CHECK_NUMERIC_TYPES},
|
||||
};
|
||||
use crate::{
|
||||
engine::ExecutionKind,
|
||||
errors::{KclError, KclErrorDetails},
|
||||
execution::{
|
||||
annotations,
|
||||
@ -66,15 +65,13 @@ impl ExecutorContext {
|
||||
exec_state.mod_local.explicit_length_units = true;
|
||||
}
|
||||
let new_units = exec_state.length_unit();
|
||||
if !self.engine.execution_kind().await.is_isolated() {
|
||||
self.engine
|
||||
.set_units(
|
||||
new_units.into(),
|
||||
annotation.as_source_range(),
|
||||
exec_state.id_generator(),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
self.engine
|
||||
.set_units(
|
||||
new_units.into(),
|
||||
annotation.as_source_range(),
|
||||
exec_state.id_generator(),
|
||||
)
|
||||
.await?;
|
||||
} else {
|
||||
exec_state.err(CompilationError::err(
|
||||
annotation.as_source_range(),
|
||||
@ -104,15 +101,11 @@ impl ExecutorContext {
|
||||
&self,
|
||||
program: &Node<Program>,
|
||||
exec_state: &mut ExecState,
|
||||
exec_kind: ExecutionKind,
|
||||
preserve_mem: bool,
|
||||
module_id: ModuleId,
|
||||
path: &ModulePath,
|
||||
) -> Result<(Option<KclValue>, EnvironmentRef, Vec<String>), KclError> {
|
||||
crate::log::log(format!("enter module {path} {} {exec_kind:?}", exec_state.stack()));
|
||||
|
||||
let old_units = exec_state.length_unit();
|
||||
let original_execution = self.engine.replace_execution_kind(exec_kind).await;
|
||||
crate::log::log(format!("enter module {path} {}", exec_state.stack()));
|
||||
|
||||
let mut local_state = ModuleState::new(path.std_path(), exec_state.stack().memory.clone(), Some(module_id));
|
||||
if !preserve_mem {
|
||||
@ -131,7 +124,6 @@ impl ExecutorContext {
|
||||
.exec_block(program, exec_state, crate::execution::BodyType::Root)
|
||||
.await;
|
||||
|
||||
let new_units = exec_state.length_unit();
|
||||
let env_ref = if preserve_mem {
|
||||
exec_state.mut_stack().pop_and_preserve_env()
|
||||
} else {
|
||||
@ -141,17 +133,6 @@ impl ExecutorContext {
|
||||
std::mem::swap(&mut exec_state.mod_local, &mut local_state);
|
||||
}
|
||||
|
||||
// We only need to reset the units if we are not on the Main path.
|
||||
// If we reset at the end of the main path, then we just add on an extra
|
||||
// command and we'd need to flush the batch again.
|
||||
// This avoids that.
|
||||
if !exec_kind.is_isolated() && new_units != old_units && *path != ModulePath::Main {
|
||||
self.engine
|
||||
.set_units(old_units.into(), Default::default(), exec_state.id_generator())
|
||||
.await?;
|
||||
}
|
||||
self.engine.replace_execution_kind(original_execution).await;
|
||||
|
||||
crate::log::log(format!("leave {path}"));
|
||||
|
||||
result.map(|result| (result, env_ref, local_state.module_exports))
|
||||
@ -161,7 +142,7 @@ impl ExecutorContext {
|
||||
#[async_recursion]
|
||||
pub(super) async fn exec_block<'a>(
|
||||
&'a self,
|
||||
program: NodeRef<'a, crate::parsing::ast::types::Program>,
|
||||
program: NodeRef<'a, Program>,
|
||||
exec_state: &mut ExecState,
|
||||
body_type: BodyType,
|
||||
) -> Result<Option<KclValue>, KclError> {
|
||||
@ -185,9 +166,8 @@ impl ExecutorContext {
|
||||
|
||||
match &import_stmt.selector {
|
||||
ImportSelector::List { items } => {
|
||||
let (env_ref, module_exports) = self
|
||||
.exec_module_for_items(module_id, exec_state, ExecutionKind::Isolated, source_range)
|
||||
.await?;
|
||||
let (env_ref, module_exports) =
|
||||
self.exec_module_for_items(module_id, exec_state, source_range).await?;
|
||||
for import_item in items {
|
||||
// Extract the item from the module.
|
||||
let item = exec_state
|
||||
@ -228,9 +208,8 @@ impl ExecutorContext {
|
||||
}
|
||||
}
|
||||
ImportSelector::Glob(_) => {
|
||||
let (env_ref, module_exports) = self
|
||||
.exec_module_for_items(module_id, exec_state, ExecutionKind::Isolated, source_range)
|
||||
.await?;
|
||||
let (env_ref, module_exports) =
|
||||
self.exec_module_for_items(module_id, exec_state, source_range).await?;
|
||||
for name in module_exports.iter() {
|
||||
let item = exec_state
|
||||
.stack()
|
||||
@ -421,7 +400,7 @@ impl ExecutorContext {
|
||||
Ok(last_expr)
|
||||
}
|
||||
|
||||
pub(super) async fn open_module(
|
||||
pub async fn open_module(
|
||||
&self,
|
||||
path: &ImportPath,
|
||||
attrs: &[Node<Annotation>],
|
||||
@ -429,6 +408,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)?;
|
||||
@ -485,7 +465,6 @@ impl ExecutorContext {
|
||||
&self,
|
||||
module_id: ModuleId,
|
||||
exec_state: &mut ExecState,
|
||||
exec_kind: ExecutionKind,
|
||||
source_range: SourceRange,
|
||||
) -> Result<(EnvironmentRef, Vec<String>), KclError> {
|
||||
let path = exec_state.global.module_infos[&module_id].path.clone();
|
||||
@ -494,12 +473,12 @@ impl ExecutorContext {
|
||||
|
||||
let result = match &mut repr {
|
||||
ModuleRepr::Root => Err(exec_state.circular_import_error(&path, source_range)),
|
||||
ModuleRepr::Kcl(_, Some((env_ref, items))) => Ok((*env_ref, items.clone())),
|
||||
ModuleRepr::Kcl(_, Some((_, env_ref, items))) => Ok((*env_ref, items.clone())),
|
||||
ModuleRepr::Kcl(program, cache) => self
|
||||
.exec_module_from_ast(program, module_id, &path, exec_state, exec_kind, source_range)
|
||||
.exec_module_from_ast(program, module_id, &path, exec_state, source_range, false)
|
||||
.await
|
||||
.map(|(_, er, items)| {
|
||||
*cache = Some((er, items.clone()));
|
||||
.map(|(val, er, items)| {
|
||||
*cache = Some((val, er, items.clone()));
|
||||
(er, items)
|
||||
}),
|
||||
ModuleRepr::Foreign(geom) => Err(KclError::Semantic(KclErrorDetails {
|
||||
@ -518,7 +497,6 @@ impl ExecutorContext {
|
||||
module_id: ModuleId,
|
||||
module_name: &BoxNode<Name>,
|
||||
exec_state: &mut ExecState,
|
||||
exec_kind: ExecutionKind,
|
||||
source_range: SourceRange,
|
||||
) -> Result<Option<KclValue>, KclError> {
|
||||
exec_state.global.operations.push(Operation::GroupBegin {
|
||||
@ -535,13 +513,14 @@ impl ExecutorContext {
|
||||
|
||||
let result = match &mut repr {
|
||||
ModuleRepr::Root => Err(exec_state.circular_import_error(&path, source_range)),
|
||||
ModuleRepr::Kcl(_, Some((val, _, _))) => Ok(val.clone()),
|
||||
ModuleRepr::Kcl(program, cached_items) => {
|
||||
let result = self
|
||||
.exec_module_from_ast(program, module_id, &path, exec_state, exec_kind, source_range)
|
||||
.exec_module_from_ast(program, module_id, &path, exec_state, source_range, false)
|
||||
.await;
|
||||
match result {
|
||||
Ok((val, env, items)) => {
|
||||
*cached_items = Some((env, items));
|
||||
*cached_items = Some((val.clone(), env, items));
|
||||
Ok(val)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
@ -560,18 +539,18 @@ impl ExecutorContext {
|
||||
result
|
||||
}
|
||||
|
||||
async fn exec_module_from_ast(
|
||||
pub async fn exec_module_from_ast(
|
||||
&self,
|
||||
program: &Node<Program>,
|
||||
module_id: ModuleId,
|
||||
path: &ModulePath,
|
||||
exec_state: &mut ExecState,
|
||||
exec_kind: ExecutionKind,
|
||||
source_range: SourceRange,
|
||||
preserve_mem: bool,
|
||||
) -> Result<(Option<KclValue>, EnvironmentRef, Vec<String>), KclError> {
|
||||
exec_state.global.mod_loader.enter_module(path);
|
||||
let result = self
|
||||
.exec_module_body(program, exec_state, exec_kind, false, module_id, path)
|
||||
.exec_module_body(program, exec_state, preserve_mem, module_id, path)
|
||||
.await;
|
||||
exec_state.global.mod_loader.leave_module(path);
|
||||
|
||||
@ -608,7 +587,7 @@ impl ExecutorContext {
|
||||
Expr::Name(name) => {
|
||||
let value = name.get_result(exec_state, self).await?.clone();
|
||||
if let KclValue::Module { value: module_id, meta } = value {
|
||||
self.exec_module_for_result(module_id, name, exec_state, ExecutionKind::Normal, metadata.source_range)
|
||||
self.exec_module_for_result(module_id, name, exec_state, metadata.source_range)
|
||||
.await?
|
||||
.unwrap_or_else(|| {
|
||||
exec_state.warn(CompilationError::err(
|
||||
@ -808,7 +787,7 @@ impl Node<Name> {
|
||||
};
|
||||
|
||||
mem_spec = Some(
|
||||
ctx.exec_module_for_items(*module_id, exec_state, ExecutionKind::Normal, p.as_source_range())
|
||||
ctx.exec_module_for_items(*module_id, exec_state, p.as_source_range())
|
||||
.await?,
|
||||
);
|
||||
}
|
||||
@ -1320,7 +1299,7 @@ impl Node<CallExpressionKw> {
|
||||
));
|
||||
}
|
||||
|
||||
let op = if func.feature_tree_operation() && !ctx.is_isolated_execution().await {
|
||||
let op = if func.feature_tree_operation() {
|
||||
let op_labeled_args = args
|
||||
.kw_args
|
||||
.labeled
|
||||
@ -1406,7 +1385,7 @@ impl Node<CallExpressionKw> {
|
||||
e.add_source_ranges(vec![callsite])
|
||||
})?;
|
||||
|
||||
if matches!(fn_src, FunctionSource::User { .. }) && !ctx.is_isolated_execution().await {
|
||||
if matches!(fn_src, FunctionSource::User { .. }) {
|
||||
// Track return operation.
|
||||
exec_state.global.operations.push(Operation::GroupEnd);
|
||||
}
|
||||
@ -1458,7 +1437,7 @@ impl Node<CallExpression> {
|
||||
));
|
||||
}
|
||||
|
||||
let op = if func.feature_tree_operation() && !ctx.is_isolated_execution().await {
|
||||
let op = if func.feature_tree_operation() {
|
||||
let op_labeled_args = func
|
||||
.args(false)
|
||||
.iter()
|
||||
@ -1516,19 +1495,17 @@ impl Node<CallExpression> {
|
||||
// exec_state.
|
||||
let func = fn_name.get_result(exec_state, ctx).await?.clone();
|
||||
|
||||
if !ctx.is_isolated_execution().await {
|
||||
// Track call operation.
|
||||
exec_state.global.operations.push(Operation::GroupBegin {
|
||||
group: Group::FunctionCall {
|
||||
name: Some(fn_name.to_string()),
|
||||
function_source_range: func.function_def_source_range().unwrap_or_default(),
|
||||
unlabeled_arg: None,
|
||||
// TODO: Add the arguments for legacy positional parameters.
|
||||
labeled_args: Default::default(),
|
||||
},
|
||||
source_range: callsite,
|
||||
});
|
||||
}
|
||||
// Track call operation.
|
||||
exec_state.global.operations.push(Operation::GroupBegin {
|
||||
group: Group::FunctionCall {
|
||||
name: Some(fn_name.to_string()),
|
||||
function_source_range: func.function_def_source_range().unwrap_or_default(),
|
||||
unlabeled_arg: None,
|
||||
// TODO: Add the arguments for legacy positional parameters.
|
||||
labeled_args: Default::default(),
|
||||
},
|
||||
source_range: callsite,
|
||||
});
|
||||
|
||||
let Some(fn_src) = func.as_fn() else {
|
||||
return Err(KclError::Semantic(KclErrorDetails {
|
||||
@ -1557,10 +1534,8 @@ impl Node<CallExpression> {
|
||||
})
|
||||
})?;
|
||||
|
||||
if !ctx.is_isolated_execution().await {
|
||||
// Track return operation.
|
||||
exec_state.global.operations.push(Operation::GroupEnd);
|
||||
}
|
||||
// Track return operation.
|
||||
exec_state.global.operations.push(Operation::GroupEnd);
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
@ -2313,7 +2288,7 @@ impl FunctionSource {
|
||||
}
|
||||
}
|
||||
|
||||
let op = if props.include_in_feature_tree && !ctx.is_isolated_execution().await {
|
||||
let op = if props.include_in_feature_tree {
|
||||
let op_labeled_args = args
|
||||
.kw_args
|
||||
.labeled
|
||||
@ -2357,28 +2332,26 @@ impl FunctionSource {
|
||||
Ok(Some(result))
|
||||
}
|
||||
FunctionSource::User { ast, memory, .. } => {
|
||||
if !ctx.is_isolated_execution().await {
|
||||
// Track call operation.
|
||||
let op_labeled_args = args
|
||||
.kw_args
|
||||
.labeled
|
||||
.iter()
|
||||
.map(|(k, arg)| (k.clone(), OpArg::new(OpKclValue::from(&arg.value), arg.source_range)))
|
||||
.collect();
|
||||
exec_state.global.operations.push(Operation::GroupBegin {
|
||||
group: Group::FunctionCall {
|
||||
name: fn_name.clone(),
|
||||
function_source_range: ast.as_source_range(),
|
||||
unlabeled_arg: args
|
||||
.kw_args
|
||||
.unlabeled
|
||||
.as_ref()
|
||||
.map(|arg| OpArg::new(OpKclValue::from(&arg.value), arg.source_range)),
|
||||
labeled_args: op_labeled_args,
|
||||
},
|
||||
source_range: callsite,
|
||||
});
|
||||
}
|
||||
// Track call operation.
|
||||
let op_labeled_args = args
|
||||
.kw_args
|
||||
.labeled
|
||||
.iter()
|
||||
.map(|(k, arg)| (k.clone(), OpArg::new(OpKclValue::from(&arg.value), arg.source_range)))
|
||||
.collect();
|
||||
exec_state.global.operations.push(Operation::GroupBegin {
|
||||
group: Group::FunctionCall {
|
||||
name: fn_name.clone(),
|
||||
function_source_range: ast.as_source_range(),
|
||||
unlabeled_arg: args
|
||||
.kw_args
|
||||
.unlabeled
|
||||
.as_ref()
|
||||
.map(|arg| OpArg::new(OpKclValue::from(&arg.value), arg.source_range)),
|
||||
labeled_args: op_labeled_args,
|
||||
},
|
||||
source_range: callsite,
|
||||
});
|
||||
|
||||
call_user_defined_function_kw(fn_name.as_deref(), args.kw_args, *memory, ast, exec_state, ctx).await
|
||||
}
|
||||
@ -2391,10 +2364,13 @@ impl FunctionSource {
|
||||
mod test {
|
||||
use std::sync::Arc;
|
||||
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
use super::*;
|
||||
use crate::{
|
||||
execution::{memory::Stack, parse_execute, ContextType},
|
||||
parsing::ast::types::{DefaultParamVal, Identifier, Parameter},
|
||||
ExecutorSettings,
|
||||
};
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
@ -2504,7 +2480,7 @@ mod test {
|
||||
// Run each test.
|
||||
let func_expr = &Node::no_src(FunctionExpression {
|
||||
params,
|
||||
body: crate::parsing::ast::types::Program::empty(),
|
||||
body: Program::empty(),
|
||||
return_type: None,
|
||||
digest: None,
|
||||
});
|
||||
@ -2607,6 +2583,102 @@ a = foo()
|
||||
assert!(result.unwrap_err().to_string().contains("return"));
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn load_all_modules() {
|
||||
// program a.kcl
|
||||
let programa_kcl = r#"
|
||||
export a = 1
|
||||
"#;
|
||||
// program b.kcl
|
||||
let programb_kcl = r#"
|
||||
import a from 'a.kcl'
|
||||
|
||||
export b = a + 1
|
||||
"#;
|
||||
// program c.kcl
|
||||
let programc_kcl = r#"
|
||||
import a from 'a.kcl'
|
||||
|
||||
export c = a + 2
|
||||
"#;
|
||||
|
||||
// program main.kcl
|
||||
let main_kcl = r#"
|
||||
import b from 'b.kcl'
|
||||
import c from 'c.kcl'
|
||||
|
||||
d = b + c
|
||||
"#;
|
||||
|
||||
let main = crate::parsing::parse_str(main_kcl, ModuleId::default())
|
||||
.parse_errs_as_err()
|
||||
.unwrap();
|
||||
|
||||
let tmpdir = tempdir::TempDir::new("zma_kcl_load_all_modules").unwrap();
|
||||
|
||||
tokio::fs::File::create(tmpdir.path().join("main.kcl"))
|
||||
.await
|
||||
.unwrap()
|
||||
.write_all(main_kcl.as_bytes())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
tokio::fs::File::create(tmpdir.path().join("a.kcl"))
|
||||
.await
|
||||
.unwrap()
|
||||
.write_all(programa_kcl.as_bytes())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
tokio::fs::File::create(tmpdir.path().join("b.kcl"))
|
||||
.await
|
||||
.unwrap()
|
||||
.write_all(programb_kcl.as_bytes())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
tokio::fs::File::create(tmpdir.path().join("c.kcl"))
|
||||
.await
|
||||
.unwrap()
|
||||
.write_all(programc_kcl.as_bytes())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let exec_ctxt = ExecutorContext {
|
||||
engine: Arc::new(Box::new(
|
||||
crate::engine::conn_mock::EngineConnection::new()
|
||||
.await
|
||||
.map_err(|err| {
|
||||
KclError::Internal(crate::errors::KclErrorDetails {
|
||||
message: format!("Failed to create mock engine connection: {}", err),
|
||||
source_ranges: vec![SourceRange::default()],
|
||||
})
|
||||
})
|
||||
.unwrap(),
|
||||
)),
|
||||
fs: Arc::new(crate::fs::FileManager::new()),
|
||||
settings: ExecutorSettings {
|
||||
project_directory: Some(tmpdir.path().into()),
|
||||
..Default::default()
|
||||
},
|
||||
stdlib: Arc::new(crate::std::StdLib::new()),
|
||||
context_type: ContextType::Mock,
|
||||
};
|
||||
let mut exec_state = ExecState::new(&exec_ctxt);
|
||||
|
||||
exec_ctxt
|
||||
.run_concurrent(
|
||||
&crate::Program {
|
||||
ast: main.clone(),
|
||||
original_file_contents: "".to_owned(),
|
||||
},
|
||||
&mut exec_state,
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn user_coercion() {
|
||||
let program = r#"fn foo(x: Axis2d) {
|
||||
|
@ -4,13 +4,13 @@ use anyhow::Result;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{types::UnitLen, EnvironmentRef, ExecState, MetaSettings};
|
||||
use crate::{
|
||||
errors::KclErrorDetails,
|
||||
execution::{
|
||||
annotations::{SETTINGS, SETTINGS_UNIT_LENGTH},
|
||||
types::{NumericType, PrimitiveType, RuntimeType},
|
||||
Face, Helix, ImportedGeometry, Metadata, Plane, Sketch, Solid, TagIdentifier,
|
||||
types::{NumericType, PrimitiveType, RuntimeType, UnitLen},
|
||||
EnvironmentRef, ExecState, Face, Helix, ImportedGeometry, MetaSettings, Metadata, Plane, Sketch, Solid,
|
||||
TagIdentifier,
|
||||
},
|
||||
parsing::ast::types::{
|
||||
DefaultParamVal, FunctionExpression, KclNone, Literal, LiteralValue, Node, TagDeclarator, TagNode,
|
||||
|
@ -28,18 +28,18 @@ pub use state::{ExecState, MetaSettings};
|
||||
|
||||
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,
|
||||
CompilationError, ExecError, ExecutionKind, KclErrorWithOutputs,
|
||||
CompilationError, ExecError, KclErrorWithOutputs,
|
||||
};
|
||||
|
||||
pub(crate) mod annotations;
|
||||
@ -495,13 +495,9 @@ impl ExecutorContext {
|
||||
self.context_type == ContextType::Mock || self.context_type == ContextType::MockCustomForwarded
|
||||
}
|
||||
|
||||
pub async fn is_isolated_execution(&self) -> bool {
|
||||
self.engine.execution_kind().await.is_isolated()
|
||||
}
|
||||
|
||||
/// Returns true if we should not send engine commands for any reason.
|
||||
pub async fn no_engine_commands(&self) -> bool {
|
||||
self.is_mock() || self.is_isolated_execution().await
|
||||
self.is_mock()
|
||||
}
|
||||
|
||||
pub async fn send_clear_scene(
|
||||
@ -672,7 +668,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;
|
||||
@ -705,9 +701,210 @@ impl ExecutorContext {
|
||||
program: &crate::Program,
|
||||
exec_state: &mut ExecState,
|
||||
) -> Result<(EnvironmentRef, Option<ModelingSessionData>), KclErrorWithOutputs> {
|
||||
self.run_concurrent(program, exec_state, false).await
|
||||
}
|
||||
|
||||
/// Perform the execution of a program.
|
||||
///
|
||||
/// You can optionally pass in some initialization memory for partial
|
||||
/// execution.
|
||||
///
|
||||
/// To access non-fatal errors and warnings, extract them from the `ExecState`.
|
||||
pub async fn run_single_threaded(
|
||||
&self,
|
||||
program: &crate::Program,
|
||||
exec_state: &mut ExecState,
|
||||
) -> Result<(EnvironmentRef, Option<ModelingSessionData>), KclErrorWithOutputs> {
|
||||
exec_state.add_root_module_contents(program);
|
||||
self.eval_prelude(exec_state, SourceRange::synthetic())
|
||||
.await
|
||||
.map_err(KclErrorWithOutputs::no_outputs)?;
|
||||
|
||||
self.inner_run(program, exec_state, false).await
|
||||
}
|
||||
|
||||
/// Perform the execution of a program using an (experimental!) concurrent
|
||||
/// execution model. This has the same signature as [Self::run].
|
||||
///
|
||||
/// For now -- do not use this unless you're willing to accept some
|
||||
/// breakage.
|
||||
///
|
||||
/// You can optionally pass in some initialization memory for partial
|
||||
/// execution.
|
||||
///
|
||||
/// To access non-fatal errors and warnings, extract them from the `ExecState`.
|
||||
pub async fn run_concurrent(
|
||||
&self,
|
||||
program: &crate::Program,
|
||||
exec_state: &mut ExecState,
|
||||
preserve_mem: bool,
|
||||
) -> Result<(EnvironmentRef, Option<ModelingSessionData>), KclErrorWithOutputs> {
|
||||
exec_state.add_root_module_contents(program);
|
||||
self.eval_prelude(exec_state, SourceRange::synthetic())
|
||||
.await
|
||||
.map_err(KclErrorWithOutputs::no_outputs)?;
|
||||
|
||||
let mut universe = std::collections::HashMap::new();
|
||||
|
||||
let default_planes = self.engine.get_default_planes().read().await.clone();
|
||||
crate::walk::import_universe(self, &program.ast, &mut universe, exec_state)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
let module_id_to_module_path: IndexMap<ModuleId, ModulePath> = exec_state
|
||||
.global
|
||||
.path_to_source_id
|
||||
.iter()
|
||||
.map(|(k, v)| ((*v), k.clone()))
|
||||
.collect();
|
||||
|
||||
KclErrorWithOutputs::new(
|
||||
err,
|
||||
exec_state.global.operations.clone(),
|
||||
exec_state.global.artifact_commands.clone(),
|
||||
exec_state.global.artifact_graph.clone(),
|
||||
module_id_to_module_path,
|
||||
exec_state.global.id_to_source.clone(),
|
||||
default_planes.clone(),
|
||||
)
|
||||
})?;
|
||||
|
||||
for modules in crate::walk::import_graph(&universe, self)
|
||||
.map_err(|err| {
|
||||
let module_id_to_module_path: IndexMap<ModuleId, ModulePath> = exec_state
|
||||
.global
|
||||
.path_to_source_id
|
||||
.iter()
|
||||
.map(|(k, v)| ((*v), k.clone()))
|
||||
.collect();
|
||||
|
||||
KclErrorWithOutputs::new(
|
||||
err,
|
||||
exec_state.global.operations.clone(),
|
||||
exec_state.global.artifact_commands.clone(),
|
||||
exec_state.global.artifact_graph.clone(),
|
||||
module_id_to_module_path,
|
||||
exec_state.global.id_to_source.clone(),
|
||||
default_planes.clone(),
|
||||
)
|
||||
})?
|
||||
.into_iter()
|
||||
{
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let mut set = tokio::task::JoinSet::new();
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
let (results_tx, mut results_rx): (
|
||||
tokio::sync::mpsc::Sender<(
|
||||
ModuleId,
|
||||
ModulePath,
|
||||
Result<(Option<KclValue>, EnvironmentRef, Vec<String>), KclError>,
|
||||
)>,
|
||||
tokio::sync::mpsc::Receiver<_>,
|
||||
) = tokio::sync::mpsc::channel(1);
|
||||
|
||||
for module in modules {
|
||||
let Some((import_stmt, module_id, module_path, program)) = universe.get(&module) else {
|
||||
return Err(KclErrorWithOutputs::no_outputs(KclError::Internal(KclErrorDetails {
|
||||
message: format!("Module {module} not found in universe"),
|
||||
source_ranges: Default::default(),
|
||||
})));
|
||||
};
|
||||
let module_id = *module_id;
|
||||
let module_path = module_path.clone();
|
||||
let program = program.clone();
|
||||
let exec_state = exec_state.clone();
|
||||
let exec_ctxt = self.clone();
|
||||
let results_tx = results_tx.clone();
|
||||
let source_range = SourceRange::from(import_stmt);
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
//set.spawn(async move {
|
||||
let mut exec_state = exec_state;
|
||||
let exec_ctxt = exec_ctxt;
|
||||
|
||||
let result = exec_ctxt
|
||||
.exec_module_from_ast(
|
||||
&program,
|
||||
module_id,
|
||||
&module_path,
|
||||
&mut exec_state,
|
||||
source_range,
|
||||
false,
|
||||
)
|
||||
.await;
|
||||
|
||||
results_tx
|
||||
.send((module_id, module_path, result))
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
});
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
set.spawn(async move {
|
||||
let mut exec_state = exec_state;
|
||||
let exec_ctxt = exec_ctxt;
|
||||
|
||||
let result = exec_ctxt
|
||||
.exec_module_from_ast(
|
||||
&program,
|
||||
module_id,
|
||||
&module_path,
|
||||
&mut exec_state,
|
||||
source_range,
|
||||
false,
|
||||
)
|
||||
.await;
|
||||
|
||||
results_tx
|
||||
.send((module_id, module_path, result))
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
drop(results_tx);
|
||||
|
||||
while let Some((module_id, _, result)) = results_rx.recv().await {
|
||||
match result {
|
||||
Ok((val, session_data, variables)) => {
|
||||
let mut repr = exec_state.global.module_infos[&module_id].take_repr();
|
||||
|
||||
let ModuleRepr::Kcl(_, cache) = &mut repr else {
|
||||
continue;
|
||||
};
|
||||
*cache = Some((val, session_data, variables));
|
||||
|
||||
exec_state.global.module_infos[&module_id].restore_repr(repr);
|
||||
}
|
||||
Err(e) => {
|
||||
let module_id_to_module_path: IndexMap<ModuleId, ModulePath> = exec_state
|
||||
.global
|
||||
.path_to_source_id
|
||||
.iter()
|
||||
.map(|(k, v)| ((*v), k.clone()))
|
||||
.collect();
|
||||
|
||||
return Err(KclErrorWithOutputs::new(
|
||||
e,
|
||||
exec_state.global.operations.clone(),
|
||||
exec_state.global.artifact_commands.clone(),
|
||||
exec_state.global.artifact_graph.clone(),
|
||||
module_id_to_module_path,
|
||||
exec_state.global.id_to_source.clone(),
|
||||
default_planes,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.inner_run(program, exec_state, preserve_mem).await
|
||||
}
|
||||
|
||||
/// Perform the execution of a program. Accept all possible parameters and
|
||||
/// output everything.
|
||||
async fn inner_run(
|
||||
@ -716,8 +913,6 @@ impl ExecutorContext {
|
||||
exec_state: &mut ExecState,
|
||||
preserve_mem: bool,
|
||||
) -> Result<(EnvironmentRef, Option<ModelingSessionData>), KclErrorWithOutputs> {
|
||||
exec_state.add_root_module_contents(program);
|
||||
|
||||
let _stats = crate::log::LogPerfStats::new("Interpretation");
|
||||
|
||||
// Re-apply the settings, in case the cache was busted.
|
||||
@ -783,7 +978,6 @@ impl ExecutorContext {
|
||||
.exec_module_body(
|
||||
program,
|
||||
exec_state,
|
||||
ExecutionKind::Normal,
|
||||
preserve_mem,
|
||||
ModuleId::default(),
|
||||
&ModulePath::Main,
|
||||
@ -837,9 +1031,7 @@ impl ExecutorContext {
|
||||
source_range,
|
||||
)
|
||||
.await?;
|
||||
let (module_memory, _) = self
|
||||
.exec_module_for_items(id, exec_state, ExecutionKind::Isolated, source_range)
|
||||
.await?;
|
||||
let (module_memory, _) = self.exec_module_for_items(id, exec_state, source_range).await?;
|
||||
|
||||
exec_state.mut_stack().memory.set_std(module_memory);
|
||||
}
|
||||
@ -947,6 +1139,14 @@ impl ExecutorContext {
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) async fn parse_execute(code: &str) -> Result<ExecTestResults, KclError> {
|
||||
parse_execute_with_project_dir(code, None).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) async fn parse_execute_with_project_dir(
|
||||
code: &str,
|
||||
project_directory: Option<std::path::PathBuf>,
|
||||
) -> Result<ExecTestResults, KclError> {
|
||||
let program = crate::Program::parse_no_errs(code)?;
|
||||
|
||||
let exec_ctxt = ExecutorContext {
|
||||
@ -960,7 +1160,10 @@ pub(crate) async fn parse_execute(code: &str) -> Result<ExecTestResults, KclErro
|
||||
)),
|
||||
fs: Arc::new(crate::fs::FileManager::new()),
|
||||
stdlib: Arc::new(crate::std::StdLib::new()),
|
||||
settings: Default::default(),
|
||||
settings: ExecutorSettings {
|
||||
project_directory,
|
||||
..Default::default()
|
||||
},
|
||||
context_type: ContextType::Mock,
|
||||
};
|
||||
let mut exec_state = ExecState::new(&exec_ctxt);
|
||||
|
@ -229,6 +229,10 @@ impl ExecState {
|
||||
self.global.module_infos.insert(id, module_info);
|
||||
}
|
||||
|
||||
pub fn get_module(&mut self, id: ModuleId) -> Option<&ModuleInfo> {
|
||||
self.global.module_infos.get(&id)
|
||||
}
|
||||
|
||||
pub fn current_default_units(&self) -> NumericType {
|
||||
NumericType::Default {
|
||||
len: self.length_unit(),
|
||||
|
Reference in New Issue
Block a user