Reapply "More aggressive using of cache on engine settings changes" (#4736)
* Reapply "More aggressive using of cache on engine settings changes (#4691)" (#4729)
This reverts commit 3f1f40eeba.
* Add a utility to get all the current values from the settings object
* Use an XState selector to get the latest settings snapshot for WASM
---------
Co-authored-by: Frank Noirot <frank@kittycad.io>
This commit is contained in:
@ -120,6 +120,61 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set the visibility of edges.
|
||||
async fn set_edge_visibility(
|
||||
&self,
|
||||
visible: bool,
|
||||
source_range: SourceRange,
|
||||
) -> Result<(), crate::errors::KclError> {
|
||||
self.batch_modeling_cmd(
|
||||
uuid::Uuid::new_v4(),
|
||||
source_range,
|
||||
&ModelingCmd::from(mcmd::EdgeLinesVisible { hidden: !visible }),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_units(
|
||||
&self,
|
||||
units: crate::UnitLength,
|
||||
source_range: SourceRange,
|
||||
) -> Result<(), crate::errors::KclError> {
|
||||
// Before we even start executing the program, set the units.
|
||||
self.batch_modeling_cmd(
|
||||
uuid::Uuid::new_v4(),
|
||||
source_range,
|
||||
&ModelingCmd::from(mcmd::SetSceneUnits { unit: units.into() }),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Re-run the command to apply the settings.
|
||||
async fn reapply_settings(
|
||||
&self,
|
||||
settings: &crate::ExecutorSettings,
|
||||
source_range: SourceRange,
|
||||
) -> Result<(), crate::errors::KclError> {
|
||||
// Set the edge visibility.
|
||||
self.set_edge_visibility(settings.highlight_edges, source_range).await?;
|
||||
|
||||
// Change the units.
|
||||
self.set_units(settings.units, source_range).await?;
|
||||
|
||||
// Send the command to show the grid.
|
||||
self.modify_grid(!settings.show_grid, source_range).await?;
|
||||
|
||||
// We do not have commands for changing ssao on the fly.
|
||||
|
||||
// Flush the batch queue, so the settings are applied right away.
|
||||
self.flush_batch(false, source_range).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Add a modeling command to the batch but don't fire it right away.
|
||||
async fn batch_modeling_cmd(
|
||||
&self,
|
||||
@ -504,11 +559,11 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
|
||||
}))
|
||||
}
|
||||
|
||||
async fn modify_grid(&self, hidden: bool) -> Result<(), KclError> {
|
||||
async fn modify_grid(&self, hidden: bool, source_range: SourceRange) -> Result<(), KclError> {
|
||||
// Hide/show the grid.
|
||||
self.batch_modeling_cmd(
|
||||
uuid::Uuid::new_v4(),
|
||||
Default::default(),
|
||||
source_range,
|
||||
&ModelingCmd::from(mcmd::ObjectVisible {
|
||||
hidden,
|
||||
object_id: *GRID_OBJECT_ID,
|
||||
@ -519,7 +574,7 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
|
||||
// Hide/show the grid scale text.
|
||||
self.batch_modeling_cmd(
|
||||
uuid::Uuid::new_v4(),
|
||||
Default::default(),
|
||||
source_range,
|
||||
&ModelingCmd::from(mcmd::ObjectVisible {
|
||||
hidden,
|
||||
object_id: *GRID_SCALE_TEXT_OBJECT_ID,
|
||||
@ -527,8 +582,6 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
|
||||
)
|
||||
.await?;
|
||||
|
||||
self.flush_batch(false, Default::default()).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
50
src/wasm-lib/kcl/src/execution/cache.rs
Normal file
50
src/wasm-lib/kcl/src/execution/cache.rs
Normal file
@ -0,0 +1,50 @@
|
||||
//! Functions for helping with caching an ast and finding the parts the changed.
|
||||
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
execution::ExecState,
|
||||
parsing::ast::types::{Node, Program},
|
||||
};
|
||||
|
||||
/// Information for the caching an AST and smartly re-executing it if we can.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
pub struct CacheInformation {
|
||||
/// The old information.
|
||||
pub old: Option<OldAstState>,
|
||||
/// The new ast to executed.
|
||||
pub new_ast: Node<Program>,
|
||||
}
|
||||
|
||||
/// The old ast and program memory.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
pub struct OldAstState {
|
||||
/// The ast.
|
||||
pub ast: Node<Program>,
|
||||
/// The exec state.
|
||||
pub exec_state: ExecState,
|
||||
/// The last settings used for execution.
|
||||
pub settings: crate::execution::ExecutorSettings,
|
||||
}
|
||||
|
||||
impl From<crate::Program> for CacheInformation {
|
||||
fn from(program: crate::Program) -> Self {
|
||||
CacheInformation {
|
||||
old: None,
|
||||
new_ast: program.ast,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The result of a cache check.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
pub struct CacheResult {
|
||||
/// Should we clear the scene and start over?
|
||||
pub clear_scene: bool,
|
||||
/// The program that needs to be executed.
|
||||
pub program: Node<Program>,
|
||||
}
|
||||
@ -23,15 +23,18 @@ type Point3D = kcmc::shared::Point3d<f64>;
|
||||
pub use function_param::FunctionParam;
|
||||
pub use kcl_value::{KclObjectFields, KclValue};
|
||||
|
||||
pub(crate) mod cache;
|
||||
mod exec_ast;
|
||||
mod function_param;
|
||||
mod kcl_value;
|
||||
|
||||
use crate::{
|
||||
engine::{EngineManager, ExecutionKind},
|
||||
errors::{KclError, KclErrorDetails},
|
||||
execution::cache::{CacheInformation, CacheResult},
|
||||
fs::{FileManager, FileSystem},
|
||||
parsing::ast::{
|
||||
cache::{get_changed_program, CacheInformation},
|
||||
types::{
|
||||
BodyItem, Expr, FunctionExpression, ImportSelector, ItemVisibility, Node, NodeRef, TagDeclarator, TagNode,
|
||||
},
|
||||
parsing::ast::types::{
|
||||
BodyItem, Expr, FunctionExpression, ImportSelector, ItemVisibility, Node, NodeRef, TagDeclarator, TagNode,
|
||||
},
|
||||
settings::types::UnitLength,
|
||||
source_range::{ModuleId, SourceRange},
|
||||
@ -39,10 +42,6 @@ use crate::{
|
||||
ExecError, Program,
|
||||
};
|
||||
|
||||
mod exec_ast;
|
||||
mod function_param;
|
||||
mod kcl_value;
|
||||
|
||||
/// State for executing a program.
|
||||
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
@ -1660,17 +1659,6 @@ impl ExecutorContext {
|
||||
let engine: Arc<Box<dyn EngineManager>> =
|
||||
Arc::new(Box::new(crate::engine::conn::EngineConnection::new(ws).await?));
|
||||
|
||||
// Set the edge visibility.
|
||||
engine
|
||||
.batch_modeling_cmd(
|
||||
uuid::Uuid::new_v4(),
|
||||
SourceRange::default(),
|
||||
&ModelingCmd::from(mcmd::EdgeLinesVisible {
|
||||
hidden: !settings.highlight_edges,
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Self {
|
||||
engine,
|
||||
fs: Arc::new(FileManager::new()),
|
||||
@ -1697,7 +1685,7 @@ impl ExecutorContext {
|
||||
pub async fn new(
|
||||
engine_manager: crate::engine::conn_wasm::EngineCommandManager,
|
||||
fs_manager: crate::fs::wasm::FileSystemManager,
|
||||
units: UnitLength,
|
||||
settings: ExecutorSettings,
|
||||
) -> Result<Self, String> {
|
||||
Ok(ExecutorContext {
|
||||
engine: Arc::new(Box::new(
|
||||
@ -1707,16 +1695,16 @@ impl ExecutorContext {
|
||||
)),
|
||||
fs: Arc::new(FileManager::new(fs_manager)),
|
||||
stdlib: Arc::new(StdLib::new()),
|
||||
settings: ExecutorSettings {
|
||||
units,
|
||||
..Default::default()
|
||||
},
|
||||
settings,
|
||||
context_type: ContextType::Live,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub async fn new_mock(fs_manager: crate::fs::wasm::FileSystemManager, units: UnitLength) -> Result<Self, String> {
|
||||
pub async fn new_mock(
|
||||
fs_manager: crate::fs::wasm::FileSystemManager,
|
||||
settings: ExecutorSettings,
|
||||
) -> Result<Self, String> {
|
||||
Ok(ExecutorContext {
|
||||
engine: Arc::new(Box::new(
|
||||
crate::engine::conn_mock::EngineConnection::new()
|
||||
@ -1725,10 +1713,7 @@ impl ExecutorContext {
|
||||
)),
|
||||
fs: Arc::new(FileManager::new(fs_manager)),
|
||||
stdlib: Arc::new(StdLib::new()),
|
||||
settings: ExecutorSettings {
|
||||
units,
|
||||
..Default::default()
|
||||
},
|
||||
settings,
|
||||
context_type: ContextType::Mock,
|
||||
})
|
||||
}
|
||||
@ -1817,6 +1802,71 @@ impl ExecutorContext {
|
||||
// AND if we aren't in wasm it doesn't really matter.
|
||||
Ok(())
|
||||
}
|
||||
// Given an old ast, old program memory and new ast, find the parts of the code that need to be
|
||||
// re-executed.
|
||||
// This function should never error, because in the case of any internal error, we should just pop
|
||||
// the cache.
|
||||
pub async fn get_changed_program(&self, info: CacheInformation) -> Option<CacheResult> {
|
||||
let Some(old) = info.old else {
|
||||
// We have no old info, we need to re-execute the whole thing.
|
||||
return Some(CacheResult {
|
||||
clear_scene: true,
|
||||
program: info.new_ast,
|
||||
});
|
||||
};
|
||||
|
||||
// If the settings are different we might need to bust the cache.
|
||||
// We specifically do this before checking if they are the exact same.
|
||||
if old.settings != self.settings {
|
||||
// If the units are different we need to re-execute the whole thing.
|
||||
if old.settings.units != self.settings.units {
|
||||
return Some(CacheResult {
|
||||
clear_scene: true,
|
||||
program: info.new_ast,
|
||||
});
|
||||
}
|
||||
|
||||
// If anything else is different we do not need to re-execute, but rather just
|
||||
// run the settings again.
|
||||
|
||||
if self
|
||||
.engine
|
||||
.reapply_settings(&self.settings, Default::default())
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
// Bust the cache, we errored.
|
||||
return Some(CacheResult {
|
||||
clear_scene: true,
|
||||
program: info.new_ast,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// If the ASTs are the EXACT same we return None.
|
||||
// We don't even need to waste time computing the digests.
|
||||
if old.ast == info.new_ast {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut old_ast = old.ast.inner;
|
||||
old_ast.compute_digest();
|
||||
let mut new_ast = info.new_ast.inner.clone();
|
||||
new_ast.compute_digest();
|
||||
|
||||
// Check if the digest is the same.
|
||||
if old_ast.digest == new_ast.digest {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Check if the changes were only to Non-code areas, like comments or whitespace.
|
||||
|
||||
// For any unhandled cases just re-execute the whole thing.
|
||||
Some(CacheResult {
|
||||
clear_scene: true,
|
||||
program: info.new_ast,
|
||||
})
|
||||
}
|
||||
|
||||
/// Perform the execution of a program.
|
||||
/// You can optionally pass in some initialization memory.
|
||||
@ -1837,7 +1887,7 @@ impl ExecutorContext {
|
||||
let _stats = crate::log::LogPerfStats::new("Interpretation");
|
||||
|
||||
// Get the program that actually changed from the old and new information.
|
||||
let cache_result = get_changed_program(cache_info.clone(), &self.settings);
|
||||
let cache_result = self.get_changed_program(cache_info.clone()).await;
|
||||
|
||||
// Check if we don't need to re-execute.
|
||||
let Some(cache_result) = cache_result else {
|
||||
@ -1854,23 +1904,9 @@ impl ExecutorContext {
|
||||
|
||||
// TODO: Use the top-level file's path.
|
||||
exec_state.add_module(std::path::PathBuf::from(""));
|
||||
// Before we even start executing the program, set the units.
|
||||
self.engine
|
||||
.batch_modeling_cmd(
|
||||
exec_state.id_generator.next_uuid(),
|
||||
SourceRange::default(),
|
||||
&ModelingCmd::from(mcmd::SetSceneUnits {
|
||||
unit: match self.settings.units {
|
||||
UnitLength::Cm => kcmc::units::UnitLength::Centimeters,
|
||||
UnitLength::Ft => kcmc::units::UnitLength::Feet,
|
||||
UnitLength::In => kcmc::units::UnitLength::Inches,
|
||||
UnitLength::M => kcmc::units::UnitLength::Meters,
|
||||
UnitLength::Mm => kcmc::units::UnitLength::Millimeters,
|
||||
UnitLength::Yd => kcmc::units::UnitLength::Yards,
|
||||
},
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Re-apply the settings, in case the cache was busted.
|
||||
self.engine.reapply_settings(&self.settings, Default::default()).await?;
|
||||
|
||||
self.inner_execute(&cache_result.program, exec_state, crate::execution::BodyType::Root)
|
||||
.await?;
|
||||
@ -2147,23 +2183,8 @@ impl ExecutorContext {
|
||||
self.settings.units = units;
|
||||
}
|
||||
|
||||
/// Execute the program, then get a PNG screenshot.
|
||||
pub async fn execute_and_prepare_snapshot(
|
||||
&self,
|
||||
program: &Program,
|
||||
exec_state: &mut ExecState,
|
||||
) -> std::result::Result<TakeSnapshot, ExecError> {
|
||||
self.execute_and_prepare(program, exec_state).await
|
||||
}
|
||||
|
||||
/// Execute the program, return the interpreter and outputs.
|
||||
pub async fn execute_and_prepare(
|
||||
&self,
|
||||
program: &Program,
|
||||
exec_state: &mut ExecState,
|
||||
) -> std::result::Result<TakeSnapshot, ExecError> {
|
||||
self.run(program.clone().into(), exec_state).await?;
|
||||
|
||||
/// Get a snapshot of the current scene.
|
||||
pub async fn prepare_snapshot(&self) -> std::result::Result<TakeSnapshot, ExecError> {
|
||||
// Zoom to fit.
|
||||
self.engine
|
||||
.send_modeling_cmd(
|
||||
@ -2199,6 +2220,17 @@ impl ExecutorContext {
|
||||
};
|
||||
Ok(contents)
|
||||
}
|
||||
|
||||
/// Execute the program, then get a PNG screenshot.
|
||||
pub async fn execute_and_prepare_snapshot(
|
||||
&self,
|
||||
program: &Program,
|
||||
exec_state: &mut ExecState,
|
||||
) -> std::result::Result<TakeSnapshot, ExecError> {
|
||||
self.run(program.clone().into(), exec_state).await?;
|
||||
|
||||
self.prepare_snapshot().await
|
||||
}
|
||||
}
|
||||
|
||||
/// For each argument given,
|
||||
@ -2378,9 +2410,12 @@ mod tests {
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::*;
|
||||
use crate::parsing::ast::types::{DefaultParamVal, Identifier, Node, Parameter};
|
||||
use crate::{
|
||||
parsing::ast::types::{DefaultParamVal, Identifier, Node, Parameter},
|
||||
OldAstState,
|
||||
};
|
||||
|
||||
pub async fn parse_execute(code: &str) -> Result<ProgramMemory> {
|
||||
pub async fn parse_execute(code: &str) -> Result<(Program, ExecutorContext, ExecState)> {
|
||||
let program = Program::parse_no_errs(code)?;
|
||||
|
||||
let ctx = ExecutorContext {
|
||||
@ -2391,9 +2426,9 @@ mod tests {
|
||||
context_type: ContextType::Mock,
|
||||
};
|
||||
let mut exec_state = ExecState::default();
|
||||
ctx.run(program.into(), &mut exec_state).await?;
|
||||
ctx.run(program.clone().into(), &mut exec_state).await?;
|
||||
|
||||
Ok(exec_state.memory)
|
||||
Ok((program, ctx, exec_state))
|
||||
}
|
||||
|
||||
/// Convenience function to get a JSON value from memory and unwrap.
|
||||
@ -2804,36 +2839,39 @@ let shape = layer() |> patternTransform(10, transform, %)
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_math_execute_with_functions() {
|
||||
let ast = r#"const myVar = 2 + min(100, -1 + legLen(5, 3))"#;
|
||||
let memory = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(5.0, mem_get_json(&memory, "myVar").as_f64().unwrap());
|
||||
let (_, _, exec_state) = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(5.0, mem_get_json(&exec_state.memory, "myVar").as_f64().unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_math_execute() {
|
||||
let ast = r#"const myVar = 1 + 2 * (3 - 4) / -5 + 6"#;
|
||||
let memory = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(7.4, mem_get_json(&memory, "myVar").as_f64().unwrap());
|
||||
let (_, _, exec_state) = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(7.4, mem_get_json(&exec_state.memory, "myVar").as_f64().unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_math_execute_start_negative() {
|
||||
let ast = r#"const myVar = -5 + 6"#;
|
||||
let memory = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(1.0, mem_get_json(&memory, "myVar").as_f64().unwrap());
|
||||
let (_, _, exec_state) = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(1.0, mem_get_json(&exec_state.memory, "myVar").as_f64().unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_math_execute_with_pi() {
|
||||
let ast = r#"const myVar = pi() * 2"#;
|
||||
let memory = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(std::f64::consts::TAU, mem_get_json(&memory, "myVar").as_f64().unwrap());
|
||||
let (_, _, exec_state) = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(
|
||||
std::f64::consts::TAU,
|
||||
mem_get_json(&exec_state.memory, "myVar").as_f64().unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_math_define_decimal_without_leading_zero() {
|
||||
let ast = r#"let thing = .4 + 7"#;
|
||||
let memory = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(7.4, mem_get_json(&memory, "thing").as_f64().unwrap());
|
||||
let (_, _, exec_state) = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(7.4, mem_get_json(&exec_state.memory, "thing").as_f64().unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
@ -2872,11 +2910,11 @@ fn check = (x) => {
|
||||
}
|
||||
check(false)
|
||||
"#;
|
||||
let mem = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(false, mem_get_json(&mem, "notTrue").as_bool().unwrap());
|
||||
assert_eq!(true, mem_get_json(&mem, "notFalse").as_bool().unwrap());
|
||||
assert_eq!(true, mem_get_json(&mem, "c").as_bool().unwrap());
|
||||
assert_eq!(false, mem_get_json(&mem, "d").as_bool().unwrap());
|
||||
let (_, _, exec_state) = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(false, mem_get_json(&exec_state.memory, "notTrue").as_bool().unwrap());
|
||||
assert_eq!(true, mem_get_json(&exec_state.memory, "notFalse").as_bool().unwrap());
|
||||
assert_eq!(true, mem_get_json(&exec_state.memory, "c").as_bool().unwrap());
|
||||
assert_eq!(false, mem_get_json(&exec_state.memory, "d").as_bool().unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
@ -3258,4 +3296,310 @@ let w = f() + f()
|
||||
let json = serde_json::to_string(&mem).unwrap();
|
||||
assert_eq!(json, r#"{"type":"Solids","value":[]}"#);
|
||||
}
|
||||
|
||||
// Easy case where we have no old ast and memory.
|
||||
// We need to re-execute everything.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_no_old_information() {
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
let (program, ctx, _) = parse_execute(new).await.unwrap();
|
||||
|
||||
let result = ctx
|
||||
.get_changed_program(CacheInformation {
|
||||
old: None,
|
||||
new_ast: program.ast.clone(),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert!(result.is_some());
|
||||
|
||||
let result = result.unwrap();
|
||||
|
||||
assert_eq!(result.program, program.ast);
|
||||
assert!(result.clear_scene);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code() {
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
|
||||
let (program, ctx, exec_state) = parse_execute(new).await.unwrap();
|
||||
|
||||
let result = ctx
|
||||
.get_changed_program(CacheInformation {
|
||||
old: Some(OldAstState {
|
||||
ast: program.ast.clone(),
|
||||
exec_state,
|
||||
settings: Default::default(),
|
||||
}),
|
||||
new_ast: program.ast.clone(),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code_changed_whitespace() {
|
||||
let old = r#" // Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch) "#;
|
||||
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
|
||||
let (program_old, ctx, exec_state) = parse_execute(old).await.unwrap();
|
||||
|
||||
let program_new = crate::Program::parse_no_errs(new).unwrap();
|
||||
|
||||
let result = ctx
|
||||
.get_changed_program(CacheInformation {
|
||||
old: Some(OldAstState {
|
||||
ast: program_old.ast.clone(),
|
||||
exec_state,
|
||||
settings: Default::default(),
|
||||
}),
|
||||
new_ast: program_new.ast.clone(),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code_changed_code_comment_start_of_program() {
|
||||
let old = r#" // Removed the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch) "#;
|
||||
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
|
||||
let (program, ctx, exec_state) = parse_execute(old).await.unwrap();
|
||||
|
||||
let program_new = crate::Program::parse_no_errs(new).unwrap();
|
||||
|
||||
let result = ctx
|
||||
.get_changed_program(CacheInformation {
|
||||
old: Some(OldAstState {
|
||||
ast: program.ast.clone(),
|
||||
exec_state,
|
||||
settings: Default::default(),
|
||||
}),
|
||||
new_ast: program_new.ast.clone(),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code_changed_code_comments() {
|
||||
let old = r#" // Removed the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %) // my thing
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch) "#;
|
||||
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
|
||||
let (program, ctx, exec_state) = parse_execute(old).await.unwrap();
|
||||
|
||||
let program_new = crate::Program::parse_no_errs(new).unwrap();
|
||||
|
||||
let result = ctx
|
||||
.get_changed_program(CacheInformation {
|
||||
old: Some(OldAstState {
|
||||
ast: program.ast.clone(),
|
||||
exec_state,
|
||||
settings: Default::default(),
|
||||
}),
|
||||
new_ast: program_new.ast.clone(),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert!(result.is_some());
|
||||
|
||||
let result = result.unwrap();
|
||||
|
||||
assert_eq!(result.program, program_new.ast);
|
||||
assert!(result.clear_scene);
|
||||
}
|
||||
|
||||
// Changing the units with the exact same file should bust the cache.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code_but_different_units() {
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
|
||||
let (program, mut ctx, exec_state) = parse_execute(new).await.unwrap();
|
||||
|
||||
// Change the settings to cm.
|
||||
ctx.settings.units = crate::UnitLength::Cm;
|
||||
|
||||
let result = ctx
|
||||
.get_changed_program(CacheInformation {
|
||||
old: Some(OldAstState {
|
||||
ast: program.ast.clone(),
|
||||
exec_state,
|
||||
settings: Default::default(),
|
||||
}),
|
||||
new_ast: program.ast.clone(),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert!(result.is_some());
|
||||
|
||||
let result = result.unwrap();
|
||||
|
||||
assert_eq!(result.program, program.ast);
|
||||
assert!(result.clear_scene);
|
||||
}
|
||||
|
||||
// Changing the grid settings with the exact same file should NOT bust the cache.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code_but_different_grid_setting() {
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
|
||||
let (program, mut ctx, exec_state) = parse_execute(new).await.unwrap();
|
||||
|
||||
// Change the settings.
|
||||
ctx.settings.show_grid = !ctx.settings.show_grid;
|
||||
|
||||
let result = ctx
|
||||
.get_changed_program(CacheInformation {
|
||||
old: Some(OldAstState {
|
||||
ast: program.ast.clone(),
|
||||
exec_state,
|
||||
settings: Default::default(),
|
||||
}),
|
||||
new_ast: program.ast.clone(),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
|
||||
// Changing the edge visibility settings with the exact same file should NOT bust the cache.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code_but_different_edge_visiblity_setting() {
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
|
||||
let (program, mut ctx, exec_state) = parse_execute(new).await.unwrap();
|
||||
|
||||
// Change the settings.
|
||||
ctx.settings.highlight_edges = !ctx.settings.highlight_edges;
|
||||
|
||||
let result = ctx
|
||||
.get_changed_program(CacheInformation {
|
||||
old: Some(OldAstState {
|
||||
ast: program.ast.clone(),
|
||||
exec_state,
|
||||
settings: Default::default(),
|
||||
}),
|
||||
new_ast: program.ast.clone(),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,16 +82,15 @@ mod wasm;
|
||||
pub use coredump::CoreDump;
|
||||
pub use engine::{EngineManager, ExecutionKind};
|
||||
pub use errors::{CompilationError, ConnectionError, ExecError, KclError};
|
||||
pub use execution::{ExecState, ExecutorContext, ExecutorSettings};
|
||||
pub use execution::{
|
||||
cache::{CacheInformation, OldAstState},
|
||||
ExecState, ExecutorContext, ExecutorSettings,
|
||||
};
|
||||
pub use lsp::{
|
||||
copilot::Backend as CopilotLspBackend,
|
||||
kcl::{Backend as KclLspBackend, Server as KclLspServerSubCommand},
|
||||
};
|
||||
pub use parsing::ast::{
|
||||
cache::{CacheInformation, OldAstState},
|
||||
modify::modify_ast_for_sketch,
|
||||
types::FormatOptions,
|
||||
};
|
||||
pub use parsing::ast::{modify::modify_ast_for_sketch, types::FormatOptions};
|
||||
pub use settings::types::{project::ProjectConfiguration, Configuration, UnitLength};
|
||||
pub use source_range::{ModuleId, SourceRange};
|
||||
|
||||
|
||||
@ -45,14 +45,11 @@ use crate::{
|
||||
errors::Suggestion,
|
||||
lsp::{backend::Backend as _, util::IntoDiagnostic},
|
||||
parsing::{
|
||||
ast::{
|
||||
cache::{CacheInformation, OldAstState},
|
||||
types::{Expr, Node, VariableKind},
|
||||
},
|
||||
ast::types::{Expr, Node, VariableKind},
|
||||
token::TokenStream,
|
||||
PIPE_OPERATOR,
|
||||
},
|
||||
ModuleId, Program, SourceRange,
|
||||
CacheInformation, ModuleId, OldAstState, Program, SourceRange,
|
||||
};
|
||||
const SEMANTIC_TOKEN_TYPES: [SemanticTokenType; 10] = [
|
||||
SemanticTokenType::NUMBER,
|
||||
|
||||
@ -1,373 +0,0 @@
|
||||
//! Functions for helping with caching an ast and finding the parts the changed.
|
||||
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
execution::ExecState,
|
||||
parsing::ast::types::{Node, Program},
|
||||
};
|
||||
|
||||
/// Information for the caching an AST and smartly re-executing it if we can.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
pub struct CacheInformation {
|
||||
/// The old information.
|
||||
pub old: Option<OldAstState>,
|
||||
/// The new ast to executed.
|
||||
pub new_ast: Node<Program>,
|
||||
}
|
||||
|
||||
/// The old ast and program memory.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
pub struct OldAstState {
|
||||
/// The ast.
|
||||
pub ast: Node<Program>,
|
||||
/// The exec state.
|
||||
pub exec_state: ExecState,
|
||||
/// The last settings used for execution.
|
||||
pub settings: crate::execution::ExecutorSettings,
|
||||
}
|
||||
|
||||
impl From<crate::Program> for CacheInformation {
|
||||
fn from(program: crate::Program) -> Self {
|
||||
CacheInformation {
|
||||
old: None,
|
||||
new_ast: program.ast,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The result of a cache check.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
pub struct CacheResult {
|
||||
/// Should we clear the scene and start over?
|
||||
pub clear_scene: bool,
|
||||
/// The program that needs to be executed.
|
||||
pub program: Node<Program>,
|
||||
}
|
||||
|
||||
// Given an old ast, old program memory and new ast, find the parts of the code that need to be
|
||||
// re-executed.
|
||||
// This function should never error, because in the case of any internal error, we should just pop
|
||||
// the cache.
|
||||
pub fn get_changed_program(
|
||||
info: CacheInformation,
|
||||
new_settings: &crate::execution::ExecutorSettings,
|
||||
) -> Option<CacheResult> {
|
||||
let Some(old) = info.old else {
|
||||
// We have no old info, we need to re-execute the whole thing.
|
||||
return Some(CacheResult {
|
||||
clear_scene: true,
|
||||
program: info.new_ast,
|
||||
});
|
||||
};
|
||||
|
||||
// If the settings are different we need to bust the cache.
|
||||
// We specifically do this before checking if they are the exact same.
|
||||
if old.settings != *new_settings {
|
||||
return Some(CacheResult {
|
||||
clear_scene: true,
|
||||
program: info.new_ast,
|
||||
});
|
||||
}
|
||||
|
||||
// If the ASTs are the EXACT same we return None.
|
||||
// We don't even need to waste time computing the digests.
|
||||
if old.ast == info.new_ast {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut old_ast = old.ast.inner;
|
||||
old_ast.compute_digest();
|
||||
let mut new_ast = info.new_ast.inner.clone();
|
||||
new_ast.compute_digest();
|
||||
|
||||
// Check if the digest is the same.
|
||||
if old_ast.digest == new_ast.digest {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Check if the changes were only to Non-code areas, like comments or whitespace.
|
||||
|
||||
// For any unhandled cases just re-execute the whole thing.
|
||||
Some(CacheResult {
|
||||
clear_scene: true,
|
||||
program: info.new_ast,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::*;
|
||||
|
||||
async fn execute(program: &crate::Program) -> Result<ExecState> {
|
||||
let ctx = crate::execution::ExecutorContext {
|
||||
engine: Arc::new(Box::new(crate::engine::conn_mock::EngineConnection::new().await?)),
|
||||
fs: Arc::new(crate::fs::FileManager::new()),
|
||||
stdlib: Arc::new(crate::std::StdLib::new()),
|
||||
settings: Default::default(),
|
||||
context_type: crate::execution::ContextType::Mock,
|
||||
};
|
||||
let mut exec_state = crate::execution::ExecState::default();
|
||||
ctx.run(program.clone().into(), &mut exec_state).await?;
|
||||
|
||||
Ok(exec_state)
|
||||
}
|
||||
|
||||
// Easy case where we have no old ast and memory.
|
||||
// We need to re-execute everything.
|
||||
#[test]
|
||||
fn test_get_changed_program_no_old_information() {
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
let program = crate::Program::parse_no_errs(new).unwrap().ast;
|
||||
|
||||
let result = get_changed_program(
|
||||
CacheInformation {
|
||||
old: None,
|
||||
new_ast: program.clone(),
|
||||
},
|
||||
&Default::default(),
|
||||
);
|
||||
|
||||
assert!(result.is_some());
|
||||
|
||||
let result = result.unwrap();
|
||||
|
||||
assert_eq!(result.program, program);
|
||||
assert!(result.clear_scene);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code() {
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
let program = crate::Program::parse_no_errs(new).unwrap();
|
||||
|
||||
let executed = execute(&program).await.unwrap();
|
||||
|
||||
let result = get_changed_program(
|
||||
CacheInformation {
|
||||
old: Some(OldAstState {
|
||||
ast: program.ast.clone(),
|
||||
exec_state: executed,
|
||||
settings: Default::default(),
|
||||
}),
|
||||
new_ast: program.ast.clone(),
|
||||
},
|
||||
&Default::default(),
|
||||
);
|
||||
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code_changed_whitespace() {
|
||||
let old = r#" // Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch) "#;
|
||||
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
let program_old = crate::Program::parse_no_errs(old).unwrap();
|
||||
|
||||
let executed = execute(&program_old).await.unwrap();
|
||||
|
||||
let program_new = crate::Program::parse_no_errs(new).unwrap();
|
||||
|
||||
let result = get_changed_program(
|
||||
CacheInformation {
|
||||
old: Some(OldAstState {
|
||||
ast: program_old.ast.clone(),
|
||||
exec_state: executed,
|
||||
settings: Default::default(),
|
||||
}),
|
||||
new_ast: program_new.ast.clone(),
|
||||
},
|
||||
&Default::default(),
|
||||
);
|
||||
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code_changed_code_comment_start_of_program() {
|
||||
let old = r#" // Removed the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch) "#;
|
||||
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
let program_old = crate::Program::parse_no_errs(old).unwrap();
|
||||
|
||||
let executed = execute(&program_old).await.unwrap();
|
||||
|
||||
let program_new = crate::Program::parse_no_errs(new).unwrap();
|
||||
|
||||
let result = get_changed_program(
|
||||
CacheInformation {
|
||||
old: Some(OldAstState {
|
||||
ast: program_old.ast.clone(),
|
||||
exec_state: executed,
|
||||
settings: Default::default(),
|
||||
}),
|
||||
new_ast: program_new.ast.clone(),
|
||||
},
|
||||
&Default::default(),
|
||||
);
|
||||
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code_changed_code_comments() {
|
||||
let old = r#" // Removed the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %) // my thing
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch) "#;
|
||||
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
let program_old = crate::Program::parse_no_errs(old).unwrap();
|
||||
|
||||
let executed = execute(&program_old).await.unwrap();
|
||||
|
||||
let program_new = crate::Program::parse_no_errs(new).unwrap();
|
||||
|
||||
let result = get_changed_program(
|
||||
CacheInformation {
|
||||
old: Some(OldAstState {
|
||||
ast: program_old.ast.clone(),
|
||||
exec_state: executed,
|
||||
settings: Default::default(),
|
||||
}),
|
||||
new_ast: program_new.ast.clone(),
|
||||
},
|
||||
&Default::default(),
|
||||
);
|
||||
|
||||
assert!(result.is_some());
|
||||
|
||||
let result = result.unwrap();
|
||||
|
||||
assert_eq!(result.program, program_new.ast);
|
||||
assert!(result.clear_scene);
|
||||
}
|
||||
|
||||
// Changing the units with the exact same file should bust the cache.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_get_changed_program_same_code_but_different_units() {
|
||||
let new = r#"// Remove the end face for the extrusion.
|
||||
firstSketch = startSketchOn('XY')
|
||||
|> startProfileAt([-12, 12], %)
|
||||
|> line([24, 0], %)
|
||||
|> line([0, -24], %)
|
||||
|> line([-24, 0], %)
|
||||
|> close(%)
|
||||
|> extrude(6, %)
|
||||
|
||||
// Remove the end face for the extrusion.
|
||||
shell({ faces = ['end'], thickness = 0.25 }, firstSketch)"#;
|
||||
let program = crate::Program::parse_no_errs(new).unwrap();
|
||||
|
||||
let executed = execute(&program).await.unwrap();
|
||||
|
||||
let result = get_changed_program(
|
||||
CacheInformation {
|
||||
old: Some(OldAstState {
|
||||
ast: program.ast.clone(),
|
||||
exec_state: executed,
|
||||
settings: Default::default(),
|
||||
}),
|
||||
new_ast: program.ast.clone(),
|
||||
},
|
||||
&crate::ExecutorSettings {
|
||||
units: crate::UnitLength::Cm,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
assert!(result.is_some());
|
||||
|
||||
let result = result.unwrap();
|
||||
|
||||
assert_eq!(result.program, program.ast);
|
||||
assert!(result.clear_scene);
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,3 @@
|
||||
pub(crate) mod cache;
|
||||
pub(crate) mod digest;
|
||||
pub mod modify;
|
||||
pub mod types;
|
||||
|
||||
@ -55,7 +55,11 @@ async fn do_execute_and_snapshot(
|
||||
program: Program,
|
||||
) -> Result<(crate::execution::ExecState, image::DynamicImage), ExecError> {
|
||||
let mut exec_state = Default::default();
|
||||
let snapshot_png_bytes = ctx.execute_and_prepare(&program, &mut exec_state).await?.contents.0;
|
||||
let snapshot_png_bytes = ctx
|
||||
.execute_and_prepare_snapshot(&program, &mut exec_state)
|
||||
.await?
|
||||
.contents
|
||||
.0;
|
||||
|
||||
// Decode the snapshot, return it.
|
||||
let img = image::ImageReader::new(std::io::Cursor::new(snapshot_png_bytes))
|
||||
|
||||
Reference in New Issue
Block a user