From f3e92ae57ad30dbff5c5bb1cd8f3e24c13cd0160 Mon Sep 17 00:00:00 2001 From: setoutsoft Date: Sun, 9 Jan 2022 23:16:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0UAPI=E5=AE=8F=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=AF=BC=E5=87=BA=E5=85=B6=E5=AE=83=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E7=BA=A6=E5=AE=9A=E5=A6=82=5F=5Fstdcall?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua_tinker/lua_tinker.h | 271 ++++++++++++++++++++++++++++++++++------ 1 file changed, 230 insertions(+), 41 deletions(-) diff --git a/lua_tinker/lua_tinker.h b/lua_tinker/lua_tinker.h index dd2b9ef..45a9d2c 100644 --- a/lua_tinker/lua_tinker.h +++ b/lua_tinker/lua_tinker.h @@ -16,6 +16,9 @@ #include #include +#ifndef UAPI +#define UAPI __stdcall +#endif namespace lua_tinker { @@ -371,36 +374,42 @@ namespace lua_tinker struct functor { static int invoke ( lua_State *L ) { push ( L, upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ), read ( L, 3 ), read ( L, 4 ), read ( L, 5 ) ) ); return 1; } + static int invokeU ( lua_State *L ) { push ( L, upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ), read ( L, 3 ), read ( L, 4 ), read ( L, 5 ) ) ); return 1; } }; template struct functor < RVal, T1, T2, T3, T4 > { static int invoke ( lua_State *L ) { push ( L, upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ), read ( L, 3 ), read ( L, 4 ) ) ); return 1; } + static int invokeU ( lua_State *L ) { push ( L, upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ), read ( L, 3 ), read ( L, 4 ) ) ); return 1; } }; template struct functor < RVal, T1, T2, T3 > { static int invoke ( lua_State *L ) { push ( L, upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ), read ( L, 3 ) ) ); return 1; } + static int invokeU ( lua_State *L ) { push ( L, upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ), read ( L, 3 ) ) ); return 1; } }; template struct functor < RVal, T1, T2 > { static int invoke ( lua_State *L ) { push ( L, upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ) ) ); return 1; } + static int invokeU ( lua_State *L ) { push ( L, upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ) ) ); return 1; } }; template struct functor < RVal, T1 > { static int invoke ( lua_State *L ) { push ( L, upvalue_ ( L )( read ( L, 1 ) ) ); return 1; } + static int invokeU ( lua_State *L ) { push ( L, upvalue_ ( L )( read ( L, 1 ) ) ); return 1; } }; template struct functor < RVal > { static int invoke ( lua_State *L ) { push ( L, upvalue_ ( L )( ) ); return 1; } + static int invokeU ( lua_State *L ) { push ( L, upvalue_ ( L )( ) ); return 1; } }; // functor (without return value) @@ -411,36 +420,42 @@ namespace lua_tinker struct functor < void, T1, T2, T3, T4, T5 > { static int invoke ( lua_State *L ) { upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ), read ( L, 3 ), read ( L, 4 ), read ( L, 5 ) ); return 0; } + static int invokeU ( lua_State *L ) { upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ), read ( L, 3 ), read ( L, 4 ), read ( L, 5 ) ); return 0; } }; template struct functor < void, T1, T2, T3, T4 > { static int invoke ( lua_State *L ) { upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ), read ( L, 3 ), read ( L, 4 ) ); return 0; } + static int invokeU ( lua_State *L ) { upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ), read ( L, 3 ), read ( L, 4 ) ); return 0; } }; template struct functor < void, T1, T2, T3 > { static int invoke ( lua_State *L ) { upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ), read ( L, 3 ) ); return 0; } + static int invokeU ( lua_State *L ) { upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ), read ( L, 3 ) ); return 0; } }; template struct functor < void, T1, T2 > { static int invoke ( lua_State *L ) { upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ) ); return 0; } + static int invokeU ( lua_State *L ) { upvalue_ ( L )( read ( L, 1 ), read ( L, 2 ) ); return 0; } }; template struct functor < void, T1 > { static int invoke ( lua_State *L ) { upvalue_ ( L )( read ( L, 1 ) ); return 0; } + static int invokeU ( lua_State *L ) { upvalue_ ( L )( read ( L, 1 ) ); return 0; } }; template<> struct functor < void > { static int invoke ( lua_State *L ) { upvalue_ ( L )( ); return 0; } + static int invokeU ( lua_State *L ) { upvalue_ ( L )( ); return 0; } }; // functor (non-managed) @@ -448,12 +463,14 @@ namespace lua_tinker struct functor < int, lua_State*, T1 > { static int invoke ( lua_State *L ) { return upvalue_ ( L )( L, read ( L, 1 ) ); } + static int invokeU ( lua_State *L ) { return upvalue_ ( L )( L, read ( L, 1 ) ); } }; template<> struct functor < int, lua_State* > { static int invoke ( lua_State *L ) { return upvalue_ ( L )( L ); } + static int invokeU ( lua_State *L ) { return upvalue_ ( L )( L ); } }; // push_functor @@ -501,6 +518,49 @@ namespace lua_tinker lua_pushcclosure ( L, functor::invoke, 1 ); } +#ifndef _WIN64 + template + void push_functor ( lua_State *L, RVal (UAPI *func )( ) ) + { + ( void ) func; + lua_pushcclosure ( L, functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI *func )( T1 ) ) + { + ( void ) func; + lua_pushcclosure ( L, functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI *func )( T1, T2 ) ) + { + ( void ) func; + lua_pushcclosure ( L, functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI *func )( T1, T2, T3 ) ) + { + ( void ) func; + lua_pushcclosure ( L, functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI *func )( T1, T2, T3, T4 ) ) + { + ( void ) func; + lua_pushcclosure ( L, functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI *func )( T1, T2, T3, T4, T5 ) ) + { + ( void ) func; + lua_pushcclosure ( L, functor::invokeU, 1 ); + } +#endif//_WIN64 // member variable struct var_base { @@ -523,36 +583,42 @@ namespace lua_tinker struct mem_functor { static int invoke ( lua_State *L ) { push ( L, ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ), read ( L, 4 ), read ( L, 5 ), read ( L, 6 ) ) );; return 1; } + static int invokeU ( lua_State *L ) { push ( L, ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ), read ( L, 4 ), read ( L, 5 ), read ( L, 6 ) ) );; return 1; } }; template struct mem_functor < RVal, T, T1, T2, T3, T4 > { static int invoke ( lua_State *L ) { push ( L, ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ), read ( L, 4 ), read ( L, 5 ) ) ); return 1; } + static int invokeU ( lua_State *L ) { push ( L, ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ), read ( L, 4 ), read ( L, 5 ) ) ); return 1; } }; template struct mem_functor < RVal, T, T1, T2, T3 > { static int invoke ( lua_State *L ) { push ( L, ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ), read ( L, 4 ) ) ); return 1; } + static int invokeU ( lua_State *L ) { push ( L, ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ), read ( L, 4 ) ) ); return 1; } }; template struct mem_functor < RVal, T, T1, T2 > { static int invoke ( lua_State *L ) { push ( L, ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ) ) ); return 1; } + static int invokeU ( lua_State *L ) { push ( L, ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ) ) ); return 1; } }; template struct mem_functor < RVal, T, T1 > { static int invoke ( lua_State *L ) { push ( L, ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ) ) ); return 1; } + static int invokeU ( lua_State *L ) { push ( L, ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ) ) ); return 1; } }; template struct mem_functor < RVal, T > { static int invoke ( lua_State *L ) { push ( L, ( read ( L, 1 )->*upvalue_ ( L ) )( ) ); return 1; } + static int invokeU ( lua_State *L ) { push ( L, ( read ( L, 1 )->*upvalue_ ( L ) )( ) ); return 1; } }; // class member functor (without return value) @@ -560,36 +626,42 @@ namespace lua_tinker struct mem_functor < void, T, T1, T2, T3, T4, T5 > { static int invoke ( lua_State *L ) { ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ), read ( L, 4 ), read ( L, 5 ), read ( L, 6 ) ); return 0; } + static int invokeU ( lua_State *L ) { ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ), read ( L, 4 ), read ( L, 5 ), read ( L, 6 ) ); return 0; } }; template struct mem_functor < void, T, T1, T2, T3, T4 > { static int invoke ( lua_State *L ) { ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ), read ( L, 4 ), read ( L, 5 ) ); return 0; } + static int invokeU ( lua_State *L ) { ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ), read ( L, 4 ), read ( L, 5 ) ); return 0; } }; template struct mem_functor < void, T, T1, T2, T3 > { static int invoke ( lua_State *L ) { ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ), read ( L, 4 ) ); return 0; } + static int invokeU ( lua_State *L ) { ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ), read ( L, 4 ) ); return 0; } }; template struct mem_functor < void, T, T1, T2 > { static int invoke ( lua_State *L ) { ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ) ); return 0; } + static int invokeU ( lua_State *L ) { ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ), read ( L, 3 ) ); return 0; } }; template struct mem_functor < void, T, T1 > { static int invoke ( lua_State *L ) { ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ) ); return 0; } + static int invokeU ( lua_State *L ) { ( read ( L, 1 )->*upvalue_ ( L ) )( read ( L, 2 ) ); return 0; } }; template struct mem_functor < void, T > { static int invoke ( lua_State *L ) { ( read ( L, 1 )->*upvalue_ ( L ) )( ); return 0; } + static int invokeU ( lua_State *L ) { ( read ( L, 1 )->*upvalue_ ( L ) )( ); return 0; } }; // class member functor (non-managed) @@ -597,12 +669,14 @@ namespace lua_tinker struct mem_functor < int, T, lua_State*, T1 > { static int invoke ( lua_State *L ) { return ( read ( L, 1 )->*upvalue_ ( L ) )( L, read ( L, 2 ) ); } + static int invokeU ( lua_State *L ) { return ( read ( L, 1 )->*upvalue_ ( L ) )( L, read ( L, 2 ) ); } }; template struct mem_functor < int, T, lua_State* > { static int invoke ( lua_State *L ) { return ( read ( L, 1 )->*upvalue_ ( L ) )( L ); } + static int invokeU ( lua_State *L ) { return ( read ( L, 1 )->*upvalue_ ( L ) )( L ); } }; // push_functor @@ -690,6 +764,92 @@ namespace lua_tinker lua_pushcclosure ( L, mem_functor::invoke, 1 ); } +#ifndef _WIN64 + // push_functor + template + void push_functor ( lua_State *L, RVal (UAPI T::*func )( ) ) + { + ( void ) func; + lua_pushcclosure ( L, mem_functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI T::*func )( ) const ) + { + ( void ) func; + lua_pushcclosure ( L, mem_functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI T::*func )( T1 ) ) + { + ( void ) func; + lua_pushcclosure ( L, mem_functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI T::*func )( T1 ) const ) + { + ( void ) func; + lua_pushcclosure ( L, mem_functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI T::*func )( T1, T2 ) ) + { + ( void ) func; + lua_pushcclosure ( L, mem_functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI T::*func )( T1, T2 ) const ) + { + ( void ) func; + lua_pushcclosure ( L, mem_functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI T::*func )( T1, T2, T3 ) ) + { + ( void ) func; + lua_pushcclosure ( L, mem_functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI T::*func )( T1, T2, T3 ) const ) + { + ( void ) func; + lua_pushcclosure ( L, mem_functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI T::*func )( T1, T2, T3, T4 ) ) + { + ( void ) func; + lua_pushcclosure ( L, mem_functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI T::*func )( T1, T2, T3, T4 ) const ) + { + ( void ) func; + lua_pushcclosure ( L, mem_functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI T::*func )( T1, T2, T3, T4, T5 ) ) + { + ( void ) func; + lua_pushcclosure ( L, mem_functor::invokeU, 1 ); + } + + template + void push_functor ( lua_State *L, RVal (UAPI T::*func )( T1, T2, T3, T4, T5 ) const ) + { + ( void ) func; + lua_pushcclosure ( L, mem_functor::invokeU, 1 ); + } +#endif//_WIN64 // constructor template int constructor ( lua_State *L ) @@ -801,21 +961,6 @@ namespace lua_tinker } - - - template - int pcall_push ( lua_State*L, T value ) - { - push ( L, value ); - return 1; - } - template - int pcall_push ( lua_State *L, TFirst first, TArgs ... args ) - { - push ( L, first ); - return 1 + pcall_push ( L, args... ); - } - template RVal call ( lua_State* L, const char* name ) { @@ -835,36 +980,80 @@ namespace lua_tinker lua_remove ( L, errfunc ); return pop ( L ); } - template - RVal call ( lua_State *L, const char * name, TArgs ... args ) - { - lua_pushcclosure ( L, on_error, 0 ); - int errfunc = lua_gettop ( L ); - - lua_getglobal ( L, name ); - if ( lua_isfunction ( L, -1 ) ) - { - int count = sizeof ...(args); - if ( count > 0 ) - { - - pcall_push ( L, args... ); - } - lua_pcall ( L, count, 1, errfunc ); - } - else - { - print_error ( L, "lua_tinker::call() attempt to call global `%s' (not a function)", name ); - } - - lua_remove ( L, errfunc ); - return pop ( L ); + template + RVal call(lua_State* L, const char* name, T1 arg) + { + lua_pushcclosure(L, on_error, 0); + int errfunc = lua_gettop(L); + lua_getglobal(L, name); + if(lua_isfunction(L,-1)) + { + push(L, arg); + if(lua_pcall(L, 1, 1, errfunc) != 0) + { + lua_pop(L, 1); + } + } + else + { + print_error(L, "lua_tinker::call() attempt to call global `%s' (not a function)", name); + } + + lua_remove(L, -2); + return pop(L); + } + + template + RVal call(lua_State* L, const char* name, T1 arg1, T2 arg2) + { + lua_pushcclosure(L, on_error, 0); + int errfunc = lua_gettop(L); + lua_getglobal(L, name); + if(lua_isfunction(L,-1)) + { + push(L, arg1); + push(L, arg2); + if(lua_pcall(L, 2, 1, errfunc) != 0) + { + lua_pop(L, 1); + } + } + else + { + print_error(L, "lua_tinker::call() attempt to call global `%s' (not a function)", name); + } + + lua_remove(L, -2); + return pop(L); + } + + template + RVal call(lua_State* L, const char* name, T1 arg1, T2 arg2, T3 arg3) + { + lua_pushcclosure(L, on_error, 0); + int errfunc = lua_gettop(L); + lua_getglobal(L, name); + if(lua_isfunction(L,-1)) + { + push(L, arg1); + push(L, arg2); + push(L, arg3); + if(lua_pcall(L, 3, 1, errfunc) != 0) + { + lua_pop(L, 1); + } + } + else + { + print_error(L, "lua_tinker::call() attempt to call global `%s' (not a function)", name); + } + + lua_remove(L, -2); + return pop(L); } - - // class helper int meta_get ( lua_State *L ); int meta_set ( lua_State *L );