diff --git a/rust/kcl-lib/src/engine/conn_wasm.rs b/rust/kcl-lib/src/engine/conn_wasm.rs index b30a056a7..35b957c12 100644 --- a/rust/kcl-lib/src/engine/conn_wasm.rs +++ b/rust/kcl-lib/src/engine/conn_wasm.rs @@ -80,12 +80,12 @@ impl ResponseContext { } // Add a response to the context. - pub async fn send_response(&self, data: js_sys::Uint8Array) -> Result<(), JsValue> { + pub async fn send_response(&self, data: js_sys::Uint8Array) { let ws_result: WebSocketResponse = match bson::from_slice(&data.to_vec()) { Ok(res) => res, Err(_) => { // We don't care about the error if we can't parse it. - return Ok(()); + return; } }; @@ -96,13 +96,11 @@ impl ResponseContext { let Some(id) = id else { // We only care if we have an id. - return Ok(()); + return; }; // Add this response to our responses. self.add(id, ws_result.clone()).await; - - Ok(()) } } diff --git a/rust/kcl-lib/src/execution/typed_path.rs b/rust/kcl-lib/src/execution/typed_path.rs index e65081d9d..d3b4788a0 100644 --- a/rust/kcl-lib/src/execution/typed_path.rs +++ b/rust/kcl-lib/src/execution/typed_path.rs @@ -220,6 +220,7 @@ impl schemars::JsonSchema for TypedPath { /// /// * Does **not** touch `..` or symlinks – call `canonicalize()` if you need that. /// * Returns an owned `PathBuf` only when normalisation was required. +#[cfg(not(target_arch = "wasm32"))] fn normalise_import>(raw: S) -> std::path::PathBuf { let s = raw.as_ref(); // On Unix we need to swap `\` → `/`. On Windows we leave it alone. diff --git a/rust/kcl-wasm-lib/src/context.rs b/rust/kcl-wasm-lib/src/context.rs index 54ca9ead0..4b5eca860 100644 --- a/rust/kcl-wasm-lib/src/context.rs +++ b/rust/kcl-wasm-lib/src/context.rs @@ -6,6 +6,8 @@ use gloo_utils::format::JsValueSerdeExt; use kcl_lib::{wasm_engine::FileManager, EngineManager, ExecOutcome, KclError, KclErrorWithOutputs, Program}; use wasm_bindgen::prelude::*; +const TRUE_BUG: &str = "This is a bug in KCL and not in your code, please report this to Zoo."; + #[wasm_bindgen] pub struct Context { engine: Arc>, @@ -79,12 +81,15 @@ impl Context { self.execute_typed(program_ast_json, path, settings) .await - .and_then(|outcome| JsValue::from_serde(&outcome).map_err(|e| { - // The serde-wasm-bindgen does not work here because of weird HashMap issues. - // DO NOT USE serde_wasm_bindgen::to_value it will break the frontend. - KclErrorWithOutputs::no_outputs(KclError::internal( - format!("Could not serialize successful KCL result. This is a bug in KCL and not in your code, please report this to Zoo. Details: {e}"), - ))})) + .and_then(|outcome| { + JsValue::from_serde(&outcome).map_err(|e| { + // The serde-wasm-bindgen does not work here because of weird HashMap issues. + // DO NOT USE serde_wasm_bindgen::to_value it will break the frontend. + KclErrorWithOutputs::no_outputs(KclError::internal(format!( + "Could not serialize successful KCL result. {TRUE_BUG} Details: {e}" + ))) + }) + }) .map_err(|e: KclErrorWithOutputs| JsValue::from_serde(&e).unwrap()) } @@ -95,16 +100,14 @@ impl Context { settings: &str, ) -> Result { let program: Program = serde_json::from_str(program_ast_json).map_err(|e| { - let err = KclError::internal( - format!("Could not deserialize KCL AST. This is a bug in KCL and not in your code, please report this to Zoo. Details: {e}"), - ); - KclErrorWithOutputs::no_outputs(err) - })?; - let ctx = self - .create_executor_ctx(settings, path, false) - .map_err(|e| KclErrorWithOutputs::no_outputs(KclError::internal( - format!("Could not create KCL executor context. This is a bug in KCL and not in your code, please report this to Zoo. Details: {e}"), - )))?; + let err = KclError::internal(format!("Could not deserialize KCL AST. {TRUE_BUG} Details: {e}")); + KclErrorWithOutputs::no_outputs(err) + })?; + let ctx = self.create_executor_ctx(settings, path, false).map_err(|e| { + KclErrorWithOutputs::no_outputs(KclError::internal(format!( + "Could not create KCL executor context. {TRUE_BUG} Details: {e}" + ))) + })?; ctx.run_with_caching(program).await } @@ -125,7 +128,7 @@ impl Context { /// Send a response to kcl lib's engine. #[wasm_bindgen(js_name = sendResponse)] - pub async fn send_response(&self, data: js_sys::Uint8Array) -> Result<(), JsValue> { + pub async fn send_response(&self, data: js_sys::Uint8Array) { self.response_context.send_response(data).await } @@ -137,18 +140,40 @@ impl Context { path: Option, settings: &str, use_prev_memory: bool, - ) -> Result { + ) -> Result { console_error_panic_hook::set_once(); - let program: Program = serde_json::from_str(program_ast_json).map_err(|e| e.to_string())?; + self.execute_mock_typed(program_ast_json, path, settings, use_prev_memory) + .await + .and_then(|outcome| { + JsValue::from_serde(&outcome).map_err(|e| { + // The serde-wasm-bindgen does not work here because of weird HashMap issues. + // DO NOT USE serde_wasm_bindgen::to_value it will break the frontend. + KclErrorWithOutputs::no_outputs(KclError::internal(format!( + "Could not serialize successful KCL result. {TRUE_BUG} Details: {e}" + ))) + }) + }) + .map_err(|e: KclErrorWithOutputs| JsValue::from_serde(&e).unwrap()) + } - let ctx = self.create_executor_ctx(settings, path, true)?; - match ctx.run_mock(program, use_prev_memory).await { - // The serde-wasm-bindgen does not work here because of weird HashMap issues. - // DO NOT USE serde_wasm_bindgen::to_value it will break the frontend. - Ok(outcome) => JsValue::from_serde(&outcome).map_err(|e| e.to_string()), - Err(err) => Err(serde_json::to_string(&err).map_err(|serde_err| serde_err.to_string())?), - } + async fn execute_mock_typed( + &self, + program_ast_json: &str, + path: Option, + settings: &str, + use_prev_memory: bool, + ) -> Result { + let program: Program = serde_json::from_str(program_ast_json).map_err(|e| { + let err = KclError::internal(format!("Could not deserialize KCL AST. {TRUE_BUG} Details: {e}")); + KclErrorWithOutputs::no_outputs(err) + })?; + let ctx = self.create_executor_ctx(settings, path, true).map_err(|e| { + KclErrorWithOutputs::no_outputs(KclError::internal(format!( + "Could not create KCL executor context. {TRUE_BUG} Details: {e}" + ))) + })?; + ctx.run_mock(program, use_prev_memory).await } /// Export a scene to a file.