diff --git a/test/js-native-api/test_reference_double_free/test_reference_double_free.c b/test/js-native-api/test_reference_double_free/test_reference_double_free.c index 48491ea2aaee63..8c738aa2f0f5a8 100644 --- a/test/js-native-api/test_reference_double_free/test_reference_double_free.c +++ b/test/js-native-api/test_reference_double_free/test_reference_double_free.c @@ -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; @@ -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 diff --git a/test/js-native-api/test_reference_double_free/test_wrap.js b/test/js-native-api/test_reference_double_free/test_wrap.js new file mode 100644 index 00000000000000..4f0092111032ca --- /dev/null +++ b/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({});