kcl_lib/execution/
state.rs

1use std::{str::FromStr, sync::Arc};
2
3use anyhow::Result;
4use indexmap::IndexMap;
5use kittycad_modeling_cmds::units::{UnitAngle, UnitLength};
6use serde::{Deserialize, Serialize};
7use uuid::Uuid;
8
9#[cfg(feature = "artifact-graph")]
10use crate::execution::{Artifact, ArtifactCommand, ArtifactGraph, ArtifactId, ProgramLookup};
11use crate::{
12    CompilationError, EngineManager, ExecutorContext, KclErrorWithOutputs, SourceRange,
13    errors::{KclError, KclErrorDetails, Severity},
14    exec::DefaultPlanes,
15    execution::{
16        EnvironmentRef, ExecOutcome, ExecutorSettings, KclValue, SketchVarId, annotations,
17        cad_op::Operation,
18        id_generator::IdGenerator,
19        memory::{ProgramMemory, Stack},
20        types::NumericType,
21    },
22    modules::{ModuleId, ModuleInfo, ModuleLoader, ModulePath, ModuleRepr, ModuleSource},
23    parsing::ast::types::{Annotation, NodeRef},
24};
25
26/// State for executing a program.
27#[derive(Debug, Clone)]
28pub struct ExecState {
29    pub(super) global: GlobalState,
30    pub(super) mod_local: ModuleState,
31}
32
33pub type ModuleInfoMap = IndexMap<ModuleId, ModuleInfo>;
34
35#[derive(Debug, Clone)]
36pub(super) struct GlobalState {
37    /// Map from source file absolute path to module ID.
38    pub path_to_source_id: IndexMap<ModulePath, ModuleId>,
39    /// Map from module ID to source file.
40    pub id_to_source: IndexMap<ModuleId, ModuleSource>,
41    /// Map from module ID to module info.
42    pub module_infos: ModuleInfoMap,
43    /// Module loader.
44    pub mod_loader: ModuleLoader,
45    /// Errors and warnings.
46    pub errors: Vec<CompilationError>,
47    /// Global artifacts that represent the entire program.
48    pub artifacts: ArtifactState,
49    /// Artifacts for only the root module.
50    pub root_module_artifacts: ModuleArtifactState,
51}
52
53#[cfg(feature = "artifact-graph")]
54#[derive(Debug, Clone, Default)]
55pub(super) struct ArtifactState {
56    /// Internal map of UUIDs to exec artifacts.  This needs to persist across
57    /// executions to allow the graph building to refer to cached artifacts.
58    pub artifacts: IndexMap<ArtifactId, Artifact>,
59    /// Output artifact graph.
60    pub graph: ArtifactGraph,
61}
62
63#[cfg(not(feature = "artifact-graph"))]
64#[derive(Debug, Clone, Default)]
65pub(super) struct ArtifactState {}
66
67/// Artifact state for a single module.
68#[cfg(feature = "artifact-graph")]
69#[derive(Debug, Clone, Default, PartialEq, Serialize)]
70pub struct ModuleArtifactState {
71    /// Internal map of UUIDs to exec artifacts.
72    pub artifacts: IndexMap<ArtifactId, Artifact>,
73    /// Outgoing engine commands that have not yet been processed and integrated
74    /// into the artifact graph.
75    #[serde(skip)]
76    pub unprocessed_commands: Vec<ArtifactCommand>,
77    /// Outgoing engine commands.
78    pub commands: Vec<ArtifactCommand>,
79    /// Operations that have been performed in execution order, for display in
80    /// the Feature Tree.
81    pub operations: Vec<Operation>,
82}
83
84#[cfg(not(feature = "artifact-graph"))]
85#[derive(Debug, Clone, Default, PartialEq, Serialize)]
86pub struct ModuleArtifactState {}
87
88#[derive(Debug, Clone)]
89pub(super) struct ModuleState {
90    /// The id generator for this module.
91    pub id_generator: IdGenerator,
92    pub stack: Stack,
93    /// The current value of the pipe operator returned from the previous
94    /// expression.  If we're not currently in a pipeline, this will be None.
95    pub pipe_value: Option<KclValue>,
96    /// The closest variable declaration being executed in any parent node in the AST.
97    /// This is used to provide better error messages, e.g. noticing when the user is trying
98    /// to use the variable `length` inside the RHS of its own definition, like `length = tan(length)`.
99    pub being_declared: Option<String>,
100    /// Present if we're currently executing inside a sketch block.
101    pub sketch_block: Option<SketchBlockState>,
102    /// Tracks if KCL being executed is currently inside a stdlib function or not.
103    /// This matters because e.g. we shouldn't emit artifacts from declarations declared inside a stdlib function.
104    pub inside_stdlib: bool,
105    /// The source range where we entered the standard library.
106    pub stdlib_entry_source_range: Option<SourceRange>,
107    /// Identifiers that have been exported from the current module.
108    pub module_exports: Vec<String>,
109    /// Settings specified from annotations.
110    pub settings: MetaSettings,
111    pub(super) explicit_length_units: bool,
112    pub(super) path: ModulePath,
113    /// Artifacts for only this module.
114    pub artifacts: ModuleArtifactState,
115
116    pub(super) allowed_warnings: Vec<&'static str>,
117    pub(super) denied_warnings: Vec<&'static str>,
118}
119
120#[derive(Debug, Clone, Default)]
121pub(super) struct SketchBlockState {
122    pub sketch_vars: Vec<KclValue>,
123    pub constraints: Vec<kcl_ezpz::Constraint>,
124}
125
126impl ExecState {
127    pub fn new(exec_context: &super::ExecutorContext) -> Self {
128        ExecState {
129            global: GlobalState::new(&exec_context.settings),
130            mod_local: ModuleState::new(ModulePath::Main, ProgramMemory::new(), Default::default()),
131        }
132    }
133
134    pub(super) fn reset(&mut self, exec_context: &super::ExecutorContext) {
135        let global = GlobalState::new(&exec_context.settings);
136
137        *self = ExecState {
138            global,
139            mod_local: ModuleState::new(self.mod_local.path.clone(), ProgramMemory::new(), Default::default()),
140        };
141    }
142
143    /// Log a non-fatal error.
144    pub fn err(&mut self, e: CompilationError) {
145        self.global.errors.push(e);
146    }
147
148    /// Log a warning.
149    pub fn warn(&mut self, mut e: CompilationError, name: &'static str) {
150        debug_assert!(annotations::WARN_VALUES.contains(&name));
151
152        if self.mod_local.allowed_warnings.contains(&name) {
153            return;
154        }
155
156        if self.mod_local.denied_warnings.contains(&name) {
157            e.severity = Severity::Error;
158        } else {
159            e.severity = Severity::Warning;
160        }
161
162        self.global.errors.push(e);
163    }
164
165    pub fn warn_experimental(&mut self, feature_name: &str, source_range: SourceRange) {
166        let Some(severity) = self.mod_local.settings.experimental_features.severity() else {
167            return;
168        };
169        let error = CompilationError {
170            source_range,
171            message: format!("Use of {feature_name} is experimental and may change or be removed."),
172            suggestion: None,
173            severity,
174            tag: crate::errors::Tag::None,
175        };
176
177        self.global.errors.push(error);
178    }
179
180    pub fn clear_units_warnings(&mut self, source_range: &SourceRange) {
181        self.global.errors = std::mem::take(&mut self.global.errors)
182            .into_iter()
183            .filter(|e| {
184                e.severity != Severity::Warning
185                    || !source_range.contains_range(&e.source_range)
186                    || e.tag != crate::errors::Tag::UnknownNumericUnits
187            })
188            .collect();
189    }
190
191    pub fn errors(&self) -> &[CompilationError] {
192        &self.global.errors
193    }
194
195    /// Convert to execution outcome when running in WebAssembly.  We want to
196    /// reduce the amount of data that crosses the WASM boundary as much as
197    /// possible.
198    pub async fn into_exec_outcome(self, main_ref: EnvironmentRef, ctx: &ExecutorContext) -> ExecOutcome {
199        // Fields are opt-in so that we don't accidentally leak private internal
200        // state when we add more to ExecState.
201        ExecOutcome {
202            variables: self.mod_local.variables(main_ref),
203            filenames: self.global.filenames(),
204            #[cfg(feature = "artifact-graph")]
205            operations: self.global.root_module_artifacts.operations,
206            #[cfg(feature = "artifact-graph")]
207            artifact_graph: self.global.artifacts.graph,
208            errors: self.global.errors,
209            default_planes: ctx.engine.get_default_planes().read().await.clone(),
210        }
211    }
212
213    pub(crate) fn stack(&self) -> &Stack {
214        &self.mod_local.stack
215    }
216
217    pub(crate) fn mut_stack(&mut self) -> &mut Stack {
218        &mut self.mod_local.stack
219    }
220
221    pub fn next_uuid(&mut self) -> Uuid {
222        self.mod_local.id_generator.next_uuid()
223    }
224
225    pub fn id_generator(&mut self) -> &mut IdGenerator {
226        &mut self.mod_local.id_generator
227    }
228
229    #[cfg(feature = "artifact-graph")]
230    pub(crate) fn add_artifact(&mut self, artifact: Artifact) {
231        let id = artifact.id();
232        self.mod_local.artifacts.artifacts.insert(id, artifact);
233    }
234
235    pub(crate) fn push_op(&mut self, op: Operation) {
236        #[cfg(feature = "artifact-graph")]
237        self.mod_local.artifacts.operations.push(op);
238        #[cfg(not(feature = "artifact-graph"))]
239        drop(op);
240    }
241
242    #[cfg(feature = "artifact-graph")]
243    pub(crate) fn push_command(&mut self, command: ArtifactCommand) {
244        self.mod_local.artifacts.unprocessed_commands.push(command);
245        #[cfg(not(feature = "artifact-graph"))]
246        drop(command);
247    }
248
249    pub(super) fn next_module_id(&self) -> ModuleId {
250        ModuleId::from_usize(self.global.path_to_source_id.len())
251    }
252
253    pub(super) fn id_for_module(&self, path: &ModulePath) -> Option<ModuleId> {
254        self.global.path_to_source_id.get(path).cloned()
255    }
256
257    pub(super) fn add_path_to_source_id(&mut self, path: ModulePath, id: ModuleId) {
258        debug_assert!(!self.global.path_to_source_id.contains_key(&path));
259        self.global.path_to_source_id.insert(path, id);
260    }
261
262    pub(crate) fn add_root_module_contents(&mut self, program: &crate::Program) {
263        let root_id = ModuleId::default();
264        // Get the path for the root module.
265        let path = self
266            .global
267            .path_to_source_id
268            .iter()
269            .find(|(_, v)| **v == root_id)
270            .unwrap()
271            .0
272            .clone();
273        self.add_id_to_source(
274            root_id,
275            ModuleSource {
276                path,
277                source: program.original_file_contents.to_string(),
278            },
279        );
280    }
281
282    pub(super) fn add_id_to_source(&mut self, id: ModuleId, source: ModuleSource) {
283        self.global.id_to_source.insert(id, source);
284    }
285
286    pub(super) fn add_module(&mut self, id: ModuleId, path: ModulePath, repr: ModuleRepr) {
287        debug_assert!(self.global.path_to_source_id.contains_key(&path));
288        let module_info = ModuleInfo { id, repr, path };
289        self.global.module_infos.insert(id, module_info);
290    }
291
292    pub fn get_module(&mut self, id: ModuleId) -> Option<&ModuleInfo> {
293        self.global.module_infos.get(&id)
294    }
295
296    #[cfg(all(test, feature = "artifact-graph"))]
297    pub(crate) fn modules(&self) -> &ModuleInfoMap {
298        &self.global.module_infos
299    }
300
301    #[cfg(all(test, feature = "artifact-graph"))]
302    pub(crate) fn root_module_artifact_state(&self) -> &ModuleArtifactState {
303        &self.global.root_module_artifacts
304    }
305
306    pub fn current_default_units(&self) -> NumericType {
307        NumericType::Default {
308            len: self.length_unit(),
309            angle: self.angle_unit(),
310        }
311    }
312
313    pub fn length_unit(&self) -> UnitLength {
314        self.mod_local.settings.default_length_units
315    }
316
317    pub fn angle_unit(&self) -> UnitAngle {
318        self.mod_local.settings.default_angle_units
319    }
320
321    pub(super) fn circular_import_error(&self, path: &ModulePath, source_range: SourceRange) -> KclError {
322        KclError::new_import_cycle(KclErrorDetails::new(
323            format!(
324                "circular import of modules is not allowed: {} -> {}",
325                self.global
326                    .mod_loader
327                    .import_stack
328                    .iter()
329                    .map(|p| p.to_string_lossy())
330                    .collect::<Vec<_>>()
331                    .join(" -> "),
332                path,
333            ),
334            vec![source_range],
335        ))
336    }
337
338    pub(crate) fn pipe_value(&self) -> Option<&KclValue> {
339        self.mod_local.pipe_value.as_ref()
340    }
341
342    pub(crate) fn error_with_outputs(
343        &self,
344        error: KclError,
345        main_ref: Option<EnvironmentRef>,
346        default_planes: Option<DefaultPlanes>,
347    ) -> KclErrorWithOutputs {
348        let module_id_to_module_path: IndexMap<ModuleId, ModulePath> = self
349            .global
350            .path_to_source_id
351            .iter()
352            .map(|(k, v)| ((*v), k.clone()))
353            .collect();
354
355        KclErrorWithOutputs::new(
356            error,
357            self.errors().to_vec(),
358            main_ref
359                .map(|main_ref| self.mod_local.variables(main_ref))
360                .unwrap_or_default(),
361            #[cfg(feature = "artifact-graph")]
362            self.global.root_module_artifacts.operations.clone(),
363            #[cfg(feature = "artifact-graph")]
364            Default::default(),
365            #[cfg(feature = "artifact-graph")]
366            self.global.artifacts.graph.clone(),
367            module_id_to_module_path,
368            self.global.id_to_source.clone(),
369            default_planes,
370        )
371    }
372
373    #[cfg(feature = "artifact-graph")]
374    pub(crate) fn build_program_lookup(
375        &self,
376        current: crate::parsing::ast::types::Node<crate::parsing::ast::types::Program>,
377    ) -> ProgramLookup {
378        ProgramLookup::new(current, self.global.module_infos.clone())
379    }
380
381    #[cfg(feature = "artifact-graph")]
382    pub(crate) async fn build_artifact_graph(
383        &mut self,
384        engine: &Arc<Box<dyn EngineManager>>,
385        program: NodeRef<'_, crate::parsing::ast::types::Program>,
386    ) -> Result<(), KclError> {
387        let mut new_commands = Vec::new();
388        let mut new_exec_artifacts = IndexMap::new();
389        for module in self.global.module_infos.values_mut() {
390            match &mut module.repr {
391                ModuleRepr::Kcl(_, Some(outcome)) => {
392                    new_commands.extend(outcome.artifacts.process_commands());
393                    new_exec_artifacts.extend(outcome.artifacts.artifacts.clone());
394                }
395                ModuleRepr::Foreign(_, Some((_, module_artifacts))) => {
396                    new_commands.extend(module_artifacts.process_commands());
397                    new_exec_artifacts.extend(module_artifacts.artifacts.clone());
398                }
399                ModuleRepr::Root | ModuleRepr::Kcl(_, None) | ModuleRepr::Foreign(_, None) | ModuleRepr::Dummy => {}
400            }
401        }
402        // Take from the module artifacts so that we don't try to process them
403        // again next time due to execution caching.
404        new_commands.extend(self.global.root_module_artifacts.process_commands());
405        // Note: These will get re-processed, but since we're just adding them
406        // to a map, it's fine.
407        new_exec_artifacts.extend(self.global.root_module_artifacts.artifacts.clone());
408        let new_responses = engine.take_responses().await;
409
410        // Move the artifacts into ExecState global to simplify cache
411        // management.
412        self.global.artifacts.artifacts.extend(new_exec_artifacts);
413
414        let initial_graph = self.global.artifacts.graph.clone();
415
416        // Build the artifact graph.
417        let programs = self.build_program_lookup(program.clone());
418        let graph_result = crate::execution::artifact::build_artifact_graph(
419            &new_commands,
420            &new_responses,
421            program,
422            &mut self.global.artifacts.artifacts,
423            initial_graph,
424            &programs,
425        );
426
427        let artifact_graph = graph_result?;
428        self.global.artifacts.graph = artifact_graph;
429
430        Ok(())
431    }
432
433    #[cfg(not(feature = "artifact-graph"))]
434    pub(crate) async fn build_artifact_graph(
435        &mut self,
436        _engine: &Arc<Box<dyn EngineManager>>,
437        _program: NodeRef<'_, crate::parsing::ast::types::Program>,
438    ) -> Result<(), KclError> {
439        Ok(())
440    }
441}
442
443impl GlobalState {
444    fn new(settings: &ExecutorSettings) -> Self {
445        let mut global = GlobalState {
446            path_to_source_id: Default::default(),
447            module_infos: Default::default(),
448            artifacts: Default::default(),
449            root_module_artifacts: Default::default(),
450            mod_loader: Default::default(),
451            errors: Default::default(),
452            id_to_source: Default::default(),
453        };
454
455        let root_id = ModuleId::default();
456        let root_path = settings.current_file.clone().unwrap_or_default();
457        global.module_infos.insert(
458            root_id,
459            ModuleInfo {
460                id: root_id,
461                path: ModulePath::Local {
462                    value: root_path.clone(),
463                    original_import_path: None,
464                },
465                repr: ModuleRepr::Root,
466            },
467        );
468        global.path_to_source_id.insert(
469            ModulePath::Local {
470                value: root_path,
471                original_import_path: None,
472            },
473            root_id,
474        );
475        global
476    }
477
478    pub(super) fn filenames(&self) -> IndexMap<ModuleId, ModulePath> {
479        self.path_to_source_id.iter().map(|(k, v)| ((*v), k.clone())).collect()
480    }
481
482    pub(super) fn get_source(&self, id: ModuleId) -> Option<&ModuleSource> {
483        self.id_to_source.get(&id)
484    }
485}
486
487impl ArtifactState {
488    #[cfg(feature = "artifact-graph")]
489    pub fn cached_body_items(&self) -> usize {
490        self.graph.item_count
491    }
492
493    pub(crate) fn clear(&mut self) {
494        #[cfg(feature = "artifact-graph")]
495        {
496            self.artifacts.clear();
497            self.graph.clear();
498        }
499    }
500}
501
502impl ModuleArtifactState {
503    pub(crate) fn clear(&mut self) {
504        #[cfg(feature = "artifact-graph")]
505        {
506            self.artifacts.clear();
507            self.unprocessed_commands.clear();
508            self.commands.clear();
509            self.operations.clear();
510        }
511    }
512
513    #[cfg(not(feature = "artifact-graph"))]
514    pub(crate) fn extend(&mut self, _other: ModuleArtifactState) {}
515
516    /// When self is a cached state, extend it with new state.
517    #[cfg(feature = "artifact-graph")]
518    pub(crate) fn extend(&mut self, other: ModuleArtifactState) {
519        self.artifacts.extend(other.artifacts);
520        self.unprocessed_commands.extend(other.unprocessed_commands);
521        self.commands.extend(other.commands);
522        self.operations.extend(other.operations);
523    }
524
525    // Move unprocessed artifact commands so that we don't try to process them
526    // again next time due to execution caching.  Returns a clone of the
527    // commands that were moved.
528    #[cfg(feature = "artifact-graph")]
529    pub(crate) fn process_commands(&mut self) -> Vec<ArtifactCommand> {
530        let unprocessed = std::mem::take(&mut self.unprocessed_commands);
531        let new_module_commands = unprocessed.clone();
532        self.commands.extend(unprocessed);
533        new_module_commands
534    }
535}
536
537impl ModuleState {
538    pub(super) fn new(path: ModulePath, memory: Arc<ProgramMemory>, module_id: Option<ModuleId>) -> Self {
539        ModuleState {
540            id_generator: IdGenerator::new(module_id),
541            stack: memory.new_stack(),
542            pipe_value: Default::default(),
543            being_declared: Default::default(),
544            sketch_block: Default::default(),
545            stdlib_entry_source_range: Default::default(),
546            module_exports: Default::default(),
547            explicit_length_units: false,
548            path,
549            settings: Default::default(),
550            artifacts: Default::default(),
551            allowed_warnings: Vec::new(),
552            denied_warnings: Vec::new(),
553            inside_stdlib: false,
554        }
555    }
556
557    pub(super) fn variables(&self, main_ref: EnvironmentRef) -> IndexMap<String, KclValue> {
558        self.stack
559            .find_all_in_env(main_ref)
560            .map(|(k, v)| (k.clone(), v.clone()))
561            .collect()
562    }
563}
564
565impl SketchBlockState {
566    pub(crate) fn next_sketch_var_id(&self) -> SketchVarId {
567        SketchVarId(self.sketch_vars.len())
568    }
569}
570
571#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS)]
572#[ts(export)]
573#[serde(rename_all = "camelCase")]
574pub struct MetaSettings {
575    pub default_length_units: UnitLength,
576    pub default_angle_units: UnitAngle,
577    pub experimental_features: annotations::WarningLevel,
578    pub kcl_version: String,
579}
580
581impl Default for MetaSettings {
582    fn default() -> Self {
583        MetaSettings {
584            default_length_units: UnitLength::Millimeters,
585            default_angle_units: UnitAngle::Degrees,
586            experimental_features: annotations::WarningLevel::Deny,
587            kcl_version: "1.0".to_owned(),
588        }
589    }
590}
591
592impl MetaSettings {
593    pub(crate) fn update_from_annotation(
594        &mut self,
595        annotation: &crate::parsing::ast::types::Node<Annotation>,
596    ) -> Result<(bool, bool), KclError> {
597        let properties = annotations::expect_properties(annotations::SETTINGS, annotation)?;
598
599        let mut updated_len = false;
600        let mut updated_angle = false;
601        for p in properties {
602            match &*p.inner.key.name {
603                annotations::SETTINGS_UNIT_LENGTH => {
604                    let value = annotations::expect_ident(&p.inner.value)?;
605                    let value = super::types::length_from_str(value, annotation.as_source_range())?;
606                    self.default_length_units = value;
607                    updated_len = true;
608                }
609                annotations::SETTINGS_UNIT_ANGLE => {
610                    let value = annotations::expect_ident(&p.inner.value)?;
611                    let value = super::types::angle_from_str(value, annotation.as_source_range())?;
612                    self.default_angle_units = value;
613                    updated_angle = true;
614                }
615                annotations::SETTINGS_VERSION => {
616                    let value = annotations::expect_number(&p.inner.value)?;
617                    self.kcl_version = value;
618                }
619                annotations::SETTINGS_EXPERIMENTAL_FEATURES => {
620                    let value = annotations::expect_ident(&p.inner.value)?;
621                    let value = annotations::WarningLevel::from_str(value).map_err(|_| {
622                        KclError::new_semantic(KclErrorDetails::new(
623                            format!(
624                                "Invalid value for {} settings property, expected one of: {}",
625                                annotations::SETTINGS_EXPERIMENTAL_FEATURES,
626                                annotations::WARN_LEVELS.join(", ")
627                            ),
628                            annotation.as_source_ranges(),
629                        ))
630                    })?;
631                    self.experimental_features = value;
632                }
633                name => {
634                    return Err(KclError::new_semantic(KclErrorDetails::new(
635                        format!(
636                            "Unexpected settings key: `{name}`; expected one of `{}`, `{}`",
637                            annotations::SETTINGS_UNIT_LENGTH,
638                            annotations::SETTINGS_UNIT_ANGLE
639                        ),
640                        vec![annotation.as_source_range()],
641                    )));
642                }
643            }
644        }
645
646        Ok((updated_len, updated_angle))
647    }
648}