Search code examples
duktape

How to get the class name when running a constructor function in duktape?


I'd like to use a single duktape/C constructor function as dispatcher for these kind of calls. When the dispatcher function is called I need to know for which class this happend to call the appropriate C++ construction function.

I guess the this binding won't help since it represents the (not yet fully initialized) JS object we are creating here.

Another option would be the current function, but from the docs I can't see how to get the class name from that. What else could I use?


Solution

  • Could you elaborate what you mean by "class name"? Do you mean the .name property of the Ecmascript function object which is used as a the 'new' target?

    If so, you can use duk_is_constructor_call() to see if the current call is a constructor call, then use duk_push_current_function() to get access to the Ecmascript constructor function object, and then read its properties using the usual property API calls. For example, if by "class name" you mean .name of the function object, you'd just read its "name" property using duk_get_prop_string().