From e3b20483a0774f05d362bd56776424a095201963 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 11 Nov 2024 18:00:02 +1300 Subject: [PATCH] Make most other modules private Signed-off-by: Nick Cameron --- src/wasm-lib/derive-docs/src/lib.rs | 3 +- .../derive-docs/tests/args_with_lifetime.gen | 11 +- .../derive-docs/tests/args_with_refs.gen | 11 +- src/wasm-lib/derive-docs/tests/array.gen | 20 +- src/wasm-lib/derive-docs/tests/box.gen | 12 +- .../tests/doc_comment_with_code.gen | 20 +- src/wasm-lib/derive-docs/tests/lineTo.gen | 20 +- src/wasm-lib/derive-docs/tests/min.gen | 19 +- src/wasm-lib/derive-docs/tests/option.gen | 12 +- .../derive-docs/tests/option_input_format.gen | 12 +- .../tests/return_vec_box_sketch.gen | 12 +- .../derive-docs/tests/return_vec_sketch.gen | 12 +- src/wasm-lib/derive-docs/tests/show.gen | 12 +- .../tests/test_args_with_exec_state.gen | 11 +- src/wasm-lib/kcl-test-server/src/lib.rs | 8 +- .../kcl-to-core/src/conn_mock_core.rs | 23 +- src/wasm-lib/kcl-to-core/src/lib.rs | 16 +- .../benches/compiler_benchmark_criterion.rs | 19 +- .../kcl/benches/compiler_benchmark_iai.rs | 32 +-- src/wasm-lib/kcl/benches/digest_benchmark.rs | 2 +- .../benches/executor_benchmark_criterion.rs | 2 +- .../kcl/benches/executor_benchmark_iai.rs | 4 +- ...lsp_semantic_tokens_benchmark_criterion.rs | 2 +- .../lsp_semantic_tokens_benchmark_iai.rs | 2 +- src/wasm-lib/kcl/src/coredump/mod.rs | 1 + src/wasm-lib/kcl/src/executor.rs | 135 +++++++--- src/wasm-lib/kcl/src/lib.rs | 84 ++++-- src/wasm-lib/kcl/src/lint/checks/mod.rs | 2 + src/wasm-lib/kcl/src/lint/mod.rs | 1 + src/wasm-lib/kcl/src/lsp/copilot/mod.rs | 34 +++ src/wasm-lib/kcl/src/lsp/kcl/mod.rs | 76 +++++- src/wasm-lib/kcl/src/parser.rs | 1 + src/wasm-lib/kcl/src/settings/types/mod.rs | 2 +- src/wasm-lib/kcl/src/std/kcl_stdlib.rs | 17 +- src/wasm-lib/kcl/src/std/mod.rs | 2 - src/wasm-lib/kcl/src/std/types.rs | 1 + src/wasm-lib/kcl/src/std/utils.rs | 2 + src/wasm-lib/kcl/src/test_server.rs | 7 +- src/wasm-lib/kcl/src/thread/mod.rs | 6 +- src/wasm-lib/src/wasm.rs | 250 ++++++------------ src/wasm-lib/tests/executor/main.rs | 2 +- src/wasm-lib/tests/executor/no_visuals.rs | 35 +-- src/wasm-lib/tests/executor/visuals.rs | 2 +- src/wasm-lib/tests/modify/main.rs | 9 +- 44 files changed, 493 insertions(+), 473 deletions(-) diff --git a/src/wasm-lib/derive-docs/src/lib.rs b/src/wasm-lib/derive-docs/src/lib.rs index 6c8de48bce..fad97b7477 100644 --- a/src/wasm-lib/derive-docs/src/lib.rs +++ b/src/wasm-lib/derive-docs/src/lib.rs @@ -749,7 +749,6 @@ fn generate_code_block_test(fn_name: &str, code_block: &str, index: usize) -> pr #[tokio::test(flavor = "multi_thread")] async fn #test_name_mock() { let program = crate::Program::parse(#code_block).unwrap(); - let id_generator = crate::executor::IdGenerator::default(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new(crate::engine::conn_mock::EngineConnection::new().await.unwrap())), fs: std::sync::Arc::new(crate::fs::FileManager::new()), @@ -758,7 +757,7 @@ fn generate_code_block_test(fn_name: &str, code_block: &str, index: usize) -> pr context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()).await.unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] diff --git a/src/wasm-lib/derive-docs/tests/args_with_lifetime.gen b/src/wasm-lib/derive-docs/tests/args_with_lifetime.gen index 9a3f6cef74..db1a2ba318 100644 --- a/src/wasm-lib/derive-docs/tests/args_with_lifetime.gen +++ b/src/wasm-lib/derive-docs/tests/args_with_lifetime.gen @@ -2,8 +2,7 @@ mod test_examples_someFn { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_someFn0() { - let program = crate::parser::top_level_parse("someFn()").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("someFn()").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -15,7 +14,9 @@ mod test_examples_someFn { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -111,10 +112,10 @@ impl crate::docs::StdLibFn for SomeFn { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/derive-docs/tests/args_with_refs.gen b/src/wasm-lib/derive-docs/tests/args_with_refs.gen index 7a7f70e34f..fd7b59cff0 100644 --- a/src/wasm-lib/derive-docs/tests/args_with_refs.gen +++ b/src/wasm-lib/derive-docs/tests/args_with_refs.gen @@ -2,8 +2,7 @@ mod test_examples_someFn { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_someFn0() { - let program = crate::parser::top_level_parse("someFn()").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("someFn()").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -15,7 +14,9 @@ mod test_examples_someFn { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -111,10 +112,10 @@ impl crate::docs::StdLibFn for SomeFn { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/derive-docs/tests/array.gen b/src/wasm-lib/derive-docs/tests/array.gen index 2f6069e55d..e71f766462 100644 --- a/src/wasm-lib/derive-docs/tests/array.gen +++ b/src/wasm-lib/derive-docs/tests/array.gen @@ -3,9 +3,7 @@ mod test_examples_show { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_show0() { let program = - crate::parser::top_level_parse("This is another code block.\nyes sirrr.\nshow") - .unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + crate::Program::parse("This is another code block.\nyes sirrr.\nshow").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -17,7 +15,9 @@ mod test_examples_show { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -36,9 +36,7 @@ mod test_examples_show { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_show1() { - let program = - crate::parser::top_level_parse("This is code.\nIt does other shit.\nshow").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("This is code.\nIt does other shit.\nshow").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -50,7 +48,9 @@ mod test_examples_show { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -149,10 +149,10 @@ impl crate::docs::StdLibFn for Show { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/derive-docs/tests/box.gen b/src/wasm-lib/derive-docs/tests/box.gen index 6ff618161f..e9c1abc08a 100644 --- a/src/wasm-lib/derive-docs/tests/box.gen +++ b/src/wasm-lib/derive-docs/tests/box.gen @@ -2,9 +2,7 @@ mod test_examples_show { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_show0() { - let program = - crate::parser::top_level_parse("This is code.\nIt does other shit.\nshow").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("This is code.\nIt does other shit.\nshow").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -16,7 +14,9 @@ mod test_examples_show { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -112,10 +112,10 @@ impl crate::docs::StdLibFn for Show { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/derive-docs/tests/doc_comment_with_code.gen b/src/wasm-lib/derive-docs/tests/doc_comment_with_code.gen index 3c5d416cf0..b734cd912b 100644 --- a/src/wasm-lib/derive-docs/tests/doc_comment_with_code.gen +++ b/src/wasm-lib/derive-docs/tests/doc_comment_with_code.gen @@ -3,9 +3,7 @@ mod test_examples_my_func { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_my_func0() { let program = - crate::parser::top_level_parse("This is another code block.\nyes sirrr.\nmyFunc") - .unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + crate::Program::parse("This is another code block.\nyes sirrr.\nmyFunc").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -17,7 +15,9 @@ mod test_examples_my_func { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -36,9 +36,7 @@ mod test_examples_my_func { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_my_func1() { - let program = - crate::parser::top_level_parse("This is code.\nIt does other shit.\nmyFunc").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("This is code.\nIt does other shit.\nmyFunc").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -50,7 +48,9 @@ mod test_examples_my_func { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -149,10 +149,10 @@ impl crate::docs::StdLibFn for MyFunc { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/derive-docs/tests/lineTo.gen b/src/wasm-lib/derive-docs/tests/lineTo.gen index 013b722f2c..e3b8118486 100644 --- a/src/wasm-lib/derive-docs/tests/lineTo.gen +++ b/src/wasm-lib/derive-docs/tests/lineTo.gen @@ -3,9 +3,7 @@ mod test_examples_line_to { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_line_to0() { let program = - crate::parser::top_level_parse("This is another code block.\nyes sirrr.\nlineTo") - .unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + crate::Program::parse("This is another code block.\nyes sirrr.\nlineTo").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -17,7 +15,9 @@ mod test_examples_line_to { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -36,9 +36,7 @@ mod test_examples_line_to { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_line_to1() { - let program = - crate::parser::top_level_parse("This is code.\nIt does other shit.\nlineTo").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("This is code.\nIt does other shit.\nlineTo").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -50,7 +48,9 @@ mod test_examples_line_to { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -157,10 +157,10 @@ impl crate::docs::StdLibFn for LineTo { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/derive-docs/tests/min.gen b/src/wasm-lib/derive-docs/tests/min.gen index 076679a873..09b67fd92d 100644 --- a/src/wasm-lib/derive-docs/tests/min.gen +++ b/src/wasm-lib/derive-docs/tests/min.gen @@ -3,8 +3,7 @@ mod test_examples_min { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_min0() { let program = - crate::parser::top_level_parse("This is another code block.\nyes sirrr.\nmin").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + crate::Program::parse("This is another code block.\nyes sirrr.\nmin").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -16,7 +15,9 @@ mod test_examples_min { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -35,9 +36,7 @@ mod test_examples_min { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_min1() { - let program = - crate::parser::top_level_parse("This is code.\nIt does other shit.\nmin").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("This is code.\nIt does other shit.\nmin").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -49,7 +48,9 @@ mod test_examples_min { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -148,10 +149,10 @@ impl crate::docs::StdLibFn for Min { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/derive-docs/tests/option.gen b/src/wasm-lib/derive-docs/tests/option.gen index 23208fda81..4722dd0552 100644 --- a/src/wasm-lib/derive-docs/tests/option.gen +++ b/src/wasm-lib/derive-docs/tests/option.gen @@ -2,9 +2,7 @@ mod test_examples_show { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_show0() { - let program = - crate::parser::top_level_parse("This is code.\nIt does other shit.\nshow").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("This is code.\nIt does other shit.\nshow").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -16,7 +14,9 @@ mod test_examples_show { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -112,10 +112,10 @@ impl crate::docs::StdLibFn for Show { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/derive-docs/tests/option_input_format.gen b/src/wasm-lib/derive-docs/tests/option_input_format.gen index 5ae970f03e..5ef7c07e2b 100644 --- a/src/wasm-lib/derive-docs/tests/option_input_format.gen +++ b/src/wasm-lib/derive-docs/tests/option_input_format.gen @@ -2,9 +2,7 @@ mod test_examples_import { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_import0() { - let program = - crate::parser::top_level_parse("This is code.\nIt does other shit.\nimport").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("This is code.\nIt does other shit.\nimport").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -16,7 +14,9 @@ mod test_examples_import { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -112,10 +112,10 @@ impl crate::docs::StdLibFn for Import { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/derive-docs/tests/return_vec_box_sketch.gen b/src/wasm-lib/derive-docs/tests/return_vec_box_sketch.gen index ce8838efeb..508d714b8e 100644 --- a/src/wasm-lib/derive-docs/tests/return_vec_box_sketch.gen +++ b/src/wasm-lib/derive-docs/tests/return_vec_box_sketch.gen @@ -2,9 +2,7 @@ mod test_examples_import { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_import0() { - let program = - crate::parser::top_level_parse("This is code.\nIt does other shit.\nimport").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("This is code.\nIt does other shit.\nimport").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -16,7 +14,9 @@ mod test_examples_import { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -112,10 +112,10 @@ impl crate::docs::StdLibFn for Import { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/derive-docs/tests/return_vec_sketch.gen b/src/wasm-lib/derive-docs/tests/return_vec_sketch.gen index c7075331a8..d7bb1dfa54 100644 --- a/src/wasm-lib/derive-docs/tests/return_vec_sketch.gen +++ b/src/wasm-lib/derive-docs/tests/return_vec_sketch.gen @@ -2,9 +2,7 @@ mod test_examples_import { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_import0() { - let program = - crate::parser::top_level_parse("This is code.\nIt does other shit.\nimport").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("This is code.\nIt does other shit.\nimport").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -16,7 +14,9 @@ mod test_examples_import { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -112,10 +112,10 @@ impl crate::docs::StdLibFn for Import { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/derive-docs/tests/show.gen b/src/wasm-lib/derive-docs/tests/show.gen index 600f581a28..e966c4a824 100644 --- a/src/wasm-lib/derive-docs/tests/show.gen +++ b/src/wasm-lib/derive-docs/tests/show.gen @@ -2,9 +2,7 @@ mod test_examples_show { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_show0() { - let program = - crate::parser::top_level_parse("This is code.\nIt does other shit.\nshow").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("This is code.\nIt does other shit.\nshow").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -16,7 +14,9 @@ mod test_examples_show { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -112,10 +112,10 @@ impl crate::docs::StdLibFn for Show { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/derive-docs/tests/test_args_with_exec_state.gen b/src/wasm-lib/derive-docs/tests/test_args_with_exec_state.gen index 8e4661bbe1..0c400e5c64 100644 --- a/src/wasm-lib/derive-docs/tests/test_args_with_exec_state.gen +++ b/src/wasm-lib/derive-docs/tests/test_args_with_exec_state.gen @@ -2,8 +2,7 @@ mod test_examples_some_function { #[tokio::test(flavor = "multi_thread")] async fn test_mock_example_some_function0() { - let program = crate::parser::top_level_parse("someFunction()").unwrap(); - let id_generator = crate::executor::IdGenerator::default(); + let program = crate::Program::parse("someFunction()").unwrap(); let ctx = crate::executor::ExecutorContext { engine: std::sync::Arc::new(Box::new( crate::engine::conn_mock::EngineConnection::new() @@ -15,7 +14,9 @@ mod test_examples_some_function { settings: Default::default(), context_type: crate::executor::ContextType::Mock, }; - ctx.run(&program, None, id_generator, None).await.unwrap(); + ctx.run(&program, &mut crate::ExecState::default()) + .await + .unwrap(); } #[tokio::test(flavor = "multi_thread", worker_threads = 5)] @@ -106,10 +107,10 @@ impl crate::docs::StdLibFn for SomeFunction { code_blocks .iter() .map(|cb| { - let program = crate::parser::top_level_parse(cb).unwrap(); + let program = crate::Program::parse(cb).unwrap(); let mut options: crate::ast::types::FormatOptions = Default::default(); options.insert_final_newline = false; - program.recast(&options, 0) + program.ast.recast(&options, 0) }) .collect::>() } diff --git a/src/wasm-lib/kcl-test-server/src/lib.rs b/src/wasm-lib/kcl-test-server/src/lib.rs index c6e41156e3..7bdf2d4e54 100644 --- a/src/wasm-lib/kcl-test-server/src/lib.rs +++ b/src/wasm-lib/kcl-test-server/src/lib.rs @@ -15,7 +15,7 @@ use hyper::{ service::{make_service_fn, service_fn}, Body, Error, Response, Server, }; -use kcl_lib::{executor::ExecutorContext, settings::types::UnitLength, test_server::RequestBody, Program}; +use kcl_lib::{test_server::RequestBody, ExecState, ExecutorContext, Program, UnitLength}; use tokio::{ sync::{mpsc, oneshot}, task::JoinHandle, @@ -164,11 +164,11 @@ async fn snapshot_endpoint(body: Bytes, state: ExecutorContext) -> Response Response(); let timer = time_until(done_rx); - let snapshot = match state.execute_and_prepare_snapshot(&program, id_generator, None).await { + let snapshot = match state.execute_and_prepare_snapshot(&program, &mut exec_state).await { Ok(sn) => sn, Err(e) => return kcl_err(e), }; diff --git a/src/wasm-lib/kcl-to-core/src/conn_mock_core.rs b/src/wasm-lib/kcl-to-core/src/conn_mock_core.rs index 4d578dd5f9..c3dea7144d 100644 --- a/src/wasm-lib/kcl-to-core/src/conn_mock_core.rs +++ b/src/wasm-lib/kcl-to-core/src/conn_mock_core.rs @@ -1,9 +1,8 @@ use anyhow::Result; use indexmap::IndexMap; use kcl_lib::{ - engine::ExecutionKind, - errors::KclError, - executor::{DefaultPlanes, IdGenerator}, + exec::{DefaultPlanes, IdGenerator}, + ExecutionKind, KclError, }; use kittycad_modeling_cmds::{ self as kcmc, @@ -23,8 +22,8 @@ const NEED_PLANES: bool = true; #[derive(Debug, Clone)] pub struct EngineConnection { - batch: Arc>>, - batch_end: Arc>>, + batch: Arc>>, + batch_end: Arc>>, core_test: Arc>, default_planes: Arc>>, execution_kind: Arc>, @@ -354,12 +353,12 @@ fn codegen_cpp_repl_uuid_setters(reps_id: &str, entity_ids: &[uuid::Uuid]) -> St } #[async_trait::async_trait] -impl kcl_lib::engine::EngineManager for EngineConnection { - fn batch(&self) -> Arc>> { +impl kcl_lib::EngineManager for EngineConnection { + fn batch(&self) -> Arc>> { self.batch.clone() } - fn batch_end(&self) -> Arc>> { + fn batch_end(&self) -> Arc>> { self.batch_end.clone() } @@ -378,7 +377,7 @@ impl kcl_lib::engine::EngineManager for EngineConnection { async fn default_planes( &self, id_generator: &mut IdGenerator, - source_range: kcl_lib::executor::SourceRange, + source_range: kcl_lib::SourceRange, ) -> Result { if NEED_PLANES { { @@ -400,7 +399,7 @@ impl kcl_lib::engine::EngineManager for EngineConnection { async fn clear_scene_post_hook( &self, _id_generator: &mut IdGenerator, - _source_range: kcl_lib::executor::SourceRange, + _source_range: kcl_lib::SourceRange, ) -> Result<(), KclError> { Ok(()) } @@ -408,9 +407,9 @@ impl kcl_lib::engine::EngineManager for EngineConnection { async fn inner_send_modeling_cmd( &self, id: uuid::Uuid, - _source_range: kcl_lib::executor::SourceRange, + _source_range: kcl_lib::SourceRange, cmd: WebSocketRequest, - _id_to_source_range: std::collections::HashMap, + _id_to_source_range: std::collections::HashMap, ) -> Result { match cmd { WebSocketRequest::ModelingCmdBatchReq(ModelingBatch { diff --git a/src/wasm-lib/kcl-to-core/src/lib.rs b/src/wasm-lib/kcl-to-core/src/lib.rs index 24ae9d69c0..94aeff4bdd 100644 --- a/src/wasm-lib/kcl-to-core/src/lib.rs +++ b/src/wasm-lib/kcl-to-core/src/lib.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use kcl_lib::executor::{ExecutorContext, IdGenerator}; +use kcl_lib::{ExecState, ExecutorContext}; use std::sync::{Arc, Mutex}; #[cfg(not(target_arch = "wasm32"))] @@ -12,16 +12,10 @@ pub async fn kcl_to_engine_core(code: &str) -> Result { let result = Arc::new(Mutex::new("".into())); let ref_result = Arc::clone(&result); - let ctx = ExecutorContext { - engine: Arc::new(Box::new( - crate::conn_mock_core::EngineConnection::new(ref_result).await?, - )), - fs: Arc::new(kcl_lib::fs::FileManager::new()), - stdlib: Arc::new(kcl_lib::std::StdLib::new()), - settings: Default::default(), - context_type: kcl_lib::executor::ContextType::MockCustomForwarded, - }; - let _memory = ctx.run(&program, None, IdGenerator::default(), None).await?; + let ctx = ExecutorContext::new_forwarded_mock(Arc::new(Box::new( + crate::conn_mock_core::EngineConnection::new(ref_result).await?, + ))); + ctx.run(&program, &mut ExecState::default()).await?; let result = result.lock().expect("mutex lock").clone(); Ok(result) diff --git a/src/wasm-lib/kcl/benches/compiler_benchmark_criterion.rs b/src/wasm-lib/kcl/benches/compiler_benchmark_criterion.rs index 92e8628e24..469d696b84 100644 --- a/src/wasm-lib/kcl/benches/compiler_benchmark_criterion.rs +++ b/src/wasm-lib/kcl/benches/compiler_benchmark_criterion.rs @@ -1,12 +1,5 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; -pub fn bench_lex(c: &mut Criterion) { - let module_id = kcl_lib::ModuleId::default(); - c.bench_function("lex_cube", |b| b.iter(|| lex(CUBE_PROGRAM, module_id))); - c.bench_function("lex_big_kitt", |b| b.iter(|| lex(KITT_PROGRAM, module_id))); - c.bench_function("lex_pipes_on_pipes", |b| b.iter(|| lex(PIPES_PROGRAM, module_id))); -} - pub fn bench_parse(c: &mut Criterion) { for (name, file) in [ ("pipes_on_pipes", PIPES_PROGRAM), @@ -16,23 +9,15 @@ pub fn bench_parse(c: &mut Criterion) { ("mike_stress_test", MIKE_STRESS_TEST_PROGRAM), ("koch snowflake", LSYSTEM_KOCH_SNOWFLAKE_PROGRAM), ] { - let module_id = kcl_lib::ModuleId::default(); - let tokens = kcl_lib::token::lexer(file, module_id).unwrap(); c.bench_function(&format!("parse_{name}"), move |b| { - let tok = tokens.clone(); b.iter(move || { - let parser = kcl_lib::parser::Parser::new(tok.clone()); - black_box(parser.ast().unwrap()); + black_box(kcl_lib::Program::parse(file).unwrap()); }) }); } } -fn lex(program: &str, module_id: kcl_lib::ModuleId) { - black_box(kcl_lib::token::lexer(program, module_id).unwrap()); -} - -criterion_group!(benches, bench_lex, bench_parse); +criterion_group!(benches, bench_parse); criterion_main!(benches); const KITT_PROGRAM: &str = include_str!("../../tests/executor/inputs/kittycad_svg.kcl"); diff --git a/src/wasm-lib/kcl/benches/compiler_benchmark_iai.rs b/src/wasm-lib/kcl/benches/compiler_benchmark_iai.rs index 1790fcc541..a57e321377 100644 --- a/src/wasm-lib/kcl/benches/compiler_benchmark_iai.rs +++ b/src/wasm-lib/kcl/benches/compiler_benchmark_iai.rs @@ -1,32 +1,7 @@ use iai::black_box; pub fn parse(program: &str) { - let module_id = kcl_lib::ModuleId::default(); - let tokens = kcl_lib::token::lexer(program, module_id).unwrap(); - let tok = tokens.clone(); - let parser = kcl_lib::parser::Parser::new(tok.clone()); - black_box(parser.ast().unwrap()); -} - -fn lex_kitt() { - let module_id = kcl_lib::ModuleId::default(); - black_box(kcl_lib::token::lexer(KITT_PROGRAM, module_id).unwrap()); -} -fn lex_pipes() { - let module_id = kcl_lib::ModuleId::default(); - black_box(kcl_lib::token::lexer(PIPES_PROGRAM, module_id).unwrap()); -} -fn lex_cube() { - let module_id = kcl_lib::ModuleId::default(); - black_box(kcl_lib::token::lexer(CUBE_PROGRAM, module_id).unwrap()); -} -fn lex_math() { - let module_id = kcl_lib::ModuleId::default(); - black_box(kcl_lib::token::lexer(MATH_PROGRAM, module_id).unwrap()); -} -fn lex_lsystem() { - let module_id = kcl_lib::ModuleId::default(); - black_box(kcl_lib::token::lexer(LSYSTEM_PROGRAM, module_id).unwrap()); + black_box(kcl_lib::Program::parse(program).unwrap()); } fn parse_kitt() { @@ -46,11 +21,6 @@ fn parse_lsystem() { } iai::main! { - lex_kitt, - lex_pipes, - lex_cube, - lex_math, - lex_lsystem, parse_kitt, parse_pipes, parse_cube, diff --git a/src/wasm-lib/kcl/benches/digest_benchmark.rs b/src/wasm-lib/kcl/benches/digest_benchmark.rs index 33bdbd14a1..a10acdcca8 100644 --- a/src/wasm-lib/kcl/benches/digest_benchmark.rs +++ b/src/wasm-lib/kcl/benches/digest_benchmark.rs @@ -9,7 +9,7 @@ pub fn bench_digest(c: &mut Criterion) { ("mike_stress_test", MIKE_STRESS_TEST_PROGRAM), ("lsystem", LSYSTEM_PROGRAM), ] { - let prog = kcl_lib::parser::top_level_parse(file).unwrap(); + let prog = kcl_lib::Program::parse(file).unwrap(); c.bench_function(&format!("digest_{name}"), move |b| { let prog = prog.clone(); diff --git a/src/wasm-lib/kcl/benches/executor_benchmark_criterion.rs b/src/wasm-lib/kcl/benches/executor_benchmark_criterion.rs index d2a48a1144..42ed4054bc 100644 --- a/src/wasm-lib/kcl/benches/executor_benchmark_criterion.rs +++ b/src/wasm-lib/kcl/benches/executor_benchmark_criterion.rs @@ -1,5 +1,5 @@ use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; -use kcl_lib::{settings::types::UnitLength::Mm, test_server}; +use kcl_lib::{test_server, UnitLength::Mm}; use tokio::runtime::Runtime; pub fn bench_execute(c: &mut Criterion) { diff --git a/src/wasm-lib/kcl/benches/executor_benchmark_iai.rs b/src/wasm-lib/kcl/benches/executor_benchmark_iai.rs index f7448be529..ad4829b4e5 100644 --- a/src/wasm-lib/kcl/benches/executor_benchmark_iai.rs +++ b/src/wasm-lib/kcl/benches/executor_benchmark_iai.rs @@ -3,7 +3,7 @@ use iai::black_box; async fn execute_server_rack_heavy() { let code = SERVER_RACK_HEAVY_PROGRAM; black_box( - kcl_lib::test_server::execute_and_snapshot(code, kcl_lib::settings::types::UnitLength::Mm) + kcl_lib::test_server::execute_and_snapshot(code, kcl_lib::UnitLength::Mm) .await .unwrap(), ); @@ -12,7 +12,7 @@ async fn execute_server_rack_heavy() { async fn execute_server_rack_lite() { let code = SERVER_RACK_LITE_PROGRAM; black_box( - kcl_lib::test_server::execute_and_snapshot(code, kcl_lib::settings::types::UnitLength::Mm) + kcl_lib::test_server::execute_and_snapshot(code, kcl_lib::UnitLength::Mm) .await .unwrap(), ); diff --git a/src/wasm-lib/kcl/benches/lsp_semantic_tokens_benchmark_criterion.rs b/src/wasm-lib/kcl/benches/lsp_semantic_tokens_benchmark_criterion.rs index d74b95c050..abc0f26f2a 100644 --- a/src/wasm-lib/kcl/benches/lsp_semantic_tokens_benchmark_criterion.rs +++ b/src/wasm-lib/kcl/benches/lsp_semantic_tokens_benchmark_criterion.rs @@ -1,5 +1,5 @@ use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; -use kcl_lib::lsp::test_util::kcl_lsp_server; +use kcl_lib::kcl_lsp_server; use tokio::runtime::Runtime; use tower_lsp::LanguageServer; diff --git a/src/wasm-lib/kcl/benches/lsp_semantic_tokens_benchmark_iai.rs b/src/wasm-lib/kcl/benches/lsp_semantic_tokens_benchmark_iai.rs index d1621845fc..5b71bda94e 100644 --- a/src/wasm-lib/kcl/benches/lsp_semantic_tokens_benchmark_iai.rs +++ b/src/wasm-lib/kcl/benches/lsp_semantic_tokens_benchmark_iai.rs @@ -1,5 +1,5 @@ use iai::black_box; -use kcl_lib::lsp::test_util::kcl_lsp_server; +use kcl_lib::kcl_lsp_server; use tower_lsp::LanguageServer; async fn kcl_lsp_semantic_tokens(code: &str) { diff --git a/src/wasm-lib/kcl/src/coredump/mod.rs b/src/wasm-lib/kcl/src/coredump/mod.rs index 9e84afc524..324cf1fe53 100644 --- a/src/wasm-lib/kcl/src/coredump/mod.rs +++ b/src/wasm-lib/kcl/src/coredump/mod.rs @@ -1,4 +1,5 @@ //! Core dump related structures and functions. +#![allow(dead_code)] #[cfg(not(target_arch = "wasm32"))] pub mod local; diff --git a/src/wasm-lib/kcl/src/executor.rs b/src/wasm-lib/kcl/src/executor.rs index 71d55b5044..770191cfd1 100644 --- a/src/wasm-lib/kcl/src/executor.rs +++ b/src/wasm-lib/kcl/src/executor.rs @@ -154,6 +154,7 @@ impl ProgramMemory { /// Find all solids in the memory that are on a specific sketch id. /// This does not look inside closures. But as long as we do not allow /// mutation of variables in KCL, closure memory should be a subset of this. + #[allow(clippy::vec_box)] pub fn find_solids_on_sketch(&self, sketch_id: uuid::Uuid) -> Vec> { self.environments .iter() @@ -496,6 +497,7 @@ impl Geometry { #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[ts(export)] #[serde(tag = "type")] +#[allow(clippy::vec_box)] pub enum Geometries { Sketches(Vec>), Solids(Vec>), @@ -514,6 +516,7 @@ impl From for Geometries { #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[ts(export)] #[serde(tag = "type", rename_all = "camelCase")] +#[allow(clippy::vec_box)] pub enum SketchSet { Sketch(Box), Sketches(Vec>), @@ -594,6 +597,7 @@ impl From> for Vec> { #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[ts(export)] #[serde(tag = "type", rename_all = "camelCase")] +#[allow(clippy::vec_box)] pub enum SolidSet { Solid(Box), Solids(Vec>), @@ -2116,6 +2120,70 @@ impl ExecutorContext { }) } + #[cfg(not(target_arch = "wasm32"))] + pub async fn new_mock() -> Self { + ExecutorContext { + engine: Arc::new(Box::new( + crate::engine::conn_mock::EngineConnection::new().await.unwrap(), + )), + fs: Arc::new(FileManager::new()), + stdlib: Arc::new(StdLib::new()), + settings: Default::default(), + context_type: ContextType::Mock, + } + } + + #[cfg(target_arch = "wasm32")] + pub async fn new( + engine_manager: crate::engine::conn_wasm::EngineCommandManager, + fs_manager: crate::fs::wasm::FileSystemManager, + units: UnitLength, + ) -> Result { + Ok(ExecutorContext { + engine: Arc::new(Box::new( + crate::engine::conn_wasm::EngineConnection::new(engine_manager) + .await + .map_err(|e| format!("{:?}", e))?, + )), + fs: Arc::new(FileManager::new(fs_manager)), + stdlib: Arc::new(StdLib::new()), + settings: ExecutorSettings { + units, + ..Default::default() + }, + context_type: ContextType::Live, + }) + } + + #[cfg(target_arch = "wasm32")] + pub async fn new_mock(fs_manager: crate::fs::wasm::FileSystemManager, units: UnitLength) -> Result { + Ok(ExecutorContext { + engine: Arc::new(Box::new( + crate::engine::conn_mock::EngineConnection::new() + .await + .map_err(|e| format!("{:?}", e))?, + )), + fs: Arc::new(FileManager::new(fs_manager)), + stdlib: Arc::new(StdLib::new()), + settings: ExecutorSettings { + units, + ..Default::default() + }, + context_type: ContextType::Mock, + }) + } + + #[cfg(not(target_arch = "wasm32"))] + pub fn new_forwarded_mock(engine: Arc>) -> Self { + ExecutorContext { + engine, + fs: Arc::new(FileManager::new()), + stdlib: Arc::new(StdLib::new()), + settings: Default::default(), + context_type: ContextType::MockCustomForwarded, + } + } + /// Create a new default executor context. /// With a kittycad client. /// This allows for passing in `ZOO_API_TOKEN` and `ZOO_HOST` as environment @@ -2169,26 +2237,21 @@ impl ExecutorContext { pub async fn reset_scene( &self, - id_generator: &mut IdGenerator, + exec_state: &mut ExecState, source_range: crate::executor::SourceRange, ) -> Result<()> { - self.engine.clear_scene(id_generator, source_range).await?; + self.engine + .clear_scene(&mut exec_state.id_generator, source_range) + .await?; Ok(()) } /// Perform the execution of a program. /// You can optionally pass in some initialization memory. /// Kurt uses this for partial execution. - pub async fn run( - &self, - program: &Program, - memory: Option, - id_generator: IdGenerator, - project_directory: Option, - ) -> Result { - self.run_with_session_data(program, memory, id_generator, project_directory) - .await - .map(|x| x.0) + pub async fn run(&self, program: &Program, exec_state: &mut ExecState) -> Result<(), KclError> { + self.run_with_session_data(program, exec_state).await?; + Ok(()) } /// Perform the execution of a program. /// You can optionally pass in some initialization memory. @@ -2196,21 +2259,14 @@ impl ExecutorContext { pub async fn run_with_session_data( &self, program: &Program, - memory: Option, - id_generator: IdGenerator, - project_directory: Option, - ) -> Result<(ExecState, Option), KclError> { - let memory = if let Some(memory) = memory { - memory.clone() - } else { - Default::default() - }; - let mut exec_state = ExecState { - memory, - id_generator, - project_directory, - ..Default::default() - }; + exec_state: &mut ExecState, + ) -> Result, KclError> { + // let mut exec_state = ExecState { + // memory, + // id_generator, + // project_directory, + // ..Default::default() + // }; // TODO: Use the top-level file's path. exec_state.add_module(std::path::PathBuf::from("")); // Before we even start executing the program, set the units. @@ -2231,10 +2287,10 @@ impl ExecutorContext { ) .await?; - self.inner_execute(&program.ast, &mut exec_state, crate::executor::BodyType::Root) + self.inner_execute(&program.ast, exec_state, crate::executor::BodyType::Root) .await?; let session_data = self.engine.get_session_data(); - Ok((exec_state, session_data)) + Ok(session_data) } /// Execute an AST's program. @@ -2486,22 +2542,14 @@ impl ExecutorContext { pub async fn execute_and_prepare_snapshot( &self, program: &Program, - id_generator: IdGenerator, - project_directory: Option, + exec_state: &mut ExecState, ) -> Result { - self.execute_and_prepare(program, id_generator, project_directory) - .await - .map(|(_state, snap)| snap) + self.execute_and_prepare(program, exec_state).await } /// Execute the program, return the interpreter and outputs. - pub async fn execute_and_prepare( - &self, - program: &Program, - id_generator: IdGenerator, - project_directory: Option, - ) -> Result<(ExecState, TakeSnapshot)> { - let state = self.run(program, None, id_generator, project_directory).await?; + pub async fn execute_and_prepare(&self, program: &Program, exec_state: &mut ExecState) -> Result { + self.run(program, exec_state).await?; // Zoom to fit. self.engine @@ -2534,7 +2582,7 @@ impl ExecutorContext { else { anyhow::bail!("Unexpected response from engine: {:?}", resp); }; - Ok((state, contents)) + Ok(contents) } } @@ -2650,7 +2698,8 @@ mod tests { settings: Default::default(), context_type: ContextType::Mock, }; - let exec_state = ctx.run(&program, None, IdGenerator::default(), None).await?; + let mut exec_state = ExecState::default(); + ctx.run(&program, &mut exec_state).await?; Ok(exec_state.memory) } diff --git a/src/wasm-lib/kcl/src/lib.rs b/src/wasm-lib/kcl/src/lib.rs index b5f3be4a01..5042865497 100644 --- a/src/wasm-lib/kcl/src/lib.rs +++ b/src/wasm-lib/kcl/src/lib.rs @@ -14,38 +14,69 @@ macro_rules! println { } mod ast; -pub mod coredump; -pub mod docs; -pub mod engine; -pub mod errors; -pub mod executor; -pub mod fs; +mod coredump; +mod docs; +mod engine; +mod errors; +mod executor; +mod fs; mod function_param; -pub mod lint; -pub mod lsp; -pub mod parser; -pub mod settings; +mod lint; +mod lsp; +mod parser; +mod settings; #[cfg(test)] mod simulation_tests; -pub mod std; +mod std; #[cfg(not(target_arch = "wasm32"))] pub mod test_server; -pub mod thread; -pub mod token; +mod thread; +mod token; mod unparser; -pub mod walk; +mod walk; #[cfg(target_arch = "wasm32")] -pub mod wasm; +mod wasm; pub use ast::modify::modify_ast_for_sketch; pub use ast::types::ModuleId; +pub use coredump::CoreDump; +pub use engine::{EngineManager, ExecutionKind}; pub use errors::KclError; +pub use executor::{ExecState, ExecutorContext, SourceRange}; +pub use lsp::copilot::Backend as CopilotLspBackend; +pub use lsp::kcl::Backend as KclLspBackend; +use serde::Serialize; +pub use settings::types::{project::ProjectConfiguration, Configuration, UnitLength}; +pub use std::StdLib; +pub use token::lexer; + +// Rather than make executor public and make lots of it pub(crate), just re-export into a new module. +// Ideally we wouldn't export these things at all, they should only be used for testing. +pub mod exec { + pub use crate::executor::{DefaultPlanes, IdGenerator, KclValue, PlaneType, ProgramMemory, Sketch}; +} + +#[cfg(target_arch = "wasm32")] +pub mod wasm_engine { + pub use crate::coredump::wasm::{CoreDumpManager, CoreDumper}; + pub use crate::engine::conn_wasm::*; + pub use crate::fs::wasm::FileSystemManager; +} + +pub mod std_utils { + pub use crate::std::utils::{get_tangential_arc_to_info, is_points_ccw_wasm, TangentialArcInfoInput}; +} #[derive(Clone, Debug, PartialEq)] pub struct Program { ast: ast::types::Node, } +#[cfg(any(test, feature = "lsp-test-util"))] +pub use lsp::test_util::copilot_lsp_server; +#[cfg(any(test, feature = "lsp-test-util"))] +pub use lsp::test_util::kcl_lsp_server; + impl Program { pub fn parse(input: &str) -> Result { let module_id = ModuleId::default(); @@ -55,6 +86,29 @@ impl Program { Ok(Program { ast }) } + + pub fn from_json(json: &str) -> Result { + let ast = serde_json::from_str(json).map_err(|e| e.to_string())?; + Ok(Self { ast }) + } + + /// Deserialize the ast from a stringified json + pub fn compute_digest(&mut self) -> ast::types::digest::Digest { + self.ast.compute_digest() + } + + pub fn lint_all(&self) -> Result, anyhow::Error> { + self.ast.lint_all() + } + + pub fn as_serde(&self) -> &impl Serialize { + &self.ast + } + + pub fn recast(&self) -> String { + // Use the default options until we integrate into the UI the ability to change them. + self.ast.recast(&Default::default(), 0) + } } impl From> for Program { diff --git a/src/wasm-lib/kcl/src/lint/checks/mod.rs b/src/wasm-lib/kcl/src/lint/checks/mod.rs index 2f3cf1b119..2be4afe899 100644 --- a/src/wasm-lib/kcl/src/lint/checks/mod.rs +++ b/src/wasm-lib/kcl/src/lint/checks/mod.rs @@ -4,5 +4,7 @@ mod std_lib_args; #[allow(unused_imports)] pub use camel_case::{lint_object_properties, lint_variables, Z0001}; +#[allow(unused_imports)] pub use offset_plane::{lint_should_be_offset_plane, Z0003}; +#[allow(unused_imports)] pub use std_lib_args::{lint_call_expressions, Z0002}; diff --git a/src/wasm-lib/kcl/src/lint/mod.rs b/src/wasm-lib/kcl/src/lint/mod.rs index 21e84f8bb0..3209d96166 100644 --- a/src/wasm-lib/kcl/src/lint/mod.rs +++ b/src/wasm-lib/kcl/src/lint/mod.rs @@ -1,4 +1,5 @@ pub mod checks; pub mod rule; +#[allow(unused_imports)] pub use rule::{Discovered, Finding}; diff --git a/src/wasm-lib/kcl/src/lsp/copilot/mod.rs b/src/wasm-lib/kcl/src/lsp/copilot/mod.rs index 3f5a06d355..8516e42110 100644 --- a/src/wasm-lib/kcl/src/lsp/copilot/mod.rs +++ b/src/wasm-lib/kcl/src/lsp/copilot/mod.rs @@ -1,4 +1,5 @@ //! The copilot lsp server for ghost text. +#![allow(dead_code)] pub mod cache; pub mod types; @@ -26,6 +27,7 @@ use tower_lsp::{ use crate::lsp::{ backend::Backend as _, + copilot::cache::CopilotCache, copilot::types::{ CopilotAcceptCompletionParams, CopilotCompletionResponse, CopilotCompletionTelemetry, CopilotEditorInfo, CopilotLspCompletionParams, CopilotRejectCompletionParams, DocParams, @@ -131,6 +133,38 @@ impl crate::lsp::backend::Backend for Backend { } impl Backend { + #[cfg(target_arch = "wasm32")] + pub fn new_wasm( + client: tower_lsp::Client, + fs: crate::fs::wasm::FileSystemManager, + zoo_client: kittycad::Client, + dev_mode: bool, + ) -> Self { + Self::new(client, crate::fs::FileManager::new(fs), zoo_client, dev_mode) + } + + pub fn new( + client: tower_lsp::Client, + fs: crate::fs::FileManager, + zoo_client: kittycad::Client, + dev_mode: bool, + ) -> Self { + Self { + client, + fs: Arc::new(fs), + workspace_folders: Default::default(), + code_map: Default::default(), + editor_info: Arc::new(RwLock::new(CopilotEditorInfo::default())), + cache: Arc::new(CopilotCache::new()), + telemetry: Default::default(), + zoo_client, + + is_initialized: Default::default(), + diagnostics_map: Default::default(), + dev_mode, + } + } + /// Get completions from the kittycad api. pub async fn get_completions(&self, language: String, prompt: String, suffix: String) -> Result> { let body = kittycad::types::KclCodeCompletionRequest { diff --git a/src/wasm-lib/kcl/src/lsp/kcl/mod.rs b/src/wasm-lib/kcl/src/lsp/kcl/mod.rs index e073b178bd..7488bd68f8 100644 --- a/src/wasm-lib/kcl/src/lsp/kcl/mod.rs +++ b/src/wasm-lib/kcl/src/lsp/kcl/mod.rs @@ -1,4 +1,5 @@ //! Functions for the `kcl` lsp server. +#![allow(dead_code)] use std::{ collections::HashMap, @@ -41,11 +42,10 @@ use tower_lsp::{ use crate::{ ast::types::{Expr, ModuleId, Node, VariableKind}, - executor::{IdGenerator, SourceRange}, lsp::{backend::Backend as _, util::IntoDiagnostic}, parser::PIPE_OPERATOR, token::TokenType, - Program, + ExecState, Program, SourceRange, }; lazy_static::lazy_static! { @@ -123,6 +123,57 @@ pub struct Backend { pub is_initialized: Arc>, } +impl Backend { + #[cfg(target_arch = "wasm32")] + pub fn new_wasm( + client: Client, + executor_ctx: Option, + fs: crate::fs::wasm::FileSystemManager, + zoo_client: kittycad::Client, + can_send_telemetry: bool, + ) -> Result { + Self::new( + client, + executor_ctx, + crate::fs::FileManager::new(fs), + zoo_client, + can_send_telemetry, + ) + } + + fn new( + client: Client, + executor_ctx: Option, + fs: crate::fs::FileManager, + zoo_client: kittycad::Client, + can_send_telemetry: bool, + ) -> Result { + let stdlib = crate::std::StdLib::new(); + let stdlib_completions = get_completions_from_stdlib(&stdlib).map_err(|e| e.to_string())?; + let stdlib_signatures = get_signatures_from_stdlib(&stdlib).map_err(|e| e.to_string())?; + + Ok(Self { + client, + fs: Arc::new(fs), + stdlib_completions, + stdlib_signatures, + zoo_client, + can_send_telemetry, + can_execute: Arc::new(RwLock::new(executor_ctx.is_some())), + executor_ctx: Arc::new(RwLock::new(executor_ctx)), + workspace_folders: Default::default(), + token_map: Default::default(), + ast_map: Default::default(), + memory_map: Default::default(), + code_map: Default::default(), + diagnostics_map: Default::default(), + symbols_map: Default::default(), + semantic_tokens_map: Default::default(), + is_initialized: Default::default(), + }) + } +} + // Implement the shared backend trait for the language server. #[async_trait::async_trait] impl crate::lsp::backend::Backend for Backend { @@ -590,25 +641,22 @@ impl Backend { return Ok(()); } - let mut id_generator = IdGenerator::default(); + let mut exec_state = ExecState::default(); // Clear the scene, before we execute so it's not fugly as shit. executor_ctx .engine - .clear_scene(&mut id_generator, SourceRange::default()) + .clear_scene(&mut exec_state.id_generator, SourceRange::default()) .await?; - let exec_state = match executor_ctx.run(ast, None, id_generator, None).await { - Ok(exec_state) => exec_state, - Err(err) => { - self.memory_map.remove(params.uri.as_str()); - self.add_to_diagnostics(params, &[err], false).await; + if let Err(err) = executor_ctx.run(ast, &mut exec_state).await { + self.memory_map.remove(params.uri.as_str()); + self.add_to_diagnostics(params, &[err], false).await; - // Since we already published the diagnostics we don't really care about the error - // string. - return Err(anyhow::anyhow!("failed to execute code")); - } - }; + // Since we already published the diagnostics we don't really care about the error + // string. + return Err(anyhow::anyhow!("failed to execute code")); + } self.memory_map .insert(params.uri.to_string(), exec_state.memory.clone()); diff --git a/src/wasm-lib/kcl/src/parser.rs b/src/wasm-lib/kcl/src/parser.rs index f0de970b34..4e54ff8326 100644 --- a/src/wasm-lib/kcl/src/parser.rs +++ b/src/wasm-lib/kcl/src/parser.rs @@ -12,6 +12,7 @@ pub(crate) mod parser_impl; pub const PIPE_SUBSTITUTION_OPERATOR: &str = "%"; pub const PIPE_OPERATOR: &str = "|>"; +#[cfg(test)] /// Parse the given KCL code into an AST. This is the top-level. pub fn top_level_parse(code: &str) -> Result, KclError> { let module_id = ModuleId::default(); diff --git a/src/wasm-lib/kcl/src/settings/types/mod.rs b/src/wasm-lib/kcl/src/settings/types/mod.rs index 231a998362..baccec05d3 100644 --- a/src/wasm-lib/kcl/src/settings/types/mod.rs +++ b/src/wasm-lib/kcl/src/settings/types/mod.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; use validator::{Validate, ValidateRange}; const DEFAULT_THEME_COLOR: f64 = 264.5; -pub const DEFAULT_PROJECT_KCL_FILE: &str = "main.kcl"; +pub const _DEFAULT_PROJECT_KCL_FILE: &str = "main.kcl"; const DEFAULT_PROJECT_NAME_TEMPLATE: &str = "project-$nnn"; /// High level configuration. diff --git a/src/wasm-lib/kcl/src/std/kcl_stdlib.rs b/src/wasm-lib/kcl/src/std/kcl_stdlib.rs index 78cd204caf..6ed9fc85d7 100644 --- a/src/wasm-lib/kcl/src/std/kcl_stdlib.rs +++ b/src/wasm-lib/kcl/src/std/kcl_stdlib.rs @@ -4,7 +4,7 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use crate::{ - ast::types::{BodyItem, Expr, FunctionExpression, Node, Program}, + ast::types::{FunctionExpression, Program}, docs::{StdLibFn, StdLibFnData}, }; @@ -77,18 +77,3 @@ impl Serialize for Box { self.to_json().unwrap().serialize(serializer) } } - -/// Parse a KCL program. Expect it to have a single body item, which is a function. -/// Return the program and its single function. -/// Return None if those expectations aren't met. -pub fn extract_function(source: &str) -> Option<(Node, crate::ast::types::BoxNode)> { - let src = crate::parser::top_level_parse(source).ok()?; - assert_eq!(src.body.len(), 1); - let BodyItem::ExpressionStatement(expr) = src.body.last()? else { - panic!("expected expression statement"); - }; - let Expr::FunctionExpression(function) = expr.expression.clone() else { - panic!("expected function expr"); - }; - Some((src, function)) -} diff --git a/src/wasm-lib/kcl/src/std/mod.rs b/src/wasm-lib/kcl/src/std/mod.rs index 19c83a60a7..0ec8c1734e 100644 --- a/src/wasm-lib/kcl/src/std/mod.rs +++ b/src/wasm-lib/kcl/src/std/mod.rs @@ -48,8 +48,6 @@ pub type StdFn = fn( Args, ) -> std::pin::Pin> + Send + '_>>; -pub type FnMap = HashMap; - lazy_static! { static ref CORE_FNS: Vec> = vec![ Box::new(LegLen), diff --git a/src/wasm-lib/kcl/src/std/types.rs b/src/wasm-lib/kcl/src/std/types.rs index 53ae989166..7af88d8132 100644 --- a/src/wasm-lib/kcl/src/std/types.rs +++ b/src/wasm-lib/kcl/src/std/types.rs @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize}; #[ts(export)] pub struct Uint(f64); +#[allow(dead_code)] impl Uint { pub fn new(value: f64) -> Self { if value < 0.0 { diff --git a/src/wasm-lib/kcl/src/std/utils.rs b/src/wasm-lib/kcl/src/std/utils.rs index b4f889a781..8ef3ce4920 100644 --- a/src/wasm-lib/kcl/src/std/utils.rs +++ b/src/wasm-lib/kcl/src/std/utils.rs @@ -53,6 +53,7 @@ pub fn delta(from_angle: Angle, to_angle: Angle) -> Angle { Angle::default() } +#[allow(dead_code)] pub fn clockwise_sign(points: &[Point2d]) -> i32 { let mut sum = 0.0; for i in 0..points.len() { @@ -563,6 +564,7 @@ pub struct TangentialArcInfoInput { } /// Structure to hold the output data from calculating tangential arc information. +#[allow(dead_code)] pub struct TangentialArcInfoOutput { /// The center point of the arc. pub center: Coords2d, diff --git a/src/wasm-lib/kcl/src/test_server.rs b/src/wasm-lib/kcl/src/test_server.rs index c77fb35822..0022be33ed 100644 --- a/src/wasm-lib/kcl/src/test_server.rs +++ b/src/wasm-lib/kcl/src/test_server.rs @@ -1,9 +1,9 @@ //! Types used to send data to the test server. use crate::{ - executor::{new_zoo_client, ExecutorContext, ExecutorSettings, IdGenerator, ProgramMemory}, + executor::{new_zoo_client, ExecutorContext, ExecutorSettings, ProgramMemory}, settings::types::UnitLength, - Program, + ExecState, Program, }; #[derive(serde::Deserialize, serde::Serialize)] @@ -43,7 +43,8 @@ async fn do_execute_and_snapshot( ctx: &ExecutorContext, program: Program, ) -> anyhow::Result<(crate::executor::ExecState, image::DynamicImage)> { - let (exec_state, snapshot) = ctx.execute_and_prepare(&program, IdGenerator::default(), None).await?; + let mut exec_state = ExecState::default(); + let snapshot = ctx.execute_and_prepare(&program, &mut exec_state).await?; // Create a temporary file to write the output to. let output_file = std::env::temp_dir().join(format!("kcl_output_{}.png", uuid::Uuid::new_v4())); diff --git a/src/wasm-lib/kcl/src/thread/mod.rs b/src/wasm-lib/kcl/src/thread/mod.rs index c37bab0fb8..317ed56001 100644 --- a/src/wasm-lib/kcl/src/thread/mod.rs +++ b/src/wasm-lib/kcl/src/thread/mod.rs @@ -1,13 +1,15 @@ //! An implementation of threads that works in wasm with promises and other platforms with tokio. +#![allow(dead_code)] +#![allow(unused_imports)] #[cfg(not(target_arch = "wasm32"))] -pub mod local; +mod local; #[cfg(not(target_arch = "wasm32"))] pub use local::JoinHandle; #[cfg(target_arch = "wasm32")] #[cfg(not(test))] -pub mod wasm; +mod wasm; #[cfg(target_arch = "wasm32")] #[cfg(not(test))] diff --git a/src/wasm-lib/src/wasm.rs b/src/wasm-lib/src/wasm.rs index 741c9a86b7..4ee133df52 100644 --- a/src/wasm-lib/src/wasm.rs +++ b/src/wasm-lib/src/wasm.rs @@ -1,18 +1,10 @@ //! Wasm bindings for `kcl`. -use std::{ - str::FromStr, - sync::{Arc, RwLock}, -}; +use std::{str::FromStr, sync::Arc}; use futures::stream::TryStreamExt; use gloo_utils::format::JsValueSerdeExt; -use kcl_lib::{ - ast::types::{ModuleId, Node, Program}, - coredump::CoreDump, - engine::EngineManager, - executor::ExecutorSettings, -}; +use kcl_lib::{CoreDump, EngineManager, ExecState, ModuleId, Program}; use tower_lsp::{LspService, Server}; use wasm_bindgen::prelude::*; @@ -23,54 +15,32 @@ pub async fn execute_wasm( memory_str: &str, id_generator_str: &str, units: &str, - engine_manager: kcl_lib::engine::conn_wasm::EngineCommandManager, - fs_manager: kcl_lib::fs::wasm::FileSystemManager, + engine_manager: kcl_lib::wasm_engine::EngineCommandManager, + fs_manager: kcl_lib::wasm_engine::FileSystemManager, project_directory: Option, is_mock: bool, ) -> Result { console_error_panic_hook::set_once(); - // deserialize the ast from a stringified json - let program: Node = serde_json::from_str(program_str).map_err(|e| e.to_string())?; - let memory: kcl_lib::executor::ProgramMemory = serde_json::from_str(memory_str).map_err(|e| e.to_string())?; - let id_generator: kcl_lib::executor::IdGenerator = - serde_json::from_str(id_generator_str).map_err(|e| e.to_string())?; - let units = kcl_lib::settings::types::UnitLength::from_str(units).map_err(|e| e.to_string())?; - - let engine: std::sync::Arc> = if is_mock { - Arc::new(Box::new( - kcl_lib::engine::conn_mock::EngineConnection::new() - .await - .map_err(|e| format!("{:?}", e))?, - )) - } else { - Arc::new(Box::new( - kcl_lib::engine::conn_wasm::EngineConnection::new(engine_manager) - .await - .map_err(|e| format!("{:?}", e))?, - )) - }; - let fs = Arc::new(kcl_lib::fs::FileManager::new(fs_manager)); - let context_type = if is_mock { - kcl_lib::executor::ContextType::Mock + let program = Program::from_json(program_str)?; + let memory: kcl_lib::exec::ProgramMemory = serde_json::from_str(memory_str).map_err(|e| e.to_string())?; + let id_generator: kcl_lib::exec::IdGenerator = serde_json::from_str(id_generator_str).map_err(|e| e.to_string())?; + + let units = kcl_lib::UnitLength::from_str(units).map_err(|e| e.to_string())?; + let ctx = if is_mock { + kcl_lib::ExecutorContext::new_mock(fs_manager, units).await? } else { - kcl_lib::executor::ContextType::Live + kcl_lib::ExecutorContext::new(engine_manager, fs_manager, units).await? }; - let ctx = kcl_lib::executor::ExecutorContext { - engine, - fs, - stdlib: std::sync::Arc::new(kcl_lib::std::StdLib::new()), - settings: ExecutorSettings { - units, - ..Default::default() - }, - context_type, + + let mut exec_state = ExecState { + memory, + id_generator, + project_directory, + ..ExecState::default() }; - let exec_state = ctx - .run(&program, Some(memory), id_generator, project_directory) - .await - .map_err(String::from)?; + ctx.run(&program, &mut exec_state).await.map_err(String::from)?; // The serde-wasm-bindgen does not work here because of weird HashMap issues so we use the // gloo-serialize crate instead. @@ -84,7 +54,7 @@ pub async fn execute_wasm( pub async fn kcl_lint(program_str: &str) -> Result { console_error_panic_hook::set_once(); - let program: Node = serde_json::from_str(program_str).map_err(|e| e.to_string())?; + let program = Program::from_json(program_str)?; let mut findings = vec![]; for discovered_finding in program.lint_all().into_iter().flatten() { findings.push(discovered_finding); @@ -96,16 +66,16 @@ pub async fn kcl_lint(program_str: &str) -> Result { // wasm_bindgen wrapper for creating default planes #[wasm_bindgen] pub async fn make_default_planes( - engine_manager: kcl_lib::engine::conn_wasm::EngineCommandManager, + engine_manager: kcl_lib::wasm_engine::EngineCommandManager, ) -> Result { console_error_panic_hook::set_once(); // deserialize the ast from a stringified json - let engine = kcl_lib::engine::conn_wasm::EngineConnection::new(engine_manager) + let engine = kcl_lib::wasm_engine::EngineConnection::new(engine_manager) .await .map_err(|e| format!("{:?}", e))?; let default_planes = engine - .new_default_planes(&mut kcl_lib::executor::IdGenerator::default(), Default::default()) + .new_default_planes(&mut kcl_lib::exec::IdGenerator::default(), Default::default()) .await .map_err(String::from)?; @@ -117,13 +87,13 @@ pub async fn make_default_planes( // wasm_bindgen wrapper for modifying the grid #[wasm_bindgen] pub async fn modify_grid( - engine_manager: kcl_lib::engine::conn_wasm::EngineCommandManager, + engine_manager: kcl_lib::wasm_engine::EngineCommandManager, hidden: bool, ) -> Result<(), String> { console_error_panic_hook::set_once(); // deserialize the ast from a stringified json - let engine = kcl_lib::engine::conn_wasm::EngineConnection::new(engine_manager) + let engine = kcl_lib::wasm_engine::EngineConnection::new(engine_manager) .await .map_err(|e| format!("{:?}", e))?; engine.modify_grid(hidden).await.map_err(String::from)?; @@ -134,7 +104,7 @@ pub async fn modify_grid( // wasm_bindgen wrapper for execute #[wasm_bindgen] pub async fn modify_ast_for_sketch_wasm( - manager: kcl_lib::engine::conn_wasm::EngineCommandManager, + manager: kcl_lib::wasm_engine::EngineCommandManager, program_str: &str, sketch_name: &str, plane_type: &str, @@ -142,19 +112,18 @@ pub async fn modify_ast_for_sketch_wasm( ) -> Result { console_error_panic_hook::set_once(); - // deserialize the ast from a stringified json - let mut program: Node = serde_json::from_str(program_str).map_err(|e| e.to_string())?; + let mut program = Program::from_json(program_str)?; - let plane: kcl_lib::executor::PlaneType = serde_json::from_str(plane_type).map_err(|e| e.to_string())?; + let plane: kcl_lib::exec::PlaneType = serde_json::from_str(plane_type).map_err(|e| e.to_string())?; let engine: Arc> = Arc::new(Box::new( - kcl_lib::engine::conn_wasm::EngineConnection::new(manager) + kcl_lib::wasm_engine::EngineConnection::new(manager) .await .map_err(|e| format!("{:?}", e))?, )); let module_id = ModuleId::default(); - let _ = kcl_lib::ast::modify::modify_ast_for_sketch( + let _ = kcl_lib::modify_ast_for_sketch( &engine, &mut program, module_id, @@ -167,7 +136,7 @@ pub async fn modify_ast_for_sketch_wasm( // The serde-wasm-bindgen does not work here because of weird HashMap issues so we use the // gloo-serialize crate instead. - JsValue::from_serde(&program).map_err(|e| e.to_string()) + JsValue::from_serde(program.as_serde()).map_err(|e| e.to_string()) } #[wasm_bindgen] @@ -196,7 +165,7 @@ pub fn lexer_wasm(js: &str) -> Result { console_error_panic_hook::set_once(); let module_id = ModuleId::default(); - let tokens = kcl_lib::token::lexer(js, module_id).map_err(JsError::from)?; + let tokens = kcl_lib::lexer(js, module_id).map_err(JsError::from)?; Ok(JsValue::from_serde(&tokens)?) } @@ -204,13 +173,10 @@ pub fn lexer_wasm(js: &str) -> Result { pub fn parse_wasm(js: &str) -> Result { console_error_panic_hook::set_once(); - let module_id = ModuleId::default(); - let tokens = kcl_lib::token::lexer(js, module_id).map_err(String::from)?; - let parser = kcl_lib::parser::Parser::new(tokens); - let program = parser.ast().map_err(String::from)?; + let program = Program::parse(js).map_err(String::from)?; // The serde-wasm-bindgen does not work here because of weird HashMap issues so we use the // gloo-serialize crate instead. - JsValue::from_serde(&program).map_err(|e| e.to_string()) + JsValue::from_serde(program.as_serde()).map_err(|e| e.to_string()) } // wasm_bindgen wrapper for recast @@ -219,19 +185,15 @@ pub fn parse_wasm(js: &str) -> Result { pub fn recast_wasm(json_str: &str) -> Result { console_error_panic_hook::set_once(); - // deserialize the ast from a stringified json - let program: kcl_lib::ast::types::Program = serde_json::from_str(json_str).map_err(JsError::from)?; - - // Use the default options until we integrate into the UI the ability to change them. - let result = program.recast(&Default::default(), 0); - Ok(JsValue::from_serde(&result)?) + let program = Program::from_json(json_str).map_err(|s| JsError::new(&s))?; + Ok(JsValue::from_serde(&program.recast())?) } #[wasm_bindgen] pub struct ServerConfig { into_server: js_sys::AsyncIterator, from_server: web_sys::WritableStream, - fs: kcl_lib::fs::wasm::FileSystemManager, + fs: kcl_lib::wasm_engine::FileSystemManager, } #[wasm_bindgen] @@ -240,7 +202,7 @@ impl ServerConfig { pub fn new( into_server: js_sys::AsyncIterator, from_server: web_sys::WritableStream, - fs: kcl_lib::fs::wasm::FileSystemManager, + fs: kcl_lib::wasm_engine::FileSystemManager, ) -> Self { Self { into_server, @@ -260,7 +222,7 @@ impl ServerConfig { #[wasm_bindgen] pub async fn kcl_lsp_run( config: ServerConfig, - engine_manager: Option, + engine_manager: Option, units: &str, token: String, baseurl: String, @@ -273,74 +235,37 @@ pub async fn kcl_lsp_run( fs, } = config; - let stdlib = kcl_lib::std::StdLib::new(); - let stdlib_completions = kcl_lib::lsp::kcl::get_completions_from_stdlib(&stdlib).map_err(|e| e.to_string())?; - let stdlib_signatures = kcl_lib::lsp::kcl::get_signatures_from_stdlib(&stdlib).map_err(|e| e.to_string())?; - - let mut zoo_client = kittycad::Client::new(token); - zoo_client.set_base_url(baseurl.as_str()); - - let file_manager = Arc::new(kcl_lib::fs::FileManager::new(fs)); - let executor_ctx = if let Some(engine_manager) = engine_manager { - let units = kcl_lib::settings::types::UnitLength::from_str(units).map_err(|e| e.to_string())?; - let engine = kcl_lib::engine::conn_wasm::EngineConnection::new(engine_manager) - .await - .map_err(|e| format!("{:?}", e))?; - Some(kcl_lib::executor::ExecutorContext { - engine: Arc::new(Box::new(engine)), - fs: file_manager.clone(), - stdlib: std::sync::Arc::new(stdlib), - settings: ExecutorSettings { - units, - ..Default::default() - }, - context_type: kcl_lib::executor::ContextType::Live, - }) + let units = kcl_lib::UnitLength::from_str(units).map_err(|e| e.to_string())?; + Some(kcl_lib::ExecutorContext::new(engine_manager, fs.clone(), units).await?) } else { None }; + let mut zoo_client = kittycad::Client::new(token); + zoo_client.set_base_url(baseurl.as_str()); + // Check if we can send telememtry for this user. - let privacy_settings = match zoo_client.users().get_privacy_settings().await { - Ok(privacy_settings) => privacy_settings, + let can_send_telemetry = match zoo_client.users().get_privacy_settings().await { + Ok(privacy_settings) => privacy_settings.can_train_on_data, Err(err) => { // In the case of dev we don't always have a sub set, but prod we should. if err .to_string() .contains("The modeling app subscription type is missing.") { - kittycad::types::PrivacySettings { - can_train_on_data: true, - } + true } else { return Err(err.to_string().into()); } } }; - let (service, socket) = LspService::build(|client| kcl_lib::lsp::kcl::Backend { - client, - fs: file_manager, - workspace_folders: Default::default(), - stdlib_completions, - stdlib_signatures, - token_map: Default::default(), - ast_map: Default::default(), - memory_map: Default::default(), - code_map: Default::default(), - diagnostics_map: Default::default(), - symbols_map: Default::default(), - semantic_tokens_map: Default::default(), - zoo_client, - can_send_telemetry: privacy_settings.can_train_on_data, - can_execute: Arc::new(tokio::sync::RwLock::new(executor_ctx.is_some())), - executor_ctx: Arc::new(tokio::sync::RwLock::new(executor_ctx)), - - is_initialized: Default::default(), + let (service, socket) = LspService::build(|client| { + kcl_lib::KclLspBackend::new_wasm(client, executor_ctx, fs, zoo_client, can_send_telemetry).unwrap() }) - .custom_method("kcl/updateUnits", kcl_lib::lsp::kcl::Backend::update_units) - .custom_method("kcl/updateCanExecute", kcl_lib::lsp::kcl::Backend::update_can_execute) + .custom_method("kcl/updateUnits", kcl_lib::KclLspBackend::update_units) + .custom_method("kcl/updateCanExecute", kcl_lib::KclLspBackend::update_can_execute) .finish(); let input = wasm_bindgen_futures::stream::JsStream::from(into_server); @@ -383,40 +308,22 @@ pub async fn copilot_lsp_run(config: ServerConfig, token: String, baseurl: Strin let mut zoo_client = kittycad::Client::new(token); zoo_client.set_base_url(baseurl.as_str()); - let file_manager = Arc::new(kcl_lib::fs::FileManager::new(fs)); - - let (service, socket) = LspService::build(|client| kcl_lib::lsp::copilot::Backend { - client, - fs: file_manager, - workspace_folders: Default::default(), - code_map: Default::default(), - editor_info: Arc::new(RwLock::new(kcl_lib::lsp::copilot::types::CopilotEditorInfo::default())), - cache: Arc::new(kcl_lib::lsp::copilot::cache::CopilotCache::new()), - telemetry: Default::default(), - zoo_client, - - is_initialized: Default::default(), - diagnostics_map: Default::default(), - dev_mode: if baseurl == "https://api.dev.zoo.dev" { - true - } else { - false - }, - }) - .custom_method("copilot/setEditorInfo", kcl_lib::lsp::copilot::Backend::set_editor_info) - .custom_method( - "copilot/getCompletions", - kcl_lib::lsp::copilot::Backend::get_completions_cycling, - ) - .custom_method( - "copilot/notifyAccepted", - kcl_lib::lsp::copilot::Backend::accept_completion, - ) - .custom_method( - "copilot/notifyRejected", - kcl_lib::lsp::copilot::Backend::reject_completions, - ) - .finish(); + let dev_mode = if baseurl == "https://api.dev.zoo.dev" { + true + } else { + false + }; + + let (service, socket) = + LspService::build(|client| kcl_lib::CopilotLspBackend::new_wasm(client, fs, zoo_client, dev_mode)) + .custom_method("copilot/setEditorInfo", kcl_lib::CopilotLspBackend::set_editor_info) + .custom_method( + "copilot/getCompletions", + kcl_lib::CopilotLspBackend::get_completions_cycling, + ) + .custom_method("copilot/notifyAccepted", kcl_lib::CopilotLspBackend::accept_completion) + .custom_method("copilot/notifyRejected", kcl_lib::CopilotLspBackend::reject_completions) + .finish(); let input = wasm_bindgen_futures::stream::JsStream::from(into_server); let input = input @@ -442,7 +349,7 @@ pub async fn copilot_lsp_run(config: ServerConfig, token: String, baseurl: Strin pub fn is_points_ccw(points: &[f64]) -> i32 { console_error_panic_hook::set_once(); - kcl_lib::std::utils::is_points_ccw_wasm(points) + kcl_lib::std_utils::is_points_ccw_wasm(points) } #[wasm_bindgen] @@ -479,7 +386,7 @@ pub fn get_tangential_arc_to_info( ) -> TangentialArcInfoOutputWasm { console_error_panic_hook::set_once(); - let result = kcl_lib::std::utils::get_tangential_arc_to_info(kcl_lib::std::utils::TangentialArcInfoInput { + let result = kcl_lib::std_utils::get_tangential_arc_to_info(kcl_lib::std_utils::TangentialArcInfoInput { arc_start_point: [arc_start_point_x, arc_start_point_y], arc_end_point: [arc_end_point_x, arc_end_point_y], tan_previous_point: [tan_previous_point_x, tan_previous_point_y], @@ -503,7 +410,7 @@ pub fn get_tangential_arc_to_info( pub fn program_memory_init() -> Result { console_error_panic_hook::set_once(); - let memory = kcl_lib::executor::ProgramMemory::default(); + let memory = kcl_lib::exec::ProgramMemory::default(); // The serde-wasm-bindgen does not work here because of weird HashMap issues so we use the // gloo-serialize crate instead. @@ -512,10 +419,10 @@ pub fn program_memory_init() -> Result { /// Get a coredump. #[wasm_bindgen] -pub async fn coredump(core_dump_manager: kcl_lib::coredump::wasm::CoreDumpManager) -> Result { +pub async fn coredump(core_dump_manager: kcl_lib::wasm_engine::CoreDumpManager) -> Result { console_error_panic_hook::set_once(); - let core_dumper = kcl_lib::coredump::wasm::CoreDumper::new(core_dump_manager); + let core_dumper = kcl_lib::wasm_engine::CoreDumper::new(core_dump_manager); let dump = core_dumper.dump().await.map_err(|e| e.to_string())?; // The serde-wasm-bindgen does not work here because of weird HashMap issues so we use the @@ -528,7 +435,7 @@ pub async fn coredump(core_dump_manager: kcl_lib::coredump::wasm::CoreDumpManage pub fn default_app_settings() -> Result { console_error_panic_hook::set_once(); - let settings = kcl_lib::settings::types::Configuration::default(); + let settings = kcl_lib::Configuration::default(); // The serde-wasm-bindgen does not work here because of weird HashMap issues so we use the // gloo-serialize crate instead. @@ -540,8 +447,7 @@ pub fn default_app_settings() -> Result { pub fn parse_app_settings(toml_str: &str) -> Result { console_error_panic_hook::set_once(); - let settings = kcl_lib::settings::types::Configuration::backwards_compatible_toml_parse(&toml_str) - .map_err(|e| e.to_string())?; + let settings = kcl_lib::Configuration::backwards_compatible_toml_parse(&toml_str).map_err(|e| e.to_string())?; // The serde-wasm-bindgen does not work here because of weird HashMap issues so we use the // gloo-serialize crate instead. @@ -553,7 +459,7 @@ pub fn parse_app_settings(toml_str: &str) -> Result { pub fn default_project_settings() -> Result { console_error_panic_hook::set_once(); - let settings = kcl_lib::settings::types::project::ProjectConfiguration::default(); + let settings = kcl_lib::ProjectConfiguration::default(); // The serde-wasm-bindgen does not work here because of weird HashMap issues so we use the // gloo-serialize crate instead. @@ -565,8 +471,8 @@ pub fn default_project_settings() -> Result { pub fn parse_project_settings(toml_str: &str) -> Result { console_error_panic_hook::set_once(); - let settings = kcl_lib::settings::types::project::ProjectConfiguration::backwards_compatible_toml_parse(&toml_str) - .map_err(|e| e.to_string())?; + let settings = + kcl_lib::ProjectConfiguration::backwards_compatible_toml_parse(&toml_str).map_err(|e| e.to_string())?; // The serde-wasm-bindgen does not work here because of weird HashMap issues so we use the // gloo-serialize crate instead. @@ -578,7 +484,7 @@ pub fn parse_project_settings(toml_str: &str) -> Result { pub fn serialize_project_settings(val: JsValue) -> Result { console_error_panic_hook::set_once(); - let config: kcl_lib::settings::types::Configuration = val.into_serde().map_err(|e| e.to_string())?; + let config: kcl_lib::Configuration = val.into_serde().map_err(|e| e.to_string())?; let toml_str = toml::to_string_pretty(&config).map_err(|e| e.to_string())?; diff --git a/src/wasm-lib/tests/executor/main.rs b/src/wasm-lib/tests/executor/main.rs index 034adb81d3..12a85dfa3f 100644 --- a/src/wasm-lib/tests/executor/main.rs +++ b/src/wasm-lib/tests/executor/main.rs @@ -1,6 +1,6 @@ use kcl_lib::{ - settings::types::UnitLength, test_server::{execute_and_snapshot, execute_and_snapshot_no_auth}, + UnitLength, }; /// The minimum permissible difference between asserted twenty-twenty images. diff --git a/src/wasm-lib/tests/executor/no_visuals.rs b/src/wasm-lib/tests/executor/no_visuals.rs index 5112a33a8b..2fe50346f1 100644 --- a/src/wasm-lib/tests/executor/no_visuals.rs +++ b/src/wasm-lib/tests/executor/no_visuals.rs @@ -1,7 +1,4 @@ -use kcl_lib::{ - executor::{ExecutorContext, IdGenerator}, - parser, KclError, Program, -}; +use kcl_lib::{ExecState, KclError, Program}; macro_rules! gen_test_fail { ($file:ident, $expected:literal) => { @@ -25,38 +22,24 @@ macro_rules! gen_test_parse_fail { }; } -async fn setup(program: &str) -> (ExecutorContext, Program, IdGenerator) { +async fn setup(program: &str) -> (kcl_lib::ExecutorContext, Program, ExecState) { let program = Program::parse(program).unwrap(); - let ctx = kcl_lib::executor::ExecutorContext { - engine: std::sync::Arc::new(Box::new( - kcl_lib::engine::conn_mock::EngineConnection::new().await.unwrap(), - )), - fs: std::sync::Arc::new(kcl_lib::fs::FileManager::new()), - stdlib: std::sync::Arc::new(kcl_lib::std::StdLib::new()), - settings: Default::default(), - context_type: kcl_lib::executor::ContextType::Mock, - }; - (ctx, program, IdGenerator::default()) + let ctx = kcl_lib::ExecutorContext::new_mock().await; + let mut exec_state = ExecState::default(); + exec_state.project_directory = Some("tests/executor/inputs/no_visuals/".to_owned()); + (ctx, program, exec_state) } async fn run_fail(code: &str) -> KclError { - let (ctx, program, id_generator) = setup(code).await; - let Err(e) = ctx - .run( - &program, - None, - id_generator, - Some("tests/executor/inputs/no_visuals/".to_owned()), - ) - .await - else { + let (ctx, program, mut exec_state) = setup(code).await; + let Err(e) = ctx.run(&program, &mut exec_state).await else { panic!("Expected this KCL program to fail, but it (incorrectly) never threw an error."); }; e } async fn run_parse_fail(code: &str) -> KclError { - let Err(e) = parser::top_level_parse(code) else { + let Err(e) = Program::parse(code) else { panic!("Expected this KCL program to fail to parse, but it (incorrectly) never threw an error."); }; e diff --git a/src/wasm-lib/tests/executor/visuals.rs b/src/wasm-lib/tests/executor/visuals.rs index df46649577..1a45c90ec6 100644 --- a/src/wasm-lib/tests/executor/visuals.rs +++ b/src/wasm-lib/tests/executor/visuals.rs @@ -10,7 +10,7 @@ macro_rules! kcl_test { async fn $test_name() { let code = kcl_input!($file); - let result = super::execute_and_snapshot(code, kcl_lib::settings::types::UnitLength::Mm) + let result = super::execute_and_snapshot(code, kcl_lib::UnitLength::Mm) .await .unwrap(); super::assert_out($file, &result); diff --git a/src/wasm-lib/tests/modify/main.rs b/src/wasm-lib/tests/modify/main.rs index 914e6e8294..7545af724f 100644 --- a/src/wasm-lib/tests/modify/main.rs +++ b/src/wasm-lib/tests/modify/main.rs @@ -1,7 +1,7 @@ use anyhow::Result; use kcl_lib::{ - executor::{ExecutorContext, IdGenerator, KclValue, PlaneType, Sketch, SourceRange}, - modify_ast_for_sketch, ModuleId, Program, + exec::{KclValue, PlaneType, Sketch}, + modify_ast_for_sketch, ExecState, ExecutorContext, ModuleId, Program, SourceRange, }; use kittycad_modeling_cmds::{each_cmd as mcmd, length_unit::LengthUnit, shared::Point3d, ModelingCmd}; use pretty_assertions::assert_eq; @@ -9,8 +9,9 @@ use pretty_assertions::assert_eq; /// Setup the engine and parse code for an ast. async fn setup(code: &str, name: &str) -> Result<(ExecutorContext, Program, ModuleId, uuid::Uuid)> { let program = Program::parse(code)?; - let ctx = kcl_lib::executor::ExecutorContext::new_with_default_client(Default::default()).await?; - let exec_state = ctx.run(&program, None, IdGenerator::default(), None).await?; + let ctx = kcl_lib::ExecutorContext::new_with_default_client(Default::default()).await?; + let mut exec_state = ExecState::default(); + ctx.run(&program, &mut exec_state).await?; // We need to get the sketch ID. // Get the sketch ID from memory.