kcl_lib/execution/
state.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use anyhow::Result;
use indexmap::IndexMap;
use kittycad_modeling_cmds::websocket::WebSocketResponse;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::{
    errors::{KclError, KclErrorDetails},
    execution::{
        annotations, kcl_value, Artifact, ArtifactCommand, ArtifactGraph, ArtifactId, ExecOutcome, ExecutorSettings,
        KclValue, ModuleInfo, ModuleRepr, Operation, ProgramMemory, SolidLazyIds, UnitAngle, UnitLen,
    },
    parsing::ast::types::NonCodeValue,
    source_range::{ModuleId, SourceRange},
};

/// State for executing a program.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExecState {
    pub global: GlobalState,
    pub mod_local: ModuleState,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GlobalState {
    /// The stable artifact ID generator.
    pub id_generator: IdGenerator,
    /// Map from source file absolute path to module ID.
    pub path_to_source_id: IndexMap<std::path::PathBuf, ModuleId>,
    /// Map from module ID to module info.
    pub module_infos: IndexMap<ModuleId, ModuleInfo>,
    /// Output map of UUIDs to artifacts.
    pub artifacts: IndexMap<ArtifactId, Artifact>,
    /// Output commands to allow building the artifact graph by the caller.
    /// These are accumulated in the [`ExecutorContext`] but moved here for
    /// convenience of the execution cache.
    pub artifact_commands: Vec<ArtifactCommand>,
    /// Responses from the engine for `artifact_commands`.  We need to cache
    /// this so that we can build the artifact graph.  These are accumulated in
    /// the [`ExecutorContext`] but moved here for convenience of the execution
    /// cache.
    pub artifact_responses: IndexMap<Uuid, WebSocketResponse>,
    /// Output artifact graph.
    pub artifact_graph: ArtifactGraph,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ModuleState {
    /// Program variable bindings.
    pub memory: ProgramMemory,
    /// Dynamic state that follows dynamic flow of the program.
    pub dynamic_state: DynamicState,
    /// The current value of the pipe operator returned from the previous
    /// expression.  If we're not currently in a pipeline, this will be None.
    pub pipe_value: Option<KclValue>,
    /// Identifiers that have been exported from the current module.
    pub module_exports: Vec<String>,
    /// The stack of import statements for detecting circular module imports.
    /// If this is empty, we're not currently executing an import statement.
    pub import_stack: Vec<std::path::PathBuf>,
    /// Operations that have been performed in execution order, for display in
    /// the Feature Tree.
    pub operations: Vec<Operation>,
    /// Settings specified from annotations.
    pub settings: MetaSettings,
}

impl ExecState {
    pub fn new(exec_settings: &ExecutorSettings) -> Self {
        ExecState {
            global: GlobalState::new(exec_settings),
            mod_local: ModuleState::new(exec_settings),
        }
    }

    pub(super) fn reset(&mut self, exec_settings: &ExecutorSettings) {
        let mut id_generator = self.global.id_generator.clone();
        // We do not pop the ids, since we want to keep the same id generator.
        // This is for the front end to keep track of the ids.
        id_generator.next_id = 0;

        let mut global = GlobalState::new(exec_settings);
        global.id_generator = id_generator;

        *self = ExecState {
            global,
            mod_local: ModuleState::new(exec_settings),
        };
    }

    /// Convert to execution outcome when running in WebAssembly.  We want to
    /// reduce the amount of data that crosses the WASM boundary as much as
    /// possible.
    pub fn to_wasm_outcome(self) -> ExecOutcome {
        // Fields are opt-in so that we don't accidentally leak private internal
        // state when we add more to ExecState.
        ExecOutcome {
            memory: self.mod_local.memory,
            operations: self.mod_local.operations,
            artifacts: self.global.artifacts,
            artifact_commands: self.global.artifact_commands,
            artifact_graph: self.global.artifact_graph,
        }
    }

    pub fn memory(&self) -> &ProgramMemory {
        &self.mod_local.memory
    }

    pub fn mut_memory(&mut self) -> &mut ProgramMemory {
        &mut self.mod_local.memory
    }

    pub fn next_uuid(&mut self) -> Uuid {
        self.global.id_generator.next_uuid()
    }

    pub fn add_artifact(&mut self, artifact: Artifact) {
        let id = artifact.id();
        self.global.artifacts.insert(id, artifact);
    }

    pub(super) fn add_module(&mut self, id: ModuleId, path: std::path::PathBuf, repr: ModuleRepr) -> ModuleId {
        debug_assert!(!self.global.path_to_source_id.contains_key(&path));

        self.global.path_to_source_id.insert(path.clone(), id);

        let module_info = ModuleInfo { id, repr, path };
        self.global.module_infos.insert(id, module_info);

        id
    }

    pub fn length_unit(&self) -> UnitLen {
        self.mod_local.settings.default_length_units
    }

    pub fn angle_unit(&self) -> UnitAngle {
        self.mod_local.settings.default_angle_units
    }
}

impl GlobalState {
    fn new(settings: &ExecutorSettings) -> Self {
        let mut global = GlobalState {
            id_generator: Default::default(),
            path_to_source_id: Default::default(),
            module_infos: Default::default(),
            artifacts: Default::default(),
            artifact_commands: Default::default(),
            artifact_responses: Default::default(),
            artifact_graph: Default::default(),
        };

        let root_id = ModuleId::default();
        let root_path = settings.current_file.clone().unwrap_or_default();
        global.module_infos.insert(
            root_id,
            ModuleInfo {
                id: root_id,
                path: root_path.clone(),
                repr: ModuleRepr::Root,
            },
        );
        global.path_to_source_id.insert(root_path, root_id);
        global
    }
}

impl ModuleState {
    pub(super) fn new(exec_settings: &ExecutorSettings) -> Self {
        ModuleState {
            memory: Default::default(),
            dynamic_state: Default::default(),
            pipe_value: Default::default(),
            module_exports: Default::default(),
            import_stack: Default::default(),
            operations: Default::default(),
            settings: MetaSettings {
                default_length_units: exec_settings.units.into(),
                default_angle_units: Default::default(),
            },
        }
    }
}

#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct MetaSettings {
    pub default_length_units: kcl_value::UnitLen,
    pub default_angle_units: kcl_value::UnitAngle,
}

