kcl_lib/execution/
state.rs

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