node: added fileloader to LoadData. (#6530)

This commit is contained in:
FloVanGH 2024-10-14 07:33:42 +02:00 committed by GitHub
parent f8f1d468a0
commit fe596179da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 103 additions and 0 deletions

View file

@ -268,3 +268,24 @@ test("loadFile enum", (t) => {
t.deepEqual(test.check, "c");
});
test("file loader", (t) => {
const testSource = `export component Test {
in-out property <string> text: "Hello World";
}`;
const demo = loadFile(
path.join(dirname, "resources/test-fileloader.slint"),
{
fileLoader: (path) => {
if (path.includes("lib.slint")) {
return testSource;
}
return "";
},
},
) as any;
const test = new demo.App();
t.deepEqual(test.test_text, "Hello World");
});

View file

@ -0,0 +1,9 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
import { Test } from "lib.slint";
export component App inherits Window {
out property <string> test-text <=> test.text;
test := Test {}
}

View file

@ -2,12 +2,15 @@
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
use crate::to_js_unknown;
use crate::RefCountedReference;
use super::JsComponentDefinition;
use super::JsDiagnostic;
use i_slint_compiler::langtype::Type;
use itertools::Itertools;
use napi::Env;
use napi::JsFunction;
use napi::JsString;
use napi::JsUnknown;
use slint_interpreter::Compiler;
use slint_interpreter::Value;
@ -164,6 +167,68 @@ impl JsComponentCompiler {
.collect::<HashMap<String, JsUnknown>>()
}
#[napi(setter)]
pub fn set_file_loader(&mut self, env: Env, callback: JsFunction) -> napi::Result<()> {
let function_ref = RefCountedReference::new(&env, callback)?;
self.internal.set_file_loader(move |path| {
let Ok(callback) = function_ref.get::<JsFunction>() else {
return Box::pin(async {
Some(Err(std::io::Error::other("Node.js: cannot access file loader callback.")))
});
};
let Ok(path) = env.create_string(path.display().to_string().as_str()) else {
return Box::pin(async {
Some(Err(std::io::Error::other(
"Node.js: wrong argunemt for callback file_loader.",
)))
});
};
let result = match callback.call(None, &[path]) {
Ok(result) => result,
Err(err) => {
return Box::pin(
async move { Some(Err(std::io::Error::other(err.to_string()))) },
);
}
};
let js_string: napi::Result<JsString> = result.try_into();
let Ok(js_string) = js_string else {
return Box::pin(async {
Some(Err(std::io::Error::other(
"Node.js: cannot read return value of file loader callback as js string.",
)))
});
};
let Ok(utf8_string) = js_string.into_utf8() else {
return Box::pin(async {
Some(Err(std::io::Error::other(
"Node.js: cannot convert return value of file loader callback into utf8.",
)))
});
};
if let Ok(str) = utf8_string.as_str() {
let string = str.to_string();
return Box::pin(async { Some(Ok(string)) });
};
Box::pin(async {
Some(Err(std::io::Error::other(
"Node.js: cannot convert return value of file loader callback into string.",
)))
})
});
Ok(())
}
/// Compile a .slint file into a ComponentDefinition
///
/// Returns the compiled `ComponentDefinition` if there were no errors.

View file

@ -247,6 +247,11 @@ export interface LoadFileOptions {
* Sets library paths used for looking up `@library` imports to the specified map of library names to paths.
*/
libraryPaths?: Record<string, string>;
/**
* @hidden
*/
fileLoader?: (path: string) => string;
}
type LoadData =
@ -285,6 +290,9 @@ function loadSlint(loadData: LoadData): Object {
if (typeof options.libraryPaths !== "undefined") {
compiler.libraryPaths = options.libraryPaths;
}
if (typeof options.fileLoader !== "undefined") {
compiler.fileLoader = options.fileLoader;
}
}
const definitions =