diff --git a/packages/test/CMakeLists.txt b/packages/test/CMakeLists.txt index 39a3447e..bf064303 100644 --- a/packages/test/CMakeLists.txt +++ b/packages/test/CMakeLists.txt @@ -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) @@ -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) @@ -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) diff --git a/packages/test/arg/binding.c b/packages/test/arg/binding.c index 2f7a196f..123d9116 100644 --- a/packages/test/arg/binding.c +++ b/packages/test/arg/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" static napi_value Add(napi_env env, napi_callback_info info) { size_t argc = 2; diff --git a/packages/test/array/binding.c b/packages/test/array/binding.c index 62a19221..284e15c0 100644 --- a/packages/test/array/binding.c +++ b/packages/test/array/binding.c @@ -1,6 +1,7 @@ #include // #include #include "../common.h" +#include "../entry_point.h" static napi_value TestGetElement(napi_env env, napi_callback_info info) { size_t argc = 2; diff --git a/packages/test/bigint/binding.c b/packages/test/bigint/binding.c index d83157c4..a023700d 100644 --- a/packages/test/bigint/binding.c +++ b/packages/test/bigint/binding.c @@ -5,6 +5,7 @@ #endif #include #include "../common.h" +#include "../entry_point.h" static napi_value IsLossless(napi_env env, napi_callback_info info) { size_t argc = 2; diff --git a/packages/test/callback/binding.c b/packages/test/callback/binding.c index 5be375c9..8a4b2d5e 100644 --- a/packages/test/callback/binding.c +++ b/packages/test/callback/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" #if !defined(__wasm__) || (defined(__EMSCRIPTEN__) || defined(__wasi__)) #include #endif diff --git a/packages/test/cbinfo/binding.c b/packages/test/cbinfo/binding.c index 85c874b3..457a51e8 100644 --- a/packages/test/cbinfo/binding.c +++ b/packages/test/cbinfo/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" static napi_value Test1(napi_env env, napi_callback_info info) { size_t argc = 1; diff --git a/packages/test/common-inl.h b/packages/test/common-inl.h new file mode 100644 index 00000000..7ea3a755 --- /dev/null +++ b/packages/test/common-inl.h @@ -0,0 +1,59 @@ +#ifndef JS_NATIVE_API_COMMON_INL_H_ +#define JS_NATIVE_API_COMMON_INL_H_ + +#include +#include "common.h" + +#if !defined(__wasm__) || (defined(__EMSCRIPTEN__) || defined(__wasi__)) +#include +#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_ diff --git a/packages/test/common.c b/packages/test/common.c deleted file mode 100644 index fa8d8fcd..00000000 --- a/packages/test/common.c +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include "common.h" - -#if !defined(__wasm__) || (defined(__EMSCRIPTEN__) || defined(__wasi__)) -#include -#endif - -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)); -} - -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)); -} diff --git a/packages/test/common.h b/packages/test/common.h index 9f8e62cb..d15d53b0 100644 --- a/packages/test/common.h +++ b/packages/test/common.h @@ -1,3 +1,6 @@ +#ifndef JS_NATIVE_API_COMMON_H_ +#define JS_NATIVE_API_COMMON_H_ + #include // Empty value so that macros here are able to return NULL or void @@ -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( \ @@ -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)); \ @@ -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_ diff --git a/packages/test/constructor/binding.c b/packages/test/constructor/binding.c index 92b03985..c706170b 100644 --- a/packages/test/constructor/binding.c +++ b/packages/test/constructor/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" #include "test_null.h" static double value_ = 1; diff --git a/packages/test/conversion/test_conversions.c b/packages/test/conversion/test_conversions.c index 500962d5..89b93ef0 100644 --- a/packages/test/conversion/test_conversions.c +++ b/packages/test/conversion/test_conversions.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" #include "test_null.h" static napi_value AsBool(napi_env env, napi_callback_info info) { diff --git a/packages/test/dataview/binding.c b/packages/test/dataview/binding.c index c7c62a11..e076b580 100644 --- a/packages/test/dataview/binding.c +++ b/packages/test/dataview/binding.c @@ -1,6 +1,7 @@ #include // #include #include "../common.h" +#include "../entry_point.h" static napi_value CreateDataView(napi_env env, napi_callback_info info) { size_t argc = 3; diff --git a/packages/test/date/binding.c b/packages/test/date/binding.c index 5d9e9ff3..abf02416 100644 --- a/packages/test/date/binding.c +++ b/packages/test/date/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" static napi_value createDate(napi_env env, napi_callback_info info) { size_t argc = 1; diff --git a/packages/test/emnapitest/binding.c b/packages/test/emnapitest/binding.c index aa2297f7..3ca482ca 100644 --- a/packages/test/emnapitest/binding.c +++ b/packages/test/emnapitest/binding.c @@ -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); diff --git a/packages/test/entry_point.c b/packages/test/entry_point.h similarity index 55% rename from packages/test/entry_point.c rename to packages/test/entry_point.h index 6b7b50a3..5ba5aaff 100644 --- a/packages/test/entry_point.c +++ b/packages/test/entry_point.h @@ -1,3 +1,6 @@ +#ifndef JS_NATIVE_API_ENTRY_POINT_H_ +#define JS_NATIVE_API_ENTRY_POINT_H_ + #include EXTERN_C_START @@ -5,3 +8,5 @@ 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_ diff --git a/packages/test/error/binding.c b/packages/test/error/binding.c index 0cef9848..9babe78e 100644 --- a/packages/test/error/binding.c +++ b/packages/test/error/binding.c @@ -1,6 +1,7 @@ #define NAPI_VERSION 9 #include #include "../common.h" +#include "../entry_point.h" static napi_value checkError(napi_env env, napi_callback_info info) { size_t argc = 1; diff --git a/packages/test/exception/binding.c b/packages/test/exception/binding.c index 053f0484..84b99196 100644 --- a/packages/test/exception/binding.c +++ b/packages/test/exception/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" static bool exceptionWasPending = false; static int num = 0x23432; diff --git a/packages/test/filename/binding.c b/packages/test/filename/binding.c index bf239eab..ce937e00 100644 --- a/packages/test/filename/binding.c +++ b/packages/test/filename/binding.c @@ -1,6 +1,7 @@ #define NAPI_EXPERIMENTAL #include #include "../common.h" +#include "../entry_point.h" static napi_value GetFilename(napi_env env, napi_callback_info info) { const char* filename; diff --git a/packages/test/fnfac/binding.c b/packages/test/fnfac/binding.c index 679f09fe..8c2bdac5 100644 --- a/packages/test/fnfac/binding.c +++ b/packages/test/fnfac/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" static napi_value MyFunction(napi_env env, napi_callback_info info) { napi_value str; diff --git a/packages/test/fnwrap/binding.cc b/packages/test/fnwrap/binding.cc index 80ca6d0c..d46e8225 100644 --- a/packages/test/fnwrap/binding.cc +++ b/packages/test/fnwrap/binding.cc @@ -1,6 +1,7 @@ #include #include "myobject.h" #include "../common.h" +#include "../entry_point.h" napi_value CreateObject(napi_env env, napi_callback_info info) { size_t argc = 1; diff --git a/packages/test/function/binding.c b/packages/test/function/binding.c index ddaafef2..d2f354e9 100644 --- a/packages/test/function/binding.c +++ b/packages/test/function/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" static napi_value TestCreateFunctionParameters(napi_env env, napi_callback_info info) { diff --git a/packages/test/general/binding.c b/packages/test/general/binding.c index 0dc64dc2..72177b35 100644 --- a/packages/test/general/binding.c +++ b/packages/test/general/binding.c @@ -5,6 +5,7 @@ #include #include #include "../common.h" +#include "../entry_point.h" static napi_value testStrictEquals(napi_env env, napi_callback_info info) { size_t argc = 2; diff --git a/packages/test/newtarget/binding.c b/packages/test/newtarget/binding.c index d3fe5b0d..4e2be974 100644 --- a/packages/test/newtarget/binding.c +++ b/packages/test/newtarget/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" static napi_value BaseClass(napi_env env, napi_callback_info info) { napi_value newTargetArg; diff --git a/packages/test/number/binding.c b/packages/test/number/binding.c index c8d4733f..b8169451 100644 --- a/packages/test/number/binding.c +++ b/packages/test/number/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" #include "test_null.h" static napi_value Test(napi_env env, napi_callback_info info) { diff --git a/packages/test/object/test_object.c b/packages/test/object/test_object.c index 49785362..7c3e0add 100644 --- a/packages/test/object/test_object.c +++ b/packages/test/object/test_object.c @@ -1,6 +1,7 @@ #define NAPI_EXPERIMENTAL #include #include "../common.h" +#include "../entry_point.h" // #include #include "test_null.h" diff --git a/packages/test/objfac/binding.c b/packages/test/objfac/binding.c index 5b065177..8fd6090f 100644 --- a/packages/test/objfac/binding.c +++ b/packages/test/objfac/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" static napi_value CreateObject(napi_env env, napi_callback_info info) { size_t argc = 1; diff --git a/packages/test/objwrap/myobject.cc b/packages/test/objwrap/myobject.cc index 969b6792..5f279353 100644 --- a/packages/test/objwrap/myobject.cc +++ b/packages/test/objwrap/myobject.cc @@ -1,6 +1,7 @@ // #include #include "myobject.h" #include "../common.h" +#include "../entry_point.h" #if !(!defined(__wasm__) || (defined(__EMSCRIPTEN__) || defined(__wasi__))) #include diff --git a/packages/test/passwrap/binding.cc b/packages/test/passwrap/binding.cc index 6809c8a2..da7b0b19 100644 --- a/packages/test/passwrap/binding.cc +++ b/packages/test/passwrap/binding.cc @@ -1,6 +1,7 @@ #include #include "myobject.h" #include "../common.h" +#include "../entry_point.h" extern size_t finalize_count; diff --git a/packages/test/promise/binding.c b/packages/test/promise/binding.c index 488ecea7..eef4813a 100644 --- a/packages/test/promise/binding.c +++ b/packages/test/promise/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" napi_deferred deferred = NULL; diff --git a/packages/test/property/binding.c b/packages/test/property/binding.c index ef02ac5d..5645a70f 100644 --- a/packages/test/property/binding.c +++ b/packages/test/property/binding.c @@ -1,6 +1,7 @@ #define NAPI_VERSION 9 #include #include "../common.h" +#include "../entry_point.h" static double value_ = 1; diff --git a/packages/test/ref/binding.c b/packages/test/ref/binding.c index b16d04aa..60567878 100644 --- a/packages/test/ref/binding.c +++ b/packages/test/ref/binding.c @@ -1,6 +1,7 @@ #define NAPI_VERSION 9 #include #include "../common.h" +#include "../entry_point.h" #define assert(x) do { if (!(x)) { __builtin_trap(); } } while (0) diff --git a/packages/test/ref_double_free/binding.c b/packages/test/ref_double_free/binding.c index bb594c91..fff6dca9 100644 --- a/packages/test/ref_double_free/binding.c +++ b/packages/test/ref_double_free/binding.c @@ -1,6 +1,7 @@ // #include #include #include "../common.h" +#include "../entry_point.h" void* malloc(size_t size); void free(void* p); diff --git a/packages/test/runjs/binding.c b/packages/test/runjs/binding.c index 90561873..948ce821 100644 --- a/packages/test/runjs/binding.c +++ b/packages/test/runjs/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" #if !defined(__wasm__) || (defined(__EMSCRIPTEN__) || defined(__wasi__)) #include "stdlib.h" #else diff --git a/packages/test/runjs/entry_point.c b/packages/test/runjs/entry_point.c deleted file mode 100644 index 6b7b50a3..00000000 --- a/packages/test/runjs/entry_point.c +++ /dev/null @@ -1,7 +0,0 @@ -#include - -EXTERN_C_START -napi_value Init(napi_env env, napi_value exports); -EXTERN_C_END - -NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) diff --git a/packages/test/scope/binding.c b/packages/test/scope/binding.c index e9127067..60a9e82b 100644 --- a/packages/test/scope/binding.c +++ b/packages/test/scope/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" // #include // these tests validate the handle scope functions in the normal diff --git a/packages/test/string/binding.c b/packages/test/string/binding.c index 0f36835a..67781a5a 100644 --- a/packages/test/string/binding.c +++ b/packages/test/string/binding.c @@ -32,6 +32,7 @@ void abort() { #define NAPI_EXPERIMENTAL #include #include "../common.h" +#include "../entry_point.h" #include "test_null.h" enum length_type { actual_length, auto_length }; diff --git a/packages/test/symbol/binding.c b/packages/test/symbol/binding.c index a87b275c..b1465829 100644 --- a/packages/test/symbol/binding.c +++ b/packages/test/symbol/binding.c @@ -1,5 +1,6 @@ #include #include "../common.h" +#include "../entry_point.h" static napi_value New(napi_env env, napi_callback_info info) { size_t argc = 1; diff --git a/packages/test/typedarray/binding.c b/packages/test/typedarray/binding.c index fd454cb7..3449de18 100644 --- a/packages/test/typedarray/binding.c +++ b/packages/test/typedarray/binding.c @@ -6,6 +6,7 @@ // #include // #include #include "../common.h" +#include "../entry_point.h" void* malloc(size_t size); void free(void* p);