Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up file loader callback #6540

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 42 additions & 46 deletions api/node/rust/interpreter/component_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,60 +169,56 @@ impl JsComponentCompiler {

#[napi(setter)]
pub fn set_file_loader(&mut self, env: Env, callback: JsFunction) -> napi::Result<()> {
let function_ref = RefCountedReference::new(&env, callback)?;
let function_ref = std::rc::Rc::new(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(
let path = PathBuf::from(path);
let function_ref = function_ref.clone();
Box::pin({
async move {
let Ok(callback) = function_ref.get::<JsFunction>() else {
return 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 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 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 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(
let Ok(utf8_string) = js_string.into_utf8() else {
return 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();
if let Ok(str) = utf8_string.as_str() {
let string = str.to_string();

return Box::pin(async { Some(Ok(string)) });
};
return Some(Ok(string));
};

Box::pin(async {
Some(Err(std::io::Error::other(
"Node.js: cannot convert return value of file loader callback into string.",
)))
Some(Err(std::io::Error::other(
"Node.js: cannot convert return value of file loader callback into string.",
)))
}
})
});

Expand Down
Loading