Skip to content

Commit 21ed981

Browse files
author
Gabriel Schulhof
committed
examples: move to C examples that can be moved
Some C N-API examples use no C++ features other than `nullptr`. Those can be written in C. Signed-off-by: Gabriel Schulhof <gabriel.schulhof@intel.com> PR-URL: #170 Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
1 parent c1bdff0 commit 21ed981

File tree

12 files changed

+28
-28
lines changed

12 files changed

+28
-28
lines changed

1_hello_world/napi/binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"targets": [
33
{
44
"target_name": "hello",
5-
"sources": [ "hello.cc" ]
5+
"sources": [ "hello.c" ]
66
}
77
]
88
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <assert.h>
22
#include <node_api.h>
33

4-
napi_value Method(napi_env env, napi_callback_info info) {
4+
static napi_value Method(napi_env env, napi_callback_info info) {
55
napi_status status;
66
napi_value world;
77
status = napi_create_string_utf8(env, "world", 5, &world);
@@ -12,7 +12,7 @@ napi_value Method(napi_env env, napi_callback_info info) {
1212
#define DECLARE_NAPI_METHOD(name, func) \
1313
{ name, 0, func, 0, 0, 0, napi_default, 0 }
1414

15-
napi_value Init(napi_env env, napi_value exports) {
15+
static napi_value Init(napi_env env, napi_value exports) {
1616
napi_status status;
1717
napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method);
1818
status = napi_define_properties(env, exports, 1, &desc);
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#include <assert.h>
22
#include <node_api.h>
33
#include <stdio.h>
4-
napi_value Add(napi_env env, napi_callback_info info) {
4+
static napi_value Add(napi_env env, napi_callback_info info) {
55
napi_status status;
66

77
size_t argc = 2;
88
napi_value args[2];
9-
status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
9+
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
1010
assert(status == napi_ok);
1111

1212
if (argc < 2) {
13-
napi_throw_type_error(env, nullptr, "Wrong number of arguments");
14-
return nullptr;
13+
napi_throw_type_error(env, NULL, "Wrong number of arguments");
14+
return NULL;
1515
}
1616

1717
napi_valuetype valuetype0;
@@ -23,8 +23,8 @@ napi_value Add(napi_env env, napi_callback_info info) {
2323
assert(status == napi_ok);
2424

2525
if (valuetype0 != napi_number || valuetype1 != napi_number) {
26-
napi_throw_type_error(env, nullptr, "Wrong arguments");
27-
return nullptr;
26+
napi_throw_type_error(env, NULL, "Wrong arguments");
27+
return NULL;
2828
}
2929

3030
double value0;

2_function_arguments/napi/binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"targets": [
33
{
44
"target_name": "addon",
5-
"sources": [ "addon.cc" ]
5+
"sources": [ "addon.c" ]
66
}
77
]
88
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include <assert.h>
22
#include <node_api.h>
33

4-
napi_value RunCallback(napi_env env, const napi_callback_info info) {
4+
static napi_value RunCallback(napi_env env, const napi_callback_info info) {
55
napi_status status;
66

77
size_t argc = 1;
88
napi_value args[1];
9-
status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
9+
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
1010
assert(status == napi_ok);
1111

1212
napi_value cb = args[0];
@@ -23,13 +23,13 @@ napi_value RunCallback(napi_env env, const napi_callback_info info) {
2323
status = napi_call_function(env, global, cb, 1, argv, &result);
2424
assert(status == napi_ok);
2525

26-
return nullptr;
26+
return NULL;
2727
}
2828

29-
napi_value Init(napi_env env, napi_value exports) {
29+
static napi_value Init(napi_env env, napi_value exports) {
3030
napi_value new_exports;
3131
napi_status status = napi_create_function(
32-
env, "", NAPI_AUTO_LENGTH, RunCallback, nullptr, &new_exports);
32+
env, "", NAPI_AUTO_LENGTH, RunCallback, NULL, &new_exports);
3333
assert(status == napi_ok);
3434
return new_exports;
3535
}

3_callbacks/napi/binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"targets": [
33
{
44
"target_name": "addon",
5-
"sources": [ "addon.cc" ]
5+
"sources": [ "addon.c" ]
66
}
77
]
88
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include <assert.h>
22
#include <node_api.h>
33

4-
napi_value CreateObject(napi_env env, const napi_callback_info info) {
4+
static napi_value CreateObject(napi_env env, const napi_callback_info info) {
55
napi_status status;
66

77
size_t argc = 1;
88
napi_value args[1];
9-
status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
9+
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
1010
assert(status == napi_ok);
1111

1212
napi_value obj;
@@ -19,10 +19,10 @@ napi_value CreateObject(napi_env env, const napi_callback_info info) {
1919
return obj;
2020
}
2121

22-
napi_value Init(napi_env env, napi_value exports) {
22+
static napi_value Init(napi_env env, napi_value exports) {
2323
napi_value new_exports;
2424
napi_status status = napi_create_function(
25-
env, "", NAPI_AUTO_LENGTH, CreateObject, nullptr, &new_exports);
25+
env, "", NAPI_AUTO_LENGTH, CreateObject, NULL, &new_exports);
2626
assert(status == napi_ok);
2727
return new_exports;
2828
}

4_object_factory/napi/binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"targets": [
33
{
44
"target_name": "addon",
5-
"sources": [ "addon.cc" ]
5+
"sources": [ "addon.c" ]
66
}
77
]
88
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <node_api.h>
22
#include <assert.h>
33

4-
napi_value MyFunction(napi_env env, napi_callback_info info) {
4+
static napi_value MyFunction(napi_env env, napi_callback_info info) {
55
napi_status status;
66

77
napi_value str;
@@ -11,21 +11,21 @@ napi_value MyFunction(napi_env env, napi_callback_info info) {
1111
return str;
1212
}
1313

14-
napi_value CreateFunction(napi_env env, napi_callback_info info) {
14+
static napi_value CreateFunction(napi_env env, napi_callback_info info) {
1515
napi_status status;
1616

1717
napi_value fn;
1818
status = napi_create_function(
19-
env, "theFunction", NAPI_AUTO_LENGTH, MyFunction, nullptr, &fn);
19+
env, "theFunction", NAPI_AUTO_LENGTH, MyFunction, NULL, &fn);
2020
assert(status == napi_ok);
2121

2222
return fn;
2323
}
2424

25-
napi_value Init(napi_env env, napi_value exports) {
25+
static napi_value Init(napi_env env, napi_value exports) {
2626
napi_value new_exports;
2727
napi_status status = napi_create_function(
28-
env, "", NAPI_AUTO_LENGTH, CreateFunction, nullptr, &new_exports);
28+
env, "", NAPI_AUTO_LENGTH, CreateFunction, NULL, &new_exports);
2929
assert(status == napi_ok);
3030
return new_exports;
3131
}

5_function_factory/napi/binding.gyp

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

0 commit comments

Comments
 (0)