neon icon indicating copy to clipboard operation
neon copied to clipboard

Support the primitive type: `JsSymbol`(NEON) as `Symbol`(JS)

Open usagi opened this issue 6 years ago • 3 comments

I found the lacks to supporting to these primitive types in my working at the https://github.com/neon-bindings/examples/issues/51 :

  1. Symbol -> This issue
  2. BigInt -> #376

Note: Symbol is not a frequently used primitive type. So this issue may have a lower priority. However, this is a necessary task for NEON as a language binding core feature.

usagi avatar Mar 23 '20 06:03 usagi

As a workaround for first class support, since Symbol is not a primitive, it can be used by accessing it on the global object.

fn symbol(mut cx: FunctionContext) -> JsResult<JsValue> {
    let global = cx.global();
    let symbol_ctor = global
        .get(&mut cx, "Symbol")?
        .downcast::<JsFunction>()
        .or_throw(&mut cx)?;
    
    let symbol_label = cx.string("MySymbol");
    let my_symbol = symbol_ctor.call(&mut cx, global, vec![symbol_label])?;

    Ok(my_symbol)
}

kjvalencik avatar Mar 23 '20 12:03 kjvalencik

Thank you @kjvalencik , your alternative technique is useful for the current implementation level of NEON. 👍

But, I have one disagreement. Symbol is a primitive type. My souce is ECMA-262 and MDN. And supplementary Node.js N-API docs:

I think, JsSymbol supporting is good things for a API structure unifying and completeness. Of cource, I wrote "So this issue may have a lower priority" in the previous note. However, it should be at least including in the implementation plan even if the priority is actually very low.

usagi avatar Mar 23 '20 13:03 usagi

I stand corrected, that Symbol is a primitive type. That's good to know! Also, I didn't mean to indicate that this shouldn't be done, I only wanted to provide a workaround in the feature's absence.

kjvalencik avatar Mar 23 '20 13:03 kjvalencik