diff --git a/test/ffi/fixture_library/binding.gyp b/test/ffi/fixture_library/binding.gyp index 8e0a383f9e4c29..ede9e9416da4f6 100644 --- a/test/ffi/fixture_library/binding.gyp +++ b/test/ffi/fixture_library/binding.gyp @@ -4,15 +4,6 @@ 'target_name': 'ffi_test_library', 'sources': ['ffi_test_library.c'], 'type': 'shared_library', - 'conditions': [ - [ 'OS=="win"', { - 'msvs_settings': { - 'VCLinkerTool': { - 'ModuleDefinitionFile': 'ffi_test_library.def', - }, - }, - }], - ], } ] } diff --git a/test/ffi/fixture_library/ffi_test_library.c b/test/ffi/fixture_library/ffi_test_library.c index f387b156f227f1..85f2ccd33fdfc1 100644 --- a/test/ffi/fixture_library/ffi_test_library.c +++ b/test/ffi/fixture_library/ffi_test_library.c @@ -3,87 +3,93 @@ #include #include +#ifdef _WIN32 +#define FFI_EXPORT __declspec(dllexport) +#else +#define FFI_EXPORT +#endif + // Integer operations. -int8_t add_i8(int8_t a, int8_t b) { +FFI_EXPORT int8_t add_i8(int8_t a, int8_t b) { return a + b; } -uint8_t add_u8(uint8_t a, uint8_t b) { +FFI_EXPORT uint8_t add_u8(uint8_t a, uint8_t b) { return a + b; } -int16_t add_i16(int16_t a, int16_t b) { +FFI_EXPORT int16_t add_i16(int16_t a, int16_t b) { return a + b; } -uint16_t add_u16(uint16_t a, uint16_t b) { +FFI_EXPORT uint16_t add_u16(uint16_t a, uint16_t b) { return a + b; } -int32_t add_i32(int32_t a, int32_t b) { +FFI_EXPORT int32_t add_i32(int32_t a, int32_t b) { return a + b; } -uint32_t add_u32(uint32_t a, uint32_t b) { +FFI_EXPORT uint32_t add_u32(uint32_t a, uint32_t b) { return a + b; } -int64_t add_i64(int64_t a, int64_t b) { +FFI_EXPORT int64_t add_i64(int64_t a, int64_t b) { return a + b; } -uint64_t add_u64(uint64_t a, uint64_t b) { +FFI_EXPORT uint64_t add_u64(uint64_t a, uint64_t b) { return a + b; } -char identity_char(char value) { +FFI_EXPORT char identity_char(char value) { return value; } -int32_t char_is_signed(void) { +FFI_EXPORT int32_t char_is_signed(void) { return ((char)-1) < 0; } // Floating point operations. -float add_f32(float a, float b) { +FFI_EXPORT float add_f32(float a, float b) { return a + b; } -float multiply_f32(float a, float b) { +FFI_EXPORT float multiply_f32(float a, float b) { return a * b; } -double add_f64(double a, double b) { +FFI_EXPORT double add_f64(double a, double b) { return a + b; } -double multiply_f64(double a, double b) { +FFI_EXPORT double multiply_f64(double a, double b) { return a * b; } // Pointer operations. -void* identity_pointer(void* ptr) { +FFI_EXPORT void* identity_pointer(void* ptr) { return ptr; } -uint64_t pointer_to_usize(void* ptr) { +FFI_EXPORT uint64_t pointer_to_usize(void* ptr) { return (uint64_t)(uintptr_t)ptr; } -void* usize_to_pointer(uint64_t addr) { +FFI_EXPORT void* usize_to_pointer(uint64_t addr) { return (void*)(uintptr_t)addr; } // String operations. -uint64_t string_length(const char* str) { +FFI_EXPORT uint64_t string_length(const char* str) { return str ? strlen(str) : 0; } -char* string_concat(const char* a, const char* b) { +FFI_EXPORT char* string_concat(const char* a, const char* b) { if (!a || !b) { return NULL; } @@ -101,7 +107,7 @@ char* string_concat(const char* a, const char* b) { return result; } -char* string_duplicate(const char* str) { +FFI_EXPORT char* string_duplicate(const char* str) { if (!str) { return NULL; } @@ -117,13 +123,13 @@ char* string_duplicate(const char* str) { return result; } -void free_string(char* str) { +FFI_EXPORT void free_string(char* str) { free(str); } // Buffer/Array operations. -void fill_buffer(uint8_t* buffer, uint64_t length, uint32_t value) { +FFI_EXPORT void fill_buffer(uint8_t* buffer, uint64_t length, uint32_t value) { if (!buffer) { return; } @@ -131,7 +137,7 @@ void fill_buffer(uint8_t* buffer, uint64_t length, uint32_t value) { memset(buffer, (uint8_t)value, length); } -uint64_t sum_buffer(const uint8_t* buffer, uint64_t length) { +FFI_EXPORT uint64_t sum_buffer(const uint8_t* buffer, uint64_t length) { if (!buffer) { return 0; } @@ -143,7 +149,7 @@ uint64_t sum_buffer(const uint8_t* buffer, uint64_t length) { return sum; } -void reverse_buffer(uint8_t* buffer, uint64_t length) { +FFI_EXPORT void reverse_buffer(uint8_t* buffer, uint64_t length) { if (!buffer || length == 0) { return; } @@ -168,37 +174,37 @@ typedef struct { float z; } Point3D; -Point make_point(int32_t x, int32_t y) { +FFI_EXPORT Point make_point(int32_t x, int32_t y) { Point p = {x, y}; return p; } -int32_t point_distance_squared(Point p1, Point p2) { +FFI_EXPORT int32_t point_distance_squared(Point p1, Point p2) { int32_t dx = p1.x - p2.x; int32_t dy = p1.y - p2.y; return dx * dx + dy * dy; } -Point3D make_point3d(float x, float y, float z) { +FFI_EXPORT Point3D make_point3d(float x, float y, float z) { Point3D p = {x, y, z}; return p; } -float point3d_magnitude_squared(Point3D p) { +FFI_EXPORT float point3d_magnitude_squared(Point3D p) { return p.x * p.x + p.y * p.y + p.z * p.z; } // Boolean operations. -int32_t logical_and(int32_t a, int32_t b) { +FFI_EXPORT int32_t logical_and(int32_t a, int32_t b) { return (a && b) ? 1 : 0; } -int32_t logical_or(int32_t a, int32_t b) { +FFI_EXPORT int32_t logical_or(int32_t a, int32_t b) { return (a || b) ? 1 : 0; } -int32_t logical_not(int32_t a) { +FFI_EXPORT int32_t logical_not(int32_t a) { return !a ? 1 : 0; } @@ -206,15 +212,15 @@ int32_t logical_not(int32_t a) { static int32_t global_counter = 0; -void increment_counter(void) { +FFI_EXPORT void increment_counter(void) { global_counter++; } -int32_t get_counter(void) { +FFI_EXPORT int32_t get_counter(void) { return global_counter; } -void reset_counter(void) { +FFI_EXPORT void reset_counter(void) { global_counter = 0; } @@ -227,7 +233,7 @@ typedef void (*VoidCallback)(void); typedef void (*StringCallback)(const char*); typedef int32_t (*BinaryIntCallback)(int32_t, int32_t); -int32_t call_int_callback(IntCallback callback, int32_t value) { +FFI_EXPORT int32_t call_int_callback(IntCallback callback, int32_t value) { if (!callback) { return -1; } @@ -235,7 +241,7 @@ int32_t call_int_callback(IntCallback callback, int32_t value) { return callback(value); } -int8_t call_int8_callback(Int8Callback callback, int8_t value) { +FFI_EXPORT int8_t call_int8_callback(Int8Callback callback, int8_t value) { if (!callback) { return 0; } @@ -243,7 +249,7 @@ int8_t call_int8_callback(Int8Callback callback, int8_t value) { return callback(value); } -int32_t call_pointer_callback_is_null(PointerCallback callback) { +FFI_EXPORT int32_t call_pointer_callback_is_null(PointerCallback callback) { if (!callback) { return 1; } @@ -251,21 +257,21 @@ int32_t call_pointer_callback_is_null(PointerCallback callback) { return callback() == NULL; } -void call_void_callback(VoidCallback callback) { +FFI_EXPORT void call_void_callback(VoidCallback callback) { if (callback) { callback(); } } -void call_string_callback(StringCallback callback, const char* str) { +FFI_EXPORT void call_string_callback(StringCallback callback, const char* str) { if (callback) { callback(str); } } -int32_t call_binary_int_callback(BinaryIntCallback callback, - int32_t a, - int32_t b) { +FFI_EXPORT int32_t call_binary_int_callback(BinaryIntCallback callback, + int32_t a, + int32_t b) { if (!callback) { return -1; } @@ -274,7 +280,8 @@ int32_t call_binary_int_callback(BinaryIntCallback callback, } // Callback that calls multiple times. -void call_callback_multiple_times(IntCallback callback, int32_t times) { +FFI_EXPORT void call_callback_multiple_times(IntCallback callback, + int32_t times) { if (!callback) { return; } @@ -286,7 +293,7 @@ void call_callback_multiple_times(IntCallback callback, int32_t times) { // Edge cases and error conditions. -int32_t divide_i32(int32_t a, int32_t b) { +FFI_EXPORT int32_t divide_i32(int32_t a, int32_t b) { if (b == 0) { return 0; } @@ -294,46 +301,48 @@ int32_t divide_i32(int32_t a, int32_t b) { return a / b; } -void* allocate_memory(size_t size) { +FFI_EXPORT void* allocate_memory(size_t size) { return malloc(size); } -void deallocate_memory(void* ptr) { +FFI_EXPORT void deallocate_memory(void* ptr) { free(ptr); } // Null pointer handling. -int32_t safe_strlen(const char* str) { +FFI_EXPORT int32_t safe_strlen(const char* str) { return str ? (int32_t)strlen(str) : -1; } // Multi-parameter functions. -int32_t sum_five_i32(int32_t a, int32_t b, int32_t c, int32_t d, int32_t e) { +FFI_EXPORT int32_t +sum_five_i32(int32_t a, int32_t b, int32_t c, int32_t d, int32_t e) { return a + b + c + d + e; } -double sum_five_f64(double a, double b, double c, double d, double e) { +FFI_EXPORT double sum_five_f64( + double a, double b, double c, double d, double e) { return a + b + c + d + e; } // Mixed parameter types. -double mixed_operation(int32_t i, float f, double d, uint32_t u) { +FFI_EXPORT double mixed_operation(int32_t i, float f, double d, uint32_t u) { return (double)i + (double)f + d + (double)u; } // Constant values for testing. -const int32_t CONSTANT_I32 = 42; -const uint64_t CONSTANT_U64 = 0xDEADBEEFCAFEBABEULL; -const float CONSTANT_F32 = 3.14159f; -const double CONSTANT_F64 = 2.718281828459045; -const char* CONSTANT_STRING = "Hello from FFI addon"; +FFI_EXPORT const int32_t CONSTANT_I32 = 42; +FFI_EXPORT const uint64_t CONSTANT_U64 = 0xDEADBEEFCAFEBABEULL; +FFI_EXPORT const float CONSTANT_F32 = 3.14159f; +FFI_EXPORT const double CONSTANT_F64 = 2.718281828459045; +FFI_EXPORT const char* CONSTANT_STRING = "Hello from FFI addon"; // Array/pointer indexing. -int32_t array_get_i32(const int32_t* arr, size_t index) { +FFI_EXPORT int32_t array_get_i32(const int32_t* arr, size_t index) { if (!arr) { return 0; } @@ -341,7 +350,7 @@ int32_t array_get_i32(const int32_t* arr, size_t index) { return arr[index]; } -void array_set_i32(int32_t* arr, size_t index, int32_t value) { +FFI_EXPORT void array_set_i32(int32_t* arr, size_t index, int32_t value) { if (!arr) { return; } @@ -349,7 +358,7 @@ void array_set_i32(int32_t* arr, size_t index, int32_t value) { arr[index] = value; } -double array_get_f64(const double* arr, size_t index) { +FFI_EXPORT double array_get_f64(const double* arr, size_t index) { if (!arr) { return 0.0; } @@ -357,7 +366,7 @@ double array_get_f64(const double* arr, size_t index) { return arr[index]; } -void array_set_f64(double* arr, size_t index, double value) { +FFI_EXPORT void array_set_f64(double* arr, size_t index, double value) { if (!arr) { return; } diff --git a/vcbuild.bat b/vcbuild.bat index ef3576a987e6ad..13834e8fbedc82 100644 --- a/vcbuild.bat +++ b/vcbuild.bat @@ -17,7 +17,7 @@ set "CI_NATIVE_SUITES=%NATIVE_SUITES% benchmark" set "CI_JS_SUITES=%JS_SUITES% pummel" set CI_DOC=doctool @rem Same as the test-ci target in Makefile -set "common_test_suites=%JS_SUITES% %NATIVE_SUITES%&set build_addons=1&set build_js_native_api_tests=1&set build_node_api_tests=1" +set "common_test_suites=%JS_SUITES% %NATIVE_SUITES%&set build_addons=1&set build_js_native_api_tests=1&set build_node_api_tests=1&set build_ffi_tests=1" @rem Process arguments. set config=Release @@ -62,6 +62,7 @@ set dll= set enable_static= set build_js_native_api_tests= set build_node_api_tests= +set build_ffi_tests= set test_node_inspect= set test_check_deopts= set v8_test_options= @@ -107,11 +108,12 @@ if /i "%1"=="v8temporal" set v8temporal=1&goto arg-ok if /i "%1"=="v8windbg" set v8windbg=1&goto arg-ok if /i "%1"=="licensertf" set licensertf=1&goto arg-ok if /i "%1"=="test" set test_args=%test_args% %common_test_suites%&set lint_cpp=1&set lint_js=1&set lint_md=1&goto arg-ok -if /i "%1"=="test-ci-native" set test_args=%test_args% %test_ci_args% -p tap --logfile test.tap %CI_NATIVE_SUITES% %CI_DOC%&set build_addons=1&set build_js_native_api_tests=1&set build_node_api_tests=1&set cctest_args=%cctest_args% --gtest_output=xml:cctest.junit.xml&goto arg-ok -if /i "%1"=="test-ci-js" set test_args=%test_args% %test_ci_args% -p tap --logfile test.tap %CI_JS_SUITES%&set no_cctest=1&goto arg-ok +if /i "%1"=="test-ci-native" set test_args=%test_args% %test_ci_args% -p tap --logfile test.tap %CI_NATIVE_SUITES% %CI_DOC%&set build_addons=1&set build_js_native_api_tests=1&set build_node_api_tests=1&set build_ffi_tests=1&set cctest_args=%cctest_args% --gtest_output=xml:cctest.junit.xml&goto arg-ok +if /i "%1"=="test-ci-js" set test_args=%test_args% %test_ci_args% -p tap --logfile test.tap %CI_JS_SUITES%&set build_ffi_tests=1&set no_cctest=1&goto arg-ok if /i "%1"=="build-addons" set build_addons=1&goto arg-ok if /i "%1"=="build-js-native-api-tests" set build_js_native_api_tests=1&goto arg-ok if /i "%1"=="build-node-api-tests" set build_node_api_tests=1&goto arg-ok +if /i "%1"=="build-ffi-tests" set build_ffi_tests=1&goto arg-ok if /i "%1"=="test-addons" set test_args=%test_args% addons&set build_addons=1&goto arg-ok if /i "%1"=="test-doc" set test_args=%test_args% %CI_DOC%&set doc=1&&set lint_js=1&set lint_md=1&goto arg-ok if /i "%1"=="test-js-native-api" set test_args=%test_args% js-native-api&set build_js_native_api_tests=1&goto arg-ok @@ -712,10 +714,10 @@ endlocal goto build-node-api-tests :build-node-api-tests -if not defined build_node_api_tests goto run-tests +if not defined build_node_api_tests goto build-ffi-tests if not exist "%node_exe%" ( echo Failed to find node.exe - goto run-tests + goto build-ffi-tests ) echo Building node-api :: clear @@ -727,6 +729,19 @@ setlocal python "%~dp0tools\build_addons.py" "%~dp0test\node-api" --config %config% if errorlevel 1 exit /b 1 endlocal +goto build-ffi-tests + +:build-ffi-tests +if not defined build_ffi_tests goto run-tests +if not exist "%node_exe%" ( + echo Failed to find node.exe + goto run-tests +) +echo Building ffi tests +setlocal +python "%~dp0tools\build_addons.py" "%~dp0test\ffi" --config %config% +if errorlevel 1 exit /b 1 +endlocal goto run-tests :run-tests @@ -860,7 +875,7 @@ set exit_code=1 goto exit :help -echo vcbuild.bat [debug/release] [msi] [doc] [test/test-all/test-addons/test-doc/test-js-native-api/test-node-api/test-internet/test-tick-processor/test-known-issues/test-node-inspect/test-check-deopts/test-npm/test-v8/test-v8-intl/test-v8-benchmarks/test-v8-all] [ignore-flaky] [static/dll] [noprojgen] [projgen] [clang-cl] [ccache path-to-ccache] [small-icu/full-icu/without-intl] [nobuild] [nosnapshot] [nonpm] [ltcg] [licensetf] [sign] [x64/arm64] [vs2022/vs2026] [download-all] [enable-vtune] [lint/lint-ci/lint-js/lint-md] [lint-md-build] [format-md] [package] [build-release] [upload] [no-NODE-OPTIONS] [link-module path-to-module] [debug-http2] [debug-nghttp2] [clean] [cctest] [no-cctest] [openssl-no-asm] +echo vcbuild.bat [debug/release] [msi] [doc] [test/test-all/test-addons/test-doc/test-js-native-api/test-node-api/test-internet/test-tick-processor/test-known-issues/test-node-inspect/test-check-deopts/test-npm/test-v8/test-v8-intl/test-v8-benchmarks/test-v8-all] [build-addons/build-js-native-api-tests/build-node-api-tests/build-ffi-tests] [ignore-flaky] [static/dll] [noprojgen] [projgen] [clang-cl] [ccache path-to-ccache] [small-icu/full-icu/without-intl] [nobuild] [nosnapshot] [nonpm] [ltcg] [licensetf] [sign] [x64/arm64] [vs2022/vs2026] [download-all] [enable-vtune] [lint/lint-ci/lint-js/lint-md] [lint-md-build] [format-md] [package] [build-release] [upload] [no-NODE-OPTIONS] [link-module path-to-module] [debug-http2] [debug-nghttp2] [clean] [cctest] [no-cctest] [openssl-no-asm] echo Examples: echo vcbuild.bat : builds release build echo vcbuild.bat debug : builds debug build