Skip to content

Commit b9fb0c6

Browse files
Gabriel Schulhofcodebytere
Gabriel Schulhof
authored andcommitted
test: convert most N-API tests from C++ to C
* Prefix functions with `static` to make them local * Remove anonymous namespaces * `nullptr` -> `NULL` * .cc -> .c and update binding.gyp * `static_cast<x>()` -> `(x)()` * Replace `new`/`delete` with `malloc()`/`free()` (only in test_callback_scope) * Move lambda out and convert to local function (only in test_callback_scope) * Remove superfluous `#include <vector>` (only in test_callback_scope_recurse) Some tests are best left as C++. ```bash ls -l test/{node-api,js-native-api}/*/*.cc ``` for those remaining as C++ tests. Signed-off-by: Gabriel Schulhof <[email protected]> PR-URL: #34615 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]>
1 parent 35fdfb4 commit b9fb0c6

File tree

8 files changed

+67
-77
lines changed

8 files changed

+67
-77
lines changed

test/node-api/test_async/binding.gyp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"targets": [
33
{
44
"target_name": "test_async",
5-
"sources": [ "test_async.cc" ]
5+
"sources": [ "test_async.c" ]
66
}
77
]
88
}

test/node-api/test_async/test_async.cc test/node-api/test_async/test_async.c

+30-30
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@ typedef struct {
1919
napi_async_work _request;
2020
} carrier;
2121

22-
carrier the_carrier;
23-
carrier async_carrier[MAX_CANCEL_THREADS];
22+
static carrier the_carrier;
23+
static carrier async_carrier[MAX_CANCEL_THREADS];
2424

25-
void Execute(napi_env env, void* data) {
25+
static void Execute(napi_env env, void* data) {
2626
#if defined _WIN32
2727
Sleep(1000);
2828
#else
2929
sleep(1);
3030
#endif
31-
carrier* c = static_cast<carrier*>(data);
31+
carrier* c = (carrier*)(data);
3232

3333
assert(c == &the_carrier);
3434

3535
c->_output = c->_input * 2;
3636
}
3737

38-
void Complete(napi_env env, napi_status status, void* data) {
39-
carrier* c = static_cast<carrier*>(data);
38+
static void Complete(napi_env env, napi_status status, void* data) {
39+
carrier* c = (carrier*)(data);
4040

4141
if (c != &the_carrier) {
42-
napi_throw_type_error(env, nullptr, "Wrong data parameter to Complete.");
42+
napi_throw_type_error(env, NULL, "Wrong data parameter to Complete.");
4343
return;
4444
}
4545

4646
if (status != napi_ok) {
47-
napi_throw_type_error(env, nullptr, "Execute callback failed.");
47+
napi_throw_type_error(env, NULL, "Execute callback failed.");
4848
return;
4949
}
5050

@@ -66,7 +66,7 @@ void Complete(napi_env env, napi_status status, void* data) {
6666
NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, c->_request));
6767
}
6868

69-
napi_value Test(napi_env env, napi_callback_info info) {
69+
static napi_value Test(napi_env env, napi_callback_info info) {
7070
size_t argc = 3;
7171
napi_value argv[3];
7272
napi_value _this;
@@ -101,16 +101,16 @@ napi_value Test(napi_env env, napi_callback_info info) {
101101
NAPI_CALL(env,
102102
napi_queue_async_work(env, the_carrier._request));
103103

104-
return nullptr;
104+
return NULL;
105105
}
106106

107-
void BusyCancelComplete(napi_env env, napi_status status, void* data) {
108-
carrier* c = static_cast<carrier*>(data);
107+
static void BusyCancelComplete(napi_env env, napi_status status, void* data) {
108+
carrier* c = (carrier*)(data);
109109
NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, c->_request));
110110
}
111111

112-
void CancelComplete(napi_env env, napi_status status, void* data) {
113-
carrier* c = static_cast<carrier*>(data);
112+
static void CancelComplete(napi_env env, napi_status status, void* data) {
113+
carrier* c = (carrier*)(data);
114114

115115
if (status == napi_cancelled) {
116116
// ok we got the status we expected so make the callback to
@@ -122,22 +122,22 @@ void CancelComplete(napi_env env, napi_status status, void* data) {
122122
NAPI_CALL_RETURN_VOID(env, napi_get_global(env, &global));
123123
napi_value result;
124124
NAPI_CALL_RETURN_VOID(env,
125-
napi_call_function(env, global, callback, 0, nullptr, &result));
125+
napi_call_function(env, global, callback, 0, NULL, &result));
126126
}
127127

128128
NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, c->_request));
129129
NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, c->_callback));
130130
}
131131

