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(crate) fn stack(&self) -> &Stack {
174        &self.mod_local.stack
175    }
176
177    pub(crate) fn mut_stack(&mut self) -> &mut Stack {
178        &mut self.mod_local.stack
179    }
180
181    pub fn next_uuid(&mut self) -> Uuid {
182        self.mod_local.id_generator.next_uuid()
183    }
184
185    pub fn id_generator(&mut self) -> &mut IdGenerator {
186        &mut self.mod_local.id_generator
187    }
188
189    #[cfg(feature = "artifact-graph")]
190    pub(crate) fn add_artifact(&mut self, artifact: Artifact) {
191        let id = artifact.id();
192        self.mod_local.artifacts.artifacts.insert(id, artifact);
193    }
194
195    pub(crate) fn push_op(&mut self, op: Operation) {
196        #[cfg(feature = "artifact-graph")]
197        self.mod_local.artifacts.operations.push(op.clone());
198        #[cfg(not(feature = "artifact-graph"))]
199        drop(op);
200    }
201
202    #[cfg(feature = "artifact-graph")]
203    pub(crate) fn push_command(&mut self, command: ArtifactCommand) {
204        self.mod_local.artifacts.unprocessed_commands.push(command);
205        #[cfg(not(feature = "artifact-graph"))]
206        drop(command);
207    }
208
209    pub(super) fn next_module_id(&self) -> ModuleId {
210        ModuleId::from_usize(self.global.path_to_source_id.len())
211    }
212
213    pub(super) fn id_for_module(&self, path: &ModulePath) -> Option<ModuleId> {
214        self.global.path_to_source_id.get(path).cloned()
215    }
216
217    pub(super) fn add_path_to_source_id(&mut self, path: ModulePath, id: ModuleId) {
218        debug_assert!(!self.global.path_to_source_id.contains_key(&path));
219        self.global.path_to_source_id.insert(path.clone(), id);
220    }
221
222    pub(crate) fn add_root_module_contents(&mut self, program: &crate::Program) {
223        let root_id = ModuleId::default();
224        // Get the path for the root module.
225        let path = self
226            .global
227            .path_to_source_id
228            .iter()
229            .find(|(_, v)| **v == root_id)
230            .unwrap()
231            .0
232            .clone();
233        self.add_id_to_source(
234            root_id,
235            ModuleSource {
236                path,
237                source: program.original_file_contents.to_string(),
238            },
239        );
240    }
241
242    pub(super) fn add_id_to_source(&mut self, id: ModuleId, source: ModuleSource) {
243        self.global.id_to_source.insert(id, source.clone());
244    }
245
246    pub(super) fn add_module(&mut self, id: ModuleId, path: ModulePath, repr: ModuleRepr) {
247        debug_assert!(self.global.path_to_source_id.contains_key(&path));
248        let module_info = ModuleInfo { id, repr, path };
249        self.global.module_infos.insert(id, module_info);
250    }
251
252    pub fn get_module(&mut self, id: ModuleId) -> Option<&ModuleInfo> {
253        self.global.module_infos.get(&id)
254    }
255
256    #[cfg(all(test, feature = "artifact-graph"))]
257    pub(crate) fn modules(&self) -> &ModuleInfoMap {
258        &self.global.module_infos
259    }
260
261    #[cfg(all(test, feature = "artifact-graph"))]
262    pub(crate) fn root_module_artifact_state(&self) -> &ModuleArtifactState {
263        &self.global.root_module_artifacts
264    }
265
266    pub fn current_default_units(&self) -> NumericType {
267        NumericType::Default {
268            len: self.length_unit(),
269            angle: self.angle_unit(),
270        }
271    }
272
273    pub fn length_unit(&self) -> UnitLen {
274        self.mod_local.settings.default_length_units
275    }
276
277    pub fn angle_unit(&self) -> UnitAngle {
278        self.mod_local.settings.default_angle_units
279    }
280
281    pub(super) fn circular_import_error(&self, path: &ModulePath, source_range: SourceRange) -> KclError {
282        KclError::new_import_cycle(KclErrorDetails::new(
283            format!(
284                "circular import of modules is not allowed: {} -> {}",
285                self.global
286                    .mod_loader
287                    .import_stack
288                    .iter()
289                    .map(|p| p.to_string_lossy())
290                    .collect::<Vec<_>>()
291                    .join(" -> "),
292                path,
293            ),
294            vec![source_range],
295        ))
296    }
297
298    pub(crate) fn pipe_value(&self) -> Option<&KclValue> {
299        self.mod_local.pipe_value.as_ref()
300    }
301
302    pub(crate) fn error_with_outputs(
303        &self,
304        error: KclError,
305        main_ref: Option<EnvironmentRef>,
306        default_planes: Option<DefaultPlanes>,
307    ) -> KclErrorWithOutputs {
308        let module_id_to_module_path: IndexMap<ModuleId, ModulePath> = self
309            .global
310            .path_to_source_id
311            .iter()
312            .map(|(k, v)| ((*v), k.clone()))
313            .collect();
314
315        KclErrorWithOutputs::new(
316            error,
317            self.errors().to_vec(),
318            main_ref
319                .map(|main_ref| self.mod_local.variables(main_ref))
320                .unwrap_or_default(),
321            #[cfg(feature = "artifact-graph")]
322            self.global.root_module_artifacts.operations.clone(),
323            #[cfg(feature = "artifact-graph")]
324            Default::default(),
325            #[cfg(feature = "artifact-graph")]
326            self.global.artifacts.graph.clone(),
327            module_id_to_module_path,
328            self.global.id_to_source.clone(),
329            default_planes,
330        )
331    }
332
333    #[cfg(feature = "artifact-graph")]
334    pub(crate) async fn build_artifact_graph(
335        &mut self,
336        engine: &Arc<Box<dyn EngineManager>>,
337        program: NodeRef<'_, crate::parsing::ast::types::Program>,
338    ) -> Result<(), KclError> {
339        let mut new_commands = Vec::new();
340        let mut new_exec_artifacts = IndexMap::new();
341        for module in self.global.module_infos.values_mut() {
342            match &mut module.repr {
343                ModuleRepr::Kcl(_, Some((_, _, _, module_artifacts)))
344                | ModuleRepr::Foreign(_, Some((_, module_artifacts))) => {
345                    new_commands.extend(module_artifacts.process_commands());
346                    new_exec_artifacts.extend(module_artifacts.artifacts.clone());
347                }
348                ModuleRepr::Root | ModuleRepr::Kcl(_, None) | ModuleRepr::Foreign(_, None) | ModuleRepr::Dummy => {}
349            }
350        }
351        // Take from the module artifacts so that we don't try to process them
352        // again next time due to execution caching.
353        new_commands.extend(self.global.root_module_artifacts.process_commands());
354        // Note: These will get re-processed, but since we're just adding them
355        // to a map, it's fine.
356        new_exec_artifacts.extend(self.global.root_module_artifacts.artifacts.clone());
357        let new_responses = engine.take_responses().await;
358
359        // Move the artifacts into ExecState global to simplify cache
360        // management.
361        self.global.artifacts.artifacts.extend(new_exec_artifacts);
362
363        let initial_graph = self.global.artifacts.graph.clone();
364
365        // Build the artifact graph.
366        let graph_result = crate::execution::artifact::build_artifact_graph(
367            &new_commands,
368            &new_responses,
369            program,
370            &mut self.global.artifacts.artifacts,
371            initial_graph,
372        );
373
374        let artifact_graph = graph_result?;
375        self.global.artifacts.graph = artifact_graph;
376
377        Ok(())
378    }
379
380    #[cfg(not(feature = "artifact-graph"))]
381    pub(crate) async fn build_artifact_graph(
382        &mut self,
383        _engine: &Arc<Box<dyn EngineManager>>,
384        _program: NodeRef<'_, crate::parsing::ast::types::Program>,
385    ) -> Result<(), KclError> {
386        Ok(())
387    }
388}
389
390impl GlobalState {
391    fn new(settings: &ExecutorSettings) -> Self {
392        let mut global = GlobalState {
393            path_to_source_id: Default::default(),
394            module_infos: Default::default(),
395            artifacts: Default::default(),
396            root_module_artifacts: Default::default(),
397            mod_loader: Default::default(),
398            errors: Default::default(),
399            id_to_source: Default::default(),
400        };
401
402        let root_id = ModuleId::default();
403        let root_path = settings.current_file.clone().unwrap_or_default();
404        global.module_infos.insert(
405            root_id,
406            ModuleInfo {
407                id: root_id,
408                path: ModulePath::Local {
409                    value: root_path.clone(),
410                },
411                repr: ModuleRepr::Root,
412            },
413        );
414        global
415            .path_to_source_id
416            .insert(ModulePath::Local { value: root_path }, root_id);
417        global
418    }
419
420    pub(super) fn filenames(&self) -> IndexMap<ModuleId, ModulePath> {
421        self.path_to_source_id.iter().map(|(k, v)| ((*v), k.clone())).collect()
422    }
423
424    pub(super) fn get_source(&self, id: ModuleId) -> Option<&ModuleSource> {
425        self.id_to_source.get(&id)
426    }
427}
428
429impl ArtifactState {
430    #[cfg(feature = "artifact-graph")]
431    pub fn cached_body_items(&self) -> usize {
432        self.graph.item_count
433    }
434
435    pub(crate) fn clear(&mut self) {
436        #[cfg(feature = "artifact-graph")]
437        {
438            self.artifacts.clear();
439            self.graph.clear();
440        }
441    }
442}
443
444impl ModuleArtifactState {
445    pub(crate) fn clear(&mut self) {
446        #[cfg(feature = "artifact-graph")]
447        {
448            self.artifacts.clear();
449            self.unprocessed_commands.clear();
450            self.commands.clear();
451            self.operations.clear();
452        }
453    }
454
455    #[cfg(not(feature = "artifact-graph"))]
456    pub(crate) fn extend(&mut self, _other: ModuleArtifactState) {}
457
458    /// When self is a cached state, extend it with new state.
459    #[cfg(feature = "artifact-graph")]
460    pub(crate) fn extend(&mut self, other: ModuleArtifactState) {
461        self.artifacts.extend(other.artifacts);
462        self.unprocessed_commands.extend(other.unprocessed_commands);
463        self.commands.extend(other.commands);
464        self.operations.extend(other.operations);
465    }
466
467    // Move unprocessed artifact commands so that we don't try to process them
468    // again next time due to execution caching.  Returns a clone of the
469    // commands that were moved.
470    #[cfg(feature = "artifact-graph")]
471    pub(crate) fn process_commands(&mut self) -> Vec<ArtifactCommand> {
472        let unprocessed = std::mem::take(&mut self.unprocessed_commands);
473        let new_module_commands = unprocessed.clone();
474        self.commands.extend(unprocessed);
475        new_module_commands
476    }
477}
478
479impl ModuleState {
480    pub(super) fn new(path: ModulePath, memory: Arc<ProgramMemory>, module_id: Option<ModuleId>) -> Self {
481        ModuleState {
482            id_generator: IdGenerator::new(module_id),
483            stack: memory.new_stack(),
484            pipe_value: Default::default(),
485            being_declared: Default::default(),
486            module_exports: Default::default(),
487            explicit_length_units: false,
488            path,
489            settings: MetaSettings {
490                default_length_units: Default::default(),
491                default_angle_units: Default::default(),
492                kcl_version: "0.1".to_owned(),
493            },
494            artifacts: Default::default(),
495        }
496    }
497
498    pub(super) fn variables(&self, main_ref: EnvironmentRef) -> IndexMap<String, KclValue> {
499        self.stack
500            .find_all_in_env(main_ref)
501            .map(|(k, v)| (k.clone(), v.clone()))
502            .collect()
503    }
504}
505
506#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS, JsonSchema)]
507#[ts(export)]
508#[serde(rename_all = "camelCase")]
509pub struct MetaSettings {
510    pub default_length_units: types::UnitLen,
511    pub default_angle_units: types::UnitAngle,
512    pub kcl_version: String,
513}
514
515impl MetaSettings {
516    pub(crate) fn update_from_annotation(
517        &mut self,
518        annotation: &crate::parsing::ast::types::Node<Annotation>,
519    ) -> Result<bool, KclError> {
520        let properties = annotations::expect_properties(annotations::SETTINGS, annotation)?;
521
522        let mut updated_len = false;
523        for p in properties {
524            match &*p.inner.key.name {
525                annotations::SETTINGS_UNIT_LENGTH => {
526                    let value = annotations::expect_ident(&p.inner.value)?;
527                    let value = types::UnitLen::from_str(value, annotation.as_source_range())?;
528                    self.default_length_units = value;
529                    updated_len = true;
530                }
531                annotations::SETTINGS_UNIT_ANGLE => {
532                    let value = annotations::expect_ident(&p.inner.value)?;
533                    let value = types::UnitAngle::from_str(value, annotation.as_source_range())?;
534                    self.default_angle_units = value;
535                }
536                annotations::SETTINGS_VERSION => {
537                    let value = annotations::expect_number(&p.inner.value)?;
538                    self.kcl_version = value;
539                }
540                name => {
541                    return Err(KclError::new_semantic(KclErrorDetails::new(
542                        format!(
543                            "Unexpected settings key: `{name}`; expected one of `{}`, `{}`",
544                            annotations::SETTINGS_UNIT_LENGTH,
545                            annotations::SETTINGS_UNIT_ANGLE
546                        ),
547                        vec![annotation.as_source_range()],
548                    )));
549                }
550            }
551        }
552
553        Ok(updated_len)
554    }
555}