Skip to main content

kcl_lib/execution/
state.rs

1#[cfg(feature = "artifact-graph")]
2use std::collections::BTreeMap;
3use std::str::FromStr;
4use std::sync::Arc;
5
6use anyhow::Result;
7use indexmap::IndexMap;
8use kittycad_modeling_cmds::units::UnitAngle;
9use kittycad_modeling_cmds::units::UnitLength;
10use serde::Deserialize;
11use serde::Serialize;
12use uuid::Uuid;
13
14use crate::CompilationIssue;
15use crate::EngineManager;
16use crate::ExecutorContext;
17use crate::KclErrorWithOutputs;
18use crate::MockConfig;
19#[cfg(feature = "artifact-graph")]
20use crate::NodePath;
21use crate::SourceRange;
22use crate::collections::AhashIndexSet;
23use crate::errors::KclError;
24use crate::errors::KclErrorDetails;
25use crate::errors::Severity;
26use crate::exec::DefaultPlanes;
27#[cfg(feature = "artifact-graph")]
28use crate::execution::Artifact;
29#[cfg(feature = "artifact-graph")]
30use crate::execution::ArtifactCommand;
31#[cfg(feature = "artifact-graph")]
32use crate::execution::ArtifactGraph;
33#[cfg(feature = "artifact-graph")]
34use crate::execution::ArtifactId;
35use crate::execution::EnvironmentRef;
36use crate::execution::ExecOutcome;
37use crate::execution::ExecutorSettings;
38use crate::execution::KclValue;
39#[cfg(feature = "artifact-graph")]
40use crate::execution::ProgramLookup;
41use crate::execution::SketchVarId;
42use crate::execution::UnsolvedSegment;
43use crate::execution::annotations;
44use crate::execution::cad_op::Operation;
45use crate::execution::id_generator::IdGenerator;
46use crate::execution::memory::ProgramMemory;
47use crate::execution::memory::Stack;
48#[cfg(feature = "artifact-graph")]
49use crate::execution::sketch_solve::Solved;
50use crate::execution::types::NumericType;
51#[cfg(feature = "artifact-graph")]
52use crate::front::Number;
53use crate::front::Object;
54use crate::front::ObjectId;
55use crate::id::IncIdGenerator;
56use crate::modules::ModuleId;
57use crate::modules::ModuleInfo;
58use crate::modules::ModuleLoader;
59use crate::modules::ModulePath;
60use crate::modules::ModuleRepr;
61use crate::modules::ModuleSource;
62use crate::parsing::ast::types::Annotation;
63use crate::parsing::ast::types::NodeRef;
64use crate::parsing::ast::types::TagNode;
65
66/// State for executing a program.
67#[derive(Debug, Clone)]
68pub struct ExecState {
69    pub(super) global: GlobalState,
70    pub(super) mod_local: ModuleState,
71}
72
73pub type ModuleInfoMap = IndexMap<ModuleId, ModuleInfo>;
74
75#[derive(Debug, Clone)]
76pub(super) struct GlobalState {
77    /// Map from source file absolute path to module ID.
78    pub path_to_source_id: IndexMap<ModulePath, ModuleId>,
79    /// Map from module ID to source file.
80    pub id_to_source: IndexMap<ModuleId, ModuleSource>,
81    /// Map from module ID to module info.
82    pub module_infos: ModuleInfoMap,
83    /// Module loader.
84    pub mod_loader: ModuleLoader,
85    /// Errors and warnings.
86    pub issues: Vec<CompilationIssue>,
87    /// Global artifacts that represent the entire program.
88    pub artifacts: ArtifactState,
89    /// Artifacts for only the root module.
90    pub root_module_artifacts: ModuleArtifactState,
91    /// The segments that were edited that triggered this execution.
92    #[cfg(feature = "artifact-graph")]
93    pub segment_ids_edited: AhashIndexSet<ObjectId>,
94}
95
96#[cfg(feature = "artifact-graph")]
97#[derive(Debug, Clone, Default)]
98pub(super) struct ArtifactState {
99    /// Internal map of UUIDs to exec artifacts.  This needs to persist across
100    /// executions to allow the graph building to refer to cached artifacts.
101    pub artifacts: IndexMap<ArtifactId, Artifact>,
102    /// Output artifact graph.
103    pub graph: ArtifactGraph,
104}
105
106#[cfg(not(feature = "artifact-graph"))]
107#[derive(Debug, Clone, Default)]
108pub(super) struct ArtifactState {}
109
110/// Artifact state for a single module.
111#[cfg(feature = "artifact-graph")]
112#[derive(Debug, Clone, Default, PartialEq, Serialize)]
113pub struct ModuleArtifactState {
114    /// Internal map of UUIDs to exec artifacts.
115    pub artifacts: IndexMap<ArtifactId, Artifact>,
116    /// Outgoing engine commands that have not yet been processed and integrated
117    /// into the artifact graph.
118    #[serde(skip)]
119    pub unprocessed_commands: Vec<ArtifactCommand>,
120    /// Outgoing engine commands.
121    pub commands: Vec<ArtifactCommand>,
122    /// Operations that have been performed in execution order, for display in
123    /// the Feature Tree.
124    pub operations: Vec<Operation>,
125    /// [`ObjectId`] generator.
126    pub object_id_generator: IncIdGenerator<usize>,
127    /// Objects in the scene, created from execution.
128    pub scene_objects: Vec<Object>,
129    /// Map from source range to object ID for lookup of objects by their source
130    /// range.
131    pub source_range_to_object: BTreeMap<SourceRange, ObjectId>,
132    /// Map from artifact ID to object ID in the scene.
133    pub artifact_id_to_scene_object: IndexMap<ArtifactId, ObjectId>,
134    /// Solutions for sketch variables.
135    pub var_solutions: Vec<(SourceRange, Number)>,
136}
137
138#[cfg(not(feature = "artifact-graph"))]
139#[derive(Debug, Clone, Default, PartialEq, Serialize)]
140pub struct ModuleArtifactState {
141    /// [`ObjectId`] generator.
142    pub object_id_generator: IncIdGenerator<usize>,
143}
144
145#[derive(Debug, Clone)]
146pub(super) struct ModuleState {
147    /// The id generator for this module.
148    pub id_generator: IdGenerator,
149    pub stack: Stack,
150    /// The size of the call stack. This is used to prevent stack overflows with
151    /// recursive function calls. In general, this doesn't match `stack`'s size
152    /// since it's conservative in reclaiming frames between executions.
153    pub(super) call_stack_size: usize,
154    /// The current value of the pipe operator returned from the previous
155    /// expression.  If we're not currently in a pipeline, this will be None.
156    pub pipe_value: Option<KclValue>,
157    /// The closest variable declaration being executed in any parent node in the AST.
158    /// This is used to provide better error messages, e.g. noticing when the user is trying
159    /// to use the variable `length` inside the RHS of its own definition, like `length = tan(length)`.
160    pub being_declared: Option<String>,
161    /// Present if we're currently executing inside a sketch block.
162    pub sketch_block: Option<SketchBlockState>,
163    /// Tracks if KCL being executed is currently inside a stdlib function or not.
164    /// This matters because e.g. we shouldn't emit artifacts from declarations declared inside a stdlib function.
165    pub inside_stdlib: bool,
166    /// The source range where we entered the standard library.
167    pub stdlib_entry_source_range: Option<SourceRange>,
168    /// Identifiers that have been exported from the current module.
169    pub module_exports: Vec<String>,
170    /// Settings specified from annotations.
171    pub settings: MetaSettings,
172    /// True if executing in sketch mode. Only a single sketch block will be
173    /// executed. All other code is ignored.
174    pub sketch_mode: bool,
175    /// True to do more costly analysis of whether the sketch block segments are
176    /// under-constrained. The only time we disable this is when a user is
177    /// dragging segments.
178    pub freedom_analysis: bool,
179    pub(super) explicit_length_units: bool,
180    pub(super) path: ModulePath,
181    /// Artifacts for only this module.
182    pub artifacts: ModuleArtifactState,
183
184    pub(super) allowed_warnings: Vec<&'static str>,
185    pub(super) denied_warnings: Vec<&'static str>,
186}
187
188#[derive(Debug, Clone, Default)]
189pub(crate) struct SketchBlockState {
190    pub sketch_vars: Vec<KclValue>,
191    #[cfg(feature = "artifact-graph")]
192    pub sketch_id: Option<ObjectId>,
193    #[cfg(feature = "artifact-graph")]
194    pub sketch_constraints: Vec<ObjectId>,
195    pub solver_constraints: Vec<ezpz::Constraint>,
196    pub solver_optional_constraints: Vec<ezpz::Constraint>,
197    pub needed_by_engine: Vec<UnsolvedSegment>,
198    pub segment_tags: IndexMap<ObjectId, TagNode>,
199}
200
201impl ExecState {
202    pub fn new(exec_context: &super::ExecutorContext) -> Self {
203        ExecState {
204            global: GlobalState::new(&exec_context.settings, Default::default()),
205            mod_local: ModuleState::new(ModulePath::Main, ProgramMemory::new(), Default::default(), false, true),
206        }
207    }
208
209    pub fn new_mock(exec_context: &super::ExecutorContext, mock_config: &MockConfig) -> Self {
210        #[cfg(feature = "artifact-graph")]
211        let segment_ids_edited = mock_config.segment_ids_edited.clone();
212        #[cfg(not(feature = "artifact-graph"))]
213        let segment_ids_edited = Default::default();
214        ExecState {
215            global: GlobalState::new(&exec_context.settings, segment_ids_edited),
216            mod_local: ModuleState::new(
217                ModulePath::Main,
218                ProgramMemory::new(),
219                Default::default(),
220                mock_config.sketch_block_id.is_some(),
221                mock_config.freedom_analysis,
222            ),
223        }
224    }
225
226    pub(super) fn reset(&mut self, exec_context: &super::ExecutorContext) {
227        let global = GlobalState::new(&exec_context.settings, Default::default());
228
229        *self = ExecState {
230            global,
231            mod_local: ModuleState::new(
232                self.mod_local.path.clone(),
233                ProgramMemory::new(),
234                Default::default(),
235                false,
236                true,
237            ),
238        };
239    }
240
241    /// Log a non-fatal error.
242    pub fn err(&mut self, e: CompilationIssue) {
243        self.global.issues.push(e);
244    }
245
246    /// Log a warning.
247    pub fn warn(&mut self, mut e: CompilationIssue, name: &'static str) {
248        debug_assert!(annotations::WARN_VALUES.contains(&name));
249
250        if self.mod_local.allowed_warnings.contains(&name) {
251            return;
252        }
253
254        if self.mod_local.denied_warnings.contains(&name) {
255            e.severity = Severity::Error;
256        } else {
257            e.severity = Severity::Warning;
258        }
259
260        self.global.issues.push(e);
261    }
262
263    pub fn warn_experimental(&mut self, feature_name: &str, source_range: SourceRange) {
264        let Some(severity) = self.mod_local.settings.experimental_features.severity() else {
265            return;
266        };
267        let error = CompilationIssue {
268            source_range,
269            message: format!("Use of {feature_name} is experimental and may change or be removed."),
270            suggestion: None,
271            severity,
272            tag: crate::errors::Tag::None,
273        };
274
275        self.global.issues.push(error);
276    }
277
278    pub fn clear_units_warnings(&mut self, source_range: &SourceRange) {
279        self.global.issues = std::mem::take(&mut self.global.issues)
280            .into_iter()
281            .filter(|e| {
282                e.severity != Severity::Warning
283                    || !source_range.contains_range(&e.source_range)
284                    || e.tag != crate::errors::Tag::UnknownNumericUnits
285            })
286            .collect();
287    }
288
289    pub fn issues(&self) -> &[CompilationIssue] {
290        &self.global.issues
291    }
292
293    /// Convert to execution outcome when running in WebAssembly.  We want to
294    /// reduce the amount of data that crosses the WASM boundary as much as
295    /// possible.
296    pub async fn into_exec_outcome(self, main_ref: EnvironmentRef, ctx: &ExecutorContext) -> ExecOutcome {
297        // Fields are opt-in so that we don't accidentally leak private internal
298        // state when we add more to ExecState.
299        ExecOutcome {
300            variables: self.mod_local.variables(main_ref),
301            filenames: self.global.filenames(),
302            #[cfg(feature = "artifact-graph")]
303            operations: self.global.root_module_artifacts.operations,
304            #[cfg(feature = "artifact-graph")]
305            artifact_graph: self.global.artifacts.graph,
306            #[cfg(feature = "artifact-graph")]
307            scene_objects: self.global.root_module_artifacts.scene_objects,
308            #[cfg(feature = "artifact-graph")]
309            source_range_to_object: self.global.root_module_artifacts.source_range_to_object,
310            #[cfg(feature = "artifact-graph")]
311            var_solutions: self.global.root_module_artifacts.var_solutions,
312            issues: self.global.issues,
313            default_planes: ctx.engine.get_default_planes().read().await.clone(),
314        }
315    }
316
317    pub(crate) fn stack(&self) -> &Stack {
318        &self.mod_local.stack
319    }
320
321    pub(crate) fn mut_stack(&mut self) -> &mut Stack {
322        &mut self.mod_local.stack
323    }
324
325    /// Increment the user-level call stack size, returning an error if it
326    /// exceeds the maximum.
327    pub(super) fn inc_call_stack_size(&mut self, range: SourceRange) -> Result<(), KclError> {
328        // If you change this, make sure to test in WebAssembly in the app since
329        // that's the limiting factor.
330        if self.mod_local.call_stack_size >= 50 {
331            return Err(KclError::MaxCallStack {
332                details: KclErrorDetails::new("maximum call stack size exceeded".to_owned(), vec![range]),
333            });
334        }
335        self.mod_local.call_stack_size += 1;
336        Ok(())
337    }
338
339    /// Decrement the user-level call stack size, returning an error if it would
340    /// go below zero.
341    pub(super) fn dec_call_stack_size(&mut self, range: SourceRange) -> Result<(), KclError> {
342        // Prevent underflow.
343        if self.mod_local.call_stack_size == 0 {
344            let message = "call stack size below zero".to_owned();
345            debug_assert!(false, "{message}");
346            return Err(KclError::new_internal(KclErrorDetails::new(message, vec![range])));
347        }
348        self.mod_local.call_stack_size -= 1;
349        Ok(())
350    }
351
352    /// Returns true if we're executing in sketch mode for the current module.
353    /// In sketch mode, we still want to execute the prelude and other stdlib
354    /// modules as normal, so it can vary per module within a single overall
355    /// execution.
356    pub(crate) fn sketch_mode(&self) -> bool {
357        self.mod_local.sketch_mode
358            && match &self.mod_local.path {
359                ModulePath::Main => true,
360                ModulePath::Local { .. } => true,
361                ModulePath::Std { .. } => false,
362            }
363    }
364
365    pub fn next_object_id(&mut self) -> ObjectId {
366        ObjectId(self.mod_local.artifacts.object_id_generator.next_id())
367    }
368
369    pub fn peek_object_id(&self) -> ObjectId {
370        ObjectId(self.mod_local.artifacts.object_id_generator.peek_id())
371    }
372
373    #[cfg(feature = "artifact-graph")]
374    pub fn add_scene_object(&mut self, obj: Object, source_range: SourceRange) -> ObjectId {
375        let id = obj.id;
376        debug_assert!(
377            id.0 == self.mod_local.artifacts.scene_objects.len(),
378            "Adding scene object with ID {} but next ID is {}",
379            id.0,
380            self.mod_local.artifacts.scene_objects.len()
381        );
382        let artifact_id = obj.artifact_id;
383        self.mod_local.artifacts.scene_objects.push(obj);
384        self.mod_local.artifacts.source_range_to_object.insert(source_range, id);
385        self.mod_local
386            .artifacts
387            .artifact_id_to_scene_object
388            .insert(artifact_id, id);
389        id
390    }
391
392    /// Add a placeholder scene object. This is useful when we need to reserve
393    /// an ID before we have all the information to create the full object.
394    #[cfg(feature = "artifact-graph")]
395    pub fn add_placeholder_scene_object(
396        &mut self,
397        id: ObjectId,
398        source_range: SourceRange,
399        node_path: Option<NodePath>,
400    ) -> ObjectId {
401        debug_assert!(id.0 == self.mod_local.artifacts.scene_objects.len());
402        self.mod_local
403            .artifacts
404            .scene_objects
405            .push(Object::placeholder(id, source_range, node_path));
406        self.mod_local.artifacts.source_range_to_object.insert(source_range, id);
407        id
408    }
409
410    /// Update a scene object. This is useful to replace a placeholder.
411    #[cfg(feature = "artifact-graph")]
412    pub fn set_scene_object(&mut self, object: Object) {
413        let id = object.id;
414        let artifact_id = object.artifact_id;
415        self.mod_local.artifacts.scene_objects[id.0] = object;
416        self.mod_local
417            .artifacts
418            .artifact_id_to_scene_object
419            .insert(artifact_id, id);
420    }
421
422    #[cfg(feature = "artifact-graph")]
423    pub fn scene_object_id_by_artifact_id(&self, artifact_id: ArtifactId) -> Option<ObjectId> {
424        self.mod_local
425            .artifacts
426            .artifact_id_to_scene_object
427            .get(&artifact_id)
428            .cloned()
429    }
430
431    #[cfg(feature = "artifact-graph")]
432    pub fn segment_ids_edited_contains(&self, object_id: &ObjectId) -> bool {
433        self.global.segment_ids_edited.contains(object_id)
434    }
435
436    pub(super) fn is_in_sketch_block(&self) -> bool {
437        self.mod_local.sketch_block.is_some()
438    }
439
440    pub(crate) fn sketch_block_mut(&mut self) -> Option<&mut SketchBlockState> {
441        self.mod_local.sketch_block.as_mut()
442    }
443
444    pub fn next_uuid(&mut self) -> Uuid {
445        self.mod_local.id_generator.next_uuid()
446    }
447
448    #[cfg(feature = "artifact-graph")]
449    pub fn next_artifact_id(&mut self) -> ArtifactId {
450        self.mod_local.id_generator.next_artifact_id()
451    }
452
453    pub fn id_generator(&mut self) -> &mut IdGenerator {
454        &mut self.mod_local.id_generator
455    }
456
457    #[cfg(feature = "artifact-graph")]
458    pub(crate) fn add_artifact(&mut self, artifact: Artifact) {
459        let id = artifact.id();
460        self.mod_local.artifacts.artifacts.insert(id, artifact);
461    }
462
463    pub(crate) fn push_op(&mut self, op: Operation) {
464        #[cfg(feature = "artifact-graph")]
465        self.mod_local.artifacts.operations.push(op);
466        #[cfg(not(feature = "artifact-graph"))]
467        drop(op);
468    }
469
470    #[cfg(feature = "artifact-graph")]
471    pub(crate) fn push_command(&mut self, command: ArtifactCommand) {
472        self.mod_local.artifacts.unprocessed_commands.push(command);
473        #[cfg(not(feature = "artifact-graph"))]
474        drop(command);
475    }
476
477    pub(super) fn next_module_id(&self) -> ModuleId {
478        ModuleId::from_usize(self.global.path_to_source_id.len())
479    }
480
481    pub(super) fn id_for_module(&self, path: &ModulePath) -> Option<ModuleId> {
482        self.global.path_to_source_id.get(path).cloned()
483    }
484
485    pub(super) fn add_path_to_source_id(&mut self, path: ModulePath, id: ModuleId) {
486        debug_assert!(!self.global.path_to_source_id.contains_key(&path));
487        self.global.path_to_source_id.insert(path, id);
488    }
489
490    pub(crate) fn add_root_module_contents(&mut self, program: &crate::Program) {
491        let root_id = ModuleId::default();
492        // Get the path for the root module.
493        let path = self
494            .global
495            .path_to_source_id
496            .iter()
497            .find(|(_, v)| **v == root_id)
498            .unwrap()
499            .0
500            .clone();
501        self.add_id_to_source(
502            root_id,
503            ModuleSource {
504                path,
505                source: program.original_file_contents.to_string(),
506            },
507        );
508    }
509
510    pub(super) fn add_id_to_source(&mut self, id: ModuleId, source: ModuleSource) {
511        self.global.id_to_source.insert(id, source);
512    }
513
514    pub(super) fn add_module(&mut self, id: ModuleId, path: ModulePath, repr: ModuleRepr) {
515        debug_assert!(self.global.path_to_source_id.contains_key(&path));
516        let module_info = ModuleInfo { id, repr, path };
517        self.global.module_infos.insert(id, module_info);
518    }
519
520    pub fn get_module(&mut self, id: ModuleId) -> Option<&ModuleInfo> {
521        self.global.module_infos.get(&id)
522    }
523
524    #[cfg(all(test, feature = "artifact-graph"))]
525    pub(crate) fn modules(&self) -> &ModuleInfoMap {
526        &self.global.module_infos
527    }
528
529    #[cfg(all(test, feature = "artifact-graph"))]
530    pub(crate) fn root_module_artifact_state(&self) -> &ModuleArtifactState {
531        &self.global.root_module_artifacts
532    }
533
534    pub fn current_default_units(&self) -> NumericType {
535        NumericType::Default {
536            len: self.length_unit(),
537            angle: self.angle_unit(),
538        }
539    }
540
541    pub fn length_unit(&self) -> UnitLength {
542        self.mod_local.settings.default_length_units
543    }
544
545    pub fn angle_unit(&self) -> UnitAngle {
546        self.mod_local.settings.default_angle_units
547    }
548
549    pub(super) fn circular_import_error(&self, path: &ModulePath, source_range: SourceRange) -> KclError {
550        KclError::new_import_cycle(KclErrorDetails::new(
551            format!(
552                "circular import of modules is not allowed: {} -> {}",
553                self.global
554                    .mod_loader
555                    .import_stack
556                    .iter()
557                    .map(|p| p.to_string_lossy())
558                    .collect::<Vec<_>>()
559                    .join(" -> "),
560                path,
561            ),
562            vec![source_range],
563        ))
564    }
565
566    pub(crate) fn pipe_value(&self) -> Option<&KclValue> {
567        self.mod_local.pipe_value.as_ref()
568    }
569
570    pub(crate) fn error_with_outputs(
571        &self,
572        error: KclError,
573        main_ref: Option<EnvironmentRef>,
574        default_planes: Option<DefaultPlanes>,
575    ) -> KclErrorWithOutputs {
576        let module_id_to_module_path: IndexMap<ModuleId, ModulePath> = self
577            .global
578            .path_to_source_id
579            .iter()
580            .map(|(k, v)| ((*v), k.clone()))
581            .collect();
582
583        KclErrorWithOutputs::new(
584            error,
585            self.issues().to_vec(),
586            main_ref
587                .map(|main_ref| self.mod_local.variables(main_ref))
588                .unwrap_or_default(),
589            #[cfg(feature = "artifact-graph")]
590            self.global.root_module_artifacts.operations.clone(),
591            #[cfg(feature = "artifact-graph")]
592            Default::default(),
593            #[cfg(feature = "artifact-graph")]
594            self.global.artifacts.graph.clone(),
595            #[cfg(feature = "artifact-graph")]
596            self.global.root_module_artifacts.scene_objects.clone(),
597            #[cfg(feature = "artifact-graph")]
598            self.global.root_module_artifacts.source_range_to_object.clone(),
599            #[cfg(feature = "artifact-graph")]
600            self.global.root_module_artifacts.var_solutions.clone(),
601            module_id_to_module_path,
602            self.global.id_to_source.clone(),
603            default_planes,
604        )
605    }
606
607    #[cfg(feature = "artifact-graph")]
608    pub(crate) fn build_program_lookup(
609        &self,
610        current: crate::parsing::ast::types::Node<crate::parsing::ast::types::Program>,
611    ) -> ProgramLookup {
612        ProgramLookup::new(current, self.global.module_infos.clone())
613    }
614
615    #[cfg(feature = "artifact-graph")]
616    pub(crate) async fn build_artifact_graph(
617        &mut self,
618        engine: &Arc<Box<dyn EngineManager>>,
619        program: NodeRef<'_, crate::parsing::ast::types::Program>,
620    ) -> Result<(), KclError> {
621        let mut new_commands = Vec::new();
622        let mut new_exec_artifacts = IndexMap::new();
623        for module in self.global.module_infos.values_mut() {
624            match &mut module.repr {
625                ModuleRepr::Kcl(_, Some(outcome)) => {
626                    new_commands.extend(outcome.artifacts.process_commands());
627                    new_exec_artifacts.extend(outcome.artifacts.artifacts.clone());
628                }
629                ModuleRepr::Foreign(_, Some((_, module_artifacts))) => {
630                    new_commands.extend(module_artifacts.process_commands());
631                    new_exec_artifacts.extend(module_artifacts.artifacts.clone());
632                }
633                ModuleRepr::Root | ModuleRepr::Kcl(_, None) | ModuleRepr::Foreign(_, None) | ModuleRepr::Dummy => {}
634            }
635        }
636        // Take from the module artifacts so that we don't try to process them
637        // again next time due to execution caching.
638        new_commands.extend(self.global.root_module_artifacts.process_commands());
639        // Note: These will get re-processed, but since we're just adding them
640        // to a map, it's fine.
641        new_exec_artifacts.extend(self.global.root_module_artifacts.artifacts.clone());
642        let new_responses = engine.take_responses().await;
643
644        // Move the artifacts into ExecState global to simplify cache
645        // management.
646        for (id, exec_artifact) in new_exec_artifacts {
647            // Only insert if it wasn't already present. We don't want to
648            // overwrite what was previously there. We haven't filled in node
649            // paths yet.
650            self.global.artifacts.artifacts.entry(id).or_insert(exec_artifact);
651        }
652
653        let initial_graph = self.global.artifacts.graph.clone();
654
655        // Build the artifact graph.
656        let programs = self.build_program_lookup(program.clone());
657        let graph_result = crate::execution::artifact::build_artifact_graph(
658            &new_commands,
659            &new_responses,
660            program,
661            &mut self.global.artifacts.artifacts,
662            initial_graph,
663            &programs,
664            &self.global.module_infos,
665        );
666
667        let artifact_graph = graph_result?;
668        self.global.artifacts.graph = artifact_graph;
669
670        Ok(())
671    }
672
673    #[cfg(not(feature = "artifact-graph"))]
674    pub(crate) async fn build_artifact_graph(
675        &mut self,
676        _engine: &Arc<Box<dyn EngineManager>>,
677        _program: NodeRef<'_, crate::parsing::ast::types::Program>,
678    ) -> Result<(), KclError> {
679        Ok(())
680    }
681}
682
683impl GlobalState {
684    fn new(settings: &ExecutorSettings, segment_ids_edited: AhashIndexSet<ObjectId>) -> Self {
685        #[cfg(not(feature = "artifact-graph"))]
686        drop(segment_ids_edited);
687        let mut global = GlobalState {
688            path_to_source_id: Default::default(),
689            module_infos: Default::default(),
690            artifacts: Default::default(),
691            root_module_artifacts: Default::default(),
692            mod_loader: Default::default(),
693            issues: Default::default(),
694            id_to_source: Default::default(),
695            #[cfg(feature = "artifact-graph")]
696            segment_ids_edited,
697        };
698
699        let root_id = ModuleId::default();
700        let root_path = settings.current_file.clone().unwrap_or_default();
701        global.module_infos.insert(
702            root_id,
703            ModuleInfo {
704                id: root_id,
705                path: ModulePath::Local {
706                    value: root_path.clone(),
707                    original_import_path: None,
708                },
709                repr: ModuleRepr::Root,
710            },
711        );
712        global.path_to_source_id.insert(
713            ModulePath::Local {
714                value: root_path,
715                original_import_path: None,
716            },
717            root_id,
718        );
719        global
720    }
721
722    pub(super) fn filenames(&self) -> IndexMap<ModuleId, ModulePath> {
723        self.path_to_source_id.iter().map(|(k, v)| ((*v), k.clone())).collect()
724    }
725
726    pub(super) fn get_source(&self, id: ModuleId) -> Option<&ModuleSource> {
727        self.id_to_source.get(&id)
728    }
729}
730
731impl ArtifactState {
732    #[cfg(feature = "artifact-graph")]
733    pub fn cached_body_items(&self) -> usize {
734        self.graph.item_count
735    }
736
737    pub(crate) fn clear(&mut self) {
738        #[cfg(feature = "artifact-graph")]
739        {
740            self.artifacts.clear();
741            self.graph.clear();
742        }
743    }
744}
745
746impl ModuleArtifactState {
747    pub(crate) fn clear(&mut self) {
748        #[cfg(feature = "artifact-graph")]
749        {
750            self.artifacts.clear();
751            self.unprocessed_commands.clear();
752            self.commands.clear();
753            self.operations.clear();
754        }
755    }
756
757    #[cfg(feature = "artifact-graph")]
758    pub(crate) fn restore_scene_objects(&mut self, scene_objects: &[Object]) {
759        self.scene_objects = scene_objects.to_vec();
760        self.object_id_generator = IncIdGenerator::new(self.scene_objects.len());
761        self.source_range_to_object.clear();
762        self.artifact_id_to_scene_object.clear();
763
764        for (expected_id, object) in self.scene_objects.iter().enumerate() {
765            debug_assert_eq!(
766                object.id.0, expected_id,
767                "Restored cached scene object ID {} does not match its position {}",
768                object.id.0, expected_id
769            );
770
771            match &object.source {
772                crate::front::SourceRef::Simple { range, node_path: _ } => {
773                    self.source_range_to_object.insert(*range, object.id);
774                }
775                crate::front::SourceRef::BackTrace { ranges } => {
776                    // Don't map the entire backtrace, only the most specific
777                    // range.
778                    if let Some((range, _)) = ranges.first() {
779                        self.source_range_to_object.insert(*range, object.id);
780                    }
781                }
782            }
783
784            // Ignore placeholder artifacts.
785            if object.artifact_id != ArtifactId::placeholder() {
786                self.artifact_id_to_scene_object.insert(object.artifact_id, object.id);
787            }
788        }
789    }
790
791    #[cfg(not(feature = "artifact-graph"))]
792    pub(crate) fn extend(&mut self, _other: ModuleArtifactState) {}
793
794    /// When self is a cached state, extend it with new state.
795    #[cfg(feature = "artifact-graph")]
796    pub(crate) fn extend(&mut self, other: ModuleArtifactState) {
797        self.artifacts.extend(other.artifacts);
798        self.unprocessed_commands.extend(other.unprocessed_commands);
799        self.commands.extend(other.commands);
800        self.operations.extend(other.operations);
801        if other.scene_objects.len() > self.scene_objects.len() {
802            self.scene_objects
803                .extend(other.scene_objects[self.scene_objects.len()..].iter().cloned());
804        }
805        self.source_range_to_object.extend(other.source_range_to_object);
806        self.artifact_id_to_scene_object
807            .extend(other.artifact_id_to_scene_object);
808        self.var_solutions.extend(other.var_solutions);
809    }
810
811    // Move unprocessed artifact commands so that we don't try to process them
812    // again next time due to execution caching.  Returns a clone of the
813    // commands that were moved.
814    #[cfg(feature = "artifact-graph")]
815    pub(crate) fn process_commands(&mut self) -> Vec<ArtifactCommand> {
816        let unprocessed = std::mem::take(&mut self.unprocessed_commands);
817        let new_module_commands = unprocessed.clone();
818        self.commands.extend(unprocessed);
819        new_module_commands
820    }
821
822    #[cfg_attr(not(feature = "artifact-graph"), expect(dead_code))]
823    pub(crate) fn scene_object_by_id(&self, id: ObjectId) -> Option<&Object> {
824        #[cfg(feature = "artifact-graph")]
825        {
826            debug_assert!(
827                id.0 < self.scene_objects.len(),
828                "Requested object ID {} but only have {} objects",
829                id.0,
830                self.scene_objects.len()
831            );
832            self.scene_objects.get(id.0)
833        }
834        #[cfg(not(feature = "artifact-graph"))]
835        {
836            let _ = id;
837            None
838        }
839    }
840
841    #[cfg_attr(not(feature = "artifact-graph"), expect(dead_code))]
842    pub(crate) fn scene_object_by_id_mut(&mut self, id: ObjectId) -> Option<&mut Object> {
843        #[cfg(feature = "artifact-graph")]
844        {
845            debug_assert!(
846                id.0 < self.scene_objects.len(),
847                "Requested object ID {} but only have {} objects",
848                id.0,
849                self.scene_objects.len()
850            );
851            self.scene_objects.get_mut(id.0)
852        }
853        #[cfg(not(feature = "artifact-graph"))]
854        {
855            let _ = id;
856            None
857        }
858    }
859}
860
861impl ModuleState {
862    pub(super) fn new(
863        path: ModulePath,
864        memory: Arc<ProgramMemory>,
865        module_id: Option<ModuleId>,
866        sketch_mode: bool,
867        freedom_analysis: bool,
868    ) -> Self {
869        ModuleState {
870            id_generator: IdGenerator::new(module_id),
871            stack: memory.new_stack(),
872            call_stack_size: 0,
873            pipe_value: Default::default(),
874            being_declared: Default::default(),
875            sketch_block: Default::default(),
876            stdlib_entry_source_range: Default::default(),
877            module_exports: Default::default(),
878            explicit_length_units: false,
879            path,
880            settings: Default::default(),
881            sketch_mode,
882            freedom_analysis,
883            artifacts: Default::default(),
884            allowed_warnings: Vec::new(),
885            denied_warnings: Vec::new(),
886            inside_stdlib: false,
887        }
888    }
889
890    pub(super) fn variables(&self, main_ref: EnvironmentRef) -> IndexMap<String, KclValue> {
891        self.stack
892            .find_all_in_env(main_ref)
893            .map(|(k, v)| (k.clone(), v.clone()))
894            .collect()
895    }
896}
897
898impl SketchBlockState {
899    pub(crate) fn next_sketch_var_id(&self) -> SketchVarId {
900        SketchVarId(self.sketch_vars.len())
901    }
902
903    /// Given a solve outcome, return the solutions for the sketch variables and
904    /// enough information to update them in the source.
905    #[cfg(feature = "artifact-graph")]
906    pub(crate) fn var_solutions(
907        &self,
908        solve_outcome: &Solved,
909        solution_ty: NumericType,
910        range: SourceRange,
911    ) -> Result<Vec<(SourceRange, Number)>, KclError> {
912        self.sketch_vars
913            .iter()
914            .map(|v| {
915                let Some(sketch_var) = v.as_sketch_var() else {
916                    return Err(KclError::new_internal(KclErrorDetails::new(
917                        "Expected sketch variable".to_owned(),
918                        vec![range],
919                    )));
920                };
921                let var_index = sketch_var.id.0;
922                let solved_n = solve_outcome.final_values.get(var_index).ok_or_else(|| {
923                    let message = format!("No solution for sketch variable with id {}", var_index);
924                    debug_assert!(false, "{}", &message);
925                    KclError::new_internal(KclErrorDetails::new(
926                        message,
927                        sketch_var.meta.iter().map(|m| m.source_range).collect(),
928                    ))
929                })?;
930                let solved_value = Number {
931                    value: *solved_n,
932                    units: solution_ty.try_into().map_err(|_| {
933                        KclError::new_internal(KclErrorDetails::new(
934                            "Failed to convert numeric type to units".to_owned(),
935                            vec![range],
936                        ))
937                    })?,
938                };
939                let Some(source_range) = sketch_var.meta.first().map(|m| m.source_range) else {
940                    return Ok(None);
941                };
942                Ok(Some((source_range, solved_value)))
943            })
944            .filter_map(Result::transpose)
945            .collect::<Result<Vec<_>, KclError>>()
946    }
947}
948
949#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS)]
950#[ts(export)]
951#[serde(rename_all = "camelCase")]
952pub struct MetaSettings {
953    pub default_length_units: UnitLength,
954    pub default_angle_units: UnitAngle,
955    pub experimental_features: annotations::WarningLevel,
956    pub kcl_version: String,
957}
958
959impl Default for MetaSettings {
960    fn default() -> Self {
961        MetaSettings {
962            default_length_units: UnitLength::Millimeters,
963            default_angle_units: UnitAngle::Degrees,
964            experimental_features: annotations::WarningLevel::Deny,
965            kcl_version: "1.0".to_owned(),
966        }
967    }
968}
969
970impl MetaSettings {
971    pub(crate) fn update_from_annotation(
972        &mut self,
973        annotation: &crate::parsing::ast::types::Node<Annotation>,
974    ) -> Result<(bool, bool), KclError> {
975        let properties = annotations::expect_properties(annotations::SETTINGS, annotation)?;
976
977        let mut updated_len = false;
978        let mut updated_angle = false;
979        for p in properties {
980            match &*p.inner.key.name {
981                annotations::SETTINGS_UNIT_LENGTH => {
982                    let value = annotations::expect_ident(&p.inner.value)?;
983                    let value = super::types::length_from_str(value, annotation.as_source_range())?;
984                    self.default_length_units = value;
985                    updated_len = true;
986                }
987                annotations::SETTINGS_UNIT_ANGLE => {
988                    let value = annotations::expect_ident(&p.inner.value)?;
989                    let value = super::types::angle_from_str(value, annotation.as_source_range())?;
990                    self.default_angle_units = value;
991                    updated_angle = true;
992                }
993                annotations::SETTINGS_VERSION => {
994                    let value = annotations::expect_number(&p.inner.value)?;
995                    self.kcl_version = value;
996                }
997                annotations::SETTINGS_EXPERIMENTAL_FEATURES => {
998                    let value = annotations::expect_ident(&p.inner.value)?;
999                    let value = annotations::WarningLevel::from_str(value).map_err(|_| {
1000                        KclError::new_semantic(KclErrorDetails::new(
1001                            format!(
1002                                "Invalid value for {} settings property, expected one of: {}",
1003                                annotations::SETTINGS_EXPERIMENTAL_FEATURES,
1004                                annotations::WARN_LEVELS.join(", ")
1005                            ),
1006                            annotation.as_source_ranges(),
1007                        ))
1008                    })?;
1009                    self.experimental_features = value;
1010                }
1011                name => {
1012                    return Err(KclError::new_semantic(KclErrorDetails::new(
1013                        format!(
1014                            "Unexpected settings key: `{name}`; expected one of `{}`, `{}`",
1015                            annotations::SETTINGS_UNIT_LENGTH,
1016                            annotations::SETTINGS_UNIT_ANGLE
1017                        ),
1018                        vec![annotation.as_source_range()],
1019                    )));
1020                }
1021            }
1022        }
1023
1024        Ok((updated_len, updated_angle))
1025    }
1026}
1027
1028#[cfg(all(feature = "artifact-graph", test))]
1029mod tests {
1030    use uuid::Uuid;
1031
1032    use super::ModuleArtifactState;
1033    use crate::NodePath;
1034    use crate::SourceRange;
1035    use crate::execution::ArtifactId;
1036    use crate::front::Object;
1037    use crate::front::ObjectId;
1038    use crate::front::ObjectKind;
1039    use crate::front::Plane;
1040    use crate::front::SourceRef;
1041
1042    #[test]
1043    fn restore_scene_objects_rebuilds_lookup_maps() {
1044        let plane_artifact_id = ArtifactId::new(Uuid::from_u128(1));
1045        let sketch_artifact_id = ArtifactId::new(Uuid::from_u128(2));
1046        let plane_range = SourceRange::from([1, 4, 0]);
1047        let plane_node_path = Some(NodePath::placeholder());
1048        let sketch_ranges = vec![
1049            (SourceRange::from([5, 9, 0]), None),
1050            (SourceRange::from([10, 12, 0]), None),
1051        ];
1052        let cached_objects = vec![
1053            Object {
1054                id: ObjectId(0),
1055                kind: ObjectKind::Plane(Plane::Object(ObjectId(0))),
1056                label: Default::default(),
1057                comments: Default::default(),
1058                artifact_id: plane_artifact_id,
1059                source: SourceRef::new(plane_range, plane_node_path),
1060            },
1061            Object {
1062                id: ObjectId(1),
1063                kind: ObjectKind::Nil,
1064                label: Default::default(),
1065                comments: Default::default(),
1066                artifact_id: sketch_artifact_id,
1067                source: SourceRef::BackTrace {
1068                    ranges: sketch_ranges.clone(),
1069                },
1070            },
1071            Object::placeholder(ObjectId(2), SourceRange::from([13, 14, 0]), None),
1072        ];
1073
1074        let mut artifacts = ModuleArtifactState::default();
1075        artifacts.restore_scene_objects(&cached_objects);
1076
1077        assert_eq!(artifacts.scene_objects, cached_objects);
1078        assert_eq!(
1079            artifacts.artifact_id_to_scene_object.get(&plane_artifact_id),
1080            Some(&ObjectId(0))
1081        );
1082        assert_eq!(
1083            artifacts.artifact_id_to_scene_object.get(&sketch_artifact_id),
1084            Some(&ObjectId(1))
1085        );
1086        assert_eq!(
1087            artifacts.artifact_id_to_scene_object.get(&ArtifactId::placeholder()),
1088            None
1089        );
1090        assert_eq!(artifacts.source_range_to_object.get(&plane_range), Some(&ObjectId(0)));
1091        assert_eq!(
1092            artifacts.source_range_to_object.get(&sketch_ranges[0].0),
1093            Some(&ObjectId(1))
1094        );
1095        // We don't map all the ranges in a backtrace.
1096        assert_eq!(artifacts.source_range_to_object.get(&sketch_ranges[1].0), None);
1097    }
1098}