132-
void CancelExecute(napi_env env, void* data) {
132+
static void CancelExecute(napi_env env, void* data) {
133133
#if defined _WIN32
134134
Sleep(1000);
135135
#else
136136
sleep(1);
137137
#endif
138138
}
139139

140-
napi_value TestCancel(napi_env env, napi_callback_info info) {
140+
static napi_value TestCancel(napi_env env, napi_callback_info info) {
141141
size_t argc = 1;
142142
napi_value argv[1];
143143
napi_value _this;
@@ -150,7 +150,7 @@ napi_value TestCancel(napi_env env, napi_callback_info info) {
150150
// make sure the work we are going to cancel will not be
151151
// able to start by using all the threads in the pool
152152
for (int i = 1; i < MAX_CANCEL_THREADS; i++) {
153-
NAPI_CALL(env, napi_create_async_work(env, nullptr, resource_name,
153+
NAPI_CALL(env, napi_create_async_work(env, NULL, resource_name,
154154
CancelExecute, BusyCancelComplete,
155155
&async_carrier[i], &async_carrier[i]._request));
156156
NAPI_CALL(env, napi_queue_async_work(env, async_carrier[i]._request));
@@ -162,20 +162,20 @@ napi_value TestCancel(napi_env env, napi_callback_info info) {
162162
// workers above.
163163
NAPI_CALL(env,
164164
napi_get_cb_info(env, info, &argc, argv, &_this, &data));
165-
NAPI_CALL(env, napi_create_async_work(env, nullptr, resource_name,
165+
NAPI_CALL(env, napi_create_async_work(env, NULL, resource_name,
166166
CancelExecute, CancelComplete,
167167
&async_carrier[0], &async_carrier[0]._request));
168168
NAPI_CALL(env,
169169
napi_create_reference(env, argv[0], 1, &async_carrier[0]._callback));
170170
NAPI_CALL(env, napi_queue_async_work(env, async_carrier[0]._request));
171171
NAPI_CALL(env, napi_cancel_async_work(env, async_carrier[0]._request));
172-
return nullptr;
172+
return NULL;
173173
}
174174

175175
struct {
176176
napi_ref ref;
177177
napi_async_work work;
178-
} repeated_work_info = { nullptr, nullptr };
178+
} repeated_work_info = { NULL, NULL };
179179

180180
static void RepeatedWorkerThread(napi_env env, void* data) {}
181181

@@ -187,33 +187,33 @@ static void RepeatedWorkComplete(napi_env env, napi_status status, void* data) {
187187
napi_delete_async_work(env, repeated_work_info.work));
188188
NAPI_CALL_RETURN_VOID(env,
189189
napi_delete_reference(env, repeated_work_info.ref));
190-
repeated_work_info.work = nullptr;
191-
repeated_work_info.ref = nullptr;
190+
repeated_work_info.work = NULL;
191+
repeated_work_info.ref = NULL;
192192
NAPI_CALL_RETURN_VOID(env,
193193
napi_create_uint32(env, (uint32_t)status, &js_status));
194194
NAPI_CALL_RETURN_VOID(env,
195-
napi_call_function(env, cb, cb, 1, &js_status, nullptr));
195+
napi_call_function(env, cb, cb, 1, &js_status, NULL));
196196
}
197197

198198
static napi_value DoRepeatedWork(napi_env env, napi_callback_info info) {
199199
size_t argc = 1;
200200
napi_value cb, name;
201-
NAPI_ASSERT(env, repeated_work_info.ref == nullptr,
201+
NAPI_ASSERT(env, repeated_work_info.ref == NULL,
202202
"Reference left over from previous work");
203-
NAPI_ASSERT(env, repeated_work_info.work == nullptr,
203+
NAPI_ASSERT(env, repeated_work_info.work == NULL,
204204
"Work pointer left over from previous work");
205-
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &cb, nullptr, nullptr));
205+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &cb, NULL, NULL));
206206
NAPI_CALL(env, napi_create_reference(env, cb, 1, &repeated_work_info.ref));
207207
NAPI_CALL(env,
208208
napi_create_string_utf8(env, "Repeated Work", NAPI_AUTO_LENGTH, &name));
209209
NAPI_CALL(env,
210-
napi_create_async_work(env, nullptr, name, RepeatedWorkerThread,
210+
napi_create_async_work(env, NULL, name, RepeatedWorkerThread,
211211
RepeatedWorkComplete, &repeated_work_info, &repeated_work_info.work));
212212
NAPI_CALL(env, napi_queue_async_work(env, repeated_work_info.work));
213-
return nullptr;
213+
return NULL;
214214
}
215215

216-
napi_value Init(napi_env env, napi_value exports) {
216+
static napi_value Init(napi_env env, napi_value exports) {
217217
napi_property_descriptor properties[] = {
218218
DECLARE_NAPI_PROPERTY("Test", Test),
219219
DECLARE_NAPI_PROPERTY("TestCancel", TestCancel),

test/node-api/test_callback_scope/binding.cc test/node-api/test_callback_scope/binding.c

+22-23
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1+
#include <stdlib.h>
12
#include "node_api.h"
23
#include "uv.h"
34
#include "../../js-native-api/common.h"
45

5-
namespace {
6-
7-
napi_value RunInCallbackScope(napi_env env, napi_callback_info info) {
6+
static napi_value RunInCallbackScope(napi_env env, napi_callback_info info) {
87
size_t argc;
98
napi_value args[3];
109

11-
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr));
10+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL));
1211
NAPI_ASSERT(env, argc == 3 , "Wrong number of arguments");
1312

14-
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
13+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
1514

1615
napi_valuetype valuetype;
1716
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
@@ -29,7 +28,7 @@ napi_value RunInCallbackScope(napi_env env, napi_callback_info info) {
2928
napi_async_context context;
3029
NAPI_CALL(env, napi_async_init(env, args[0], args[1], &context));
3130

32-
napi_callback_scope scope = nullptr;
31+
napi_callback_scope scope = NULL;
3332
NAPI_CALL(
3433
env,
3534
napi_open_callback_scope(env,
@@ -39,9 +38,9 @@ napi_value RunInCallbackScope(napi_env env, napi_callback_info info) {
3938

4039
// if the function has an exception pending after the call that is ok
4140
// so we don't use NAPI_CALL as we must close the callback scope regardless
42-
napi_value result = nullptr;
41+
napi_value result = NULL;
4342
napi_status function_call_result =
44-
napi_call_function(env, args[0], args[2], 0, nullptr, &result);
43+
napi_call_function(env, args[0], args[2], 0, NULL, &result);
4544
if (function_call_result != napi_ok) {
4645
GET_AND_THROW_LAST_ERROR((env));
4746
}
@@ -52,29 +51,29 @@ napi_value RunInCallbackScope(napi_env env, napi_callback_info info) {
5251
return result;
5352
}
5453

55-
static napi_env shared_env = nullptr;
56-
static napi_deferred deferred = nullptr;
54+
static napi_env shared_env = NULL;
55+
static napi_deferred deferred = NULL;
5756

5857
static void Callback(uv_work_t* req, int ignored) {
5958
napi_env env = shared_env;
6059

61-
napi_handle_scope handle_scope = nullptr;
60+
napi_handle_scope handle_scope = NULL;
6261
NAPI_CALL_RETURN_VOID(env, napi_open_handle_scope(env, &handle_scope));
6362

6463
napi_value resource_name;
6564
NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(
6665
env, "test", NAPI_AUTO_LENGTH, &resource_name));
6766
napi_async_context context;
6867
NAPI_CALL_RETURN_VOID(env,
69-
napi_async_init(env, nullptr, resource_name, &context));
68+
napi_async_init(env, NULL, resource_name, &context));
7069

7170
napi_value resource_object;
7271
NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &resource_object));
7372

7473
napi_value undefined_value;
7574
NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined_value));
7675

77-
napi_callback_scope scope = nullptr;
76+
napi_callback_scope scope = NULL;
7877
NAPI_CALL_RETURN_VOID(env, napi_open_callback_scope(env,
7978
resource_object,
8079
context,
@@ -87,28 +86,30 @@ static void Callback(uv_work_t* req, int ignored) {
8786

8887
NAPI_CALL_RETURN_VOID(env, napi_close_handle_scope(env, handle_scope));
8988
NAPI_CALL_RETURN_VOID(env, napi_async_destroy(env, context));
90-
delete req;
89+
free(req);
9190
}
9291

93-
napi_value TestResolveAsync(napi_env env, napi_callback_info info) {
94-
napi_value promise = nullptr;
95-
if (deferred == nullptr) {
92+
static void NoopWork(uv_work_t* work) { (void) work; }
93+
94+
static napi_value TestResolveAsync(napi_env env, napi_callback_info info) {
95+
napi_value promise = NULL;
96+
if (deferred == NULL) {
9697
shared_env = env;
9798
NAPI_CALL(env, napi_create_promise(env, &deferred, &promise));
9899

99-
uv_loop_t* loop = nullptr;
100+
uv_loop_t* loop = NULL;
100101
NAPI_CALL(env, napi_get_uv_event_loop(env, &loop));
101102

102-
uv_work_t* req = new uv_work_t();
103+
uv_work_t* req = malloc(sizeof(*req));
103104
uv_queue_work(loop,
104105
req,
105-
[](uv_work_t*) {},
106+
NoopWork,
106107
Callback);
107108
}
108109
return promise;
109110
}
110111

111-
napi_value Init(napi_env env, napi_value exports) {
112+
static napi_value Init(napi_env env, napi_value exports) {
112113
napi_property_descriptor descriptors[] = {
113114
DECLARE_NAPI_PROPERTY("runInCallbackScope", RunInCallbackScope),
114115
DECLARE_NAPI_PROPERTY("testResolveAsync", TestResolveAsync)
@@ -120,6 +121,4 @@ napi_value Init(napi_env env, napi_value exports) {
120121
return exports;
121122
}
122123

123-
} // anonymous namespace
124-
125124
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

test/node-api/test_callback_scope/binding.gyp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
'target_name': 'binding',
55
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
6-
'sources': [ 'binding.cc' ]
6+
'sources': [ 'binding.c' ]
77
}
88
]
99
}

test/node-api/test_cleanup_hook/binding.cc test/node-api/test_cleanup_hook/binding.c

+6-10
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22
#include "uv.h"
33
#include "../../js-native-api/common.h"
44

5-
namespace {
6-
7-
void cleanup(void* arg) {
8-
printf("cleanup(%d)\n", *static_cast<int*>(arg));
5+
static void cleanup(void* arg) {
6+
printf("cleanup(%d)\n", *(int*)(arg));
97
}
108

11-
int secret = 42;
12-
int wrong_secret = 17;
9+
static int secret = 42;
10+
static int wrong_secret = 17;
1311

14-
napi_value Init(napi_env env, napi_value exports) {
12+
static napi_value Init(napi_env env, napi_value exports) {
1513
napi_add_env_cleanup_hook(env, cleanup, &wrong_secret);
1614
napi_add_env_cleanup_hook(env, cleanup, &secret);
1715
napi_remove_env_cleanup_hook(env, cleanup, &wrong_secret);
1816

19-
return nullptr;
17+
return NULL;
2018
}
2119

22-
} // anonymous namespace
23-
2420
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

test/node-api/test_cleanup_hook/binding.gyp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
'target_name': 'binding',
55
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
6-
'sources': [ 'binding.cc' ]
6+
'sources': [ 'binding.c' ]
77
}
88
]
99
}

0 commit comments

Comments
 (0)