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

feat(neon-macros): Allow Cx in exported functions #1068

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion crates/neon-macros/src/export/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn has_context_arg(meta: &meta::Meta, sig: &syn::Signature) -> syn::Result<bool>
};

// First argument isn't context
if first != "FunctionContext" {
if first != "FunctionContext" && first != "Cx" {
return Ok(false);
}

Expand Down
11 changes: 6 additions & 5 deletions crates/neon/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,16 @@ pub use neon_macros::main;
/// More complex functions may need to interact directly with the JavaScript runtime,
/// for example with [`Context`](crate::context::Context) or handles to JavaScript values.
///
/// Functions may optionally include a [`FunctionContext`](crate::context::FunctionContext) argument. Note
/// that unlike functions created with [`JsFunction::new`](crate::types::JsFunction), exported function
/// receive a borrowed context and may require explicit lifetimes.
/// Functions may optionally include a [`Cx`](crate::context::Cx) or
/// [`FunctionContext`](crate::context::FunctionContext) argument. Note that unlike functions
/// created with [`JsFunction::new`](crate::types::JsFunction), exported function receive a borrowed
/// context and may require explicit lifetimes.
///
/// ```
/// # use neon::prelude::*;
/// #[neon::export]
/// fn add<'cx>(
/// cx: &mut FunctionContext<'cx>,
/// cx: &mut Cx<'cx>,
/// a: Handle<JsNumber>,
/// b: Handle<JsNumber>,
/// ) -> JsResult<'cx, JsNumber> {
Expand All @@ -180,7 +181,7 @@ pub use neon_macros::main;
/// #### `context`
///
/// The `#[neon::export]` macro looks checks if the first argument has a type of
/// `&mut FunctionContext` to determine if the [`Context`](crate::context::Context)
/// `&mut Cx` or `&mut FunctionContext` to determine if the [`Context`](crate::context::Context)
/// should be passed to the function.
///
/// If the type has been renamed when importing, the `context` attribute can be
Expand Down
4 changes: 4 additions & 0 deletions test/napi/lib/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,8 @@ function functions() {

assert.ok(duration < maxExpected);
});

it("can use generic Cx in exported functions", () => {
assert.strictEqual(addon.number_with_cx(42), 42);
});
}
5 changes: 5 additions & 0 deletions test/napi/src/js/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@ fn sleep_task(ms: f64) {

thread::sleep(Duration::from_millis(ms as u64));
}

#[neon::export]
fn number_with_cx<'cx>(cx: &mut Cx<'cx>, n: f64) -> Handle<'cx, JsNumber> {
cx.number(n)
}