Skip to content

[Feature Request] API for jsi::Symbol::wellKnown(...) - e.g. Symbol.dispose #1807

@mrousavy

Description

@mrousavy

Problem

Currently it's super hard to expose a native property using a Symbol as a name. Symbols are generally pretty impossible to work with on the native side.

For example, in ES2023 (ES14) the Symbol.dispose (and the using ... syntax) was introduced in JS. In Nitro I actually have dispose() methods for every HybridObject, but they are literally just methods exposed under a property that has the string key "dispose".

I would want to expose them using the Symbol.dispose key, which is a well-known symbol.

In JSI, there is no way to achieve this unless you use eval or global().
This would be the current solution in JSI:

auto symbol = runtime.global().getPropertyAsObject(runtime, "Symbol");
auto dispose = symbol.getProperty(runtime, "dispose").asSymbol(runtime);

Solution

Would be super useful if we could add jsi::Symbol::wellKnown(...), which would be a more convenient way of looking up well-known Symbols, and potentially also a faster, cached way of doing so.

auto dispose = jsi::Symbol::wellKnown(runtime, "dispose");

Additional Context

There's many well known Symbols in JS - with this new API, overriding some of those symbols on native objects would be much easier - e.g. Symbol.toStringTag (for toString()), Symbol.iterator (for loops), Symbol.hasInstance (for instanceof), and more.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions