Skip to main content

kcl_lib/execution/
state.rs

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