1#[cfg(feature = "artifact-graph")]
2use std::collections::BTreeMap;
3use std::str::FromStr;
4use std::sync::Arc;
5
6use ahash::AHashMap;
7use anyhow::Result;
8use indexmap::IndexMap;
9use kittycad_modeling_cmds::units::UnitAngle;
10use kittycad_modeling_cmds::units::UnitLength;
11use serde::Deserialize;
12use serde::Serialize;
13use uuid::Uuid;
14
15use crate::CompilationIssue;
16use crate::EngineManager;
17use crate::ExecutorContext;
18use crate::KclErrorWithOutputs;
19use crate::MockConfig;
20#[cfg(feature = "artifact-graph")]
21use crate::NodePath;
22use crate::SourceRange;
23use crate::collections::AhashIndexSet;
24use crate::errors::KclError;
25use crate::errors::KclErrorDetails;
26use crate::errors::Severity;
27use crate::exec::DefaultPlanes;
28#[cfg(feature = "artifact-graph")]
29use crate::execution::Artifact;
30#[cfg(feature = "artifact-graph")]
31use crate::execution::ArtifactCommand;
32#[cfg(feature = "artifact-graph")]
33use crate::execution::ArtifactGraph;
34#[cfg(feature = "artifact-graph")]
35use crate::execution::ArtifactId;
36use crate::execution::EnvironmentRef;
37use crate::execution::ExecOutcome;
38use crate::execution::ExecutorSettings;
39use crate::execution::KclValue;
40#[cfg(feature = "artifact-graph")]
41use crate::execution::ProgramLookup;
42use crate::execution::SketchVarId;
43use crate::execution::UnsolvedSegment;
44use crate::execution::annotations;
45use crate::execution::cad_op::Operation;
46use crate::execution::id_generator::IdGenerator;
47use crate::execution::memory::ProgramMemory;
48use crate::execution::memory::Stack;
49#[cfg(feature = "artifact-graph")]
50use crate::execution::sketch_solve::Solved;
51use crate::execution::types::NumericType;
52#[cfg(feature = "artifact-graph")]
53use crate::front::Number;
54use crate::front::Object;
55use crate::front::ObjectId;
56use crate::id::IncIdGenerator;
57use crate::modules::ModuleId;
58use crate::modules::ModuleInfo;
59use crate::modules::ModuleLoader;
60use crate::modules::ModulePath;
61use crate::modules::ModuleRepr;
62use crate::modules::ModuleSource;
63use crate::parsing::ast::types::Annotation;
64use crate::parsing::ast::types::NodeRef;
65use crate::parsing::ast::types::TagNode;
66
67#[derive(Debug, Clone)]
69pub struct ExecState {
70 pub(super) global: GlobalState,
71 pub(super) mod_local: ModuleState,
72}
73
74pub type ModuleInfoMap = IndexMap<ModuleId, ModuleInfo>;
75
76#[derive(Debug, Clone)]
77pub(super) struct GlobalState {
78 pub path_to_source_id: IndexMap<ModulePath, ModuleId>,
80 pub id_to_source: IndexMap<ModuleId, ModuleSource>,
82 pub module_infos: ModuleInfoMap,
84 pub mod_loader: ModuleLoader,
86 pub issues: Vec<CompilationIssue>,
88 pub artifacts: ArtifactState,
90 pub root_module_artifacts: ModuleArtifactState,
92 #[cfg(feature = "artifact-graph")]
94 pub segment_ids_edited: AhashIndexSet<ObjectId>,
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
98pub(crate) enum ConstraintKey {
99 LineCircle([usize; 10]),
100 CircleCircle([usize; 12]),
101}
102
103#[derive(Debug, Clone, Copy, PartialEq, Eq)]
104pub(crate) enum TangencyMode {
105 LineCircle(ezpz::LineSide),
106 CircleCircle(ezpz::CircleSide),
107}
108
109#[derive(Debug, Clone, Copy, PartialEq, Eq)]
110pub(crate) enum ConstraintState {
111 Tangency(TangencyMode),
112}
113
114#[cfg(feature = "artifact-graph")]
115#[derive(Debug, Clone, Default)]
116pub(super) struct ArtifactState {
117 pub artifacts: IndexMap<ArtifactId, Artifact>,
120 pub graph: ArtifactGraph,
122}
123
124#[cfg(not(feature = "artifact-graph"))]
125#[derive(Debug, Clone, Default)]
126pub(super) struct ArtifactState {}
127
128#[cfg(feature = "artifact-graph")]
130#[derive(Debug, Clone, Default, PartialEq, Serialize)]
131pub struct ModuleArtifactState {
132 pub artifacts: IndexMap<ArtifactId, Artifact>,
134 #[serde(skip)]
137 pub unprocessed_commands: Vec<ArtifactCommand>,
138 pub commands: Vec<ArtifactCommand>,
140 #[cfg(feature = "snapshot-engine-responses")]
142 pub responses: IndexMap<Uuid, kittycad_modeling_cmds::websocket::WebSocketResponse>,
143 pub operations: Vec<Operation>,
146 pub object_id_generator: IncIdGenerator<usize>,
148 pub scene_objects: Vec<Object>,
150 pub source_range_to_object: BTreeMap<SourceRange, ObjectId>,
153 pub artifact_id_to_scene_object: IndexMap<ArtifactId, ObjectId>,
155 pub var_solutions: Vec<(SourceRange, Number)>,
157}
158
159#[cfg(not(feature = "artifact-graph"))]
160#[derive(Debug, Clone, Default, PartialEq, Serialize)]
161pub struct ModuleArtifactState {
162 pub object_id_generator: IncIdGenerator<usize>,
164}
165
166#[derive(Debug, Clone)]
167pub(super) struct ModuleState {
168 pub id_generator: IdGenerator,
170 pub stack: Stack,
171 pub(super) call_stack_size: usize,
175 pub pipe_value: Option<KclValue>,
178 pub being_declared: Option<String>,
182 pub sketch_block: Option<SketchBlockState>,
184 pub inside_stdlib: bool,
187 pub stdlib_entry_source_range: Option<SourceRange>,
189 pub module_exports: Vec<String>,
191 pub settings: MetaSettings,
193 pub sketch_mode: bool,
196 pub freedom_analysis: bool,
200 pub(super) explicit_length_units: bool,
201 pub(super) path: ModulePath,
202 pub artifacts: ModuleArtifactState,
204 pub constraint_state: IndexMap<ObjectId, IndexMap<ConstraintKey, ConstraintState>>,
208
209 pub(super) allowed_warnings: Vec<&'static str>,
210 pub(super) denied_warnings: Vec<&'static str>,
211
212 pub(super) consumed_solids: AHashMap<Uuid, ConsumedSolidInfo>,
217}
218
219#[derive(Debug, Clone)]
223pub(crate) struct ConsumedSolidInfo {
224 pub operation: ConsumedSolidOperation,
226 pub output_solid_id: Option<Uuid>,
230}
231
232#[derive(Debug, Clone, Copy, PartialEq, Eq)]
233pub(crate) enum ConsumedSolidOperation {
234 Union,
235 Intersect,
236 Subtract,
237 Split,
238 JoinSurfaces,
239}
240
241impl ConsumedSolidOperation {
242 pub(crate) fn indefinite_article(self) -> &'static str {
243 match self {
244 Self::Intersect => "an",
245 Self::Union | Self::Subtract | Self::Split | Self::JoinSurfaces => "a",
246 }
247 }
248}
249
250impl std::fmt::Display for ConsumedSolidOperation {
251 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
252 match self {
253 Self::Union => f.write_str("union"),
254 Self::Intersect => f.write_str("intersect"),
255 Self::Subtract => f.write_str("subtract"),
256 Self::Split => f.write_str("split"),
257 Self::JoinSurfaces => f.write_str("joinSurfaces"),
258 }
259 }
260}
261
262#[derive(Debug, Clone, Default)]
263pub(crate) struct SketchBlockState {
264 pub sketch_vars: Vec<KclValue>,
265 pub sketch_id: Option<ObjectId>,
266 #[cfg(feature = "artifact-graph")]
267 pub sketch_constraints: Vec<ObjectId>,
268 pub solver_constraints: Vec<ezpz::Constraint>,
269 pub solver_optional_constraints: Vec<ezpz::Constraint>,
270 pub needed_by_engine: Vec<UnsolvedSegment>,
271 pub segment_tags: IndexMap<ObjectId, TagNode>,
272}
273
274impl ExecState {
275 pub fn new(exec_context: &super::ExecutorContext) -> Self {
276 ExecState {
277 global: GlobalState::new(&exec_context.settings, Default::default()),
278 mod_local: ModuleState::new(ModulePath::Main, ProgramMemory::new(), Default::default(), false, true),
279 }
280 }
281
282 pub fn new_mock(exec_context: &super::ExecutorContext, mock_config: &MockConfig) -> Self {
283 #[cfg(feature = "artifact-graph")]
284 let segment_ids_edited = mock_config.segment_ids_edited.clone();
285 #[cfg(not(feature = "artifact-graph"))]
286 let segment_ids_edited = Default::default();
287 ExecState {
288 global: GlobalState::new(&exec_context.settings, segment_ids_edited),
289 mod_local: ModuleState::new(
290 ModulePath::Main,
291 ProgramMemory::new(),
292 Default::default(),
293 mock_config.sketch_block_id.is_some(),
294 mock_config.freedom_analysis,
295 ),
296 }
297 }
298
299 pub(super) fn reset(&mut self, exec_context: &super::ExecutorContext) {
300 let global = GlobalState::new(&exec_context.settings, Default::default());
301
302 *self = ExecState {
303 global,
304 mod_local: ModuleState::new(
305 self.mod_local.path.clone(),
306 ProgramMemory::new(),
307 Default::default(),
308 false,
309 true,
310 ),
311 };
312 }
313
314 pub fn err(&mut self, e: CompilationIssue) {
316 self.global.issues.push(e);
317 }
318
319 pub fn warn(&mut self, mut e: CompilationIssue, name: &'static str) {
321 debug_assert!(annotations::WARN_VALUES.contains(&name));
322
323 if self.mod_local.allowed_warnings.contains(&name) {
324 return;
325 }
326
327 if self.mod_local.denied_warnings.contains(&name) {
328 e.severity = Severity::Error;
329 } else {
330 e.severity = Severity::Warning;
331 }
332
333 self.global.issues.push(e);
334 }
335
336 pub fn warn_experimental(&mut self, feature_name: &str, source_range: SourceRange) {
337 let Some(severity) = self.mod_local.settings.experimental_features.severity() else {
338 return;
339 };
340 let error = CompilationIssue {
341 source_range,
342 message: format!("Use of {feature_name} is experimental and may change or be removed."),
343 suggestion: None,
344 severity,
345 tag: crate::errors::Tag::None,
346 };
347
348 self.global.issues.push(error);
349 }
350
351 pub fn clear_units_warnings(&mut self, source_range: &SourceRange) {
352 self.global.issues = std::mem::take(&mut self.global.issues)
353 .into_iter()
354 .filter(|e| {
355 e.severity != Severity::Warning
356 || !source_range.contains_range(&e.source_range)
357 || e.tag != crate::errors::Tag::UnknownNumericUnits
358 })
359 .collect();
360 }
361
362 pub fn issues(&self) -> &[CompilationIssue] {
363 &self.global.issues
364 }
365
366 pub async fn into_exec_outcome(self, main_ref: EnvironmentRef, ctx: &ExecutorContext) -> ExecOutcome {
370 ExecOutcome {
373 variables: self.mod_local.variables(main_ref),
374 filenames: self.global.filenames(),
375 #[cfg(feature = "artifact-graph")]
376 operations: self.global.root_module_artifacts.operations,
377 #[cfg(feature = "artifact-graph")]
378 artifact_graph: self.global.artifacts.graph,
379 #[cfg(feature = "artifact-graph")]
380 scene_objects: self.global.root_module_artifacts.scene_objects,
381 #[cfg(feature = "artifact-graph")]
382 source_range_to_object: self.global.root_module_artifacts.source_range_to_object,
383 #[cfg(feature = "artifact-graph")]
384 var_solutions: self.global.root_module_artifacts.var_solutions,
385 issues: self.global.issues,
386 default_planes: ctx.engine.get_default_planes().read().await.clone(),
387 }
388 }
389
390 #[cfg(all(feature = "artifact-graph", feature = "snapshot-engine-responses"))]
391 pub(crate) fn take_root_module_responses(
392 &mut self,
393 ) -> IndexMap<Uuid, kittycad_modeling_cmds::websocket::WebSocketResponse> {
394 std::mem::take(&mut self.global.root_module_artifacts.responses)
395 }
396
397 pub(crate) fn stack(&self) -> &Stack {
398 &self.mod_local.stack
399 }
400
401 pub(crate) fn mut_stack(&mut self) -> &mut Stack {
402 &mut self.mod_local.stack
403 }
404
405 pub(super) fn inc_call_stack_size(&mut self, range: SourceRange) -> Result<(), KclError> {
408 if self.mod_local.call_stack_size >= 50 {
411 return Err(KclError::MaxCallStack {
412 details: KclErrorDetails::new("maximum call stack size exceeded".to_owned(), vec![range]),
413 });
414 }
415 self.mod_local.call_stack_size += 1;
416 Ok(())
417 }
418
419 pub(super) fn dec_call_stack_size(&mut self, range: SourceRange) -> Result<(), KclError> {
422 if self.mod_local.call_stack_size == 0 {
424 let message = "call stack size below zero".to_owned();
425 debug_assert!(false, "{message}");
426 return Err(KclError::new_internal(KclErrorDetails::new(message, vec![range])));
427 }
428 self.mod_local.call_stack_size -= 1;
429 Ok(())
430 }
431
432 pub(crate) fn sketch_mode(&self) -> bool {
437 self.mod_local.sketch_mode
438 && match &self.mod_local.path {
439 ModulePath::Main => true,
440 ModulePath::Local { .. } => true,
441 ModulePath::Std { .. } => false,
442 }
443 }
444
445 pub fn next_object_id(&mut self) -> ObjectId {
446 ObjectId(self.mod_local.artifacts.object_id_generator.next_id())
447 }
448
449 pub fn peek_object_id(&self) -> ObjectId {
450 ObjectId(self.mod_local.artifacts.object_id_generator.peek_id())
451 }
452
453 pub(crate) fn constraint_state(&self, sketch_block_id: ObjectId, key: &ConstraintKey) -> Option<ConstraintState> {
454 let map = self.mod_local.constraint_state.get(&sketch_block_id)?;
455 map.get(key).copied()
456 }
457
458 pub(crate) fn set_constraint_state(
459 &mut self,
460 sketch_block_id: ObjectId,
461 key: ConstraintKey,
462 state: ConstraintState,
463 ) {
464 let map = self.mod_local.constraint_state.entry(sketch_block_id).or_default();
465 map.insert(key, state);
466 }
467
468 #[cfg(feature = "artifact-graph")]
469 pub fn add_scene_object(&mut self, obj: Object, source_range: SourceRange) -> ObjectId {
470 let id = obj.id;
471 debug_assert!(
472 id.0 == self.mod_local.artifacts.scene_objects.len(),
473 "Adding scene object with ID {} but next ID is {}",
474 id.0,
475 self.mod_local.artifacts.scene_objects.len()
476 );
477 let artifact_id = obj.artifact_id;
478 self.mod_local.artifacts.scene_objects.push(obj);
479 self.mod_local.artifacts.source_range_to_object.insert(source_range, id);
480 self.mod_local
481 .artifacts
482 .artifact_id_to_scene_object
483 .insert(artifact_id, id);
484 id
485 }
486
487 #[cfg(feature = "artifact-graph")]
490 pub fn add_placeholder_scene_object(
491 &mut self,
492 id: ObjectId,
493 source_range: SourceRange,
494 node_path: Option<NodePath>,
495 ) -> ObjectId {
496 debug_assert!(id.0 == self.mod_local.artifacts.scene_objects.len());
497 self.mod_local
498 .artifacts
499 .scene_objects
500 .push(Object::placeholder(id, source_range, node_path));
501 self.mod_local.artifacts.source_range_to_object.insert(source_range, id);
502 id
503 }
504
505 #[cfg(feature = "artifact-graph")]
507 pub fn set_scene_object(&mut self, object: Object) {
508 let id = object.id;
509 let artifact_id = object.artifact_id;
510 self.mod_local.artifacts.scene_objects[id.0] = object;
511 self.mod_local
512 .artifacts
513 .artifact_id_to_scene_object
514 .insert(artifact_id, id);
515 }
516
517 #[cfg(feature = "artifact-graph")]
518 pub fn scene_object_id_by_artifact_id(&self, artifact_id: ArtifactId) -> Option<ObjectId> {
519 self.mod_local
520 .artifacts
521 .artifact_id_to_scene_object
522 .get(&artifact_id)
523 .cloned()
524 }
525
526 #[cfg(feature = "artifact-graph")]
527 pub fn segment_ids_edited_contains(&self, object_id: &ObjectId) -> bool {
528 self.global.segment_ids_edited.contains(object_id)
529 }
530
531 pub(super) fn is_in_sketch_block(&self) -> bool {
532 self.mod_local.sketch_block.is_some()
533 }
534
535 pub(crate) fn sketch_block_mut(&mut self) -> Option<&mut SketchBlockState> {
536 self.mod_local.sketch_block.as_mut()
537 }
538
539 pub(crate) fn sketch_block(&mut self) -> Option<&SketchBlockState> {
540 self.mod_local.sketch_block.as_ref()
541 }
542
543 pub fn next_uuid(&mut self) -> Uuid {
544 self.mod_local.id_generator.next_uuid()
545 }
546
547 #[cfg(feature = "artifact-graph")]
548 pub fn next_artifact_id(&mut self) -> ArtifactId {
549 self.mod_local.id_generator.next_artifact_id()
550 }
551
552 pub fn id_generator(&mut self) -> &mut IdGenerator {
553 &mut self.mod_local.id_generator
554 }
555
556 pub(crate) fn mark_solid_consumed(&mut self, consumed_id: Uuid, info: ConsumedSolidInfo) {
558 self.mod_local.consumed_solids.insert(consumed_id, info);
559 }
560
561 pub(crate) fn check_solid_consumed(&self, id: &Uuid) -> Option<&ConsumedSolidInfo> {
564 self.mod_local.consumed_solids.get(id)
565 }
566
567 pub(crate) fn latest_consumed_output(&self, output_solid_id: Option<Uuid>) -> Option<Uuid> {
570 let mut latest = output_solid_id?;
571 let mut seen = AhashIndexSet::default();
572
573 while seen.insert(latest) {
574 let Some(next) = self
575 .mod_local
576 .consumed_solids
577 .get(&latest)
578 .and_then(|info| info.output_solid_id)
579 else {
580 break;
581 };
582 latest = next;
583 }
584
585 Some(latest)
586 }
587
588 pub(crate) fn find_var_name_for_solid_id(&self, target_id: Uuid) -> Option<String> {
592 fn contains_solid_id(value: &KclValue, target_id: Uuid) -> bool {
593 match value {
594 KclValue::Solid { value } => value.id == target_id,
595 KclValue::HomArray { value, .. } => value.iter().any(|v| contains_solid_id(v, target_id)),
596 _ => false,
597 }
598 }
599 self.mod_local
600 .stack
601 .find_var_name_in_all_envs(|value| contains_solid_id(value, target_id))
602 }
603
604 #[cfg(feature = "artifact-graph")]
605 pub(crate) fn add_artifact(&mut self, artifact: Artifact) {
606 let id = artifact.id();
607 self.mod_local.artifacts.artifacts.insert(id, artifact);
608 }
609
610 #[cfg(feature = "artifact-graph")]
611 pub(crate) fn artifact_mut(&mut self, id: ArtifactId) -> Option<&mut Artifact> {
612 self.mod_local.artifacts.artifacts.get_mut(&id)
613 }
614
615 pub(crate) fn push_op(&mut self, op: Operation) {
616 #[cfg(feature = "artifact-graph")]
617 self.mod_local.artifacts.operations.push(op);
618 #[cfg(not(feature = "artifact-graph"))]
619 drop(op);
620 }
621
622 #[cfg(feature = "artifact-graph")]
623 pub(crate) fn push_command(&mut self, command: ArtifactCommand) {
624 self.mod_local.artifacts.unprocessed_commands.push(command);
625 #[cfg(not(feature = "artifact-graph"))]
626 drop(command);
627 }
628
629 pub(super) fn next_module_id(&self) -> ModuleId {
630 ModuleId::from_usize(self.global.path_to_source_id.len())
631 }
632
633 pub(super) fn id_for_module(&self, path: &ModulePath) -> Option<ModuleId> {
634 self.global.path_to_source_id.get(path).cloned()
635 }
636
637 pub(super) fn add_path_to_source_id(&mut self, path: ModulePath, id: ModuleId) {
638 debug_assert!(!self.global.path_to_source_id.contains_key(&path));
639 self.global.path_to_source_id.insert(path, id);
640 }
641
642 pub(crate) fn add_root_module_contents(&mut self, program: &crate::Program) {
643 let root_id = ModuleId::default();
644 let path = self
646 .global
647 .path_to_source_id
648 .iter()
649 .find(|(_, v)| **v == root_id)
650 .unwrap()
651 .0
652 .clone();
653 self.add_id_to_source(
654 root_id,
655 ModuleSource {
656 path,
657 source: program.original_file_contents.to_string(),
658 },
659 );
660 }
661
662 pub(super) fn add_id_to_source(&mut self, id: ModuleId, source: ModuleSource) {
663 self.global.id_to_source.insert(id, source);
664 }
665
666 pub(super) fn add_module(&mut self, id: ModuleId, path: ModulePath, repr: ModuleRepr) {
667 debug_assert!(self.global.path_to_source_id.contains_key(&path));
668 let module_info = ModuleInfo { id, repr, path };
669 self.global.module_infos.insert(id, module_info);
670 }
671
672 pub fn get_module(&mut self, id: ModuleId) -> Option<&ModuleInfo> {
673 self.global.module_infos.get(&id)
674 }
675
676 #[cfg(all(test, feature = "artifact-graph"))]
677 pub(crate) fn modules(&self) -> &ModuleInfoMap {
678 &self.global.module_infos
679 }
680
681 #[cfg(all(test, feature = "artifact-graph"))]
682 pub(crate) fn root_module_artifact_state(&self) -> &ModuleArtifactState {
683 &self.global.root_module_artifacts
684 }
685
686 pub fn current_default_units(&self) -> NumericType {
687 NumericType::Default {
688 len: self.length_unit(),
689 angle: self.angle_unit(),
690 }
691 }
692
693 pub fn length_unit(&self) -> UnitLength {
694 self.mod_local.settings.default_length_units
695 }
696
697 pub fn angle_unit(&self) -> UnitAngle {
698 self.mod_local.settings.default_angle_units
699 }
700
701 pub(super) fn circular_import_error(&self, path: &ModulePath, source_range: SourceRange) -> KclError {
702 KclError::new_import_cycle(KclErrorDetails::new(
703 format!(
704 "circular import of modules is not allowed: {} -> {}",
705 self.global
706 .mod_loader
707 .import_stack
708 .iter()
709 .map(|p| p.to_string_lossy())
710 .collect::<Vec<_>>()
711 .join(" -> "),
712 path,
713 ),
714 vec![source_range],
715 ))
716 }
717
718 pub(crate) fn pipe_value(&self) -> Option<&KclValue> {
719 self.mod_local.pipe_value.as_ref()
720 }
721
722 pub(crate) fn error_with_outputs(
723 &self,
724 error: KclError,
725 main_ref: Option<EnvironmentRef>,
726 default_planes: Option<DefaultPlanes>,
727 ) -> KclErrorWithOutputs {
728 let module_id_to_module_path: IndexMap<ModuleId, ModulePath> = self
729 .global
730 .path_to_source_id
731 .iter()
732 .map(|(k, v)| ((*v), k.clone()))
733 .collect();
734
735 KclErrorWithOutputs::new(
736 error,
737 self.issues().to_vec(),
738 main_ref
739 .map(|main_ref| self.mod_local.variables(main_ref))
740 .unwrap_or_default(),
741 #[cfg(feature = "artifact-graph")]
742 self.global.root_module_artifacts.operations.clone(),
743 #[cfg(feature = "artifact-graph")]
744 Default::default(),
745 #[cfg(feature = "artifact-graph")]
746 self.global.artifacts.graph.clone(),
747 #[cfg(feature = "artifact-graph")]
748 self.global.root_module_artifacts.scene_objects.clone(),
749 #[cfg(feature = "artifact-graph")]
750 self.global.root_module_artifacts.source_range_to_object.clone(),
751 #[cfg(feature = "artifact-graph")]
752 self.global.root_module_artifacts.var_solutions.clone(),
753 module_id_to_module_path,
754 self.global.id_to_source.clone(),
755 default_planes,
756 )
757 }
758
759 #[cfg(feature = "artifact-graph")]
760 pub(crate) fn build_program_lookup(
761 &self,
762 current: crate::parsing::ast::types::Node<crate::parsing::ast::types::Program>,
763 ) -> ProgramLookup {
764 ProgramLookup::new(current, self.global.module_infos.clone())
765 }
766
767 #[cfg(feature = "artifact-graph")]
768 pub(crate) async fn build_artifact_graph(
769 &mut self,
770 engine: &Arc<Box<dyn EngineManager>>,
771 program: NodeRef<'_, crate::parsing::ast::types::Program>,
772 ) -> Result<(), KclError> {
773 let mut new_commands = Vec::new();
774 let mut new_exec_artifacts = IndexMap::new();
775 for module in self.global.module_infos.values_mut() {
776 match &mut module.repr {
777 ModuleRepr::Kcl(_, Some(outcome)) => {
778 new_commands.extend(outcome.artifacts.process_commands());
779 new_exec_artifacts.extend(outcome.artifacts.artifacts.clone());
780 }
781 ModuleRepr::Foreign(_, Some((_, module_artifacts))) => {
782 new_commands.extend(module_artifacts.process_commands());
783 new_exec_artifacts.extend(module_artifacts.artifacts.clone());
784 }
785 ModuleRepr::Root | ModuleRepr::Kcl(_, None) | ModuleRepr::Foreign(_, None) | ModuleRepr::Dummy => {}
786 }
787 }
788 new_commands.extend(self.global.root_module_artifacts.process_commands());
791 new_exec_artifacts.extend(self.global.root_module_artifacts.artifacts.clone());
794 let new_responses = engine.take_responses().await;
795
796 for (id, exec_artifact) in new_exec_artifacts {
799 self.global.artifacts.artifacts.entry(id).or_insert(exec_artifact);
803 }
804
805 let initial_graph = self.global.artifacts.graph.clone();
806
807 let programs = self.build_program_lookup(program.clone());
809 let graph_result = crate::execution::artifact::build_artifact_graph(
810 &new_commands,
811 &new_responses,
812 program,
813 &mut self.global.artifacts.artifacts,
814 initial_graph,
815 &programs,
816 &self.global.module_infos,
817 );
818
819 #[cfg(feature = "snapshot-engine-responses")]
820 {
821 self.global.root_module_artifacts.responses.extend(new_responses);
823 }
824
825 let artifact_graph = graph_result?;
826 self.global.artifacts.graph = artifact_graph;
827
828 Ok(())
829 }
830
831 #[cfg(not(feature = "artifact-graph"))]
832 pub(crate) async fn build_artifact_graph(
833 &mut self,
834 _engine: &Arc<Box<dyn EngineManager>>,
835 _program: NodeRef<'_, crate::parsing::ast::types::Program>,
836 ) -> Result<(), KclError> {
837 Ok(())
838 }
839
840 pub(crate) fn kcl_version(&self) -> KclVersion {
841 self.mod_local.settings.kcl_version.parse().unwrap_or_default()
842 }
843}
844
845#[derive(Default)]
846pub enum KclVersion {
847 #[default]
848 V1,
849 V2,
850}
851
852impl FromStr for KclVersion {
853 type Err = KclError;
854
855 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
856 match s {
857 "1" | "1.0" | "1.0.0" => Ok(Self::V1),
858 "2" | "2.0" | "2.0.0" => Ok(Self::V2),
859 other => Err(KclError::new_semantic(KclErrorDetails {
860 source_ranges: Default::default(),
861 backtrace: Default::default(),
862 message: format!("Unrecognized version {other}. Valid versions are 1.0 and 2.0"),
863 })),
864 }
865 }
866}
867
868impl GlobalState {
869 fn new(settings: &ExecutorSettings, segment_ids_edited: AhashIndexSet<ObjectId>) -> Self {
870 #[cfg(not(feature = "artifact-graph"))]
871 drop(segment_ids_edited);
872 let mut global = GlobalState {
873 path_to_source_id: Default::default(),
874 module_infos: Default::default(),
875 artifacts: Default::default(),
876 root_module_artifacts: Default::default(),
877 mod_loader: Default::default(),
878 issues: Default::default(),
879 id_to_source: Default::default(),
880 #[cfg(feature = "artifact-graph")]
881 segment_ids_edited,
882 };
883
884 let root_id = ModuleId::default();
885 let root_path = settings.current_file.clone().unwrap_or_default();
886 global.module_infos.insert(
887 root_id,
888 ModuleInfo {
889 id: root_id,
890 path: ModulePath::Local {
891 value: root_path.clone(),
892 original_import_path: None,
893 },
894 repr: ModuleRepr::Root,
895 },
896 );
897 global.path_to_source_id.insert(
898 ModulePath::Local {
899 value: root_path,
900 original_import_path: None,
901 },
902 root_id,
903 );
904 global
905 }
906
907 pub(super) fn filenames(&self) -> IndexMap<ModuleId, ModulePath> {
908 self.path_to_source_id.iter().map(|(k, v)| ((*v), k.clone())).collect()
909 }
910
911 pub(super) fn get_source(&self, id: ModuleId) -> Option<&ModuleSource> {
912 self.id_to_source.get(&id)
913 }
914}
915
916impl ArtifactState {
917 #[cfg(feature = "artifact-graph")]
918 pub fn cached_body_items(&self) -> usize {
919 self.graph.item_count
920 }
921
922 pub(crate) fn clear(&mut self) {
923 #[cfg(feature = "artifact-graph")]
924 {
925 self.artifacts.clear();
926 self.graph.clear();
927 }
928 }
929}
930
931impl ModuleArtifactState {
932 pub(crate) fn clear(&mut self) {
933 #[cfg(feature = "artifact-graph")]
934 {
935 self.artifacts.clear();
936 self.unprocessed_commands.clear();
937 self.commands.clear();
938 self.operations.clear();
939 }
940 }
941
942 #[cfg(feature = "artifact-graph")]
943 pub(crate) fn restore_scene_objects(&mut self, scene_objects: &[Object]) {
944 self.scene_objects = scene_objects.to_vec();
945 self.object_id_generator = IncIdGenerator::new(self.scene_objects.len());
946 self.source_range_to_object.clear();
947 self.artifact_id_to_scene_object.clear();
948
949 for (expected_id, object) in self.scene_objects.iter().enumerate() {
950 debug_assert_eq!(
951 object.id.0, expected_id,
952 "Restored cached scene object ID {} does not match its position {}",
953 object.id.0, expected_id
954 );
955
956 match &object.source {
957 crate::front::SourceRef::Simple { range, node_path: _ } => {
958 self.source_range_to_object.insert(*range, object.id);
959 }
960 crate::front::SourceRef::BackTrace { ranges } => {
961 if let Some((range, _)) = ranges.first() {
964 self.source_range_to_object.insert(*range, object.id);
965 }
966 }
967 }
968
969 if object.artifact_id != ArtifactId::placeholder() {
971 self.artifact_id_to_scene_object.insert(object.artifact_id, object.id);
972 }
973 }
974 }
975
976 #[cfg(not(feature = "artifact-graph"))]
977 pub(crate) fn extend(&mut self, _other: ModuleArtifactState) {}
978
979 #[cfg(feature = "artifact-graph")]
981 pub(crate) fn extend(&mut self, other: ModuleArtifactState) {
982 self.artifacts.extend(other.artifacts);
983 self.unprocessed_commands.extend(other.unprocessed_commands);
984 self.commands.extend(other.commands);
985 self.operations.extend(other.operations);
986 if other.scene_objects.len() > self.scene_objects.len() {
987 self.scene_objects
988 .extend(other.scene_objects[self.scene_objects.len()..].iter().cloned());
989 }
990 self.source_range_to_object.extend(other.source_range_to_object);
991 self.artifact_id_to_scene_object
992 .extend(other.artifact_id_to_scene_object);
993 self.var_solutions.extend(other.var_solutions);
994 }
995
996 #[cfg(feature = "artifact-graph")]
1000 pub(crate) fn process_commands(&mut self) -> Vec<ArtifactCommand> {
1001 let unprocessed = std::mem::take(&mut self.unprocessed_commands);
1002 let new_module_commands = unprocessed.clone();
1003 self.commands.extend(unprocessed);
1004 new_module_commands
1005 }
1006
1007 #[cfg_attr(not(feature = "artifact-graph"), expect(dead_code))]
1008 pub(crate) fn scene_object_by_id(&self, id: ObjectId) -> Option<&Object> {
1009 #[cfg(feature = "artifact-graph")]
1010 {
1011 debug_assert!(
1012 id.0 < self.scene_objects.len(),
1013 "Requested object ID {} but only have {} objects",
1014 id.0,
1015 self.scene_objects.len()
1016 );
1017 self.scene_objects.get(id.0)
1018 }
1019 #[cfg(not(feature = "artifact-graph"))]
1020 {
1021 let _ = id;
1022 None
1023 }
1024 }
1025
1026 #[cfg_attr(not(feature = "artifact-graph"), expect(dead_code))]
1027 pub(crate) fn scene_object_by_id_mut(&mut self, id: ObjectId) -> Option<&mut Object> {
1028 #[cfg(feature = "artifact-graph")]
1029 {
1030 debug_assert!(
1031 id.0 < self.scene_objects.len(),
1032 "Requested object ID {} but only have {} objects",
1033 id.0,
1034 self.scene_objects.len()
1035 );
1036 self.scene_objects.get_mut(id.0)
1037 }
1038 #[cfg(not(feature = "artifact-graph"))]
1039 {
1040 let _ = id;
1041 None
1042 }
1043 }
1044}
1045
1046impl ModuleState {
1047 pub(super) fn new(
1048 path: ModulePath,
1049 memory: Arc<ProgramMemory>,
1050 module_id: Option<ModuleId>,
1051 sketch_mode: bool,
1052 freedom_analysis: bool,
1053 ) -> Self {
1054 ModuleState {
1055 id_generator: IdGenerator::new(module_id),
1056 stack: memory.new_stack(),
1057 call_stack_size: 0,
1058 pipe_value: Default::default(),
1059 being_declared: Default::default(),
1060 sketch_block: Default::default(),
1061 stdlib_entry_source_range: Default::default(),
1062 module_exports: Default::default(),
1063 explicit_length_units: false,
1064 path,
1065 settings: Default::default(),
1066 sketch_mode,
1067 freedom_analysis,
1068 artifacts: Default::default(),
1069 constraint_state: Default::default(),
1070 allowed_warnings: Vec::new(),
1071 denied_warnings: Vec::new(),
1072 consumed_solids: AHashMap::default(),
1073 inside_stdlib: false,
1074 }
1075 }
1076
1077 pub(super) fn variables(&self, main_ref: EnvironmentRef) -> IndexMap<String, KclValue> {
1078 self.stack
1079 .find_all_in_env(main_ref)
1080 .map(|(k, v)| (k.clone(), v.clone()))
1081 .collect()
1082 }
1083}
1084
1085impl SketchBlockState {
1086 pub(crate) fn next_sketch_var_id(&self) -> SketchVarId {
1087 SketchVarId(self.sketch_vars.len())
1088 }
1089
1090 #[cfg(feature = "artifact-graph")]
1093 pub(crate) fn var_solutions(
1094 &self,
1095 solve_outcome: &Solved,
1096 solution_ty: NumericType,
1097 range: SourceRange,
1098 ) -> Result<Vec<(SourceRange, Number)>, KclError> {
1099 self.sketch_vars
1100 .iter()
1101 .map(|v| {
1102 let Some(sketch_var) = v.as_sketch_var() else {
1103 return Err(KclError::new_internal(KclErrorDetails::new(
1104 "Expected sketch variable".to_owned(),
1105 vec![range],
1106 )));
1107 };
1108 let var_index = sketch_var.id.0;
1109 let solved_n = solve_outcome.final_values.get(var_index).ok_or_else(|| {
1110 let message = format!("No solution for sketch variable with id {}", var_index);
1111 debug_assert!(false, "{}", &message);
1112 KclError::new_internal(KclErrorDetails::new(
1113 message,
1114 sketch_var.meta.iter().map(|m| m.source_range).collect(),
1115 ))
1116 })?;
1117 let solved_value = Number {
1118 value: *solved_n,
1119 units: solution_ty.try_into().map_err(|_| {
1120 KclError::new_internal(KclErrorDetails::new(
1121 "Failed to convert numeric type to units".to_owned(),
1122 vec![range],
1123 ))
1124 })?,
1125 };
1126 let Some(source_range) = sketch_var.meta.first().map(|m| m.source_range) else {
1127 return Ok(None);
1128 };
1129 Ok(Some((source_range, solved_value)))
1130 })
1131 .filter_map(Result::transpose)
1132 .collect::<Result<Vec<_>, KclError>>()
1133 }
1134}
1135
1136#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS)]
1137#[ts(export)]
1138#[serde(rename_all = "camelCase")]
1139pub struct MetaSettings {
1140 pub default_length_units: UnitLength,
1141 pub default_angle_units: UnitAngle,
1142 pub experimental_features: annotations::WarningLevel,
1143 pub kcl_version: String,
1144}
1145
1146impl Default for MetaSettings {
1147 fn default() -> Self {
1148 MetaSettings {
1149 default_length_units: UnitLength::Millimeters,
1150 default_angle_units: UnitAngle::Degrees,
1151 experimental_features: annotations::WarningLevel::Deny,
1152 kcl_version: "1.0".to_owned(),
1153 }
1154 }
1155}
1156
1157impl MetaSettings {
1158 pub(crate) fn update_from_annotation(
1159 &mut self,
1160 annotation: &crate::parsing::ast::types::Node<Annotation>,
1161 ) -> Result<(bool, bool), KclError> {
1162 let properties = annotations::expect_properties(annotations::SETTINGS, annotation)?;
1163
1164 let mut updated_len = false;
1165 let mut updated_angle = false;
1166 for p in properties {
1167 match &*p.inner.key.name {
1168 annotations::SETTINGS_UNIT_LENGTH => {
1169 let value = annotations::expect_ident(&p.inner.value)?;
1170 let value = super::types::length_from_str(value, annotation.as_source_range())?;
1171 self.default_length_units = value;
1172 updated_len = true;
1173 }
1174 annotations::SETTINGS_UNIT_ANGLE => {
1175 let value = annotations::expect_ident(&p.inner.value)?;
1176 let value = super::types::angle_from_str(value, annotation.as_source_range())?;
1177 self.default_angle_units = value;
1178 updated_angle = true;
1179 }
1180 annotations::SETTINGS_VERSION => {
1181 let value = annotations::expect_number(&p.inner.value)?;
1182 self.kcl_version = value;
1183 }
1184 annotations::SETTINGS_EXPERIMENTAL_FEATURES => {
1185 let value = annotations::expect_ident(&p.inner.value)?;
1186 let value = annotations::WarningLevel::from_str(value).map_err(|_| {
1187 KclError::new_semantic(KclErrorDetails::new(
1188 format!(
1189 "Invalid value for {} settings property, expected one of: {}",
1190 annotations::SETTINGS_EXPERIMENTAL_FEATURES,
1191 annotations::WARN_LEVELS.join(", ")
1192 ),
1193 annotation.as_source_ranges(),
1194 ))
1195 })?;
1196 self.experimental_features = value;
1197 }
1198 name => {
1199 return Err(KclError::new_semantic(KclErrorDetails::new(
1200 format!(
1201 "Unexpected settings key: `{name}`; expected one of `{}`, `{}`",
1202 annotations::SETTINGS_UNIT_LENGTH,
1203 annotations::SETTINGS_UNIT_ANGLE
1204 ),
1205 vec![annotation.as_source_range()],
1206 )));
1207 }
1208 }
1209 }
1210
1211 Ok((updated_len, updated_angle))
1212 }
1213}
1214
1215#[cfg(all(feature = "artifact-graph", test))]
1216mod tests {
1217 use uuid::Uuid;
1218
1219 use super::ModuleArtifactState;
1220 use crate::NodePath;
1221 use crate::SourceRange;
1222 use crate::execution::ArtifactId;
1223 use crate::front::Object;
1224 use crate::front::ObjectId;
1225 use crate::front::ObjectKind;
1226 use crate::front::Plane;
1227 use crate::front::SourceRef;
1228
1229 #[test]
1230 fn restore_scene_objects_rebuilds_lookup_maps() {
1231 let plane_artifact_id = ArtifactId::new(Uuid::from_u128(1));
1232 let sketch_artifact_id = ArtifactId::new(Uuid::from_u128(2));
1233 let plane_range = SourceRange::from([1, 4, 0]);
1234 let plane_node_path = Some(NodePath::placeholder());
1235 let sketch_ranges = vec![
1236 (SourceRange::from([5, 9, 0]), None),
1237 (SourceRange::from([10, 12, 0]), None),
1238 ];
1239 let cached_objects = vec![
1240 Object {
1241 id: ObjectId(0),
1242 kind: ObjectKind::Plane(Plane::Object(ObjectId(0))),
1243 label: Default::default(),
1244 comments: Default::default(),
1245 artifact_id: plane_artifact_id,
1246 source: SourceRef::new(plane_range, plane_node_path),
1247 },
1248 Object {
1249 id: ObjectId(1),
1250 kind: ObjectKind::Nil,
1251 label: Default::default(),
1252 comments: Default::default(),
1253 artifact_id: sketch_artifact_id,
1254 source: SourceRef::BackTrace {
1255 ranges: sketch_ranges.clone(),
1256 },
1257 },
1258 Object::placeholder(ObjectId(2), SourceRange::from([13, 14, 0]), None),
1259 ];
1260
1261 let mut artifacts = ModuleArtifactState::default();
1262 artifacts.restore_scene_objects(&cached_objects);
1263
1264 assert_eq!(artifacts.scene_objects, cached_objects);
1265 assert_eq!(
1266 artifacts.artifact_id_to_scene_object.get(&plane_artifact_id),
1267 Some(&ObjectId(0))
1268 );
1269 assert_eq!(
1270 artifacts.artifact_id_to_scene_object.get(&sketch_artifact_id),
1271 Some(&ObjectId(1))
1272 );
1273 assert_eq!(
1274 artifacts.artifact_id_to_scene_object.get(&ArtifactId::placeholder()),
1275 None
1276 );
1277 assert_eq!(artifacts.source_range_to_object.get(&plane_range), Some(&ObjectId(0)));
1278 assert_eq!(
1279 artifacts.source_range_to_object.get(&sketch_ranges[0].0),
1280 Some(&ObjectId(1))
1281 );
1282 assert_eq!(artifacts.source_range_to_object.get(&sketch_ranges[1].0), None);
1284 }
1285}