-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust-plugin): pass
PluginContext
to JS (#628)
<!-- Thank you for contributing! --> ### Description <!-- Please insert your description here and provide especially info about the "what" this PR is solving --> ### Test Plan <!-- e.g. is there anything you'd like reviewers to focus on? --> ---
- Loading branch information
Showing
10 changed files
with
80 additions
and
21 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
crates/rolldown_binding/src/options/plugin/binding_plugin_context.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use rolldown_plugin::SharedPluginContext; | ||
|
||
#[napi_derive::napi] | ||
pub struct BindingPluginContext { | ||
#[allow(dead_code)] | ||
inner: SharedPluginContext, | ||
} | ||
|
||
impl From<SharedPluginContext> for BindingPluginContext { | ||
fn from(inner: SharedPluginContext) -> Self { | ||
Self { inner } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod binding_plugin_context; | ||
mod plugin; | ||
mod plugin_adapter; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use napi::{ | ||
bindgen_prelude::{Either3, Promise}, | ||
threadsafe_function::{ThreadsafeFunction, UnknownReturnValue}, | ||
}; | ||
|
||
/// This represents a JavaScript function, whose return value is `Promise<T> | T` | ||
pub type JsAsyncCallback<Args, Ret> = ThreadsafeFunction<Args, JsAsyncCallbackReturn<Ret>, false>; | ||
|
||
// Explicitly using `UnknownReturnValue` in `Either3<Promise<T>, T, UnknownReturnValue>` to get control in rust while | ||
// receiving unknown return value from JS side. This avoids unexpected inner panics. | ||
pub type JsAsyncCallbackReturn<Ret> = Either3<Promise<Ret>, Ret, UnknownReturnValue>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
crates/rolldown_binding/src/utils/js_async_callback_ext.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::any::type_name; | ||
|
||
use futures::Future; | ||
use napi::bindgen_prelude::{Either3, FromNapiValue}; | ||
|
||
use crate::types::js_async_callback::{JsAsyncCallback, JsAsyncCallbackReturn}; | ||
|
||
pub trait JsAsyncCallbackExt<Args, Ret>: Send { | ||
fn call_async_normalized( | ||
&self, | ||
args: Args, | ||
) -> impl Future<Output = Result<Ret, napi::Error>> + Send; | ||
} | ||
|
||
impl<Args: Send, Ret: FromNapiValue + Send> JsAsyncCallbackExt<Args, Ret> | ||
for JsAsyncCallback<Args, Ret> | ||
where | ||
JsAsyncCallbackReturn<Ret>: Send + FromNapiValue, | ||
{ | ||
/// Call the hook and normalize the returned `Either3<Promise<Ret>, Ret, UnknownReturnValue>` to `Result<Ret, napi::Error>`. | ||
#[allow(clippy::manual_async_fn)] | ||
fn call_async_normalized( | ||
&self, | ||
args: Args, | ||
) -> impl Future<Output = Result<Ret, napi::Error>> + Send { | ||
async move { | ||
let ret = self.call_async(args).await?; | ||
match ret { | ||
Either3::A(p) => p.await, | ||
Either3::B(v) => Ok(v), | ||
Either3::C(_) => Err(napi::Error::new( | ||
napi::Status::InvalidArg, | ||
format!("Unknown return value. Cannot convert to `{}`.", type_name::<Ret>()), | ||
)), | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters