1
1
use neon:: prelude:: * ;
2
+ use neon:: types:: buffer:: TypedArray as _;
2
3
use serde:: { Deserialize , Serialize } ;
3
4
use std:: cell:: RefCell ;
4
5
use std:: sync:: Mutex ;
@@ -17,8 +18,8 @@ mod json_ffi {
17
18
/// Call `JSON.stringify` to convert the input to a `JsString`, then call serde_json to parse
18
19
/// it to an instance of a native Rust type
19
20
pub fn from_js < ' a , C : Context < ' a > , T : DeserializeOwned > ( cx : & mut C , input : Handle < JsValue > ) -> NeonResult < T > {
20
- let json: Handle < JsObject > = cx. global ( ) . get ( cx, "JSON" ) ?. downcast :: < JsObject , _ > ( cx ) . or_throw ( cx ) ? ;
21
- let json_stringify: Handle < JsFunction > = json. get ( cx, "stringify" ) ?. downcast :: < JsFunction , _ > ( cx ) . or_throw ( cx ) ? ;
21
+ let json: Handle < JsObject > = cx. global ( ) . get ( cx, "JSON" ) ?;
22
+ let json_stringify: Handle < JsFunction > = json. get ( cx, "stringify" ) ?;
22
23
23
24
let undefined = JsUndefined :: new ( cx) ;
24
25
let js_string = json_stringify
@@ -36,12 +37,13 @@ mod json_ffi {
36
37
pub fn to_js < ' a , C : Context < ' a > , T : serde:: Serialize > ( cx : & mut C , input : & T ) -> JsResult < ' a , JsValue > {
37
38
let input_handle = JsString :: new ( cx, serde_json:: to_string ( & input) . unwrap ( ) ) ;
38
39
39
- let json: Handle < JsObject > = cx. global ( ) . get ( cx, "JSON" ) ?. downcast :: < JsObject , _ > ( cx ) . or_throw ( cx ) ? ;
40
- let json_parse: Handle < JsFunction > = json. get ( cx, "parse" ) ?. downcast :: < JsFunction , _ > ( cx ) . or_throw ( cx ) ? ;
40
+ let json: Handle < JsObject > = cx. global ( ) . get ( cx, "JSON" ) ?;
41
+ let json_parse: Handle < JsFunction > = json. get ( cx, "parse" ) ?;
41
42
42
- let undefined = JsUndefined :: new ( cx) ;
43
43
json_parse
44
- . call ( cx, undefined, [ input_handle] )
44
+ . call_with ( cx)
45
+ . arg ( input_handle)
46
+ . apply ( cx)
45
47
}
46
48
}
47
49
@@ -237,12 +239,9 @@ fn engine_serialize_raw(mut cx: FunctionContext) -> JsResult<JsArrayBuffer> {
237
239
} ;
238
240
239
241
// initialise new Array Buffer in the JS context
240
- let mut buffer = JsArrayBuffer :: new ( & mut cx, serialized. len ( ) as u32 ) ?;
242
+ let mut buffer = JsArrayBuffer :: new ( & mut cx, serialized. len ( ) ) ?;
241
243
// copy data from Rust buffer to JS Array Buffer
242
- cx. borrow_mut ( & mut buffer, |bufferdata| {
243
- let slice = bufferdata. as_mut_slice :: < u8 > ( ) ;
244
- slice. copy_from_slice ( & serialized)
245
- } ) ;
244
+ buffer. as_mut_slice ( & mut cx) . copy_from_slice ( & serialized) ;
246
245
247
246
Ok ( buffer)
248
247
}
@@ -252,10 +251,7 @@ fn engine_deserialize(mut cx: FunctionContext) -> JsResult<JsNull> {
252
251
let serialized_handle = cx. argument :: < JsArrayBuffer > ( 1 ) ?;
253
252
254
253
if let Ok ( mut engine) = this. 0 . lock ( ) {
255
- let _result = cx. borrow ( & serialized_handle, |bufferdata| {
256
- let slice = bufferdata. as_slice :: < u8 > ( ) ;
257
- engine. deserialize ( & slice)
258
- } ) . unwrap ( ) ;
254
+ let _result = engine. deserialize ( & serialized_handle. as_slice ( & mut cx) ) ;
259
255
}
260
256
261
257
Ok ( JsNull :: new ( & mut cx) )
0 commit comments