hi,
i try to compile the example from the readme:
#[macro_use]
extern crate neon;
use neon::vm::{Call, JsResult};
use neon::js::{JsString, JsArray, JsInteger, JsObject, JsNumber};
use neon::internal::js::{PropertyName};
fn make_an_array(call: Call) -> JsResult<JsArray> {
let scope = call.scope; // the current scope for rooting handles
let array = JsArray::new(scope, 3);
try!(array.set(0, JsInteger::new(scope, 9000)));
try!(array.set(1, JsObject::new(scope)));
try!(array.set(2, JsNumber::new(scope, 3.14159)));
Ok(array)
}
register_module!(m, {
m.export("main", make_an_array)
});
use neon::internal::js::{PropertyName}; looks fishy (no _non-internal_ export; and causes more errors), but im getting without it:
src/lib.rs:20:13: 20:48 error: no method named `set` found for type `neon::internal::mem::Handle<'_, neon::internal::js::JsArray>` in the current scope
src/lib.rs:20 try!(array.set(0, JsInteger::new(scope, 9000)));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/lib.rs:20:2: 20:50 note: in this expansion of try! (defined in <std macros>)
src/lib.rs:20:13: 20:48 help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
src/lib.rs:20:13: 20:48 help: candidate #1: use `neon::internal::js::PropertyName`
[...]
I'm hitting the same error in the code example as well as my project (source). I checked out neon itself and ran the test suite, and it works properly in this case and tests the array .set method, so potentially this will be fixed if there's a new tagged release?
Solution: use neon::js:Object;
Unfortunately that doesn't seem to work either:
Input:
let js_object: Handle<JsObject> = JsObject::new(scope);
try!(js_object.set("number", JsInteger::new(scope, 9000)));
// Ok(obj)
Ok(js_object)
Compiling node v0.1.0 (file:///Users/tmcw/src/rust-polyline/node/native)
src/lib.rs:28:20: 28:23 error: this function takes 3 parameters but 2 parameters were supplied [E0061]
src/lib.rs:28 try!(js_object.set("number", JsInteger::new(scope, 9000)));
^~~
src/lib.rs:28:5: 28:64 note: in this expansion of try! (defined in <std macros>)
src/lib.rs:28:20: 28:23 help: run `rustc --explain E0061` to see a detailed explanation
src/lib.rs:28:20: 28:23 note: the following parameter types were expected: &mut bool, neon::sys::raw::Local, neon::sys::raw::Local
<std macros>:3:1: 3:42 error: mismatched types [E0308]
<std macros>:3 $ crate :: result :: Result :: Ok ( val ) => val , $ crate :: result :: Result
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/lib.rs:28:5: 28:64 note: in this expansion of try! (defined in <std macros>)
<std macros>:3:1: 3:42 help: run `rustc --explain E0308` to see a detailed explanation
<std macros>:3:1: 3:42 note: expected type `bool`
<std macros>:3:1: 3:42 note: found type `std::result::Result<_, _>`
<std macros>:3:52: 4:15 error: mismatched types [E0308]
<std macros>:3 $ crate :: result :: Result :: Ok ( val ) => val , $ crate :: result :: Result
^
src/lib.rs:28:5: 28:64 note: in this expansion of try! (defined in <std macros>)
<std macros>:3:52: 4:15 help: run `rustc --explain E0308` to see a detailed explanation
<std macros>:3:52: 4:15 note: expected type `bool`
<std macros>:3:52: 4:15 note: found type `std::result::Result<_, _>`
error: aborting due to 3 previous errors
I was able to get this sample code working using neon v "0.1.18" by removing "use neon::internal::js::{PropertyName};" and by explicitly importing the "Object" trait from neon::js.
#[macro_use]
extern crate neon;
use neon::vm::{Call, JsResult};
use neon::js::{Object, JsString, JsArray, JsInteger, JsObject, JsNumber};
fn make_an_array(call: Call) -> JsResult<JsArray> {
let scope = call.scope; // the current scope for rooting handles
let array = JsArray::new(scope, 3);
try!(array.set(0, JsInteger::new(scope, 9000)));
try!(array.set(1, JsObject::new(scope)));
try!(array.set(2, JsNumber::new(scope, 3.14159)));
Ok(array)
}
register_module!(m, {
m.export("main", make_an_array)
});
Another way to shoot yourself in the foot: if you use neon::js::Key, which also defines the set method. Removing that import cleared up other cases.
While this technically isn't a bug, it's an indication that the API design isn't very good. I'm going to close this issue but we probably want to improve the UX of the API, perhaps as part of the VM 2.0 RFC.
Most helpful comment
I was able to get this sample code working using neon v "0.1.18" by removing "use neon::internal::js::{PropertyName};" and by explicitly importing the "Object" trait from neon::js.