Skip to content

Commit eb43562

Browse files
committed
review(neon): Add test to try_catch
1 parent a68f115 commit eb43562

File tree

6 files changed

+28
-0
lines changed

6 files changed

+28
-0
lines changed

test/dynamic/lib/functions.js

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ describe('JsFunction', function() {
4949
assert.equal(addon.call_and_catch(() => { return 42 }), 42);
5050
});
5151

52+
it('can return Rust type from cx.try_catch', function() {
53+
const n = Math.random();
54+
assert.strictEqual(addon.get_number_or_default(n), n);
55+
assert.strictEqual(addon.get_number_or_default(), 0);
56+
});
57+
5258
it('propagates a panic with cx.try_catch', function() {
5359
assert.throws(function() {
5460
addon.panic_and_catch();

test/dynamic/native/src/js/functions.rs

+7
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ pub fn call_and_catch(mut cx: FunctionContext) -> JsResult<JsValue> {
118118
}).unwrap_or_else(|err| err))
119119
}
120120

121+
pub fn get_number_or_default(mut cx: FunctionContext) -> JsResult<JsNumber> {
122+
let n = cx.try_catch(|cx| Ok(cx.argument::<JsNumber>(0)?.value()))
123+
.unwrap_or(0.0);
124+
125+
Ok(cx.number(n))
126+
}
127+
121128
pub fn panic_and_catch(mut cx: FunctionContext) -> JsResult<JsValue> {
122129
Ok(cx.try_catch(|_| { panic!("oh no") })
123130
.unwrap_or_else(|err| err))

test/dynamic/native/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
7474
cx.export_function("panic_after_throw", panic_after_throw)?;
7575
cx.export_function("throw_and_catch", throw_and_catch)?;
7676
cx.export_function("call_and_catch", call_and_catch)?;
77+
cx.export_function("get_number_or_default", get_number_or_default)?;
7778
cx.export_function("panic_and_catch", panic_and_catch)?;
7879
cx.export_function("unexpected_throw_and_catch", unexpected_throw_and_catch)?;
7980
cx.export_function("downcast_error", downcast_error)?;

test/napi/lib/functions.js

+6
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ describe('JsFunction', function() {
104104
assert.equal(addon.call_and_catch(() => { return 42 }), 42);
105105
});
106106

107+
it('can return Rust type from cx.try_catch', function() {
108+
const n = Math.random();
109+
assert.strictEqual(addon.get_number_or_default(n), n);
110+
assert.strictEqual(addon.get_number_or_default(), 0);
111+
});
112+
107113
it('distinguishes calls from constructs', function() {
108114
assert.equal(addon.is_construct.call({}).wasConstructed, false);
109115
assert.equal((new addon.is_construct()).wasConstructed, true);

test/napi/src/js/functions.rs

+7
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ pub fn call_and_catch(mut cx: FunctionContext) -> JsResult<JsValue> {
119119
}).unwrap_or_else(|err| err))
120120
}
121121

122+
pub fn get_number_or_default(mut cx: FunctionContext) -> JsResult<JsNumber> {
123+
let n = cx.try_catch(|cx| Ok(cx.argument::<JsNumber>(0)?.value(cx)))
124+
.unwrap_or(0.0);
125+
126+
Ok(cx.number(n))
127+
}
128+
122129
pub fn is_construct(mut cx: FunctionContext) -> JsResult<JsObject> {
123130
let this = cx.this();
124131
let construct = match cx.kind() {

test/napi/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
168168

169169
cx.export_function("throw_and_catch", throw_and_catch)?;
170170
cx.export_function("call_and_catch", call_and_catch)?;
171+
cx.export_function("get_number_or_default", get_number_or_default)?;
171172
cx.export_function("is_construct", is_construct)?;
172173

173174
fn call_get_own_property_names(mut cx: FunctionContext) -> JsResult<JsArray> {

0 commit comments

Comments
 (0)