Skip to content

Commit

Permalink
test: convert most addons-napi tests to C
Browse files Browse the repository at this point in the history
Some tests need to stay C++ because they define classes.

Fixes nodejs/abi-stable-node#183
Closes nodejs/abi-stable-node#190
  • Loading branch information
Gabriel Schulhof authored and jasongin committed Mar 23, 2017
1 parent b129624 commit 66944b8
Show file tree
Hide file tree
Showing 37 changed files with 94 additions and 90 deletions.
26 changes: 16 additions & 10 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define SRC_NODE_API_H_

#include <stddef.h>
#include <stdbool.h>
#include "node_api_types.h"

#ifdef _WIN32
Expand All @@ -37,23 +38,20 @@
#endif


namespace node {

NAPI_EXTERN typedef void (*napi_addon_register_func)(napi_env env,
napi_value exports,
napi_value module,
void* priv);
} // namespace node

struct napi_module {
typedef struct {
int nm_version;
unsigned int nm_flags;
const char* nm_filename;
node::napi_addon_register_func nm_register_func;
napi_addon_register_func nm_register_func;
const char* nm_modname;
void* nm_priv;
void* reserved[4];
};
} napi_module;

#define NAPI_MODULE_VERSION 1

Expand All @@ -70,8 +68,16 @@ struct napi_module {
static void fn(void)
#endif

#ifdef __cplusplus
#define EXTERN_C_START extern "C" {
#define EXTERN_C_END }
#else /* ndef __cplusplus */
#define EXTERN_C_START
#define EXTERN_C_END
#endif /* def __cplusplus */

#define NAPI_MODULE_X(modname, regfunc, priv, flags) \
extern "C" { \
EXTERN_C_START \
static napi_module _module = \
{ \
NAPI_MODULE_VERSION, \
Expand All @@ -85,12 +91,12 @@ struct napi_module {
NAPI_C_CTOR(_register_ ## modname) { \
napi_module_register(&_module); \
} \
}
EXTERN_C_END

#define NAPI_MODULE(modname, regfunc) \
NAPI_MODULE_X(modname, regfunc, NULL, 0)

extern "C" {
EXTERN_C_START

NAPI_EXTERN void napi_module_register(napi_module* mod);

Expand Down Expand Up @@ -478,6 +484,6 @@ NAPI_EXTERN napi_status napi_get_typedarray_info(napi_env env,
void** data,
napi_value* arraybuffer,
size_t* byte_offset);
} // extern "C"
EXTERN_C_END

#endif // SRC_NODE_API_H__
24 changes: 12 additions & 12 deletions src/node_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct napi_callback_info__ *napi_callback_info;
typedef void (*napi_callback)(napi_env, napi_callback_info);
typedef void (*napi_finalize)(void* finalize_data, void* finalize_hint);

enum napi_property_attributes {
typedef enum {
napi_default = 0,
napi_read_only = 1 << 0,
napi_dont_enum = 1 << 1,
Expand All @@ -29,9 +29,9 @@ enum napi_property_attributes {
// Used with napi_define_class to distinguish static properties
// from instance properties. Ignored by napi_define_properties.
napi_static_property = 1 << 10,
};
} napi_property_attributes;

struct napi_property_descriptor {
typedef struct {
const char* utf8name;

napi_callback method;
Expand All @@ -41,11 +41,11 @@ struct napi_property_descriptor {

napi_property_attributes attributes;
void* data;
};
} napi_property_descriptor;

#define DEFAULT_ATTR 0, 0, 0, napi_default, 0

enum napi_valuetype {
typedef enum {
// ES6 types (corresponds to typeof)
napi_undefined,
napi_null,
Expand All @@ -56,9 +56,9 @@ enum napi_valuetype {
napi_object,
napi_function,
napi_external,
};
} napi_valuetype;

enum napi_typedarray_type {
typedef enum {
napi_int8,
napi_uint8,
napi_uint8_clamped,
Expand All @@ -68,9 +68,9 @@ enum napi_typedarray_type {
napi_uint32,
napi_float32,
napi_float64,
};
} napi_typedarray_type;

enum napi_status {
typedef enum {
napi_ok,
napi_invalid_arg,
napi_object_expected,
Expand All @@ -81,13 +81,13 @@ enum napi_status {
napi_generic_failure,
napi_pending_exception,
napi_status_last
};
} napi_status;

struct napi_extended_error_info {
typedef struct {
const char* error_message;
void* engine_reserved;
uint32_t engine_error_code;
napi_status error_code;
};
} napi_extended_error_info;

#endif // SRC_NODE_API_TYPES_H_
File renamed without changes.
2 changes: 1 addition & 1 deletion test/addons-napi/1_hello_world/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"targets": [
{
"target_name": "binding",
"sources": [ "binding.cc" ]
"sources": [ "binding.c" ]
}
]
}
2 changes: 1 addition & 1 deletion test/addons-napi/2_function_arguments/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"targets": [
{
"target_name": "binding",
"sources": [ "binding.cc" ]
"sources": [ "binding.c" ]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void RunCallback(napi_env env, const napi_callback_info info) {
status = napi_get_global(env, &global);
if (status != napi_ok) return;

status = napi_call_function(env, global, cb, 1, argv, nullptr);
status = napi_call_function(env, global, cb, 1, argv, NULL);
if (status != napi_ok) return;
}

Expand All @@ -31,7 +31,7 @@ void RunCallbackWithRecv(napi_env env, const napi_callback_info info) {
napi_value cb = args[0];
napi_value recv = args[1];

status = napi_call_function(env, recv, cb, 0, nullptr, nullptr);
status = napi_call_function(env, recv, cb, 0, NULL, NULL);
if (status != napi_ok) return;
}

Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/3_callbacks/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"targets": [
{
"target_name": "binding",
"sources": [ "binding.cc" ]
"sources": [ "binding.c" ]
}
]
}
File renamed without changes.
2 changes: 1 addition & 1 deletion test/addons-napi/4_object_factory/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"targets": [
{
"target_name": "binding",
"sources": [ "binding.cc" ]
"sources": [ "binding.c" ]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <node_api.h>
#include <node_api.h>

void MyFunction(napi_env env, napi_callback_info info) {
napi_status status;
Expand All @@ -15,7 +15,7 @@ void CreateFunction(napi_env env, napi_callback_info info) {
napi_status status;

napi_value fn;
status = napi_create_function(env, "theFunction", MyFunction, nullptr, &fn);
status = napi_create_function(env, "theFunction", MyFunction, NULL, &fn);
if (status != napi_ok) return;

status = napi_set_return_value(env, info, fn);
Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/5_function_factory/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"targets": [
{
"target_name": "binding",
"sources": [ "binding.cc" ]
"sources": [ "binding.c" ]
}
]
}
2 changes: 1 addition & 1 deletion test/addons-napi/test_array/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"targets": [
{
"target_name": "test_array",
"sources": [ "test_array.cc" ]
"sources": [ "test_array.c" ]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void Test(napi_env env, napi_callback_info info) {
status = napi_get_array_length(env, array, &size);
if (status != napi_ok) return;

if (index >= static_cast<int>(size)) {
if (index >= (int)(size)) {
napi_value str;
status = napi_create_string_utf8(env, "Index out of bound!", -1, &str);
if (status != napi_ok) return;
Expand Down Expand Up @@ -101,11 +101,11 @@ void New(napi_env env, napi_callback_info info) {
status = napi_create_array(env, &ret);
if (status != napi_ok) return;

uint32_t length;
uint32_t i, length;
status = napi_get_array_length(env, args[0], &length);
if (status != napi_ok) return;

for (uint32_t i = 0; i < length; i++) {
for (i = 0; i < length; i++) {
napi_value e;
status = napi_get_element(env, args[0], i, &e);
if (status != napi_ok) return;
Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/test_buffer/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"targets": [
{
"target_name": "test_buffer",
"sources": [ "test_buffer.cc" ]
"sources": [ "test_buffer.c" ]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include <string.h>
#include <string>
#include <stdlib.h>
#include <string.h>
#include <node_api.h>

#define JS_ASSERT(env, assertion, message) \
if (!(assertion)) { \
napi_throw_error( \
(env), \
(std::string("assertion (" #assertion ") failed: ") + message) \
.c_str()); \
return; \
#define JS_ASSERT(env, assertion, message) \
if (!(assertion)) { \
napi_throw_error( \
(env), \
"assertion (" #assertion ") failed: " message); \
return; \
}

#define NAPI_CALL(env, theCall) \
Expand All @@ -23,13 +22,15 @@ static const char theText[] =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

static int deleterCallCount = 0;
static void deleteTheText(void *data, void* /*finalize_hint*/) {
delete reinterpret_cast<char *>(data);
static void deleteTheText(void *data, void* finalize_hint) {
(void)finalize_hint;
free(data);
deleterCallCount++;
}

static void noopDeleter(void *data, void* /*finalize_hint*/) {
deleterCallCount++;
static void noopDeleter(void *data, void* finalize_hint) {
(void)finalize_hint;
deleterCallCount++;
}

void newBuffer(napi_env env, napi_callback_info info) {
Expand All @@ -41,7 +42,7 @@ void newBuffer(napi_env env, napi_callback_info info) {
napi_create_buffer(
env,
sizeof(theText),
reinterpret_cast<void**>(&theCopy),
(void **)(&theCopy),
&theBuffer));
JS_ASSERT(env, theCopy, "Failed to copy static text for newBuffer");
memcpy(theCopy, theText, kBufferSize);
Expand All @@ -58,7 +59,7 @@ void newExternalBuffer(napi_env env, napi_callback_info info) {
sizeof(theText),
theCopy,
deleteTheText,
nullptr, // finalize_hint
NULL, // finalize_hint
&theBuffer));
NAPI_CALL(env, napi_set_return_value(env, info, theBuffer));
}
Expand Down Expand Up @@ -107,7 +108,7 @@ void bufferInfo(napi_env env, napi_callback_info info) {
napi_get_buffer_info(
env,
theBuffer,
reinterpret_cast<void**>(&bufferData),
(void **)(&bufferData),
&bufferLength));
NAPI_CALL(env, napi_create_boolean(env,
!strcmp(bufferData, theText) && bufferLength == sizeof(theText),
Expand All @@ -121,9 +122,9 @@ void staticBuffer(napi_env env, napi_callback_info info) {
env,
napi_create_external_buffer(env,
sizeof(theText),
const_cast<char *>(theText),
(const char *)(theText),
noopDeleter,
nullptr, // finalize_hint
NULL, // finalize_hint
&theBuffer));
NAPI_CALL(env, napi_set_return_value(env, info, theBuffer));
}
Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/test_constructor/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"targets": [
{
"target_name": "test_constructor",
"sources": [ "test_constructor.cc" ]
"sources": [ "test_constructor.c" ]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
{ "accessorValue", 0, GetValue, SetValue, 0, napi_default, 0},
{ "readwriteValue", 0, 0, 0, number, napi_default, 0 },
{ "readonlyValue", 0, 0, 0, number, napi_read_only, 0},
{ "hiddenValue", 0, 0, 0, number, static_cast<napi_property_attributes>(
napi_read_only | napi_dont_enum), 0},
{ "hiddenValue", 0, 0, 0, number, napi_read_only | napi_dont_enum, 0},
};

napi_value cons;
status = napi_define_class(env, "MyObject", New,
nullptr, sizeof(properties)/sizeof(*properties), properties, &cons);
NULL, sizeof(properties)/sizeof(*properties), properties, &cons);
if (status != napi_ok) return;

status = napi_set_named_property(env, module, "exports", cons);
Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/test_error/test_error.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <node_api.h>
#include <node_api.h>

void checkError(napi_env e, napi_callback_info info) {
napi_status status;
Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/test_exception/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"targets": [
{
"target_name": "test_exception",
"sources": [ "test_exception.cc" ]
"sources": [ "test_exception.c" ]
}
]
}
Loading

0 comments on commit 66944b8

Please sign in to comment.