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

Created on 23 Mar 2020  路  3Comments  路  Source: neon-bindings/neon

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.

Most helpful comment

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.

All 3 comments

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)
}

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.

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.

Was this page helpful?
0 / 5 - 0 ratings