Skip to content

Commit

Permalink
test: rename n-api to node-api
Browse files Browse the repository at this point in the history
This renames the macros used in the tests from `NAPI_*` to
`NODE_API_*`.

PR-URL: #37217
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Beth Griggs <bgriggs@redhat.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
Gabriel Schulhof authored and danielleadams committed Feb 16, 2021
1 parent 3e2746f commit 48a634e
Show file tree
Hide file tree
Showing 52 changed files with 1,374 additions and 1,451 deletions.
20 changes: 10 additions & 10 deletions test/js-native-api/2_function_arguments/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
28 changes: 14 additions & 14 deletions test/js-native-api/3_callbacks/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,53 @@
static napi_value RunCallback(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 == 1,
NODE_API_ASSERT(env, argc == 1,
"Wrong number of arguments. Expects a single argument.");

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

napi_valuetype valuetype1;
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
NAPI_ASSERT(env, valuetype1 == napi_undefined,
NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype1));
NODE_API_ASSERT(env, valuetype1 == napi_undefined,
"Additional arguments should be undefined.");

napi_value argv[1];
const char* str = "hello world";
size_t str_len = strlen(str);
NAPI_CALL(env, napi_create_string_utf8(env, str, str_len, argv));
NODE_API_CALL(env, napi_create_string_utf8(env, str, str_len, argv));

napi_value global;
NAPI_CALL(env, napi_get_global(env, &global));
NODE_API_CALL(env, napi_get_global(env, &global));

napi_value cb = args[0];
NAPI_CALL(env, napi_call_function(env, global, cb, 1, argv, NULL));
NODE_API_CALL(env, napi_call_function(env, global, cb, 1, argv, NULL));

return NULL;
}

static napi_value RunCallbackWithRecv(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_value cb = args[0];
napi_value recv = args[1];
NAPI_CALL(env, napi_call_function(env, recv, cb, 0, NULL, NULL));
NODE_API_CALL(env, napi_call_function(env, recv, cb, 0, NULL, NULL));
return NULL;
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor desc[2] = {
DECLARE_NAPI_PROPERTY("RunCallback", RunCallback),
DECLARE_NAPI_PROPERTY("RunCallbackWithRecv", RunCallbackWithRecv),
DECLARE_NODE_API_PROPERTY("RunCallback", RunCallback),
DECLARE_NODE_API_PROPERTY("RunCallbackWithRecv", RunCallbackWithRecv),
};
NAPI_CALL(env, napi_define_properties(env, exports, 2, desc));
NODE_API_CALL(env, napi_define_properties(env, exports, 2, desc));
return exports;
}
EXTERN_C_END
8 changes: 4 additions & 4 deletions test/js-native-api/4_object_factory/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
static napi_value CreateObject(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_value obj;
NAPI_CALL(env, napi_create_object(env, &obj));
NODE_API_CALL(env, napi_create_object(env, &obj));

NAPI_CALL(env, napi_set_named_property(env, obj, "msg", args[0]));
NODE_API_CALL(env, napi_set_named_property(env, obj, "msg", args[0]));

return obj;
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
NAPI_CALL(env,
NODE_API_CALL(env,
napi_create_function(env, "exports", -1, CreateObject, NULL, &exports));
return exports;
}
Expand Down
6 changes: 3 additions & 3 deletions test/js-native-api/5_function_factory/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@

static napi_value MyFunction(napi_env env, napi_callback_info info) {
napi_value str;
NAPI_CALL(env, napi_create_string_utf8(env, "hello world", -1, &str));
NODE_API_CALL(env, napi_create_string_utf8(env, "hello world", -1, &str));
return str;
}

static napi_value CreateFunction(napi_env env, napi_callback_info info) {
napi_value fn;
NAPI_CALL(env,
NODE_API_CALL(env,
napi_create_function(env, "theFunction", -1, MyFunction, NULL, &fn));
return fn;
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
NAPI_CALL(env,
NODE_API_CALL(env,
napi_create_function(env, "exports", -1, CreateFunction, NULL, &exports));
return exports;
}
Expand Down
62 changes: 30 additions & 32 deletions test/js-native-api/6_object_wrap/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,50 @@ void MyObject::Init(napi_env env, napi_value exports) {
{ "value", nullptr, nullptr, GetValue, SetValue, 0, napi_default, 0 },
{ "valueReadonly", nullptr, nullptr, GetValue, nullptr, 0, napi_default,
0 },
DECLARE_NAPI_PROPERTY("plusOne", PlusOne),
DECLARE_NAPI_PROPERTY("multiply", Multiply),
DECLARE_NODE_API_PROPERTY("plusOne", PlusOne),
DECLARE_NODE_API_PROPERTY("multiply", Multiply),
};

napi_value cons;
NAPI_CALL_RETURN_VOID(env, napi_define_class(
NODE_API_CALL_RETURN_VOID(env, napi_define_class(
env, "MyObject", -1, New, nullptr,
sizeof(properties) / sizeof(napi_property_descriptor),
properties, &cons));

NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, cons, 1, &constructor));
NODE_API_CALL_RETURN_VOID(env,
napi_create_reference(env, cons, 1, &constructor));

NAPI_CALL_RETURN_VOID(env,
NODE_API_CALL_RETURN_VOID(env,
napi_set_named_property(env, exports, "MyObject", cons));
}

napi_value MyObject::New(napi_env env, napi_callback_info info) {
napi_value new_target;
NAPI_CALL(env, napi_get_new_target(env, info, &new_target));
NODE_API_CALL(env, napi_get_new_target(env, info, &new_target));
bool is_constructor = (new_target != nullptr);

size_t argc = 1;
napi_value args[1];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));

if (is_constructor) {
// Invoked as constructor: `new MyObject(...)`
double value = 0;

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

if (valuetype != napi_undefined) {
NAPI_CALL(env, napi_get_value_double(env, args[0], &value));
NODE_API_CALL(env, napi_get_value_double(env, args[0], &value));
}

MyObject* obj = new MyObject(value);

obj->env_ = env;
NAPI_CALL(env, napi_wrap(env,
_this,
obj,
MyObject::Destructor,
nullptr, // finalize_hint
&obj->wrapper_));
NODE_API_CALL(env,
napi_wrap(env, _this, obj, MyObject::Destructor,
nullptr /* finalize_hint */, &obj->wrapper_));

return _this;
}
Expand All @@ -74,24 +72,24 @@ napi_value MyObject::New(napi_env env, napi_callback_info info) {
napi_value argv[1] = {args[0]};

napi_value cons;
NAPI_CALL(env, napi_get_reference_value(env, constructor, &cons));
NODE_API_CALL(env, napi_get_reference_value(env, constructor, &cons));

napi_value instance;
NAPI_CALL(env, napi_new_instance(env, cons, argc, argv, &instance));
NODE_API_CALL(env, napi_new_instance(env, cons, argc, argv, &instance));

return instance;
}

napi_value MyObject::GetValue(napi_env env, napi_callback_info info) {
napi_value _this;
NAPI_CALL(env,
NODE_API_CALL(env,
napi_get_cb_info(env, info, nullptr, nullptr, &_this, nullptr));

MyObject* obj;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&obj)));
NODE_API_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&obj)));

