From 5f91614f1a5fac645af3bb0262ba4b837f1855f1 Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Wed, 8 Oct 2025 12:50:03 +0200 Subject: [PATCH 01/17] FFI: isolate ffi outside of hl.h --- libhl.vcxproj | 1 + libhl.vcxproj.filters | 1 + src/hl.h | 75 +-------------------------- src/hl_ffi.h | 116 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 74 deletions(-) create mode 100644 src/hl_ffi.h diff --git a/libhl.vcxproj b/libhl.vcxproj index 12de44ea3..73d8a53fc 100644 --- a/libhl.vcxproj +++ b/libhl.vcxproj @@ -306,6 +306,7 @@ + diff --git a/libhl.vcxproj.filters b/libhl.vcxproj.filters index 915e4e9d7..a92821700 100644 --- a/libhl.vcxproj.filters +++ b/libhl.vcxproj.filters @@ -175,5 +175,6 @@ pcre + \ No newline at end of file diff --git a/src/hl.h b/src/hl.h index 30bcdf59c..f81a4acf9 100644 --- a/src/hl.h +++ b/src/hl.h @@ -847,80 +847,7 @@ HL_API void hl_throw_buffer( hl_buffer *b ); // ----------------------- FFI ------------------------------------------------------ -// match GNU C++ mangling -#define TYPE_STR "vcsilfdbBDPOATR??X?N?S?g" - -#undef _VOID -#define _NO_ARG -#define _VOID "v" -#define _I8 "c" -#define _I16 "s" -#define _I32 "i" -#define _I64 "l" -#define _F32 "f" -#define _F64 "d" -#define _BOOL "b" -#define _BYTES "B" -#define _DYN "D" -#define _FUN(t, args) "P" args "_" t -#define _OBJ(fields) "O" fields "_" -#define _ARR "A" -#define _TYPE "T" -#define _REF(t) "R" t -#define _ABSTRACT(name) "X" #name "_" -#undef _NULL -#define _NULL(t) "N" t -#define _STRUCT "S" -#define _GUID "g" - -#undef _STRING -#define _STRING _OBJ(_BYTES _I32) - -typedef struct { - hl_type *t; - uchar *bytes; - int length; -} vstring; - -#define DEFINE_PRIM(t,name,args) DEFINE_PRIM_WITH_NAME(t,name,args,name) -#define _DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = _FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END - -#if !defined(HL_NAME) -# define HL_NAME(p) p -# ifdef LIBHL_EXPORTS -# define HL_PRIM EXPORT -# undef DEFINE_PRIM -# define DEFINE_PRIM(t,name,args) _DEFINE_PRIM_WITH_NAME(t,hl_##name,args,name) -# define DEFINE_PRIM_WITH_NAME _DEFINE_PRIM_WITH_NAME -# else -# define HL_PRIM -# define DEFINE_PRIM_WITH_NAME(t,name,args,realName) -# endif -#elif defined(LIBHL_STATIC) -# ifdef __cplusplus -# define HL_PRIM extern "C" -# else -# define HL_PRIM -# endif -#define DEFINE_PRIM_WITH_NAME(t,name,args,realName) -#else -# ifdef __cplusplus -# define HL_PRIM extern "C" EXPORT -# else -# define HL_PRIM EXPORT -# endif -# define DEFINE_PRIM_WITH_NAME _DEFINE_PRIM_WITH_NAME -#endif - -#if defined(HL_GCC) && !defined(HL_CONSOLE) -# ifdef HL_CLANG -# define HL_NO_OPT __attribute__ ((optnone)) -# else -# define HL_NO_OPT __attribute__((optimize("-O0"))) -# endif -#else -# define HL_NO_OPT -#endif +#include // -------------- EXTRA ------------------------------------ diff --git a/src/hl_ffi.h b/src/hl_ffi.h new file mode 100644 index 000000000..e49539509 --- /dev/null +++ b/src/hl_ffi.h @@ -0,0 +1,116 @@ +#ifndef HLFFI_H +#define HLFFI_H + +#include + +// match GNU C++ mangling +#define HL_TYPE_STR "vcsilfdbBDPOATR??X?N?S?g" + +#define HL_NO_ARG +#define HL_VOID "v" +#define HL_I8 "c" +#define HL_I16 "s" +#define HL_I32 "i" +#define HL_I64 "l" +#define HL_F32 "f" +#define HL_F64 "d" +#define HL_BOOL "b" +#define HL_BYTES "B" +#define HL_DYN "D" +#define HL_FUN(t, args) "P" args "_" t +#define HL_OBJ(fields) "O" fields "_" +#define HL_ARR "A" +#define HL_TYPE "T" +#define HL_REF(t) "R" t +#define HL_ABSTRACT(name) "X" #name "_" +#define HL_NULL(t) "N" t +#define HL_STRUCT "S" +#define HL_GUID "g" + +#define HL_STRING _OBJ(_BYTES HL_I32) + +typedef struct { + hl_type *t; + uchar *bytes; + int length; +} vstring; + +#define HL__DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = HL_FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END + +#if !defined(HL_NAME) +# define HL_NAME(p) p +# ifdef LIBHL_EXPORTS +# define HL_PRIM EXPORT +# define HL_DEFINE_PRIM(t,name,args) HL__DEFINE_PRIM_WITH_NAME(t,hl_##name,args,name) +# define HL_DEFINE_PRIM_WITH_NAME HL__DEFINE_PRIM_WITH_NAME +# else +# define HL_PRIM +# define HL_DEFINE_PRIM(t,name,args) +# define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) +# endif +#elif defined(LIBHL_STATIC) +# ifdef __cplusplus +# define HL_PRIM extern "C" +# else +# define HL_PRIM +# endif +# define HL_DEFINE_PRIM(t,name,args) +# define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) +#else +# ifdef __cplusplus +# define HL_PRIM extern "C" EXPORT +# else +# define HL_PRIM EXPORT +# endif +# define HL_DEFINE_PRIM(t,name,args) HL__DEFINE_PRIM_WITH_NAME(t,name,args,name) +# define HL_DEFINE_PRIM_WITH_NAME HL__DEFINE_PRIM_WITH_NAME +#endif + + +#if defined(HL_GCC) && !defined(HL_CONSOLE) +# ifdef HL_CLANG +# define HL_NO_OPT __attribute__ ((optnone)) +# else +# define HL_NO_OPT __attribute__((optimize("-O0"))) +# endif +#else +# define HL_NO_OPT +#endif + +// legacy ffi without HL_ prefix +#ifndef HL_DISABLE_LEGACY_FFI + +#define TYPE_STR HL_TYPE_STR + +#undef _VOID +#define _NO_ARG +#define _VOID HL_VOID +#define _I8 HL_I8 +#define _I16 HL_I16 +#define _I32 HL_I32 +#define _I64 HL_I64 +#define _F32 HL_F32 +#define _F64 HL_F64 +#define _BOOL HL_BOOL +#define _BYTES HL_BYTES +#define _DYN HL_DYN +#define _FUN(t, args) HL_FUN(t, args) +#define _OBJ(fields) HL_OBJ(fields) +#define _ARR HL_ARR +#define _TYPE HL_TYPE +#define _REF(t) HL_REF(t) +#define _ABSTRACT(name) HL_ABSTRACT(name) +#undef _NULL +#define _NULL(t) HL_NULL(t) +#define _STRUCT HL_STRUCT +#define _GUID HL_GUID + +#undef _STRING +#define _STRING HL_STRING + +#define DEFINE_PRIM HL_DEFINE_PRIM +#define DEFINE_PRIM_WITH_NAME HL_DEFINE_PRIM_WITH_NAME + +#endif // ifndef HL_DISABLE_LEGACY_FFI + +#endif // ifndef HLFFI_H From d26d87442f78904b2d8aee12eb0bfd91e8b6d809 Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Wed, 31 Dec 2025 15:23:47 +0100 Subject: [PATCH 02/17] Do not auto include hl_ffi.h in hl.h if disable legacy --- src/hl.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/hl.h b/src/hl.h index f81a4acf9..1373d9bb9 100644 --- a/src/hl.h +++ b/src/hl.h @@ -847,7 +847,9 @@ HL_API void hl_throw_buffer( hl_buffer *b ); // ----------------------- FFI ------------------------------------------------------ +#ifndef HL_DISABLE_LEGACY_FFI #include +#endif // -------------- EXTRA ------------------------------------ From f23d428b952b6c1565d8fba90eed1cca8e1134c0 Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Fri, 2 Jan 2026 10:30:53 +0100 Subject: [PATCH 03/17] Add hl_ffi.h in release, cleanup vcxproj ffi not used --- CMakeLists.txt | 1 + Makefile | 6 +++--- libhl.vcxproj | 1 - libhl.vcxproj.filters | 1 - 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a78b53316..0c5a9b938 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -171,6 +171,7 @@ target_compile_features(libhl PUBLIC cxx_std_11 c_std_11) set(public_headers src/hl.h + src/hl_ffi.h src/hlc.h ) diff --git a/Makefile b/Makefile index d691933c2..8a3339399 100644 --- a/Makefile +++ b/Makefile @@ -241,11 +241,11 @@ endif cp *.hdll $(INSTALL_LIB_DIR) cp libhl.${LIBEXT} $(INSTALL_LIB_DIR) mkdir -p $(INSTALL_INCLUDE_DIR) - cp src/hl.h src/hlc.h src/hlc_main.c $(INSTALL_INCLUDE_DIR) + cp src/hl.h src/hl_ffi.h src/hlc.h src/hlc_main.c $(INSTALL_INCLUDE_DIR) uninstall: rm -f $(INSTALL_BIN_DIR)/hl $(INSTALL_LIB_DIR)/libhl.${LIBEXT} $(INSTALL_LIB_DIR)/*.hdll - rm -f $(INSTALL_INCLUDE_DIR)/hl.h $(INSTALL_INCLUDE_DIR)/hlc.h $(INSTALL_INCLUDE_DIR)/hlc_main.c + rm -f $(INSTALL_INCLUDE_DIR)/hl.h $(INSTALL_INCLUDE_DIR)/hl_ffi.h $(INSTALL_INCLUDE_DIR)/hlc.h $(INSTALL_INCLUDE_DIR)/hlc_main.c libs: $(LIBS) @@ -354,7 +354,7 @@ release_prepare: rm -rf $(PACKAGE_NAME) mkdir $(PACKAGE_NAME) mkdir $(PACKAGE_NAME)/include - cp src/hl.h src/hlc.h src/hlc_main.c $(PACKAGE_NAME)/include + cp src/hl.h src/hl_ffi.h src/hlc.h src/hlc_main.c $(PACKAGE_NAME)/include release_win: cp $(BUILD_DIR)/{hl.exe,libhl.dll,*.hdll,*.lib} $(PACKAGE_NAME) diff --git a/libhl.vcxproj b/libhl.vcxproj index 73d8a53fc..12de44ea3 100644 --- a/libhl.vcxproj +++ b/libhl.vcxproj @@ -306,7 +306,6 @@ - diff --git a/libhl.vcxproj.filters b/libhl.vcxproj.filters index a92821700..915e4e9d7 100644 --- a/libhl.vcxproj.filters +++ b/libhl.vcxproj.filters @@ -175,6 +175,5 @@ pcre - \ No newline at end of file From 6b6feb56196a42c460a996e63b150c06e1daf016 Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Mon, 5 Jan 2026 11:27:07 +0100 Subject: [PATCH 04/17] Add -D HL_DISABLE_LEGACY_FFI to hlc template --- other/haxelib/templates/ci/__file__.vcxproj | 8 ++++---- other/haxelib/templates/hxcpp/Build.xml | 1 + other/haxelib/templates/make/Makefile | 2 +- other/haxelib/templates/vs2015/__file__.vcxproj | 8 ++++---- other/haxelib/templates/vs2017/__file__.vcxproj | 8 ++++---- other/haxelib/templates/vs2019/__file__.vcxproj | 8 ++++---- other/haxelib/templates/vs2022/__file__.vcxproj | 8 ++++---- 7 files changed, 22 insertions(+), 21 deletions(-) diff --git a/other/haxelib/templates/ci/__file__.vcxproj b/other/haxelib/templates/ci/__file__.vcxproj index a5cf73547..d1210c68a 100644 --- a/other/haxelib/templates/ci/__file__.vcxproj +++ b/other/haxelib/templates/ci/__file__.vcxproj @@ -95,7 +95,7 @@ Level3 Disabled true - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir)\%(Filename).obj @@ -112,7 +112,7 @@ true true true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir)\%(Filename).obj @@ -129,7 +129,7 @@ Level3 Disabled true - _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir)\%(Filename).obj @@ -146,7 +146,7 @@ true true true - NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + NDEBUG;_CONSOLE;HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir)\%(Filename).obj diff --git a/other/haxelib/templates/hxcpp/Build.xml b/other/haxelib/templates/hxcpp/Build.xml index 23167f2b1..6648514f7 100644 --- a/other/haxelib/templates/hxcpp/Build.xml +++ b/other/haxelib/templates/hxcpp/Build.xml @@ -2,6 +2,7 @@ + diff --git a/other/haxelib/templates/make/Makefile b/other/haxelib/templates/make/Makefile index 994543531..76232efb3 100644 --- a/other/haxelib/templates/make/Makefile +++ b/other/haxelib/templates/make/Makefile @@ -1,7 +1,7 @@ # Auto-generated from hashlink's Makefile template; do not edit UNAME := $(shell uname) -CFLAGS=-O3 -std=c11 -DHL_MAKE -I$$makePath(::relDir::) +CFLAGS=-O3 -std=c11 -DHL_MAKE -DHL_DISABLE_LEGACY_FFI -I$$makePath(::relDir::) ifeq ($(UNAME),Darwin) LDLIBS=-lhl -lm::foreach libraries::::if (name == "uv"):: -luv::end:: /usr/local/lib/::name::.hdll::end:: else diff --git a/other/haxelib/templates/vs2015/__file__.vcxproj b/other/haxelib/templates/vs2015/__file__.vcxproj index 943589982..b1b1872a4 100644 --- a/other/haxelib/templates/vs2015/__file__.vcxproj +++ b/other/haxelib/templates/vs2015/__file__.vcxproj @@ -95,7 +95,7 @@ Level3 Disabled - WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -115,7 +115,7 @@ Level3 Disabled - _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -137,7 +137,7 @@ MaxSpeed true true - WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -161,7 +161,7 @@ MaxSpeed true true - NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true diff --git a/other/haxelib/templates/vs2017/__file__.vcxproj b/other/haxelib/templates/vs2017/__file__.vcxproj index 7f5dff64a..a9526b01e 100644 --- a/other/haxelib/templates/vs2017/__file__.vcxproj +++ b/other/haxelib/templates/vs2017/__file__.vcxproj @@ -97,7 +97,7 @@ Level3 Disabled true - WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -118,7 +118,7 @@ Level3 Disabled true - _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -141,7 +141,7 @@ true true true - WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -166,7 +166,7 @@ true true true - NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true diff --git a/other/haxelib/templates/vs2019/__file__.vcxproj b/other/haxelib/templates/vs2019/__file__.vcxproj index 9b78d2c08..43acd3963 100644 --- a/other/haxelib/templates/vs2019/__file__.vcxproj +++ b/other/haxelib/templates/vs2019/__file__.vcxproj @@ -95,7 +95,7 @@ Level3 Disabled true - WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -116,7 +116,7 @@ true true true - WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -137,7 +137,7 @@ Level3 Disabled true - _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -158,7 +158,7 @@ true true true - NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true diff --git a/other/haxelib/templates/vs2022/__file__.vcxproj b/other/haxelib/templates/vs2022/__file__.vcxproj index cfe0ce61a..79be09bcb 100644 --- a/other/haxelib/templates/vs2022/__file__.vcxproj +++ b/other/haxelib/templates/vs2022/__file__.vcxproj @@ -95,7 +95,7 @@ Level3 Disabled true - WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -116,7 +116,7 @@ true true true - WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -137,7 +137,7 @@ Level3 Disabled true - _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -158,7 +158,7 @@ true true true - NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) + NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true From 5dd06506baecc08dd106177f83757a053898732f Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Mon, 5 Jan 2026 14:23:24 +0100 Subject: [PATCH 05/17] Keep legacy definitions in hl.h, fix HL_STRING --- src/hl.h | 85 +++++++++++++++++++++++++++++++++++++++++++++++++- src/hl_ffi.h | 87 ++-------------------------------------------------- 2 files changed, 87 insertions(+), 85 deletions(-) diff --git a/src/hl.h b/src/hl.h index 1373d9bb9..62b312c0b 100644 --- a/src/hl.h +++ b/src/hl.h @@ -848,7 +848,90 @@ HL_API void hl_throw_buffer( hl_buffer *b ); // ----------------------- FFI ------------------------------------------------------ #ifndef HL_DISABLE_LEGACY_FFI -#include + +// match GNU C++ mangling +#define TYPE_STR "vcsilfdbBDPOATR??X?N?S?g" + +#undef _VOID +#define _NO_ARG +#define _VOID "v" +#define _I8 "c" +#define _I16 "s" +#define _I32 "i" +#define _I64 "l" +#define _F32 "f" +#define _F64 "d" +#define _BOOL "b" +#define _BYTES "B" +#define _DYN "D" +#define _FUN(t, args) "P" args "_" t +#define _OBJ(fields) "O" fields "_" +#define _ARR "A" +#define _TYPE "T" +#define _REF(t) "R" t +#define _ABSTRACT(name) "X" #name "_" +#undef _NULL +#define _NULL(t) "N" t +#define _STRUCT "S" +#define _GUID "g" + +#undef _STRING +#define _STRING _OBJ(_BYTES _I32) + +#define DEFINE_PRIM(t,name,args) DEFINE_PRIM_WITH_NAME(t,name,args,name) +#define _DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = _FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END + +#endif // HL_DISABLE_LEGACY_FFI + +#if !defined(HL_NAME) +# define HL_NAME(p) p +# ifdef LIBHL_EXPORTS +# define HL_PRIM EXPORT +# ifndef HL_DISABLE_LEGACY_FFI +# undef DEFINE_PRIM +# define DEFINE_PRIM(t,name,args) _DEFINE_PRIM_WITH_NAME(t,hl_##name,args,name) +# define DEFINE_PRIM_WITH_NAME _DEFINE_PRIM_WITH_NAME +# endif +# else +# define HL_PRIM +# ifndef HL_DISABLE_LEGACY_FFI +# define DEFINE_PRIM_WITH_NAME(t,name,args,realName) +# endif +# endif +#elif defined(LIBHL_STATIC) +# ifdef __cplusplus +# define HL_PRIM extern "C" +# else +# define HL_PRIM +# endif +# ifndef HL_DISABLE_LEGACY_FFI +# define DEFINE_PRIM_WITH_NAME(t,name,args,realName) +# endif +#else +# ifdef __cplusplus +# define HL_PRIM extern "C" EXPORT +# else +# define HL_PRIM EXPORT +# endif +# ifndef HL_DISABLE_LEGACY_FFI +# define DEFINE_PRIM_WITH_NAME _DEFINE_PRIM_WITH_NAME +# endif +#endif + +typedef struct { + hl_type *t; + uchar *bytes; + int length; +} vstring; + +#if defined(HL_GCC) && !defined(HL_CONSOLE) +# ifdef HL_CLANG +# define HL_NO_OPT __attribute__ ((optnone)) +# else +# define HL_NO_OPT __attribute__((optimize("-O0"))) +# endif +#else +# define HL_NO_OPT #endif // -------------- EXTRA ------------------------------------ diff --git a/src/hl_ffi.h b/src/hl_ffi.h index e49539509..85b305d13 100644 --- a/src/hl_ffi.h +++ b/src/hl_ffi.h @@ -1,8 +1,6 @@ #ifndef HLFFI_H #define HLFFI_H -#include - // match GNU C++ mangling #define HL_TYPE_STR "vcsilfdbBDPOATR??X?N?S?g" @@ -27,90 +25,11 @@ #define HL_STRUCT "S" #define HL_GUID "g" -#define HL_STRING _OBJ(_BYTES HL_I32) - -typedef struct { - hl_type *t; - uchar *bytes; - int length; -} vstring; +#define HL_STRING HL_OBJ(HL_BYTES HL_I32) #define HL__DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = HL_FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END -#if !defined(HL_NAME) -# define HL_NAME(p) p -# ifdef LIBHL_EXPORTS -# define HL_PRIM EXPORT -# define HL_DEFINE_PRIM(t,name,args) HL__DEFINE_PRIM_WITH_NAME(t,hl_##name,args,name) -# define HL_DEFINE_PRIM_WITH_NAME HL__DEFINE_PRIM_WITH_NAME -# else -# define HL_PRIM -# define HL_DEFINE_PRIM(t,name,args) -# define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) -# endif -#elif defined(LIBHL_STATIC) -# ifdef __cplusplus -# define HL_PRIM extern "C" -# else -# define HL_PRIM -# endif -# define HL_DEFINE_PRIM(t,name,args) -# define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) -#else -# ifdef __cplusplus -# define HL_PRIM extern "C" EXPORT -# else -# define HL_PRIM EXPORT -# endif -# define HL_DEFINE_PRIM(t,name,args) HL__DEFINE_PRIM_WITH_NAME(t,name,args,name) -# define HL_DEFINE_PRIM_WITH_NAME HL__DEFINE_PRIM_WITH_NAME -#endif - - -#if defined(HL_GCC) && !defined(HL_CONSOLE) -# ifdef HL_CLANG -# define HL_NO_OPT __attribute__ ((optnone)) -# else -# define HL_NO_OPT __attribute__((optimize("-O0"))) -# endif -#else -# define HL_NO_OPT -#endif - -// legacy ffi without HL_ prefix -#ifndef HL_DISABLE_LEGACY_FFI - -#define TYPE_STR HL_TYPE_STR - -#undef _VOID -#define _NO_ARG -#define _VOID HL_VOID -#define _I8 HL_I8 -#define _I16 HL_I16 -#define _I32 HL_I32 -#define _I64 HL_I64 -#define _F32 HL_F32 -#define _F64 HL_F64 -#define _BOOL HL_BOOL -#define _BYTES HL_BYTES -#define _DYN HL_DYN -#define _FUN(t, args) HL_FUN(t, args) -#define _OBJ(fields) HL_OBJ(fields) -#define _ARR HL_ARR -#define _TYPE HL_TYPE -#define _REF(t) HL_REF(t) -#define _ABSTRACT(name) HL_ABSTRACT(name) -#undef _NULL -#define _NULL(t) HL_NULL(t) -#define _STRUCT HL_STRUCT -#define _GUID HL_GUID - -#undef _STRING -#define _STRING HL_STRING - -#define DEFINE_PRIM HL_DEFINE_PRIM -#define DEFINE_PRIM_WITH_NAME HL_DEFINE_PRIM_WITH_NAME - -#endif // ifndef HL_DISABLE_LEGACY_FFI +#define HL_DEFINE_PRIM(t,name,args) HL__DEFINE_PRIM_WITH_NAME(t,name,args,name) +#define HL_DEFINE_PRIM_WITH_NAME HL__DEFINE_PRIM_WITH_NAME #endif // ifndef HLFFI_H From d42138cba9328718d6bc03b9faf893a7a754e363 Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Mon, 5 Jan 2026 14:32:21 +0100 Subject: [PATCH 06/17] Revert "Add -D HL_DISABLE_LEGACY_FFI to hlc template" This reverts commit 6b6feb56196a42c460a996e63b150c06e1daf016. --- other/haxelib/templates/ci/__file__.vcxproj | 8 ++++---- other/haxelib/templates/hxcpp/Build.xml | 1 - other/haxelib/templates/make/Makefile | 2 +- other/haxelib/templates/vs2015/__file__.vcxproj | 8 ++++---- other/haxelib/templates/vs2017/__file__.vcxproj | 8 ++++---- other/haxelib/templates/vs2019/__file__.vcxproj | 8 ++++---- other/haxelib/templates/vs2022/__file__.vcxproj | 8 ++++---- 7 files changed, 21 insertions(+), 22 deletions(-) diff --git a/other/haxelib/templates/ci/__file__.vcxproj b/other/haxelib/templates/ci/__file__.vcxproj index d1210c68a..a5cf73547 100644 --- a/other/haxelib/templates/ci/__file__.vcxproj +++ b/other/haxelib/templates/ci/__file__.vcxproj @@ -95,7 +95,7 @@ Level3 Disabled true - WIN32;_DEBUG;_CONSOLE;HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir)\%(Filename).obj @@ -112,7 +112,7 @@ true true true - WIN32;NDEBUG;_CONSOLE;HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir)\%(Filename).obj @@ -129,7 +129,7 @@ Level3 Disabled true - _DEBUG;_CONSOLE;HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir)\%(Filename).obj @@ -146,7 +146,7 @@ true true true - NDEBUG;_CONSOLE;HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir)\%(Filename).obj diff --git a/other/haxelib/templates/hxcpp/Build.xml b/other/haxelib/templates/hxcpp/Build.xml index 6648514f7..23167f2b1 100644 --- a/other/haxelib/templates/hxcpp/Build.xml +++ b/other/haxelib/templates/hxcpp/Build.xml @@ -2,7 +2,6 @@ - diff --git a/other/haxelib/templates/make/Makefile b/other/haxelib/templates/make/Makefile index 76232efb3..994543531 100644 --- a/other/haxelib/templates/make/Makefile +++ b/other/haxelib/templates/make/Makefile @@ -1,7 +1,7 @@ # Auto-generated from hashlink's Makefile template; do not edit UNAME := $(shell uname) -CFLAGS=-O3 -std=c11 -DHL_MAKE -DHL_DISABLE_LEGACY_FFI -I$$makePath(::relDir::) +CFLAGS=-O3 -std=c11 -DHL_MAKE -I$$makePath(::relDir::) ifeq ($(UNAME),Darwin) LDLIBS=-lhl -lm::foreach libraries::::if (name == "uv"):: -luv::end:: /usr/local/lib/::name::.hdll::end:: else diff --git a/other/haxelib/templates/vs2015/__file__.vcxproj b/other/haxelib/templates/vs2015/__file__.vcxproj index b1b1872a4..943589982 100644 --- a/other/haxelib/templates/vs2015/__file__.vcxproj +++ b/other/haxelib/templates/vs2015/__file__.vcxproj @@ -95,7 +95,7 @@ Level3 Disabled - WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -115,7 +115,7 @@ Level3 Disabled - _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -137,7 +137,7 @@ MaxSpeed true true - WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -161,7 +161,7 @@ MaxSpeed true true - NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true diff --git a/other/haxelib/templates/vs2017/__file__.vcxproj b/other/haxelib/templates/vs2017/__file__.vcxproj index a9526b01e..7f5dff64a 100644 --- a/other/haxelib/templates/vs2017/__file__.vcxproj +++ b/other/haxelib/templates/vs2017/__file__.vcxproj @@ -97,7 +97,7 @@ Level3 Disabled true - WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -118,7 +118,7 @@ Level3 Disabled true - _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -141,7 +141,7 @@ true true true - WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -166,7 +166,7 @@ true true true - NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true diff --git a/other/haxelib/templates/vs2019/__file__.vcxproj b/other/haxelib/templates/vs2019/__file__.vcxproj index 43acd3963..9b78d2c08 100644 --- a/other/haxelib/templates/vs2019/__file__.vcxproj +++ b/other/haxelib/templates/vs2019/__file__.vcxproj @@ -95,7 +95,7 @@ Level3 Disabled true - WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -116,7 +116,7 @@ true true true - WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -137,7 +137,7 @@ Level3 Disabled true - _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -158,7 +158,7 @@ true true true - NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true diff --git a/other/haxelib/templates/vs2022/__file__.vcxproj b/other/haxelib/templates/vs2022/__file__.vcxproj index 79be09bcb..cfe0ce61a 100644 --- a/other/haxelib/templates/vs2022/__file__.vcxproj +++ b/other/haxelib/templates/vs2022/__file__.vcxproj @@ -95,7 +95,7 @@ Level3 Disabled true - WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -116,7 +116,7 @@ true true true - WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -137,7 +137,7 @@ Level3 Disabled true - _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true @@ -158,7 +158,7 @@ true true true - NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::HL_DISABLE_LEGACY_FFI;%(PreprocessorDefinitions) + NDEBUG;_CONSOLE;::if (jumboBuild != "true")::HL_MAKE;::end::%(PreprocessorDefinitions) true $(IntDir)\%(RelativeDir) true From 358f36fd2cb0d2fcab5873c3e99a534a88fa8619 Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Mon, 5 Jan 2026 14:33:26 +0100 Subject: [PATCH 07/17] Add -D HL_DISABLE_LEGACY_FFI in hlc.h instead of template --- src/hlc.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/hlc.h b/src/hlc.h index 00cc4d2f5..c9ba45953 100644 --- a/src/hlc.h +++ b/src/hlc.h @@ -23,6 +23,7 @@ #define HLC_H #include +#define HL_DISABLE_LEGACY_FFI #include #ifdef HLC_BOOT From 11af8b31a5b9d993fabefc49b7b2874f12a43c2a Mon Sep 17 00:00:00 2001 From: Tobiasz Laskowski Date: Tue, 17 Mar 2026 23:24:07 +0000 Subject: [PATCH 08/17] Avoid redefining DEFINE_PRIM --- src/hl.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/hl.h b/src/hl.h index 12848b98c..e73ea75e2 100644 --- a/src/hl.h +++ b/src/hl.h @@ -876,7 +876,11 @@ HL_API void hl_throw_buffer( hl_buffer *b ); #undef _STRING #define _STRING _OBJ(_BYTES _I32) -#define DEFINE_PRIM(t,name,args) DEFINE_PRIM_WITH_NAME(t,name,args,name) +#if defined(LIBHL_EXPORTS) +# define DEFINE_PRIM(t,name,args) DEFINE_PRIM_WITH_NAME(t,hl_##name,args,name) +#else +# define DEFINE_PRIM(t,name,args) DEFINE_PRIM_WITH_NAME(t,name,args,name) +#endif #define _DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = _FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END #endif // HL_DISABLE_LEGACY_FFI @@ -886,8 +890,6 @@ HL_API void hl_throw_buffer( hl_buffer *b ); # ifdef LIBHL_EXPORTS # define HL_PRIM EXPORT # ifndef HL_DISABLE_LEGACY_FFI -# undef DEFINE_PRIM -# define DEFINE_PRIM(t,name,args) _DEFINE_PRIM_WITH_NAME(t,hl_##name,args,name) # define DEFINE_PRIM_WITH_NAME _DEFINE_PRIM_WITH_NAME # endif # else From 4711bae7db0b6c85bb52c594914723cb628716bd Mon Sep 17 00:00:00 2001 From: Tobiasz Laskowski Date: Tue, 17 Mar 2026 23:34:53 +0000 Subject: [PATCH 09/17] Combine non-static and LIBHL_EXPORTS branch --- src/hl.h | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/src/hl.h b/src/hl.h index e73ea75e2..f7f6dbca3 100644 --- a/src/hl.h +++ b/src/hl.h @@ -881,43 +881,29 @@ HL_API void hl_throw_buffer( hl_buffer *b ); #else # define DEFINE_PRIM(t,name,args) DEFINE_PRIM_WITH_NAME(t,name,args,name) #endif -#define _DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = _FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END -#endif // HL_DISABLE_LEGACY_FFI - -#if !defined(HL_NAME) -# define HL_NAME(p) p -# ifdef LIBHL_EXPORTS -# define HL_PRIM EXPORT -# ifndef HL_DISABLE_LEGACY_FFI -# define DEFINE_PRIM_WITH_NAME _DEFINE_PRIM_WITH_NAME -# endif -# else -# define HL_PRIM -# ifndef HL_DISABLE_LEGACY_FFI -# define DEFINE_PRIM_WITH_NAME(t,name,args,realName) -# endif -# endif -#elif defined(LIBHL_STATIC) +#if (defined(HL_NAME) && !defined(LIBHL_STATIC)) || defined(LIBHL_EXPORTS) # ifdef __cplusplus -# define HL_PRIM extern "C" +# define HL_PRIM extern "C" EXPORT # else -# define HL_PRIM -# endif -# ifndef HL_DISABLE_LEGACY_FFI -# define DEFINE_PRIM_WITH_NAME(t,name,args,realName) +# define HL_PRIM EXPORT # endif +# define DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = _FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END #else # ifdef __cplusplus -# define HL_PRIM extern "C" EXPORT +# define HL_PRIM extern "C" # else -# define HL_PRIM EXPORT -# endif -# ifndef HL_DISABLE_LEGACY_FFI -# define DEFINE_PRIM_WITH_NAME _DEFINE_PRIM_WITH_NAME +# define HL_PRIM # endif +# define DEFINE_PRIM_WITH_NAME(t,name,args,realName) #endif +#ifndef HL_NAME +# define HL_NAME(p) p +#endif + +#endif // HL_DISABLE_LEGACY_FFI + typedef struct { hl_type *t; uchar *bytes; From 5c374b246305fbc60bdb214bd7f42ad283c66408 Mon Sep 17 00:00:00 2001 From: Tobiasz Laskowski Date: Tue, 17 Mar 2026 23:36:16 +0000 Subject: [PATCH 10/17] Remove intermediate HL__DEFINE_PRIM_WITH_NAME --- src/hl_ffi.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/hl_ffi.h b/src/hl_ffi.h index 85b305d13..3db3b3cef 100644 --- a/src/hl_ffi.h +++ b/src/hl_ffi.h @@ -27,9 +27,7 @@ #define HL_STRING HL_OBJ(HL_BYTES HL_I32) -#define HL__DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = HL_FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END - -#define HL_DEFINE_PRIM(t,name,args) HL__DEFINE_PRIM_WITH_NAME(t,name,args,name) -#define HL_DEFINE_PRIM_WITH_NAME HL__DEFINE_PRIM_WITH_NAME +#define HL_DEFINE_PRIM(t,name,args) HL_DEFINE_PRIM_WITH_NAME(t,name,args,name) +#define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = HL_FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END #endif // ifndef HLFFI_H From e2c8c700ee8d1cd602ce4a9542bcc8ce26d98eea Mon Sep 17 00:00:00 2001 From: Tobiasz Laskowski Date: Tue, 17 Mar 2026 23:41:22 +0000 Subject: [PATCH 11/17] Define HL_PRIM in hl_ffi.h --- src/hl_ffi.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/hl_ffi.h b/src/hl_ffi.h index 3db3b3cef..86e86cdd1 100644 --- a/src/hl_ffi.h +++ b/src/hl_ffi.h @@ -27,7 +27,13 @@ #define HL_STRING HL_OBJ(HL_BYTES HL_I32) +#ifdef __cplusplus +# define HL_EXTERN_C extern "C" +#else +# define HL_EXTERN_C +#endif #define HL_DEFINE_PRIM(t,name,args) HL_DEFINE_PRIM_WITH_NAME(t,name,args,name) -#define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = HL_FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END +#define HL_PRIM HL_EXTERN_C EXPORT +#define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) HL_EXTERN_C EXPORT void *hlp_##realName( const char **sign ) { *sign = HL_FUN(t,args); return (void*)(&HL_NAME(name)); } #endif // ifndef HLFFI_H From c83b001da722d001fee9dfe890707c93dba5a225 Mon Sep 17 00:00:00 2001 From: Tobiasz Laskowski Date: Tue, 17 Mar 2026 23:48:39 +0000 Subject: [PATCH 12/17] Fix HL_PRIM redefinition between hl.h and hl_ffi.h --- src/hl.h | 28 ++++++++++++++++++++-------- src/hl_ffi.h | 8 ++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/hl.h b/src/hl.h index f7f6dbca3..5205e6f3e 100644 --- a/src/hl.h +++ b/src/hl.h @@ -847,6 +847,14 @@ HL_API void hl_throw_buffer( hl_buffer *b ); #ifndef HL_DISABLE_LEGACY_FFI +#if defined(HLFFI_H) +# if defined(_MSC_VER) +# pragma message("Warning: Please define HL_DISABLE_LEGACY_FFI if using hl.h with hl_ffi.h") +# else +# warning Please define HL_DISABLE_LEGACY_FFI if using hl.h with hl_ffi.h +# endif +#endif + // match GNU C++ mangling #define TYPE_STR "vcsilfdbBDPOATR??X?N?S?g" @@ -883,17 +891,21 @@ HL_API void hl_throw_buffer( hl_buffer *b ); #endif #if (defined(HL_NAME) && !defined(LIBHL_STATIC)) || defined(LIBHL_EXPORTS) -# ifdef __cplusplus -# define HL_PRIM extern "C" EXPORT -# else -# define HL_PRIM EXPORT +# ifndef HL_PRIM +# ifdef __cplusplus +# define HL_PRIM extern "C" EXPORT +# else +# define HL_PRIM EXPORT +# endif # endif # define DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = _FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END #else -# ifdef __cplusplus -# define HL_PRIM extern "C" -# else -# define HL_PRIM +# ifndef HL_PRIM +# ifdef __cplusplus +# define HL_PRIM extern "C" +# else +# define HL_PRIM +# endif # endif # define DEFINE_PRIM_WITH_NAME(t,name,args,realName) #endif diff --git a/src/hl_ffi.h b/src/hl_ffi.h index 86e86cdd1..dbe6eb670 100644 --- a/src/hl_ffi.h +++ b/src/hl_ffi.h @@ -1,6 +1,14 @@ #ifndef HLFFI_H #define HLFFI_H +#if defined(HL_H) && !defined(HL_DISABLE_LEGACY_FFI) +# if defined(_MSC_VER) +# pragma message("Warning: Please define HL_DISABLE_LEGACY_FFI if using hl.h before hl_ffi.h") +# else +# warning Please define HL_DISABLE_LEGACY_FFI if using hl.h before hl_ffi.h +# endif +#endif + // match GNU C++ mangling #define HL_TYPE_STR "vcsilfdbBDPOATR??X?N?S?g" From 08ccca796685607ba310505da5112452d8ea304f Mon Sep 17 00:00:00 2001 From: Tobiasz Laskowski Date: Tue, 17 Mar 2026 23:53:20 +0000 Subject: [PATCH 13/17] Use HL_EXTERN_C macro in hl.h as well --- src/hl.h | 22 ++++++++++------------ src/hl_ffi.h | 10 ++++++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/hl.h b/src/hl.h index 5205e6f3e..eece1cb61 100644 --- a/src/hl.h +++ b/src/hl.h @@ -196,9 +196,15 @@ #endif #ifdef __cplusplus -# define C_FUNCTION_BEGIN extern "C" { +# ifndef HL_EXTERN_C +# define HL_EXTERN_C extern "C" +# endif +# define C_FUNCTION_BEGIN HL_EXTERN_C { # define C_FUNCTION_END }; #else +# ifndef HL_EXTERN_C +# define HL_EXTERN_C +# endif # define C_FUNCTION_BEGIN # define C_FUNCTION_END #endif @@ -892,20 +898,12 @@ HL_API void hl_throw_buffer( hl_buffer *b ); #if (defined(HL_NAME) && !defined(LIBHL_STATIC)) || defined(LIBHL_EXPORTS) # ifndef HL_PRIM -# ifdef __cplusplus -# define HL_PRIM extern "C" EXPORT -# else -# define HL_PRIM EXPORT -# endif +# define HL_PRIM HL_EXTERN_C EXPORT # endif -# define DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = _FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END +# define DEFINE_PRIM_WITH_NAME(t,name,args,realName) HL_EXTERN_C EXPORT void *hlp_##realName( const char **sign ) { *sign = _FUN(t,args); return (void*)(&HL_NAME(name)); } #else # ifndef HL_PRIM -# ifdef __cplusplus -# define HL_PRIM extern "C" -# else -# define HL_PRIM -# endif +# define HL_PRIM HL_EXTERN_C # endif # define DEFINE_PRIM_WITH_NAME(t,name,args,realName) #endif diff --git a/src/hl_ffi.h b/src/hl_ffi.h index dbe6eb670..926a4fac6 100644 --- a/src/hl_ffi.h +++ b/src/hl_ffi.h @@ -35,10 +35,12 @@ #define HL_STRING HL_OBJ(HL_BYTES HL_I32) -#ifdef __cplusplus -# define HL_EXTERN_C extern "C" -#else -# define HL_EXTERN_C +#ifndef HL_EXTERN_C +# ifdef __cplusplus +# define HL_EXTERN_C extern "C" +# else +# define HL_EXTERN_C +# endif #endif #define HL_DEFINE_PRIM(t,name,args) HL_DEFINE_PRIM_WITH_NAME(t,name,args,name) #define HL_PRIM HL_EXTERN_C EXPORT From af9ad2c9996a1ec7f85a2583f481d586b604cf70 Mon Sep 17 00:00:00 2001 From: Tobiasz Laskowski Date: Tue, 17 Mar 2026 23:55:50 +0000 Subject: [PATCH 14/17] Allow using hl_ffi.h without HL_NAME This means the primitive macros will not export anything. --- src/hl_ffi.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/hl_ffi.h b/src/hl_ffi.h index 926a4fac6..ee9c4c0b8 100644 --- a/src/hl_ffi.h +++ b/src/hl_ffi.h @@ -43,7 +43,13 @@ # endif #endif #define HL_DEFINE_PRIM(t,name,args) HL_DEFINE_PRIM_WITH_NAME(t,name,args,name) -#define HL_PRIM HL_EXTERN_C EXPORT -#define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) HL_EXTERN_C EXPORT void *hlp_##realName( const char **sign ) { *sign = HL_FUN(t,args); return (void*)(&HL_NAME(name)); } + +#ifdef HL_NAME +# define HL_PRIM HL_EXTERN_C EXPORT +# define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) HL_EXTERN_C EXPORT void *hlp_##realName( const char **sign ) { *sign = HL_FUN(t,args); return (void*)(&HL_NAME(name)); } +#else +# define HL_PRIM HL_EXTERN_C +# define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) +#endif #endif // ifndef HLFFI_H From d40b9d0717569703899131fe6a7a5c06ca41fcae Mon Sep 17 00:00:00 2001 From: Tobiasz Laskowski Date: Wed, 18 Mar 2026 00:00:44 +0000 Subject: [PATCH 15/17] Undefine C_FUNCTION_BEGIN/END for new ffi Avoids polluting with global macros --- src/hl.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/hl.h b/src/hl.h index eece1cb61..e9e8cf5de 100644 --- a/src/hl.h +++ b/src/hl.h @@ -1032,4 +1032,9 @@ HL_API hl_track_info hl_track; C_FUNCTION_END +#ifndef HL_DISABLE_LEGACY_FFI +# undef C_FUNCTION_BEGIN +# undef C_FUNCTION_END +#endif + #endif From 376adef87d34b2df4db192dc72d32ce8dd440595 Mon Sep 17 00:00:00 2001 From: Tobiasz Laskowski Date: Wed, 18 Mar 2026 01:01:56 +0000 Subject: [PATCH 16/17] Make hl_ffi.h independent of hl.h --- src/hl_ffi.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/hl_ffi.h b/src/hl_ffi.h index ee9c4c0b8..053275f81 100644 --- a/src/hl_ffi.h +++ b/src/hl_ffi.h @@ -42,11 +42,20 @@ # define HL_EXTERN_C # endif #endif + +#if defined(_WIN32) +# define HL_EXPORT __declspec( dllexport ) +#elif defined(__GNUC__) || defined(__clang__) +# define HL_EXPORT __attribute__((visibility("default"))) +#else +# define HL_EXPORT +#endif + #define HL_DEFINE_PRIM(t,name,args) HL_DEFINE_PRIM_WITH_NAME(t,name,args,name) #ifdef HL_NAME -# define HL_PRIM HL_EXTERN_C EXPORT -# define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) HL_EXTERN_C EXPORT void *hlp_##realName( const char **sign ) { *sign = HL_FUN(t,args); return (void*)(&HL_NAME(name)); } +# define HL_PRIM HL_EXTERN_C HL_EXPORT +# define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) HL_EXTERN_C HL_EXPORT void *hlp_##realName( const char **sign ) { *sign = HL_FUN(t,args); return (void*)(&HL_NAME(name)); } #else # define HL_PRIM HL_EXTERN_C # define HL_DEFINE_PRIM_WITH_NAME(t,name,args,realName) From ed778366ff1cc4441c29e2a583d67530fb898f1a Mon Sep 17 00:00:00 2001 From: Tobiasz Laskowski Date: Wed, 18 Mar 2026 01:43:28 +0000 Subject: [PATCH 17/17] Replace EXPORT/IMPORT with HL_EXPORT/HL_IMPORT IMPORT and EXPORT are still defined by default for backwards compatbility, unless HL_DISABLE_LEGACY_FFI is used. --- src/hl.h | 33 ++++++++++++++++++++------------- src/hl_ffi.h | 14 ++++++++------ 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/hl.h b/src/hl.h index e9e8cf5de..a7d633199 100644 --- a/src/hl.h +++ b/src/hl.h @@ -165,16 +165,20 @@ #include #include -#if defined(HL_VCC) || defined(HL_MINGW) -# define EXPORT __declspec( dllexport ) -# define IMPORT __declspec( dllimport ) -#else -#if defined(HL_GCC) || defined(HL_CLANG) -# define EXPORT __attribute__((visibility("default"))) -#else -# define EXPORT +#ifndef HL_EXPORT +# ifdef HL_WIN +# define HL_EXPORT __declspec( dllexport ) +# elif defined(HL_GCC) || defined(HL_CLANG) +# define HL_EXPORT __attribute__((visibility("default"))) +# else +# define HL_EXPORT +# endif #endif -# define IMPORT extern + +#ifdef HL_WIN +# define HL_IMPORT __declspec( dllimport ) +#else +# define HL_IMPORT extern #endif #ifdef HL_64 @@ -219,11 +223,11 @@ typedef unsigned long long uint64; #include #if defined(LIBHL_EXPORTS) -#define HL_API extern EXPORT +#define HL_API extern HL_EXPORT #elif defined(LIBHL_STATIC) #define HL_API extern #else -#define HL_API IMPORT +#define HL_API HL_IMPORT #endif #if defined(HL_VCC) @@ -898,9 +902,9 @@ HL_API void hl_throw_buffer( hl_buffer *b ); #if (defined(HL_NAME) && !defined(LIBHL_STATIC)) || defined(LIBHL_EXPORTS) # ifndef HL_PRIM -# define HL_PRIM HL_EXTERN_C EXPORT +# define HL_PRIM HL_EXTERN_C HL_EXPORT # endif -# define DEFINE_PRIM_WITH_NAME(t,name,args,realName) HL_EXTERN_C EXPORT void *hlp_##realName( const char **sign ) { *sign = _FUN(t,args); return (void*)(&HL_NAME(name)); } +# define DEFINE_PRIM_WITH_NAME(t,name,args,realName) HL_EXTERN_C HL_EXPORT void *hlp_##realName( const char **sign ) { *sign = _FUN(t,args); return (void*)(&HL_NAME(name)); } #else # ifndef HL_PRIM # define HL_PRIM HL_EXTERN_C @@ -912,6 +916,9 @@ HL_API void hl_throw_buffer( hl_buffer *b ); # define HL_NAME(p) p #endif +#define EXPORT HL_EXPORT +#define IMPORT HL_IMPORT + #endif // HL_DISABLE_LEGACY_FFI typedef struct { diff --git a/src/hl_ffi.h b/src/hl_ffi.h index 053275f81..4af06187b 100644 --- a/src/hl_ffi.h +++ b/src/hl_ffi.h @@ -43,12 +43,14 @@ # endif #endif -#if defined(_WIN32) -# define HL_EXPORT __declspec( dllexport ) -#elif defined(__GNUC__) || defined(__clang__) -# define HL_EXPORT __attribute__((visibility("default"))) -#else -# define HL_EXPORT +#ifndef HL_EXPORT +# if defined(_WIN32) +# define HL_EXPORT __declspec( dllexport ) +# elif defined(__GNUC__) || defined(__clang__) +# define HL_EXPORT __attribute__((visibility("default"))) +# else +# define HL_EXPORT +# endif #endif #define HL_DEFINE_PRIM(t,name,args) HL_DEFINE_PRIM_WITH_NAME(t,name,args,name)