diff --git a/api/wasm-interpreter/src/lib.rs b/api/wasm-interpreter/src/lib.rs index 319042471..9946176c3 100644 --- a/api/wasm-interpreter/src/lib.rs +++ b/api/wasm-interpreter/src/lib.rs @@ -33,6 +33,17 @@ impl CompilationResult { } } +#[wasm_bindgen(typescript_custom_section)] +const IMPORT_CALLBACK_FUNCTION_SECTION: &'static str = r#" +type ImportCallbackFunction = (url: string) => Promise; +"#; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "ImportCallbackFunction")] + pub type ImportCallbackFunction; +} + /// Compile the content of a string. /// /// Returns a promise to a compiled component which can be run with ".run()" @@ -40,7 +51,7 @@ impl CompilationResult { pub async fn compile_from_string( source: String, base_url: String, - optional_import_callback: Option, + optional_import_callback: Option, ) -> Result { compile_from_string_with_style(source, base_url, String::new(), optional_import_callback).await } @@ -51,7 +62,7 @@ pub async fn compile_from_string_with_style( source: String, base_url: String, style: String, - optional_import_callback: Option, + optional_import_callback: Option, ) -> Result { #[cfg(feature = "console_error_panic_hook")] console_error_panic_hook::set_once(); @@ -66,7 +77,7 @@ pub async fn compile_from_string_with_style( Box>>>, > { Box::pin({ - let load_callback = load_callback.clone(); + let load_callback = js_sys::Function::from(load_callback.clone()); let file_name: String = file_name.to_string_lossy().into(); async move { let result = load_callback.call1(&JsValue::UNDEFINED, &file_name.into()); diff --git a/tools/lsp/wasm_main.rs b/tools/lsp/wasm_main.rs index d23389139..2c3623dda 100644 --- a/tools/lsp/wasm_main.rs +++ b/tools/lsp/wasm_main.rs @@ -127,6 +127,17 @@ impl Drop for ReentryGuardLock { } } +#[wasm_bindgen(typescript_custom_section)] +const IMPORT_CALLBACK_FUNCTION_SECTION: &'static str = r#" +type ImportCallbackFunction = (url: string) => Promise; +"#; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "ImportCallbackFunction")] + pub type ImportCallbackFunction; +} + #[wasm_bindgen] pub struct SlintServer { document_cache: Rc>, @@ -139,7 +150,7 @@ pub struct SlintServer { pub fn create( init_param: JsValue, send_notification: Function, - load_file: Function, + load_file: ImportCallbackFunction, ) -> Result { console_error_panic_hook::set_once(); @@ -148,7 +159,7 @@ pub fn create( let mut compiler_config = CompilerConfiguration::new(i_slint_compiler::generator::OutputFormat::Interpreter); compiler_config.open_import_fallback = Some(Rc::new(move |path| { - let load_file = load_file.clone(); + let load_file = Function::from(load_file.clone()); Box::pin(async move { Some(self::load_file(path, &load_file).await) }) }));