Skip to content

Commit b26410e

Browse files
Gabriel SchulhofMylesBorins
Gabriel Schulhof
authored andcommitted
n-api: update reference test
Remove the necessity for allocating on the heap, and assert that the correct pointer gets passed to the finalizer. Backport-PR-URL: #19265 PR-URL: #19086 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 1abb168 commit b26410e

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

test/addons-napi/test_reference/test_reference.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <node_api.h>
22
#include "../common.h"
3-
#include <stdlib.h>
43

54
static int test_value = 1;
65
static int finalize_count = 0;
@@ -13,7 +12,9 @@ napi_value GetFinalizeCount(napi_env env, napi_callback_info info) {
1312
}
1413

1514
void FinalizeExternal(napi_env env, void* data, void* hint) {
16-
free(data);
15+
int *actual_value = data;
16+
NAPI_ASSERT_RETURN_VOID(env, actual_value == &test_value,
17+
"The correct pointer was passed to the finalizer");
1718
finalize_count++;
1819
}
1920

@@ -33,13 +34,10 @@ napi_value CreateExternal(napi_env env, napi_callback_info info) {
3334
}
3435

3536
napi_value CreateExternalWithFinalize(napi_env env, napi_callback_info info) {
36-
int* data = malloc(sizeof(int));
37-
*data = test_value;
38-
3937
napi_value result;
4038
NAPI_CALL(env,
4139
napi_create_external(env,
42-
data,
40+
&test_value,
4341
FinalizeExternal,
4442
NULL, /* finalize_hint */
4543
&result));

0 commit comments

Comments
 (0)