neon icon indicating copy to clipboard operation
neon copied to clipboard

Expose to_object()

Open goto-bus-stop opened this issue 5 years ago • 5 comments

The neon-runtime crate implements a function for the ToObject() intrinsic: https://github.com/neon-bindings/neon/blob/f305e92febfcbe87be02ef6be452cb8d11754348/crates/neon-runtime/src/napi/convert.rs#L5

but this is not currently used anywhere. Perhaps this should be exposed on JsValue types somehow? There is value.to_string() for ToString(), so value.to_object() could be a candidate API.

goto-bus-stop avatar Jul 25 '20 08:07 goto-bus-stop

There are also N-API functions for ToNumber() and ToBoolean(), which we currently do not implement. Perhaps this should be part of https://github.com/neon-bindings/neon/issues/569 :)

goto-bus-stop avatar Jul 25 '20 09:07 goto-bus-stop

Good catch—do you happen to know if those N-API functions correspond to core operations in the ECMA-262 spec, or are they more ad hoc? I think it'd be ideal if they correspond to very common operations, like ToObject() I would hope would correspond to what happens in JS when you call Object(x).

dherman avatar Jul 26 '20 18:07 dherman

@kjvalencik Relevant to our discussions about Root not supporting non-object types? I wouldn't expect Root to implicitly box but if this were exposed it would be an option.

jrose-signal avatar Jan 07 '21 18:01 jrose-signal

@jrose-signal Thanks! This is neat, I didn't know this was a thing. I agree that Root shouldn't implicitly do this because it would make the API fallible. 👍

kjvalencik avatar Jan 07 '21 18:01 kjvalencik

Now that I think about it, we could override the blanket impl on Value (which would be fallible) with specific implementations on each of the types where it's safe. 🤔

kjvalencik avatar Jan 07 '21 19:01 kjvalencik