neon-bindings / neon

Rust bindings for writing safe and fast native Node.js modules.
https://www.neon-bindings.com/
Apache License 2.0
7.98k stars 282 forks source link

feat(neon): `JsFunction::bind()` and `Object::prop()` #1056

Open dherman opened 1 month ago

dherman commented 1 month ago

This PR implements JsFunction::bind(), which creates a builder for calling a function using the Try{From,Into}Js traits.

let n = f.bind(&mut cx)
    .args((1, 2, 3))?
    .arg(4)?
    .arg_with(|cx| Ok(cx.number(5)))?
    .apply::<f64>()?;

It also implements Object::prop(), which creates a builder for accessing object properties using the Try{From,Into}Js traits.

let x: f64 = obj
    .prop(&mut cx, "x")
    .get()?;

obj.prop(&mut cx, "y")
    .set(x)?;

let s: String = obj.prop(&mut cx, "toString")
    .bind()?
    .apply()?;