neon icon indicating copy to clipboard operation
neon copied to clipboard

Explicit function signatures

Open MOZGIII opened this issue 4 years ago • 4 comments

Inspired by wasm-bindgen, it would be great to have richer function signatures.

I'm thinking about something like this:

fn add_1_to_argument(mut cx: FunctionContext, x: Handle<'_, JsNumber>) -> JsResult<JsNumber> {
    Ok(cx.number(x + 1.0))
}

instead of this:

fn add_1_to_argument(mut cx: FunctionContext) -> JsResult<JsNumber> {
    let x = cx.argument::<JsNumber>(0)?.value();
    Ok(cx.number(x + 1.0))
}

This will, in turn, unlock further progress with https://github.com/neon-bindings/neon/issues/349

MOZGIII avatar Jul 21 '20 07:07 MOZGIII

@MOZGIII I like this a lot. I had been thinking of it directly mapping to Rust types. E.g., fn add_1_to_argument(x: f64), but, mapping to neon types is a little more flexible. It gives the user the option to keep it as Js if they don't need a native type.

Interestingly, a facsimile of this feature could be implemented without proc macros by defining it for enums of certain sizes.

fn add_1_to_argument(mut cx: FunctionContext, (x: Handle<'_, JsNumber>)) -> JsResult<JsNumber> {
    Ok(cx.number(x + 1.0))
}

It would be neat to have a FromJs trait that we could use implement for the auto-conversions.

kjvalencik avatar Jul 21 '20 14:07 kjvalencik

relatively new to rust and extremely new to javascript. Is there anything I could do to help with this?

skewballfox avatar Oct 28 '20 17:10 skewballfox