forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_reference.c
194 lines (156 loc) · 6.24 KB
/
test_reference.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdlib.h>
#include <assert.h>
#include <js_native_api.h>
#include "../common.h"
static int test_value = 1;
static int finalize_count = 0;
static napi_ref test_reference = NULL;
static napi_value GetFinalizeCount(napi_env env, napi_callback_info info) {
napi_value result;
NAPI_CALL(env, napi_create_int32(env, finalize_count, &result));
return result;
}
static void FinalizeExternal(napi_env env, void* data, void* hint) {
int *actual_value = data;
NAPI_ASSERT_RETURN_VOID(env, actual_value == &test_value,
"The correct pointer was passed to the finalizer");
finalize_count++;
}
static napi_value CreateExternal(napi_env env, napi_callback_info info) {
int* data = &test_value;
napi_value result;
NAPI_CALL(env,
napi_create_external(env,
data,
NULL, /* finalize_cb */
NULL, /* finalize_hint */
&result));
finalize_count = 0;
return result;
}
static napi_value
CreateExternalWithFinalize(napi_env env, napi_callback_info info) {
napi_value result;
NAPI_CALL(env,
napi_create_external(env,
&test_value,
FinalizeExternal,
NULL, /* finalize_hint */
&result));
finalize_count = 0;
return result;
}
static napi_value CheckExternal(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value arg;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &arg, NULL, NULL));
NAPI_ASSERT(env, argc == 1, "Expected one argument.");
napi_valuetype argtype;
NAPI_CALL(env, napi_typeof(env, arg, &argtype));
NAPI_ASSERT(env, argtype == napi_external, "Expected an external value.");
void* data;
NAPI_CALL(env, napi_get_value_external(env, arg, &data));
NAPI_ASSERT(env, data != NULL && *(int*)data == test_value,
"An external data value of 1 was expected.");
return NULL;
}
static napi_value CreateReference(napi_env env, napi_callback_info info) {
NAPI_ASSERT(env, test_reference == NULL,
"The test allows only one reference at a time.");
size_t argc = 2;
napi_value args[2];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NAPI_ASSERT(env, argc == 2, "Expected two arguments.");
uint32_t initial_refcount;
NAPI_CALL(env, napi_get_value_uint32(env, args[1], &initial_refcount));
NAPI_CALL(env,
napi_create_reference(env, args[0], initial_refcount, &test_reference));
NAPI_ASSERT(env, test_reference != NULL,
"A reference should have been created.");
return NULL;
}
static napi_value DeleteReference(napi_env env, napi_callback_info info) {
NAPI_ASSERT(env, test_reference != NULL,
"A reference must have been created.");
NAPI_CALL(env, napi_delete_reference(env, test_reference));
test_reference = NULL;
return NULL;
}
static napi_value IncrementRefcount(napi_env env, napi_callback_info info) {
NAPI_ASSERT(env, test_reference != NULL,
"A reference must have been created.");
uint32_t refcount;
NAPI_CALL(env, napi_reference_ref(env, test_reference, &refcount));
napi_value result;
NAPI_CALL(env, napi_create_uint32(env, refcount, &result));
return result;
}
static napi_value DecrementRefcount(napi_env env, napi_callback_info info) {
NAPI_ASSERT(env, test_reference != NULL,
"A reference must have been created.");
uint32_t refcount;
NAPI_CALL(env, napi_reference_unref(env, test_reference, &refcount));
napi_value result;
NAPI_CALL(env, napi_create_uint32(env, refcount, &result));
return result;
}
static napi_value GetReferenceValue(napi_env env, napi_callback_info info) {
NAPI_ASSERT(env, test_reference != NULL,
"A reference must have been created.");
napi_value result;
NAPI_CALL(env, napi_get_reference_value(env, test_reference, &result));
return result;
}
static void DeleteBeforeFinalizeFinalizer(
napi_env env, void* finalize_data, void* finalize_hint) {
napi_ref* ref = (napi_ref*)finalize_data;
napi_value value;
assert(napi_get_reference_value(env, *ref, &value) == napi_ok);
assert(value == NULL);
napi_delete_reference(env, *ref);
free(ref);
}
static napi_value ValidateDeleteBeforeFinalize(napi_env env, napi_callback_info info) {
napi_value wrapObject;
size_t argc = 1;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &wrapObject, NULL, NULL));
napi_ref* ref_t = malloc(sizeof(napi_ref));
NAPI_CALL(env, napi_wrap(env,
wrapObject,
ref_t,
DeleteBeforeFinalizeFinalizer,
NULL,
NULL));
// Create a reference that will be eligible for collection at the same
// time as the wrapped object by passing in the same wrapObject.
// This means that the FinalizeOrderValidation callback may be run
// before the finalizer for the newly created reference (there is a finalizer
// behind the scenes even though it cannot be passed to napi_create_reference)
// The Finalizer for the wrap (which is different than the finalizer
// for the reference) calls napi_delete_reference validating that
// napi_delete_reference can be called before the finalizer for the
// reference runs.
NAPI_CALL(env, napi_create_reference(env, wrapObject, 0, ref_t));
return wrapObject;
}
EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptors[] = {
DECLARE_NAPI_GETTER("finalizeCount", GetFinalizeCount),
DECLARE_NAPI_PROPERTY("createExternal", CreateExternal),
DECLARE_NAPI_PROPERTY("createExternalWithFinalize",
CreateExternalWithFinalize),
DECLARE_NAPI_PROPERTY("checkExternal", CheckExternal),
DECLARE_NAPI_PROPERTY("createReference", CreateReference),
DECLARE_NAPI_PROPERTY("deleteReference", DeleteReference),
DECLARE_NAPI_PROPERTY("incrementRefcount", IncrementRefcount),
DECLARE_NAPI_PROPERTY("decrementRefcount", DecrementRefcount),
DECLARE_NAPI_GETTER("referenceValue", GetReferenceValue),
DECLARE_NAPI_PROPERTY("validateDeleteBeforeFinalize",
ValidateDeleteBeforeFinalize),
};
NAPI_CALL(env, napi_define_properties(
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
return exports;
}
EXTERN_C_END