impl MetaSettings {
    pub(crate) fn update_from_annotation(
        &mut self,
        annotation: &NonCodeValue,
        source_range: SourceRange,
    ) -> Result<(), KclError> {
        let properties = annotations::expect_properties(annotations::SETTINGS, annotation, source_range)?;

        for p in properties {
            match &*p.inner.key.name {
                annotations::SETTINGS_UNIT_LENGTH => {
                    let value = annotations::expect_ident(&p.inner.value)?;
                    let value = kcl_value::UnitLen::from_str(value, source_range)?;
                    self.default_length_units = value;
                }
                annotations::SETTINGS_UNIT_ANGLE => {
                    let value = annotations::expect_ident(&p.inner.value)?;
                    let value = kcl_value::UnitAngle::from_str(value, source_range)?;
                    self.default_angle_units = value;
                }
                name => {
                    return Err(KclError::Semantic(KclErrorDetails {
                        message: format!(
                            "Unexpected settings key: `{name}`; expected one of `{}`, `{}`",
                            annotations::SETTINGS_UNIT_LENGTH,
                            annotations::SETTINGS_UNIT_ANGLE
                        ),
                        source_ranges: vec![source_range],
                    }))
                }
            }
        }

        Ok(())
    }
}

/// Dynamic state that depends on the dynamic flow of the program, like the call
/// stack.  If the language had exceptions, for example, you could store the
/// stack of exception handlers here.
#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct DynamicState {
    pub solid_ids: Vec<SolidLazyIds>,
}

impl DynamicState {
    #[must_use]
    pub(super) fn merge(&self, memory: &ProgramMemory) -> Self {
        let mut merged = self.clone();
        merged.append(memory);
        merged
    }

    fn append(&mut self, memory: &ProgramMemory) {
        for env in &memory.environments {
            for item in env.bindings.values() {
                if let KclValue::Solid { value } = item {
                    self.solid_ids.push(SolidLazyIds::from(value.as_ref()));
                }
            }
        }
    }

    pub(crate) fn edge_cut_ids_on_sketch(&self, sketch_id: uuid::Uuid) -> Vec<uuid::Uuid> {
        self.solid_ids
            .iter()
            .flat_map(|eg| {
                if eg.sketch_id == sketch_id {
                    eg.edge_cuts.clone()
                } else {
                    Vec::new()
                }
            })
            .collect::<Vec<_>>()
    }
}

/// A generator for ArtifactIds that can be stable across executions.
#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct IdGenerator {
    pub(super) next_id: usize,
    ids: Vec<uuid::Uuid>,
}

impl IdGenerator {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn next_uuid(&mut self) -> uuid::Uuid {
        if let Some(id) = self.ids.get(self.next_id) {
            self.next_id += 1;
            *id
        } else {
            let id = uuid::Uuid::new_v4();
            self.ids.push(id);
            self.next_id += 1;
            id
        }
    }
}