mirror of
https://github.com/slint-ui/slint.git
synced 2025-09-30 13:51:13 +00:00
Enforce the signature of the file load callback in the wasm build of the interpreter and the lsp
This commit is contained in:
parent
8adc4404d7
commit
a8849c1ede
2 changed files with 27 additions and 5 deletions
|
@ -33,6 +33,17 @@ impl CompilationResult {
|
|||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen(typescript_custom_section)]
|
||||
const IMPORT_CALLBACK_FUNCTION_SECTION: &'static str = r#"
|
||||
type ImportCallbackFunction = (url: string) => Promise<string>;
|
||||
"#;
|
||||
|
||||
#[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<js_sys::Function>,
|
||||
optional_import_callback: Option<ImportCallbackFunction>,
|
||||
) -> Result<CompilationResult, JsValue> {
|
||||
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<js_sys::Function>,
|
||||
optional_import_callback: Option<ImportCallbackFunction>,
|
||||
) -> Result<CompilationResult, JsValue> {
|
||||
#[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<dyn core::future::Future<Output = Option<std::io::Result<String>>>>,
|
||||
> {
|
||||
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());
|
||||
|
|
|
@ -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<Uint8Array>;
|
||||
"#;
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
#[wasm_bindgen(typescript_type = "ImportCallbackFunction")]
|
||||
pub type ImportCallbackFunction;
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct SlintServer {
|
||||
document_cache: Rc<RefCell<DocumentCache>>,
|
||||
|
@ -139,7 +150,7 @@ pub struct SlintServer {
|
|||
pub fn create(
|
||||
init_param: JsValue,
|
||||
send_notification: Function,
|
||||
load_file: Function,
|
||||
load_file: ImportCallbackFunction,
|
||||
) -> Result<SlintServer, JsError> {
|
||||
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) })
|
||||
}));
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue