Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Schulhof committed Feb 10, 2021
1 parent da42a46 commit cd3e2d1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions test/js-native-api/test_reference_double_free/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"targets": [
{
"target_name": "test_reference_double_free",
"sources": [
"../entry_point.c",
"test_reference_double_free.c"
]
}
]
}
10 changes: 10 additions & 0 deletions test/js-native-api/test_reference_double_free/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

// This test makes no assertions. It tests a fix without which it will crash
// with a double free.

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

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

{ const obj = new addon.MyObject(); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdlib.h>
#include <js_native_api.h>
#include "../common.h"

static void Destructor(napi_env env, void* data, void* nothing) {
napi_ref* ref = data;
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, *ref));
free(ref);
}

static napi_value New(napi_env env, napi_callback_info info) {
size_t argc = 0;
napi_value js_this;
napi_ref* ref = malloc(sizeof(*ref));

NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, NULL, &js_this, NULL));
NODE_API_CALL(env, napi_wrap(env, js_this, ref, Destructor, NULL, ref));
NODE_API_CALL(env, napi_reference_ref(env, *ref, NULL));

return js_this;
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_value myobj_ctor;
NODE_API_CALL(env,
napi_define_class(
env, "MyObject", NAPI_AUTO_LENGTH, New, NULL, 0, NULL, &myobj_ctor));
NODE_API_CALL(env,
napi_set_named_property(env, exports, "MyObject", myobj_ctor));
return exports;
}
EXTERN_C_END

0 comments on commit cd3e2d1

Please sign in to comment.