Closed
Description
- Version: 8.1.2
- Platform: Windows 10
- Subsystem: N-API
Maybe a trivial issue, but I found I can't easily distinguish from this
retrieved with napi_get_cb_info
inside a getter called on an instance of a wrapped class, and the same getter called on that class prototype
.
const example = require('example');
let m = new example.MyClass();
console.dir(m); // ok
console.dir(m.prototype); // here getters fail
The getter code fails because napi_get_cb_info
returns a thisArg
value which can not be used in napi_unwrap
(of course).
napi_value MyClass::GetCount(napi_env env, napi_callback_info info) {
const size_t argc_expected = 0;
size_t argc = argc_expected;
napi_value es_this;
napi_status status = napi_get_cb_info(env, info, &argc, nullptr, &es_this, nullptr);
assert(napi_ok == status);
MyClass* native_instance = nullptr;
status = napi_unwrap(env, es_this, reinterpret_cast<void**>(&native_instance));
assert(napi_ok == status); // status is napi_invalid_arg when es_this is prototype
double count = (double) native_instance->count;
napi_value count_value;
status = napi_create_number(env, count, &count_value);
assert(napi_ok == status);
return count_value;
}
Is checking if (napi_invalid_arg == status)
after calling napi_unwrap
the only way to detect a getter is called on prototype
?