diff --git a/test/dynamic/lib/functions.js b/test/dynamic/lib/functions.js index 95ab2bf48..6c37ebd8f 100644 --- a/test/dynamic/lib/functions.js +++ b/test/dynamic/lib/functions.js @@ -49,6 +49,12 @@ describe('JsFunction', function() { assert.equal(addon.call_and_catch(() => { return 42 }), 42); }); + it('can return Rust type from cx.try_catch', function() { + const n = Math.random(); + assert.strictEqual(addon.get_number_or_default(n), n); + assert.strictEqual(addon.get_number_or_default(), 0); + }); + it('propagates a panic with cx.try_catch', function() { assert.throws(function() { addon.panic_and_catch(); diff --git a/test/dynamic/native/src/js/functions.rs b/test/dynamic/native/src/js/functions.rs index 08e5f888b..68b1bb117 100644 --- a/test/dynamic/native/src/js/functions.rs +++ b/test/dynamic/native/src/js/functions.rs @@ -118,6 +118,13 @@ pub fn call_and_catch(mut cx: FunctionContext) -> JsResult { }).unwrap_or_else(|err| err)) } +pub fn get_number_or_default(mut cx: FunctionContext) -> JsResult { + let n = cx.try_catch(|cx| Ok(cx.argument::(0)?.value())) + .unwrap_or(0.0); + + Ok(cx.number(n)) +} + pub fn panic_and_catch(mut cx: FunctionContext) -> JsResult { Ok(cx.try_catch(|_| { panic!("oh no") }) .unwrap_or_else(|err| err)) diff --git a/test/dynamic/native/src/lib.rs b/test/dynamic/native/src/lib.rs index e325c38c0..0a6291537 100644 --- a/test/dynamic/native/src/lib.rs +++ b/test/dynamic/native/src/lib.rs @@ -74,6 +74,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> { cx.export_function("panic_after_throw", panic_after_throw)?; cx.export_function("throw_and_catch", throw_and_catch)?; cx.export_function("call_and_catch", call_and_catch)?; + cx.export_function("get_number_or_default", get_number_or_default)?; cx.export_function("panic_and_catch", panic_and_catch)?; cx.export_function("unexpected_throw_and_catch", unexpected_throw_and_catch)?; cx.export_function("downcast_error", downcast_error)?; diff --git a/test/napi/lib/functions.js b/test/napi/lib/functions.js index e9222ea96..6caeb387e 100644 --- a/test/napi/lib/functions.js +++ b/test/napi/lib/functions.js @@ -104,6 +104,12 @@ describe('JsFunction', function() { assert.equal(addon.call_and_catch(() => { return 42 }), 42); }); + it('can return Rust type from cx.try_catch', function() { + const n = Math.random(); + assert.strictEqual(addon.get_number_or_default(n), n); + assert.strictEqual(addon.get_number_or_default(), 0); + }); + it('distinguishes calls from constructs', function() { assert.equal(addon.is_construct.call({}).wasConstructed, false); assert.equal((new addon.is_construct()).wasConstructed, true); diff --git a/test/napi/native/src/js/functions.rs b/test/napi/native/src/js/functions.rs index a25b39fc9..219b6f5c0 100644 --- a/test/napi/native/src/js/functions.rs +++ b/test/napi/native/src/js/functions.rs @@ -119,6 +119,13 @@ pub fn call_and_catch(mut cx: FunctionContext) -> JsResult { }).unwrap_or_else(|err| err)) } +pub fn get_number_or_default(mut cx: FunctionContext) -> JsResult { + let n = cx.try_catch(|cx| Ok(cx.argument::(0)?.value(cx))) + .unwrap_or(0.0); + + Ok(cx.number(n)) +} + pub fn is_construct(mut cx: FunctionContext) -> JsResult { let this = cx.this(); let construct = match cx.kind() { diff --git a/test/napi/native/src/lib.rs b/test/napi/native/src/lib.rs index 46584d302..fda5e166f 100644 --- a/test/napi/native/src/lib.rs +++ b/test/napi/native/src/lib.rs @@ -168,6 +168,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> { cx.export_function("throw_and_catch", throw_and_catch)?; cx.export_function("call_and_catch", call_and_catch)?; + cx.export_function("get_number_or_default", get_number_or_default)?; cx.export_function("is_construct", is_construct)?; fn call_get_own_property_names(mut cx: FunctionContext) -> JsResult {