Skip to content

How to implement custom inspect into ObjectWrap class? #919

Closed

Description

@ThomasWeinert

I tried registering the an instance method for "nodejs.util.inspect.custom".

InstanceMethod(Napi::Symbol::New(env, "nodejs.util.inspect.custom"), &MyObjectWrap::Inspect)

But this doesn't get called by console.log(). It makes sense because it needs to be a public symbol, but how do I register a method for a public symbol? Here is no Napi::Symbol::For().

Activity

  1. KevinEady commented on Mar 3, 2021

    @KevinEady
    Contributor

    Have you tried Napi::Symbol::WellKnown? Otherwise, you could either pass the JavaScript value of Symbol.for(...) to your addon so it can be used, or you can grab it from global, something like:

    auto Symbol = env.Global().Get("Symbol").As<Object>();
    auto inspectSym = Symbol.Get("for").As<Function>().Call(Symbol, { Napi::String::New(env, "nodejs.util.inspect.custom") });
  2. ThomasWeinert commented on Mar 3, 2021

    @ThomasWeinert
    Author

    Napi::Symbol::WellKnown(env, "nodejs.util.inspect.custom") returns Napi::Env::Undefined().

    Fetching it from JS works. I put that into a utility function:

    Napi::Symbol GetPublicSymbol(const Napi::Env &env, std::string name) {
        auto Symbol = env.Global().Get("Symbol").As<Napi::Object>();
        auto forSymbol = Symbol.Get("for").As<Napi::Function>().Call(
            Symbol, { Napi::String::New(env, name) }
        );
        return forSymbol.As<Napi::Symbol>();
    }

    And then

    InstanceMethod(GetPublicSymbol(env, "nodejs.util.inspect.custom"), &MyObjectWrap::Inspect),

    Thank you.

  3. gabrielschulhof commented on Mar 5, 2021

    @gabrielschulhof
    Contributor

    @KevinEady is it a problem with Napi::Symbol::WellKnown that it returns undefined in this case?

  4. KevinEady commented on Mar 26, 2021

    @KevinEady
    Contributor

    @KevinEady is it a problem with Napi::Symbol::WellKnown that it returns undefined in this case?

    @gabrielschulhof hmmm... the implementation for WellKnown is:

    node-addon-api/napi-inl.h

    Lines 996 to 998 in 8ef0725

    inline Symbol Symbol::WellKnown(napi_env env, const std::string& name) {
    return Napi::Env(env).Global().Get("Symbol").As<Object>().Get(name).As<Symbol>();
    }

    This is the equivalent of:

    Symbol[name]

    Which is different than

    Symbol.for(name)

    Is the WellKnown implementation incorrect?

  5. KevinEady commented on Apr 16, 2021

    @KevinEady
    Contributor

    As discussed in the Node-API meeting, the implementation for Symbol::WellKnown is correct: it is used to retrieve values like Symbol.toStringTag, which is a direct property on Symbol.

    Closing issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions