Skip to content

Commit be81d85

Browse files
committed
update neon to 0.10
1 parent bc39fa8 commit be81d85

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
lines changed

js/Cargo.lock

Lines changed: 29 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ crate-type = ["cdylib"]
1313
serde = { version = "1.0", features = ["derive", "rc"] }
1414
serde_json = "1.0"
1515
adblock = { path = "../", features = ["css-validation", "content-blocking", "resource-assembler"] }
16-
neon = { version = "^ 0.9", default-features = false, features = ["napi-1"] }
16+
neon = { version = "^ 0.10", default-features = false, features = ["napi-1"] }

js/src/lib.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use neon::prelude::*;
2+
use neon::types::buffer::TypedArray as _;
23
use serde::{Deserialize, Serialize};
34
use std::cell::RefCell;
45
use std::sync::Mutex;
@@ -17,8 +18,8 @@ mod json_ffi {
1718
/// Call `JSON.stringify` to convert the input to a `JsString`, then call serde_json to parse
1819
/// it to an instance of a native Rust type
1920
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")?;
2223

2324
let undefined = JsUndefined::new(cx);
2425
let js_string = json_stringify
@@ -36,12 +37,13 @@ mod json_ffi {
3637
pub fn to_js<'a, C: Context<'a>, T: serde::Serialize>(cx: &mut C, input: &T) -> JsResult<'a, JsValue> {
3738
let input_handle = JsString::new(cx, serde_json::to_string(&input).unwrap());
3839

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")?;
4142

42-
let undefined = JsUndefined::new(cx);
4343
json_parse
44-
.call(cx, undefined, [input_handle])
44+
.call_with(cx)
45+
.arg(input_handle)
46+
.apply(cx)
4547
}
4648
}
4749

@@ -237,12 +239,9 @@ fn engine_serialize_raw(mut cx: FunctionContext) -> JsResult<JsArrayBuffer> {
237239
};
238240

239241
// 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())?;
241243
// 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);
246245

247246
Ok(buffer)
248247
}
@@ -252,10 +251,7 @@ fn engine_deserialize(mut cx: FunctionContext) -> JsResult<JsNull> {
252251
let serialized_handle = cx.argument::<JsArrayBuffer>(1)?;
253252

254253
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));
259255
}
260256

261257
Ok(JsNull::new(&mut cx))

0 commit comments

Comments
 (0)