-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
napi_get_cb_info: how to detect instance this and prototype #13824
Comments
/cc @nodejs/n-api |
You may be able to keep the prototype for the wrapped class and check that the We don't have an API like is_wrapped, even if we did that might only fix some cases as you might be able to pass in the wrong type of object even if it is one that was wrapped. |
@mhdawson since I save a reference to the class constructor, maybe I can use napi_value MyClass_constructor = nullptr;
status = napi_get_reference_value(env, MyClass::es_constructor, & MyClass_constructor);
assert(napi_ok == status);
bool is_instance = false;
status = napi_instanceof(env, es_this, MyClass_constructor, &is_instance);
assert(napi_ok == status);
if (is_instance) {
// napi_unwrap() ...
} else {
// otherwise...
} |
@ealib that sounds reasonable- did that workaround work for you or do you need changes in n-api? |
Saving a persistent reference to the class constructor for use with later instanceof checks is a very common pattern for native addons. We should probably document that. |
@ealib, if that worked for you it would be great to add it to the documentation. If you are interested I could help you submit a PR ? If you don't have time I'll take a look at it. |
Sorry for the late reply. @digitalinfinity yes it works OK so far @jasongin yes, saving a reference to constructor is a pattern I use for 100% classes because the native module I am working on exposes a few functions to lookup some entities and returned IDs are used to actually get (wrapped) instances of those very entities (another old pattern) @mhdawson I just updated the module code to use |
PR-URL: nodejs#16699 Fixes: nodejs#13824 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Maybe a trivial issue, but I found I can't easily distinguish from
this
retrieved withnapi_get_cb_info
inside a getter called on an instance of a wrapped class, and the same getter called on that classprototype
.The getter code fails because
napi_get_cb_info
returns athisArg
value which can not be used innapi_unwrap
(of course).Is checking
if (napi_invalid_arg == status)
after callingnapi_unwrap
the only way to detect a getter is called onprototype
?The text was updated successfully, but these errors were encountered: