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 self.global.artifacts.artifacts.extend(new_exec_artifacts);
640
641 let initial_graph = self.global.artifacts.graph.clone();
642
643 let programs = self.build_program_lookup(program.clone());
645 let graph_result = crate::execution::artifact::build_artifact_graph(
646 &new_commands,
647 &new_responses,
648 program,
649 &mut self.global.artifacts.artifacts,
650 initial_graph,
651 &programs,
652 &self.global.module_infos,
653 );
654
655 let artifact_graph = graph_result?;
656 self.global.artifacts.graph = artifact_graph;
657
658 Ok(())
659 }
660
661 #[cfg(not(feature = "artifact-graph"))]
662 pub(crate) async fn build_artifact_graph(
663 &mut self,
664 _engine: &Arc<Box<dyn EngineManager>>,
665 _program: NodeRef<'_, crate::parsing::ast::types::Program>,
666 ) -> Result<(), KclError> {
667 Ok(())
668 }
669}
670
671impl GlobalState {
672 fn new(settings: &ExecutorSettings, segment_ids_edited: AhashIndexSet<ObjectId>) -> Self {
673 #[cfg(not(feature = "artifact-graph"))]
674 drop(segment_ids_edited);
675 let mut global = GlobalState {
676 path_to_source_id: Default::default(),
677 module_infos: Default::default(),
678 artifacts: Default::default(),
679 root_module_artifacts: Default::default(),
680 mod_loader: Default::default(),
681 errors: Default::default(),
682 id_to_source: Default::default(),
683 #[cfg(feature = "artifact-graph")]
684 segment_ids_edited,
685 };
686
687 let root_id = ModuleId::default();
688 let root_path = settings.current_file.clone().unwrap_or_default();
689 global.module_infos.insert(
690 root_id,
691 ModuleInfo {
692 id: root_id,
693 path: ModulePath::Local {
694 value: root_path.clone(),
695 original_import_path: None,
696 },
697 repr: ModuleRepr::Root,
698 },
699 );
700 global.path_to_source_id.insert(
701 ModulePath::Local {
702 value: root_path,
703 original_import_path: None,
704 },
705 root_id,
706 );
707 global
708 }
709
710 pub(super) fn filenames(&self) -> IndexMap<ModuleId, ModulePath> {
711 self.path_to_source_id.iter().map(|(k, v)| ((*v), k.clone())).collect()
712 }
713
714 pub(super) fn get_source(&self, id: ModuleId) -> Option<&ModuleSource> {
715 self.id_to_source.get(&id)
716 }
717}
718
719impl ArtifactState {
720 #[cfg(feature = "artifact-graph")]
721 pub fn cached_body_items(&self) -> usize {
722 self.graph.item_count
723 }
724
725 pub(crate) fn clear(&mut self) {
726 #[cfg(feature = "artifact-graph")]
727 {
728 self.artifacts.clear();
729 self.graph.clear();
730 }
731 }
732}
733
734impl ModuleArtifactState {
735 pub(crate) fn clear(&mut self) {
736 #[cfg(feature = "artifact-graph")]
737 {
738 self.artifacts.clear();
739 self.unprocessed_commands.clear();
740 self.commands.clear();
741 self.operations.clear();
742 }
743 }
744
745 #[cfg(feature = "artifact-graph")]
746 pub(crate) fn restore_scene_objects(&mut self, scene_objects: &[Object]) {
747 self.scene_objects = scene_objects.to_vec();
748 self.object_id_generator = IncIdGenerator::new(self.scene_objects.len());
749 self.source_range_to_object.clear();
750 self.artifact_id_to_scene_object.clear();
751
752 for (expected_id, object) in self.scene_objects.iter().enumerate() {
753 debug_assert_eq!(
754 object.id.0, expected_id,
755 "Restored cached scene object ID {} does not match its position {}",
756 object.id.0, expected_id
757 );
758
759 match &object.source {
760 crate::front::SourceRef::Simple { range } => {
761 self.source_range_to_object.insert(*range, object.id);
762 }
763 crate::front::SourceRef::BackTrace { ranges } => {
764 if let Some(range) = ranges.first() {
767 self.source_range_to_object.insert(*range, object.id);
768 }
769 }
770 }
771
772 if object.artifact_id != ArtifactId::placeholder() {
774 self.artifact_id_to_scene_object.insert(object.artifact_id, object.id);
775 }
776 }
777 }
778
779 #[cfg(not(feature = "artifact-graph"))]
780 pub(crate) fn extend(&mut self, _other: ModuleArtifactState) {}
781
782 #[cfg(feature = "artifact-graph")]
784 pub(crate) fn extend(&mut self, other: ModuleArtifactState) {
785 self.artifacts.extend(other.artifacts);
786 self.unprocessed_commands.extend(other.unprocessed_commands);
787 self.commands.extend(other.commands);
788 self.operations.extend(other.operations);
789 if other.scene_objects.len() > self.scene_objects.len() {
790 self.scene_objects
791 .extend(other.scene_objects[self.scene_objects.len()..].iter().cloned());
792 }
793 self.source_range_to_object.extend(other.source_range_to_object);
794 self.artifact_id_to_scene_object
795 .extend(other.artifact_id_to_scene_object);
796 self.var_solutions.extend(other.var_solutions);
797 }
798
799 #[cfg(feature = "artifact-graph")]
803 pub(crate) fn process_commands(&mut self) -> Vec<ArtifactCommand> {
804 let unprocessed = std::mem::take(&mut self.unprocessed_commands);
805 let new_module_commands = unprocessed.clone();
806 self.commands.extend(unprocessed);
807 new_module_commands
808 }
809
810 #[cfg_attr(not(feature = "artifact-graph"), expect(dead_code))]
811 pub(crate) fn scene_object_by_id(&self, id: ObjectId) -> Option<&Object> {
812 #[cfg(feature = "artifact-graph")]
813 {
814 debug_assert!(
815 id.0 < self.scene_objects.len(),
816 "Requested object ID {} but only have {} objects",
817 id.0,
818 self.scene_objects.len()
819 );
820 self.scene_objects.get(id.0)
821 }
822 #[cfg(not(feature = "artifact-graph"))]
823 {
824 let _ = id;
825 None
826 }
827 }
828
829 #[cfg_attr(not(feature = "artifact-graph"), expect(dead_code))]
830 pub(crate) fn scene_object_by_id_mut(&mut self, id: ObjectId) -> Option<&mut Object> {
831 #[cfg(feature = "artifact-graph")]
832 {
833 debug_assert!(
834 id.0 < self.scene_objects.len(),
835 "Requested object ID {} but only have {} objects",
836 id.0,
837 self.scene_objects.len()
838 );
839 self.scene_objects.get_mut(id.0)
840 }
841 #[cfg(not(feature = "artifact-graph"))]
842 {
843 let _ = id;
844 None
845 }
846 }
847}
848
849impl ModuleState {
850 pub(super) fn new(
851 path: ModulePath,
852 memory: Arc<ProgramMemory>,
853 module_id: Option<ModuleId>,
854 sketch_mode: bool,
855 freedom_analysis: bool,
856 ) -> Self {
857 ModuleState {
858 id_generator: IdGenerator::new(module_id),
859 stack: memory.new_stack(),
860 call_stack_size: 0,
861 pipe_value: Default::default(),
862 being_declared: Default::default(),
863 sketch_block: Default::default(),
864 stdlib_entry_source_range: Default::default(),
865 module_exports: Default::default(),
866 explicit_length_units: false,
867 path,
868 settings: Default::default(),
869 sketch_mode,
870 freedom_analysis,
871 artifacts: Default::default(),
872 allowed_warnings: Vec::new(),
873 denied_warnings: Vec::new(),
874 inside_stdlib: false,
875 }
876 }
877
878 pub(super) fn variables(&self, main_ref: EnvironmentRef) -> IndexMap<String, KclValue> {
879 self.stack
880 .find_all_in_env(main_ref)
881 .map(|(k, v)| (k.clone(), v.clone()))
882 .collect()
883 }
884}
885
886impl SketchBlockState {
887 pub(crate) fn next_sketch_var_id(&self) -> SketchVarId {
888 SketchVarId(self.sketch_vars.len())
889 }
890
891 #[cfg(feature = "artifact-graph")]
894 pub(crate) fn var_solutions(
895 &self,
896 solve_outcome: &Solved,
897 solution_ty: NumericType,
898 range: SourceRange,
899 ) -> Result<Vec<(SourceRange, Number)>, KclError> {
900 self.sketch_vars
901 .iter()
902 .map(|v| {
903 let Some(sketch_var) = v.as_sketch_var() else {
904 return Err(KclError::new_internal(KclErrorDetails::new(
905 "Expected sketch variable".to_owned(),
906 vec![range],
907 )));
908 };
909 let var_index = sketch_var.id.0;
910 let solved_n = solve_outcome.final_values.get(var_index).ok_or_else(|| {
911 let message = format!("No solution for sketch variable with id {}", var_index);
912 debug_assert!(false, "{}", &message);
913 KclError::new_internal(KclErrorDetails::new(
914 message,
915 sketch_var.meta.iter().map(|m| m.source_range).collect(),
916 ))
917 })?;
918 let solved_value = Number {
919 value: *solved_n,
920 units: solution_ty.try_into().map_err(|_| {
921 KclError::new_internal(KclErrorDetails::new(
922 "Failed to convert numeric type to units".to_owned(),
923 vec![range],
924 ))
925 })?,
926 };
927 let Some(source_range) = sketch_var.meta.first().map(|m| m.source_range) else {
928 return Ok(None);
929 };
930 Ok(Some((source_range, solved_value)))
931 })
932 .filter_map(Result::transpose)
933 .collect::<Result<Vec<_>, KclError>>()
934 }
935}
936
937#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS)]
938#[ts(export)]
939#[serde(rename_all = "camelCase")]
940pub struct MetaSettings {
941 pub default_length_units: UnitLength,
942 pub default_angle_units: UnitAngle,
943 pub experimental_features: annotations::WarningLevel,
944 pub kcl_version: String,
945}
946
947impl Default for MetaSettings {
948 fn default() -> Self {
949 MetaSettings {
950 default_length_units: UnitLength::Millimeters,
951 default_angle_units: UnitAngle::Degrees,
952 experimental_features: annotations::WarningLevel::Deny,
953 kcl_version: "1.0".to_owned(),
954 }
955 }
956}
957
958impl MetaSettings {
959 pub(crate) fn update_from_annotation(
960 &mut self,
961 annotation: &crate::parsing::ast::types::Node<Annotation>,
962 ) -> Result<(bool, bool), KclError> {
963 let properties = annotations::expect_properties(annotations::SETTINGS, annotation)?;
964
965 let mut updated_len = false;
966 let mut updated_angle = false;
967 for p in properties {
968 match &*p.inner.key.name {
969 annotations::SETTINGS_UNIT_LENGTH => {
970 let value = annotations::expect_ident(&p.inner.value)?;
971 let value = super::types::length_from_str(value, annotation.as_source_range())?;
972 self.default_length_units = value;
973 updated_len = true;
974 }
975 annotations::SETTINGS_UNIT_ANGLE => {
976 let value = annotations::expect_ident(&p.inner.value)?;
977 let value = super::types::angle_from_str(value, annotation.as_source_range())?;
978 self.default_angle_units = value;
979 updated_angle = true;
980 }
981 annotations::SETTINGS_VERSION => {
982 let value = annotations::expect_number(&p.inner.value)?;
983 self.kcl_version = value;
984 }
985 annotations::SETTINGS_EXPERIMENTAL_FEATURES => {
986 let value = annotations::expect_ident(&p.inner.value)?;
987 let value = annotations::WarningLevel::from_str(value).map_err(|_| {
988 KclError::new_semantic(KclErrorDetails::new(
989 format!(
990 "Invalid value for {} settings property, expected one of: {}",
991 annotations::SETTINGS_EXPERIMENTAL_FEATURES,
992 annotations::WARN_LEVELS.join(", ")
993 ),
994 annotation.as_source_ranges(),
995 ))
996 })?;
997 self.experimental_features = value;
998 }
999 name => {
1000 return Err(KclError::new_semantic(KclErrorDetails::new(
1001 format!(
1002 "Unexpected settings key: `{name}`; expected one of `{}`, `{}`",
1003 annotations::SETTINGS_UNIT_LENGTH,
1004 annotations::SETTINGS_UNIT_ANGLE
1005 ),
1006 vec![annotation.as_source_range()],
1007 )));
1008 }
1009 }
1010 }
1011
1012 Ok((updated_len, updated_angle))
1013 }
1014}
1015
1016#[cfg(all(feature = "artifact-graph", test))]
1017mod tests {
1018 use uuid::Uuid;
1019
1020 use super::ModuleArtifactState;
1021 use crate::SourceRange;
1022 use crate::execution::ArtifactId;
1023 use crate::front::Object;
1024 use crate::front::ObjectId;
1025 use crate::front::ObjectKind;
1026 use crate::front::Plane;
1027 use crate::front::SourceRef;
1028
1029 #[test]
1030 fn restore_scene_objects_rebuilds_lookup_maps() {
1031 let plane_artifact_id = ArtifactId::new(Uuid::from_u128(1));
1032 let sketch_artifact_id = ArtifactId::new(Uuid::from_u128(2));
1033 let plane_range = SourceRange::from([1, 4, 0]);
1034 let sketch_ranges = vec![SourceRange::from([5, 9, 0]), SourceRange::from([10, 12, 0])];
1035 let cached_objects = vec![
1036 Object {
1037 id: ObjectId(0),
1038 kind: ObjectKind::Plane(Plane::Object(ObjectId(0))),
1039 label: Default::default(),
1040 comments: Default::default(),
1041 artifact_id: plane_artifact_id,
1042 source: SourceRef::Simple { range: plane_range },
1043 },
1044 Object {
1045 id: ObjectId(1),
1046 kind: ObjectKind::Nil,
1047 label: Default::default(),
1048 comments: Default::default(),
1049 artifact_id: sketch_artifact_id,
1050 source: SourceRef::BackTrace {
1051 ranges: sketch_ranges.clone(),
1052 },
1053 },
1054 Object::placeholder(ObjectId(2), SourceRange::from([13, 14, 0])),
1055 ];
1056
1057 let mut artifacts = ModuleArtifactState::default();
1058 artifacts.restore_scene_objects(&cached_objects);
1059
1060 assert_eq!(artifacts.scene_objects, cached_objects);
1061 assert_eq!(
1062 artifacts.artifact_id_to_scene_object.get(&plane_artifact_id),
1063 Some(&ObjectId(0))
1064 );
1065 assert_eq!(
1066 artifacts.artifact_id_to_scene_object.get(&sketch_artifact_id),
1067 Some(&ObjectId(1))
1068 );
1069 assert_eq!(
1070 artifacts.artifact_id_to_scene_object.get(&ArtifactId::placeholder()),
1071 None
1072 );
1073 assert_eq!(artifacts.source_range_to_object.get(&plane_range), Some(&ObjectId(0)));
1074 assert_eq!(
1075 artifacts.source_range_to_object.get(&sketch_ranges[0]),
1076 Some(&ObjectId(1))
1077 );
1078 assert_eq!(artifacts.source_range_to_object.get(&sketch_ranges[1]), None);
1080 }
1081}