Skip to content
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

test: verify napi_remove_wrap with napi_delete_reference #44754

Merged
merged 1 commit into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -42,6 +42,25 @@ static napi_value New(napi_env env, napi_callback_info info) {
return js_this;
}

static void NoopDeleter(napi_env env, void* data, void* hint) {}

static void DeleteImmediately(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value js_obj;
napi_ref ref;

NODE_API_CALL_RETURN_VOID(env,
napi_get_cb_info(env, info, &argc, &js_obj, NULL, NULL));

napi_valuetype type;
NODE_API_CALL_RETURN_VOID(env, napi_typeof(env, js_obj, &type));

NODE_API_CALL_RETURN_VOID(env,
napi_wrap(env, js_obj, NULL, NoopDeleter, NULL, &ref));
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, ref));
NODE_API_CALL_RETURN_VOID(env, napi_remove_wrap(env, js_obj, NULL));
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_value myobj_ctor;
Expand All @@ -50,6 +69,13 @@ napi_value Init(napi_env env, napi_value exports) {
env, "MyObject", NAPI_AUTO_LENGTH, New, NULL, 0, NULL, &myobj_ctor));
NODE_API_CALL(env,
napi_set_named_property(env, exports, "MyObject", myobj_ctor));

napi_property_descriptor descriptors[] = {
DECLARE_NODE_API_PROPERTY("deleteImmediately", DeleteImmediately),
};
NODE_API_CALL(env, napi_define_properties(
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));

return exports;
}
EXTERN_C_END
10 changes: 10 additions & 0 deletions test/js-native-api/test_reference_double_free/test_wrap.js
@@ -0,0 +1,10 @@
'use strict';

// This test makes no assertions. It tests that calling napi_remove_wrap and
// napi_delete_reference consecutively doesn't crash the process.

const { buildType } = require('../../common');

const addon = require(`./build/${buildType}/test_reference_double_free`);

addon.deleteImmediately({});