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

How to use JsArray #57

Closed
chpio opened this issue Mar 15, 2016 · 6 comments
Closed

How to use JsArray #57

chpio opened this issue Mar 15, 2016 · 6 comments
Labels

Comments

@chpio
Copy link

chpio commented Mar 15, 2016

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`
[...]
@tmcw
Copy link

tmcw commented Jun 21, 2016

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?

@jeandudey
Copy link

Solution: use neon::js:Object;

@tmcw
Copy link

tmcw commented Jul 27, 2016

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

@paulsevere
Copy link

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)
});

@shepmaster
Copy link

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.

dherman pushed a commit that referenced this issue Sep 18, 2017
Move ts-typed-json to dependencies
@dherman
Copy link
Collaborator

dherman commented Nov 22, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants