Skip to content

Commit

Permalink
test: inline helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Sep 14, 2023
1 parent 1165480 commit 3d0a7e0
Show file tree
Hide file tree
Showing 38 changed files with 140 additions and 89 deletions.
14 changes: 7 additions & 7 deletions packages/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ set(EMNAPI_FIND_NODE_ADDON_API ON)
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../emnapi" "${CMAKE_CURRENT_BINARY_DIR}/emnapi")
endif()

add_library(testcommon STATIC "./common.c")
# add_library(testcommon STATIC "./common.c")

set(WASM32_MALLOC "emmalloc")

function(add_test NAME SOURCE_LIST NEED_ENTRY PTHREAD LINKOPTIONS)
set(__SRC_LIST ${SOURCE_LIST})
if(NEED_ENTRY)
list(APPEND __SRC_LIST "./entry_point.c")
endif()
# if(NEED_ENTRY)
# list(APPEND __SRC_LIST "./entry_point.c")
# endif()
if(IS_WASM)
add_executable(${NAME} ${__SRC_LIST})
if(IS_WASI OR IS_WASM32)
Expand All @@ -157,7 +157,7 @@ function(add_test NAME SOURCE_LIST NEED_ENTRY PTHREAD LINKOPTIONS)

set_target_properties(${NAME} PROPERTIES
BUILD_RPATH "$ORIGIN")
target_link_libraries(${NAME} PRIVATE "testcommon")
# target_link_libraries(${NAME} PRIVATE "testcommon")
if(IS_WASM)
if(PTHREAD)
if((IS_WASI AND NOT IS_WASI_THREADS) OR IS_WASM32)
Expand Down Expand Up @@ -302,9 +302,9 @@ target_compile_definitions("reference_obj_only" PRIVATE "NAPI_VERSION=8")
add_test("reference_all_types" "./ref_by_node_api_version/binding.c" OFF OFF "")
target_compile_definitions("reference_all_types" PRIVATE "NAPI_EXPERIMENTAL")

add_test("runjs_pe" "./runjs/binding.c;./runjs/entry_point.c" OFF OFF "")
add_test("runjs_pe" "./runjs/binding.c" OFF OFF "")
target_compile_definitions("runjs_pe" PRIVATE "NAPI_VERSION=8")
add_test("runjs_cnrj" "./runjs/binding.c;./runjs/entry_point.c" OFF OFF "")
add_test("runjs_cnrj" "./runjs/binding.c" OFF OFF "")
target_compile_definitions("runjs_cnrj" PRIVATE "NAPI_EXPERIMENTAL")

if(IS_WASM)
Expand Down
1 change: 1 addition & 0 deletions packages/test/arg/binding.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"

