1#[cfg(feature = "artifact-graph")]
2use std::collections::BTreeMap;
3use std::str::FromStr;
4use std::sync::Arc;
5
6use anyhow::Result;
7use indexmap::IndexMap;
8use kittycad_modeling_cmds::units::UnitAngle;
9use kittycad_modeling_cmds::units::UnitLength;
10use serde::Deserialize;
11use serde::Serialize;
12use uuid::Uuid;
13
14use crate::CompilationError;
15use crate::EngineManager;
16use crate::ExecutorContext;
17use crate::KclErrorWithOutputs;
18use crate::MockConfig;
19use crate::SourceRange;
20use crate::collections::AhashIndexSet;
21use crate::errors::KclError;
22use crate::errors::KclErrorDetails;
23use crate::errors::Severity;
24use crate::exec::DefaultPlanes;
25#[cfg(feature = "artifact-graph")]
26use crate::execution::Artifact;
27#[cfg(feature = "artifact-graph")]
28use crate::execution::ArtifactCommand;
29#[cfg(feature = "artifact-graph")]
30use crate::execution::ArtifactGraph;
31#[cfg(feature = "artifact-graph")]
32use crate::execution::ArtifactId;
33use crate::execution::EnvironmentRef;
34use crate::execution::ExecOutcome;
35use crate::execution::ExecutorSettings;
36use crate::execution::KclValue;
37#[cfg(feature = "artifact-graph")]
38use crate::execution::ProgramLookup;
39use crate::execution::SketchVarId;
40use crate::execution::UnsolvedSegment;
41use crate::execution::annotations;
42use crate::execution::cad_op::Operation;
43use crate::execution::id_generator::IdGenerator;
44use crate::execution::memory::ProgramMemory;
45use crate::execution::memory::Stack;
46#[cfg(feature = "artifact-graph")]
47use crate::execution::sketch_solve::Solved;
48use crate::execution::types::NumericType;
49#[cfg(feature = "artifact-graph")]
50use crate::front::Number;
51use crate::front::Object;
52use crate::front::ObjectId;
53use crate::id::IncIdGenerator;
54use crate::modules::ModuleId;
55use crate::modules::ModuleInfo;
56use crate::modules::ModuleLoader;
57use crate::modules::ModulePath;
58use crate::modules::ModuleRepr;
59use crate::modules::ModuleSource;
60use crate::parsing::ast::types::Annotation;
61use crate::parsing::ast::types::NodeRef;
62use crate::parsing::ast::types::TagNode;
63
64#[derive(Debug, Clone)]
66pub struct ExecState {
67 pub(super) global: GlobalState,
68 pub(super) mod_local: ModuleState,
69}
70
71pub type ModuleInfoMap = IndexMap<ModuleId, ModuleInfo>;
72
73#[derive(Debug, Clone)]
74pub(super) struct GlobalState {
75 pub path_to_source_id: IndexMap<ModulePath, ModuleId>,
77 pub id_to_source: IndexMap<ModuleId, ModuleSource>,
79 pub module_infos: ModuleInfoMap,
81 pub mod_loader: ModuleLoader,
83 pub errors: Vec<CompilationError>,
85 pub artifacts: ArtifactState,
87 pub root_module_artifacts: ModuleArtifactState,
89 #[cfg(feature = "artifact-graph")]
91 pub segment_ids_edited: AhashIndexSet<ObjectId>,
92}
93
94#[cfg(feature = "artifact-graph")]
95#[derive(Debug, Clone, Default)]
96pub(super) struct ArtifactState {
97 pub artifacts: IndexMap<ArtifactId, Artifact>,
100 pub graph: ArtifactGraph,
102}
103
104#[cfg(not(feature = "artifact-graph"))]
105#[derive(Debug, Clone, Default)]
106pub(super) struct ArtifactState {}
107
108#[cfg(feature = "artifact-graph")]
110#[derive(Debug, Clone, Default, PartialEq, Serialize)]
111pub struct ModuleArtifactState {
112 pub artifacts: IndexMap<ArtifactId, Artifact>,
114 #[serde(skip)]
117 pub unprocessed_commands: Vec<ArtifactCommand>,
118 pub commands: Vec<ArtifactCommand>,
120 pub operations: Vec<Operation>,
123 pub object_id_generator: IncIdGenerator<usize>,
125 pub scene_objects: Vec<Object>,
127 pub source_range_to_object: BTreeMap<SourceRange, ObjectId>,
130 pub artifact_id_to_scene_object: IndexMap<ArtifactId, ObjectId>,
132 pub var_solutions: Vec<(SourceRange, Number)>,
134}
135
136#[cfg(not(feature = "artifact-graph"))]
137#[derive(Debug, Clone, Default, PartialEq, Serialize)]
138pub struct ModuleArtifactState {
139 pub object_id_generator: IncIdGenerator<usize>,
141}
142
143#[derive(Debug, Clone)]
144pub(super) struct ModuleState {
145 pub id_generator: IdGenerator,
147 pub stack: Stack,
148 pub(super) call_stack_size: usize,
152 pub pipe_value: Option<KclValue>,
155 pub being_declared: Option<String>,
159 pub sketch_block: Option<SketchBlockState>,
161 pub inside_stdlib: bool,
164 pub stdlib_entry_source_range: Option<SourceRange>,
166 pub module_exports: Vec<String>,
168 pub settings: MetaSettings,
170 pub sketch_mode: bool,
173 pub freedom_analysis: bool,
177 pub(super) explicit_length_units: bool,
178 pub(super) path: ModulePath,
179 pub artifacts: ModuleArtifactState,
181
182 pub(super) allowed_warnings: Vec<&'static str>,
183 pub(super) denied_warnings: Vec<&'static str>,
184}
185
186#[derive(Debug, Clone, Default)]
187pub(crate) struct SketchBlockState {
188 pub sketch_vars: Vec<KclValue>,
189 #[cfg(feature = "artifact-graph")]
190 pub sketch_id: Option<ObjectId>,
191 #[cfg(feature = "artifact-graph")]
192 pub sketch_constraints: Vec<ObjectId>,
193 pub solver_constraints: Vec<ezpz::Constraint>,
194 pub solver_optional_constraints: Vec<ezpz::Constraint>,
195 pub needed_by_engine: Vec<UnsolvedSegment>,
196 pub segment_tags: IndexMap<ObjectId, TagNode>,
197}
198
199impl ExecState {
200 pub fn new(exec_context: &super::ExecutorContext) -> Self {
201 ExecState {
202 global: GlobalState::new(&exec_context.settings, Default::default()),
203 mod_local: ModuleState::new(ModulePath::Main, ProgramMemory::new(), Default::default(), false, true),
204 }
205 }
206
207 pub fn new_mock(exec_context: &super::ExecutorContext, mock_config: &MockConfig) -> Self {
208 #[cfg(feature = "artifact-graph")]
209 let segment_ids_edited = mock_config.segment_ids_edited.clone();
210 #[cfg(not(feature = "artifact-graph"))]
211 let segment_ids_edited = Default::default();
212 ExecState {
213 global: GlobalState::new(&exec_context.settings, segment_ids_edited),
214 mod_local: ModuleState::new(
215 ModulePath::Main,
216 ProgramMemory::new(),
217 Default::default(),
218 mock_config.sketch_block_id.is_some(),
219 mock_config.freedom_analysis,
220 ),
221 }
222 }
223
224 pub(super) fn reset(&mut self, exec_context: &super::ExecutorContext) {
225 let global = GlobalState::new(&exec_context.settings, Default::default());
226
227 *self = ExecState {
228 global,
229 mod_local: ModuleState::new(
230 self.mod_local.path.clone(),
231 ProgramMemory::new(),
232 Default::default(),
233 false,
234 true,
235 ),
236 };
237 }
238
239 pub fn err(&mut self, e: CompilationError) {
241 self.global.errors.push(e);
242 }
243
244 pub fn warn(&mut self, mut e: CompilationError, name: &'static str) {
246 debug_assert!(annotations::WARN_VALUES.contains(&name));
247
248 if self.mod_local.allowed_warnings.contains(&name) {
249 return;
250 }
251
252 if self.mod_local.denied_warnings.contains(&name) {
253 e.severity = Severity::Error;
254 } else {
255 e.severity = Severity::Warning;
256 }
257
258 self.global.errors.push(e);
259 }
260
261 pub fn warn_experimental(&mut self, feature_name: &str, source_range: SourceRange) {
262 let Some(severity) = self.mod_local.settings.experimental_features.severity() else {
263 return;
264 };
265 let error = CompilationError {
266 source_range,
267 message: format!("Use of {feature_name} is experimental and may change or be removed."),
268 suggestion: None,
269 severity,
270 tag: crate::errors::Tag::None,
271 };
272
273 self.global.errors.push(error);
274 }
275
276 pub fn clear_units_warnings(&mut self, source_range: &SourceRange) {
277 self.global.errors = std::mem::take(&mut self.global.errors)
278 .into_iter()
279 .filter(|e| {
280 e.severity != Severity::Warning
281 || !source_range.contains_range(&e.source_range)
282 || e.tag != crate::errors::Tag::UnknownNumericUnits
283 })
284 .collect();
285 }
286
287 pub fn errors(&self) -> &[CompilationError] {
288 &self.global.errors
289 }
290
291 pub async fn into_exec_outcome(self, main_ref: EnvironmentRef, ctx: &ExecutorContext) -> ExecOutcome {
295 ExecOutcome {
298 variables: self.mod_local.variables(main_ref),
299 filenames: self.global.filenames(),
300 #[cfg(feature = "artifact-graph")]
301 operations: self.global.root_module_artifacts.operations,
302 #[cfg(feature = "artifact-graph")]
303 artifact_graph: self.global.artifacts.graph,
304 #[cfg(feature = "artifact-graph")]
305 scene_objects: self.global.root_module_artifacts.scene_objects,
306 #[cfg(feature = "artifact-graph")]
307 source_range_to_object: self.global.root_module_artifacts.source_range_to_object,
308 #[cfg(feature = "artifact-graph")]
309 var_solutions: self.global.root_module_artifacts.var_solutions,
310 errors: self.global.errors,
311 default_planes: ctx.engine.get_default_planes().read().await.clone(),
312 }
313 }
314
315 pub(crate) fn stack(&self) -> &Stack {
316 &self.mod_local.stack
317 }
318
319 pub(crate) fn mut_stack(&mut self) -> &mut Stack {
320 &mut self.mod_local.stack
321 }
322
323 pub(super) fn inc_call_stack_size(&mut self, range: SourceRange) -> Result<(), KclError> {
326 if self.mod_local.call_stack_size >= 50 {
329 return Err(KclError::MaxCallStack {
330 details: KclErrorDetails::new("maximum call stack size exceeded".to_owned(), vec![range]),
331 });
332 }
333 self.mod_local.call_stack_size += 1;
334 Ok(())
335 }
336
337 pub(super) fn dec_call_stack_size(&mut self, range: SourceRange) -> Result<(), KclError> {
340 if self.mod_local.call_stack_size == 0 {
342 let message = "call stack size below zero".to_owned();
343 debug_assert!(false, "{message}");
344 return Err(KclError::new_internal(KclErrorDetails::new(message, vec![range])));
345 }
346 self.mod_local.call_stack_size -= 1;
347 Ok(())
348 }
349
350 pub(crate) fn sketch_mode(&self) -> bool {
355 self.mod_local.sketch_mode
356 && match &self.mod_local.path {
357 ModulePath::Main => true,
358 ModulePath::Local { .. } => true,
359 ModulePath::Std { .. } => false,
360 }
361 }
362
363 pub fn next_object_id(&mut self) -> ObjectId {
364 ObjectId(self.mod_local.artifacts.object_id_generator.next_id())
365 }
366
367 pub fn peek_object_id(&self) -> ObjectId {
368 ObjectId(self.mod_local.artifacts.object_id_generator.peek_id())
369 }
370
371 #[cfg(feature = "artifact-graph")]
372 pub fn add_scene_object(&mut self, obj: Object, source_range: SourceRange) -> ObjectId {
373 let id = obj.id;
374 debug_assert!(
375 id.0 == self.mod_local.artifacts.scene_objects.len(),
376 "Adding scene object with ID {} but next ID is {}",
377 id.0,
378 self.mod_local.artifacts.scene_objects.len()
379 );
380 let artifact_id = obj.artifact_id;
381 self.mod_local.artifacts.scene_objects.push(obj);
382 self.mod_local.artifacts.source_range_to_object.insert(source_range, id);
383 self.mod_local
384 .artifacts
385 .artifact_id_to_scene_object
386 .insert(artifact_id, id);
387 id
388 }
389
390 #[cfg(feature = "artifact-graph")]
393 pub fn add_placeholder_scene_object(&mut self, id: ObjectId, source_range: SourceRange) -> ObjectId {
394 debug_assert!(id.0 == self.mod_local.artifacts.scene_objects.len());
395 self.mod_local
396 .artifacts
397 .scene_objects
398 .push(Object::placeholder(id, source_range));
399 self.mod_local.artifacts.source_range_to_object.insert(source_range, id);
400 id
401 }
402
403 #[cfg(feature = "artifact-graph")]
405 pub fn set_scene_object(&mut self, object: Object) {
406 let id = object.id;
407 let artifact_id = object.artifact_id;
408 self.mod_local.artifacts.scene_objects[id.0] = object;
409 self.mod_local
410 .artifacts
411 .artifact_id_to_scene_object
412 .insert(artifact_id, id);
413 }
414
415 #[cfg(feature = "artifact-graph")]
416 pub fn scene_object_id_by_artifact_id(&self, artifact_id: ArtifactId) -> Option<ObjectId> {
417 self.mod_local
418 .artifacts
419 .artifact_id_to_scene_object
420 .get(&artifact_id)
421 .cloned()
422 }
423
424 #[cfg(feature = "artifact-graph")]
425 pub fn segment_ids_edited_contains(&self, object_id: &ObjectId) -> bool {
426 self.global.segment_ids_edited.contains(object_id)
427 }
428
429 pub(super) fn is_in_sketch_block(&self) -> bool {
430 self.mod_local.sketch_block.is_some()
431 }
432
433 pub(crate) fn sketch_block_mut(&mut self) -> Option<&mut SketchBlockState> {
434 self.mod_local.sketch_block.as_mut()
435 }
436
437 pub fn next_uuid(&mut self) -> Uuid {
438 self.mod_local.id_generator.next_uuid()
439 }
440
441 #[cfg(feature = "artifact-graph")]
442 pub fn next_artifact_id(&mut self) -> ArtifactId {
443 self.mod_local.id_generator.next_artifact_id()
444 }
445
446 pub fn id_generator(&mut self) -> &mut IdGenerator {
447 &mut self.mod_local.id_generator
448 }
449
450 #[cfg(feature = "artifact-graph")]
451 pub(crate) fn add_artifact(&mut self, artifact: Artifact) {
452 let id = artifact.id();
453 self.mod_local.artifacts.artifacts.insert(id, artifact);
454 }
455
456 pub(crate) fn push_op(&mut self, op: Operation) {
457 #[cfg(feature = "artifact-graph")]
458 self.mod_local.artifacts.operations.push(op);
459 #[cfg(not(feature = "artifact-graph"))]
460 drop(op);
461 }
462
463 #[cfg(feature = "artifact-graph")]
464 pub(crate) fn push_command(&mut self, command: ArtifactCommand) {
465 self.mod_local.artifacts.unprocessed_commands.push(command);
466 #[cfg(not(feature = "artifact-graph"))]
467 drop(command);
468 }
469
470 pub(super) fn next_module_id(&self) -> ModuleId {
471 ModuleId::from_usize(self.global.path_to_source_id.len())
472 }
473
474 pub(super) fn id_for_module(&self, path: &ModulePath) -> Option<ModuleId> {
475 self.global.path_to_source_id.get(path).cloned()
476 }
477
478 pub(super) fn add_path_to_source_id(&mut self, path: ModulePath, id: ModuleId) {
479 debug_assert!(!self.global.path_to_source_id.contains_key(&path));
480 self.global.path_to_source_id.insert(path, id);
481 }
482
483 pub(crate) fn add_root_module_contents(&mut self, program: &crate::Program) {
484 let root_id = ModuleId::default();
485 let path = self
487 .global
488 .path_to_source_id
489 .iter()
490 .find(|(_, v)| **v == root_id)
491 .unwrap()
492 .0
493 .clone();
494 self.add_id_to_source(
495 root_id,
496 ModuleSource {
497 path,
498 source: program.original_file_contents.to_string(),
499 },
500 );
501 }
502
503 pub(super) fn add_id_to_source(&mut self, id: ModuleId, source: ModuleSource) {
504 self.global.id_to_source.insert(id, source);
505 }
506
507 pub(super) fn add_module(&mut self, id: ModuleId, path: ModulePath, repr: ModuleRepr) {
508 debug_assert!(self.global.path_to_source_id.contains_key(&path));
509 let module_info = ModuleInfo { id, repr, path };
510 self.global.module_infos.insert(id, module_info);
511 }
512
513 pub fn get_module(&mut self, id: ModuleId) -> Option<&ModuleInfo> {
514 self.global.module_infos.get(&id)
515 }
516
517 #[cfg(all(test, feature = "artifact-graph"))]
518 pub(crate) fn modules(&self) -> &ModuleInfoMap {
519 &self.global.module_infos
520 }
521
522 #[cfg(all(test, feature = "artifact-graph"))]
523 pub(crate) fn root_module_artifact_state(&self) -> &ModuleArtifactState {
524 &self.global.root_module_artifacts
525 }
526
527 pub fn current_default_units(&self) -> NumericType {
528 NumericType::Default {
529 len: self.length_unit(),
530 angle: self.angle_unit(),
531 }
532 }
533
534 pub fn length_unit(&self) -> UnitLength {
535 self.mod_local.settings.default_length_units
536 }
537
538 pub fn angle_unit(&self) -> UnitAngle {
539 self.mod_local.settings.default_angle_units
540 }
541
542 pub(super) fn circular_import_error(&self, path: &ModulePath, source_range: SourceRange) -> KclError {
543 KclError::new_import_cycle(KclErrorDetails::new(
544 format!(
545 "circular import of modules is not allowed: {} -> {}",
546 self.global
547 .mod_loader
548 .import_stack
549 .iter()
550 .map(|p| p.to_string_lossy())
551 .collect::<Vec<_>>()
552 .join(" -> "),
553 path,
554 ),
555 vec![source_range],
556 ))
557 }
558
559 pub(crate) fn pipe_value(&self) -> Option<&KclValue> {
560 self.mod_local.pipe_value.as_ref()
561 }
562
563 pub(crate) fn error_with_outputs(
564 &self,
565 error: KclError,
566 main_ref: Option<EnvironmentRef>,
567 default_planes: Option<DefaultPlanes>,
568 ) -> KclErrorWithOutputs {
569 let module_id_to_module_path: IndexMap<ModuleId, ModulePath> = self
570 .global
571 .path_to_source_id
572 .iter()
573 .map(|(k, v)| ((*v), k.clone()))
574 .collect();
575
576 KclErrorWithOutputs::new(
577 error,
578 self.errors().to_vec(),
579 main_ref
580 .map(|main_ref| self.mod_local.variables(main_ref))
581 .unwrap_or_default(),
582 #[cfg(feature = "artifact-graph")]
583 self.global.root_module_artifacts.operations.clone(),
584 #[cfg(feature = "artifact-graph")]
585 Default::default(),
586 #[cfg(feature = "artifact-graph")]
587 self.global.artifacts.graph.clone(),
588 #[cfg(feature = "artifact-graph")]
589 self.global.root_module_artifacts.scene_objects.clone(),
590 #[cfg(feature = "artifact-graph")]
591 self.global.root_module_artifacts.source_range_to_object.clone(),
592 #[cfg(feature = "artifact-graph")]
593 self.global.root_module_artifacts.var_solutions.clone(),
594 module_id_to_module_path,
595 self.global.id_to_source.clone(),
596 default_planes,
597 )
598 }
599
600 #[cfg(feature = "artifact-graph")]
601 pub(crate) fn build_program_lookup(
602 &self,
603 current: crate::parsing::ast::types::Node<crate::parsing::ast::types::Program>,
604 ) -> ProgramLookup {
605 ProgramLookup::new(current, self.global.module_infos.clone())
606 }
607
608 #[cfg(feature = "artifact-graph")]
609 pub(crate) async fn build_artifact_graph(
610 &mut self,
611 engine: &Arc<Box<dyn EngineManager>>,
612 program: NodeRef<'_, crate::parsing::ast::types::Program>,
613 ) -> Result<(), KclError> {
614 let mut new_commands = Vec::new();
615 let mut new_exec_artifacts = IndexMap::new();
616 for module in self.global.module_infos.values_mut() {
617 match &mut module.repr {
618 ModuleRepr::Kcl(_, Some(outcome)) => {
619 new_commands.extend(outcome.artifacts.process_commands());
620 new_exec_artifacts.extend(outcome.artifacts.artifacts.clone());
621 }
622 ModuleRepr::Foreign(_, Some((_, module_artifacts))) => {
623 new_commands.extend(module_artifacts.process_commands());
624 new_exec_artifacts.extend(module_artifacts.artifacts.clone());
625 }
626 ModuleRepr::Root | ModuleRepr::Kcl(_, None) | ModuleRepr::Foreign(_, None) | ModuleRepr::Dummy => {}
627 }
628 }
629 new_commands.extend(self.global.root_module_artifacts.process_commands());
632 new_exec_artifacts.extend(self.global.root_module_artifacts.artifacts.clone());
635 let new_responses = engine.take_responses().await;
636
637 for (id, exec_artifact) in new_exec_artifacts {
640 self.global.artifacts.artifacts.entry(id).or_insert(exec_artifact);
644 }
645
646 let initial_graph = self.global.artifacts.graph.clone();
647
648 let programs = self.build_program_lookup(program.clone());
650 let graph_result = crate::execution::artifact::build_artifact_graph(
651 &new_commands,
652 &new_responses,
653 program,
654 &mut self.global.artifacts.artifacts,
655 initial_graph,
656 &programs,
657 &self.global.module_infos,
658 );
659
660 let artifact_graph = graph_result?;
661 self.global.artifacts.graph = artifact_graph;
662
663 Ok(())
664 }
665
666 #[cfg(not(feature = "artifact-graph"))]
667 pub(crate) async fn build_artifact_graph(
668 &mut self,
669 _engine: &Arc<Box<dyn EngineManager>>,
670 _program: NodeRef<'_, crate::parsing::ast::types::Program>,
671 ) -> Result<(), KclError> {
672 Ok(())
673 }
674}
675
676impl GlobalState {
677 fn new(settings: &ExecutorSettings, segment_ids_edited: AhashIndexSet<ObjectId>) -> Self {
678 #[cfg(not(feature = "artifact-graph"))]
679 drop(segment_ids_edited);
680 let mut global = GlobalState {
681 path_to_source_id: Default::default(),
682 module_infos: Default::default(),
683 artifacts: Default::default(),
684 root_module_artifacts: Default::default(),
685 mod_loader: Default::default(),
686 errors: Default::default(),
687 id_to_source: Default::default(),
688 #[cfg(feature = "artifact-graph")]
689 segment_ids_edited,
690 };
691
692 let root_id = ModuleId::default();
693 let root_path = settings.current_file.clone().unwrap_or_default();
694 global.module_infos.insert(
695 root_id,
696 ModuleInfo {
697 id: root_id,
698 path: ModulePath::Local {
699 value: root_path.clone(),
700 original_import_path: None,
701 },
702 repr: ModuleRepr::Root,
703 },
704 );
705 global.path_to_source_id.insert(
706 ModulePath::Local {
707 value: root_path,
708 original_import_path: None,
709 },
710 root_id,
711 );
712 global
713 }
714
715 pub(super) fn filenames(&self) -> IndexMap<ModuleId, ModulePath> {
716 self.path_to_source_id.iter().map(|(k, v)| ((*v), k.clone())).collect()
717 }
718
719 pub(super) fn get_source(&self, id: ModuleId) -> Option<&ModuleSource> {
720 self.id_to_source.get(&id)
721 }
722}
723
724impl ArtifactState {
725 #[cfg(feature = "artifact-graph")]
726 pub fn cached_body_items(&self) -> usize {
727 self.graph.item_count
728 }
729
730 pub(crate) fn clear(&mut self) {
731 #[cfg(feature = "artifact-graph")]
732 {
733 self.artifacts.clear();
734 self.graph.clear();
735 }
736 }
737}
738
739impl ModuleArtifactState {
740 pub(crate) fn clear(&mut self) {
741 #[cfg(feature = "artifact-graph")]
742 {
743 self.artifacts.clear();
744 self.unprocessed_commands.clear();
745 self.commands.clear();
746 self.operations.clear();
747 }
748 }
749
750 #[cfg(feature = "artifact-graph")]
751 pub(crate) fn restore_scene_objects(&mut self, scene_objects: &[Object]) {
752 self.scene_objects = scene_objects.to_vec();
753 self.object_id_generator = IncIdGenerator::new(self.scene_objects.len());
754 self.source_range_to_object.clear();
755 self.artifact_id_to_scene_object.clear();
756
757 for (expected_id, object) in self.scene_objects.iter().enumerate() {
758 debug_assert_eq!(
759 object.id.0, expected_id,
760 "Restored cached scene object ID {} does not match its position {}",
761 object.id.0, expected_id
762 );
763
764 match &object.source {
765 crate::front::SourceRef::Simple { range } => {
766 self.source_range_to_object.insert(*range, object.id);
767 }
768 crate::front::SourceRef::BackTrace { ranges } => {
769 if let Some(range) = ranges.first() {
772 self.source_range_to_object.insert(*range, object.id);
773 }
774 }
775 }
776
777 if object.artifact_id != ArtifactId::placeholder() {
779 self.artifact_id_to_scene_object.insert(object.artifact_id, object.id);
780 }
781 }
782 }
783
784 #[cfg(not(feature = "artifact-graph"))]
785 pub(crate) fn extend(&mut self, _other: ModuleArtifactState) {}
786
787 #[cfg(feature = "artifact-graph")]
789 pub(crate) fn extend(&mut self, other: ModuleArtifactState) {
790 self.artifacts.extend(other.artifacts);
791 self.unprocessed_commands.extend(other.unprocessed_commands);
792 self.commands.extend(other.commands);
793 self.operations.extend(other.operations);
794 if other.scene_objects.len() > self.scene_objects.len() {
795 self.scene_objects
796 .extend(other.scene_objects[self.scene_objects.len()..].iter().cloned());
797 }
798 self.source_range_to_object.extend(other.source_range_to_object);
799 self.artifact_id_to_scene_object
800 .extend(other.artifact_id_to_scene_object);
801 self.var_solutions.extend(other.var_solutions);
802 }
803
804 #[cfg(feature = "artifact-graph")]
808 pub(crate) fn process_commands(&mut self) -> Vec<ArtifactCommand> {
809 let unprocessed = std::mem::take(&mut self.unprocessed_commands);
810 let new_module_commands = unprocessed.clone();
811 self.commands.extend(unprocessed);
812 new_module_commands
813 }
814
815 #[cfg_attr(not(feature = "artifact-graph"), expect(dead_code))]
816 pub(crate) fn scene_object_by_id(&self, id: ObjectId) -> Option<&Object> {
817 #[cfg(feature = "artifact-graph")]
818 {
819 debug_assert!(
820 id.0 < self.scene_objects.len(),
821 "Requested object ID {} but only have {} objects",
822 id.0,
823 self.scene_objects.len()
824 );
825 self.scene_objects.get(id.0)
826 }
827 #[cfg(not(feature = "artifact-graph"))]
828 {
829 let _ = id;
830 None
831 }
832 }
833
834 #[cfg_attr(not(feature = "artifact-graph"), expect(dead_code))]
835 pub(crate) fn scene_object_by_id_mut(&mut self, id: ObjectId) -> Option<&mut Object> {
836 #[cfg(feature = "artifact-graph")]
837 {
838 debug_assert!(
839 id.0 < self.scene_objects.len(),
840 "Requested object ID {} but only have {} objects",
841 id.0,
842 self.scene_objects.len()
843 );
844 self.scene_objects.get_mut(id.0)
845 }
846 #[cfg(not(feature = "artifact-graph"))]
847 {
848 let _ = id;
849 None
850 }
851 }
852}
853
854impl ModuleState {
855 pub(super) fn new(
856 path: ModulePath,
857 memory: Arc<ProgramMemory>,
858 module_id: Option<ModuleId>,
859 sketch_mode: bool,
860 freedom_analysis: bool,
861 ) -> Self {
862 ModuleState {
863 id_generator: IdGenerator::new(module_id),
864 stack: memory.new_stack(),
865 call_stack_size: 0,
866 pipe_value: Default::default(),
867 being_declared: Default::default(),
868 sketch_block: Default::default(),
869 stdlib_entry_source_range: Default::default(),
870 module_exports: Default::default(),
871 explicit_length_units: false,
872 path,
873 settings: Default::default(),
874 sketch_mode,
875 freedom_analysis,
876 artifacts: Default::default(),
877 allowed_warnings: Vec::new(),
878 denied_warnings: Vec::new(),
879 inside_stdlib: false,
880 }
881 }
882
883 pub(super) fn variables(&self, main_ref: EnvironmentRef) -> IndexMap<String, KclValue> {
884 self.stack
885 .find_all_in_env(main_ref)
886 .map(|(k, v)| (k.clone(), v.clone()))
887 .collect()
888 }
889}
890
891impl SketchBlockState {
892 pub(crate) fn next_sketch_var_id(&self) -> SketchVarId {
893 SketchVarId(self.sketch_vars.len())
894 }
895
896 #[cfg(feature = "artifact-graph")]
899 pub(crate) fn var_solutions(
900 &self,
901 solve_outcome: &Solved,
902 solution_ty: NumericType,
903 range: SourceRange,
904 ) -> Result<Vec<(SourceRange, Number)>, KclError> {
905 self.sketch_vars
906 .iter()
907 .map(|v| {
908 let Some(sketch_var) = v.as_sketch_var() else {
909 return Err(KclError::new_internal(KclErrorDetails::new(
910 "Expected sketch variable".to_owned(),
911 vec![range],
912 )));
913 };
914 let var_index = sketch_var.id.0;
915 let solved_n = solve_outcome.final_values.get(var_index).ok_or_else(|| {
916 let message = format!("No solution for sketch variable with id {}", var_index);
917 debug_assert!(false, "{}", &message);
918 KclError::new_internal(KclErrorDetails::new(
919 message,
920 sketch_var.meta.iter().map(|m| m.source_range).collect(),
921 ))
922 })?;
923 let solved_value = Number {
924 value: *solved_n,
925 units: solution_ty.try_into().map_err(|_| {
926 KclError::new_internal(KclErrorDetails::new(
927 "Failed to convert numeric type to units".to_owned(),
928 vec![range],
929 ))
930 })?,
931 };
932 let Some(source_range) = sketch_var.meta.first().map(|m| m.source_range) else {
933 return Ok(None);
934 };
935 Ok(Some((source_range, solved_value)))
936 })
937 .filter_map(Result::transpose)
938 .collect::<Result<Vec<_>, KclError>>()
939 }
940}
941
942#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS)]
943#[ts(export)]
944#[serde(rename_all = "camelCase")]
945pub struct MetaSettings {
946 pub default_length_units: UnitLength,
947 pub default_angle_units: UnitAngle,
948 pub experimental_features: annotations::WarningLevel,
949 pub kcl_version: String,
950}
951
952impl Default for MetaSettings {
953 fn default() -> Self {
954 MetaSettings {
955 default_length_units: UnitLength::Millimeters,
956 default_angle_units: UnitAngle::Degrees,
957 experimental_features: annotations::WarningLevel::Deny,
958 kcl_version: "1.0".to_owned(),
959 }
960 }
961}
962
963impl MetaSettings {
964 pub(crate) fn update_from_annotation(
965 &mut self,
966 annotation: &crate::parsing::ast::types::Node<Annotation>,
967 ) -> Result<(bool, bool), KclError> {
968 let properties = annotations::expect_properties(annotations::SETTINGS, annotation)?;
969
970 let mut updated_len = false;
971 let mut updated_angle = false;
972 for p in properties {
973 match &*p.inner.key.name {
974 annotations::SETTINGS_UNIT_LENGTH => {
975 let value = annotations::expect_ident(&p.inner.value)?;
976 let value = super::types::length_from_str(value, annotation.as_source_range())?;
977 self.default_length_units = value;
978 updated_len = true;
979 }
980 annotations::SETTINGS_UNIT_ANGLE => {
981 let value = annotations::expect_ident(&p.inner.value)?;
982 let value = super::types::angle_from_str(value, annotation.as_source_range())?;
983 self.default_angle_units = value;
984 updated_angle = true;
985 }
986 annotations::SETTINGS_VERSION => {
987 let value = annotations::expect_number(&p.inner.value)?;
988 self.kcl_version = value;
989 }
990 annotations::SETTINGS_EXPERIMENTAL_FEATURES => {
991 let value = annotations::expect_ident(&p.inner.value)?;
992 let value = annotations::WarningLevel::from_str(value).map_err(|_| {
993 KclError::new_semantic(KclErrorDetails::new(
994 format!(
995 "Invalid value for {} settings property, expected one of: {}",
996 annotations::SETTINGS_EXPERIMENTAL_FEATURES,
997 annotations::WARN_LEVELS.join(", ")
998 ),
999 annotation.as_source_ranges(),
1000 ))
1001 })?;
1002 self.experimental_features = value;
1003 }
1004 name => {
1005 return Err(KclError::new_semantic(KclErrorDetails::new(
1006 format!(
1007 "Unexpected settings key: `{name}`; expected one of `{}`, `{}`",
1008 annotations::SETTINGS_UNIT_LENGTH,
1009 annotations::SETTINGS_UNIT_ANGLE
1010 ),
1011 vec![annotation.as_source_range()],
1012 )));
1013 }
1014 }
1015 }
1016
1017 Ok((updated_len, updated_angle))
1018 }
1019}
1020
1021#[cfg(all(feature = "artifact-graph", test))]
1022mod tests {
1023 use uuid::Uuid;
1024
1025 use super::ModuleArtifactState;
1026 use crate::SourceRange;
1027 use crate::execution::ArtifactId;
1028 use crate::front::Object;
1029 use crate::front::ObjectId;
1030 use crate::front::ObjectKind;
1031 use crate::front::Plane;
1032 use crate::front::SourceRef;
1033
1034 #[test]
1035 fn restore_scene_objects_rebuilds_lookup_maps() {
1036 let plane_artifact_id = ArtifactId::new(Uuid::from_u128(1));
1037 let sketch_artifact_id = ArtifactId::new(Uuid::from_u128(2));
1038 let plane_range = SourceRange::from([1, 4, 0]);
1039 let sketch_ranges = vec![SourceRange::from([5, 9, 0]), SourceRange::from([10, 12, 0])];
1040 let cached_objects = vec![
1041 Object {
1042 id: ObjectId(0),
1043 kind: ObjectKind::Plane(Plane::Object(ObjectId(0))),
1044 label: Default::default(),
1045 comments: Default::default(),
1046 artifact_id: plane_artifact_id,
1047 source: SourceRef::Simple { range: plane_range },
1048 },
1049 Object {
1050 id: ObjectId(1),
1051 kind: ObjectKind::Nil,
1052 label: Default::default(),
1053 comments: Default::default(),
1054 artifact_id: sketch_artifact_id,
1055 source: SourceRef::BackTrace {
1056 ranges: sketch_ranges.clone(),
1057 },
1058 },
1059 Object::placeholder(ObjectId(2), SourceRange::from([13, 14, 0])),
1060 ];
1061
1062 let mut artifacts = ModuleArtifactState::default();
1063 artifacts.restore_scene_objects(&cached_objects);
1064
1065 assert_eq!(artifacts.scene_objects, cached_objects);
1066 assert_eq!(
1067 artifacts.artifact_id_to_scene_object.get(&plane_artifact_id),
1068 Some(&ObjectId(0))
1069 );
1070 assert_eq!(
1071 artifacts.artifact_id_to_scene_object.get(&sketch_artifact_id),
1072 Some(&ObjectId(1))
1073 );
1074 assert_eq!(
1075 artifacts.artifact_id_to_scene_object.get(&ArtifactId::placeholder()),
1076 None
1077 );
1078 assert_eq!(artifacts.source_range_to_object.get(&plane_range), Some(&ObjectId(0)));
1079 assert_eq!(
1080 artifacts.source_range_to_object.get(&sketch_ranges[0]),
1081 Some(&ObjectId(1))
1082 );
1083 assert_eq!(artifacts.source_range_to_object.get(&sketch_ranges[1]), None);
1085 }
1086}