File tree 6 files changed +28
-0
lines changed
6 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,12 @@ describe('JsFunction', function() {
49
49
assert . equal ( addon . call_and_catch ( ( ) => { return 42 } ) , 42 ) ;
50
50
} ) ;
51
51
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
+
52
58
it ( 'propagates a panic with cx.try_catch' , function ( ) {
53
59
assert . throws ( function ( ) {
54
60
addon . panic_and_catch ( ) ;
Original file line number Diff line number Diff line change @@ -118,6 +118,13 @@ pub fn call_and_catch(mut cx: FunctionContext) -> JsResult<JsValue> {
118
118
} ) . unwrap_or_else ( |err| err) )
119
119
}
120
120
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
+
121
128
pub fn panic_and_catch ( mut cx : FunctionContext ) -> JsResult < JsValue > {
122
129
Ok ( cx. try_catch ( |_| { panic ! ( "oh no" ) } )
123
130
. unwrap_or_else ( |err| err) )
Original file line number Diff line number Diff line change @@ -74,6 +74,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
74
74
cx. export_function ( "panic_after_throw" , panic_after_throw) ?;
75
75
cx. export_function ( "throw_and_catch" , throw_and_catch) ?;
76
76
cx. export_function ( "call_and_catch" , call_and_catch) ?;
77
+ cx. export_function ( "get_number_or_default" , get_number_or_default) ?;
77
78
cx. export_function ( "panic_and_catch" , panic_and_catch) ?;
78
79
cx. export_function ( "unexpected_throw_and_catch" , unexpected_throw_and_catch) ?;
79
80
cx. export_function ( "downcast_error" , downcast_error) ?;
Original file line number Diff line number Diff line change @@ -104,6 +104,12 @@ describe('JsFunction', function() {
104
104
assert . equal ( addon . call_and_catch ( ( ) => { return 42 } ) , 42 ) ;
105
105
} ) ;
106
106
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
+
107
113
it ( 'distinguishes calls from constructs' , function ( ) {
108
114
assert . equal ( addon . is_construct . call ( { } ) . wasConstructed , false ) ;
109
115
assert . equal ( ( new addon . is_construct ( ) ) . wasConstructed , true ) ;
Original file line number Diff line number Diff line change @@ -119,6 +119,13 @@ pub fn call_and_catch(mut cx: FunctionContext) -> JsResult<JsValue> {
119
119
} ) . unwrap_or_else ( |err| err) )
120
120
}
121
121
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
+
122
129
pub fn is_construct ( mut cx : FunctionContext ) -> JsResult < JsObject > {
123
130
let this = cx. this ( ) ;
124
131
let construct = match cx. kind ( ) {
Original file line number Diff line number Diff line change @@ -168,6 +168,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
168
168
169
169
cx. export_function ( "throw_and_catch" , throw_and_catch) ?;
170
170
cx. export_function ( "call_and_catch" , call_and_catch) ?;
171
+ cx. export_function ( "get_number_or_default" , get_number_or_default) ?;
171
172
cx. export_function ( "is_construct" , is_construct) ?;
172
173
173
174
fn call_get_own_property_names ( mut cx : FunctionContext ) -> JsResult < JsArray > {
You can’t perform that action at this time.
0 commit comments