static napi_value Add(napi_env env, napi_callback_info info) {
size_t argc = 2;
Expand Down
1 change: 1 addition & 0 deletions packages/test/array/binding.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <js_native_api.h>
// #include <string.h>
#include "../common.h"
#include "../entry_point.h"

static napi_value TestGetElement(napi_env env, napi_callback_info info) {
size_t argc = 2;
Expand Down
1 change: 1 addition & 0 deletions packages/test/bigint/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#endif
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"

static napi_value IsLossless(napi_env env, napi_callback_info info) {
size_t argc = 2;
Expand Down
1 change: 1 addition & 0 deletions packages/test/callback/binding.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
#if !defined(__wasm__) || (defined(__EMSCRIPTEN__) || defined(__wasi__))
#include <string.h>
#endif
Expand Down
1 change: 1 addition & 0 deletions packages/test/cbinfo/binding.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"

static napi_value Test1(napi_env env, napi_callback_info info) {
size_t argc = 1;
Expand Down
59 changes: 59 additions & 0 deletions packages/test/common-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef JS_NATIVE_API_COMMON_INL_H_
#define JS_NATIVE_API_COMMON_INL_H_

#include <js_native_api.h>
#include "common.h"

#if !defined(__wasm__) || (defined(__EMSCRIPTEN__) || defined(__wasi__))
#include <stdio.h>
#endif

inline void add_returned_status(napi_env env,
const char* key,
napi_value object,
char* expected_message,
napi_status expected_status,
napi_status actual_status) {
char napi_message_string[100] = "";
napi_value prop_value;
#if !defined(__wasm__) || (defined(__EMSCRIPTEN__) || defined(__wasi__))
if (actual_status != expected_status) {
snprintf(napi_message_string,
sizeof(napi_message_string),
"Invalid status [%d]",
actual_status);
}
#endif

NODE_API_CALL_RETURN_VOID(
env,
napi_create_string_utf8(
env,
(actual_status == expected_status ? expected_message
: napi_message_string),
NAPI_AUTO_LENGTH,
&prop_value));
NODE_API_CALL_RETURN_VOID(
env, napi_set_named_property(env, object, key, prop_value));
}

inline void add_last_status(napi_env env,
const char* key,
napi_value return_value) {
napi_value prop_value;
const napi_extended_error_info* p_last_error;
NODE_API_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &p_last_error));

NODE_API_CALL_RETURN_VOID(
env,
napi_create_string_utf8(
env,
(p_last_error->error_message == NULL ? "napi_ok"
: p_last_error->error_message),
NAPI_AUTO_LENGTH,
&prop_value));
NODE_API_CALL_RETURN_VOID(
env, napi_set_named_property(env, return_value, key, prop_value));
}

#endif // JS_NATIVE_API_COMMON_INL_H_
51 changes: 0 additions & 51 deletions packages/test/common.c

This file was deleted.

61 changes: 37 additions & 24 deletions packages/test/common.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef JS_NATIVE_API_COMMON_H_
#define JS_NATIVE_API_COMMON_H_

#include <js_native_api.h>

// Empty value so that macros here are able to return NULL or void
Expand All @@ -10,15 +13,16 @@
bool is_pending; \
const char* err_message = error_info->error_message; \
napi_is_exception_pending((env), &is_pending); \
/* If an exception is already pending, don't rethrow it */ \
if (!is_pending) { \
const char* error_message = err_message != NULL ? \
err_message : \
"empty error message"; \
err_message : \
"empty error message"; \
napi_throw_error((env), NULL, error_message); \
} \
} while (0)

#define NODE_API_ASSERT_BASE(env, assertion, message, ret_val) \
#define NODE_API_ASSERT_BASE(env, assertion, message, ret_val) \
do { \
if (!(assertion)) { \
napi_throw_error( \
Expand All @@ -31,15 +35,15 @@

// Returns NULL on failed assertion.
// This is meant to be used inside napi_callback methods.
#define NODE_API_ASSERT(env, assertion, message) \
#define NODE_API_ASSERT(env, assertion, message) \
NODE_API_ASSERT_BASE(env, assertion, message, NULL)

// Returns empty on failed assertion.
// This is meant to be used inside functions with void return type.
#define NODE_API_ASSERT_RETURN_VOID(env, assertion, message) \
#define NODE_API_ASSERT_RETURN_VOID(env, assertion, message) \
NODE_API_ASSERT_BASE(env, assertion, message, NODE_API_RETVAL_NOTHING)

#define NODE_API_CALL_BASE(env, the_call, ret_val) \
#define NODE_API_CALL_BASE(env, the_call, ret_val) \
do { \
if ((the_call) != napi_ok) { \
GET_AND_THROW_LAST_ERROR((env)); \
Expand All @@ -48,35 +52,44 @@
} while (0)

// Returns NULL if the_call doesn't return napi_ok.
#define NODE_API_CALL(env, the_call) \
#define NODE_API_CALL(env, the_call) \
NODE_API_CALL_BASE(env, the_call, NULL)

// Returns empty if the_call doesn't return napi_ok.
#define NODE_API_CALL_RETURN_VOID(env, the_call) \
#define NODE_API_CALL_RETURN_VOID(env, the_call) \
NODE_API_CALL_BASE(env, the_call, NODE_API_RETVAL_NOTHING)

#define NODE_API_CHECK_STATUS(the_call) \
do { \
napi_status status = (the_call); \
if (status != napi_ok) { \
return status; \
} \
#define NODE_API_CHECK_STATUS(the_call) \
do { \
napi_status status = (the_call); \
if (status != napi_ok) { \
return status; \
} \
} while (0)

#define NODE_API_ASSERT_STATUS(env, assertion, message) \
#define NODE_API_ASSERT_STATUS(env, assertion, message) \
NODE_API_ASSERT_BASE(env, assertion, message, napi_generic_failure)

#define DECLARE_NODE_API_PROPERTY(name, func) \
#define DECLARE_NODE_API_PROPERTY(name, func) \
{ (name), NULL, (func), NULL, NULL, NULL, napi_default, NULL }

#define DECLARE_NODE_API_GETTER(name, func) \
#define DECLARE_NODE_API_GETTER(name, func) \
{ (name), NULL, NULL, (func), NULL, NULL, napi_default, NULL }

void add_returned_status(napi_env env,
const char* key,
napi_value object,
char* expected_message,
napi_status expected_status,
napi_status actual_status);
#define DECLARE_NODE_API_PROPERTY_VALUE(name, value) \
{ (name), NULL, NULL, NULL, NULL, (value), napi_default, NULL }

static inline void add_returned_status(napi_env env,
const char* key,
napi_value object,
char* expected_message,
napi_status expected_status,
napi_status actual_status);

static inline void add_last_status(napi_env env,
const char* key,
napi_value return_value);

#include "common-inl.h"

void add_last_status(napi_env env, const char* key, napi_value return_value);
#endif // JS_NATIVE_API_COMMON_H_
1 change: 1 addition & 0 deletions packages/test/constructor/binding.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
#include "test_null.h"

static double value_ = 1;
Expand Down
1 change: 1 addition & 0 deletions packages/test/conversion/test_conversions.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
#include "test_null.h"

static napi_value AsBool(napi_env env, napi_callback_info info) {
Expand Down
1 change: 1 addition & 0 deletions packages/test/dataview/binding.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <js_native_api.h>
// #include <string.h>
#include "../common.h"
#include "../entry_point.h"

static napi_value CreateDataView(napi_env env, napi_callback_info info) {
size_t argc = 3;
Expand Down
1 change: 1 addition & 0 deletions packages/test/date/binding.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"

static napi_value createDate(napi_env env, napi_callback_info info) {
size_t argc = 1;
Expand Down
1 change: 1 addition & 0 deletions packages/test/emnapitest/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "js_native_api.h"
#include "emnapi.h"
#include "../common.h"
#include "../entry_point.h"

void* malloc(size_t size);
void free(void* p);
Expand Down
5 changes: 5 additions & 0 deletions packages/test/entry_point.c → packages/test/entry_point.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#ifndef JS_NATIVE_API_ENTRY_POINT_H_
#define JS_NATIVE_API_ENTRY_POINT_H_

#include <node_api.h>

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports);
EXTERN_C_END

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

#endif // JS_NATIVE_API_ENTRY_POINT_H_
1 change: 1 addition & 0 deletions packages/test/error/binding.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define NAPI_VERSION 9
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"

static napi_value checkError(napi_env env, napi_callback_info info) {
size_t argc = 1;
Expand Down
1 change: 1 addition & 0 deletions packages/test/exception/binding.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"

static bool exceptionWasPending = false;
static int num = 0x23432;
Expand Down
1 change: 1 addition & 0 deletions packages/test/filename/binding.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define NAPI_EXPERIMENTAL
#include <node_api.h>
#include "../common.h"
#include "../entry_point.h"

static napi_value GetFilename(napi_env env, napi_callback_info info) {
const char* filename;
Expand Down
1 change: 1 addition & 0 deletions packages/test/fnfac/binding.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"

static napi_value MyFunction(napi_env env, napi_callback_info info) {
napi_value str;
Expand Down
1 change: 1 addition & 0 deletions packages/test/fnwrap/binding.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <js_native_api.h>
#include "myobject.h"
#include "../common.h"
#include "../entry_point.h"

napi_value CreateObject(napi_env env, napi_callback_info info) {
size_t argc = 1;
Expand Down
1 change: 1 addition & 0 deletions packages/test/function/binding.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"

static napi_value TestCreateFunctionParameters(napi_env env,
napi_callback_info info) {
Expand Down
Loading

0 comments on commit 3d0a7e0

Please sign in to comment.