Skip to content
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
10 changes: 5 additions & 5 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl CallbackInfo<'_> {
}
}

pub fn len<'b, C: Context<'b>>(&self, cx: &C) -> i32 {
pub fn len<'b, C: Context<'b>>(&self, cx: &C) -> usize {
unsafe { sys::call::len(cx.env().to_raw(), self.info) }
}

Expand Down Expand Up @@ -652,7 +652,7 @@ impl<'a, T: This> CallContext<'a, T> {
}

/// Indicates the number of arguments that were passed to the function.
pub fn len(&self) -> i32 {
pub fn len(&self) -> usize {
self.info.len(self)
}

Expand All @@ -662,20 +662,20 @@ impl<'a, T: This> CallContext<'a, T> {
}

/// Produces the `i`th argument, or `None` if `i` is greater than or equal to `self.len()`.
pub fn argument_opt(&mut self, i: i32) -> Option<Handle<'a, JsValue>> {
pub fn argument_opt(&mut self, i: usize) -> Option<Handle<'a, JsValue>> {
let argv = if let Some(argv) = self.arguments.as_ref() {
argv
} else {
let argv = self.info.argv(self);
self.arguments.insert(argv)
};

argv.get(i as usize)
argv.get(i)
.map(|v| Handle::new_internal(JsValue::from_raw(self.env(), v)))
}

/// Produces the `i`th argument and casts it to the type `V`, or throws an exception if `i` is greater than or equal to `self.len()` or cannot be cast to `V`.
pub fn argument<V: Value>(&mut self, i: i32) -> JsResult<'a, V> {
pub fn argument<V: Value>(&mut self, i: usize) -> JsResult<'a, V> {
match self.argument_opt(i) {
Some(v) => v.downcast_or_throw(self),
None => self.throw_type_error("not enough arguments"),
Expand Down
4 changes: 2 additions & 2 deletions src/sys/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub unsafe fn this(env: Env, info: FunctionCallbackInfo, out: &mut Local) {

/// Gets the number of arguments passed to the function.
// TODO: Remove this when `CallContext` is refactored to get call info upfront.
pub unsafe fn len(env: Env, info: FunctionCallbackInfo) -> i32 {
pub unsafe fn len(env: Env, info: FunctionCallbackInfo) -> usize {
let mut argc = 0usize;
let status = napi::get_cb_info(
env,
Expand All @@ -64,7 +64,7 @@ pub unsafe fn len(env: Env, info: FunctionCallbackInfo) -> i32 {
null_mut(),
);
assert_eq!(status, napi::Status::Ok);
argc as i32
argc
}

/// Returns the function arguments for a call
Expand Down
6 changes: 3 additions & 3 deletions test/napi/src/js/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ pub fn construct_js_function_with_overloaded_result(mut cx: FunctionContext) ->
}

trait CheckArgument<'a> {
fn check_argument<V: Value>(&mut self, i: i32) -> JsResult<'a, V>;
fn check_argument<V: Value>(&mut self, i: usize) -> JsResult<'a, V>;
}

impl<'a, T: This> CheckArgument<'a> for CallContext<'a, T> {
fn check_argument<V: Value>(&mut self, i: i32) -> JsResult<'a, V> {
fn check_argument<V: Value>(&mut self, i: usize) -> JsResult<'a, V> {
self.argument::<V>(i)
}
}
Expand All @@ -166,7 +166,7 @@ pub fn panic_after_throw(mut cx: FunctionContext) -> JsResult<JsUndefined> {

pub fn num_arguments(mut cx: FunctionContext) -> JsResult<JsNumber> {
let n = cx.len();
Ok(cx.number(n))
Ok(cx.number(n as i32))
}

pub fn return_this(mut cx: FunctionContext) -> JsResult<JsValue> {
Expand Down