Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: inline helper functions #80

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 55 additions & 61 deletions packages/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,11 @@ 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")

set(WASM32_MALLOC "emmalloc")

function(add_test NAME SOURCE_LIST NEED_ENTRY PTHREAD LINKOPTIONS)
function(add_test NAME SOURCE_LIST PTHREAD)
set(__SRC_LIST ${SOURCE_LIST})
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 +153,6 @@ function(add_test NAME SOURCE_LIST NEED_ENTRY PTHREAD LINKOPTIONS)

set_target_properties(${NAME} PROPERTIES
BUILD_RPATH "$ORIGIN")
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 @@ -186,7 +181,6 @@ function(add_test NAME SOURCE_LIST NEED_ENTRY PTHREAD LINKOPTIONS)
if(IS_EMSCRIPTEN)
target_link_options(${NAME} PRIVATE "-sEXPORT_NAME=emnapitest_${NAME}")
endif()
target_link_options(${NAME} PRIVATE ${LINKOPTIONS})
endfunction()

function(add_naa_test NAME SOURCE_LIST DEFINES ENABLE_EXCEPTION)
Expand Down Expand Up @@ -246,78 +240,78 @@ function(add_naa_test NAME SOURCE_LIST DEFINES ENABLE_EXCEPTION)
if(IS_EMSCRIPTEN)
target_link_options(${NAME} PRIVATE "-sEXPORT_NAME=emnapitest_${NAME}")
endif()
target_link_options(${NAME} PRIVATE ${LINKOPTIONS})
endfunction()

add_test("env" "./env/binding.c" OFF OFF "")
add_test("hello" "./hello/binding.c" OFF OFF "")
add_test("env" "./env/binding.c" OFF)
add_test("hello" "./hello/binding.c" OFF)

add_test("async" "./async/binding.c" OFF ON "")
add_test("tsfn2" "./tsfn2/binding.c" OFF ON "")
add_test("async" "./async/binding.c" ON)
add_test("tsfn2" "./tsfn2/binding.c" ON)

if((NOT IS_WASM) OR IS_EMSCRIPTEN OR IS_WASI_THREADS)
add_test("string_mt" "./string/binding.c;./string/test_null.c" ON ON "")
add_test("pool" "./pool/binding.c" OFF ON "")
add_test("tsfn" "./tsfn/binding.c" OFF ON "")
add_test("async_cleanup_hook" "./async_cleanup_hook/binding.c" OFF ON "")
add_test("uv_threadpool_size" "./uv_threadpool_size/binding.c" OFF ON "")
add_test("string_mt" "./string/binding.c;./string/test_null.c" ON)
add_test("pool" "./pool/binding.c" ON)
add_test("tsfn" "./tsfn/binding.c" ON)
add_test("async_cleanup_hook" "./async_cleanup_hook/binding.c" ON)
add_test("uv_threadpool_size" "./uv_threadpool_size/binding.c" ON)
endif()

add_test("arg" "./arg/binding.c" ON OFF "")
add_test("cbinfo" "./cbinfo/binding.c" ON OFF "")
add_test("callback" "./callback/binding.c" ON OFF "")
add_test("objfac" "./objfac/binding.c" ON OFF "")
add_test("fnfac" "./fnfac/binding.c" ON OFF "")
add_test("general" "./general/binding.c" ON OFF "")
add_test("filename" "./filename/binding.c" ON OFF "")
add_test("string" "./string/binding.c;./string/test_null.c" ON OFF "")
add_test("property" "./property/binding.c" ON OFF "")
add_test("promise" "./promise/binding.c" ON OFF "")
add_test("object" "./object/test_null.c;./object/test_object.c" ON OFF "")
add_test("objwrap" "./objwrap/myobject.cc" ON OFF "")
add_test("bigint" "./bigint/binding.c" ON OFF "")
add_test("fnwrap" "./fnwrap/myobject.cc;./fnwrap/binding.cc" ON OFF "")
add_test("passwrap" "./passwrap/myobject.cc;./passwrap/binding.cc" ON OFF "")
add_test("array" "./array/binding.c" ON OFF "")
add_test("constructor" "./constructor/binding.c;./constructor/test_null.c" ON OFF "")
add_test("conversion" "./conversion/test_conversions.c;./conversion/test_null.c" ON OFF "")
add_test("dataview" "./dataview/binding.c" ON OFF "")
add_test("date" "./date/binding.c" ON OFF "")
add_test("error" "./error/binding.c" ON OFF "")
add_test("exception" "./exception/binding.c" ON OFF "")
add_test("ref" "./ref/binding.c" ON OFF "")
add_test("ref_double_free" "./ref_double_free/binding.c" ON OFF "")
add_test("function" "./function/binding.c" ON OFF "")
add_test("scope" "./scope/binding.c" ON OFF "")
add_test("newtarget" "./newtarget/binding.c" ON OFF "")
add_test("number" "./number/binding.c;./number/test_null.c" ON OFF "")
add_test("symbol" "./symbol/binding.c" ON OFF "")
add_test("typedarray" "./typedarray/binding.c" ON OFF "")
add_test("buffer" "./buffer/binding.c" OFF OFF "")
add_test("fatal_exception" "./fatal_exception/binding.c" OFF OFF "")
add_test("cleanup_hook" "./cleanup_hook/binding.c" OFF OFF "")

add_test("reference_obj_only" "./ref_by_node_api_version/binding.c" OFF OFF "")
add_test("arg" "./arg/binding.c" OFF)
add_test("cbinfo" "./cbinfo/binding.c" OFF)
add_test("callback" "./callback/binding.c" OFF)
add_test("objfac" "./objfac/binding.c" OFF)
add_test("fnfac" "./fnfac/binding.c" OFF)
add_test("general" "./general/binding.c" OFF)
add_test("filename" "./filename/binding.c" OFF)
add_test("string" "./string/binding.c;./string/test_null.c" OFF)
add_test("property" "./property/binding.c" OFF)
add_test("promise" "./promise/binding.c" OFF)
add_test("object" "./object/test_null.c;./object/test_object.c" OFF)
add_test("objwrap" "./objwrap/myobject.cc" OFF)
add_test("bigint" "./bigint/binding.c" OFF)
add_test("fnwrap" "./fnwrap/myobject.cc;./fnwrap/binding.cc" OFF)
add_test("passwrap" "./passwrap/myobject.cc;./passwrap/binding.cc" OFF)
add_test("array" "./array/binding.c" OFF)
add_test("constructor" "./constructor/binding.c;./constructor/test_null.c" OFF)
add_test("conversion" "./conversion/test_conversions.c;./conversion/test_null.c" OFF)
add_test("dataview" "./dataview/binding.c" OFF)
add_test("date" "./date/binding.c" OFF)
add_test("error" "./error/binding.c" OFF)
add_test("exception" "./exception/binding.c" OFF)
add_test("ref" "./ref/binding.c" OFF)
add_test("ref_double_free" "./ref_double_free/binding.c" OFF)
add_test("function" "./function/binding.c" OFF)
add_test("scope" "./scope/binding.c" OFF)
add_test("newtarget" "./newtarget/binding.c" OFF)
add_test("number" "./number/binding.c;./number/test_null.c" OFF)
add_test("symbol" "./symbol/binding.c" OFF)
add_test("typedarray" "./typedarray/binding.c" OFF)
add_test("buffer" "./buffer/binding.c" OFF)
add_test("fatal_exception" "./fatal_exception/binding.c" OFF)
add_test("cleanup_hook" "./cleanup_hook/binding.c" OFF)

add_test("reference_obj_only" "./ref_by_node_api_version/binding.c" OFF)
target_compile_definitions("reference_obj_only" PRIVATE "NAPI_VERSION=8")
add_test("reference_all_types" "./ref_by_node_api_version/binding.c" OFF OFF "")
add_test("reference_all_types" "./ref_by_node_api_version/binding.c" 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)
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)
target_compile_definitions("runjs_cnrj" PRIVATE "NAPI_EXPERIMENTAL")

if(IS_WASM)
if(IS_EMSCRIPTEN)
add_test("emnapitest" "./emnapitest/binding.c" ON OFF "-sEXPORTED_RUNTIME_METHODS=['emnapiSyncMemory']")
add_test("emnapitest" "./emnapitest/binding.c" OFF)
target_link_options("emnapitest" PRIVATE "-sEXPORTED_RUNTIME_METHODS=['emnapiSyncMemory']")
else()
add_test("emnapitest" "./emnapitest/binding.c" ON OFF "")
add_test("emnapitest" "./emnapitest/binding.c" OFF)
endif()
endif()

add_test("version" "./version/binding.c" OFF OFF "")
add_test("make_callback" "./make_callback/binding.c" OFF OFF "")
add_test("async_context" "./async_context/binding.c" OFF OFF "")
add_test("version" "./version/binding.c" OFF)
add_test("make_callback" "./make_callback/binding.c" OFF)
add_test("async_context" "./async_context/binding.c" OFF)

if(IS_EMSCRIPTEN OR IS_WASI_THREADS)
file(GLOB_RECURSE naa_binding_SRC
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.

Loading
Loading