diff --git a/src/context/mod.rs b/src/context/mod.rs index 17a8d7844..154f5d3ac 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -149,9 +149,9 @@ pub trait Context<'a>: ContextInternal<'a> { /// /// ```no_run /// # use neon::prelude::*; - /// # fn my_neon_function(mut cx: FunctionContext) -> JsResult { - /// let mut b: &JsArrayBuffer = cx.argument(0)?; - /// cx.borrow_mut(&b, |data| { + /// # fn my_neon_function(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { + /// let b: &JsArrayBuffer = cx.argument(0)?; + /// cx.borrow_mut(b, |data| { /// let slice = data.as_mut_slice::(); /// slice[0] += 1; /// }); diff --git a/src/prelude.rs b/src/prelude.rs index 59bfaf816..e0a570e94 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,6 +1,6 @@ //! A convenience module that re-exports the most commonly-used Neon APIs. -pub use types::{JsBuffer, JsArrayBuffer, BinaryData, JsError, JsUndefined, JsNull, JsBoolean, JsString, JsNumber, JsObject, JsArray, JsFunction, Value}; +pub use types::{JsBuffer, JsArrayBuffer, BinaryData, JsError, JsUndefined, JsNull, JsBoolean, JsString, JsNumber, JsObject, JsArray, JsFunction, JsValue, Value}; pub use object::{Object, Class}; pub use borrow::{Borrow, BorrowMut}; pub use context::{CallKind, Context, ModuleContext, ExecuteContext, ComputeContext, CallContext, FunctionContext, MethodContext, TaskContext}; diff --git a/src/types/binary.rs b/src/types/binary.rs index 01f445c28..fd451945d 100644 --- a/src/types/binary.rs +++ b/src/types/binary.rs @@ -119,9 +119,9 @@ impl<'a> BinaryData<'a> { /// /// ```no_run /// # use neon::prelude::*; - /// # fn get_x_and_y(mut cx: FunctionContext) -> JsResult { - /// let b: Handle = cx.argument(0)?; - /// let (x, y) = cx.borrow(&b, |data| { + /// # fn get_x_and_y(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { + /// let b: &JsArrayBuffer = cx.argument(0)?; + /// let (x, y) = cx.borrow(b, |data| { /// let slice = data.as_slice::(); /// (slice[0], slice[1]) /// }); @@ -141,9 +141,9 @@ impl<'a> BinaryData<'a> { /// /// ```no_run /// # use neon::prelude::*; - /// # fn modify_buffer(mut cx: FunctionContext) -> JsResult { - /// let mut b: Handle = cx.argument(0)?; - /// cx.borrow_mut(&mut b, |data| { + /// # fn modify_buffer(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { + /// let b: &JsArrayBuffer = cx.argument(0)?; + /// cx.borrow_mut(b, |data| { /// let slice = data.as_mut_slice::(); /// slice[0] /= 2.0; /// slice[1] *= 2.0; diff --git a/src/types/mod.rs b/src/types/mod.rs index f42718eba..af6f4888c 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -63,8 +63,8 @@ pub trait Value: ValueInternal { /// /// ```no_run /// # use neon::prelude::*; - /// # fn my_neon_function(mut cx: FunctionContext) -> JsResult { - /// let v: Handle = cx.number(17).upcast(); + /// # fn my_neon_function(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { + /// let v: &JsValue = cx.number(17).upcast(); /// v.is_a::(); // false /// v.is_a::(); // true /// v.is_a::(); // true @@ -287,7 +287,7 @@ impl JsString { } } - pub fn value(self) -> String { + pub fn value(&self) -> String { unsafe { let capacity = neon_runtime::string::utf8_len(self.to_raw()); let mut buffer: Vec = Vec::with_capacity(capacity as usize); diff --git a/test/dynamic/native/src/js/arrays.rs b/test/dynamic/native/src/js/arrays.rs index 6bbcac7d8..ba3e03463 100644 --- a/test/dynamic/native/src/js/arrays.rs +++ b/test/dynamic/native/src/js/arrays.rs @@ -1,18 +1,18 @@ use neon::prelude::*; -pub fn return_js_array(mut cx: FunctionContext) -> JsResult { +pub fn return_js_array(mut cx: FunctionContext) -> NeonResult<&JsArray> { Ok(cx.empty_array()) } -pub fn return_js_array_with_number(mut cx: FunctionContext) -> JsResult { - let array: Handle = JsArray::new(&mut cx, 1); +pub fn return_js_array_with_number(mut cx: FunctionContext) -> NeonResult<&JsArray> { + let array: &JsArray = JsArray::new(&mut cx, 1); let n = cx.number(9000.0); array.set(&mut cx, 0, n)?; Ok(array) } -pub fn return_js_array_with_string(mut cx: FunctionContext) -> JsResult { - let array: Handle = JsArray::new(&mut cx, 1); +pub fn return_js_array_with_string(mut cx: FunctionContext) -> NeonResult<&JsArray> { + let array: &JsArray = JsArray::new(&mut cx, 1); let s = cx.string("hello node"); array.set(&mut cx, 0, s)?; Ok(array) diff --git a/test/dynamic/native/src/js/classes.rs b/test/dynamic/native/src/js/classes.rs index 54cc839fa..54584f494 100644 --- a/test/dynamic/native/src/js/classes.rs +++ b/test/dynamic/native/src/js/classes.rs @@ -33,9 +33,9 @@ declare_types! { pub class JsUser for User { init(mut cx) { let id = cx.argument::(0)?; - let first_name: Handle = cx.argument::(1)?; - let last_name: Handle = cx.argument::(2)?; - let email: Handle = cx.argument::(3)?; + let first_name: &JsString = cx.argument::(1)?; + let last_name: &JsString = cx.argument::(2)?; + let email: &JsString = cx.argument::(3)?; Ok(User { id: id.value() as i32, diff --git a/test/dynamic/native/src/js/functions.rs b/test/dynamic/native/src/js/functions.rs index 351c6f400..f5a47d213 100644 --- a/test/dynamic/native/src/js/functions.rs +++ b/test/dynamic/native/src/js/functions.rs @@ -1,66 +1,67 @@ use neon::prelude::*; use neon::object::This; +use neon::result::NeonResultExt; -fn add1(mut cx: FunctionContext) -> JsResult { +fn add1(mut cx: FunctionContext) -> NeonResult<&JsNumber> { let x = cx.argument::(0)?.value(); Ok(cx.number(x + 1.0)) } -pub fn return_js_function(mut cx: FunctionContext) -> JsResult { +pub fn return_js_function(mut cx: FunctionContext) -> NeonResult<&JsFunction> { JsFunction::new(&mut cx, add1) } -pub fn call_js_function(mut cx: FunctionContext) -> JsResult { +pub fn call_js_function(mut cx: FunctionContext) -> NeonResult<&JsNumber> { let f = cx.argument::(0)?; - let args: Vec> = vec![cx.number(16.0)]; + let args: Vec<&JsNumber> = vec![cx.number(16.0)]; let null = cx.null(); f.call(&mut cx, null, args)?.downcast::().or_throw(&mut cx) } -pub fn construct_js_function(mut cx: FunctionContext) -> JsResult { +pub fn construct_js_function(mut cx: FunctionContext) -> NeonResult<&JsNumber> { let f = cx.argument::(0)?; let zero = cx.number(0.0); let o = f.construct(&mut cx, vec![zero])?; let get_utc_full_year_method = o.get(&mut cx, "getUTCFullYear")?.downcast::().or_throw(&mut cx)?; - let args: Vec> = vec![]; + let args: Vec<&JsValue> = vec![]; get_utc_full_year_method.call(&mut cx, o.upcast::(), args)?.downcast::().or_throw(&mut cx) } trait CheckArgument<'a> { - fn check_argument(&mut self, i: i32) -> JsResult<'a, V>; + fn check_argument(&mut self, i: i32) -> NeonResult<&'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: i32) -> NeonResult<&'a V> { self.argument::(i) } } -pub fn check_string_and_number(mut cx: FunctionContext) -> JsResult { +pub fn check_string_and_number(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { cx.check_argument::(0)?; cx.check_argument::(1)?; Ok(cx.undefined()) } -pub fn panic(_: FunctionContext) -> JsResult { +pub fn panic(_: FunctionContext) -> NeonResult<&JsUndefined> { panic!("zomg") } -pub fn panic_after_throw(mut cx: FunctionContext) -> JsResult { +pub fn panic_after_throw(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { cx.throw_range_error::<_, ()>("entering throw state with a RangeError").unwrap_err(); panic!("this should override the RangeError") } -pub fn num_arguments(mut cx: FunctionContext) -> JsResult { +pub fn num_arguments(mut cx: FunctionContext) -> NeonResult<&JsNumber> { let n = cx.len(); Ok(cx.number(n)) } -pub fn return_this(mut cx: FunctionContext) -> JsResult { +pub fn return_this(mut cx: FunctionContext) -> NeonResult<&JsValue> { Ok(cx.this().upcast()) } -pub fn require_object_this(mut cx: FunctionContext) -> JsResult { +pub fn require_object_this(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { let this = cx.this(); let this = this.downcast::().or_throw(&mut cx)?; let t = cx.boolean(true); @@ -68,17 +69,17 @@ pub fn require_object_this(mut cx: FunctionContext) -> JsResult { Ok(cx.undefined()) } -pub fn is_argument_zero_some(mut cx: FunctionContext) -> JsResult { +pub fn is_argument_zero_some(mut cx: FunctionContext) -> NeonResult<&JsBoolean> { let b = cx.argument_opt(0).is_some(); Ok(cx.boolean(b)) } -pub fn require_argument_zero_string(mut cx: FunctionContext) -> JsResult { +pub fn require_argument_zero_string(mut cx: FunctionContext) -> NeonResult<&JsString> { let s = cx.argument(0)?; Ok(s) } -pub fn execute_scoped(mut cx: FunctionContext) -> JsResult { +pub fn execute_scoped(mut cx: FunctionContext) -> NeonResult<&JsNumber> { let mut i = 0; for _ in 1..100 { cx.execute_scoped(|mut cx| { @@ -89,7 +90,7 @@ pub fn execute_scoped(mut cx: FunctionContext) -> JsResult { Ok(cx.number(i)) } -pub fn compute_scoped(mut cx: FunctionContext) -> JsResult { +pub fn compute_scoped(mut cx: FunctionContext) -> NeonResult<&JsNumber> { let mut i = cx.number(0); for _ in 1..100 { i = cx.compute_scoped(|mut cx| { diff --git a/test/dynamic/native/src/js/numbers.rs b/test/dynamic/native/src/js/numbers.rs index 9da7c79eb..26b9ff976 100644 --- a/test/dynamic/native/src/js/numbers.rs +++ b/test/dynamic/native/src/js/numbers.rs @@ -1,41 +1,41 @@ use neon::prelude::*; -pub fn return_js_number(mut cx: FunctionContext) -> JsResult { +pub fn return_js_number(mut cx: FunctionContext) -> NeonResult<&JsNumber> { Ok(cx.number(9000_f64)) } -pub fn return_large_js_number(mut cx: FunctionContext) -> JsResult { +pub fn return_large_js_number(mut cx: FunctionContext) -> NeonResult<&JsNumber> { Ok(cx.number(4294967296_f64)) } -pub fn return_negative_js_number(mut cx: FunctionContext) -> JsResult { +pub fn return_negative_js_number(mut cx: FunctionContext) -> NeonResult<&JsNumber> { Ok(cx.number(-9000_f64)) } -pub fn return_float_js_number(mut cx: FunctionContext) -> JsResult { +pub fn return_float_js_number(mut cx: FunctionContext) -> NeonResult<&JsNumber> { Ok(cx.number(1.4747_f64)) } -pub fn return_negative_float_js_number(mut cx: FunctionContext) -> JsResult { +pub fn return_negative_float_js_number(mut cx: FunctionContext) -> NeonResult<&JsNumber> { Ok(cx.number(-1.4747_f64)) } -pub fn accept_and_return_js_number(mut cx: FunctionContext) -> JsResult { - let number: Handle = cx.argument(0)?; +pub fn accept_and_return_js_number(mut cx: FunctionContext) -> NeonResult<&JsNumber> { + let number: &JsNumber = cx.argument(0)?; Ok(number) } -pub fn accept_and_return_large_js_number(mut cx: FunctionContext) -> JsResult { - let number: Handle = cx.argument(0)?; +pub fn accept_and_return_large_js_number(mut cx: FunctionContext) -> NeonResult<&JsNumber> { + let number: &JsNumber = cx.argument(0)?; Ok(number) } -pub fn accept_and_return_float_js_number(mut cx: FunctionContext) -> JsResult { - let number: Handle = cx.argument(0)?; +pub fn accept_and_return_float_js_number(mut cx: FunctionContext) -> NeonResult<&JsNumber> { + let number: &JsNumber = cx.argument(0)?; Ok(number) } -pub fn accept_and_return_negative_js_number(mut cx: FunctionContext) -> JsResult { - let number: Handle = cx.argument(0)?; +pub fn accept_and_return_negative_js_number(mut cx: FunctionContext) -> NeonResult<&JsNumber> { + let number: &JsNumber = cx.argument(0)?; Ok(number) } diff --git a/test/dynamic/native/src/js/objects.rs b/test/dynamic/native/src/js/objects.rs index e6a2fc436..6a7cd73c8 100644 --- a/test/dynamic/native/src/js/objects.rs +++ b/test/dynamic/native/src/js/objects.rs @@ -1,15 +1,15 @@ use neon::prelude::*; -pub fn return_js_global_object(mut cx: FunctionContext) -> JsResult { +pub fn return_js_global_object(mut cx: FunctionContext) -> NeonResult<&JsObject> { Ok(cx.global()) } -pub fn return_js_object(mut cx: FunctionContext) -> JsResult { +pub fn return_js_object(mut cx: FunctionContext) -> NeonResult<&JsObject> { Ok(cx.empty_object()) } -pub fn return_js_object_with_mixed_content(mut cx: FunctionContext) -> JsResult { - let js_object: Handle = cx.empty_object(); +pub fn return_js_object_with_mixed_content(mut cx: FunctionContext) -> NeonResult<&JsObject> { + let js_object: &JsObject = cx.empty_object(); let n = cx.number(9000.0); js_object.set(&mut cx, "number", n)?; let s = cx.string("hello node"); @@ -17,27 +17,27 @@ pub fn return_js_object_with_mixed_content(mut cx: FunctionContext) -> JsResult< Ok(js_object) } -pub fn return_js_object_with_number(mut cx: FunctionContext) -> JsResult { - let js_object: Handle = cx.empty_object(); +pub fn return_js_object_with_number(mut cx: FunctionContext) -> NeonResult<&JsObject> { + let js_object: &JsObject = cx.empty_object(); let n = cx.number(9000.0); js_object.set(&mut cx, "number", n)?; Ok(js_object) } -pub fn return_js_object_with_string(mut cx: FunctionContext) -> JsResult { - let js_object: Handle = cx.empty_object(); +pub fn return_js_object_with_string(mut cx: FunctionContext) -> NeonResult<&JsObject> { + let js_object: &JsObject = cx.empty_object(); let s = cx.string("hello node"); js_object.set(&mut cx, "string", s)?; Ok(js_object) } -pub fn return_array_buffer(mut cx: FunctionContext) -> JsResult { - let b: Handle = cx.array_buffer(16)?; +pub fn return_array_buffer(mut cx: FunctionContext) -> NeonResult<&JsArrayBuffer> { + let b: &JsArrayBuffer = cx.array_buffer(16)?; Ok(b) } -pub fn read_array_buffer_with_lock(mut cx: FunctionContext) -> JsResult { - let b: Handle = cx.argument(0)?; +pub fn read_array_buffer_with_lock(mut cx: FunctionContext) -> NeonResult<&JsNumber> { + let b: &JsArrayBuffer = cx.argument(0)?; let i = cx.argument::(1)?.value() as u32 as usize; let x = { let guard = cx.lock(); @@ -48,15 +48,15 @@ pub fn read_array_buffer_with_lock(mut cx: FunctionContext) -> JsResult JsResult { - let b: Handle = cx.argument(0)?; +pub fn read_array_buffer_with_borrow(mut cx: FunctionContext) -> NeonResult<&JsNumber> { + let b: &JsArrayBuffer = cx.argument(0)?; let i = cx.argument::(1)?.value() as u32 as usize; - let x = cx.borrow(&b, |data| { data.as_slice::()[i] }); + let x = cx.borrow(b, |data| { data.as_slice::()[i] }); Ok(cx.number(x)) } -pub fn write_array_buffer_with_lock(mut cx: FunctionContext) -> JsResult { - let mut b: Handle = cx.argument(0)?; +pub fn write_array_buffer_with_lock(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { + let b: &JsArrayBuffer = cx.argument(0)?; let i = cx.argument::(1)?.value() as u32 as usize; let x = cx.argument::(2)?.value() as u32; { @@ -68,16 +68,16 @@ pub fn write_array_buffer_with_lock(mut cx: FunctionContext) -> JsResult JsResult { - let mut b: Handle = cx.argument(0)?; +pub fn write_array_buffer_with_borrow_mut(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { + let b: &JsArrayBuffer = cx.argument(0)?; let i = cx.argument::(1)?.value() as u32 as usize; let x = cx.argument::(2)?.value() as u32; - cx.borrow_mut(&mut b, |data| { data.as_mut_slice::()[i] = x; }); + cx.borrow_mut(b, |data| { data.as_mut_slice::()[i] = x; }); Ok(cx.undefined()) } -pub fn read_buffer_with_lock(mut cx: FunctionContext) -> JsResult { - let b: Handle = cx.argument(0)?; +pub fn read_buffer_with_lock(mut cx: FunctionContext) -> NeonResult<&JsNumber> { + let b: &JsBuffer = cx.argument(0)?; let i = cx.argument::(1)?.value() as u32 as usize; let x = { let guard = cx.lock(); @@ -88,15 +88,15 @@ pub fn read_buffer_with_lock(mut cx: FunctionContext) -> JsResult { Ok(cx.number(x)) } -pub fn read_buffer_with_borrow(mut cx: FunctionContext) -> JsResult { - let b: Handle = cx.argument(0)?; +pub fn read_buffer_with_borrow(mut cx: FunctionContext) -> NeonResult<&JsNumber> { + let b: &JsBuffer = cx.argument(0)?; let i = cx.argument::(1)?.value() as u32 as usize; - let x = cx.borrow(&b, |data| { data.as_slice::()[i] }); + let x = cx.borrow(b, |data| { data.as_slice::()[i] }); Ok(cx.number(x)) } -pub fn write_buffer_with_lock(mut cx: FunctionContext) -> JsResult { - let mut b: Handle = cx.argument(0)?; +pub fn write_buffer_with_lock(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { + let b: &JsBuffer = cx.argument(0)?; let i = cx.argument::(1)?.value() as u32 as usize; let x = cx.argument::(2)?.value() as u32; { @@ -108,10 +108,10 @@ pub fn write_buffer_with_lock(mut cx: FunctionContext) -> JsResult Ok(cx.undefined()) } -pub fn write_buffer_with_borrow_mut(mut cx: FunctionContext) -> JsResult { - let mut b: Handle = cx.argument(0)?; +pub fn write_buffer_with_borrow_mut(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { + let b: &JsBuffer = cx.argument(0)?; let i = cx.argument::(1)?.value() as u32 as usize; let x = cx.argument::(2)?.value() as u32; - cx.borrow_mut(&mut b, |data| { data.as_mut_slice::()[i] = x; }); + cx.borrow_mut(b, |data| { data.as_mut_slice::()[i] = x; }); Ok(cx.undefined()) } diff --git a/test/dynamic/native/src/js/strings.rs b/test/dynamic/native/src/js/strings.rs index 7fcb0b14b..94fb334c1 100644 --- a/test/dynamic/native/src/js/strings.rs +++ b/test/dynamic/native/src/js/strings.rs @@ -1,5 +1,5 @@ use neon::prelude::*; -pub fn return_js_string(mut cx: FunctionContext) -> JsResult { +pub fn return_js_string(mut cx: FunctionContext) -> NeonResult<&JsString> { Ok(cx.string("hello node")) } diff --git a/test/dynamic/native/src/js/tasks.rs b/test/dynamic/native/src/js/tasks.rs index 40e6b5d87..81d968ebb 100644 --- a/test/dynamic/native/src/js/tasks.rs +++ b/test/dynamic/native/src/js/tasks.rs @@ -11,12 +11,12 @@ impl Task for SuccessTask { Ok(17) } - fn complete(self, mut cx: TaskContext, result: Result) -> JsResult { + fn complete(self, mut cx: TaskContext, result: Result) -> NeonResult<&Self::JsEvent> { Ok(cx.number(result.unwrap())) } } -pub fn perform_async_task(mut cx: FunctionContext) -> JsResult { +pub fn perform_async_task(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { let f = cx.argument::(0)?; SuccessTask.schedule(f); Ok(cx.undefined()) @@ -33,12 +33,12 @@ impl Task for FailureTask { Err(format!("I am a failing task")) } - fn complete(self, mut cx: TaskContext, result: Result) -> JsResult { + fn complete(self, mut cx: TaskContext, result: Result) -> NeonResult<&Self::JsEvent> { cx.throw_error(&result.unwrap_err()) } } -pub fn perform_failing_task(mut cx: FunctionContext) -> JsResult { +pub fn perform_failing_task(mut cx: FunctionContext) -> NeonResult<&JsUndefined> { let f = cx.argument::(0)?; FailureTask.schedule(f); Ok(cx.undefined()) diff --git a/test/static/Cargo.toml b/test/static/Cargo.toml index 6a94cde14..0838c6d35 100644 --- a/test/static/Cargo.toml +++ b/test/static/Cargo.toml @@ -6,4 +6,4 @@ license = "MIT/Apache-2.0" [dependencies] neon = { path = "../../" } -compiletest_rs = "0.3.2" +compiletest_rs = "0.3.18"