Skip to content

Commit

Permalink
Merge pull request #1068 from neon-bindings/kv/allow-cx-in-export
Browse files Browse the repository at this point in the history
feat(neon-macros): Allow `Cx` in exported functions
  • Loading branch information
kjvalencik authored Sep 20, 2024
2 parents ffb3f99 + 75d5e00 commit 2083aed
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
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 @@ -167,7 +167,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 @@ -210,15 +210,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 @@ -237,7 +238,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)
}

0 comments on commit 2083aed

Please sign in to comment.