Skip to content

Commit

Permalink
test: rename n-api to node-api (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi authored Sep 14, 2023
1 parent d7cc080 commit 1165480
Show file tree
Hide file tree
Showing 57 changed files with 1,414 additions and 1,414 deletions.
4 changes: 2 additions & 2 deletions packages/bench/src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ static napi_value js_fib(napi_env env, napi_callback_info info) {
#define EXPORT_FUNCTION(env, exports, name, f) \
do { \
napi_value f##_fn; \
NAPI_CALL((env), napi_create_function((env), NULL, NAPI_AUTO_LENGTH, (f), NULL, &(f##_fn))); \
NAPI_CALL((env), napi_set_named_property((env), (exports), (name), (f##_fn))); \
NODE_API_CALL((env), napi_create_function((env), NULL, NAPI_AUTO_LENGTH, (f), NULL, &(f##_fn))); \
NODE_API_CALL((env), napi_set_named_property((env), (exports), (name), (f##_fn))); \
} while (0)

NAPI_MODULE_INIT() {
Expand Down
8 changes: 4 additions & 4 deletions packages/emnapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Create `hello.c`.
```c
#include <node_api.h>

#define NAPI_CALL(env, the_call) \
#define NODE_API_CALL(env, the_call) \
do { \
if ((the_call) != napi_ok) { \
const napi_extended_error_info *error_info; \
Expand All @@ -130,15 +130,15 @@ Create `hello.c`.
static napi_value js_hello(napi_env env, napi_callback_info info) {
napi_value world;
const char* str = "world";
NAPI_CALL(env, napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &world));
NODE_API_CALL(env, napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &world));
return world;
}

NAPI_MODULE_INIT() {
napi_value hello;
NAPI_CALL(env, napi_create_function(env, "hello", NAPI_AUTO_LENGTH,
NODE_API_CALL(env, napi_create_function(env, "hello", NAPI_AUTO_LENGTH,
js_hello, NULL, &hello));
NAPI_CALL(env, napi_set_named_property(env, exports, "hello", hello));
NODE_API_CALL(env, napi_set_named_property(env, exports, "hello", hello));
return exports;
}
```
Expand Down
20 changes: 10 additions & 10 deletions packages/test/arg/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@
static napi_value Add(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc >= 2, "Wrong number of arguments");
NODE_API_ASSERT(env, argc >= 2, "Wrong number of arguments");

napi_valuetype valuetype0;
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0));

napi_valuetype valuetype1;
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype1));

NAPI_ASSERT(env, valuetype0 == napi_number && valuetype1 == napi_number,
NODE_API_ASSERT(env, valuetype0 == napi_number && valuetype1 == napi_number,
"Wrong argument type. Numbers expected.");

double value0;
NAPI_CALL(env, napi_get_value_double(env, args[0], &value0));
NODE_API_CALL(env, napi_get_value_double(env, args[0], &value0));

double value1;
NAPI_CALL(env, napi_get_value_double(env, args[1], &value1));
NODE_API_CALL(env, napi_get_value_double(env, args[1], &value1));

napi_value sum;
NAPI_CALL(env, napi_create_double(env, value0 + value1, &sum));
NODE_API_CALL(env, napi_create_double(env, value0 + value1, &sum));

return sum;
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor desc = DECLARE_NAPI_PROPERTY("add", Add);
NAPI_CALL(env, napi_define_properties(env, exports, 1, &desc));
napi_property_descriptor desc = DECLARE_NODE_API_PROPERTY("add", Add);
NODE_API_CALL(env, napi_define_properties(env, exports, 1, &desc));
return exports;
}
EXTERN_C_END
104 changes: 52 additions & 52 deletions packages/test/array/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,81 @@
static napi_value TestGetElement(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc >= 2, "Wrong number of arguments");
NODE_API_ASSERT(env, argc >= 2, "Wrong number of arguments");

napi_valuetype valuetype0;
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0));

NAPI_ASSERT(env, valuetype0 == napi_object,
NODE_API_ASSERT(env, valuetype0 == napi_object,
"Wrong type of arguments. Expects an array as first argument.");

napi_valuetype valuetype1;
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype1));

NAPI_ASSERT(env, valuetype1 == napi_number,
NODE_API_ASSERT(env, valuetype1 == napi_number,
"Wrong type of arguments. Expects an integer as second argument.");

napi_value array = args[0];
int32_t index;
NAPI_CALL(env, napi_get_value_int32(env, args[1], &index));
NODE_API_CALL(env, napi_get_value_int32(env, args[1], &index));

NAPI_ASSERT(env, index >= 0, "Invalid index. Expects a positive integer.");
NODE_API_ASSERT(env, index >= 0, "Invalid index. Expects a positive integer.");

bool isarray;
NAPI_CALL(env, napi_is_array(env, array, &isarray));
NODE_API_CALL(env, napi_is_array(env, array, &isarray));

if (!isarray) {
return NULL;
}

uint32_t length;
NAPI_CALL(env, napi_get_array_length(env, array, &length));
NODE_API_CALL(env, napi_get_array_length(env, array, &length));

NAPI_ASSERT(env, ((uint32_t)index < length), "Index out of bounds!");
NODE_API_ASSERT(env, ((uint32_t)index < length), "Index out of bounds!");

napi_value ret;
NAPI_CALL(env, napi_get_element(env, array, index, &ret));
NODE_API_CALL(env, napi_get_element(env, array, index, &ret));

return ret;
}

static napi_value TestHasElement(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc >= 2, "Wrong number of arguments");
NODE_API_ASSERT(env, argc >= 2, "Wrong number of arguments");

napi_valuetype valuetype0;
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0));

NAPI_ASSERT(env, valuetype0 == napi_object,
NODE_API_ASSERT(env, valuetype0 == napi_object,
"Wrong type of arguments. Expects an array as first argument.");

napi_valuetype valuetype1;
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype1));

NAPI_ASSERT(env, valuetype1 == napi_number,
NODE_API_ASSERT(env, valuetype1 == napi_number,
"Wrong type of arguments. Expects an integer as second argument.");

napi_value array = args[0];
int32_t index;
NAPI_CALL(env, napi_get_value_int32(env, args[1], &index));
NODE_API_CALL(env, napi_get_value_int32(env, args[1], &index));

bool isarray;
NAPI_CALL(env, napi_is_array(env, array, &isarray));
NODE_API_CALL(env, napi_is_array(env, array, &isarray));

if (!isarray) {
return NULL;
}

bool has_element;
NAPI_CALL(env, napi_has_element(env, array, index, &has_element));
NODE_API_CALL(env, napi_has_element(env, array, index, &has_element));

napi_value ret;
NAPI_CALL(env, napi_get_boolean(env, has_element, &ret));
NODE_API_CALL(env, napi_get_boolean(env, has_element, &ret));

return ret;
}
Expand All @@ -88,60 +88,60 @@ static napi_value TestDeleteElement(napi_env env, napi_callback_info info) {
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, "Wrong number of arguments");
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_ASSERT(env, argc == 2, "Wrong number of arguments");

napi_valuetype valuetype0;
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
NAPI_ASSERT(env, valuetype0 == napi_object,
NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0));
NODE_API_ASSERT(env, valuetype0 == napi_object,
"Wrong type of arguments. Expects an array as first argument.");

napi_valuetype valuetype1;
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
NAPI_ASSERT(env, valuetype1 == napi_number,
NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype1));
NODE_API_ASSERT(env, valuetype1 == napi_number,
"Wrong type of arguments. Expects an integer as second argument.");

napi_value array = args[0];
int32_t index;
bool result;
napi_value ret;

NAPI_CALL(env, napi_get_value_int32(env, args[1], &index));
NAPI_CALL(env, napi_is_array(env, array, &result));
NODE_API_CALL(env, napi_get_value_int32(env, args[1], &index));
NODE_API_CALL(env, napi_is_array(env, array, &result));

if (!result) {
return NULL;
}

NAPI_CALL(env, napi_delete_element(env, array, index, &result));
NAPI_CALL(env, napi_get_boolean(env, result, &ret));
NODE_API_CALL(env, napi_delete_element(env, array, index, &result));
NODE_API_CALL(env, napi_get_boolean(env, result, &ret));

return ret;
}

static napi_value New(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments");

napi_valuetype valuetype0;
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0));

NAPI_ASSERT(env, valuetype0 == napi_object,
NODE_API_ASSERT(env, valuetype0 == napi_object,
"Wrong type of arguments. Expects an array as first argument.");

napi_value ret;
NAPI_CALL(env, napi_create_array(env, &ret));
NODE_API_CALL(env, napi_create_array(env, &ret));

uint32_t i, length;
NAPI_CALL(env, napi_get_array_length(env, args[0], &length));
NODE_API_CALL(env, napi_get_array_length(env, args[0], &length));

for (i = 0; i < length; i++) {
napi_value e;
NAPI_CALL(env, napi_get_element(env, args[0], i, &e));
NAPI_CALL(env, napi_set_element(env, ret, i, e));
NODE_API_CALL(env, napi_get_element(env, args[0], i, &e));
NODE_API_CALL(env, napi_set_element(env, ret, i, e));
}

return ret;
Expand All @@ -150,36 +150,36 @@ static napi_value New(napi_env env, napi_callback_info info) {
static napi_value NewWithLength(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments");

napi_valuetype valuetype0;
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0));

NAPI_ASSERT(env, valuetype0 == napi_number,
NODE_API_ASSERT(env, valuetype0 == napi_number,
"Wrong type of arguments. Expects an integer the first argument.");

int32_t array_length;
NAPI_CALL(env, napi_get_value_int32(env, args[0], &array_length));
NODE_API_CALL(env, napi_get_value_int32(env, args[0], &array_length));

napi_value ret;
NAPI_CALL(env, napi_create_array_with_length(env, array_length, &ret));
NODE_API_CALL(env, napi_create_array_with_length(env, array_length, &ret));

return ret;
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptors[] = {
DECLARE_NAPI_PROPERTY("TestGetElement", TestGetElement),
DECLARE_NAPI_PROPERTY("TestHasElement", TestHasElement),
DECLARE_NAPI_PROPERTY("TestDeleteElement", TestDeleteElement),
DECLARE_NAPI_PROPERTY("New", New),
DECLARE_NAPI_PROPERTY("NewWithLength", NewWithLength),
DECLARE_NODE_API_PROPERTY("TestGetElement", TestGetElement),
DECLARE_NODE_API_PROPERTY("TestHasElement", TestHasElement),
DECLARE_NODE_API_PROPERTY("TestDeleteElement", TestDeleteElement),
DECLARE_NODE_API_PROPERTY("New", New),
DECLARE_NODE_API_PROPERTY("NewWithLength", NewWithLength),
};

NAPI_CALL(env, napi_define_properties(
NODE_API_CALL(env, napi_define_properties(
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));

return exports;
Expand Down
Loading

0 comments on commit 1165480

Please sign in to comment.