├── README.md ├── example ├── Makefile ├── fflua_win │ ├── fflua_win.sln │ ├── fflua_win.v12.suo │ ├── fflua_win.vcxproj │ └── winlua-5.3.4 │ │ ├── include │ │ ├── lauxlib.h │ │ ├── lua.h │ │ ├── lua.hpp │ │ ├── luaconf.h │ │ └── lualib.h │ │ └── lua53.lib ├── main.cpp └── test.lua └── lua ├── fflua.h ├── fflua_register.h └── fflua_type.h /README.md: -------------------------------------------------------------------------------- 1 | # FFLua 2 | FFLua is a C++ lib to wrap operations of embeding lua and extending lua. 3 | FFLUA 是嵌入LUA,注册C++相关的对象到lua的封装库, FFLUA 拥有如下特性: 4 | ### Features 5 | * Onlye three header files 6 | * lightweight wrap 7 | * Efficient 8 | * clean code 9 | 10 | ### FFLUA make it easy load lua file [载入函数、设置路径更简单] 11 | ``` c++ 12 | fflua_t fflua; 13 | try 14 | { 15 | //! 注册C++ 对象到lua中 16 | fflua.reg(lua_reg); 17 | 18 | //! 载入lua文件 19 | fflua.add_package_path("./"); 20 | fflua.load_file("test.lua"); 21 | ``` 22 | 23 | ### FFLUA make it easy to operate global varialbe of lua [获取全局变量、设置全局变量更容易 ] 24 | ``` c++ 25 | int var = 0; 26 | assert(0 == fflua.get_global_variable("test_var", var)); 27 | //! 设置全局变量 28 | assert(0 == fflua.set_global_variable("test_var", ++var)); 29 | ``` 30 | ### FFlua make it easy to run lua string [执行lua语句更简单 ] 31 | ``` c++ 32 | fflua.run_string("print("exe run_string!!")"); 33 | ``` 34 | ### FFlua make it easy to invoke lua function in c++ [调用lua函数更容易 ] 35 | ``` c++ 36 | //! 调用lua函数, 基本类型作为参数 37 | int32_t arg1 = 1; 38 | float arg2 = 2; 39 | double arg3 = 3; 40 | string arg4 = "4"; 41 | fflua.call("test_func", arg1, arg2, arg3, arg4); 42 | ``` 43 | ### FFlua make it easy to convert between lua talbe and C++ STL container. [lua table 和 C++ STL 转换更容易 ] 44 | ``` 45 | //! 调用lua函数,stl类型作为参数, 自动转换为lua talbe 46 | vector vec; vec.push_back(100); 47 | list lt; lt.push_back(99.99); 48 | set st; st.insert("OhNIce"); 49 | map mp; mp["key"] = 200; 50 | fflua.call("test_stl", vec, lt, st, mp); 51 | ``` 52 | ### FFlua make it easy to convert value returned by lua funciton to C++ STL container. [lua返回值转换 C++ STL 更简单 ] 53 | ``` c++ 54 | //! 调用lua 函数返回 talbe,自动转换为stl结构 55 | vec = fflua.call >("test_return_stl_vector"); 56 | lt = fflua.call >("test_return_stl_list"); 57 | st = fflua.call >("test_return_stl_set"); 58 | mp = fflua.call >("test_return_stl_map"); 59 | ``` 60 | ### FFlua make it easy to use c++ object pointer as argument of lua function directly. [C++ 最为参数更容易:] 61 | ``` c++ 62 | //! 调用lua函数,c++ 对象作为参数, foo_t 必须被注册过 63 | foo_t* foo_ptr = new foo_t(456); 64 | fflua.call("test_object", foo_ptr); 65 | ``` 66 | ### FFlua make it easy to convert lua object returned by lua function to C++ object directly. [C++ 对象作为返回值更容易 ] 67 | ``` c++ 68 | //! 调用lua函数,c++ 对象作为返回值, foo_t 必须被注册过 69 | assert(foo_ptr == fflua.call("test_ret_object", foo_ptr)); 70 | //! 调用lua函数,c++ 对象作为返回值, 自动转换为基类 71 | base_t* base_ptr = fflua.call("test_ret_base_object", foo_ptr); 72 | assert(base_ptr == foo_ptr); 73 | ``` 74 | ### FFlua make it easy to register C++ class to lua. [注册C++ 对象更容易 ] 75 | ``` c++ 76 | //! 注册C++ 对象到lua中 77 | fflua.reg(lua_reg); 78 | 79 | ``` 80 | ### Some Example code 81 | ``` c++ 82 | class base_t 83 | { 84 | public: 85 | base_t():v(789){} 86 | void dump() 87 | { 88 | printf("in %s a:%dn", __FUNCTION__, v); 89 | } 90 | int v; 91 | }; 92 | 93 | 94 | class foo_t: public base_t 95 | { 96 | public: 97 | foo_t(int b):a(b) 98 | { 99 | printf("in %s b:%d this=%pn", __FUNCTION__, b, this); 100 | } 101 | ~foo_t() 102 | { 103 | printf("in %sn", __FUNCTION__); 104 | } 105 | void print(int64_t a, base_t* p) const 106 | { 107 | printf("in foo_t::print a:%ld p:%pn", (long)a, p); 108 | } 109 | 110 | static void dumy() 111 | { 112 | printf("in %sn", __FUNCTION__); 113 | } 114 | int a; 115 | }; 116 | 117 | //! lua talbe 可以自动转换为stl 对象 118 | void dumy(map ret, vector a, list b, set c) 119 | { 120 | printf("in %s begin ------------n", __FUNCTION__); 121 | for (map::iterator it = ret.begin(); it != ret.end(); ++it) 122 | { 123 | printf("map:%s, val:%s:n", it->first.c_str(), it->second.c_str()); 124 | } 125 | printf("in %s end ------------n", __FUNCTION__); 126 | } 127 | 128 | static void lua_reg(lua_State* ls) 129 | { 130 | //! 注册基类函数, ctor() 为构造函数的类型 131 | fflua_register_t(ls, "base_t") //! 注册构造函数 132 | .def(&base_t::dump, "dump") //! 注册基类的函数 133 | .def(&base_t::v, "v"); //! 注册基类的属性 134 | 135 | //! 注册子类,ctor(int) 为构造函数, foo_t为类型名称, base_t为继承的基类名称 136 | fflua_register_t(ls, "foo_t", "base_t") 137 | .def(&foo_t::print, "print") //! 子类的函数 138 | .def(&foo_t::a, "a"); //! 子类的字段 139 | 140 | fflua_register_t<>(ls) 141 | .def(&dumy, "dumy"); //! 注册静态函数 142 | /*支持注册基类 143 | fflua_register_t(ls, "base_t"); 144 | */ 145 | } 146 | ``` 147 | -------------------------------------------------------------------------------- /example/Makefile: -------------------------------------------------------------------------------- 1 | #交叉编译器路径 2 | CROSS= 3 | CP=/bin/cp 4 | RM=-/bin/rm -rf 5 | LN=/bin/ln -s 6 | CFLAGS=-g -Wall 7 | LDFLAGS= -llua -ldl 8 | #链接库名 9 | LIB_NAME= 10 | #链接库版本 11 | LIB_VER=1.0.0 12 | #平台 13 | ARCH= 14 | # 二进制目标 15 | BIN=app_lua 16 | 17 | #源文件目录 18 | SrcDir= . 19 | #头文件目录 20 | IncDir= ../ 21 | #连接库目录 22 | LibDir= /usr/local/lib 23 | SRCS=$(foreach dir,$(SrcDir),$(wildcard $(dir)/*.cpp)) 24 | #INCS=$(foreach dir,$(IncDir),$(wildcard $(dir)/*.h)) 25 | INCS=$(foreach dir,$(IncDir),$(addprefix -I,$(dir))) 26 | LINKS=$(foreach dir,$(LibDir),$(addprefix -L,$(dir))) 27 | CFLAGS := $(CFLAGS) $(INCS) 28 | LDFLAGS:= $(LINKS) $(LDFLAGS) 29 | CC=g++ 30 | ARCH=PC 31 | OBJS = $(SRCS:%.cpp=%.o) 32 | .PHONY:all clean 33 | 34 | all:$(BIN) 35 | $(BIN):$(OBJS) 36 | $(CC) -o $(BIN) $(OBJS) $(LDFLAGS) 37 | @echo -e " OK!\tComplie $@ " 38 | # @$(LN) $(shell pwd)/$(LIB_NAME).$(LIB_VER) /lib/$(LIB_NAME) 39 | 40 | %.o:%.cpp 41 | @echo -e "[$(ARCH)] \t\tCompileing $@..." 42 | @$(CC) $(CFLAGS) -c $< -o $@ 43 | .PHONY: clean 44 | clean: 45 | @echo -e "[$(ARCH)] \tCleaning files..." 46 | @$(RM) $(OBJS) $(BIN) 47 | -------------------------------------------------------------------------------- /example/fflua_win/fflua_win.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fflua_win", "fflua_win.vcxproj", "{127DA513-22BF-48DD-A56C-DFE4FFC151AF}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {127DA513-22BF-48DD-A56C-DFE4FFC151AF}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {127DA513-22BF-48DD-A56C-DFE4FFC151AF}.Debug|Win32.Build.0 = Debug|Win32 16 | {127DA513-22BF-48DD-A56C-DFE4FFC151AF}.Release|Win32.ActiveCfg = Release|Win32 17 | {127DA513-22BF-48DD-A56C-DFE4FFC151AF}.Release|Win32.Build.0 = Release|Win32 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /example/fflua_win/fflua_win.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fanchy/fflua/de6e5a50458c56001a3871701673c05197bdfe52/example/fflua_win/fflua_win.v12.suo -------------------------------------------------------------------------------- /example/fflua_win/fflua_win.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {127DA513-22BF-48DD-A56C-DFE4FFC151AF} 15 | fflua_win 16 | Win32Proj 17 | 18 | 19 | 20 | Application 21 | v120 22 | Unicode 23 | true 24 | 25 | 26 | Application 27 | v120 28 | Unicode 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | <_ProjectFileVersion>12.0.21005.1 42 | 43 | 44 | $(SolutionDir)$(Configuration)\ 45 | $(Configuration)\ 46 | true 47 | 48 | 49 | $(SolutionDir)$(Configuration)\ 50 | $(Configuration)\ 51 | false 52 | 53 | 54 | 55 | Disabled 56 | ../../;./winlua-5.3.4/include;%(AdditionalIncludeDirectories) 57 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 58 | true 59 | EnableFastChecks 60 | MultiThreadedDebugDLL 61 | 62 | Level3 63 | EditAndContinue 64 | 65 | 66 | lua53.lib;%(AdditionalDependencies) 67 | ./winlua-5.3.4;%(AdditionalLibraryDirectories) 68 | true 69 | Console 70 | MachineX86 71 | 72 | 73 | 74 | 75 | MaxSpeed 76 | true 77 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 78 | MultiThreadedDLL 79 | true 80 | 81 | Level3 82 | ProgramDatabase 83 | 84 | 85 | true 86 | Console 87 | true 88 | true 89 | MachineX86 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /example/fflua_win/winlua-5.3.4/include/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h,v 1.131 2016/12/06 14:54:31 roberto Exp $ 3 | ** Auxiliary functions for building Lua libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lauxlib_h 9 | #define lauxlib_h 10 | 11 | 12 | #include 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | 18 | 19 | /* extra error code for 'luaL_loadfilex' */ 20 | #define LUA_ERRFILE (LUA_ERRERR+1) 21 | 22 | 23 | /* key, in the registry, for table of loaded modules */ 24 | #define LUA_LOADED_TABLE "_LOADED" 25 | 26 | 27 | /* key, in the registry, for table of preloaded loaders */ 28 | #define LUA_PRELOAD_TABLE "_PRELOAD" 29 | 30 | 31 | typedef struct luaL_Reg { 32 | const char *name; 33 | lua_CFunction func; 34 | } luaL_Reg; 35 | 36 | 37 | #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) 38 | 39 | LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); 40 | #define luaL_checkversion(L) \ 41 | luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) 42 | 43 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 44 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 45 | LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 46 | LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); 47 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, 48 | size_t *l); 49 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, 50 | const char *def, size_t *l); 51 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); 52 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); 53 | 54 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); 55 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, 56 | lua_Integer def); 57 | 58 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 59 | LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); 60 | LUALIB_API void (luaL_checkany) (lua_State *L, int arg); 61 | 62 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 63 | LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 64 | LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 65 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 66 | 67 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); 68 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 69 | 70 | LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, 71 | const char *const lst[]); 72 | 73 | LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 74 | LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 75 | 76 | /* predefined references */ 77 | #define LUA_NOREF (-2) 78 | #define LUA_REFNIL (-1) 79 | 80 | LUALIB_API int (luaL_ref) (lua_State *L, int t); 81 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 82 | 83 | LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 84 | const char *mode); 85 | 86 | #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 87 | 88 | LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 89 | const char *name, const char *mode); 90 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 91 | 92 | LUALIB_API lua_State *(luaL_newstate) (void); 93 | 94 | LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); 95 | 96 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 97 | const char *r); 98 | 99 | LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 100 | 101 | LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 102 | 103 | LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 104 | const char *msg, int level); 105 | 106 | LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 107 | lua_CFunction openf, int glb); 108 | 109 | /* 110 | ** =============================================================== 111 | ** some useful macros 112 | ** =============================================================== 113 | */ 114 | 115 | 116 | #define luaL_newlibtable(L,l) \ 117 | lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 118 | 119 | #define luaL_newlib(L,l) \ 120 | (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 121 | 122 | #define luaL_argcheck(L, cond,arg,extramsg) \ 123 | ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) 124 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 125 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 126 | 127 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 128 | 129 | #define luaL_dofile(L, fn) \ 130 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 131 | 132 | #define luaL_dostring(L, s) \ 133 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 134 | 135 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 136 | 137 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 138 | 139 | #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 140 | 141 | 142 | /* 143 | ** {====================================================== 144 | ** Generic Buffer manipulation 145 | ** ======================================================= 146 | */ 147 | 148 | typedef struct luaL_Buffer { 149 | char *b; /* buffer address */ 150 | size_t size; /* buffer size */ 151 | size_t n; /* number of characters in buffer */ 152 | lua_State *L; 153 | char initb[LUAL_BUFFERSIZE]; /* initial buffer */ 154 | } luaL_Buffer; 155 | 156 | 157 | #define luaL_addchar(B,c) \ 158 | ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 159 | ((B)->b[(B)->n++] = (c))) 160 | 161 | #define luaL_addsize(B,s) ((B)->n += (s)) 162 | 163 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 164 | LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 165 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 166 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 167 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 168 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 169 | LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 170 | LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 171 | 172 | #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 173 | 174 | /* }====================================================== */ 175 | 176 | 177 | 178 | /* 179 | ** {====================================================== 180 | ** File handles for IO library 181 | ** ======================================================= 182 | */ 183 | 184 | /* 185 | ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 186 | ** initial structure 'luaL_Stream' (it may contain other fields 187 | ** after that initial structure). 188 | */ 189 | 190 | #define LUA_FILEHANDLE "FILE*" 191 | 192 | 193 | typedef struct luaL_Stream { 194 | FILE *f; /* stream (NULL for incompletely created streams) */ 195 | lua_CFunction closef; /* to close stream (NULL for closed streams) */ 196 | } luaL_Stream; 197 | 198 | /* }====================================================== */ 199 | 200 | 201 | 202 | /* compatibility with old module system */ 203 | #if defined(LUA_COMPAT_MODULE) 204 | 205 | LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, 206 | int sizehint); 207 | LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, 208 | const luaL_Reg *l, int nup); 209 | 210 | #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) 211 | 212 | #endif 213 | 214 | 215 | /* 216 | ** {================================================================== 217 | ** "Abstraction Layer" for basic report of messages and errors 218 | ** =================================================================== 219 | */ 220 | 221 | /* print a string */ 222 | #if !defined(lua_writestring) 223 | #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) 224 | #endif 225 | 226 | /* print a newline and flush the output */ 227 | #if !defined(lua_writeline) 228 | #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) 229 | #endif 230 | 231 | /* print an error message */ 232 | #if !defined(lua_writestringerror) 233 | #define lua_writestringerror(s,p) \ 234 | (fprintf(stderr, (s), (p)), fflush(stderr)) 235 | #endif 236 | 237 | /* }================================================================== */ 238 | 239 | 240 | /* 241 | ** {============================================================ 242 | ** Compatibility with deprecated conversions 243 | ** ============================================================= 244 | */ 245 | #if defined(LUA_COMPAT_APIINTCASTS) 246 | 247 | #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) 248 | #define luaL_optunsigned(L,a,d) \ 249 | ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) 250 | 251 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 252 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 253 | 254 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 255 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 256 | 257 | #endif 258 | /* }============================================================ */ 259 | 260 | 261 | 262 | #endif 263 | 264 | 265 | -------------------------------------------------------------------------------- /example/fflua_win/winlua-5.3.4/include/lua.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lua.h,v 1.332 2016/12/22 15:51:20 roberto Exp $ 3 | ** Lua - A Scripting Language 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 5 | ** See Copyright Notice at the end of this file 6 | */ 7 | 8 | 9 | #ifndef lua_h 10 | #define lua_h 11 | 12 | #include 13 | #include 14 | 15 | 16 | #include "luaconf.h" 17 | 18 | 19 | #define LUA_VERSION_MAJOR "5" 20 | #define LUA_VERSION_MINOR "3" 21 | #define LUA_VERSION_NUM 503 22 | #define LUA_VERSION_RELEASE "4" 23 | 24 | #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR 25 | #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE 26 | #define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2017 Lua.org, PUC-Rio" 27 | #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" 28 | 29 | 30 | /* mark for precompiled code ('Lua') */ 31 | #define LUA_SIGNATURE "\x1bLua" 32 | 33 | /* option for multiple returns in 'lua_pcall' and 'lua_call' */ 34 | #define LUA_MULTRET (-1) 35 | 36 | 37 | /* 38 | ** Pseudo-indices 39 | ** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty 40 | ** space after that to help overflow detection) 41 | */ 42 | #define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) 43 | #define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) 44 | 45 | 46 | /* thread status */ 47 | #define LUA_OK 0 48 | #define LUA_YIELD 1 49 | #define LUA_ERRRUN 2 50 | #define LUA_ERRSYNTAX 3 51 | #define LUA_ERRMEM 4 52 | #define LUA_ERRGCMM 5 53 | #define LUA_ERRERR 6 54 | 55 | 56 | typedef struct lua_State lua_State; 57 | 58 | 59 | /* 60 | ** basic types 61 | */ 62 | #define LUA_TNONE (-1) 63 | 64 | #define LUA_TNIL 0 65 | #define LUA_TBOOLEAN 1 66 | #define LUA_TLIGHTUSERDATA 2 67 | #define LUA_TNUMBER 3 68 | #define LUA_TSTRING 4 69 | #define LUA_TTABLE 5 70 | #define LUA_TFUNCTION 6 71 | #define LUA_TUSERDATA 7 72 | #define LUA_TTHREAD 8 73 | 74 | #define LUA_NUMTAGS 9 75 | 76 | 77 | 78 | /* minimum Lua stack available to a C function */ 79 | #define LUA_MINSTACK 20 80 | 81 | 82 | /* predefined values in the registry */ 83 | #define LUA_RIDX_MAINTHREAD 1 84 | #define LUA_RIDX_GLOBALS 2 85 | #define LUA_RIDX_LAST LUA_RIDX_GLOBALS 86 | 87 | 88 | /* type of numbers in Lua */ 89 | typedef LUA_NUMBER lua_Number; 90 | 91 | 92 | /* type for integer functions */ 93 | typedef LUA_INTEGER lua_Integer; 94 | 95 | /* unsigned integer type */ 96 | typedef LUA_UNSIGNED lua_Unsigned; 97 | 98 | /* type for continuation-function contexts */ 99 | typedef LUA_KCONTEXT lua_KContext; 100 | 101 | 102 | /* 103 | ** Type for C functions registered with Lua 104 | */ 105 | typedef int (*lua_CFunction) (lua_State *L); 106 | 107 | /* 108 | ** Type for continuation functions 109 | */ 110 | typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); 111 | 112 | 113 | /* 114 | ** Type for functions that read/write blocks when loading/dumping Lua chunks 115 | */ 116 | typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); 117 | 118 | typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); 119 | 120 | 121 | /* 122 | ** Type for memory-allocation functions 123 | */ 124 | typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); 125 | 126 | 127 | 128 | /* 129 | ** generic extra include file 130 | */ 131 | #if defined(LUA_USER_H) 132 | #include LUA_USER_H 133 | #endif 134 | 135 | 136 | /* 137 | ** RCS ident string 138 | */ 139 | extern const char lua_ident[]; 140 | 141 | 142 | /* 143 | ** state manipulation 144 | */ 145 | LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); 146 | LUA_API void (lua_close) (lua_State *L); 147 | LUA_API lua_State *(lua_newthread) (lua_State *L); 148 | 149 | LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); 150 | 151 | 152 | LUA_API const lua_Number *(lua_version) (lua_State *L); 153 | 154 | 155 | /* 156 | ** basic stack manipulation 157 | */ 158 | LUA_API int (lua_absindex) (lua_State *L, int idx); 159 | LUA_API int (lua_gettop) (lua_State *L); 160 | LUA_API void (lua_settop) (lua_State *L, int idx); 161 | LUA_API void (lua_pushvalue) (lua_State *L, int idx); 162 | LUA_API void (lua_rotate) (lua_State *L, int idx, int n); 163 | LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); 164 | LUA_API int (lua_checkstack) (lua_State *L, int n); 165 | 166 | LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); 167 | 168 | 169 | /* 170 | ** access functions (stack -> C) 171 | */ 172 | 173 | LUA_API int (lua_isnumber) (lua_State *L, int idx); 174 | LUA_API int (lua_isstring) (lua_State *L, int idx); 175 | LUA_API int (lua_iscfunction) (lua_State *L, int idx); 176 | LUA_API int (lua_isinteger) (lua_State *L, int idx); 177 | LUA_API int (lua_isuserdata) (lua_State *L, int idx); 178 | LUA_API int (lua_type) (lua_State *L, int idx); 179 | LUA_API const char *(lua_typename) (lua_State *L, int tp); 180 | 181 | LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); 182 | LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); 183 | LUA_API int (lua_toboolean) (lua_State *L, int idx); 184 | LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); 185 | LUA_API size_t (lua_rawlen) (lua_State *L, int idx); 186 | LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); 187 | LUA_API void *(lua_touserdata) (lua_State *L, int idx); 188 | LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); 189 | LUA_API const void *(lua_topointer) (lua_State *L, int idx); 190 | 191 | 192 | /* 193 | ** Comparison and arithmetic functions 194 | */ 195 | 196 | #define LUA_OPADD 0 /* ORDER TM, ORDER OP */ 197 | #define LUA_OPSUB 1 198 | #define LUA_OPMUL 2 199 | #define LUA_OPMOD 3 200 | #define LUA_OPPOW 4 201 | #define LUA_OPDIV 5 202 | #define LUA_OPIDIV 6 203 | #define LUA_OPBAND 7 204 | #define LUA_OPBOR 8 205 | #define LUA_OPBXOR 9 206 | #define LUA_OPSHL 10 207 | #define LUA_OPSHR 11 208 | #define LUA_OPUNM 12 209 | #define LUA_OPBNOT 13 210 | 211 | LUA_API void (lua_arith) (lua_State *L, int op); 212 | 213 | #define LUA_OPEQ 0 214 | #define LUA_OPLT 1 215 | #define LUA_OPLE 2 216 | 217 | LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); 218 | LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); 219 | 220 | 221 | /* 222 | ** push functions (C -> stack) 223 | */ 224 | LUA_API void (lua_pushnil) (lua_State *L); 225 | LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); 226 | LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); 227 | LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); 228 | LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); 229 | LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, 230 | va_list argp); 231 | LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); 232 | LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); 233 | LUA_API void (lua_pushboolean) (lua_State *L, int b); 234 | LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); 235 | LUA_API int (lua_pushthread) (lua_State *L); 236 | 237 | 238 | /* 239 | ** get functions (Lua -> stack) 240 | */ 241 | LUA_API int (lua_getglobal) (lua_State *L, const char *name); 242 | LUA_API int (lua_gettable) (lua_State *L, int idx); 243 | LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k); 244 | LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n); 245 | LUA_API int (lua_rawget) (lua_State *L, int idx); 246 | LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); 247 | LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); 248 | 249 | LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); 250 | LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); 251 | LUA_API int (lua_getmetatable) (lua_State *L, int objindex); 252 | LUA_API int (lua_getuservalue) (lua_State *L, int idx); 253 | 254 | 255 | /* 256 | ** set functions (stack -> Lua) 257 | */ 258 | LUA_API void (lua_setglobal) (lua_State *L, const char *name); 259 | LUA_API void (lua_settable) (lua_State *L, int idx); 260 | LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); 261 | LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n); 262 | LUA_API void (lua_rawset) (lua_State *L, int idx); 263 | LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n); 264 | LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); 265 | LUA_API int (lua_setmetatable) (lua_State *L, int objindex); 266 | LUA_API void (lua_setuservalue) (lua_State *L, int idx); 267 | 268 | 269 | /* 270 | ** 'load' and 'call' functions (load and run Lua code) 271 | */ 272 | LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, 273 | lua_KContext ctx, lua_KFunction k); 274 | #define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) 275 | 276 | LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, 277 | lua_KContext ctx, lua_KFunction k); 278 | #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) 279 | 280 | LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, 281 | const char *chunkname, const char *mode); 282 | 283 | LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); 284 | 285 | 286 | /* 287 | ** coroutine functions 288 | */ 289 | LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, 290 | lua_KFunction k); 291 | LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); 292 | LUA_API int (lua_status) (lua_State *L); 293 | LUA_API int (lua_isyieldable) (lua_State *L); 294 | 295 | #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) 296 | 297 | 298 | /* 299 | ** garbage-collection function and options 300 | */ 301 | 302 | #define LUA_GCSTOP 0 303 | #define LUA_GCRESTART 1 304 | #define LUA_GCCOLLECT 2 305 | #define LUA_GCCOUNT 3 306 | #define LUA_GCCOUNTB 4 307 | #define LUA_GCSTEP 5 308 | #define LUA_GCSETPAUSE 6 309 | #define LUA_GCSETSTEPMUL 7 310 | #define LUA_GCISRUNNING 9 311 | 312 | LUA_API int (lua_gc) (lua_State *L, int what, int data); 313 | 314 | 315 | /* 316 | ** miscellaneous functions 317 | */ 318 | 319 | LUA_API int (lua_error) (lua_State *L); 320 | 321 | LUA_API int (lua_next) (lua_State *L, int idx); 322 | 323 | LUA_API void (lua_concat) (lua_State *L, int n); 324 | LUA_API void (lua_len) (lua_State *L, int idx); 325 | 326 | LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); 327 | 328 | LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); 329 | LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); 330 | 331 | 332 | 333 | /* 334 | ** {============================================================== 335 | ** some useful macros 336 | ** =============================================================== 337 | */ 338 | 339 | #define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) 340 | 341 | #define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) 342 | #define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) 343 | 344 | #define lua_pop(L,n) lua_settop(L, -(n)-1) 345 | 346 | #define lua_newtable(L) lua_createtable(L, 0, 0) 347 | 348 | #define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) 349 | 350 | #define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) 351 | 352 | #define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) 353 | #define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) 354 | #define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) 355 | #define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) 356 | #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) 357 | #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) 358 | #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) 359 | #define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) 360 | 361 | #define lua_pushliteral(L, s) lua_pushstring(L, "" s) 362 | 363 | #define lua_pushglobaltable(L) \ 364 | ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)) 365 | 366 | #define lua_tostring(L,i) lua_tolstring(L, (i), NULL) 367 | 368 | 369 | #define lua_insert(L,idx) lua_rotate(L, (idx), 1) 370 | 371 | #define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) 372 | 373 | #define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1)) 374 | 375 | /* }============================================================== */ 376 | 377 | 378 | /* 379 | ** {============================================================== 380 | ** compatibility macros for unsigned conversions 381 | ** =============================================================== 382 | */ 383 | #if defined(LUA_COMPAT_APIINTCASTS) 384 | 385 | #define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) 386 | #define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) 387 | #define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) 388 | 389 | #endif 390 | /* }============================================================== */ 391 | 392 | /* 393 | ** {====================================================================== 394 | ** Debug API 395 | ** ======================================================================= 396 | */ 397 | 398 | 399 | /* 400 | ** Event codes 401 | */ 402 | #define LUA_HOOKCALL 0 403 | #define LUA_HOOKRET 1 404 | #define LUA_HOOKLINE 2 405 | #define LUA_HOOKCOUNT 3 406 | #define LUA_HOOKTAILCALL 4 407 | 408 | 409 | /* 410 | ** Event masks 411 | */ 412 | #define LUA_MASKCALL (1 << LUA_HOOKCALL) 413 | #define LUA_MASKRET (1 << LUA_HOOKRET) 414 | #define LUA_MASKLINE (1 << LUA_HOOKLINE) 415 | #define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) 416 | 417 | typedef struct lua_Debug lua_Debug; /* activation record */ 418 | 419 | 420 | /* Functions to be called by the debugger in specific events */ 421 | typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); 422 | 423 | 424 | LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); 425 | LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); 426 | LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n); 427 | LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n); 428 | LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n); 429 | LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n); 430 | 431 | LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); 432 | LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, 433 | int fidx2, int n2); 434 | 435 | LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); 436 | LUA_API lua_Hook (lua_gethook) (lua_State *L); 437 | LUA_API int (lua_gethookmask) (lua_State *L); 438 | LUA_API int (lua_gethookcount) (lua_State *L); 439 | 440 | 441 | struct lua_Debug { 442 | int event; 443 | const char *name; /* (n) */ 444 | const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ 445 | const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ 446 | const char *source; /* (S) */ 447 | int currentline; /* (l) */ 448 | int linedefined; /* (S) */ 449 | int lastlinedefined; /* (S) */ 450 | unsigned char nups; /* (u) number of upvalues */ 451 | unsigned char nparams;/* (u) number of parameters */ 452 | char isvararg; /* (u) */ 453 | char istailcall; /* (t) */ 454 | char short_src[LUA_IDSIZE]; /* (S) */ 455 | /* private part */ 456 | struct CallInfo *i_ci; /* active function */ 457 | }; 458 | 459 | /* }====================================================================== */ 460 | 461 | 462 | /****************************************************************************** 463 | * Copyright (C) 1994-2017 Lua.org, PUC-Rio. 464 | * 465 | * Permission is hereby granted, free of charge, to any person obtaining 466 | * a copy of this software and associated documentation files (the 467 | * "Software"), to deal in the Software without restriction, including 468 | * without limitation the rights to use, copy, modify, merge, publish, 469 | * distribute, sublicense, and/or sell copies of the Software, and to 470 | * permit persons to whom the Software is furnished to do so, subject to 471 | * the following conditions: 472 | * 473 | * The above copyright notice and this permission notice shall be 474 | * included in all copies or substantial portions of the Software. 475 | * 476 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 477 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 478 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 479 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 480 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 481 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 482 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 483 | ******************************************************************************/ 484 | 485 | 486 | #endif 487 | -------------------------------------------------------------------------------- /example/fflua_win/winlua-5.3.4/include/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /example/fflua_win/winlua-5.3.4/include/luaconf.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: luaconf.h,v 1.259 2016/12/22 13:08:50 roberto Exp $ 3 | ** Configuration file for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef luaconf_h 9 | #define luaconf_h 10 | 11 | #include 12 | #include 13 | 14 | 15 | /* 16 | ** =================================================================== 17 | ** Search for "@@" to find all configurable definitions. 18 | ** =================================================================== 19 | */ 20 | 21 | 22 | /* 23 | ** {==================================================================== 24 | ** System Configuration: macros to adapt (if needed) Lua to some 25 | ** particular platform, for instance compiling it with 32-bit numbers or 26 | ** restricting it to C89. 27 | ** ===================================================================== 28 | */ 29 | 30 | /* 31 | @@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You 32 | ** can also define LUA_32BITS in the make file, but changing here you 33 | ** ensure that all software connected to Lua will be compiled with the 34 | ** same configuration. 35 | */ 36 | /* #define LUA_32BITS */ 37 | 38 | 39 | /* 40 | @@ LUA_USE_C89 controls the use of non-ISO-C89 features. 41 | ** Define it if you want Lua to avoid the use of a few C99 features 42 | ** or Windows-specific features on Windows. 43 | */ 44 | /* #define LUA_USE_C89 */ 45 | 46 | 47 | /* 48 | ** By default, Lua on Windows use (some) specific Windows features 49 | */ 50 | #if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE) 51 | #define LUA_USE_WINDOWS /* enable goodies for regular Windows */ 52 | #endif 53 | 54 | 55 | #if defined(LUA_USE_WINDOWS) 56 | #define LUA_DL_DLL /* enable support for DLL */ 57 | #define LUA_USE_C89 /* broadly, Windows is C89 */ 58 | #endif 59 | 60 | 61 | #if defined(LUA_USE_LINUX) 62 | #define LUA_USE_POSIX 63 | #define LUA_USE_DLOPEN /* needs an extra library: -ldl */ 64 | #define LUA_USE_READLINE /* needs some extra libraries */ 65 | #endif 66 | 67 | 68 | #if defined(LUA_USE_MACOSX) 69 | #define LUA_USE_POSIX 70 | #define LUA_USE_DLOPEN /* MacOS does not need -ldl */ 71 | #define LUA_USE_READLINE /* needs an extra library: -lreadline */ 72 | #endif 73 | 74 | 75 | /* 76 | @@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for 77 | ** C89 ('long' and 'double'); Windows always has '__int64', so it does 78 | ** not need to use this case. 79 | */ 80 | #if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) 81 | #define LUA_C89_NUMBERS 82 | #endif 83 | 84 | 85 | 86 | /* 87 | @@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'. 88 | */ 89 | /* avoid undefined shifts */ 90 | #if ((INT_MAX >> 15) >> 15) >= 1 91 | #define LUAI_BITSINT 32 92 | #else 93 | /* 'int' always must have at least 16 bits */ 94 | #define LUAI_BITSINT 16 95 | #endif 96 | 97 | 98 | /* 99 | @@ LUA_INT_TYPE defines the type for Lua integers. 100 | @@ LUA_FLOAT_TYPE defines the type for Lua floats. 101 | ** Lua should work fine with any mix of these options (if supported 102 | ** by your C compiler). The usual configurations are 64-bit integers 103 | ** and 'double' (the default), 32-bit integers and 'float' (for 104 | ** restricted platforms), and 'long'/'double' (for C compilers not 105 | ** compliant with C99, which may not have support for 'long long'). 106 | */ 107 | 108 | /* predefined options for LUA_INT_TYPE */ 109 | #define LUA_INT_INT 1 110 | #define LUA_INT_LONG 2 111 | #define LUA_INT_LONGLONG 3 112 | 113 | /* predefined options for LUA_FLOAT_TYPE */ 114 | #define LUA_FLOAT_FLOAT 1 115 | #define LUA_FLOAT_DOUBLE 2 116 | #define LUA_FLOAT_LONGDOUBLE 3 117 | 118 | #if defined(LUA_32BITS) /* { */ 119 | /* 120 | ** 32-bit integers and 'float' 121 | */ 122 | #if LUAI_BITSINT >= 32 /* use 'int' if big enough */ 123 | #define LUA_INT_TYPE LUA_INT_INT 124 | #else /* otherwise use 'long' */ 125 | #define LUA_INT_TYPE LUA_INT_LONG 126 | #endif 127 | #define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT 128 | 129 | #elif defined(LUA_C89_NUMBERS) /* }{ */ 130 | /* 131 | ** largest types available for C89 ('long' and 'double') 132 | */ 133 | #define LUA_INT_TYPE LUA_INT_LONG 134 | #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE 135 | 136 | #endif /* } */ 137 | 138 | 139 | /* 140 | ** default configuration for 64-bit Lua ('long long' and 'double') 141 | */ 142 | #if !defined(LUA_INT_TYPE) 143 | #define LUA_INT_TYPE LUA_INT_LONGLONG 144 | #endif 145 | 146 | #if !defined(LUA_FLOAT_TYPE) 147 | #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE 148 | #endif 149 | 150 | /* }================================================================== */ 151 | 152 | 153 | 154 | 155 | /* 156 | ** {================================================================== 157 | ** Configuration for Paths. 158 | ** =================================================================== 159 | */ 160 | 161 | /* 162 | ** LUA_PATH_SEP is the character that separates templates in a path. 163 | ** LUA_PATH_MARK is the string that marks the substitution points in a 164 | ** template. 165 | ** LUA_EXEC_DIR in a Windows path is replaced by the executable's 166 | ** directory. 167 | */ 168 | #define LUA_PATH_SEP ";" 169 | #define LUA_PATH_MARK "?" 170 | #define LUA_EXEC_DIR "!" 171 | 172 | 173 | /* 174 | @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for 175 | ** Lua libraries. 176 | @@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for 177 | ** C libraries. 178 | ** CHANGE them if your machine has a non-conventional directory 179 | ** hierarchy or if you want to install your libraries in 180 | ** non-conventional directories. 181 | */ 182 | #define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR 183 | #if defined(_WIN32) /* { */ 184 | /* 185 | ** In Windows, any exclamation mark ('!') in the path is replaced by the 186 | ** path of the directory of the executable file of the current process. 187 | */ 188 | #define LUA_LDIR "!\\lua\\" 189 | #define LUA_CDIR "!\\" 190 | #define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" 191 | #define LUA_PATH_DEFAULT \ 192 | LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ 193 | LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ 194 | LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ 195 | ".\\?.lua;" ".\\?\\init.lua" 196 | #define LUA_CPATH_DEFAULT \ 197 | LUA_CDIR"?.dll;" \ 198 | LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ 199 | LUA_CDIR"loadall.dll;" ".\\?.dll;" \ 200 | LUA_CDIR"?53.dll;" ".\\?53.dll" 201 | 202 | #else /* }{ */ 203 | 204 | #define LUA_ROOT "/usr/local/" 205 | #define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" 206 | #define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" 207 | #define LUA_PATH_DEFAULT \ 208 | LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ 209 | LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ 210 | "./?.lua;" "./?/init.lua" 211 | #define LUA_CPATH_DEFAULT \ 212 | LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so;" \ 213 | LUA_CDIR"lib?53.so;" "./lib?53.so" 214 | #endif /* } */ 215 | 216 | 217 | /* 218 | @@ LUA_DIRSEP is the directory separator (for submodules). 219 | ** CHANGE it if your machine does not use "/" as the directory separator 220 | ** and is not Windows. (On Windows Lua automatically uses "\".) 221 | */ 222 | #if defined(_WIN32) 223 | #define LUA_DIRSEP "\\" 224 | #else 225 | #define LUA_DIRSEP "/" 226 | #endif 227 | 228 | /* }================================================================== */ 229 | 230 | 231 | /* 232 | ** {================================================================== 233 | ** Marks for exported symbols in the C code 234 | ** =================================================================== 235 | */ 236 | 237 | /* 238 | @@ LUA_API is a mark for all core API functions. 239 | @@ LUALIB_API is a mark for all auxiliary library functions. 240 | @@ LUAMOD_API is a mark for all standard library opening functions. 241 | ** CHANGE them if you need to define those functions in some special way. 242 | ** For instance, if you want to create one Windows DLL with the core and 243 | ** the libraries, you may want to use the following definition (define 244 | ** LUA_BUILD_AS_DLL to get it). 245 | */ 246 | #if defined(LUA_BUILD_AS_DLL) /* { */ 247 | 248 | #if defined(LUA_CORE) || defined(LUA_LIB) /* { */ 249 | #define LUA_API __declspec(dllexport) 250 | #else /* }{ */ 251 | #define LUA_API __declspec(dllimport) 252 | #endif /* } */ 253 | 254 | #else /* }{ */ 255 | 256 | #define LUA_API extern 257 | 258 | #endif /* } */ 259 | 260 | 261 | /* more often than not the libs go together with the core */ 262 | #define LUALIB_API LUA_API 263 | #define LUAMOD_API LUALIB_API 264 | 265 | 266 | /* 267 | @@ LUAI_FUNC is a mark for all extern functions that are not to be 268 | ** exported to outside modules. 269 | @@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables 270 | ** that are not to be exported to outside modules (LUAI_DDEF for 271 | ** definitions and LUAI_DDEC for declarations). 272 | ** CHANGE them if you need to mark them in some special way. Elf/gcc 273 | ** (versions 3.2 and later) mark them as "hidden" to optimize access 274 | ** when Lua is compiled as a shared library. Not all elf targets support 275 | ** this attribute. Unfortunately, gcc does not offer a way to check 276 | ** whether the target offers that support, and those without support 277 | ** give a warning about it. To avoid these warnings, change to the 278 | ** default definition. 279 | */ 280 | #if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ 281 | defined(__ELF__) /* { */ 282 | #define LUAI_FUNC __attribute__((visibility("hidden"))) extern 283 | #else /* }{ */ 284 | #define LUAI_FUNC extern 285 | #endif /* } */ 286 | 287 | #define LUAI_DDEC LUAI_FUNC 288 | #define LUAI_DDEF /* empty */ 289 | 290 | /* }================================================================== */ 291 | 292 | 293 | /* 294 | ** {================================================================== 295 | ** Compatibility with previous versions 296 | ** =================================================================== 297 | */ 298 | 299 | /* 300 | @@ LUA_COMPAT_5_2 controls other macros for compatibility with Lua 5.2. 301 | @@ LUA_COMPAT_5_1 controls other macros for compatibility with Lua 5.1. 302 | ** You can define it to get all options, or change specific options 303 | ** to fit your specific needs. 304 | */ 305 | #if defined(LUA_COMPAT_5_2) /* { */ 306 | 307 | /* 308 | @@ LUA_COMPAT_MATHLIB controls the presence of several deprecated 309 | ** functions in the mathematical library. 310 | */ 311 | #define LUA_COMPAT_MATHLIB 312 | 313 | /* 314 | @@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'. 315 | */ 316 | #define LUA_COMPAT_BITLIB 317 | 318 | /* 319 | @@ LUA_COMPAT_IPAIRS controls the effectiveness of the __ipairs metamethod. 320 | */ 321 | #define LUA_COMPAT_IPAIRS 322 | 323 | /* 324 | @@ LUA_COMPAT_APIINTCASTS controls the presence of macros for 325 | ** manipulating other integer types (lua_pushunsigned, lua_tounsigned, 326 | ** luaL_checkint, luaL_checklong, etc.) 327 | */ 328 | #define LUA_COMPAT_APIINTCASTS 329 | 330 | #endif /* } */ 331 | 332 | 333 | #if defined(LUA_COMPAT_5_1) /* { */ 334 | 335 | /* Incompatibilities from 5.2 -> 5.3 */ 336 | #define LUA_COMPAT_MATHLIB 337 | #define LUA_COMPAT_APIINTCASTS 338 | 339 | /* 340 | @@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'. 341 | ** You can replace it with 'table.unpack'. 342 | */ 343 | #define LUA_COMPAT_UNPACK 344 | 345 | /* 346 | @@ LUA_COMPAT_LOADERS controls the presence of table 'package.loaders'. 347 | ** You can replace it with 'package.searchers'. 348 | */ 349 | #define LUA_COMPAT_LOADERS 350 | 351 | /* 352 | @@ macro 'lua_cpcall' emulates deprecated function lua_cpcall. 353 | ** You can call your C function directly (with light C functions). 354 | */ 355 | #define lua_cpcall(L,f,u) \ 356 | (lua_pushcfunction(L, (f)), \ 357 | lua_pushlightuserdata(L,(u)), \ 358 | lua_pcall(L,1,0,0)) 359 | 360 | 361 | /* 362 | @@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library. 363 | ** You can rewrite 'log10(x)' as 'log(x, 10)'. 364 | */ 365 | #define LUA_COMPAT_LOG10 366 | 367 | /* 368 | @@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base 369 | ** library. You can rewrite 'loadstring(s)' as 'load(s)'. 370 | */ 371 | #define LUA_COMPAT_LOADSTRING 372 | 373 | /* 374 | @@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library. 375 | */ 376 | #define LUA_COMPAT_MAXN 377 | 378 | /* 379 | @@ The following macros supply trivial compatibility for some 380 | ** changes in the API. The macros themselves document how to 381 | ** change your code to avoid using them. 382 | */ 383 | #define lua_strlen(L,i) lua_rawlen(L, (i)) 384 | 385 | #define lua_objlen(L,i) lua_rawlen(L, (i)) 386 | 387 | #define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) 388 | #define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) 389 | 390 | /* 391 | @@ LUA_COMPAT_MODULE controls compatibility with previous 392 | ** module functions 'module' (Lua) and 'luaL_register' (C). 393 | */ 394 | #define LUA_COMPAT_MODULE 395 | 396 | #endif /* } */ 397 | 398 | 399 | /* 400 | @@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a 401 | @@ a float mark ('.0'). 402 | ** This macro is not on by default even in compatibility mode, 403 | ** because this is not really an incompatibility. 404 | */ 405 | /* #define LUA_COMPAT_FLOATSTRING */ 406 | 407 | /* }================================================================== */ 408 | 409 | 410 | 411 | /* 412 | ** {================================================================== 413 | ** Configuration for Numbers. 414 | ** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* 415 | ** satisfy your needs. 416 | ** =================================================================== 417 | */ 418 | 419 | /* 420 | @@ LUA_NUMBER is the floating-point type used by Lua. 421 | @@ LUAI_UACNUMBER is the result of a 'default argument promotion' 422 | @@ over a floating number. 423 | @@ l_mathlim(x) corrects limit name 'x' to the proper float type 424 | ** by prefixing it with one of FLT/DBL/LDBL. 425 | @@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. 426 | @@ LUA_NUMBER_FMT is the format for writing floats. 427 | @@ lua_number2str converts a float to a string. 428 | @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. 429 | @@ l_floor takes the floor of a float. 430 | @@ lua_str2number converts a decimal numeric string to a number. 431 | */ 432 | 433 | 434 | /* The following definitions are good for most cases here */ 435 | 436 | #define l_floor(x) (l_mathop(floor)(x)) 437 | 438 | #define lua_number2str(s,sz,n) \ 439 | l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n)) 440 | 441 | /* 442 | @@ lua_numbertointeger converts a float number to an integer, or 443 | ** returns 0 if float is not within the range of a lua_Integer. 444 | ** (The range comparisons are tricky because of rounding. The tests 445 | ** here assume a two-complement representation, where MININTEGER always 446 | ** has an exact representation as a float; MAXINTEGER may not have one, 447 | ** and therefore its conversion to float may have an ill-defined value.) 448 | */ 449 | #define lua_numbertointeger(n,p) \ 450 | ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ 451 | (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ 452 | (*(p) = (LUA_INTEGER)(n), 1)) 453 | 454 | 455 | /* now the variable definitions */ 456 | 457 | #if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ 458 | 459 | #define LUA_NUMBER float 460 | 461 | #define l_mathlim(n) (FLT_##n) 462 | 463 | #define LUAI_UACNUMBER double 464 | 465 | #define LUA_NUMBER_FRMLEN "" 466 | #define LUA_NUMBER_FMT "%.7g" 467 | 468 | #define l_mathop(op) op##f 469 | 470 | #define lua_str2number(s,p) strtof((s), (p)) 471 | 472 | 473 | #elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */ 474 | 475 | #define LUA_NUMBER long double 476 | 477 | #define l_mathlim(n) (LDBL_##n) 478 | 479 | #define LUAI_UACNUMBER long double 480 | 481 | #define LUA_NUMBER_FRMLEN "L" 482 | #define LUA_NUMBER_FMT "%.19Lg" 483 | 484 | #define l_mathop(op) op##l 485 | 486 | #define lua_str2number(s,p) strtold((s), (p)) 487 | 488 | #elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */ 489 | 490 | #define LUA_NUMBER double 491 | 492 | #define l_mathlim(n) (DBL_##n) 493 | 494 | #define LUAI_UACNUMBER double 495 | 496 | #define LUA_NUMBER_FRMLEN "" 497 | #define LUA_NUMBER_FMT "%.14g" 498 | 499 | #define l_mathop(op) op 500 | 501 | #define lua_str2number(s,p) strtod((s), (p)) 502 | 503 | #else /* }{ */ 504 | 505 | #error "numeric float type not defined" 506 | 507 | #endif /* } */ 508 | 509 | 510 | 511 | /* 512 | @@ LUA_INTEGER is the integer type used by Lua. 513 | ** 514 | @@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. 515 | ** 516 | @@ LUAI_UACINT is the result of a 'default argument promotion' 517 | @@ over a lUA_INTEGER. 518 | @@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. 519 | @@ LUA_INTEGER_FMT is the format for writing integers. 520 | @@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. 521 | @@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. 522 | @@ lua_integer2str converts an integer to a string. 523 | */ 524 | 525 | 526 | /* The following definitions are good for most cases here */ 527 | 528 | #define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" 529 | 530 | #define LUAI_UACINT LUA_INTEGER 531 | 532 | #define lua_integer2str(s,sz,n) \ 533 | l_sprintf((s), sz, LUA_INTEGER_FMT, (LUAI_UACINT)(n)) 534 | 535 | /* 536 | ** use LUAI_UACINT here to avoid problems with promotions (which 537 | ** can turn a comparison between unsigneds into a signed comparison) 538 | */ 539 | #define LUA_UNSIGNED unsigned LUAI_UACINT 540 | 541 | 542 | /* now the variable definitions */ 543 | 544 | #if LUA_INT_TYPE == LUA_INT_INT /* { int */ 545 | 546 | #define LUA_INTEGER int 547 | #define LUA_INTEGER_FRMLEN "" 548 | 549 | #define LUA_MAXINTEGER INT_MAX 550 | #define LUA_MININTEGER INT_MIN 551 | 552 | #elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ 553 | 554 | #define LUA_INTEGER long 555 | #define LUA_INTEGER_FRMLEN "l" 556 | 557 | #define LUA_MAXINTEGER LONG_MAX 558 | #define LUA_MININTEGER LONG_MIN 559 | 560 | #elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ 561 | 562 | /* use presence of macro LLONG_MAX as proxy for C99 compliance */ 563 | #if defined(LLONG_MAX) /* { */ 564 | /* use ISO C99 stuff */ 565 | 566 | #define LUA_INTEGER long long 567 | #define LUA_INTEGER_FRMLEN "ll" 568 | 569 | #define LUA_MAXINTEGER LLONG_MAX 570 | #define LUA_MININTEGER LLONG_MIN 571 | 572 | #elif defined(LUA_USE_WINDOWS) /* }{ */ 573 | /* in Windows, can use specific Windows types */ 574 | 575 | #define LUA_INTEGER __int64 576 | #define LUA_INTEGER_FRMLEN "I64" 577 | 578 | #define LUA_MAXINTEGER _I64_MAX 579 | #define LUA_MININTEGER _I64_MIN 580 | 581 | #else /* }{ */ 582 | 583 | #error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ 584 | or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)" 585 | 586 | #endif /* } */ 587 | 588 | #else /* }{ */ 589 | 590 | #error "numeric integer type not defined" 591 | 592 | #endif /* } */ 593 | 594 | /* }================================================================== */ 595 | 596 | 597 | /* 598 | ** {================================================================== 599 | ** Dependencies with C99 and other C details 600 | ** =================================================================== 601 | */ 602 | 603 | /* 604 | @@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89. 605 | ** (All uses in Lua have only one format item.) 606 | */ 607 | #if !defined(LUA_USE_C89) 608 | #define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i) 609 | #else 610 | #define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s,f,i)) 611 | #endif 612 | 613 | 614 | /* 615 | @@ lua_strx2number converts an hexadecimal numeric string to a number. 616 | ** In C99, 'strtod' does that conversion. Otherwise, you can 617 | ** leave 'lua_strx2number' undefined and Lua will provide its own 618 | ** implementation. 619 | */ 620 | #if !defined(LUA_USE_C89) 621 | #define lua_strx2number(s,p) lua_str2number(s,p) 622 | #endif 623 | 624 | 625 | /* 626 | @@ lua_number2strx converts a float to an hexadecimal numeric string. 627 | ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. 628 | ** Otherwise, you can leave 'lua_number2strx' undefined and Lua will 629 | ** provide its own implementation. 630 | */ 631 | #if !defined(LUA_USE_C89) 632 | #define lua_number2strx(L,b,sz,f,n) \ 633 | ((void)L, l_sprintf(b,sz,f,(LUAI_UACNUMBER)(n))) 634 | #endif 635 | 636 | 637 | /* 638 | ** 'strtof' and 'opf' variants for math functions are not valid in 639 | ** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the 640 | ** availability of these variants. ('math.h' is already included in 641 | ** all files that use these macros.) 642 | */ 643 | #if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) 644 | #undef l_mathop /* variants not available */ 645 | #undef lua_str2number 646 | #define l_mathop(op) (lua_Number)op /* no variant */ 647 | #define lua_str2number(s,p) ((lua_Number)strtod((s), (p))) 648 | #endif 649 | 650 | 651 | /* 652 | @@ LUA_KCONTEXT is the type of the context ('ctx') for continuation 653 | ** functions. It must be a numerical type; Lua will use 'intptr_t' if 654 | ** available, otherwise it will use 'ptrdiff_t' (the nearest thing to 655 | ** 'intptr_t' in C89) 656 | */ 657 | #define LUA_KCONTEXT ptrdiff_t 658 | 659 | #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ 660 | __STDC_VERSION__ >= 199901L 661 | #include 662 | #if defined(INTPTR_MAX) /* even in C99 this type is optional */ 663 | #undef LUA_KCONTEXT 664 | #define LUA_KCONTEXT intptr_t 665 | #endif 666 | #endif 667 | 668 | 669 | /* 670 | @@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). 671 | ** Change that if you do not want to use C locales. (Code using this 672 | ** macro must include header 'locale.h'.) 673 | */ 674 | #if !defined(lua_getlocaledecpoint) 675 | #define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) 676 | #endif 677 | 678 | /* }================================================================== */ 679 | 680 | 681 | /* 682 | ** {================================================================== 683 | ** Language Variations 684 | ** ===================================================================== 685 | */ 686 | 687 | /* 688 | @@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some 689 | ** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from 690 | ** numbers to strings. Define LUA_NOCVTS2N to turn off automatic 691 | ** coercion from strings to numbers. 692 | */ 693 | /* #define LUA_NOCVTN2S */ 694 | /* #define LUA_NOCVTS2N */ 695 | 696 | 697 | /* 698 | @@ LUA_USE_APICHECK turns on several consistency checks on the C API. 699 | ** Define it as a help when debugging C code. 700 | */ 701 | #if defined(LUA_USE_APICHECK) 702 | #include 703 | #define luai_apicheck(l,e) assert(e) 704 | #endif 705 | 706 | /* }================================================================== */ 707 | 708 | 709 | /* 710 | ** {================================================================== 711 | ** Macros that affect the API and must be stable (that is, must be the 712 | ** same when you compile Lua and when you compile code that links to 713 | ** Lua). You probably do not want/need to change them. 714 | ** ===================================================================== 715 | */ 716 | 717 | /* 718 | @@ LUAI_MAXSTACK limits the size of the Lua stack. 719 | ** CHANGE it if you need a different limit. This limit is arbitrary; 720 | ** its only purpose is to stop Lua from consuming unlimited stack 721 | ** space (and to reserve some numbers for pseudo-indices). 722 | */ 723 | #if LUAI_BITSINT >= 32 724 | #define LUAI_MAXSTACK 1000000 725 | #else 726 | #define LUAI_MAXSTACK 15000 727 | #endif 728 | 729 | 730 | /* 731 | @@ LUA_EXTRASPACE defines the size of a raw memory area associated with 732 | ** a Lua state with very fast access. 733 | ** CHANGE it if you need a different size. 734 | */ 735 | #define LUA_EXTRASPACE (sizeof(void *)) 736 | 737 | 738 | /* 739 | @@ LUA_IDSIZE gives the maximum size for the description of the source 740 | @@ of a function in debug information. 741 | ** CHANGE it if you want a different size. 742 | */ 743 | #define LUA_IDSIZE 60 744 | 745 | 746 | /* 747 | @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. 748 | ** CHANGE it if it uses too much C-stack space. (For long double, 749 | ** 'string.format("%.99f", -1e4932)' needs 5034 bytes, so a 750 | ** smaller buffer would force a memory allocation for each call to 751 | ** 'string.format'.) 752 | */ 753 | #if LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE 754 | #define LUAL_BUFFERSIZE 8192 755 | #else 756 | #define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) 757 | #endif 758 | 759 | /* }================================================================== */ 760 | 761 | 762 | /* 763 | @@ LUA_QL describes how error messages quote program elements. 764 | ** Lua does not use these macros anymore; they are here for 765 | ** compatibility only. 766 | */ 767 | #define LUA_QL(x) "'" x "'" 768 | #define LUA_QS LUA_QL("%s") 769 | 770 | 771 | 772 | 773 | /* =================================================================== */ 774 | 775 | /* 776 | ** Local configuration. You can use this space to add your redefinitions 777 | ** without modifying the main part of the file. 778 | */ 779 | 780 | 781 | 782 | 783 | 784 | #endif 785 | 786 | -------------------------------------------------------------------------------- /example/fflua_win/winlua-5.3.4/include/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.45 2017/01/12 17:14:26 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | /* version suffix for environment variable names */ 15 | #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR 16 | 17 | 18 | LUAMOD_API int (luaopen_base) (lua_State *L); 19 | 20 | #define LUA_COLIBNAME "coroutine" 21 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 22 | 23 | #define LUA_TABLIBNAME "table" 24 | LUAMOD_API int (luaopen_table) (lua_State *L); 25 | 26 | #define LUA_IOLIBNAME "io" 27 | LUAMOD_API int (luaopen_io) (lua_State *L); 28 | 29 | #define LUA_OSLIBNAME "os" 30 | LUAMOD_API int (luaopen_os) (lua_State *L); 31 | 32 | #define LUA_STRLIBNAME "string" 33 | LUAMOD_API int (luaopen_string) (lua_State *L); 34 | 35 | #define LUA_UTF8LIBNAME "utf8" 36 | LUAMOD_API int (luaopen_utf8) (lua_State *L); 37 | 38 | #define LUA_BITLIBNAME "bit32" 39 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 40 | 41 | #define LUA_MATHLIBNAME "math" 42 | LUAMOD_API int (luaopen_math) (lua_State *L); 43 | 44 | #define LUA_DBLIBNAME "debug" 45 | LUAMOD_API int (luaopen_debug) (lua_State *L); 46 | 47 | #define LUA_LOADLIBNAME "package" 48 | LUAMOD_API int (luaopen_package) (lua_State *L); 49 | 50 | 51 | /* open all previous libraries */ 52 | LUALIB_API void (luaL_openlibs) (lua_State *L); 53 | 54 | 55 | 56 | #if !defined(lua_assert) 57 | #define lua_assert(x) ((void)0) 58 | #endif 59 | 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /example/fflua_win/winlua-5.3.4/lua53.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fanchy/fflua/de6e5a50458c56001a3871701673c05197bdfe52/example/fflua_win/winlua-5.3.4/lua53.lib -------------------------------------------------------------------------------- /example/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #include "lua/fflua.h" 7 | 8 | using namespace ff; 9 | 10 | class base_t 11 | { 12 | public: 13 | base_t():v(789){} 14 | void dump() 15 | { 16 | printf("in %s a:%d\n", __FUNCTION__, v); 17 | } 18 | int v; 19 | }; 20 | class foo_t: public base_t 21 | { 22 | public: 23 | foo_t(int b):a(b) 24 | { 25 | printf("in %s b:%d this=%p\n", __FUNCTION__, b, this); 26 | } 27 | ~foo_t() 28 | { 29 | printf("in %s\n", __FUNCTION__); 30 | } 31 | void print(int64_t a, base_t* p) const 32 | { 33 | printf("in foo_t::print a:%ld p:%p\n", (long)a, p); 34 | } 35 | 36 | static void dumy() 37 | { 38 | printf("in %s\n", __FUNCTION__); 39 | } 40 | int a; 41 | }; 42 | 43 | //! lua talbe 可以自动转换为stl 对象 44 | void dumy(map ret, vector a, list b, set c) 45 | { 46 | printf("in %s begin ------------\n", __FUNCTION__); 47 | for (map::iterator it = ret.begin(); it != ret.end(); ++it) 48 | { 49 | printf("map:%s, val:%s:\n", it->first.c_str(), it->second.c_str()); 50 | } 51 | printf("in %s end ------------\n", __FUNCTION__); 52 | } 53 | 54 | class clazz{ 55 | public: 56 | static void static_func(){ 57 | printf("in clazz::%s end ------------\n", __FUNCTION__); 58 | } 59 | }; 60 | 61 | static void lua_reg(lua_State* ls) 62 | { 63 | //! 注册基类函数, ctor() 为构造函数的类型 64 | fflua_register_t(ls, "base_t") //! 注册构造函数 65 | .def(&base_t::dump, "dump") //! 注册基类的函数 66 | .def(&base_t::v, "v"); //! 注册基类的属性 67 | 68 | //! 注册子类,ctor(int) 为构造函数, foo_t为类型名称, base_t为继承的基类名称 69 | fflua_register_t(ls, "foo_t", "base_t") 70 | .def(&foo_t::print, "print") //! 子类的函数 71 | .def(&foo_t::a, "a"); //! 子类的字段 72 | 73 | fflua_register_t<>(ls) 74 | .def(&dumy, "dumy"); //! 注册静态函数 75 | 76 | 77 | fflua_register_t(ls, "clazz") 78 | .def(&clazz::static_func, "static_func"); 79 | 80 | } 81 | 82 | int main(int argc, char* argv[]) 83 | { 84 | 85 | fflua_t fflua; 86 | try 87 | { 88 | fflua.setModFuncFlag(true); 89 | //! 注册C++ 对象到lua中 90 | fflua.reg(lua_reg); 91 | 92 | //! 载入lua文件 93 | fflua.add_package_path("./"); 94 | #ifdef _WIN32 95 | fflua.load_file("../test.lua"); 96 | #else 97 | fflua.load_file("test.lua"); 98 | #endif 99 | //! 获取全局变量 100 | int var = 0; 101 | assert(0 == fflua.get_global_variable("test_var", var)); 102 | //! 设置全局变量 103 | assert(0 == fflua.set_global_variable("test_var", ++var)); 104 | 105 | //! 执行lua 语句 106 | fflua.run_string("print(\"exe run_string!!\")"); 107 | 108 | //! 调用lua函数, 基本类型作为参数 109 | int32_t arg1 = 1; 110 | float arg2 = 2; 111 | double arg3 = 3; 112 | string arg4 = "4"; 113 | fflua.call("test_func", arg1, arg2, arg3, arg4); 114 | fflua.call("Mod:funcTest1", arg1, arg2); 115 | 116 | //! 调用lua函数,stl类型作为参数, 自动转换为lua talbe 117 | vector vec; vec.push_back(100); 118 | list lt; lt.push_back((float)99.99); 119 | set st; st.insert("OhNIce"); 120 | map mp; mp["key"] = 200; 121 | fflua.call("test_stl", vec, lt, st, mp); 122 | 123 | //! 调用lua 函数返回 talbe,自动转换为stl结构 124 | vec = fflua.call >("test_return_stl_vector"); 125 | lt = fflua.call >("test_return_stl_list"); 126 | st = fflua.call >("test_return_stl_set"); 127 | mp = fflua.call >("test_return_stl_map"); 128 | 129 | //! 调用lua函数,c++ 对象作为参数, foo_t 必须被注册过 130 | foo_t* foo_ptr = new foo_t(456); 131 | fflua.call("test_object", foo_ptr); 132 | 133 | //! 调用lua函数,c++ 对象作为返回值, foo_t 必须被注册过 134 | assert(foo_ptr == fflua.call("test_ret_object", foo_ptr)); 135 | //! 调用lua函数,c++ 对象作为返回值, 自动转换为基类 136 | base_t* base_ptr = fflua.call("test_ret_base_object", foo_ptr); 137 | assert(base_ptr == foo_ptr); 138 | 139 | } 140 | catch (exception& e) 141 | { 142 | printf("exception:%s\n", e.what()); 143 | 144 | 145 | } 146 | #ifdef _WIN32 147 | system("pause"); 148 | #endif 149 | return 0; 150 | } 151 | -------------------------------------------------------------------------------- /example/test.lua: -------------------------------------------------------------------------------- 1 | 2 | test_var = 99 3 | 4 | function dump_table(tb, str) 5 | if nil == str then str = "" end 6 | for k, v in pairs(tb) 7 | do 8 | print(str, k, v) 9 | end 10 | end 11 | 12 | Mod = {} 13 | function Mod:funcTest1(arg1, arg2) 14 | print("in funcTest1:", self, arg1, arg2) 15 | return true 16 | end 17 | Mod.funcTest1() 18 | -- 测试调用lua 19 | function test_func(arg1, arg2, arg3, arg4) 20 | print("in test_func:", arg1, arg2, arg3, arg4) 21 | mp = {["k"] = "v"} 22 | vc = {1,2,3} 23 | lt = {4,5,6} 24 | st = {7,8,9} 25 | dumy(mp, vc, lt, st) 26 | return arg1 27 | end 28 | 29 | -- 接受stl参数 30 | function test_stl(vec, lt, st, mp) 31 | print("--------------dump_table begin ----------------") 32 | dump_table(vec, "vec") 33 | dump_table(lt, "lt") 34 | dump_table(st, "st") 35 | dump_table(mp, "mp") 36 | print("--------------dump_table end ----------------") 37 | return "ok" 38 | end 39 | 40 | -- 返回stl 参数 41 | function test_return_stl_vector() 42 | return {1,2,3,4} 43 | end 44 | function test_return_stl_list() 45 | return {1,2,3,4} 46 | end 47 | function test_return_stl_set() 48 | return {1,2,3,4} 49 | end 50 | function test_return_stl_map() 51 | return { 52 | ["key"] = 124 53 | } 54 | end 55 | -- 测试接受C++对象 56 | function test_object(foo_obj) 57 | --测试构造 58 | base = base_t:new() 59 | -- 每个对象都有一个get_pointer获取指针 60 | print("base ptr:", base:get_pointer()) 61 | -- 测试C++对象函数 62 | foo_obj:print(12333, base) 63 | base:delete() 64 | --基类的函数 65 | foo_obj:dump() 66 | -- 测试C++ 对象属性 67 | print("foo property", foo_obj.a) 68 | print("base property", foo_obj.v) 69 | end 70 | 71 | -- 测试返回C++对象 72 | function test_ret_object(foo_obj) 73 | return foo_obj 74 | end 75 | 76 | -- 测试返回C++对象 77 | function test_ret_base_object(foo_obj) 78 | return foo_obj 79 | end 80 | 81 | 82 | clazz:static_func() -------------------------------------------------------------------------------- /lua/fflua.h: -------------------------------------------------------------------------------- 1 | #ifndef _FF_LUA_H_ 2 | #define _FF_LUA_H_ 3 | 4 | #ifndef _WIN32 5 | #include 6 | #endif 7 | 8 | #include 9 | #include 10 | 11 | #include 12 | using namespace std; 13 | 14 | #include "lua/fflua_type.h" 15 | #include "lua/fflua_register.h" 16 | 17 | namespace ff 18 | { 19 | //! 表示void类型,由于void类型不能return,用void_ignore_t适配 20 | template 21 | struct void_ignore_t; 22 | 23 | template 24 | struct void_ignore_t 25 | { 26 | typedef T value_t; 27 | }; 28 | 29 | template<> 30 | struct void_ignore_t 31 | { 32 | typedef cpp_void_t value_t; 33 | }; 34 | 35 | #define RET_V typename void_ignore_t::value_t 36 | 37 | class fflua_t 38 | { 39 | enum STACK_MIN_NUM_e 40 | { 41 | STACK_MIN_NUM = 20 42 | }; 43 | public: 44 | fflua_t(bool b = false): 45 | m_ls(NULL), 46 | m_bEnableModFunc(b) 47 | { 48 | m_ls = ::luaL_newstate(); 49 | ::luaL_openlibs(m_ls); 50 | } 51 | virtual ~fflua_t() 52 | { 53 | if (m_ls) 54 | { 55 | ::lua_close(m_ls); 56 | m_ls = NULL; 57 | } 58 | } 59 | void dump_stack() const { fflua_tool_t::dump_stack(m_ls); } 60 | void setModFuncFlag(bool b) { m_bEnableModFunc = b; } 61 | 62 | lua_State* get_lua_state() 63 | { 64 | return m_ls; 65 | } 66 | 67 | int add_package_path(const string& str_) 68 | { 69 | string new_path = "package.path = package.path .. \""; 70 | if (str_.empty()) 71 | { 72 | return -1; 73 | } 74 | 75 | if (str_[0] != ';') 76 | { 77 | new_path += ";"; 78 | } 79 | 80 | new_path += str_; 81 | 82 | if (str_[str_.length() - 1] != '/') 83 | { 84 | new_path += "/"; 85 | } 86 | 87 | new_path += "?.lua\" "; 88 | 89 | run_string(new_path); 90 | return 0; 91 | } 92 | int load_file(const string& file_name_)// 93 | { 94 | if (luaL_dofile(m_ls, file_name_.c_str())) 95 | { 96 | string err = fflua_tool_t::dump_error(m_ls, "cannot load file<%s>", file_name_.c_str()); 97 | ::lua_pop(m_ls, 1); 98 | throw lua_exception_t(err); 99 | } 100 | 101 | return 0; 102 | } 103 | template 104 | void open_lib(T arg_); 105 | 106 | void run_string(const char* str_) 107 | { 108 | if (luaL_dostring(m_ls, str_)) 109 | { 110 | string err = fflua_tool_t::dump_error(m_ls, "fflua_t::run_string ::lua_pcall failed str<%s>", str_); 111 | ::lua_pop(m_ls, 1); 112 | throw lua_exception_t(err); 113 | } 114 | } 115 | void run_string(const string& str_) 116 | { 117 | run_string(str_.c_str()); 118 | } 119 | 120 | 121 | 122 | template 123 | int get_global_variable(const string& field_name_, T& ret_); 124 | template 125 | int get_global_variable(const char* field_name_, T& ret_); 126 | 127 | template 128 | int set_global_variable(const string& field_name_, const T& value_); 129 | template 130 | int set_global_variable(const char* field_name_, const T& value_); 131 | 132 | void register_raw_function(const char* func_name_, lua_function_t func_) 133 | { 134 | lua_checkstack(m_ls, STACK_MIN_NUM); 135 | 136 | lua_pushcfunction(m_ls, func_); 137 | lua_setglobal(m_ls, func_name_); 138 | } 139 | 140 | template 141 | void reg(T a); 142 | 143 | void call(const char* func_name_) 144 | { 145 | ::lua_getglobal(m_ls, func_name_); 146 | 147 | if (::lua_pcall(m_ls, 0, 0, 0) != 0) 148 | { 149 | string err = fflua_tool_t::dump_error(m_ls, "lua_pcall failed func_name<%s>", func_name_); 150 | ::lua_pop(m_ls, 1); 151 | throw lua_exception_t(err); 152 | } 153 | } 154 | 155 | template 156 | RET_V call(const char* func_name_) ; 157 | 158 | template 159 | RET_V call(const char* func_name_, const ARG1& arg1_) ; 160 | 161 | template 162 | RET_V call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_) ; 163 | 164 | template 165 | RET_V call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, 166 | const ARG3& arg3_) ; 167 | 168 | template 169 | RET_V call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, 170 | const ARG4& arg4_) ; 171 | 172 | template 174 | RET_V call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, 175 | const ARG4& arg4_, const ARG5& arg5_) ; 176 | 177 | template 179 | RET_V call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, 180 | const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_) ; 181 | 182 | template 184 | RET_V call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, 185 | const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_, 186 | const ARG7& arg7_) ; 187 | 188 | template 190 | RET_V call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, 191 | const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_, 192 | const ARG8& arg8_) ; 193 | 194 | template 196 | RET_V call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, 197 | const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_, 198 | const ARG8& arg8_, const ARG9& arg9_) ; 199 | 200 | private: 201 | int getFuncByName(const char* func_name_) 202 | { 203 | if (false == m_bEnableModFunc) 204 | { 205 | lua_getglobal(m_ls, func_name_); 206 | return 0; 207 | } 208 | char tmpBuff[512] = {0}; 209 | char* begin = tmpBuff; 210 | for (unsigned int i = 0; i < sizeof(tmpBuff); ++i) 211 | { 212 | char c = func_name_[i]; 213 | tmpBuff[i] = c; 214 | if (c == '\0') 215 | { 216 | break; 217 | } 218 | 219 | if (c == '.') 220 | { 221 | tmpBuff[i] = '\0'; 222 | lua_getglobal(m_ls, lua_string_tool_t::c_str(begin)); 223 | const char* begin2 = func_name_ + i + 1; 224 | lua_getfield(m_ls, -1, begin2); 225 | lua_remove(m_ls, -2); 226 | return 0; 227 | } 228 | else if (c == ':') 229 | { 230 | tmpBuff[i] = '\0'; 231 | lua_getglobal(m_ls, begin); 232 | const char* begin2 = func_name_ + i + 1; 233 | lua_getfield(m_ls, -1, begin2); 234 | lua_pushvalue(m_ls, -2); 235 | lua_remove(m_ls, -3); 236 | return 1; 237 | } 238 | } 239 | 240 | lua_getglobal(m_ls, func_name_); 241 | return 0; 242 | } 243 | 244 | private: 245 | lua_State* m_ls; 246 | bool m_bEnableModFunc; 247 | }; 248 | 249 | template 250 | void fflua_t::open_lib(T arg_) 251 | { 252 | arg_(m_ls); 253 | } 254 | 255 | template 256 | int fflua_t::get_global_variable(const string& field_name_, T& ret_) 257 | { 258 | return get_global_variable(field_name_.c_str(), ret_); 259 | } 260 | 261 | template 262 | int fflua_t::get_global_variable(const char* field_name_, T& ret_) 263 | { 264 | int ret = 0; 265 | 266 | lua_getglobal(m_ls, field_name_); 267 | ret = lua_op_t::get_ret_value(m_ls, -1, ret_); 268 | 269 | lua_pop(m_ls, 1); 270 | return ret; 271 | } 272 | 273 | template 274 | int fflua_t::set_global_variable(const string& field_name_, const T& value_) 275 | { 276 | return set_global_variable(field_name_.c_str(), value_); 277 | } 278 | 279 | template 280 | int fflua_t::set_global_variable(const char* field_name_, const T& value_) 281 | { 282 | lua_op_t::push_stack(m_ls, value_); 283 | lua_setglobal(m_ls, field_name_); 284 | return 0; 285 | } 286 | 287 | template 288 | void fflua_t::reg(T a) 289 | { 290 | a(this->get_lua_state()); 291 | } 292 | 293 | //! impl for common RET 294 | template 295 | RET_V fflua_t::call(const char* func_name_) 296 | { 297 | RET_V ret = init_value_traits_t::value(); 298 | 299 | int tmpArg = getFuncByName(func_name_); 300 | 301 | if (lua_pcall(m_ls, tmpArg + 0, 1, 0) != 0) 302 | { 303 | string err = fflua_tool_t::dump_error(m_ls, "lua_pcall failed func_name<%s>", func_name_); 304 | lua_pop(m_ls, 1); 305 | throw lua_exception_t(err); 306 | } 307 | 308 | if (lua_op_t::get_ret_value(m_ls, -1, ret)) 309 | { 310 | lua_pop(m_ls, 1); 311 | char buff[512]; 312 | SPRINTF_F(buff, sizeof(buff), "callfunc [arg0] get_ret_value failed func_name<%s>", func_name_); 313 | throw lua_exception_t(buff); 314 | } 315 | 316 | lua_pop(m_ls, 1); 317 | 318 | return ret; 319 | } 320 | 321 | 322 | template 323 | RET_V fflua_t::call(const char* func_name_, const ARG1& arg1_) 324 | { 325 | RET_V ret = init_value_traits_t::value(); 326 | 327 | int tmpArg = getFuncByName(func_name_); 328 | 329 | lua_op_t::push_stack(m_ls, arg1_); 330 | 331 | if (lua_pcall(m_ls, tmpArg + 1, 1, 0) != 0) 332 | { 333 | string err = fflua_tool_t::dump_error(m_ls, "lua_pcall failed func_name<%s>", func_name_); 334 | lua_pop(m_ls, 1); 335 | throw lua_exception_t(err); 336 | } 337 | 338 | if (lua_op_t::get_ret_value(m_ls, -1, ret)) 339 | { 340 | lua_pop(m_ls, 1); 341 | char buff[512]; 342 | SPRINTF_F(buff, sizeof(buff), "callfunc [arg1] get_ret_value failed func_name<%s>", func_name_); 343 | throw lua_exception_t(buff); 344 | } 345 | 346 | lua_pop(m_ls, 1); 347 | 348 | return ret; 349 | } 350 | 351 | 352 | template 353 | RET_V fflua_t::call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_) 354 | 355 | { 356 | RET_V ret = init_value_traits_t::value(); 357 | 358 | int tmpArg = getFuncByName(func_name_); 359 | 360 | lua_op_t::push_stack(m_ls, arg1_); 361 | lua_op_t::push_stack(m_ls, arg2_); 362 | 363 | if (lua_pcall(m_ls, tmpArg + 2, 1, 0) != 0) 364 | { 365 | string err = fflua_tool_t::dump_error(m_ls, "lua_pcall failed func_name<%s>", func_name_); 366 | lua_pop(m_ls, 1); 367 | throw lua_exception_t(err); 368 | } 369 | 370 | if (lua_op_t::get_ret_value(m_ls, -1, ret)) 371 | { 372 | lua_pop(m_ls, 1); 373 | char buff[512]; 374 | SPRINTF_F(buff, sizeof(buff), "callfunc [arg2] get_ret_value failed func_name<%s>", func_name_); 375 | throw lua_exception_t(buff); 376 | } 377 | 378 | lua_pop(m_ls, 1); 379 | 380 | return ret; 381 | } 382 | 383 | template 384 | RET_V fflua_t::call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, 385 | const ARG3& arg3_) 386 | { 387 | RET_V ret = init_value_traits_t::value(); 388 | 389 | int tmpArg = getFuncByName(func_name_); 390 | 391 | lua_op_t::push_stack(m_ls, arg1_); 392 | lua_op_t::push_stack(m_ls, arg2_); 393 | lua_op_t::push_stack(m_ls, arg3_); 394 | 395 | if (lua_pcall(m_ls, tmpArg + 3, 1, 0) != 0) 396 | { 397 | string err = fflua_tool_t::dump_error(m_ls, "lua_pcall failed func_name<%s>", func_name_); 398 | lua_pop(m_ls, 1); 399 | throw lua_exception_t(err); 400 | } 401 | 402 | if (lua_op_t::get_ret_value(m_ls, -1, ret)) 403 | { 404 | lua_pop(m_ls, 1); 405 | char buff[512]; 406 | SPRINTF_F(buff, sizeof(buff), "callfunc [arg3] get_ret_value failed func_name<%s>", func_name_); 407 | throw lua_exception_t(buff); 408 | } 409 | 410 | lua_pop(m_ls, 1); 411 | 412 | return ret; 413 | } 414 | 415 | template 416 | RET_V fflua_t::call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, 417 | const ARG4& arg4_) 418 | { 419 | RET_V ret = init_value_traits_t::value(); 420 | 421 | int tmpArg = getFuncByName(func_name_); 422 | 423 | lua_op_t::push_stack(m_ls, arg1_); 424 | lua_op_t::push_stack(m_ls, arg2_); 425 | lua_op_t::push_stack(m_ls, arg3_); 426 | lua_op_t::push_stack(m_ls, arg4_); 427 | 428 | if (lua_pcall(m_ls, tmpArg + 4, 1, 0) != 0) 429 | { 430 | string err = fflua_tool_t::dump_error(m_ls, "lua_pcall failed func_name<%s>", func_name_); 431 | lua_pop(m_ls, 1); 432 | throw lua_exception_t(err); 433 | } 434 | 435 | if (lua_op_t::get_ret_value(m_ls, -1, ret)) 436 | { 437 | lua_pop(m_ls, 1); 438 | char buff[512]; 439 | SPRINTF_F(buff, sizeof(buff), "callfunc [arg4] get_ret_value failed func_name<%s>", func_name_); 440 | throw lua_exception_t(buff); 441 | } 442 | 443 | lua_pop(m_ls, 1); 444 | 445 | return ret; 446 | } 447 | 448 | template 449 | RET_V fflua_t::call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, 450 | const ARG4& arg4_, const ARG5& arg5_) 451 | { 452 | RET_V ret = init_value_traits_t::value(); 453 | 454 | int tmpArg = getFuncByName(func_name_); 455 | 456 | lua_op_t::push_stack(m_ls, arg1_); 457 | lua_op_t::push_stack(m_ls, arg2_); 458 | lua_op_t::push_stack(m_ls, arg3_); 459 | lua_op_t::push_stack(m_ls, arg4_); 460 | lua_op_t::push_stack(m_ls, arg5_); 461 | 462 | if (lua_pcall(m_ls, tmpArg + 5, 1, 0) != 0) 463 | { 464 | string err = fflua_tool_t::dump_error(m_ls, "lua_pcall failed func_name<%s>", func_name_); 465 | lua_pop(m_ls, 1); 466 | throw lua_exception_t(err); 467 | } 468 | 469 | if (lua_op_t::get_ret_value(m_ls, -1, ret)) 470 | { 471 | lua_pop(m_ls, 1); 472 | char buff[512]; 473 | SPRINTF_F(buff, sizeof(buff), "callfunc [arg5] get_ret_value failed func_name<%s>", func_name_); 474 | throw lua_exception_t(buff); 475 | } 476 | 477 | lua_pop(m_ls, 1); 478 | 479 | return ret; 480 | } 481 | 482 | 483 | template 484 | RET_V fflua_t::call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, 485 | const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_) 486 | { 487 | RET_V ret = init_value_traits_t::value(); 488 | 489 | int tmpArg = getFuncByName(func_name_); 490 | 491 | lua_op_t::push_stack(m_ls, arg1_); 492 | lua_op_t::push_stack(m_ls, arg2_); 493 | lua_op_t::push_stack(m_ls, arg3_); 494 | lua_op_t::push_stack(m_ls, arg4_); 495 | lua_op_t::push_stack(m_ls, arg5_); 496 | lua_op_t::push_stack(m_ls, arg6_); 497 | 498 | if (lua_pcall(m_ls, tmpArg + 6, 1, 0) != 0) 499 | { 500 | string err = fflua_tool_t::dump_error(m_ls, "lua_pcall failed func_name<%s>", func_name_); 501 | lua_pop(m_ls, 1); 502 | throw lua_exception_t(err); 503 | } 504 | 505 | if (lua_op_t::get_ret_value(m_ls, -1, ret)) 506 | { 507 | lua_pop(m_ls, 1); 508 | char buff[512]; 509 | SPRINTF_F(buff, sizeof(buff), "callfunc [arg6] get_ret_value failed func_name<%s>", func_name_); 510 | throw lua_exception_t(buff); 511 | } 512 | 513 | lua_pop(m_ls, 1); 514 | 515 | return ret; 516 | } 517 | 518 | 519 | template 521 | RET_V fflua_t::call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, 522 | const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_, 523 | const ARG7& arg7_) 524 | { 525 | RET_V ret = init_value_traits_t::value(); 526 | 527 | int tmpArg = getFuncByName(func_name_); 528 | 529 | lua_op_t::push_stack(m_ls, arg1_); 530 | lua_op_t::push_stack(m_ls, arg2_); 531 | lua_op_t::push_stack(m_ls, arg3_); 532 | lua_op_t::push_stack(m_ls, arg4_); 533 | lua_op_t::push_stack(m_ls, arg5_); 534 | lua_op_t::push_stack(m_ls, arg6_); 535 | lua_op_t::push_stack(m_ls, arg7_); 536 | 537 | if (lua_pcall(m_ls, tmpArg + 7, 1, 0) != 0) 538 | { 539 | string err = fflua_tool_t::dump_error(m_ls, "lua_pcall failed func_name<%s>", func_name_); 540 | lua_pop(m_ls, 1); 541 | throw lua_exception_t(err); 542 | } 543 | 544 | if (lua_op_t::get_ret_value(m_ls, -1, ret)) 545 | { 546 | lua_pop(m_ls, 1); 547 | char buff[512]; 548 | SPRINTF_F(buff, sizeof(buff), "callfunc [arg7] get_ret_value failed func_name<%s>", func_name_); 549 | throw lua_exception_t(buff); 550 | } 551 | 552 | lua_pop(m_ls, 1); 553 | 554 | return ret; 555 | } 556 | 557 | 558 | template 560 | RET_V fflua_t::call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, 561 | const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_, 562 | const ARG8& arg8_) 563 | { 564 | RET_V ret = init_value_traits_t::value(); 565 | 566 | int tmpArg = getFuncByName(func_name_); 567 | 568 | lua_op_t::push_stack(m_ls, arg1_); 569 | lua_op_t::push_stack(m_ls, arg2_); 570 | lua_op_t::push_stack(m_ls, arg3_); 571 | lua_op_t::push_stack(m_ls, arg4_); 572 | lua_op_t::push_stack(m_ls, arg5_); 573 | lua_op_t::push_stack(m_ls, arg6_); 574 | lua_op_t::push_stack(m_ls, arg7_); 575 | lua_op_t::push_stack(m_ls, arg8_); 576 | 577 | if (lua_pcall(m_ls, tmpArg + 8, 1, 0) != 0) 578 | { 579 | string err = fflua_tool_t::dump_error(m_ls, "lua_pcall failed func_name<%s>", func_name_); 580 | lua_pop(m_ls, 1); 581 | throw lua_exception_t(err); 582 | } 583 | 584 | if (lua_op_t::get_ret_value(m_ls, -1, ret)) 585 | { 586 | lua_pop(m_ls, 1); 587 | char buff[512]; 588 | SPRINTF_F(buff, sizeof(buff), "callfunc [arg8] get_ret_value failed func_name<%s>", func_name_); 589 | throw lua_exception_t(buff); 590 | } 591 | 592 | lua_pop(m_ls, 1); 593 | 594 | return ret; 595 | } 596 | 597 | 598 | template 600 | RET_V fflua_t::call(const char* func_name_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, 601 | const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_, 602 | const ARG8& arg8_, const ARG9& arg9_) 603 | { 604 | RET_V ret = init_value_traits_t::value(); 605 | 606 | int tmpArg = getFuncByName(func_name_); 607 | 608 | lua_op_t::push_stack(m_ls, arg1_); 609 | lua_op_t::push_stack(m_ls, arg2_); 610 | lua_op_t::push_stack(m_ls, arg3_); 611 | lua_op_t::push_stack(m_ls, arg4_); 612 | lua_op_t::push_stack(m_ls, arg5_); 613 | lua_op_t::push_stack(m_ls, arg6_); 614 | lua_op_t::push_stack(m_ls, arg7_); 615 | lua_op_t::push_stack(m_ls, arg8_); 616 | lua_op_t::push_stack(m_ls, arg9_); 617 | 618 | if (lua_pcall(m_ls, tmpArg + 9, 1, 0) != 0) 619 | { 620 | string err = fflua_tool_t::dump_error(m_ls, "lua_pcall failed func_name<%s>", func_name_); 621 | lua_pop(m_ls, 1); 622 | throw lua_exception_t(err); 623 | } 624 | 625 | if (lua_op_t::get_ret_value(m_ls, -1, ret)) 626 | { 627 | lua_pop(m_ls, 1); 628 | char buff[512]; 629 | SPRINTF_F(buff, sizeof(buff), "callfunc [arg9] get_ret_value failed func_name<%s>", func_name_); 630 | throw lua_exception_t(buff); 631 | } 632 | 633 | lua_pop(m_ls, 1); 634 | 635 | return ret; 636 | } 637 | 638 | } 639 | #endif 640 | -------------------------------------------------------------------------------- /lua/fflua_type.h: -------------------------------------------------------------------------------- 1 | #ifndef _FF_LUA_TYPE_H_ 2 | #define _FF_LUA_TYPE_H_ 3 | 4 | 5 | #ifndef _WIN32 6 | #include 7 | #define SPRINTF_F snprintf 8 | #else 9 | #include 10 | /* 11 | typedef long int64_t; 12 | typedef unsigned long uint64_t; 13 | typedef int int32_t; 14 | typedef unsigned int uint32_t; 15 | typedef short int16_t; 16 | typedef unsigned short uint16_t; 17 | typedef char int8_t; 18 | typedef unsigned char uint8_t; 19 | */ 20 | #define SPRINTF_F _snprintf_s 21 | 22 | struct strtoll_tool_t 23 | { 24 | static long do_strtoll(const char* s, const char*, int){ 25 | return atol(s); 26 | } 27 | }; 28 | #define strtoll strtoll_tool_t::do_strtoll 29 | #define strtoull (unsigned long)strtoll_tool_t::do_strtoll 30 | #endif 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | using namespace std; 42 | 43 | namespace ff 44 | { 45 | 46 | #define INHERIT_TABLE "inherit_table" 47 | 48 | struct cpp_void_t{}; 49 | 50 | struct lua_string_tool_t 51 | { 52 | inline static const char* c_str(const string& s_) { return s_.c_str(); } 53 | inline static const char* c_str(const char* s_) { return s_; } 54 | }; 55 | 56 | class lua_exception_t: public exception 57 | { 58 | public: 59 | explicit lua_exception_t(const char* err_): 60 | m_err(err_) 61 | {} 62 | explicit lua_exception_t(const string& err_): 63 | m_err(err_) 64 | { 65 | } 66 | ~lua_exception_t() throw (){} 67 | 68 | const char* what() const throw () { return m_err.c_str(); } 69 | private: 70 | string m_err; 71 | }; 72 | 73 | class fflua_tool_t 74 | { 75 | public: 76 | static void dump_stack(lua_State* ls_) 77 | { 78 | int i; 79 | int top = lua_gettop(ls_); 80 | 81 | for (i = 1; i <= top; i++) 82 | { 83 | int t = lua_type(ls_, i); 84 | switch (t) 85 | { 86 | case LUA_TSTRING: 87 | { 88 | printf("`%s'", lua_tostring(ls_, i)); 89 | } 90 | break; 91 | case LUA_TBOOLEAN: 92 | { 93 | printf(lua_toboolean(ls_, i) ? "true" : "false"); 94 | } 95 | break; 96 | case LUA_TNUMBER: 97 | { 98 | printf("`%g`", lua_tonumber(ls_, i)); 99 | } 100 | break; 101 | case LUA_TTABLE: 102 | { 103 | printf("table end\n"); 104 | lua_pushnil(ls_); 105 | while (lua_next(ls_, i) != 0) { 106 | printf(" %s - %s\n", 107 | lua_typename(ls_, lua_type(ls_, -2)), 108 | lua_typename(ls_, lua_type(ls_, -1))); 109 | lua_pop(ls_, 1); 110 | } 111 | printf("table end"); 112 | } 113 | break; 114 | default: 115 | { 116 | printf("`%s`", lua_typename(ls_, t)); 117 | } 118 | break; 119 | } 120 | printf(" "); 121 | } 122 | printf("\n"); 123 | } 124 | static string dump_error(lua_State* ls_, const char *fmt, ...) 125 | { 126 | string ret; 127 | char buff[1024]; 128 | 129 | va_list argp; 130 | va_start(argp, fmt); 131 | #ifndef _WIN32 132 | vsnprintf(buff, sizeof(buff), fmt, argp); 133 | #else 134 | vsnprintf_s(buff, sizeof(buff), sizeof(buff), fmt, argp); 135 | #endif 136 | va_end(argp); 137 | 138 | ret = buff; 139 | SPRINTF_F(buff, sizeof(buff), " tracback:%s", lua_tostring(ls_, -1)); 140 | ret += buff; 141 | 142 | return ret; 143 | } 144 | }; 145 | 146 | typedef int (*lua_function_t) (lua_State *L); 147 | 148 | class lua_nil_t{}; 149 | 150 | template 151 | struct userdata_for_object_t 152 | { 153 | userdata_for_object_t(T* p_ = NULL): obj(p_){} 154 | T* obj; 155 | }; 156 | 157 | 158 | template 159 | struct lua_type_info_t 160 | { 161 | static void set_name(const string& name_, string inherit_name_ = "") 162 | { 163 | size_t n = name_.length() > sizeof(name) - 1? sizeof(name) - 1: name_.length(); 164 | #ifndef _WIN32 165 | ::strncpy(name, name_.c_str(), n); 166 | #else 167 | ::strncpy_s(name, name_.c_str(), n); 168 | #endif 169 | if (false == inherit_name_.empty()) 170 | { 171 | n = inherit_name_.length() > sizeof(inherit_name) - 1? sizeof(inherit_name) - 1: inherit_name_.length(); 172 | #ifndef _WIN32 173 | ::strncpy(inherit_name, inherit_name_.c_str(), n); 174 | #else 175 | ::strncpy_s(inherit_name, inherit_name_.c_str(), n); 176 | #endif 177 | } 178 | } 179 | inline static const char* get_name() 180 | { 181 | return name; 182 | } 183 | inline static const char* get_inherit_name() 184 | { 185 | return inherit_name; 186 | } 187 | inline static bool is_registed() 188 | { 189 | return name[0] != '\0'; 190 | } 191 | inline static bool is_inherit() 192 | { 193 | return inherit_name[0] != '\0'; 194 | } 195 | static char name[128]; 196 | static char inherit_name[128]; 197 | }; 198 | template 199 | char lua_type_info_t::name[128] = {0}; 200 | template 201 | char lua_type_info_t::inherit_name[128] = {0}; 202 | 203 | 204 | template 205 | struct basetype_ptr_traits_t; 206 | template<> 207 | struct basetype_ptr_traits_t 208 | { 209 | typedef string arg_type_t; 210 | }; 211 | template<> 212 | struct basetype_ptr_traits_t 213 | { 214 | typedef string arg_type_t; 215 | }; 216 | template<> 217 | struct basetype_ptr_traits_t 218 | { 219 | typedef string arg_type_t; 220 | }; 221 | template<> 222 | struct basetype_ptr_traits_t 223 | { 224 | typedef char* arg_type_t; 225 | }; 226 | template<> 227 | struct basetype_ptr_traits_t 228 | { 229 | typedef char* arg_type_t; 230 | }; 231 | template<> 232 | struct basetype_ptr_traits_t 233 | { 234 | typedef char arg_type_t; 235 | }; 236 | template<> 237 | struct basetype_ptr_traits_t 238 | { 239 | typedef unsigned char arg_type_t; 240 | }; 241 | template<> 242 | struct basetype_ptr_traits_t 243 | { 244 | typedef short arg_type_t; 245 | }; 246 | template<> 247 | struct basetype_ptr_traits_t 248 | { 249 | typedef unsigned short arg_type_t; 250 | }; 251 | template<> 252 | struct basetype_ptr_traits_t 253 | { 254 | typedef int arg_type_t; 255 | }; 256 | template<> 257 | struct basetype_ptr_traits_t 258 | { 259 | typedef unsigned int arg_type_t; 260 | }; 261 | template<> 262 | struct basetype_ptr_traits_t 263 | { 264 | typedef long arg_type_t; 265 | }; 266 | template<> 267 | struct basetype_ptr_traits_t 268 | { 269 | typedef unsigned long arg_type_t; 270 | }; 271 | template<> 272 | struct basetype_ptr_traits_t 273 | { 274 | typedef long long arg_type_t; 275 | }; 276 | template<> 277 | struct basetype_ptr_traits_t 278 | { 279 | typedef unsigned long long arg_type_t; 280 | }; 281 | 282 | template<> 283 | struct basetype_ptr_traits_t 284 | { 285 | typedef float arg_type_t; 286 | }; 287 | template<> 288 | struct basetype_ptr_traits_t 289 | { 290 | typedef bool arg_type_t; 291 | }; 292 | 293 | template<> 294 | struct basetype_ptr_traits_t 295 | { 296 | typedef double arg_type_t; 297 | }; 298 | template 299 | struct basetype_ptr_traits_t 300 | { 301 | typedef T* arg_type_t; 302 | }; 303 | template 304 | struct basetype_ptr_traits_t 305 | { 306 | typedef T* arg_type_t; 307 | }; 308 | template 309 | struct basetype_ptr_traits_t 310 | { 311 | typedef T* arg_type_t; 312 | }; 313 | template 314 | struct basetype_ptr_traits_t 315 | { 316 | typedef T* arg_type_t; 317 | }; 318 | template 319 | struct basetype_ptr_traits_t > 320 | { 321 | typedef vector arg_type_t; 322 | }; 323 | template 324 | struct basetype_ptr_traits_t > 325 | { 326 | typedef list arg_type_t; 327 | }; 328 | 329 | template 330 | struct basetype_ptr_traits_t > 331 | { 332 | typedef deque arg_type_t; 333 | }; 334 | template 335 | struct basetype_ptr_traits_t &> 336 | { 337 | typedef deque arg_type_t; 338 | }; 339 | template 340 | struct basetype_ptr_traits_t&> 341 | { 342 | typedef deque arg_type_t; 343 | }; 344 | template 345 | struct basetype_ptr_traits_t > 346 | { 347 | typedef set arg_type_t; 348 | }; 349 | template 350 | struct basetype_ptr_traits_t > 351 | { 352 | typedef map arg_type_t; 353 | }; 354 | template 355 | struct basetype_ptr_traits_t &> 356 | { 357 | typedef vector arg_type_t; 358 | }; 359 | template 360 | struct basetype_ptr_traits_t &> 361 | { 362 | typedef list arg_type_t; 363 | }; 364 | template 365 | struct basetype_ptr_traits_t &> 366 | { 367 | typedef set arg_type_t; 368 | }; 369 | template 370 | struct basetype_ptr_traits_t &> 371 | { 372 | typedef map arg_type_t; 373 | }; 374 | template 375 | struct basetype_ptr_traits_t &> 376 | { 377 | typedef vector arg_type_t; 378 | }; 379 | template 380 | struct basetype_ptr_traits_t &> 381 | { 382 | typedef list arg_type_t; 383 | }; 384 | template 385 | struct basetype_ptr_traits_t &> 386 | { 387 | typedef set arg_type_t; 388 | }; 389 | template 390 | struct basetype_ptr_traits_t &> 391 | { 392 | typedef map arg_type_t; 393 | }; 394 | 395 | 396 | //!------------------------------------------------------------------------------------------------------------------------- 397 | template 398 | struct p_t; 399 | 400 | template 401 | struct p_t 402 | { 403 | static ARG_TYPE r(ARG_TYPE a) { return a; } 404 | static ARG_TYPE& r(ARG_TYPE* a) { return *a; } 405 | }; 406 | template 407 | struct p_t 408 | { 409 | static ARG_TYPE& r(ARG_TYPE& a) { return a; } 410 | static ARG_TYPE& r(ARG_TYPE* a) { return *a; } 411 | }; 412 | //!######################################################################################################################### 413 | template 414 | struct reference_traits_t; 415 | 416 | template 417 | struct reference_traits_t 418 | { 419 | typedef ARG_TYPE arg_type_t; 420 | }; 421 | 422 | template<> 423 | struct reference_traits_t 424 | { 425 | typedef string arg_type_t; 426 | }; 427 | 428 | template<> 429 | struct reference_traits_t 430 | { 431 | typedef string arg_type_t; 432 | }; 433 | 434 | template 435 | struct reference_traits_t 436 | { 437 | typedef T* arg_type_t; 438 | }; 439 | template 440 | struct reference_traits_t 441 | { 442 | typedef T arg_type_t; 443 | }; 444 | 445 | template<> 446 | struct reference_traits_t 447 | { 448 | typedef char* arg_type_t; 449 | }; 450 | 451 | template 452 | struct init_value_traits_t; 453 | 454 | template 455 | struct init_value_traits_t 456 | { 457 | inline static T value(){ return T(); } 458 | }; 459 | 460 | template 461 | struct init_value_traits_t 462 | { 463 | inline static T* value(){ return NULL; } 464 | }; 465 | 466 | template 467 | struct init_value_traits_t 468 | { 469 | inline static T value(){ return T(); } 470 | }; 471 | 472 | template <> 473 | struct init_value_traits_t 474 | { 475 | inline static const char* value(){ return ""; } 476 | }; 477 | 478 | template <> 479 | struct init_value_traits_t 480 | { 481 | inline static const char* value(){ return ""; } 482 | }; 483 | 484 | template 485 | struct lua_op_t 486 | { 487 | static void push_stack(lua_State* ls_, const T& arg_) 488 | { 489 | void* ptr = lua_newuserdata(ls_, sizeof(userdata_for_object_t)); 490 | new (ptr) userdata_for_object_t((T*)&arg_); 491 | 492 | luaL_getmetatable(ls_, lua_type_info_t::get_name()); 493 | lua_setmetatable(ls_, -2); 494 | //T* ptmp = (T*)&arg_; 495 | //lua_op_t::push_stack(ls_, ptmp); 496 | } 497 | static int lua_to_value(lua_State* ls_, int pos_, T& param_) 498 | { 499 | if (false == lua_type_info_t::is_registed()) 500 | { 501 | string strErr = string("type not supported1:") + __FUNCTION__; 502 | luaL_argerror(ls_, pos_, strErr.c_str()); 503 | } 504 | void* arg_data = lua_touserdata(ls_, pos_); 505 | 506 | if (NULL == arg_data || 0 == lua_getmetatable(ls_, pos_)) 507 | { 508 | char buff[128]; 509 | SPRINTF_F(buff, sizeof(buff), "`%s` arg1 connot be null", 510 | lua_type_info_t::get_name()); 511 | luaL_argerror(ls_, pos_, buff); 512 | } 513 | 514 | luaL_getmetatable(ls_, lua_type_info_t::get_name()); 515 | if (0 == lua_rawequal(ls_, -1, -2)) 516 | { 517 | lua_getfield(ls_, -2, INHERIT_TABLE); 518 | if (0 == lua_rawequal(ls_, -1, -2)) 519 | { 520 | lua_pop(ls_, 3); 521 | char buff[128]; 522 | SPRINTF_F(buff, sizeof(buff), "`%s` arg1 type not equal", 523 | lua_type_info_t::get_name()); 524 | luaL_argerror(ls_, pos_, buff); 525 | } 526 | lua_pop(ls_, 3); 527 | } 528 | else 529 | { 530 | lua_pop(ls_, 2); 531 | } 532 | 533 | T* ret_ptr = ((userdata_for_object_t*)arg_data)->obj; 534 | if (NULL == ret_ptr) 535 | { 536 | char buff[128]; 537 | SPRINTF_F(buff, sizeof(buff), "`%s` object ptr can't be null", 538 | lua_type_info_t::get_name()); 539 | luaL_argerror(ls_, pos_, buff); 540 | } 541 | 542 | param_ = *ret_ptr; 543 | return 0; 544 | } 545 | }; 546 | 547 | 548 | template<> 549 | struct lua_op_t 550 | { 551 | static void push_stack(lua_State* ls_, const char* arg_) 552 | { 553 | lua_pushstring(ls_, arg_); 554 | } 555 | static int lua_to_value(lua_State* ls_, int pos_, char*& param_) 556 | { 557 | const char* str = luaL_checkstring(ls_, pos_); 558 | param_ = (char*)str; 559 | return 0; 560 | } 561 | }; 562 | template<> 563 | struct lua_op_t 564 | { 565 | static void push_stack(lua_State* ls_, const char* arg_) 566 | { 567 | lua_pushstring(ls_, arg_); 568 | } 569 | static int lua_to_value(lua_State* ls_, int pos_, char*& param_) 570 | { 571 | const char* str = luaL_checkstring(ls_, pos_); 572 | param_ = (char*)str; 573 | return 0; 574 | } 575 | }; 576 | 577 | template<> 578 | struct lua_op_t 579 | { 580 | static void push_stack(lua_State* ls_, const lua_nil_t& arg_) 581 | { 582 | lua_pushnil (ls_); 583 | } 584 | 585 | }; 586 | 587 | template<> 588 | struct lua_op_t 589 | { 590 | static int get_ret_value(lua_State* ls_, int pos_, cpp_void_t& param_) 591 | { 592 | return 0; 593 | } 594 | }; 595 | 596 | template<> 597 | struct lua_op_t 598 | { 599 | static void push_stack(lua_State* ls_, int64_t arg_) 600 | { 601 | #if LUA_VERSION_NUM >= 503 602 | lua_pushinteger(ls_, arg_); 603 | #else 604 | stringstream ss; 605 | ss << arg_; 606 | string str = ss.str(); 607 | lua_pushlstring(ls_, str.c_str(), str.length()); 608 | #endif 609 | } 610 | 611 | static int get_ret_value(lua_State* ls_, int pos_, int64_t& param_) 612 | { 613 | #if LUA_VERSION_NUM >= 503 614 | if (!lua_isinteger(ls_, pos_)) 615 | { 616 | return -1; 617 | } 618 | param_ = lua_tointeger(ls_, pos_); 619 | #else 620 | if (!lua_isstring(ls_, pos_)) 621 | { 622 | return -1; 623 | } 624 | 625 | size_t len = 0; 626 | const char* src = lua_tolstring(ls_, pos_, &len); 627 | param_ = (int64_t)strtoll(src, NULL, 10); 628 | #endif 629 | return 0; 630 | } 631 | 632 | static int lua_to_value(lua_State* ls_, int pos_, int64_t& param_) 633 | { 634 | #if LUA_VERSION_NUM >= 503 635 | param_ = luaL_checkinteger(ls_, pos_); 636 | #else 637 | size_t len = 0; 638 | const char* str = luaL_checklstring(ls_, pos_, &len); 639 | param_ = (int64_t)strtoll(str, NULL, 10); 640 | #endif 641 | return 0; 642 | } 643 | }; 644 | 645 | template<> struct lua_op_t 646 | { 647 | static void push_stack(lua_State* ls_, uint64_t arg_) 648 | { 649 | stringstream ss; 650 | ss << arg_; 651 | string str = ss.str(); 652 | lua_pushlstring(ls_, str.c_str(), str.length()); 653 | } 654 | 655 | static int get_ret_value(lua_State* ls_, int pos_, uint64_t& param_) 656 | { 657 | if (!lua_isstring(ls_, pos_)) 658 | { 659 | return -1; 660 | } 661 | 662 | size_t len = 0; 663 | const char* src = lua_tolstring(ls_, pos_, &len); 664 | param_ = (uint64_t)strtoull(src, NULL, 10); 665 | return 0; 666 | } 667 | 668 | static int lua_to_value(lua_State* ls_, int pos_, uint64_t& param_) 669 | { 670 | size_t len = 0; 671 | const char* str = luaL_checklstring(ls_, pos_, &len); 672 | param_ = (uint64_t)strtoull(str, NULL, 10); 673 | return 0; 674 | } 675 | }; 676 | 677 | template<> struct lua_op_t 678 | { 679 | 680 | static void push_stack(lua_State* ls_, int8_t arg_) 681 | { 682 | lua_pushnumber(ls_, (lua_Number)arg_); 683 | } 684 | static int get_ret_value(lua_State* ls_, int pos_, int8_t& param_) 685 | { 686 | if (!lua_isnumber(ls_, pos_)) 687 | { 688 | return -1; 689 | } 690 | param_ = (int8_t)lua_tonumber(ls_, pos_); 691 | return 0; 692 | } 693 | static int lua_to_value(lua_State* ls_, int pos_, int8_t& param_) 694 | { 695 | param_ = (int8_t)luaL_checknumber(ls_, pos_); 696 | return 0; 697 | } 698 | }; 699 | 700 | template<> 701 | struct lua_op_t 702 | { 703 | static void push_stack(lua_State* ls_, uint8_t arg_) 704 | { 705 | lua_pushnumber(ls_, (lua_Number)arg_); 706 | } 707 | static int get_ret_value(lua_State* ls_, int pos_, uint8_t& param_) 708 | { 709 | if (!lua_isnumber(ls_, pos_)) 710 | { 711 | return -1; 712 | } 713 | param_ = (uint8_t)lua_tonumber(ls_, pos_); 714 | return 0; 715 | } 716 | static int lua_to_value(lua_State* ls_, int pos_, uint8_t& param_) 717 | { 718 | param_ = (uint8_t)luaL_checknumber(ls_, pos_); 719 | return 0; 720 | } 721 | }; 722 | #ifdef _WIN32 723 | 724 | template<> struct lua_op_t 725 | { 726 | 727 | static void push_stack(lua_State* ls_, char arg_) 728 | { 729 | lua_pushnumber(ls_, (lua_Number)arg_); 730 | } 731 | static int get_ret_value(lua_State* ls_, int pos_, char& param_) 732 | { 733 | if (!lua_isnumber(ls_, pos_)) 734 | { 735 | return -1; 736 | } 737 | param_ = (char)lua_tonumber(ls_, pos_); 738 | return 0; 739 | } 740 | static int lua_to_value(lua_State* ls_, int pos_, char& param_) 741 | { 742 | param_ = (char)luaL_checknumber(ls_, pos_); 743 | return 0; 744 | } 745 | }; 746 | 747 | #endif 748 | template<> struct lua_op_t 749 | { 750 | static void push_stack(lua_State* ls_, int16_t arg_) 751 | { 752 | lua_pushnumber(ls_, (lua_Number)arg_); 753 | } 754 | static int get_ret_value(lua_State* ls_, int pos_, int16_t& param_) 755 | { 756 | if (!lua_isnumber(ls_, pos_)) 757 | { 758 | return -1; 759 | } 760 | param_ = (int16_t)lua_tonumber(ls_, pos_); 761 | return 0; 762 | } 763 | static int lua_to_value(lua_State* ls_, int pos_, int16_t& param_) 764 | { 765 | param_ = (int16_t)luaL_checknumber(ls_, pos_); 766 | return 0; 767 | } 768 | }; 769 | template<> struct lua_op_t 770 | { 771 | 772 | static void push_stack(lua_State* ls_, uint16_t arg_) 773 | { 774 | lua_pushnumber(ls_, (lua_Number)arg_); 775 | } 776 | static int get_ret_value(lua_State* ls_, int pos_, uint16_t& param_) 777 | { 778 | if (!lua_isnumber(ls_, pos_)) 779 | { 780 | return -1; 781 | } 782 | param_ = (uint16_t)lua_tonumber(ls_, pos_); 783 | return 0; 784 | } 785 | static int lua_to_value(lua_State* ls_, int pos_, uint16_t& param_) 786 | { 787 | param_ = (uint16_t)luaL_checknumber(ls_, pos_); 788 | return 0; 789 | } 790 | }; 791 | template<> struct lua_op_t 792 | { 793 | static void push_stack(lua_State* ls_, int32_t arg_) 794 | { 795 | lua_pushnumber(ls_, (lua_Number)arg_); 796 | } 797 | static int get_ret_value(lua_State* ls_, int pos_, int32_t& param_) 798 | { 799 | if (!lua_isnumber(ls_, pos_)) 800 | { 801 | return -1; 802 | } 803 | param_ = (int32_t)lua_tonumber(ls_, pos_); 804 | return 0; 805 | } 806 | static int lua_to_value(lua_State* ls_, int pos_, int32_t& param_) 807 | { 808 | param_ = (int32_t)luaL_checknumber(ls_, pos_); 809 | return 0; 810 | } 811 | }; 812 | template<> struct lua_op_t 813 | { 814 | 815 | static void push_stack(lua_State* ls_, uint32_t arg_) 816 | { 817 | lua_pushnumber(ls_, (lua_Number)arg_); 818 | } 819 | static int get_ret_value(lua_State* ls_, int pos_, uint32_t& param_) 820 | { 821 | if (!lua_isnumber(ls_, pos_)) 822 | { 823 | return -1; 824 | } 825 | param_ = (uint32_t)lua_tonumber(ls_, pos_); 826 | return 0; 827 | } 828 | static int lua_to_value(lua_State* ls_, int pos_, uint32_t& param_) 829 | { 830 | param_ = (uint32_t)luaL_checknumber(ls_, pos_); 831 | return 0; 832 | } 833 | }; 834 | 835 | template<> 836 | struct lua_op_t 837 | { 838 | static void push_stack(lua_State* ls_, bool arg_) 839 | { 840 | lua_pushboolean(ls_, arg_); 841 | } 842 | 843 | static int get_ret_value(lua_State* ls_, int pos_, bool& param_) 844 | { 845 | //! nil 自动转换为false 846 | if (lua_isnil(ls_, pos_)) 847 | { 848 | param_ = false; 849 | return 0; 850 | } 851 | if (!lua_isboolean(ls_, pos_)) 852 | { 853 | return -1; 854 | } 855 | 856 | param_ = (0 != lua_toboolean(ls_, pos_)); 857 | return 0; 858 | } 859 | static int lua_to_value(lua_State* ls_, int pos_, bool& param_) 860 | { 861 | luaL_checktype(ls_, pos_, LUA_TBOOLEAN); 862 | param_ = (0 != lua_toboolean(ls_, pos_)); 863 | return 0; 864 | } 865 | }; 866 | 867 | template<> 868 | struct lua_op_t 869 | { 870 | 871 | static void push_stack(lua_State* ls_, const string& arg_) 872 | { 873 | lua_pushlstring(ls_, arg_.c_str(), arg_.length()); 874 | } 875 | 876 | static int get_ret_value(lua_State* ls_, int pos_, string& param_) 877 | { 878 | if (!lua_isstring(ls_, pos_)) 879 | { 880 | return -1; 881 | } 882 | 883 | lua_pushvalue(ls_, pos_); 884 | size_t len = 0; 885 | const char* src = lua_tolstring(ls_, -1, &len); 886 | param_.assign(src, len); 887 | lua_pop(ls_, 1); 888 | 889 | return 0; 890 | } 891 | static int lua_to_value(lua_State* ls_, int pos_, string& param_) 892 | { 893 | size_t len = 0; 894 | const char* str = luaL_checklstring(ls_, pos_, &len); 895 | param_.assign(str, len); 896 | return 0; 897 | } 898 | }; 899 | 900 | template<> 901 | struct lua_op_t 902 | { 903 | static void push_stack(lua_State* ls_, const string& arg_) 904 | { 905 | lua_pushlstring(ls_, arg_.c_str(), arg_.length()); 906 | } 907 | 908 | static int get_ret_value(lua_State* ls_, int pos_, string& param_) 909 | { 910 | if (!lua_isstring(ls_, pos_)) 911 | { 912 | return -1; 913 | } 914 | 915 | lua_pushvalue(ls_, pos_); 916 | size_t len = 0; 917 | const char* src = lua_tolstring(ls_, -1, &len); 918 | param_.assign(src, len); 919 | lua_pop(ls_, 1); 920 | 921 | return 0; 922 | } 923 | static int lua_to_value(lua_State* ls_, int pos_, string& param_) 924 | { 925 | size_t len = 0; 926 | const char* str = luaL_checklstring(ls_, pos_, &len); 927 | param_.assign(str, len); 928 | return 0; 929 | } 930 | }; 931 | template<> struct lua_op_t 932 | { 933 | static void push_stack(lua_State* ls_, float arg_) 934 | { 935 | lua_pushnumber(ls_, (lua_Number)arg_); 936 | } 937 | static int get_ret_value(lua_State* ls_, int pos_, float& param_) 938 | { 939 | if (!lua_isnumber(ls_, pos_)) 940 | { 941 | return -1; 942 | } 943 | param_ = (float)lua_tonumber(ls_, pos_); 944 | return 0; 945 | } 946 | static int lua_to_value(lua_State* ls_, int pos_, float& param_) 947 | { 948 | param_ = (float)luaL_checknumber(ls_, pos_); 949 | return 0; 950 | } 951 | }; 952 | template<> struct lua_op_t 953 | { 954 | static void push_stack(lua_State* ls_, double arg_) 955 | { 956 | lua_pushnumber(ls_, (lua_Number)arg_); 957 | } 958 | static int get_ret_value(lua_State* ls_, int pos_, double& param_) 959 | { 960 | if (!lua_isnumber(ls_, pos_)) 961 | { 962 | return -1; 963 | } 964 | param_ = (double)lua_tonumber(ls_, pos_); 965 | return 0; 966 | } 967 | static int lua_to_value(lua_State* ls_, int pos_, double& param_) 968 | { 969 | param_ = (double)luaL_checknumber(ls_, pos_); 970 | return 0; 971 | } 972 | }; 973 | /*template<> struct lua_op_t 974 | { 975 | 976 | static void push_stack(lua_State* ls_, long arg_) 977 | { 978 | lua_pushnumber(ls_, (lua_Number)arg_); 979 | } 980 | static int get_ret_value(lua_State* ls_, int pos_, long& param_) 981 | { 982 | if (!lua_isnumber(ls_, pos_)) 983 | { 984 | return -1; 985 | } 986 | param_ = (long)lua_tonumber(ls_, pos_); 987 | return 0; 988 | } 989 | static int lua_to_value(lua_State* ls_, int pos_, long& param_) 990 | { 991 | param_ = (long)luaL_checknumber(ls_, pos_); 992 | return 0; 993 | } 994 | };*/ 995 | template<> 996 | struct lua_op_t 997 | { 998 | static void push_stack(lua_State* ls_, void* arg_) 999 | { 1000 | lua_pushlightuserdata(ls_, arg_); 1001 | } 1002 | 1003 | static int get_ret_value(lua_State* ls_, int pos_, void* & param_) 1004 | { 1005 | if (!lua_isuserdata(ls_, pos_)) 1006 | { 1007 | char buff[128]; 1008 | SPRINTF_F(buff, sizeof(buff), "userdata param expected, but type<%s> provided", 1009 | lua_typename(ls_, lua_type(ls_, pos_))); 1010 | printf("%s\n", buff); 1011 | return -1; 1012 | } 1013 | 1014 | param_ = lua_touserdata(ls_, pos_); 1015 | return 0; 1016 | } 1017 | 1018 | static int lua_to_value(lua_State* ls_, int pos_, void*& param_) 1019 | { 1020 | if (!lua_isuserdata(ls_, pos_)) 1021 | { 1022 | luaL_argerror (ls_, 1, "userdata param expected"); 1023 | return -1; 1024 | } 1025 | param_ = lua_touserdata(ls_, pos_); 1026 | return 0; 1027 | } 1028 | }; 1029 | 1030 | template 1031 | struct lua_op_t 1032 | { 1033 | static void push_stack(lua_State* ls_, T& arg_) 1034 | { 1035 | void* ptr = lua_newuserdata(ls_, sizeof(userdata_for_object_t)); 1036 | new (ptr) userdata_for_object_t(&arg_); 1037 | 1038 | luaL_getmetatable(ls_, lua_type_info_t::get_name()); 1039 | lua_setmetatable(ls_, -2); 1040 | } 1041 | static void push_stack(lua_State* ls_, const T& arg_) 1042 | { 1043 | void* ptr = lua_newuserdata(ls_, sizeof(userdata_for_object_t)); 1044 | new (ptr) userdata_for_object_t(&arg_); 1045 | 1046 | luaL_getmetatable(ls_, lua_type_info_t::get_name()); 1047 | lua_setmetatable(ls_, -2); 1048 | } 1049 | static void push_stack(lua_State* ls_, T* arg_) 1050 | { 1051 | void* ptr = lua_newuserdata(ls_, sizeof(userdata_for_object_t)); 1052 | new (ptr) userdata_for_object_t(arg_); 1053 | 1054 | luaL_getmetatable(ls_, lua_type_info_t::get_name()); 1055 | lua_setmetatable(ls_, -2); 1056 | } 1057 | 1058 | static int get_ret_value(lua_State* ls_, int pos_, T* & param_) 1059 | { 1060 | if (false == lua_type_info_t::is_registed()) 1061 | { 1062 | string strErr = string("type not supported2:") + __FUNCTION__; 1063 | luaL_argerror(ls_, pos_, strErr.c_str()); 1064 | } 1065 | 1066 | void *arg_data = lua_touserdata(ls_, pos_); 1067 | 1068 | if (NULL == arg_data) 1069 | { 1070 | printf("expect<%s> but <%s> NULL\n", lua_type_info_t::get_name(), lua_typename(ls_, lua_type(ls_, pos_))); 1071 | return -1; 1072 | } 1073 | 1074 | if (0 == lua_getmetatable(ls_, pos_)) 1075 | { 1076 | return -1; 1077 | } 1078 | 1079 | luaL_getmetatable(ls_, lua_type_info_t::get_name()); 1080 | if (0 == lua_rawequal(ls_, -1, -2)) 1081 | { 1082 | lua_getfield(ls_, -2, INHERIT_TABLE); 1083 | if (0 == lua_rawequal(ls_, -1, -2)) 1084 | { 1085 | printf("expect<%s> but <%s> not equal\n", lua_type_info_t::get_name(), lua_typename(ls_, lua_type(ls_, pos_))); 1086 | lua_pop(ls_, 3); 1087 | return -1; 1088 | } 1089 | lua_pop(ls_, 3); 1090 | } 1091 | else 1092 | { 1093 | lua_pop(ls_, 2); 1094 | } 1095 | T* ret_ptr = ((userdata_for_object_t*)arg_data)->obj; 1096 | if (NULL == ret_ptr) 1097 | { 1098 | return -1; 1099 | } 1100 | 1101 | param_ = ret_ptr; 1102 | return 0; 1103 | } 1104 | 1105 | static int lua_to_value(lua_State* ls_, int pos_, T*& param_) 1106 | { 1107 | if (false == lua_type_info_t::is_registed()) 1108 | { 1109 | string strErr = string("type not supported3:") + __FUNCTION__; 1110 | luaL_argerror(ls_, pos_, strErr.c_str()); 1111 | } 1112 | void *arg_data = lua_touserdata(ls_, pos_); 1113 | 1114 | if (NULL == arg_data || 0 == lua_getmetatable(ls_, pos_)) 1115 | { 1116 | char buff[128]; 1117 | SPRINTF_F(buff, sizeof(buff), "`%s` arg1 connot be null", 1118 | lua_type_info_t::get_name()); 1119 | luaL_argerror(ls_, pos_, buff); 1120 | } 1121 | 1122 | luaL_getmetatable(ls_, lua_type_info_t::get_name()); 1123 | if (0 == lua_rawequal(ls_, -1, -2)) 1124 | { 1125 | lua_getfield(ls_, -2, INHERIT_TABLE); 1126 | if (0 == lua_rawequal(ls_, -1, -2)) 1127 | { 1128 | lua_pop(ls_, 3); 1129 | char buff[128]; 1130 | SPRINTF_F(buff, sizeof(buff), "`%s` arg1 type not equal", 1131 | lua_type_info_t::get_name()); 1132 | luaL_argerror(ls_, pos_, buff); 1133 | } 1134 | lua_pop(ls_, 3); 1135 | } 1136 | else 1137 | { 1138 | lua_pop(ls_, 2); 1139 | } 1140 | 1141 | T* ret_ptr = ((userdata_for_object_t*)arg_data)->obj; 1142 | if (NULL == ret_ptr) 1143 | { 1144 | char buff[128]; 1145 | SPRINTF_F(buff, sizeof(buff), "`%s` object ptr can't be null", 1146 | lua_type_info_t::get_name()); 1147 | luaL_argerror(ls_, pos_, buff); 1148 | } 1149 | 1150 | param_ = ret_ptr; 1151 | return 0; 1152 | } 1153 | }; 1154 | 1155 | template 1156 | struct lua_op_t 1157 | { 1158 | static void push_stack(lua_State* ls_, const T* arg_) 1159 | { 1160 | lua_op_t::push_stack(ls_, (T*)arg_); 1161 | } 1162 | 1163 | static int get_ret_value(lua_State* ls_, int pos_, T* & param_) 1164 | { 1165 | return lua_op_t::get_ret_value(ls_, pos_, param_); 1166 | } 1167 | 1168 | static int lua_to_value(lua_State* ls_, int pos_, T*& param_) 1169 | { 1170 | return lua_op_t::lua_to_value(ls_, pos_, param_); 1171 | } 1172 | }; 1173 | 1174 | template 1175 | struct lua_op_t > 1176 | { 1177 | static void push_stack(lua_State* ls_, const vector& arg_) 1178 | { 1179 | lua_newtable(ls_); 1180 | typename vector::const_iterator it = arg_.begin(); 1181 | for (int i = 1; it != arg_.end(); ++it, ++i) 1182 | { 1183 | lua_op_t::push_stack(ls_, i); 1184 | lua_op_t::push_stack(ls_, *it); 1185 | lua_settable(ls_, -3); 1186 | } 1187 | } 1188 | 1189 | static int get_ret_value(lua_State* ls_, int pos_, vector& param_) 1190 | { 1191 | if (0 == lua_istable(ls_, pos_)) 1192 | { 1193 | return -1; 1194 | } 1195 | lua_pushnil(ls_); 1196 | int real_pos = pos_; 1197 | if (pos_ < 0) real_pos = real_pos - 1; 1198 | 1199 | while (lua_next(ls_, real_pos) != 0) 1200 | { 1201 | param_.push_back(T()); 1202 | if (lua_op_t::get_ret_value(ls_, -1, param_[param_.size() - 1]) < 0) 1203 | { 1204 | return -1; 1205 | } 1206 | lua_pop(ls_, 1); 1207 | } 1208 | return 0; 1209 | } 1210 | 1211 | static int lua_to_value(lua_State* ls_, int pos_, vector& param_) 1212 | { 1213 | luaL_checktype(ls_, pos_, LUA_TTABLE); 1214 | 1215 | lua_pushnil(ls_); 1216 | int real_pos = pos_; 1217 | if (pos_ < 0) real_pos = real_pos - 1; 1218 | while (lua_next(ls_, real_pos) != 0) 1219 | { 1220 | param_.push_back(T()); 1221 | if (lua_op_t::lua_to_value(ls_, -1, param_[param_.size() - 1]) < 0) 1222 | { 1223 | luaL_argerror(ls_, pos_>0?pos_:-pos_, "convert to vector failed"); 1224 | } 1225 | lua_pop(ls_, 1); 1226 | } 1227 | return 0; 1228 | } 1229 | }; 1230 | 1231 | template 1232 | struct lua_op_t > 1233 | { 1234 | static void push_stack(lua_State* ls_, const deque& arg_) 1235 | { 1236 | lua_newtable(ls_); 1237 | typename deque::const_iterator it = arg_.begin(); 1238 | for (int i = 1; it != arg_.end(); ++it, ++i) 1239 | { 1240 | lua_op_t::push_stack(ls_, i); 1241 | lua_op_t::push_stack(ls_, *it); 1242 | lua_settable(ls_, -3); 1243 | } 1244 | } 1245 | 1246 | static int get_ret_value(lua_State* ls_, int pos_, deque& param_) 1247 | { 1248 | if (0 == lua_istable(ls_, pos_)) 1249 | { 1250 | return -1; 1251 | } 1252 | lua_pushnil(ls_); 1253 | int real_pos = pos_; 1254 | if (pos_ < 0) real_pos = real_pos - 1; 1255 | 1256 | while (lua_next(ls_, real_pos) != 0) 1257 | { 1258 | param_.push_back(T()); 1259 | if (lua_op_t::get_ret_value(ls_, -1, (param_.back())) < 0) 1260 | { 1261 | return -1; 1262 | } 1263 | lua_pop(ls_, 1); 1264 | } 1265 | return 0; 1266 | } 1267 | 1268 | static int lua_to_value(lua_State* ls_, int pos_, deque& param_) 1269 | { 1270 | luaL_checktype(ls_, pos_, LUA_TTABLE); 1271 | 1272 | lua_pushnil(ls_); 1273 | int real_pos = pos_; 1274 | if (pos_ < 0) real_pos = real_pos - 1; 1275 | while (lua_next(ls_, real_pos) != 0) 1276 | { 1277 | param_.push_back(T()); 1278 | if (lua_op_t::lua_to_value(ls_, -1, (param_.back())) < 0) 1279 | { 1280 | luaL_argerror(ls_, pos_ > 0 ? pos_ : -pos_, "convert to vector failed"); 1281 | } 1282 | lua_pop(ls_, 1); 1283 | } 1284 | return 0; 1285 | } 1286 | }; 1287 | 1288 | 1289 | template 1290 | struct lua_op_t > 1291 | { 1292 | static void push_stack(lua_State* ls_, const list& arg_) 1293 | { 1294 | lua_newtable(ls_); 1295 | typename list::const_iterator it = arg_.begin(); 1296 | for (int i = 1; it != arg_.end(); ++it, ++i) 1297 | { 1298 | lua_op_t::push_stack(ls_, i); 1299 | lua_op_t::push_stack(ls_, *it); 1300 | lua_settable(ls_, -3); 1301 | } 1302 | } 1303 | 1304 | static int get_ret_value(lua_State* ls_, int pos_, list& param_) 1305 | { 1306 | if (0 == lua_istable(ls_, pos_)) 1307 | { 1308 | return -1; 1309 | } 1310 | lua_pushnil(ls_); 1311 | int real_pos = pos_; 1312 | if (pos_ < 0) real_pos = real_pos - 1; 1313 | 1314 | while (lua_next(ls_, real_pos) != 0) 1315 | { 1316 | param_.push_back(T()); 1317 | if (lua_op_t::get_ret_value(ls_, -1, (param_.back())) < 0) 1318 | { 1319 | return -1; 1320 | } 1321 | lua_pop(ls_, 1); 1322 | } 1323 | return 0; 1324 | } 1325 | 1326 | static int lua_to_value(lua_State* ls_, int pos_, list& param_) 1327 | { 1328 | luaL_checktype(ls_, pos_, LUA_TTABLE); 1329 | 1330 | lua_pushnil(ls_); 1331 | int real_pos = pos_; 1332 | if (pos_ < 0) real_pos = real_pos - 1; 1333 | while (lua_next(ls_, real_pos) != 0) 1334 | { 1335 | param_.push_back(T()); 1336 | if (lua_op_t::lua_to_value(ls_, -1, (param_.back())) < 0) 1337 | { 1338 | luaL_argerror(ls_, pos_>0?pos_:-pos_, "convert to vector failed"); 1339 | } 1340 | lua_pop(ls_, 1); 1341 | } 1342 | return 0; 1343 | } 1344 | }; 1345 | 1346 | template 1347 | struct lua_op_t > 1348 | { 1349 | static void push_stack(lua_State* ls_, const set& arg_) 1350 | { 1351 | lua_newtable(ls_); 1352 | typename set::const_iterator it = arg_.begin(); 1353 | for (int i = 1; it != arg_.end(); ++it, ++i) 1354 | { 1355 | lua_op_t::push_stack(ls_, i); 1356 | lua_op_t::push_stack(ls_, *it); 1357 | lua_settable(ls_, -3); 1358 | } 1359 | } 1360 | 1361 | static int get_ret_value(lua_State* ls_, int pos_, set& param_) 1362 | { 1363 | if (0 == lua_istable(ls_, pos_)) 1364 | { 1365 | return -1; 1366 | } 1367 | lua_pushnil(ls_); 1368 | int real_pos = pos_; 1369 | if (pos_ < 0) real_pos = real_pos - 1; 1370 | 1371 | while (lua_next(ls_, real_pos) != 0) 1372 | { 1373 | T val = init_value_traits_t::value(); 1374 | if (lua_op_t::get_ret_value(ls_, -1, val) < 0) 1375 | { 1376 | return -1; 1377 | } 1378 | param_.insert(val); 1379 | lua_pop(ls_, 1); 1380 | } 1381 | return 0; 1382 | } 1383 | 1384 | static int lua_to_value(lua_State* ls_, int pos_, set& param_) 1385 | { 1386 | luaL_checktype(ls_, pos_, LUA_TTABLE); 1387 | 1388 | lua_pushnil(ls_); 1389 | int real_pos = pos_; 1390 | if (pos_ < 0) real_pos = real_pos - 1; 1391 | while (lua_next(ls_, real_pos) != 0) 1392 | { 1393 | T val = init_value_traits_t::value(); 1394 | if (lua_op_t::lua_to_value(ls_, -1, val) < 0) 1395 | { 1396 | luaL_argerror(ls_, pos_>0?pos_:-pos_, "convert to vector failed"); 1397 | } 1398 | param_.insert(val); 1399 | lua_pop(ls_, 1); 1400 | } 1401 | return 0; 1402 | } 1403 | }; 1404 | template 1405 | struct lua_op_t > 1406 | { 1407 | static void push_stack(lua_State* ls_, const map& arg_) 1408 | { 1409 | lua_newtable(ls_); 1410 | typename map::const_iterator it = arg_.begin(); 1411 | for (; it != arg_.end(); ++it) 1412 | { 1413 | lua_op_t::push_stack(ls_, it->first); 1414 | lua_op_t::push_stack(ls_, it->second); 1415 | lua_settable(ls_, -3); 1416 | } 1417 | } 1418 | 1419 | static int get_ret_value(lua_State* ls_, int pos_, map& param_) 1420 | { 1421 | if (0 == lua_istable(ls_, pos_)) 1422 | { 1423 | return -1; 1424 | } 1425 | lua_pushnil(ls_); 1426 | int real_pos = pos_; 1427 | if (pos_ < 0) real_pos = real_pos - 1; 1428 | 1429 | while (lua_next(ls_, real_pos) != 0) 1430 | { 1431 | K key = init_value_traits_t::value(); 1432 | V val = init_value_traits_t::value(); 1433 | 1434 | if (lua_op_t::get_ret_value(ls_, -2, key) < 0 || 1435 | lua_op_t::get_ret_value(ls_, -1, val) < 0) 1436 | { 1437 | return -1; 1438 | } 1439 | param_.insert(make_pair(key, val)); 1440 | lua_pop(ls_, 1); 1441 | } 1442 | return 0; 1443 | } 1444 | 1445 | static int lua_to_value(lua_State* ls_, int pos_, map& param_) 1446 | { 1447 | luaL_checktype(ls_, pos_, LUA_TTABLE); 1448 | 1449 | lua_pushnil(ls_); 1450 | int real_pos = pos_; 1451 | if (pos_ < 0) real_pos = real_pos - 1; 1452 | while (lua_next(ls_, real_pos) != 0) 1453 | { 1454 | K key = init_value_traits_t::value(); 1455 | V val = init_value_traits_t::value(); 1456 | if (lua_op_t::get_ret_value(ls_, -2, key) < 0 || 1457 | lua_op_t::get_ret_value(ls_, -1, val) < 0) 1458 | { 1459 | luaL_argerror(ls_, pos_>0?pos_:-pos_, "convert to vector failed"); 1460 | } 1461 | param_.insert(make_pair(key, val)); 1462 | lua_pop(ls_, 1); 1463 | } 1464 | return 0; 1465 | } 1466 | }; 1467 | 1468 | } 1469 | #endif 1470 | --------------------------------------------------------------------------------