Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(crate): update to neon 0.7, closes #64 #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ readme = "readme.md"
[dependencies]
serde = "1"
error-chain = "0.12"
neon = "0.4"
neon-runtime = "0.4"
neon = "0.7"

[dependencies.num]
version = "0.2"
Expand Down
11 changes: 8 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ struct AnObject {
fn deserialize_something(mut cx: FunctionContext) -> JsResult<JsValue> {
let arg0 = cx.argument::<JsValue>(0)?;

let arg0_value :AnObject = neon_serde::from_value(&mut cx, arg0)?;
let arg0_value: AnObject = match neon_serde::from_value(&mut cx, arg0) {
Ok(value) => value,
Err(e) => {
return cx.throw_error(e.to_string());
}
};
println!("{:?}", arg0_value);

Ok(JsUndefined::new().upcast())
Expand All @@ -127,8 +132,8 @@ fn serialize_something(mut cx: FunctionContext) -> JsResult<JsValue> {
c: "a string".into()
};

let js_value = neon_serde::to_value(&mut cx, &value)?;
Ok(js_value)
neon_serde::to_value(&mut cx, &value)
.or_else(|e| cx.throw_error(e.to_string()))
}
```

Expand Down
16 changes: 0 additions & 16 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Defines error handling types used by the create
//! uses the `error-chain` create for generation

extern crate neon_runtime;

use neon;
use serde::{de, ser};
use std::convert::From;
Expand Down Expand Up @@ -85,20 +83,6 @@ impl de::Error for Error {
}
}

#[allow(use_debug)]
impl From<Error> for neon::result::Throw {
fn from(err: Error) -> Self {
if let ErrorKind::Js(_) = *err.kind() {
return neon::result::Throw;
};
let msg = format!("{:?}", err);
unsafe {
neon_runtime::error::throw_error_from_utf8(msg.as_ptr(), msg.len() as i32);
neon::result::Throw
}
}
}

Comment on lines -88 to -101

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshuef Thanks for doing the work of upgrading. I just started using this. Not having this trait means we can't use the ? operator for handling Result types. Is there a reason we cannot have this trait?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's explained here: https://docs.rs/neon/0.7.1/neon/result/struct.Throw.html

Kinda unfortunate though, breaks the export! macro. It uses ? and should be changed to handle errors properly. Couldn't tell you exactly how though.

impl From<neon::result::Throw> for Error {
fn from(throw: neon::result::Throw) -> Self {
ErrorKind::Js(throw).into()
Expand Down
28 changes: 19 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
//! fn deserialize_something(mut cx: FunctionContext) -> JsResult<JsValue> {
//! let arg0 = cx.argument::<JsValue>(0)?;
//!
//! let arg0_value :AnObject = neon_serde::from_value(&mut cx, arg0)?;
//! let arg0_value :AnObject = neon_serde::from_value(&mut cx, arg0)
//! .or_else(|e| cx.throw_error(e.to_string()))
//! .unwrap();
//! println!("{:?}", arg0_value);
//!
//! Ok(JsUndefined::new().upcast())
Expand All @@ -60,7 +62,9 @@
//! c: "a string".into()
//! };
//!
//! let js_value = neon_serde::to_value(&mut cx, &value)?;
//! let js_value = neon_serde::to_value(&mut cx, &value)
//! .or_else(|e| cx.throw_error(e.to_string()))
//! .unwrap();
//! Ok(js_value)
//! }
//!
Expand All @@ -77,9 +81,9 @@ extern crate num;
#[macro_use]
extern crate serde;

pub mod ser;
pub mod de;
pub mod errors;
pub mod ser;

mod macros;

Expand All @@ -97,10 +101,14 @@ mod tests {
fn check<'j>(mut cx: FunctionContext<'j>) -> JsResult<'j, JsValue> {
let result: () = {
let arg: Handle<'j, JsValue> = cx.argument::<JsValue>(0)?;
let () = from_value(&mut cx, arg)?;
let () = from_value(&mut cx, arg)
.or_else(|e| cx.throw_error(e.to_string()))
.unwrap();
()
};
let result: Handle<'j, JsValue> = to_value(&mut cx, &result)?;
let result: Handle<'j, JsValue> = to_value(&mut cx, &result)
.or_else(|e| cx.throw_error(e.to_string()))
.unwrap();
Ok(result)
}

Expand All @@ -112,14 +120,16 @@ mod tests {
fn check<'j>(mut cx: FunctionContext<'j>) -> JsResult<'j, JsValue> {
let result: () = {
let arg: Option<Handle<'j, JsValue>> = cx.argument_opt(0);
let () = from_value_opt(&mut cx, arg)?;
()
let () = from_value_opt(&mut cx, arg)
.or_else(|e| cx.throw_error(e.to_string()))
.unwrap();
};
let result: Handle<'j, JsValue> = to_value(&mut cx, &result)?;
let result: Handle<'j, JsValue> = to_value(&mut cx, &result)
.or_else(|e| cx.throw_error(e.to_string()))
.unwrap();
Ok(result)
}

let _ = check;
}

}
2 changes: 1 addition & 1 deletion test/__tests__/get_values.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,6 @@ describe('throwing functions', () => {
})
}
expect(() => native.expect_obj(obj))
.toThrow(/Hi There prop c/);
.toThrow('JS exception');
})
});
4 changes: 2 additions & 2 deletions test/native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ name = "test"
crate-type = ["dylib"]

[build-dependencies]
neon-build = "0.4.0"
neon-build = "0.7.0"

[dependencies]
neon = "0.4.0"
neon = "0.7"
neon-serde = { path = "../../" }
serde_derive = "1.0.106"
serde = "1.0.106"
Expand Down
41 changes: 21 additions & 20 deletions test/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,10 @@ struct AnObjectTwo {

macro_rules! make_test {
($name:ident, $val:expr) => {
fn $name(cx: FunctionContext) -> JsResult<JsValue> {
fn inner(mut cx: FunctionContext) -> neon_serde::errors::Result<Handle<JsValue>> {
let value = $val;
fn $name(mut cx: FunctionContext) -> JsResult<JsValue> {
let value = $val;

let handle = neon_serde::to_value(&mut cx, &value)?;
Ok(handle)
}

Ok(inner(cx)?)
neon_serde::to_value(&mut cx, &value).or_else(|e| cx.throw_error(e.to_string()))
}
};
}
Expand Down Expand Up @@ -116,16 +111,18 @@ make_test!(make_buff, { serde_bytes::Bytes::new(NUMBER_BYTES) });

macro_rules! make_expect {
($name:ident, $val:expr, $val_type:ty) => {
fn $name(cx: FunctionContext) -> JsResult<JsValue> {
fn inner(mut cx: FunctionContext) -> neon_serde::errors::Result<Handle<JsValue>> {
let value = $val;
let arg0 = cx.argument::<JsValue>(0)?;

let de_serialized: $val_type = neon_serde::from_value(&mut cx, arg0)?;
assert_eq!(value, de_serialized);
Ok(JsUndefined::new().upcast())
}
Ok(inner(cx)?)
fn $name(mut cx: FunctionContext) -> JsResult<JsValue> {
let value = $val;
let arg0 = cx.argument::<JsValue>(0)?;

let de_serialized: $val_type = match neon_serde::from_value(&mut cx, arg0) {
Ok(value) => value,
Err(e) => {
return cx.throw_error(e.to_string());
}
};
assert_eq!(value, de_serialized);
Ok(JsUndefined::new().upcast())
}
};
}
Expand Down Expand Up @@ -170,8 +167,12 @@ make_expect!(
fn roundtrip_object(mut cx: FunctionContext) -> JsResult<JsValue> {
let arg0 = cx.argument::<JsValue>(0)?;

let de_serialized: AnObjectTwo = neon_serde::from_value(&mut cx, arg0)?;
let handle = neon_serde::to_value(&mut cx, &de_serialized)?;
let de_serialized: AnObjectTwo = neon_serde::from_value(&mut cx, arg0)
.or_else(|e| cx.throw_error(e.to_string()))
.unwrap();
let handle = neon_serde::to_value(&mut cx, &de_serialized)
.or_else(|e| cx.throw_error(e.to_string()))
.unwrap();
Ok(handle)
}

Expand Down
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"devDependencies": {
"expect": "^24.9.0",
"mocha": "^6.2.1",
"neon-cli": "^0.4.0"
"neon-cli": "^0.7.0"
},
"scripts": {
"build": "neon build --release",
Expand Down
Loading