diff --git a/src/context/mod.rs b/src/context/mod.rs index ff08a78e0..20304c4bc 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -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) } } @@ -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) } @@ -662,7 +662,7 @@ 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> { + pub fn argument_opt(&mut self, i: usize) -> Option> { let argv = if let Some(argv) = self.arguments.as_ref() { argv } else { @@ -670,12 +670,12 @@ impl<'a, T: This> CallContext<'a, T> { 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(&mut self, i: i32) -> JsResult<'a, V> { + pub fn argument(&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"), diff --git a/src/sys/call.rs b/src/sys/call.rs index 0ee04e485..7176f27d8 100644 --- a/src/sys/call.rs +++ b/src/sys/call.rs @@ -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, @@ -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 diff --git a/test/napi/src/js/functions.rs b/test/napi/src/js/functions.rs index 709489701..78ac033aa 100644 --- a/test/napi/src/js/functions.rs +++ b/test/napi/src/js/functions.rs @@ -139,11 +139,11 @@ pub fn construct_js_function_with_overloaded_result(mut cx: FunctionContext) -> } trait CheckArgument<'a> { - fn check_argument(&mut self, i: i32) -> JsResult<'a, V>; + fn check_argument(&mut self, i: usize) -> JsResult<'a, V>; } impl<'a, T: This> CheckArgument<'a> for CallContext<'a, T> { - fn check_argument(&mut self, i: i32) -> JsResult<'a, V> { + fn check_argument(&mut self, i: usize) -> JsResult<'a, V> { self.argument::(i) } } @@ -166,7 +166,7 @@ pub fn panic_after_throw(mut cx: FunctionContext) -> JsResult { pub fn num_arguments(mut cx: FunctionContext) -> JsResult { let n = cx.len(); - Ok(cx.number(n)) + Ok(cx.number(n as i32)) } pub fn return_this(mut cx: FunctionContext) -> JsResult {