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): Extractors #997

Closed
wants to merge 1 commit into from
Closed
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
56 changes: 56 additions & 0 deletions crates/neon/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub use crate::types::buffer::lock::Lock;

use crate::{
event::TaskBuilder,
extract::FromArgs,
handle::Handle,
object::Object,
result::{JsResult, NeonResult, Throw},
Expand Down Expand Up @@ -212,6 +213,36 @@ impl CallbackInfo<'_> {
local
}
}

pub(crate) fn argv_exact<'b, C: Context<'b>, const N: usize>(
&self,
cx: &mut C,
) -> [Handle<'b, JsValue>; N] {
use std::ptr;

let mut argv = [JsValue::new_internal(ptr::null_mut()); N];
let mut argc = argv.len();

// # Safety
// * Node-API fills empty slots with `undefined
// * `Handle` and `JsValue` are transparent wrappers around a raw pointer
unsafe {
assert_eq!(
sys::get_cb_info(
cx.env().to_raw(),
self.info,
&mut argc,
argv.as_mut_ptr().cast(),
ptr::null_mut(),
ptr::null_mut(),
),
sys::Status::Ok,
);
}

// Empty values will be filled with `undefined`
argv
}
}

/// Indicates whether a function was called with `new`.
Expand Down Expand Up @@ -691,6 +722,27 @@ impl<'a> FunctionContext<'a> {
}
}

/// Extract Rust data from the JavaScript arguments.
///
/// This is frequently more efficient and ergonomic than getting arguments
/// individually. See the [`extract`](crate::extract) module documentation
/// for more examples.
///
/// ```
/// # use neon::prelude::*;
/// fn add(mut cx: FunctionContext) -> JsResult<JsNumber> {
/// let (a, b): (f64, f64) = cx.args()?;
///
/// Ok(cx.number(a + b))
/// }
/// ```
pub fn args<T>(&mut self) -> NeonResult<T>
where
T: FromArgs<'a>,
{
T::from_args(self)
}

/// Produces a handle to the `this`-binding and attempts to downcast as a specific type.
/// Equivalent to calling `cx.this_value().downcast_or_throw(&mut cx)`.
///
Expand All @@ -703,6 +755,10 @@ impl<'a> FunctionContext<'a> {
pub fn this_value(&mut self) -> Handle<'a, JsValue> {
JsValue::new_internal(self.info.this(self))
}

pub(crate) fn argv<const N: usize>(&mut self) -> [Handle<'a, JsValue>; N] {
self.info.argv_exact(self)
}
}

impl<'a> ContextInternal<'a> for FunctionContext<'a> {
Expand Down
Loading
Loading