-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
fix(napi): Make napi_wrap work on regular objects #15622
Open
190n
wants to merge
11
commits into
main
Choose a base branch
from
ben/napi-wrap-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+940
−109
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1ff193e
Add functions for napi_wrap test
190n 3566216
Some napi_wrap testing
190n 979dd82
Rewrite napi_wrap
190n 5095f35
More extensive napi_wrap test (still needs to be better)
190n 287e52d
Use NapiRef pointer field on NapiPrototype to store wrap information …
190n 9af0713
Fix bugs and test more thoroughly
190n 6f980c5
Less redundancy
190n fbf6c1a
Unskip lifetime tests
190n 9ed3c07
Add test for @napi-rs/canvas
190n 1df5055
Merge branch 'main' into ben/napi-wrap-fix
Jarred-Sumner 96e45b4
Upgrade @napi-rs/canvas and install Jimp for test suite
190n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1025,6 +1025,15 @@ extern "C" void napi_module_register(napi_module* mod) | |
globalObject->m_pendingNapiModuleAndExports[1].set(vm, globalObject, object); | ||
} | ||
|
||
static inline bool hasNapiWrap(VM& vm, JSGlobalObject* globalObject, JSObject* object) | ||
{ | ||
if (auto* napi_instance = jsDynamicCast<NapiPrototype*>(object)) { | ||
return napi_instance->napiRef != nullptr; | ||
} else { | ||
return !object->getDirect(vm, WebCore::builtinNames(vm).napiWrappedContentsPrivateName()).isEmpty(); | ||
} | ||
} | ||
|
||
extern "C" napi_status napi_wrap(napi_env env, | ||
napi_value js_object, | ||
void* native_object, | ||
|
@@ -1039,50 +1048,51 @@ extern "C" napi_status napi_wrap(napi_env env, | |
{ | ||
NAPI_PREMABLE | ||
|
||
JSValue value = toJS(js_object); | ||
if (!value || value.isUndefinedOrNull()) { | ||
return napi_object_expected; | ||
} | ||
|
||
auto* globalObject = toJS(env); | ||
|
||
NapiRef** refPtr = nullptr; | ||
if (auto* val = jsDynamicCast<NapiPrototype*>(value)) { | ||
refPtr = &val->napiRef; | ||
} else if (auto* val = jsDynamicCast<NapiClass*>(value)) { | ||
refPtr = &val->napiRef; | ||
auto& vm = globalObject->vm(); | ||
auto scope = DECLARE_THROW_SCOPE(vm); | ||
JSValue jsc_value = toJS(js_object); | ||
if (jsc_value.isEmpty()) { | ||
return napi_invalid_arg; | ||
} | ||
|
||
if (!refPtr) { | ||
JSObject* jsc_object = jsc_value.getObject(); | ||
if (!jsc_object) { | ||
scope.assertNoException(); | ||
return napi_object_expected; | ||
} | ||
|
||
if (*refPtr) { | ||
// Calling napi_wrap() a second time on an object will return an error. | ||
// To associate another native instance with the object, use | ||
// napi_remove_wrap() first. | ||
// NapiPrototype has an inline field to store a napi_ref, so we use that if we can | ||
auto* napi_instance = jsDynamicCast<NapiPrototype*>(jsc_object); | ||
|
||
const JSC::Identifier& propertyName = WebCore::builtinNames(vm).napiWrappedContentsPrivateName(); | ||
|
||
if (hasNapiWrap(vm, globalObject, jsc_object)) { | ||
// already wrapped | ||
scope.assertNoException(); | ||
return napi_invalid_arg; | ||
} | ||
|
||
// create a new weak reference (refcount 0) | ||
auto* ref = new NapiRef(globalObject, 0); | ||
ref->weakValueRef.set(jsc_value, weakValueHandleOwner(), ref); | ||
|
||
ref->weakValueRef.set(value, weakValueHandleOwner(), ref); | ||
|
||
if (finalize_cb) { | ||
ref->finalizer.finalize_cb = finalize_cb; | ||
ref->finalizer.finalize_hint = finalize_hint; | ||
} | ||
ref->finalizer.finalize_cb = finalize_cb; | ||
ref->finalizer.finalize_hint = finalize_hint; | ||
ref->data = native_object; | ||
|
||
if (native_object) { | ||
ref->data = native_object; | ||
if (napi_instance) { | ||
napi_instance->napiRef = ref; | ||
} else { | ||
// wrap the ref in an external so that it can serve as a JSValue | ||
auto* external = Bun::NapiExternal::create(globalObject->vm(), globalObject->NapiExternalStructure(), ref, nullptr, nullptr); | ||
jsc_object->putDirect(vm, propertyName, JSValue(external)); | ||
RETURN_IF_EXCEPTION(scope, napi_pending_exception); | ||
} | ||
|
||
*refPtr = ref; | ||
|
||
if (result) { | ||
*result = toNapi(ref); | ||
} | ||
|
||
scope.assertNoException(); | ||
return napi_ok; | ||
} | ||
|
||
|
@@ -1091,35 +1101,48 @@ extern "C" napi_status napi_remove_wrap(napi_env env, napi_value js_object, | |
{ | ||
NAPI_PREMABLE | ||
|
||
JSValue value = toJS(js_object); | ||
if (!value || value.isUndefinedOrNull()) { | ||
JSValue jsc_value = toJS(js_object); | ||
if (jsc_value.isEmpty()) { | ||
return napi_invalid_arg; | ||
} | ||
JSObject* jsc_object = jsc_value.getObject(); | ||
if (!js_object) { | ||
return napi_object_expected; | ||
} | ||
// may be null | ||
auto* napi_instance = jsDynamicCast<NapiPrototype*>(jsc_object); | ||
|
||
NapiRef** refPtr = nullptr; | ||
if (auto* val = jsDynamicCast<NapiPrototype*>(value)) { | ||
refPtr = &val->napiRef; | ||
} else if (auto* val = jsDynamicCast<NapiClass*>(value)) { | ||
refPtr = &val->napiRef; | ||
} | ||
auto* globalObject = toJS(env); | ||
auto& vm = globalObject->vm(); | ||
auto scope = DECLARE_THROW_SCOPE(vm); | ||
NapiRef* ref = nullptr; | ||
|
||
if (!refPtr) { | ||
return napi_object_expected; | ||
if (!hasNapiWrap(vm, globalObject, jsc_object)) { | ||
return napi_invalid_arg; | ||
} | ||
|
||
if (!(*refPtr)) { | ||
// not sure if this should succeed or return an error | ||
return napi_ok; | ||
if (napi_instance) { | ||
ref = napi_instance->napiRef; | ||
napi_instance->napiRef = nullptr; | ||
} else { | ||
const JSC::Identifier& propertyName = WebCore::builtinNames(vm).napiWrappedContentsPrivateName(); | ||
JSValue contents_value = jsc_object->getDirect(vm, propertyName); | ||
RETURN_IF_EXCEPTION(scope, napi_pending_exception); | ||
// this cast asserts: we should not have stored anything but a NapiExternal here | ||
auto* contents_external = jsCast<Bun::NapiExternal*>(contents_value); | ||
ref = static_cast<NapiRef*>(contents_external->value()); | ||
jsc_object->deleteProperty(globalObject, propertyName); | ||
RETURN_IF_EXCEPTION(scope, napi_pending_exception); | ||
} | ||
|
||
auto* ref = *refPtr; | ||
*refPtr = nullptr; | ||
|
||
if (result) { | ||
*result = ref->data; | ||
} | ||
delete ref; | ||
ref->finalizer.finalize_cb = nullptr; | ||
|
||
// don't delete the ref: if weak, it'll delete itself when the JS object is deleted; | ||
// if strong, native addon needs to clean it up. | ||
// the external is garbage collected. | ||
return napi_ok; | ||
} | ||
|
||
|
@@ -1128,23 +1151,40 @@ extern "C" napi_status napi_unwrap(napi_env env, napi_value js_object, | |
{ | ||
NAPI_PREMABLE | ||
|
||
JSValue value = toJS(js_object); | ||
|
||
if (!value.isObject()) { | ||
return NAPI_OBJECT_EXPECTED; | ||
JSValue jsc_value = toJS(js_object); | ||
if (jsc_value.isEmpty()) { | ||
return napi_invalid_arg; | ||
} | ||
JSObject* jsc_object = jsc_value.getObject(); | ||
if (!jsc_object) { | ||
return napi_object_expected; | ||
} | ||
auto* napi_instance = jsDynamicCast<NapiPrototype*>(jsc_object); | ||
|
||
auto* globalObject = toJS(env); | ||
auto& vm = globalObject->vm(); | ||
auto scope = DECLARE_THROW_SCOPE(vm); | ||
const JSC::Identifier& propertyName = WebCore::builtinNames(vm).napiWrappedContentsPrivateName(); | ||
NapiRef* ref = nullptr; | ||
if (auto* val = jsDynamicCast<NapiPrototype*>(value)) { | ||
ref = val->napiRef; | ||
} else if (auto* val = jsDynamicCast<NapiClass*>(value)) { | ||
ref = val->napiRef; | ||
|
||
if (!hasNapiWrap(vm, globalObject, jsc_object)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also calls getDirect twice |
||
return napi_invalid_arg; | ||
} | ||
|
||
if (napi_instance) { | ||
ref = napi_instance->napiRef; | ||
} else { | ||
ASSERT(false); | ||
JSValue contents_value = jsc_object->getDirect(vm, propertyName); | ||
RETURN_IF_EXCEPTION(scope, napi_pending_exception); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to check for exceptions when using getDirect |
||
|
||
// this cast asserts: we should not have stored anything but a NapiExternal here | ||
auto* contents_external = jsCast<Bun::NapiExternal*>(contents_value); | ||
ref = static_cast<NapiRef*>(contents_external->value()); | ||
} | ||
ASSERT(ref); | ||
|
||
if (ref && result) { | ||
*result = ref ? ref->data : nullptr; | ||
if (result) { | ||
*result = ref->data; | ||
} | ||
|
||
return napi_ok; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This calls
getDirect
twice. We only need to call it once. Let's call it once.