napi_value num;
NAPI_CALL(env, napi_create_double(env, obj->value_, &num));
NODE_API_CALL(env, napi_create_double(env, obj->value_, &num));

return num;
}
Expand All @@ -100,28 +98,28 @@ napi_value MyObject::SetValue(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));

MyObject* obj;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&obj)));
NODE_API_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&obj)));

NAPI_CALL(env, napi_get_value_double(env, args[0], &obj->value_));
NODE_API_CALL(env, napi_get_value_double(env, args[0], &obj->value_));

return nullptr;
}

napi_value MyObject::PlusOne(napi_env env, napi_callback_info info) {
napi_value _this;
NAPI_CALL(env,
NODE_API_CALL(env,
napi_get_cb_info(env, info, nullptr, nullptr, &_this, nullptr));

MyObject* obj;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&obj)));
NODE_API_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&obj)));

obj->value_ += 1;

napi_value num;
NAPI_CALL(env, napi_create_double(env, obj->value_, &num));
NODE_API_CALL(env, napi_create_double(env, obj->value_, &num));

return num;
}
Expand All @@ -130,25 +128,25 @@ napi_value MyObject::Multiply(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));

double multiple = 1;
if (argc >= 1) {
NAPI_CALL(env, napi_get_value_double(env, args[0], &multiple));
NODE_API_CALL(env, napi_get_value_double(env, args[0], &multiple));
}

MyObject* obj;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&obj)));
NODE_API_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&obj)));

napi_value cons;
NAPI_CALL(env, napi_get_reference_value(env, constructor, &cons));
NODE_API_CALL(env, napi_get_reference_value(env, constructor, &cons));

const int kArgCount = 1;
napi_value argv[kArgCount];
NAPI_CALL(env, napi_create_double(env, obj->value_ * multiple, argv));
NODE_API_CALL(env, napi_create_double(env, obj->value_ * multiple, argv));

napi_value instance;
NAPI_CALL(env, napi_new_instance(env, cons, kArgCount, argv, &instance));
NODE_API_CALL(env, napi_new_instance(env, cons, kArgCount, argv, &instance));

return instance;
}
Expand Down
13 changes: 7 additions & 6 deletions test/js-native-api/7_factory_wrap/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
napi_value CreateObject(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, nullptr, nullptr));
NODE_API_CALL(env,
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));

napi_value instance;
NAPI_CALL(env, MyObject::NewInstance(env, args[0], &instance));
NODE_API_CALL(env, MyObject::NewInstance(env, args[0], &instance));

return instance;
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
NAPI_CALL(env, MyObject::Init(env));
NODE_API_CALL(env, MyObject::Init(env));

napi_property_descriptor descriptors[] = {
DECLARE_NAPI_GETTER("finalizeCount", MyObject::GetFinalizeCount),
DECLARE_NAPI_PROPERTY("createObject", CreateObject),
DECLARE_NODE_API_GETTER("finalizeCount", MyObject::GetFinalizeCount),
DECLARE_NODE_API_PROPERTY("createObject", CreateObject),
};

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 48a634e

Please sign in to comment.