Skip to content

Commit

Permalink
test: convert most N-API tests from C++ to C
Browse files Browse the repository at this point in the history
* 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 <gabriel.schulhof@intel.com>
PR-URL: #34615
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
  • Loading branch information
Gabriel Schulhof authored and codebytere committed Aug 6, 2020
1 parent 35fdfb4 commit b9fb0c6
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 77 deletions.
2 changes: 1 addition & 1 deletion test/node-api/test_async/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"targets": [
{
"target_name": "test_async",
"sources": [ "test_async.cc" ]
"sources": [ "test_async.c" ]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,32 @@ typedef struct {
napi_async_work _request;
} carrier;

carrier the_carrier;
carrier async_carrier[MAX_CANCEL_THREADS];
static carrier the_carrier;
static carrier async_carrier[MAX_CANCEL_THREADS];

void Execute(napi_env env, void* data) {
static void Execute(napi_env env, void* data) {
#if defined _WIN32
Sleep(1000);
#else
sleep(1);
#endif
carrier* c = static_cast<carrier*>(data);
carrier* c = (carrier*)(data);

assert(c == &the_carrier);

c->_output = c->_input * 2;
}

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

if (c != &the_carrier) {
napi_throw_type_error(env, nullptr, "Wrong data parameter to Complete.");
napi_throw_type_error(env, NULL, "Wrong data parameter to Complete.");
return;
}

if (status != napi_ok) {
napi_throw_type_error(env, nullptr, "Execute callback failed.");
napi_throw_type_error(env, NULL, "Execute callback failed.");
return;
}

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

napi_value Test(napi_env env, napi_callback_info info) {
static napi_value Test(napi_env env, napi_callback_info info) {
size_t argc = 3;
napi_value argv[3];
napi_value _this;
Expand Down Expand Up @@ -101,16 +101,16 @@ napi_value Test(napi_env env, napi_callback_info info) {
NAPI_CALL(env,
napi_queue_async_work(env, the_carrier._request));

return nullptr;
return NULL;
}

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

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

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

NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, c->_request));
NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, c->_callback));
}

