starting from the hello_world demo:
- we are gonna need the
to_stringmethod. but items from traits can only be used if the trait is in scope so we need to addValueto ourusestatement.
use neon::js::JsString;
to
use neon::js::{JsString, Value};
- we need to store our argument and convert it to a string, so we need to add the following line after
let scope = call.scope;:
let my_str = try!(try!(call.arguments.require(scope, 0)).to_string(scope)).value();
- the
newmethod on JsString class takes a&strso we add a&before the name of the variable:
Ok(JsString::new(scope, "hello node").unwrap())
to
Ok(JsString::new(scope, &my_str).unwrap())
And that's it!
#[macro_use]
extern crate neon;
use neon::vm::{Call, JsResult};
use neon::js::{JsString, Value};
fn echo(call: Call) -> JsResult<JsString> {
let scope = call.scope;
let my_str = try!(try!(call.arguments.require(scope, 0)).to_string(scope)).value();
Ok(JsString::new(scope, &my_str).unwrap())
}
register_module!(m, {
m.export("echo", echo)
});const addon = require('../native')
console.log(addon.echo('abc')) // 'abc'