Hi, I'd like to submit some basic examples to help people who are trying out this module and Rust for the first time. I hit quite a few frustrating things when trying to get each of these working. I'd like to submit a PR somewhere but don't know where you'd like this to go. Please advise.
#[macro_use]
extern crate neon;
use neon::vm::{Call, JsResult};
use neon::js::{
Object, // <- https://github.com/neon-bindings/neon/issues/57
JsFunction,
JsObject,
JsArray,
JsBoolean,
JsInteger,
JsNull,
JsNumber,
JsString,
JsUndefined };
/*
// If you need to inspect the value of an expression:
let () = JsBoolean::new();
*/
// Use _ to squelch warnings, or any name starting with _
fn get_null_sync(_: Call) -> JsResult<JsNull> {
Ok(JsNull::new())
}
// Use _ to squelch warnings, or any name starting with _
fn get_undefined_sync(_: Call) -> JsResult<JsUndefined> {
Ok(JsUndefined::new())
}
fn get_number_sync(call: Call) -> JsResult<JsNumber> {
Ok(JsNumber::new(call.scope, 5f64))
}
fn get_integer_sync(call: Call) -> JsResult<JsInteger> {
Ok(JsInteger::new(call.scope, 5i32))
}
fn get_string_sync(call: Call) -> JsResult<JsString> {
Ok(JsString::new(call.scope, "my string").unwrap())
}
fn get_boolean_sync(call: Call) -> JsResult<JsBoolean> {
Ok(JsBoolean::new(call.scope, false))
}
fn get_array_sync(call: Call) -> JsResult<JsArray> {
let scope = call.scope;
let array = JsArray::new(scope, 3);
try!(array.set(0, JsInteger::new(scope, 1)));
try!(array.set(1, JsInteger::new(scope, 2)));
try!(array.set(2, JsInteger::new(scope, 3)));
Ok(array)
}
fn get_object_sync(call: Call) -> JsResult<JsObject> {
let scope = call.scope;
let object = JsObject::new(scope);
try!(object.set("prop1", JsInteger::new(scope, 1)));
try!(object.set("prop2", JsInteger::new(scope, 2)));
try!(object.set("prop3", JsInteger::new(scope, 3)));
Ok(object)
}
fn get_function_sync(call: Call) -> JsResult<JsFunction> {
fn func(call: Call) -> JsResult<JsInteger> {
Ok(JsInteger::new(call.scope, 5))
}
Ok(JsFunction::new(call.scope, func).unwrap())
}
register_module!(m, {
try!(m.export("getNullSync", get_null_sync));
try!(m.export("getUndefinedSync", get_undefined_sync));
try!(m.export("getNumberSync", get_number_sync));
try!(m.export("getIntegerSync", get_integer_sync));
try!(m.export("getStringSync", get_string_sync));
try!(m.export("getBooleanSync", get_boolean_sync));
try!(m.export("getArraySync", get_array_sync));
try!(m.export("getObjectSync", get_object_sync));
try!(m.export("getFunctionSync", get_function_sync));
Ok(())
});
I think it would be nice to see an example of getting js arguments.
@ocboogie
// in JS:
// require('neon-module').async_method('show me the money', (n) => console.log(n))
// >> 'show me the money'
fn async_method(call: Call) -> JsResult<JsUndefined> {
let fn_handle = call.arguments.get(call.scope, 1).unwrap();
let arg_handle = call.arguments.get(call.scope, 0).unwrap();
if let Some(function) = fn_handle.downcast::<JsFunction>() {
let args: Vec<Handle<JsValue>> = vec![arg_handle];
let _ = function.call(call.scope, JsNull::new(), args);
}
Ok(JsUndefined::new())
}
register_module!(m, {
try!(m.export("async_method", async_method));
Ok(())
});
Cargo tooling expects executable examples to live in an directory called examples http://doc.crates.io/guide.html#project-layout
Most helpful comment
@ocboogie