void CancelExecute(napi_env env, void* data) {
static void CancelExecute(napi_env env, void* data) {
#if defined _WIN32
Sleep(1000);
#else
sleep(1);
#endif
}

napi_value TestCancel(napi_env env, napi_callback_info info) {
static napi_value TestCancel(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value argv[1];
napi_value _this;
Expand All @@ -150,7 +150,7 @@ napi_value TestCancel(napi_env env, napi_callback_info info) {
// make sure the work we are going to cancel will not be
// able to start by using all the threads in the pool
for (int i = 1; i < MAX_CANCEL_THREADS; i++) {
NAPI_CALL(env, napi_create_async_work(env, nullptr, resource_name,
NAPI_CALL(env, napi_create_async_work(env, NULL, resource_name,
CancelExecute, BusyCancelComplete,
&async_carrier[i], &async_carrier[i]._request));
NAPI_CALL(env, napi_queue_async_work(env, async_carrier[i]._request));
Expand All @@ -162,20 +162,20 @@ napi_value TestCancel(napi_env env, napi_callback_info info) {
// workers above.
NAPI_CALL(env,
napi_get_cb_info(env, info, &argc, argv, &_this, &data));
NAPI_CALL(env, napi_create_async_work(env, nullptr, resource_name,
NAPI_CALL(env, napi_create_async_work(env, NULL, resource_name,
CancelExecute, CancelComplete,
&async_carrier[0], &async_carrier[0]._request));
NAPI_CALL(env,
napi_create_reference(env, argv[0], 1, &async_carrier[0]._callback));
NAPI_CALL(env, napi_queue_async_work(env, async_carrier[0]._request));
NAPI_CALL(env, napi_cancel_async_work(env, async_carrier[0]._request));
return nullptr;
return NULL;
}

struct {
napi_ref ref;
napi_async_work work;
} repeated_work_info = { nullptr, nullptr };
} repeated_work_info = { NULL, NULL };

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

Expand All @@ -187,33 +187,33 @@ static void RepeatedWorkComplete(napi_env env, napi_status status, void* data) {
napi_delete_async_work(env, repeated_work_info.work));
NAPI_CALL_RETURN_VOID(env,
napi_delete_reference(env, repeated_work_info.ref));
repeated_work_info.work = nullptr;
repeated_work_info.ref = nullptr;
repeated_work_info.work = NULL;
repeated_work_info.ref = NULL;
NAPI_CALL_RETURN_VOID(env,
napi_create_uint32(env, (uint32_t)status, &js_status));
NAPI_CALL_RETURN_VOID(env,
napi_call_function(env, cb, cb, 1, &js_status, nullptr));
napi_call_function(env, cb, cb, 1, &js_status, NULL));
}

static napi_value DoRepeatedWork(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value cb, name;
NAPI_ASSERT(env, repeated_work_info.ref == nullptr,
NAPI_ASSERT(env, repeated_work_info.ref == NULL,
"Reference left over from previous work");
NAPI_ASSERT(env, repeated_work_info.work == nullptr,
NAPI_ASSERT(env, repeated_work_info.work == NULL,
"Work pointer left over from previous work");
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &cb, nullptr, nullptr));
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &cb, NULL, NULL));
NAPI_CALL(env, napi_create_reference(env, cb, 1, &repeated_work_info.ref));
NAPI_CALL(env,
napi_create_string_utf8(env, "Repeated Work", NAPI_AUTO_LENGTH, &name));
NAPI_CALL(env,
napi_create_async_work(env, nullptr, name, RepeatedWorkerThread,
napi_create_async_work(env, NULL, name, RepeatedWorkerThread,
RepeatedWorkComplete, &repeated_work_info, &repeated_work_info.work));
NAPI_CALL(env, napi_queue_async_work(env, repeated_work_info.work));
return nullptr;
return NULL;
}

napi_value Init(napi_env env, napi_value exports) {
static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor properties[] = {
DECLARE_NAPI_PROPERTY("Test", Test),
DECLARE_NAPI_PROPERTY("TestCancel", TestCancel),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#include <stdlib.h>
#include "node_api.h"
#include "uv.h"
#include "../../js-native-api/common.h"

namespace {

napi_value RunInCallbackScope(napi_env env, napi_callback_info info) {
static napi_value RunInCallbackScope(napi_env env, napi_callback_info info) {
size_t argc;
napi_value args[3];

NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr));
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL));
NAPI_ASSERT(env, argc == 3 , "Wrong number of arguments");

NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

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

napi_callback_scope scope = nullptr;
napi_callback_scope scope = NULL;
NAPI_CALL(
env,
napi_open_callback_scope(env,
Expand All @@ -39,9 +38,9 @@ napi_value RunInCallbackScope(napi_env env, napi_callback_info info) {

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

static napi_env shared_env = nullptr;
static napi_deferred deferred = nullptr;
static napi_env shared_env = NULL;
static napi_deferred deferred = NULL;

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

napi_handle_scope handle_scope = nullptr;
napi_handle_scope handle_scope = NULL;
NAPI_CALL_RETURN_VOID(env, napi_open_handle_scope(env, &handle_scope));

napi_value resource_name;
NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(
env, "test", NAPI_AUTO_LENGTH, &resource_name));
napi_async_context context;
NAPI_CALL_RETURN_VOID(env,
napi_async_init(env, nullptr, resource_name, &context));
napi_async_init(env, NULL, resource_name, &context));

napi_value resource_object;
NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &resource_object));

napi_value undefined_value;
NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined_value));

napi_callback_scope scope = nullptr;
napi_callback_scope scope = NULL;
NAPI_CALL_RETURN_VOID(env, napi_open_callback_scope(env,
resource_object,
context,
Expand All @@ -87,28 +86,30 @@ static void Callback(uv_work_t* req, int ignored) {

NAPI_CALL_RETURN_VOID(env, napi_close_handle_scope(env, handle_scope));
NAPI_CALL_RETURN_VOID(env, napi_async_destroy(env, context));
delete req;
free(req);
}

napi_value TestResolveAsync(napi_env env, napi_callback_info info) {
napi_value promise = nullptr;
if (deferred == nullptr) {
static void NoopWork(uv_work_t* work) { (void) work; }

static napi_value TestResolveAsync(napi_env env, napi_callback_info info) {
napi_value promise = NULL;
if (deferred == NULL) {
shared_env = env;
NAPI_CALL(env, napi_create_promise(env, &deferred, &promise));

uv_loop_t* loop = nullptr;
uv_loop_t* loop = NULL;
NAPI_CALL(env, napi_get_uv_event_loop(env, &loop));

uv_work_t* req = new uv_work_t();
uv_work_t* req = malloc(sizeof(*req));
uv_queue_work(loop,
req,
[](uv_work_t*) {},
NoopWork,
Callback);
}
return promise;
}

napi_value Init(napi_env env, napi_value exports) {
static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptors[] = {
DECLARE_NAPI_PROPERTY("runInCallbackScope", RunInCallbackScope),
DECLARE_NAPI_PROPERTY("testResolveAsync", TestResolveAsync)
Expand All @@ -120,6 +121,4 @@ napi_value Init(napi_env env, napi_value exports) {
return exports;
}

} // anonymous namespace

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
2 changes: 1 addition & 1 deletion test/node-api/test_callback_scope/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
'target_name': 'binding',
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
'sources': [ 'binding.cc' ]
'sources': [ 'binding.c' ]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@
#include "uv.h"
#include "../../js-native-api/common.h"

namespace {

void cleanup(void* arg) {
printf("cleanup(%d)\n", *static_cast<int*>(arg));
static void cleanup(void* arg) {
printf("cleanup(%d)\n", *(int*)(arg));
}

int secret = 42;
int wrong_secret = 17;
static int secret = 42;
static int wrong_secret = 17;

napi_value Init(napi_env env, napi_value exports) {
static napi_value Init(napi_env env, napi_value exports) {
napi_add_env_cleanup_hook(env, cleanup, &wrong_secret);
napi_add_env_cleanup_hook(env, cleanup, &secret);
napi_remove_env_cleanup_hook(env, cleanup, &wrong_secret);

return nullptr;
return NULL;
}

} // anonymous namespace

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
2 changes: 1 addition & 1 deletion test/node-api/test_cleanup_hook/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
'target_name': 'binding',
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
'sources': [ 'binding.cc' ]
'sources': [ 'binding.c' ]
}
]
}
Loading

0 comments on commit b9fb0c6

Please sign in to comment.