├── .gitattributes ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── SConscript ├── lua-5.1.4 ├── SConscript ├── compiler.h ├── lapi.c ├── lapi.h ├── lauxlib.c ├── lauxlib.h ├── lbaselib.c ├── lcode.c ├── lcode.h ├── ldblib.c ├── ldebug.c ├── ldebug.h ├── ldo.c ├── ldo.h ├── ldump.c ├── legc.c ├── legc.h ├── lfunc.c ├── lfunc.h ├── lgc.c ├── lgc.h ├── linit.c ├── liolib.c ├── llex.c ├── llex.h ├── llimits.h ├── lmathlib.c ├── lmem.c ├── lmem.h ├── loadlib.c ├── lobject.c ├── lobject.h ├── lopcodes.c ├── lopcodes.h ├── loslib.c ├── lparser.c ├── lparser.h ├── lrodefs.h ├── lrotable.c ├── lrotable.h ├── lstate.c ├── lstate.h ├── lstring.c ├── lstring.h ├── lstrlib.c ├── ltable.c ├── ltable.h ├── ltablib.c ├── ltm.c ├── ltm.h ├── lua.c ├── lua.h ├── luaconf.h ├── lualib.h ├── lundump.c ├── lundump.h ├── lvm.c ├── lvm.h ├── lzio.c └── lzio.h ├── lua-5.3.4 ├── SConscript ├── lapi.c ├── lapi.h ├── lauxlib.c ├── lauxlib.h ├── lbaselib.c ├── lbitlib.c ├── lcode.c ├── lcode.h ├── lcorolib.c ├── lctype.c ├── lctype.h ├── ldblib.c ├── ldebug.c ├── ldebug.h ├── ldo.c ├── ldo.h ├── ldump.c ├── lfunc.c ├── lfunc.h ├── lgc.c ├── lgc.h ├── linit.c ├── liolib.c ├── llex.c ├── llex.h ├── llimits.h ├── lmathlib.c ├── lmem.c ├── lmem.h ├── loadlib.c ├── lobject.c ├── lobject.h ├── lopcodes.c ├── lopcodes.h ├── loslib.c ├── lparser.c ├── lparser.h ├── lprefix.h ├── lstate.c ├── lstate.h ├── lstring.c ├── lstring.h ├── lstrlib.c ├── ltable.c ├── ltable.h ├── ltablib.c ├── ltm.c ├── ltm.h ├── lua.c ├── lua.h ├── lua.hpp ├── luac.c ├── luaconf.h ├── lualib.h ├── lundump.c ├── lundump.h ├── lutf8lib.c ├── lvm.c ├── lvm.h ├── lzio.c └── lzio.h ├── lua2rtt.c ├── lua2rtt.h └── script ├── script_bspcfg └── script_scons /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | 3 | notifications: 4 | email: true 5 | 6 | git: 7 | depth: 3 8 | 9 | before_script: 10 | - USER_NAME='liu2guang' REPO_NAME='Lua2RTT' 11 | 12 | - sudo apt-get update 13 | - "sudo apt-get -qq install gcc-multilib libc6:i386 libgcc1:i386 gcc-4.6-base:i386 libstdc++5:i386 libstdc++6:i386 libsdl-dev || true" 14 | - "[ $RTT_TOOL_CHAIN = 'sourcery-arm' ] && curl -s https://launchpadlibrarian.net/287101520/gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2 | sudo tar xjf - -C /opt && export RTT_EXEC_PATH=/opt/gcc-arm-none-eabi-5_4-2016q3/bin && /opt/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi-gcc --version || true" 15 | 16 | - git clone --depth=3 --branch=master https://github.com/RT-Thread/rt-thread.git ../RT-Thread 17 | 18 | - export RTT_ROOT=/home/travis/build/$USER_NAME/RT-Thread 19 | - "[ x$RTT_CC == x ] && export RTT_CC='gcc' || true" 20 | 21 | - sudo mkdir $RTT_ROOT/bsp/$RTT_BSP/packages 22 | - sudo cp /home/travis/build/$USER_NAME/$REPO_NAME/script/script_bspcfg $RTT_ROOT/bsp/$RTT_BSP/rtconfig.h 23 | - sudo cp /home/travis/build/$USER_NAME/$REPO_NAME/script/script_scons $RTT_ROOT/bsp/$RTT_BSP/packages/SConscript 24 | - sudo cp -r /home/travis/build/$USER_NAME/$REPO_NAME $RTT_ROOT/bsp/$RTT_BSP/packages/$REPO_NAME 25 | 26 | script: 27 | - scons -C $RTT_ROOT/bsp/$RTT_BSP 28 | 29 | env: 30 | - RTT_BSP='imxrt1052-evk' RTT_TOOL_CHAIN='sourcery-arm' 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 liu2guang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # :tada: Lua2RTT :tada: # 2 | 3 | [![Build Status](https://travis-ci.org/liu2guang/Lua2RTT.svg?branch=master)](https://travis-ci.org/liu2guang/Lua2RTT) 4 | [![release](https://img.shields.io/badge/Release-v1.0.0-orange.svg)](https://github.com/liu2guang/Lua2RTT/releases) 5 | 6 | Lua2RTT是在RT-Thread3.0及以上版本移植的Lua库, 目的是无缝嵌入RTT, 无需开发者去移植. 如果您觉得该库看得顺眼舒服,请捐赠颗小星星. 小星星就是更新的动力!!! 7 | 8 | ## 1. 效果图 9 | 10 | ![效果图](https://i.imgur.com/NzP2WOe.gif) 11 | 12 | ## 2. 安装Lua2RTT 13 | 14 | 目前Lua2RTT库已经添加到RT-Thread官方pkgs包中, 可以直接在menuconfig在线包中直接使能. 15 | 16 | 1. 在env中运行menuconfig. 17 | 2. 进入RT-Thread online packages -> language目录. 18 | 3. 开启Lua2RTT, 选择Submitted version(Lua2RTT库移植的发布版本)为lateset最新版本, 然后选择Porting Lua version(移植的Lua源码版本)为您想要移植的版本(目前支持5.1.4和5.3.4). 19 | 4. 执行pkgs --update更新Lua2RTT包到你的bsp下面. 20 | 5. 执行scons/scons --target=xxx, 进行编译生成工程, 下载运行. 21 | 22 | ![安装流程](https://i.imgur.com/wOuODbj.gif) 23 | 24 | ## 3. 卸载Lua2RTT 25 | 26 | 1. 在env中运行menuconfig. 27 | 2. 进入RT-Thread online packages -> language目录. 28 | 3. 关闭Lua2RTT. 29 | 4. 执行pkgs --update, 并输入`Y`表示同意删除pkg包文件. 30 | 5. 执行scons/scons --target=xxx, 进行编译生成工程, 下载运行. 31 | 32 | ![卸载流程](https://i.imgur.com/idFfFPN.gif) 33 | 34 | ## 4. 教程推荐 35 | 36 | 1. [AlbertS 作者简书文章](https://www.jianshu.com/u/8fad76e7e05c). 37 | 2. [Lua 5.1 参考手册中文版](https://www.codingnow.com/2000/download/lua_manual.html). 38 | 3. [Lua 5.3 参考手册中文版](http://cloudwu.github.io/lua53doc/contents.html). 39 | 40 | ## 5. 欢迎加入. 41 | 42 | 非官方讨论腾讯QQ群: [289156309](). 43 | 44 | ## 6. 感谢 45 | 46 | 1. 该库基于 https://github.com/lua/lua 移植. 47 | 2. 感谢Lua团队. 本移植是修改了部分原作者的代码针对RTT在线包实现的版本, 该仓库保留原作者的许可声明! 具体原作者许可请查看 https://www.lua.org/license.html, 移植代码部分保留 https://github.com/liu2guang/Lua2RTT/blob/master/LICENSE 许可. 48 | -------------------------------------------------------------------------------- /SConscript: -------------------------------------------------------------------------------- 1 | import os 2 | from building import * 3 | 4 | # get current dir path 5 | cwd = GetCurrentDir() 6 | 7 | # init src and inc vars 8 | src = [] 9 | inc = [] 10 | 11 | # add lua common include 12 | inc = inc + [cwd] 13 | 14 | # add lua basic code 15 | src = src + ['./lua2rtt.c'] 16 | 17 | # add group to IDE project 18 | if GetDepend('LUA_USING_PORTING_V514'): 19 | objs = DefineGroup('lua-5.1.4', src, depend = ['PKG_USING_LUA', 'LUA_USING_PORTING_V514'], CPPPATH = inc) 20 | 21 | if GetDepend('LUA_USING_PORTING_V534'): 22 | objs = DefineGroup('lua-5.3.4', src, depend = ['PKG_USING_LUA', 'LUA_USING_PORTING_V534'], CPPPATH = inc) 23 | 24 | # traversal subscript 25 | list = os.listdir(cwd) 26 | if GetDepend('PKG_USING_LUA'): 27 | for d in list: 28 | path = os.path.join(cwd, d) 29 | if os.path.isfile(os.path.join(path, 'SConscript')): 30 | objs = objs + SConscript(os.path.join(d, 'SConscript')) 31 | 32 | Return('objs') 33 | -------------------------------------------------------------------------------- /lua-5.1.4/SConscript: -------------------------------------------------------------------------------- 1 | Import('rtconfig') 2 | import os 3 | from building import * 4 | 5 | cwd = GetCurrentDir() 6 | src = Glob('*.c') 7 | CPPPATH = [cwd] 8 | 9 | group = DefineGroup('lua-5.1.4', src, depend = ['PKG_USING_LUA', 'LUA_USING_PORTING_V514'], CPPPATH = CPPPATH) 10 | 11 | Return('group') 12 | -------------------------------------------------------------------------------- /lua-5.1.4/compiler.h: -------------------------------------------------------------------------------- 1 | /** 2 | * define start/end address of ro data. 3 | * different compiler with different implementation. 4 | */ 5 | 6 | #ifndef __COMPILER_H__ 7 | #define __COMPILER_H__ 8 | 9 | #if defined(__CC_ARM) // armcc 10 | 11 | #warning "Please check scatter file to ensure rodata is in ER_IROM1 region." 12 | 13 | /* symbols reference to the scatter file */ 14 | extern char Image$$ER_IROM1$$Base; 15 | extern char Image$$ER_IROM1$$Limit; 16 | 17 | #define RODATA_START_ADDRESS (&Image$$ER_IROM1$$Base) 18 | #define RODATA_END_ADDRESS (&Image$$ER_IROM1$$Limit) 19 | 20 | #elif defined(__GNUC__) // gcc 21 | 22 | #warning "Please check linker script to ensure rodata is between _stext and _etext." 23 | 24 | /* symbols defined in linker script */ 25 | extern char _stext; 26 | extern char _etext; 27 | 28 | #define RODATA_START_ADDRESS (&_stext) 29 | #define RODATA_END_ADDRESS (&_etext) 30 | 31 | #else // other compilers 32 | 33 | /* Firstly, modify rodata's start/end address. Then, comment the line below */ 34 | #error "Please modify RODATA_START_ADDRESS and RODATA_END_ADDRESS below */ 35 | 36 | /* Perhaps you can use start/end address of flash */ 37 | #define RODATA_START_ADDRESS ((char*)0x08000000) 38 | #define RODATA_END_ADDRESS ((char*)0x08080000) 39 | 40 | #endif 41 | 42 | #endif // __COMPILER_H__ 43 | 44 | -------------------------------------------------------------------------------- /lua-5.1.4/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | LUAI_FUNC void luaA_pushobject(lua_State *L, const TValue *o); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /lua-5.1.4/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 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 | #if defined(LUA_COMPAT_GETN) 19 | LUALIB_API int (luaL_getn)(lua_State *L, int t); 20 | LUALIB_API void (luaL_setn)(lua_State *L, int t, int n); 21 | #else 22 | #define luaL_getn(L,i) ((int)lua_objlen(L, i)) 23 | #define luaL_setn(L,i,j) ((void)0) /* no op! */ 24 | #endif 25 | 26 | #if defined(LUA_COMPAT_OPENLIB) 27 | #define luaI_openlib luaL_openlib 28 | #endif 29 | 30 | 31 | /* extra error code for `luaL_load' */ 32 | #define LUA_ERRFILE (LUA_ERRERR+1) 33 | 34 | 35 | typedef struct luaL_Reg 36 | { 37 | const char *name; 38 | lua_CFunction func; 39 | } luaL_Reg; 40 | 41 | 42 | 43 | LUALIB_API void (luaI_openlib)(lua_State *L, const char *libname, 44 | const luaL_Reg *l, int nup, int ftype); 45 | LUALIB_API void (luaL_register)(lua_State *L, const char *libname, 46 | const luaL_Reg *l); 47 | LUALIB_API void (luaL_register_light)(lua_State *L, const char *libname, 48 | const luaL_Reg *l); 49 | LUALIB_API int (luaL_getmetafield)(lua_State *L, int obj, const char *e); 50 | LUALIB_API int (luaL_callmeta)(lua_State *L, int obj, const char *e); 51 | LUALIB_API int (luaL_typerror)(lua_State *L, int narg, const char *tname); 52 | LUALIB_API int (luaL_argerror)(lua_State *L, int numarg, const char *extramsg); 53 | LUALIB_API const char *(luaL_checklstring)(lua_State *L, int numArg, 54 | size_t *l); 55 | LUALIB_API const char *(luaL_optlstring)(lua_State *L, int numArg, 56 | const char *def, size_t *l); 57 | LUALIB_API lua_Number(luaL_checknumber)(lua_State *L, int numArg); 58 | LUALIB_API lua_Number(luaL_optnumber)(lua_State *L, int nArg, lua_Number def); 59 | 60 | LUALIB_API lua_Integer(luaL_checkinteger)(lua_State *L, int numArg); 61 | LUALIB_API lua_Integer(luaL_optinteger)(lua_State *L, int nArg, 62 | lua_Integer def); 63 | 64 | LUALIB_API void (luaL_checkstack)(lua_State *L, int sz, const char *msg); 65 | LUALIB_API void (luaL_checktype)(lua_State *L, int narg, int t); 66 | LUALIB_API void (luaL_checkany)(lua_State *L, int narg); 67 | LUALIB_API void (luaL_checkanyfunction)(lua_State *L, int narg); 68 | LUALIB_API void (luaL_checkanytable)(lua_State *L, int narg); 69 | 70 | LUALIB_API int (luaL_newmetatable)(lua_State *L, const char *tname); 71 | LUALIB_API int (luaL_rometatable)(lua_State *L, const char *tname, void *p); 72 | LUALIB_API void *(luaL_checkudata)(lua_State *L, int ud, const char *tname); 73 | 74 | LUALIB_API void (luaL_where)(lua_State *L, int lvl); 75 | LUALIB_API int (luaL_error)(lua_State *L, const char *fmt, ...); 76 | 77 | LUALIB_API int (luaL_checkoption)(lua_State *L, int narg, const char *def, 78 | const char *const lst[]); 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_loadfile)(lua_State *L, const char *filename); 84 | LUALIB_API int (luaL_loadbuffer)(lua_State *L, const char *buff, size_t sz, 85 | const char *name); 86 | LUALIB_API int (luaL_loadstring)(lua_State *L, const char *s); 87 | 88 | LUALIB_API lua_State *(luaL_newstate)(void); 89 | 90 | 91 | LUALIB_API const char *(luaL_gsub)(lua_State *L, const char *s, const char *p, 92 | const char *r); 93 | 94 | LUALIB_API const char *(luaL_findtable)(lua_State *L, int idx, 95 | const char *fname, int szhint); 96 | 97 | 98 | 99 | 100 | /* 101 | ** =============================================================== 102 | ** some useful macros 103 | ** =============================================================== 104 | */ 105 | 106 | #define luaL_argcheck(L, cond,numarg,extramsg) \ 107 | ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) 108 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 109 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 110 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 111 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 112 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 113 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 114 | 115 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 116 | 117 | #define luaL_dofile(L, fn) \ 118 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 119 | 120 | #define luaL_dostring(L, s) \ 121 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 122 | 123 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 124 | 125 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 126 | 127 | /* 128 | ** {====================================================== 129 | ** Generic Buffer manipulation 130 | ** ======================================================= 131 | */ 132 | 133 | 134 | 135 | typedef struct luaL_Buffer 136 | { 137 | char *p; /* current position in buffer */ 138 | int lvl; /* number of strings in the stack (level) */ 139 | lua_State *L; 140 | char buffer[LUAL_BUFFERSIZE]; 141 | } luaL_Buffer; 142 | 143 | #define luaL_addchar(B,c) \ 144 | ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ 145 | (*(B)->p++ = (char)(c))) 146 | 147 | /* compatibility only */ 148 | #define luaL_putchar(B,c) luaL_addchar(B,c) 149 | 150 | #define luaL_addsize(B,n) ((B)->p += (n)) 151 | 152 | LUALIB_API void (luaL_buffinit)(lua_State *L, luaL_Buffer *B); 153 | LUALIB_API char *(luaL_prepbuffer)(luaL_Buffer *B); 154 | LUALIB_API void (luaL_addlstring)(luaL_Buffer *B, const char *s, size_t l); 155 | LUALIB_API void (luaL_addstring)(luaL_Buffer *B, const char *s); 156 | LUALIB_API void (luaL_addvalue)(luaL_Buffer *B); 157 | LUALIB_API void (luaL_pushresult)(luaL_Buffer *B); 158 | 159 | 160 | /* }====================================================== */ 161 | 162 | 163 | /* compatibility with ref system */ 164 | 165 | /* pre-defined references */ 166 | #define LUA_NOREF (-2) 167 | #define LUA_REFNIL (-1) 168 | 169 | #define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ 170 | (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) 171 | 172 | #define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) 173 | 174 | #define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) 175 | 176 | 177 | #define luaL_reg luaL_Reg 178 | 179 | #endif 180 | 181 | 182 | -------------------------------------------------------------------------------- /lua-5.1.4/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums 25 | */ 26 | typedef enum BinOpr 27 | { 28 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 29 | OPR_CONCAT, 30 | OPR_NE, OPR_EQ, 31 | OPR_LT, OPR_LE, OPR_GT, OPR_GE, 32 | OPR_AND, OPR_OR, 33 | OPR_NOBINOPR 34 | } BinOpr; 35 | 36 | 37 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 38 | 39 | 40 | #define getcode(fs,e) ((fs)->f->code[(e)->u.s.info]) 41 | 42 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 43 | 44 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 45 | 46 | LUAI_FUNC int luaK_codeABx(FuncState *fs, OpCode o, int A, unsigned int Bx); 47 | LUAI_FUNC int luaK_codeABC(FuncState *fs, OpCode o, int A, int B, int C); 48 | LUAI_FUNC void luaK_fixline(FuncState *fs, int line); 49 | LUAI_FUNC void luaK_nil(FuncState *fs, int from, int n); 50 | LUAI_FUNC void luaK_reserveregs(FuncState *fs, int n); 51 | LUAI_FUNC void luaK_checkstack(FuncState *fs, int n); 52 | LUAI_FUNC int luaK_stringK(FuncState *fs, TString *s); 53 | LUAI_FUNC int luaK_numberK(FuncState *fs, lua_Number r); 54 | LUAI_FUNC void luaK_dischargevars(FuncState *fs, expdesc *e); 55 | LUAI_FUNC int luaK_exp2anyreg(FuncState *fs, expdesc *e); 56 | LUAI_FUNC void luaK_exp2nextreg(FuncState *fs, expdesc *e); 57 | LUAI_FUNC void luaK_exp2val(FuncState *fs, expdesc *e); 58 | LUAI_FUNC int luaK_exp2RK(FuncState *fs, expdesc *e); 59 | LUAI_FUNC void luaK_self(FuncState *fs, expdesc *e, expdesc *key); 60 | LUAI_FUNC void luaK_indexed(FuncState *fs, expdesc *t, expdesc *k); 61 | LUAI_FUNC void luaK_goiftrue(FuncState *fs, expdesc *e); 62 | LUAI_FUNC void luaK_storevar(FuncState *fs, expdesc *var, expdesc *e); 63 | LUAI_FUNC void luaK_setreturns(FuncState *fs, expdesc *e, int nresults); 64 | LUAI_FUNC void luaK_setoneret(FuncState *fs, expdesc *e); 65 | LUAI_FUNC int luaK_jump(FuncState *fs); 66 | LUAI_FUNC void luaK_ret(FuncState *fs, int first, int nret); 67 | LUAI_FUNC void luaK_patchlist(FuncState *fs, int list, int target); 68 | LUAI_FUNC void luaK_patchtohere(FuncState *fs, int list); 69 | LUAI_FUNC void luaK_concat(FuncState *fs, int *l1, int l2); 70 | LUAI_FUNC int luaK_getlabel(FuncState *fs); 71 | LUAI_FUNC void luaK_prefix(FuncState *fs, UnOpr op, expdesc *v); 72 | LUAI_FUNC void luaK_infix(FuncState *fs, BinOpr op, expdesc *v); 73 | LUAI_FUNC void luaK_posfix(FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); 74 | LUAI_FUNC void luaK_setlist(FuncState *fs, int base, int nelems, int tostore); 75 | 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /lua-5.1.4/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | LUAI_FUNC void luaG_typeerror(lua_State *L, const TValue *o, 22 | const char *opname); 23 | LUAI_FUNC void luaG_concaterror(lua_State *L, StkId p1, StkId p2); 24 | LUAI_FUNC void luaG_aritherror(lua_State *L, const TValue *p1, 25 | const TValue *p2); 26 | LUAI_FUNC int luaG_ordererror(lua_State *L, const TValue *p1, 27 | const TValue *p2); 28 | LUAI_FUNC void luaG_runerror(lua_State *L, const char *fmt, ...); 29 | LUAI_FUNC void luaG_errormsg(lua_State *L); 30 | LUAI_FUNC int luaG_checkcode(const Proto *pt); 31 | LUAI_FUNC int luaG_checkopenop(Instruction i); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /lua-5.1.4/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) \ 17 | if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \ 18 | luaD_growstack(L, n); \ 19 | else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); 20 | 21 | 22 | #define incr_top(L) {luaD_checkstack(L,1); L->top++;} 23 | 24 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 25 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 26 | 27 | #define saveci(L,p) ((char *)(p) - (char *)L->base_ci) 28 | #define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) 29 | 30 | 31 | /* results from luaD_precall */ 32 | #define PCRLUA 0 /* initiated a call to a Lua function */ 33 | #define PCRC 1 /* did a call to a C function */ 34 | #define PCRYIELD 2 /* C funtion yielded */ 35 | 36 | 37 | /* type of protected functions, to be ran by `runprotected' */ 38 | typedef void (*Pfunc)(lua_State *L, void *ud); 39 | 40 | LUAI_FUNC int luaD_protectedparser(lua_State *L, ZIO *z, const char *name); 41 | LUAI_FUNC void luaD_callhook(lua_State *L, int event, int line); 42 | LUAI_FUNC int luaD_precall(lua_State *L, StkId func, int nresults); 43 | LUAI_FUNC void luaD_call(lua_State *L, StkId func, int nResults); 44 | LUAI_FUNC int luaD_pcall(lua_State *L, Pfunc func, void *u, 45 | ptrdiff_t oldtop, ptrdiff_t ef); 46 | LUAI_FUNC int luaD_poscall(lua_State *L, StkId firstResult); 47 | LUAI_FUNC void luaD_reallocCI(lua_State *L, int newsize); 48 | LUAI_FUNC void luaD_reallocstack(lua_State *L, int newsize); 49 | LUAI_FUNC void luaD_growstack(lua_State *L, int n); 50 | 51 | LUAI_FUNC void luaD_throw(lua_State *L, int errcode); 52 | LUAI_FUNC int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud); 53 | 54 | LUAI_FUNC void luaD_seterrorobj(lua_State *L, int errcode, StkId oldtop); 55 | 56 | #endif 57 | 58 | -------------------------------------------------------------------------------- /lua-5.1.4/legc.c: -------------------------------------------------------------------------------- 1 | // Lua EGC (Emergeny Garbage Collector) interface 2 | 3 | #include "legc.h" 4 | #include "lstate.h" 5 | 6 | void legc_set_mode(lua_State *L, int mode, unsigned limit) 7 | { 8 | global_State *g = G(L); 9 | 10 | g->egcmode = mode; 11 | g->memlimit = limit; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /lua-5.1.4/legc.h: -------------------------------------------------------------------------------- 1 | // Lua EGC (Emergeny Garbage Collector) interface 2 | 3 | #ifndef __LEGC_H__ 4 | #define __LEGC_H__ 5 | 6 | #include "lstate.h" 7 | 8 | // EGC operations modes 9 | #define EGC_NOT_ACTIVE 0 // EGC disabled 10 | #define EGC_ON_ALLOC_FAILURE 1 // run EGC on allocation failure 11 | #define EGC_ON_MEM_LIMIT 2 // run EGC when an upper memory limit is hit 12 | #define EGC_ALWAYS 4 // always run EGC before an allocation 13 | 14 | void legc_set_mode(lua_State *L, int mode, unsigned limit); 15 | 16 | #endif 17 | 18 | -------------------------------------------------------------------------------- /lua-5.1.4/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lfunc_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lfunc.h" 16 | #include "lgc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | Closure *luaF_newCclosure(lua_State *L, int nelems, Table *e) 24 | { 25 | Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); 26 | luaC_link(L, obj2gco(c), LUA_TFUNCTION); 27 | c->c.isC = 1; 28 | c->c.env = e; 29 | c->c.nupvalues = cast_byte(nelems); 30 | return c; 31 | } 32 | 33 | 34 | Closure *luaF_newLclosure(lua_State *L, int nelems, Table *e) 35 | { 36 | Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); 37 | luaC_link(L, obj2gco(c), LUA_TFUNCTION); 38 | c->l.isC = 0; 39 | c->l.env = e; 40 | c->l.nupvalues = cast_byte(nelems); 41 | while (nelems--) c->l.upvals[nelems] = NULL; 42 | return c; 43 | } 44 | 45 | 46 | UpVal *luaF_newupval(lua_State *L) 47 | { 48 | UpVal *uv = luaM_new(L, UpVal); 49 | luaC_link(L, obj2gco(uv), LUA_TUPVAL); 50 | uv->v = &uv->u.value; 51 | setnilvalue(uv->v); 52 | return uv; 53 | } 54 | 55 | 56 | UpVal *luaF_findupval(lua_State *L, StkId level) 57 | { 58 | global_State *g = G(L); 59 | GCObject **pp = &L->openupval; 60 | UpVal *p; 61 | UpVal *uv; 62 | while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) 63 | { 64 | lua_assert(p->v != &p->u.value); 65 | if (p->v == level) /* found a corresponding upvalue? */ 66 | { 67 | if (isdead(g, obj2gco(p))) /* is it dead? */ 68 | changewhite(obj2gco(p)); /* ressurect it */ 69 | return p; 70 | } 71 | pp = &p->next; 72 | } 73 | uv = luaM_new(L, UpVal); /* not found: create a new one */ 74 | uv->tt = LUA_TUPVAL; 75 | uv->v = level; /* current value lives in the stack */ 76 | uv->next = *pp; /* chain it in the proper position */ 77 | *pp = obj2gco(uv); 78 | uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 79 | uv->u.l.next = g->uvhead.u.l.next; 80 | uv->u.l.next->u.l.prev = uv; 81 | g->uvhead.u.l.next = uv; 82 | luaC_marknew(L, obj2gco(uv)); 83 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 84 | return uv; 85 | } 86 | 87 | 88 | static void unlinkupval(UpVal *uv) 89 | { 90 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 91 | uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 92 | uv->u.l.prev->u.l.next = uv->u.l.next; 93 | } 94 | 95 | 96 | void luaF_freeupval(lua_State *L, UpVal *uv) 97 | { 98 | if (uv->v != &uv->u.value) /* is it open? */ 99 | unlinkupval(uv); /* remove from open list */ 100 | luaM_free(L, uv); /* free upvalue */ 101 | } 102 | 103 | 104 | void luaF_close(lua_State *L, StkId level) 105 | { 106 | UpVal *uv; 107 | global_State *g = G(L); 108 | while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) 109 | { 110 | GCObject *o = obj2gco(uv); 111 | lua_assert(!isblack(o) && uv->v != &uv->u.value); 112 | L->openupval = uv->next; /* remove from `open' list */ 113 | if (isdead(g, o)) 114 | luaF_freeupval(L, uv); /* free upvalue */ 115 | else 116 | { 117 | unlinkupval(uv); 118 | setobj(L, &uv->u.value, uv->v); 119 | uv->v = &uv->u.value; /* now current value lives here */ 120 | luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ 121 | } 122 | } 123 | } 124 | 125 | 126 | Proto *luaF_newproto(lua_State *L) 127 | { 128 | Proto *f = luaM_new(L, Proto); 129 | luaC_link(L, obj2gco(f), LUA_TPROTO); 130 | f->k = NULL; 131 | f->sizek = 0; 132 | f->p = NULL; 133 | f->sizep = 0; 134 | f->code = NULL; 135 | f->sizecode = 0; 136 | f->sizelineinfo = 0; 137 | f->sizeupvalues = 0; 138 | f->nups = 0; 139 | f->upvalues = NULL; 140 | f->numparams = 0; 141 | f->is_vararg = 0; 142 | f->maxstacksize = 0; 143 | f->lineinfo = NULL; 144 | f->sizelocvars = 0; 145 | f->locvars = NULL; 146 | f->linedefined = 0; 147 | f->lastlinedefined = 0; 148 | f->source = NULL; 149 | return f; 150 | } 151 | 152 | 153 | void luaF_freeproto(lua_State *L, Proto *f) 154 | { 155 | luaM_freearray(L, f->p, f->sizep, Proto *); 156 | luaM_freearray(L, f->k, f->sizek, TValue); 157 | luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); 158 | luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); 159 | if (!proto_is_readonly(f)) 160 | { 161 | luaM_freearray(L, f->code, f->sizecode, Instruction); 162 | luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); 163 | } 164 | luaM_free(L, f); 165 | } 166 | 167 | 168 | void luaF_freeclosure(lua_State *L, Closure *c) 169 | { 170 | int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : 171 | sizeLclosure(c->l.nupvalues); 172 | luaM_freemem(L, c, size); 173 | } 174 | 175 | 176 | /* 177 | ** Look for n-th local variable at line `line' in function `func'. 178 | ** Returns NULL if not found. 179 | */ 180 | const char *luaF_getlocalname(const Proto *f, int local_number, int pc) 181 | { 182 | int i; 183 | for (i = 0; i < f->sizelocvars && f->locvars[i].startpc <= pc; i++) 184 | { 185 | if (pc < f->locvars[i].endpc) /* is variable active? */ 186 | { 187 | local_number--; 188 | if (local_number == 0) 189 | return getstr(f->locvars[i].varname); 190 | } 191 | } 192 | return NULL; /* not found */ 193 | } 194 | 195 | -------------------------------------------------------------------------------- /lua-5.1.4/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | #include "lgc.h" 14 | 15 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 16 | cast(int, sizeof(TValue)*((n)-1))) 17 | 18 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 19 | cast(int, sizeof(TValue *)*((n)-1))) 20 | 21 | #define proto_readonly(p) l_setbit((p)->marked, READONLYBIT) 22 | #define proto_is_readonly(p) testbit((p)->marked, READONLYBIT) 23 | 24 | LUAI_FUNC Proto *luaF_newproto(lua_State *L); 25 | LUAI_FUNC Closure *luaF_newCclosure(lua_State *L, int nelems, Table *e); 26 | LUAI_FUNC Closure *luaF_newLclosure(lua_State *L, int nelems, Table *e); 27 | LUAI_FUNC UpVal *luaF_newupval(lua_State *L); 28 | LUAI_FUNC UpVal *luaF_findupval(lua_State *L, StkId level); 29 | LUAI_FUNC void luaF_close(lua_State *L, StkId level); 30 | LUAI_FUNC void luaF_freeproto(lua_State *L, Proto *f); 31 | LUAI_FUNC void luaF_freeclosure(lua_State *L, Closure *c); 32 | LUAI_FUNC void luaF_freeupval(lua_State *L, UpVal *uv); 33 | LUAI_FUNC const char *luaF_getlocalname(const Proto *func, int local_number, 34 | int pc); 35 | 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /lua-5.1.4/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | ** Possible states of the Garbage Collector 16 | */ 17 | #define GCSpause 0 18 | #define GCSpropagate 1 19 | #define GCSsweepstring 2 20 | #define GCSsweep 3 21 | #define GCSfinalize 4 22 | 23 | 24 | /* 25 | ** some userful bit tricks 26 | */ 27 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 28 | #define setbits(x,m) ((x) |= (m)) 29 | #define testbits(x,m) ((x) & (m)) 30 | #define bitmask(b) (1<<(b)) 31 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 32 | #define l_setbit(x,b) setbits(x, bitmask(b)) 33 | #define resetbit(x,b) resetbits(x, bitmask(b)) 34 | #define testbit(x,b) testbits(x, bitmask(b)) 35 | #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) 36 | #define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2))) 37 | #define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2))) 38 | 39 | 40 | /* 41 | ** Possible Garbage Collector flags. 42 | ** Layout for bit use in 'gsflags' field in global_State structure. 43 | ** bit 0 - Protect GC from recursive calls. 44 | ** bit 1 - Don't try to shrink string table if EGC was called during a string table resize. 45 | */ 46 | #define GCFlagsNone 0 47 | #define GCBlockGCBit 0 48 | #define GCResizingStringsBit 1 49 | 50 | 51 | #define is_block_gc(L) testbit(G(L)->gcflags, GCBlockGCBit) 52 | #define set_block_gc(L) l_setbit(G(L)->gcflags, GCBlockGCBit) 53 | #define unset_block_gc(L) resetbit(G(L)->gcflags, GCBlockGCBit) 54 | #define is_resizing_strings_gc(L) testbit(G(L)->gcflags, GCResizingStringsBit) 55 | #define set_resizing_strings_gc(L) l_setbit(G(L)->gcflags, GCResizingStringsBit) 56 | #define unset_resizing_strings_gc(L) resetbit(G(L)->gcflags, GCResizingStringsBit) 57 | 58 | /* 59 | ** Layout for bit use in `marked' field: 60 | ** bit 0 - object is white (type 0) 61 | ** bit 1 - object is white (type 1) 62 | ** bit 2 - object is black 63 | ** bit 3 - for thread: Don't resize thread's stack 64 | ** bit 3 - for userdata: has been finalized 65 | ** bit 3 - for tables: has weak keys 66 | ** bit 4 - for tables: has weak values 67 | ** bit 5 - object is fixed (should not be collected) 68 | ** bit 6 - object is "super" fixed (only the main thread) 69 | ** bit 7 - object is (partially) stored in read-only memory 70 | */ 71 | 72 | 73 | #define WHITE0BIT 0 74 | #define WHITE1BIT 1 75 | #define BLACKBIT 2 76 | #define FIXEDSTACKBIT 3 77 | #define FINALIZEDBIT 3 78 | #define KEYWEAKBIT 3 79 | #define VALUEWEAKBIT 4 80 | #define FIXEDBIT 5 81 | #define SFIXEDBIT 6 82 | #define READONLYBIT 7 83 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 84 | 85 | 86 | #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) 87 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 88 | #define isgray(x) (!isblack(x) && !iswhite(x)) 89 | 90 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 91 | #define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) 92 | 93 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 94 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 95 | 96 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 97 | 98 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 99 | 100 | #define isfixedstack(x) testbit((x)->marked, FIXEDSTACKBIT) 101 | #define fixedstack(x) l_setbit((x)->marked, FIXEDSTACKBIT) 102 | #define unfixedstack(x) resetbit((x)->marked, FIXEDSTACKBIT) 103 | 104 | #define luaC_checkGC(L) { \ 105 | condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ 106 | if (G(L)->totalbytes >= G(L)->GCthreshold) \ 107 | luaC_step(L); } 108 | 109 | 110 | #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 111 | luaC_barrierf(L,obj2gco(p),gcvalue(v)); } 112 | 113 | #define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ 114 | luaC_barrierback(L,t); } 115 | 116 | #define luaC_objbarrier(L,p,o) \ 117 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 118 | luaC_barrierf(L,obj2gco(p),obj2gco(o)); } 119 | 120 | #define luaC_objbarriert(L,t,o) \ 121 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } 122 | 123 | LUAI_FUNC size_t luaC_separateudata(lua_State *L, int all); 124 | LUAI_FUNC void luaC_callGCTM(lua_State *L); 125 | LUAI_FUNC void luaC_freeall(lua_State *L); 126 | LUAI_FUNC void luaC_step(lua_State *L); 127 | LUAI_FUNC void luaC_fullgc(lua_State *L); 128 | LUAI_FUNC int luaC_sweepstrgc(lua_State *L); 129 | LUAI_FUNC void luaC_marknew(lua_State *L, GCObject *o); 130 | LUAI_FUNC void luaC_link(lua_State *L, GCObject *o, lu_byte tt); 131 | LUAI_FUNC void luaC_linkupval(lua_State *L, UpVal *uv); 132 | LUAI_FUNC void luaC_barrierf(lua_State *L, GCObject *o, GCObject *v); 133 | LUAI_FUNC void luaC_barrierback(lua_State *L, Table *t); 134 | 135 | 136 | #endif 137 | -------------------------------------------------------------------------------- /lua-5.1.4/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Initialization of libraries for lua.c 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | #include "lua.h" 12 | 13 | #include "lualib.h" 14 | #include "lauxlib.h" 15 | #include "lrotable.h" 16 | #include "luaconf.h" 17 | 18 | #if defined(LUA2RTT_USING_EXLIBS_CJSON) 19 | LUALIB_API int luaopen_cjson(lua_State *L); 20 | #endif 21 | 22 | static const luaL_Reg lualibs[] = 23 | { 24 | /* 基本库 */ 25 | {"" , luaopen_base }, 26 | {LUA_LOADLIBNAME, luaopen_package}, 27 | {LUA_IOLIBNAME , luaopen_io }, 28 | {LUA_STRLIBNAME , luaopen_string }, 29 | 30 | /* 扩展库 */ 31 | #if LUA_OPTIMIZE_MEMORY == 0 32 | {LUA_MATHLIBNAME, luaopen_math }, 33 | {LUA_TABLIBNAME , luaopen_table }, 34 | {LUA_DBLIBNAME , luaopen_debug }, 35 | {LUA_OSLIBNAME , luaopen_os }, 36 | #endif 37 | 38 | /* 外部库 */ 39 | #if defined(LUA2RTT_USING_EXLIBS_CJSON) 40 | {"cjson" , luaopen_cjson }, 41 | #endif 42 | 43 | {NULL, NULL} 44 | }; 45 | 46 | extern const luaR_entry strlib[]; 47 | extern const luaR_entry syslib[]; 48 | extern const luaR_entry tab_funcs[]; 49 | extern const luaR_entry dblib[]; 50 | extern const luaR_entry co_funcs[]; 51 | #if defined(LUA_EXLIBS_ROM) && LUA_OPTIMIZE_MEMORY == 2 52 | #undef _ROM 53 | #define _ROM( name, openf, table ) extern const luaR_entry table[]; 54 | LUA_EXLIBS_ROM 55 | #endif 56 | const luaR_table lua_rotable[] = 57 | { 58 | #if LUA_OPTIMIZE_MEMORY > 0 59 | {LUA_STRLIBNAME, strlib }, 60 | {LUA_TABLIBNAME, tab_funcs}, 61 | {LUA_DBLIBNAME , dblib }, 62 | {LUA_COLIBNAME , co_funcs }, 63 | #if defined(LUA_EXLIBS_ROM) && LUA_OPTIMIZE_MEMORY == 2 64 | #undef _ROM 65 | #define _ROM( name, openf, table ) { name, table }, 66 | LUA_EXLIBS_ROM 67 | #endif 68 | #endif 69 | {NULL, NULL} 70 | }; 71 | 72 | LUALIB_API void luaL_openlibs(lua_State *L) 73 | { 74 | const luaL_Reg *lib = lualibs; 75 | 76 | for (; lib->func; lib++) 77 | { 78 | lua_pushcfunction(L, lib->func); 79 | lua_pushstring(L, lib->name); 80 | lua_call(L, 1, 0); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /lua-5.1.4/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | /* maximum length of a reserved word */ 17 | #define TOKEN_LEN (sizeof("function")/sizeof(char)) 18 | 19 | 20 | /* 21 | * WARNING: if you change the order of this enumeration, 22 | * grep "ORDER RESERVED" 23 | */ 24 | enum RESERVED 25 | { 26 | /* terminal symbols denoted by reserved words */ 27 | TK_AND = FIRST_RESERVED, TK_BREAK, 28 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 29 | TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 30 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 31 | /* other terminal symbols */ 32 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER, 33 | TK_NAME, TK_STRING, TK_EOS 34 | }; 35 | 36 | /* number of reserved words */ 37 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 38 | 39 | 40 | /* array with token `names' */ 41 | LUAI_DATA const char *const luaX_tokens []; 42 | 43 | 44 | typedef union 45 | { 46 | lua_Number r; 47 | TString *ts; 48 | } SemInfo; /* semantics information */ 49 | 50 | 51 | typedef struct Token 52 | { 53 | int token; 54 | SemInfo seminfo; 55 | } Token; 56 | 57 | 58 | typedef struct LexState 59 | { 60 | int current; /* current character (charint) */ 61 | int linenumber; /* input line counter */ 62 | int lastline; /* line of last token `consumed' */ 63 | Token t; /* current token */ 64 | Token lookahead; /* look ahead token */ 65 | struct FuncState *fs; /* `FuncState' is private to the parser */ 66 | struct lua_State *L; 67 | ZIO *z; /* input stream */ 68 | Mbuffer *buff; /* buffer for tokens */ 69 | TString *source; /* current source name */ 70 | char decpoint; /* locale decimal point */ 71 | } LexState; 72 | 73 | 74 | LUAI_FUNC void luaX_init(lua_State *L); 75 | LUAI_FUNC void luaX_setinput(lua_State *L, LexState *ls, ZIO *z, 76 | TString *source); 77 | LUAI_FUNC TString *luaX_newstring(LexState *ls, const char *str, size_t l); 78 | LUAI_FUNC void luaX_next(LexState *ls); 79 | LUAI_FUNC void luaX_lookahead(LexState *ls); 80 | LUAI_FUNC void luaX_lexerror(LexState *ls, const char *msg, int token); 81 | LUAI_FUNC void luaX_syntaxerror(LexState *ls, const char *s); 82 | LUAI_FUNC const char *luaX_token2str(LexState *ls, int token); 83 | 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /lua-5.1.4/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Limits, basic types, and some other `installation-dependent' definitions 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llimits_h 8 | #define llimits_h 9 | 10 | 11 | #include 12 | #include 13 | 14 | 15 | #include "lua.h" 16 | 17 | 18 | typedef LUAI_UINT32 lu_int32; 19 | 20 | typedef LUAI_UMEM lu_mem; 21 | 22 | typedef LUAI_MEM l_mem; 23 | 24 | 25 | 26 | /* chars used as small naturals (so that `char' is reserved for characters) */ 27 | typedef unsigned char lu_byte; 28 | 29 | 30 | #define MAX_SIZET ((size_t)(~(size_t)0)-2) 31 | 32 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) 33 | 34 | 35 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 36 | 37 | /* 38 | ** conversion of pointer to integer 39 | ** this is for hashing only; there is no problem if the integer 40 | ** cannot hold the whole pointer value 41 | */ 42 | #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 43 | 44 | 45 | 46 | /* type to ensure maximum alignment */ 47 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 48 | 49 | 50 | /* result of a `usual argument conversion' over lua_Number */ 51 | typedef LUAI_UACNUMBER l_uacNumber; 52 | 53 | 54 | /* internal assertions for in-house debugging */ 55 | #ifdef lua_assert 56 | 57 | #define check_exp(c,e) (lua_assert(c), (e)) 58 | #define api_check(l,e) lua_assert(e) 59 | 60 | #else 61 | 62 | #define lua_assert(c) ((void)0) 63 | #define check_exp(c,e) (e) 64 | #define api_check luai_apicheck 65 | 66 | #endif 67 | 68 | 69 | #ifndef UNUSED 70 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 71 | #endif 72 | 73 | 74 | #ifndef cast 75 | #define cast(t, exp) ((t)(exp)) 76 | #endif 77 | 78 | #define cast_byte(i) cast(lu_byte, (i)) 79 | #define cast_num(i) cast(lua_Number, (i)) 80 | #define cast_int(i) cast(int, (i)) 81 | 82 | 83 | 84 | /* 85 | ** type for virtual-machine instructions 86 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 87 | */ 88 | typedef lu_int32 Instruction; 89 | 90 | 91 | 92 | /* maximum stack for a Lua function */ 93 | #define MAXSTACK 250 94 | 95 | 96 | 97 | /* minimum size for the string table (must be power of 2) */ 98 | #ifndef MINSTRTABSIZE 99 | #define MINSTRTABSIZE 32 100 | #endif 101 | 102 | 103 | /* minimum size for string buffer */ 104 | #ifndef LUA_MINBUFFER 105 | #define LUA_MINBUFFER 32 106 | #endif 107 | 108 | 109 | #ifndef lua_lock 110 | #define lua_lock(L) ((void) 0) 111 | #define lua_unlock(L) ((void) 0) 112 | #endif 113 | 114 | #ifndef luai_threadyield 115 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 116 | #endif 117 | 118 | 119 | /* 120 | ** macro to control inclusion of some hard tests on stack reallocation 121 | */ 122 | #ifndef HARDSTACKTESTS 123 | #define condhardstacktests(x) ((void)0) 124 | #else 125 | #define condhardstacktests(x) x 126 | #endif 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /lua-5.1.4/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lmem_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | /* 24 | ** About the realloc function: 25 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 26 | ** (`osize' is the old size, `nsize' is the new size) 27 | ** 28 | ** Lua ensures that (ptr == NULL) iff (osize == 0). 29 | ** 30 | ** * frealloc(ud, NULL, 0, x) creates a new block of size `x' 31 | ** 32 | ** * frealloc(ud, p, x, 0) frees the block `p' 33 | ** (in this specific case, frealloc must return NULL). 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 35 | ** (which is equivalent to free(NULL) in ANSI C) 36 | ** 37 | ** frealloc returns NULL if it cannot create or reallocate the area 38 | ** (any reallocation to an equal or smaller size cannot fail!) 39 | */ 40 | 41 | 42 | 43 | #define MINSIZEARRAY 4 44 | 45 | 46 | void *luaM_growaux_(lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *errormsg) 48 | { 49 | void *newblock; 50 | int newsize; 51 | if (*size >= limit / 2) /* cannot double it? */ 52 | { 53 | if (*size >= limit) /* cannot grow even a little? */ 54 | luaG_runerror(L, errormsg); 55 | newsize = limit; /* still have at least one free place */ 56 | } 57 | else 58 | { 59 | newsize = (*size) * 2; 60 | if (newsize < MINSIZEARRAY) 61 | newsize = MINSIZEARRAY; /* minimum size */ 62 | } 63 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 64 | *size = newsize; /* update only when everything else is OK */ 65 | return newblock; 66 | } 67 | 68 | 69 | void *luaM_toobig(lua_State *L) 70 | { 71 | luaG_runerror(L, "memory allocation error: block too big"); 72 | return NULL; /* to avoid warnings */ 73 | } 74 | 75 | 76 | 77 | /* 78 | ** generic allocation routine. 79 | */ 80 | void *luaM_realloc_(lua_State *L, void *block, size_t osize, size_t nsize) 81 | { 82 | global_State *g = G(L); 83 | lua_assert((osize == 0) == (block == NULL)); 84 | block = (*g->frealloc)(g->ud, block, osize, nsize); 85 | if (block == NULL && nsize > 0) 86 | luaD_throw(L, LUA_ERRMEM); 87 | lua_assert((nsize == 0) == (block == NULL)); 88 | g->totalbytes = (g->totalbytes - osize) + nsize; 89 | return block; 90 | } 91 | 92 | -------------------------------------------------------------------------------- /lua-5.1.4/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | #define MEMERRMSG "not enough memory" 17 | 18 | 19 | #define luaM_reallocv(L,b,on,n,e) \ 20 | ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 21 | luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ 22 | luaM_toobig(L)) 23 | 24 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 25 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 26 | #define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) 27 | 28 | #define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) 29 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 30 | #define luaM_newvector(L,n,t) \ 31 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 32 | 33 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 34 | if ((nelems)+1 > (size)) \ 35 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 36 | 37 | #define luaM_reallocvector(L, v,oldn,n,t) \ 38 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 39 | 40 | 41 | LUAI_FUNC void *luaM_realloc_(lua_State *L, void *block, size_t oldsize, 42 | size_t size); 43 | LUAI_FUNC void *luaM_toobig(lua_State *L); 44 | LUAI_FUNC void *luaM_growaux_(lua_State *L, void *block, int *size, 45 | size_t size_elem, int limit, 46 | const char *errormsg); 47 | 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /lua-5.1.4/lobject.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Some generic functions over Lua objects 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #define lobject_c 14 | #define LUA_CORE 15 | 16 | #include "lua.h" 17 | 18 | #include "ldo.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | #include "lvm.h" 24 | 25 | 26 | 27 | const TValue luaO_nilobject_ = {LUA_TVALUE_NIL}; 28 | 29 | 30 | /* 31 | ** converts an integer to a "floating point byte", represented as 32 | ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if 33 | ** eeeee != 0 and (xxx) otherwise. 34 | */ 35 | int luaO_int2fb(unsigned int x) 36 | { 37 | int e = 0; /* expoent */ 38 | while (x >= 16) 39 | { 40 | x = (x + 1) >> 1; 41 | e++; 42 | } 43 | if (x < 8) return x; 44 | else return ((e + 1) << 3) | (cast_int(x) - 8); 45 | } 46 | 47 | 48 | /* converts back */ 49 | int luaO_fb2int(int x) 50 | { 51 | int e = (x >> 3) & 31; 52 | if (e == 0) return x; 53 | else return ((x & 7) + 8) << (e - 1); 54 | } 55 | 56 | 57 | int luaO_log2(unsigned int x) 58 | { 59 | static const lu_byte log_2[256] = 60 | { 61 | 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 62 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 63 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 64 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 65 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 66 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 67 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 68 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 69 | }; 70 | int l = -1; 71 | while (x >= 256) 72 | { 73 | l += 8; 74 | x >>= 8; 75 | } 76 | return l + log_2[x]; 77 | 78 | } 79 | 80 | 81 | int luaO_rawequalObj(const TValue *t1, const TValue *t2) 82 | { 83 | if (ttype(t1) != ttype(t2)) return 0; 84 | else switch (ttype(t1)) 85 | { 86 | case LUA_TNIL: 87 | return 1; 88 | case LUA_TNUMBER: 89 | return luai_numeq(nvalue(t1), nvalue(t2)); 90 | case LUA_TBOOLEAN: 91 | return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ 92 | case LUA_TLIGHTUSERDATA: 93 | case LUA_TROTABLE: 94 | case LUA_TLIGHTFUNCTION: 95 | return pvalue(t1) == pvalue(t2); 96 | default: 97 | lua_assert(iscollectable(t1)); 98 | return gcvalue(t1) == gcvalue(t2); 99 | } 100 | } 101 | 102 | 103 | int luaO_str2d(const char *s, lua_Number *result) 104 | { 105 | char *endptr; 106 | *result = lua_str2number(s, &endptr); 107 | if (endptr == s) return 0; /* conversion failed */ 108 | if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */ 109 | *result = cast_num(strtoul(s, &endptr, 16)); 110 | if (*endptr == '\0') return 1; /* most common case */ 111 | while (isspace(cast(unsigned char, *endptr))) endptr++; 112 | if (*endptr != '\0') return 0; /* invalid trailing characters? */ 113 | return 1; 114 | } 115 | 116 | 117 | 118 | static void pushstr(lua_State *L, const char *str) 119 | { 120 | setsvalue2s(L, L->top, luaS_new(L, str)); 121 | incr_top(L); 122 | } 123 | 124 | 125 | /* this function handles only `%d', `%c', %f, %p, and `%s' formats */ 126 | const char *luaO_pushvfstring(lua_State *L, const char *fmt, va_list argp) 127 | { 128 | int n = 1; 129 | pushstr(L, ""); 130 | for (;;) 131 | { 132 | const char *e = strchr(fmt, '%'); 133 | if (e == NULL) break; 134 | setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e - fmt)); 135 | incr_top(L); 136 | switch (*(e + 1)) 137 | { 138 | case 's': 139 | { 140 | const char *s = va_arg(argp, char *); 141 | if (s == NULL) s = "(null)"; 142 | pushstr(L, s); 143 | break; 144 | } 145 | case 'c': 146 | { 147 | char buff[2]; 148 | buff[0] = cast(char, va_arg(argp, int)); 149 | buff[1] = '\0'; 150 | pushstr(L, buff); 151 | break; 152 | } 153 | case 'd': 154 | { 155 | setnvalue(L->top, cast_num(va_arg(argp, int))); 156 | incr_top(L); 157 | break; 158 | } 159 | case 'f': 160 | { 161 | setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); 162 | incr_top(L); 163 | break; 164 | } 165 | case 'p': 166 | { 167 | char buff[4 * sizeof(void *) + 8]; /* should be enough space for a `%p' */ 168 | sprintf(buff, "%p", va_arg(argp, void *)); 169 | pushstr(L, buff); 170 | break; 171 | } 172 | case '%': 173 | { 174 | pushstr(L, "%"); 175 | break; 176 | } 177 | default: 178 | { 179 | char buff[3]; 180 | buff[0] = '%'; 181 | buff[1] = *(e + 1); 182 | buff[2] = '\0'; 183 | pushstr(L, buff); 184 | break; 185 | } 186 | } 187 | n += 2; 188 | fmt = e + 2; 189 | } 190 | pushstr(L, fmt); 191 | luaV_concat(L, n + 1, cast_int(L->top - L->base) - 1); 192 | L->top -= n; 193 | return svalue(L->top - 1); 194 | } 195 | 196 | 197 | const char *luaO_pushfstring(lua_State *L, const char *fmt, ...) 198 | { 199 | const char *msg; 200 | va_list argp; 201 | va_start(argp, fmt); 202 | msg = luaO_pushvfstring(L, fmt, argp); 203 | va_end(argp); 204 | return msg; 205 | } 206 | 207 | 208 | void luaO_chunkid(char *out, const char *source, size_t bufflen) 209 | { 210 | if (*source == '=') 211 | { 212 | strncpy(out, source + 1, bufflen); /* remove first char */ 213 | out[bufflen - 1] = '\0'; /* ensures null termination */ 214 | } 215 | else /* out = "source", or "...source" */ 216 | { 217 | if (*source == '@') 218 | { 219 | size_t l; 220 | source++; /* skip the `@' */ 221 | bufflen -= sizeof(" '...' "); 222 | l = strlen(source); 223 | strcpy(out, ""); 224 | if (l > bufflen) 225 | { 226 | source += (l - bufflen); /* get last part of file name */ 227 | strcat(out, "..."); 228 | } 229 | strcat(out, source); 230 | } 231 | else /* out = [string "string"] */ 232 | { 233 | size_t len = strcspn(source, "\n\r"); /* stop at first newline */ 234 | bufflen -= sizeof(" [string \"...\"] "); 235 | if (len > bufflen) len = bufflen; 236 | strcpy(out, "[string \""); 237 | if (source[len] != '\0') /* must truncate? */ 238 | { 239 | strncat(out, source, len); 240 | strcat(out, "..."); 241 | } 242 | else 243 | strcat(out, source); 244 | strcat(out, "\"]"); 245 | } 246 | } 247 | } 248 | -------------------------------------------------------------------------------- /lua-5.1.4/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** See Copyright Notice in lua.h 4 | */ 5 | 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | 11 | #include "lopcodes.h" 12 | 13 | 14 | /* ORDER OP */ 15 | 16 | const char *const luaP_opnames[NUM_OPCODES + 1] = 17 | { 18 | "MOVE", 19 | "LOADK", 20 | "LOADBOOL", 21 | "LOADNIL", 22 | "GETUPVAL", 23 | "GETGLOBAL", 24 | "GETTABLE", 25 | "SETGLOBAL", 26 | "SETUPVAL", 27 | "SETTABLE", 28 | "NEWTABLE", 29 | "SELF", 30 | "ADD", 31 | "SUB", 32 | "MUL", 33 | "DIV", 34 | "MOD", 35 | "POW", 36 | "UNM", 37 | "NOT", 38 | "LEN", 39 | "CONCAT", 40 | "JMP", 41 | "EQ", 42 | "LT", 43 | "LE", 44 | "TEST", 45 | "TESTSET", 46 | "CALL", 47 | "TAILCALL", 48 | "RETURN", 49 | "FORLOOP", 50 | "FORPREP", 51 | "TFORLOOP", 52 | "SETLIST", 53 | "CLOSE", 54 | "CLOSURE", 55 | "VARARG", 56 | NULL 57 | }; 58 | 59 | 60 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 61 | 62 | const lu_byte luaP_opmodes[NUM_OPCODES] = 63 | { 64 | /* T A B C mode opcode */ 65 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 66 | , opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 67 | , opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 68 | , opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */ 69 | , opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 70 | , opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */ 71 | , opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 72 | , opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */ 73 | , opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 74 | , opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 75 | , opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 76 | , opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 77 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 78 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 79 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 80 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 81 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 82 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 83 | , opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 84 | , opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 85 | , opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 86 | , opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 87 | , opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 88 | , opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 89 | , opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 90 | , opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 91 | , opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */ 92 | , opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 93 | , opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 94 | , opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 95 | , opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 96 | , opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 97 | , opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 98 | , opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */ 99 | , opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 100 | , opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ 101 | , opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 102 | , opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 103 | }; 104 | 105 | -------------------------------------------------------------------------------- /lua-5.1.4/loslib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $ 3 | ** Standard Operating System library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define loslib_c 15 | #define LUA_LIB 16 | 17 | #include "lua.h" 18 | 19 | #include "lauxlib.h" 20 | #include "lualib.h" 21 | #include "lrotable.h" 22 | 23 | static int os_pushresult(lua_State *L, int i, const char *filename) 24 | { 25 | int en = errno; /* calls to Lua API may change this value */ 26 | if (i) 27 | { 28 | lua_pushboolean(L, 1); 29 | return 1; 30 | } 31 | else 32 | { 33 | lua_pushnil(L); 34 | lua_pushfstring(L, "%s: %s", filename, strerror(en)); 35 | lua_pushinteger(L, en); 36 | return 3; 37 | } 38 | } 39 | 40 | static int os_execute(lua_State *L) 41 | { 42 | lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); 43 | return 1; 44 | } 45 | 46 | static int os_remove(lua_State *L) 47 | { 48 | const char *filename = luaL_checkstring(L, 1); 49 | return os_pushresult(L, remove(filename) == 0, filename); 50 | } 51 | 52 | static int os_rename(lua_State *L) 53 | { 54 | const char *fromname = luaL_checkstring(L, 1); 55 | const char *toname = luaL_checkstring(L, 2); 56 | return os_pushresult(L, rename(fromname, toname) == 0, fromname); 57 | } 58 | 59 | static int os_tmpname(lua_State *L) 60 | { 61 | char buff[LUA_TMPNAMBUFSIZE]; 62 | int err; 63 | lua_tmpnam(buff, err); 64 | if (err) 65 | return luaL_error(L, "unable to generate a unique filename"); 66 | lua_pushstring(L, buff); 67 | return 1; 68 | } 69 | 70 | static int os_getenv(lua_State *L) 71 | { 72 | lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ 73 | return 1; 74 | } 75 | 76 | #include 77 | static int os_clock(lua_State *L) 78 | { 79 | // lua_pushnumber(L, ((lua_Number)clock()) / (lua_Number)CLOCKS_PER_SEC); 80 | lua_pushnumber(L, ((lua_Number)clock()) / (lua_Number)RT_TICK_PER_SECOND); 81 | return 1; 82 | } 83 | 84 | /* 85 | ** {====================================================== 86 | ** Time/Date operations 87 | ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, 88 | ** wday=%w+1, yday=%j, isdst=? } 89 | ** ======================================================= 90 | */ 91 | static void setfield(lua_State *L, const char *key, int value) 92 | { 93 | lua_pushinteger(L, value); 94 | lua_setfield(L, -2, key); 95 | } 96 | 97 | static void setboolfield(lua_State *L, const char *key, int value) 98 | { 99 | if (value < 0) /* undefined? */ 100 | return; /* does not set field */ 101 | lua_pushboolean(L, value); 102 | lua_setfield(L, -2, key); 103 | } 104 | 105 | static int getboolfield(lua_State *L, const char *key) 106 | { 107 | int res; 108 | lua_getfield(L, -1, key); 109 | res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); 110 | lua_pop(L, 1); 111 | return res; 112 | } 113 | 114 | 115 | static int getfield(lua_State *L, const char *key, int d) 116 | { 117 | int res; 118 | lua_getfield(L, -1, key); 119 | if (lua_isnumber(L, -1)) 120 | res = (int)lua_tointeger(L, -1); 121 | else 122 | { 123 | if (d < 0) 124 | return luaL_error(L, "field " LUA_QS " missing in date table", key); 125 | res = d; 126 | } 127 | lua_pop(L, 1); 128 | return res; 129 | } 130 | 131 | 132 | static int os_date(lua_State *L) 133 | { 134 | const char *s = luaL_optstring(L, 1, "%c"); 135 | time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); 136 | struct tm *stm; 137 | if (*s == '!') /* UTC? */ 138 | { 139 | stm = gmtime(&t); 140 | s++; /* skip `!' */ 141 | } 142 | else 143 | { 144 | stm = localtime(&t); 145 | } 146 | 147 | if (stm == NULL) /* invalid date? */ 148 | { 149 | lua_pushnil(L); 150 | } 151 | else if (strcmp(s, "*t") == 0) 152 | { 153 | lua_createtable(L, 0, 9); /* 9 = number of fields */ 154 | setfield(L, "sec", stm->tm_sec); 155 | setfield(L, "min", stm->tm_min); 156 | setfield(L, "hour", stm->tm_hour); 157 | setfield(L, "day", stm->tm_mday); 158 | setfield(L, "month", stm->tm_mon + 1); 159 | setfield(L, "year", stm->tm_year + 1900); 160 | setfield(L, "wday", stm->tm_wday + 1); 161 | setfield(L, "yday", stm->tm_yday + 1); 162 | setboolfield(L, "isdst", stm->tm_isdst); 163 | } 164 | else 165 | { 166 | char cc[3]; 167 | luaL_Buffer b; 168 | cc[0] = '%'; 169 | cc[2] = '\0'; 170 | luaL_buffinit(L, &b); 171 | for (; *s; s++) 172 | { 173 | if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ 174 | luaL_addchar(&b, *s); 175 | else 176 | { 177 | size_t reslen; 178 | char buff[200]; /* should be big enough for any conversion result */ 179 | cc[1] = *(++s); 180 | reslen = strftime(buff, sizeof(buff), cc, stm); 181 | luaL_addlstring(&b, buff, reslen); 182 | } 183 | } 184 | luaL_pushresult(&b); 185 | } 186 | return 1; 187 | } 188 | 189 | static int os_time(lua_State *L) 190 | { 191 | time_t t; 192 | if (lua_isnoneornil(L, 1)) /* called without args? */ 193 | t = time(NULL); /* get current time */ 194 | else 195 | { 196 | struct tm ts; 197 | luaL_checktype(L, 1, LUA_TTABLE); 198 | lua_settop(L, 1); /* make sure table is at the top */ 199 | ts.tm_sec = getfield(L, "sec", 0); 200 | ts.tm_min = getfield(L, "min", 0); 201 | ts.tm_hour = getfield(L, "hour", 12); 202 | ts.tm_mday = getfield(L, "day", -1); 203 | ts.tm_mon = getfield(L, "month", -1) - 1; 204 | ts.tm_year = getfield(L, "year", -1) - 1900; 205 | ts.tm_isdst = getboolfield(L, "isdst"); 206 | t = mktime(&ts); 207 | } 208 | if (t == (time_t)(-1)) 209 | lua_pushnil(L); 210 | else 211 | lua_pushnumber(L, (lua_Number)t); 212 | return 1; 213 | } 214 | 215 | #if !defined LUA_NUMBER_INTEGRAL 216 | static int os_difftime(lua_State *L) 217 | { 218 | lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), 219 | (time_t)(luaL_optnumber(L, 2, 0)))); 220 | return 1; 221 | } 222 | #endif 223 | 224 | /* }====================================================== */ 225 | static int os_setlocale(lua_State *L) 226 | { 227 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, 228 | LC_NUMERIC, LC_TIME 229 | }; 230 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary", 231 | "numeric", "time", NULL 232 | }; 233 | const char *l = luaL_optstring(L, 1, NULL); 234 | int op = luaL_checkoption(L, 2, "all", catnames); 235 | lua_pushstring(L, setlocale(cat[op], l)); 236 | return 1; 237 | } 238 | 239 | static int os_exit(lua_State *L) 240 | { 241 | exit(luaL_optint(L, 1, EXIT_SUCCESS)); 242 | } 243 | 244 | #define MIN_OPT_LEVEL 1 245 | #include "lrodefs.h" 246 | const LUA_REG_TYPE syslib[] = 247 | { 248 | {LSTRKEY("clock"), LFUNCVAL(os_clock) }, 249 | {LSTRKEY("date"), LFUNCVAL(os_date) }, 250 | #if !defined LUA_NUMBER_INTEGRAL 251 | {LSTRKEY("difftime"), LFUNCVAL(os_difftime) }, 252 | #endif 253 | {LSTRKEY("execute"), LFUNCVAL(os_execute) }, 254 | {LSTRKEY("exit"), LFUNCVAL(os_exit) }, 255 | {LSTRKEY("getenv"), LFUNCVAL(os_getenv) }, 256 | {LSTRKEY("remove"), LFUNCVAL(os_remove) }, 257 | {LSTRKEY("rename"), LFUNCVAL(os_rename) }, 258 | {LSTRKEY("setlocale"), LFUNCVAL(os_setlocale)}, 259 | {LSTRKEY("time"), LFUNCVAL(os_time) }, 260 | {LSTRKEY("tmpname"), LFUNCVAL(os_tmpname) }, 261 | {LNILKEY, LNILVAL} 262 | }; 263 | 264 | LUALIB_API int luaopen_os(lua_State *L) 265 | { 266 | LREGISTER(L, LUA_OSLIBNAME, syslib); 267 | } 268 | -------------------------------------------------------------------------------- /lua-5.1.4/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum 20 | { 21 | VVOID, /* no value */ 22 | VNIL, 23 | VTRUE, 24 | VFALSE, 25 | VK, /* info = index of constant in `k' */ 26 | VKNUM, /* nval = numerical value */ 27 | VLOCAL, /* info = local register */ 28 | VUPVAL, /* info = index of upvalue in `upvalues' */ 29 | VGLOBAL, /* info = index of table; aux = index of global name in `k' */ 30 | VINDEXED, /* info = table register; aux = index register (or `k') */ 31 | VJMP, /* info = instruction pc */ 32 | VRELOCABLE, /* info = instruction pc */ 33 | VNONRELOC, /* info = result register */ 34 | VCALL, /* info = instruction pc */ 35 | VVARARG /* info = instruction pc */ 36 | } expkind; 37 | 38 | typedef struct expdesc 39 | { 40 | expkind k; 41 | union 42 | { 43 | struct 44 | { 45 | int info, aux; 46 | } s; 47 | lua_Number nval; 48 | } u; 49 | int t; /* patch list of `exit when true' */ 50 | int f; /* patch list of `exit when false' */ 51 | } expdesc; 52 | 53 | 54 | typedef struct upvaldesc 55 | { 56 | lu_byte k; 57 | lu_byte info; 58 | } upvaldesc; 59 | 60 | 61 | struct BlockCnt; /* defined in lparser.c */ 62 | 63 | 64 | /* state needed to generate code for a given function */ 65 | typedef struct FuncState 66 | { 67 | Proto *f; /* current function header */ 68 | Table *h; /* table to find (and reuse) elements in `k' */ 69 | struct FuncState *prev; /* enclosing function */ 70 | struct LexState *ls; /* lexical state */ 71 | struct lua_State *L; /* copy of the Lua state */ 72 | struct BlockCnt *bl; /* chain of current blocks */ 73 | int pc; /* next position to code (equivalent to `ncode') */ 74 | int lasttarget; /* `pc' of last `jump target' */ 75 | int jpc; /* list of pending jumps to `pc' */ 76 | int freereg; /* first free register */ 77 | int nk; /* number of elements in `k' */ 78 | int np; /* number of elements in `p' */ 79 | short nlocvars; /* number of elements in `locvars' */ 80 | lu_byte nactvar; /* number of active local variables */ 81 | upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */ 82 | unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */ 83 | } FuncState; 84 | 85 | 86 | LUAI_FUNC Proto *luaY_parser(lua_State *L, ZIO *z, Mbuffer *buff, 87 | const char *name); 88 | 89 | 90 | #endif 91 | -------------------------------------------------------------------------------- /lua-5.1.4/lrodefs.h: -------------------------------------------------------------------------------- 1 | /* Read-only tables helper */ 2 | 3 | #ifndef lrodefs_h 4 | #define lrodefs_h 5 | 6 | #include "lrotable.h" 7 | 8 | #undef LUA_REG_TYPE 9 | #undef LSTRKEY 10 | #undef LNILKEY 11 | #undef LNUMKEY 12 | #undef LFUNCVAL 13 | #undef LNUMVAL 14 | #undef LROVAL 15 | #undef LNILVAL 16 | #undef LREGISTER 17 | 18 | #if (MIN_OPT_LEVEL > 0) && (LUA_OPTIMIZE_MEMORY >= MIN_OPT_LEVEL) 19 | #define LUA_REG_TYPE luaR_entry 20 | #define LSTRKEY LRO_STRKEY 21 | #define LNUMKEY LRO_NUMKEY 22 | #define LNILKEY LRO_NILKEY 23 | #define LFUNCVAL LRO_FUNCVAL 24 | #define LNUMVAL LRO_NUMVAL 25 | #define LROVAL LRO_ROVAL 26 | #define LNILVAL LRO_NILVAL 27 | #define LREGISTER(L, name, table) \ 28 | return 0 29 | #else 30 | #define LUA_REG_TYPE luaL_reg 31 | #define LSTRKEY(x) x 32 | #define LNILKEY NULL 33 | #define LFUNCVAL(x) x 34 | #define LNILVAL NULL 35 | #define LREGISTER(L, name, table) \ 36 | luaL_register(L, name, table); \ 37 | return 1 38 | #endif 39 | 40 | #endif /* lrodefs_h */ 41 | -------------------------------------------------------------------------------- /lua-5.1.4/lrotable.c: -------------------------------------------------------------------------------- 1 | /* Read-only tables for Lua */ 2 | 3 | #include 4 | #include "lrotable.h" 5 | #include "lua.h" 6 | #include "lauxlib.h" 7 | #include "lstring.h" 8 | #include "lobject.h" 9 | #include "lapi.h" 10 | 11 | /* Local defines */ 12 | #define LUAR_FINDFUNCTION 0 13 | #define LUAR_FINDVALUE 1 14 | 15 | /* Externally defined read-only table array */ 16 | extern const luaR_table lua_rotable[]; 17 | 18 | /* Find a global "read only table" in the constant lua_rotable array */ 19 | void *luaR_findglobal(const char *name, unsigned len) 20 | { 21 | unsigned i; 22 | 23 | if (strlen(name) > LUA_MAX_ROTABLE_NAME) 24 | return NULL; 25 | for (i = 0; lua_rotable[i].name; i ++) 26 | if (*lua_rotable[i].name != '\0' && strlen(lua_rotable[i].name) == len && !strncmp(lua_rotable[i].name, name, len)) 27 | { 28 | return (void *)(lua_rotable[i].pentries); 29 | } 30 | return NULL; 31 | } 32 | 33 | /* Find an entry in a rotable and return it */ 34 | static const TValue *luaR_auxfind(const luaR_entry *pentry, const char *strkey, luaR_numkey numkey, unsigned *ppos) 35 | { 36 | const TValue *res = NULL; 37 | unsigned i = 0; 38 | 39 | if (pentry == NULL) 40 | return NULL; 41 | while (pentry->key.type != LUA_TNIL) 42 | { 43 | if ((strkey && (pentry->key.type == LUA_TSTRING) && (!strcmp(pentry->key.id.strkey, strkey))) || 44 | (!strkey && (pentry->key.type == LUA_TNUMBER) && ((luaR_numkey)pentry->key.id.numkey == numkey))) 45 | { 46 | res = &pentry->value; 47 | break; 48 | } 49 | i ++; 50 | pentry ++; 51 | } 52 | if (res && ppos) 53 | *ppos = i; 54 | return res; 55 | } 56 | 57 | int luaR_findfunction(lua_State *L, const luaR_entry *ptable) 58 | { 59 | const TValue *res = NULL; 60 | const char *key = luaL_checkstring(L, 2); 61 | 62 | res = luaR_auxfind(ptable, key, 0, NULL); 63 | if (res && ttislightfunction(res)) 64 | { 65 | luaA_pushobject(L, res); 66 | return 1; 67 | } 68 | else 69 | return 0; 70 | } 71 | 72 | /* Find an entry in a rotable and return its type 73 | If "strkey" is not NULL, the function will look for a string key, 74 | otherwise it will look for a number key */ 75 | const TValue *luaR_findentry(void *data, const char *strkey, luaR_numkey numkey, unsigned *ppos) 76 | { 77 | return luaR_auxfind((const luaR_entry *)data, strkey, numkey, ppos); 78 | } 79 | 80 | /* Find the metatable of a given table */ 81 | void *luaR_getmeta(void *data) 82 | { 83 | #ifdef LUA_META_ROTABLES 84 | const TValue *res = luaR_auxfind((const luaR_entry *)data, "__metatable", 0, NULL); 85 | return res && ttisrotable(res) ? rvalue(res) : NULL; 86 | #else 87 | return NULL; 88 | #endif 89 | } 90 | 91 | static void luaR_next_helper(lua_State *L, const luaR_entry *pentries, int pos, TValue *key, TValue *val) 92 | { 93 | setnilvalue(key); 94 | setnilvalue(val); 95 | if (pentries[pos].key.type != LUA_TNIL) 96 | { 97 | /* Found an entry */ 98 | if (pentries[pos].key.type == LUA_TSTRING) 99 | setsvalue(L, key, luaS_newro(L, pentries[pos].key.id.strkey)) 100 | else 101 | setnvalue(key, (lua_Number)pentries[pos].key.id.numkey) 102 | setobj2s(L, val, &pentries[pos].value); 103 | } 104 | } 105 | /* next (used for iteration) */ 106 | void luaR_next(lua_State *L, void *data, TValue *key, TValue *val) 107 | { 108 | const luaR_entry *pentries = (const luaR_entry *)data; 109 | char strkey[LUA_MAX_ROTABLE_NAME + 1], *pstrkey = NULL; 110 | luaR_numkey numkey = 0; 111 | unsigned keypos; 112 | 113 | /* Special case: if key is nil, return the first element of the rotable */ 114 | if (ttisnil(key)) 115 | luaR_next_helper(L, pentries, 0, key, val); 116 | else if (ttisstring(key) || ttisnumber(key)) 117 | { 118 | /* Find the previoud key again */ 119 | if (ttisstring(key)) 120 | { 121 | luaR_getcstr(strkey, rawtsvalue(key), LUA_MAX_ROTABLE_NAME); 122 | pstrkey = strkey; 123 | } 124 | else 125 | numkey = (luaR_numkey)nvalue(key); 126 | luaR_findentry(data, pstrkey, numkey, &keypos); 127 | /* Advance to next key */ 128 | keypos ++; 129 | luaR_next_helper(L, pentries, keypos, key, val); 130 | } 131 | } 132 | 133 | /* Convert a Lua string to a C string */ 134 | void luaR_getcstr(char *dest, const TString *src, size_t maxsize) 135 | { 136 | if (src->tsv.len + 1 > maxsize) 137 | dest[0] = '\0'; 138 | else 139 | { 140 | memcpy(dest, getstr(src), src->tsv.len); 141 | dest[src->tsv.len] = '\0'; 142 | } 143 | } 144 | 145 | /* Return 1 if the given pointer is a rotable */ 146 | #ifdef LUA_META_ROTABLES 147 | 148 | #include "compiler.h" 149 | 150 | int luaR_isrotable(void *p) 151 | { 152 | return RODATA_START_ADDRESS <= (char *)p && (char *)p <= RODATA_END_ADDRESS; 153 | } 154 | #endif 155 | -------------------------------------------------------------------------------- /lua-5.1.4/lrotable.h: -------------------------------------------------------------------------------- 1 | /* Read-only tables for Lua */ 2 | 3 | #ifndef lrotable_h 4 | #define lrotable_h 5 | 6 | #include "lua.h" 7 | #include "llimits.h" 8 | #include "lobject.h" 9 | #include "luaconf.h" 10 | 11 | /* Macros one can use to define rotable entries */ 12 | #ifndef LUA_PACK_VALUE 13 | #define LRO_FUNCVAL(v) {{.p = v}, LUA_TLIGHTFUNCTION} 14 | #define LRO_NUMVAL(v) {{.n = v}, LUA_TNUMBER} 15 | #define LRO_ROVAL(v) {{.p = (void*)v}, LUA_TROTABLE} 16 | #define LRO_NILVAL {{.p = NULL}, LUA_TNIL} 17 | #else // #ifndef LUA_PACK_VALUE 18 | #define LRO_NUMVAL(v) {.value.n = v} 19 | #ifdef ELUA_ENDIAN_LITTLE 20 | #define LRO_FUNCVAL(v) {{(int)v, add_sig(LUA_TLIGHTFUNCTION)}} 21 | #define LRO_ROVAL(v) {{(int)v, add_sig(LUA_TROTABLE)}} 22 | #define LRO_NILVAL {{0, add_sig(LUA_TNIL)}} 23 | #else // #ifdef ELUA_ENDIAN_LITTLE 24 | #define LRO_FUNCVAL(v) {{add_sig(LUA_TLIGHTFUNCTION), (int)v}} 25 | #define LRO_ROVAL(v) {{add_sig(LUA_TROTABLE), (int)v}} 26 | #define LRO_NILVAL {{add_sig(LUA_TNIL), 0}} 27 | #endif // #ifdef ELUA_ENDIAN_LITTLE 28 | #endif // #ifndef LUA_PACK_VALUE 29 | 30 | #define LRO_STRKEY(k) {LUA_TSTRING, {.strkey = k}} 31 | #define LRO_NUMKEY(k) {LUA_TNUMBER, {.numkey = k}} 32 | #define LRO_NILKEY {LUA_TNIL, {.strkey=NULL}} 33 | 34 | /* Maximum length of a rotable name and of a string key*/ 35 | #define LUA_MAX_ROTABLE_NAME 32 36 | 37 | /* Type of a numeric key in a rotable */ 38 | typedef int luaR_numkey; 39 | 40 | /* The next structure defines the type of a key */ 41 | typedef struct 42 | { 43 | int type; 44 | union 45 | { 46 | const char *strkey; 47 | luaR_numkey numkey; 48 | } id; 49 | } luaR_key; 50 | 51 | /* An entry in the read only table */ 52 | typedef struct 53 | { 54 | const luaR_key key; 55 | const TValue value; 56 | } luaR_entry; 57 | 58 | /* A rotable */ 59 | typedef struct 60 | { 61 | const char *name; 62 | const luaR_entry *pentries; 63 | } luaR_table; 64 | 65 | void *luaR_findglobal(const char *key, unsigned len); 66 | int luaR_findfunction(lua_State *L, const luaR_entry *ptable); 67 | const TValue *luaR_findentry(void *data, const char *strkey, luaR_numkey numkey, unsigned *ppos); 68 | void luaR_getcstr(char *dest, const TString *src, size_t maxsize); 69 | void luaR_next(lua_State *L, void *data, TValue *key, TValue *val); 70 | void *luaR_getmeta(void *data); 71 | #ifdef LUA_META_ROTABLES 72 | int luaR_isrotable(void *p); 73 | #else 74 | #define luaR_isrotable(p) (0) 75 | #endif 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /lua-5.1.4/lstate.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstate_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lfunc.h" 18 | #include "lgc.h" 19 | #include "llex.h" 20 | #include "lmem.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | #include "ltable.h" 24 | #include "ltm.h" 25 | 26 | #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) 27 | #define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) 28 | #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) 29 | 30 | 31 | /* 32 | ** Main thread combines a thread state and the global state 33 | */ 34 | typedef struct LG 35 | { 36 | lua_State l; 37 | global_State g; 38 | } LG; 39 | 40 | 41 | 42 | static void stack_init(lua_State *L1, lua_State *L) 43 | { 44 | /* initialize CallInfo array */ 45 | L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo); 46 | L1->ci = L1->base_ci; 47 | L1->size_ci = BASIC_CI_SIZE; 48 | L1->end_ci = L1->base_ci + L1->size_ci - 1; 49 | /* initialize stack array */ 50 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue); 51 | L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; 52 | L1->top = L1->stack; 53 | L1->stack_last = L1->stack + (L1->stacksize - EXTRA_STACK) - 1; 54 | /* initialize first ci */ 55 | L1->ci->func = L1->top; 56 | setnilvalue(L1->top++); /* `function' entry for this `ci' */ 57 | L1->base = L1->ci->base = L1->top; 58 | L1->ci->top = L1->top + LUA_MINSTACK; 59 | } 60 | 61 | 62 | static void freestack(lua_State *L, lua_State *L1) 63 | { 64 | luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo); 65 | luaM_freearray(L, L1->stack, L1->stacksize, TValue); 66 | } 67 | 68 | 69 | /* 70 | ** open parts that may cause memory-allocation errors 71 | */ 72 | static void f_luaopen(lua_State *L, void *ud) 73 | { 74 | global_State *g = G(L); 75 | UNUSED(ud); 76 | stack_init(L, L); /* init stack */ 77 | sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ 78 | sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ 79 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 80 | luaT_init(L); 81 | luaX_init(L); 82 | luaS_fix(luaS_newliteral(L, MEMERRMSG)); 83 | g->GCthreshold = 4 * g->totalbytes; 84 | } 85 | 86 | 87 | static void preinit_state(lua_State *L, global_State *g) 88 | { 89 | G(L) = g; 90 | L->stack = NULL; 91 | L->stacksize = 0; 92 | L->errorJmp = NULL; 93 | L->hook = NULL; 94 | L->hookmask = 0; 95 | L->basehookcount = 0; 96 | L->allowhook = 1; 97 | resethookcount(L); 98 | L->openupval = NULL; 99 | L->size_ci = 0; 100 | L->nCcalls = L->baseCcalls = 0; 101 | L->status = 0; 102 | L->base_ci = L->ci = NULL; 103 | L->savedpc = NULL; 104 | L->errfunc = 0; 105 | setnilvalue(gt(L)); 106 | } 107 | 108 | 109 | static void close_state(lua_State *L) 110 | { 111 | global_State *g = G(L); 112 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 113 | luaC_freeall(L); /* collect all objects */ 114 | lua_assert(g->rootgc == obj2gco(L)); 115 | lua_assert(g->strt.nuse == 0); 116 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); 117 | luaZ_freebuffer(L, &g->buff); 118 | freestack(L, L); 119 | lua_assert(g->totalbytes == sizeof(LG)); 120 | (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); 121 | } 122 | 123 | 124 | lua_State *luaE_newthread(lua_State *L) 125 | { 126 | lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); 127 | luaC_link(L, obj2gco(L1), LUA_TTHREAD); 128 | setthvalue(L, L->top, L1); /* put thread on stack */ 129 | incr_top(L); 130 | preinit_state(L1, G(L)); 131 | stack_init(L1, L); /* init stack */ 132 | setobj2n(L, gt(L1), gt(L)); /* share table of globals */ 133 | L1->hookmask = L->hookmask; 134 | L1->basehookcount = L->basehookcount; 135 | L1->hook = L->hook; 136 | resethookcount(L1); 137 | lua_assert(!isdead(G(L), obj2gco(L1))); 138 | L->top--; /* remove thread from stack */ 139 | return L1; 140 | } 141 | 142 | 143 | void luaE_freethread(lua_State *L, lua_State *L1) 144 | { 145 | luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 146 | lua_assert(L1->openupval == NULL); 147 | luai_userstatefree(L1); 148 | freestack(L, L1); 149 | luaM_freemem(L, fromstate(L1), state_size(lua_State)); 150 | } 151 | 152 | 153 | LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) 154 | { 155 | int i; 156 | lua_State *L; 157 | global_State *g; 158 | void *l = (*f)(ud, NULL, 0, state_size(LG)); 159 | if (l == NULL) return NULL; 160 | L = tostate(l); 161 | g = &((LG *)L)->g; 162 | L->next = NULL; 163 | L->tt = LUA_TTHREAD; 164 | g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); 165 | L->marked = luaC_white(g); 166 | set2bits(L->marked, FIXEDBIT, SFIXEDBIT); 167 | preinit_state(L, g); 168 | g->frealloc = f; 169 | g->ud = ud; 170 | g->mainthread = L; 171 | g->uvhead.u.l.prev = &g->uvhead; 172 | g->uvhead.u.l.next = &g->uvhead; 173 | g->GCthreshold = 0; /* mark it as unfinished state */ 174 | g->estimate = 0; 175 | g->strt.size = 0; 176 | g->strt.nuse = 0; 177 | g->strt.hash = NULL; 178 | setnilvalue(registry(L)); 179 | luaZ_initbuffer(L, &g->buff); 180 | g->panic = NULL; 181 | g->gcstate = GCSpause; 182 | g->gcflags = GCFlagsNone; 183 | g->rootgc = obj2gco(L); 184 | g->sweepstrgc = 0; 185 | g->sweepgc = &g->rootgc; 186 | g->gray = NULL; 187 | g->grayagain = NULL; 188 | g->weak = NULL; 189 | g->tmudata = NULL; 190 | g->totalbytes = sizeof(LG); 191 | g->memlimit = 0; 192 | g->gcpause = LUAI_GCPAUSE; 193 | g->gcstepmul = LUAI_GCMUL; 194 | g->gcdept = 0; 195 | #ifdef EGC_INITIAL_MODE 196 | g->egcmode = EGC_INITIAL_MODE; 197 | #else 198 | g->egcmode = 0; 199 | #endif 200 | #ifdef EGC_INITIAL_MEMLIMIT 201 | g->memlimit = EGC_INITIAL_MEMLIMIT; 202 | #else 203 | g->memlimit = 0; 204 | #endif 205 | for (i = 0; i < NUM_TAGS; i++) g->mt[i] = NULL; 206 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) 207 | { 208 | /* memory allocation error: free partial state */ 209 | close_state(L); 210 | L = NULL; 211 | } 212 | else 213 | luai_userstateopen(L); 214 | return L; 215 | } 216 | 217 | 218 | static void callallgcTM(lua_State *L, void *ud) 219 | { 220 | UNUSED(ud); 221 | luaC_callGCTM(L); /* call GC metamethods for all udata */ 222 | } 223 | 224 | // BogdanM: modified for eLua interrupt support 225 | extern lua_State *luaL_newstate(void); 226 | static lua_State *lua_crtstate; 227 | 228 | lua_State *lua_open(void) 229 | { 230 | lua_crtstate = luaL_newstate(); 231 | return lua_crtstate; 232 | } 233 | 234 | lua_State *lua_getstate(void) 235 | { 236 | return lua_crtstate; 237 | } 238 | LUA_API void lua_close(lua_State *L) 239 | { 240 | #ifndef LUA_CROSS_COMPILER 241 | lua_sethook(L, NULL, 0, 0); 242 | lua_crtstate = NULL; 243 | lua_pushnil(L); 244 | // lua_rawseti( L, LUA_REGISTRYINDEX, LUA_INT_HANDLER_KEY ); 245 | #endif 246 | L = G(L)->mainthread; /* only the main thread can be closed */ 247 | lua_lock(L); 248 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 249 | luaC_separateudata(L, 1); /* separate udata that have GC metamethods */ 250 | L->errfunc = 0; /* no error function during GC metamethods */ 251 | do /* repeat until no more errors */ 252 | { 253 | L->ci = L->base_ci; 254 | L->base = L->top = L->ci->base; 255 | L->nCcalls = L->baseCcalls = 0; 256 | } 257 | while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); 258 | lua_assert(G(L)->tmudata == NULL); 259 | luai_userstateclose(L); 260 | close_state(L); 261 | } 262 | 263 | -------------------------------------------------------------------------------- /lua-5.1.4/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstate_h 8 | #define lstate_h 9 | 10 | #include "lua.h" 11 | 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | #include "lzio.h" 15 | 16 | 17 | 18 | struct lua_longjmp; /* defined in ldo.c */ 19 | 20 | 21 | /* table of globals */ 22 | #define gt(L) (&L->l_gt) 23 | 24 | /* registry */ 25 | #define registry(L) (&G(L)->l_registry) 26 | 27 | 28 | /* extra stack space to handle TM calls and some other extras */ 29 | #define EXTRA_STACK 5 30 | 31 | 32 | #define BASIC_CI_SIZE 8 33 | 34 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 35 | 36 | 37 | 38 | typedef struct stringtable 39 | { 40 | GCObject **hash; 41 | lu_int32 nuse; /* number of elements */ 42 | int size; 43 | } stringtable; 44 | 45 | 46 | /* 47 | ** informations about a call 48 | */ 49 | typedef struct CallInfo 50 | { 51 | StkId base; /* base for this function */ 52 | StkId func; /* function index in the stack */ 53 | StkId top; /* top for this function */ 54 | const Instruction *savedpc; 55 | int nresults; /* expected number of results from this function */ 56 | int tailcalls; /* number of tail calls lost under this entry */ 57 | } CallInfo; 58 | 59 | 60 | 61 | #define curr_func(L) (ttisfunction(L->ci->func) ? clvalue(L->ci->func) : NULL) 62 | #define ci_func(ci) (ttisfunction((ci)->func) ? clvalue((ci)->func) : NULL) 63 | #define f_isLua(ci) (!ttislightfunction((ci)->func) && !ci_func(ci)->c.isC) 64 | #define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci)) 65 | 66 | 67 | /* 68 | ** `global state', shared by all threads of this state 69 | */ 70 | typedef struct global_State 71 | { 72 | stringtable strt; /* hash table for strings */ 73 | lua_Alloc frealloc; /* function to reallocate memory */ 74 | void *ud; /* auxiliary data to `frealloc' */ 75 | lu_byte currentwhite; 76 | lu_byte gcstate; /* state of garbage collector */ 77 | lu_byte gcflags; /* flags for the garbage collector */ 78 | int sweepstrgc; /* position of sweep in `strt' */ 79 | GCObject *rootgc; /* list of all collectable objects */ 80 | GCObject **sweepgc; /* position of sweep in `rootgc' */ 81 | GCObject *gray; /* list of gray objects */ 82 | GCObject *grayagain; /* list of objects to be traversed atomically */ 83 | GCObject *weak; /* list of weak tables (to be cleared) */ 84 | GCObject *tmudata; /* last element of list of userdata to be GC */ 85 | Mbuffer buff; /* temporary buffer for string concatentation */ 86 | lu_mem GCthreshold; 87 | lu_mem totalbytes; /* number of bytes currently allocated */ 88 | lu_mem memlimit; /* maximum number of bytes that can be allocated, 0 = no limit. */ 89 | lu_mem estimate; /* an estimate of number of bytes actually in use */ 90 | lu_mem gcdept; /* how much GC is `behind schedule' */ 91 | int gcpause; /* size of pause between successive GCs */ 92 | int gcstepmul; /* GC `granularity' */ 93 | int egcmode; /* emergency garbage collection operation mode */ 94 | lua_CFunction panic; /* to be called in unprotected errors */ 95 | TValue l_registry; 96 | struct lua_State *mainthread; 97 | UpVal uvhead; /* head of double-linked list of all open upvalues */ 98 | struct Table *mt[NUM_TAGS]; /* metatables for basic types */ 99 | TString *tmname[TM_N]; /* array with tag-method names */ 100 | } global_State; 101 | 102 | 103 | /* 104 | ** `per thread' state 105 | */ 106 | struct lua_State 107 | { 108 | CommonHeader; 109 | lu_byte status; 110 | StkId top; /* first free slot in the stack */ 111 | StkId base; /* base of current function */ 112 | global_State *l_G; 113 | CallInfo *ci; /* call info for current function */ 114 | const Instruction *savedpc; /* `savedpc' of current function */ 115 | StkId stack_last; /* last free slot in the stack */ 116 | StkId stack; /* stack base */ 117 | CallInfo *end_ci; /* points after end of ci array*/ 118 | CallInfo *base_ci; /* array of CallInfo's */ 119 | int stacksize; 120 | int size_ci; /* size of array `base_ci' */ 121 | unsigned short nCcalls; /* number of nested C calls */ 122 | unsigned short baseCcalls; /* nested C calls when resuming coroutine */ 123 | lu_byte hookmask; 124 | lu_byte allowhook; 125 | int basehookcount; 126 | int hookcount; 127 | lua_Hook hook; 128 | TValue l_gt; /* table of globals */ 129 | TValue env; /* temporary place for environments */ 130 | GCObject *openupval; /* list of open upvalues in this stack */ 131 | GCObject *gclist; 132 | struct lua_longjmp *errorJmp; /* current error recover point */ 133 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 134 | }; 135 | 136 | 137 | #define G(L) (L->l_G) 138 | 139 | 140 | /* 141 | ** Union of all collectable objects 142 | */ 143 | union GCObject 144 | { 145 | GCheader gch; 146 | union TString ts; 147 | union Udata u; 148 | union Closure cl; 149 | struct Table h; 150 | struct Proto p; 151 | struct UpVal uv; 152 | struct lua_State th; /* thread */ 153 | }; 154 | 155 | 156 | /* macros to convert a GCObject into a specific value */ 157 | #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) 158 | #define gco2ts(o) (&rawgco2ts(o)->tsv) 159 | #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 160 | #define gco2u(o) (&rawgco2u(o)->uv) 161 | #define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) 162 | #define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 163 | #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 164 | #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 165 | #define ngcotouv(o) \ 166 | check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 167 | #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 168 | 169 | /* macro to convert any Lua object into a GCObject */ 170 | 171 | #define obj2gco(v) (cast(GCObject *, (v))) 172 | 173 | LUAI_FUNC lua_State *luaE_newthread(lua_State *L); 174 | LUAI_FUNC void luaE_freethread(lua_State *L, lua_State *L1); 175 | 176 | #endif 177 | 178 | -------------------------------------------------------------------------------- /lua-5.1.4/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstring_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lmem.h" 16 | #include "lobject.h" 17 | #include "lstate.h" 18 | #include "lstring.h" 19 | 20 | #define LUAS_READONLY_STRING 1 21 | #define LUAS_REGULAR_STRING 0 22 | 23 | void luaS_resize(lua_State *L, int newsize) 24 | { 25 | stringtable *tb; 26 | int i; 27 | tb = &G(L)->strt; 28 | if (luaC_sweepstrgc(L) || newsize == tb->size || is_resizing_strings_gc(L)) 29 | return; /* cannot resize during GC traverse or doesn't need to be resized */ 30 | set_resizing_strings_gc(L); 31 | if (newsize > tb->size) 32 | { 33 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 34 | for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; 35 | } 36 | /* rehash */ 37 | for (i = 0; i < tb->size; i++) 38 | { 39 | GCObject *p = tb->hash[i]; 40 | tb->hash[i] = NULL; 41 | while (p) /* for each node in the list */ 42 | { 43 | GCObject *next = p->gch.next; /* save next */ 44 | unsigned int h = gco2ts(p)->hash; 45 | int h1 = lmod(h, newsize); /* new position */ 46 | lua_assert(cast_int(h % newsize) == lmod(h, newsize)); 47 | p->gch.next = tb->hash[h1]; /* chain it */ 48 | tb->hash[h1] = p; 49 | p = next; 50 | } 51 | } 52 | if (newsize < tb->size) 53 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 54 | tb->size = newsize; 55 | unset_resizing_strings_gc(L); 56 | } 57 | 58 | static TString *newlstr(lua_State *L, const char *str, size_t l, 59 | unsigned int h, int readonly) 60 | { 61 | TString *ts; 62 | stringtable *tb; 63 | if (l + 1 > (MAX_SIZET - sizeof(TString)) / sizeof(char)) 64 | luaM_toobig(L); 65 | tb = &G(L)->strt; 66 | if ((tb->nuse + 1) > cast(lu_int32, tb->size) && tb->size <= MAX_INT / 2) 67 | luaS_resize(L, tb->size * 2); /* too crowded */ 68 | ts = cast(TString *, luaM_malloc(L, readonly ? sizeof(char **) + sizeof(TString) : (l + 1) * sizeof(char) + sizeof(TString))); 69 | ts->tsv.len = l; 70 | ts->tsv.hash = h; 71 | ts->tsv.marked = luaC_white(G(L)); 72 | ts->tsv.tt = LUA_TSTRING; 73 | if (!readonly) 74 | { 75 | memcpy(ts + 1, str, l * sizeof(char)); 76 | ((char *)(ts + 1))[l] = '\0'; /* ending 0 */ 77 | } 78 | else 79 | { 80 | *(char **)(ts + 1) = (char *)str; 81 | luaS_readonly(ts); 82 | } 83 | h = lmod(h, tb->size); 84 | ts->tsv.next = tb->hash[h]; /* chain new entry */ 85 | tb->hash[h] = obj2gco(ts); 86 | tb->nuse++; 87 | return ts; 88 | } 89 | 90 | 91 | static TString *luaS_newlstr_helper(lua_State *L, const char *str, size_t l, int readonly) 92 | { 93 | GCObject *o; 94 | unsigned int h = cast(unsigned int, l); /* seed */ 95 | size_t step = (l >> 5) + 1; /* if string is too long, don't hash all its chars */ 96 | size_t l1; 97 | for (l1 = l; l1 >= step; l1 -= step) /* compute hash */ 98 | h = h ^ ((h << 5) + (h >> 2) + cast(unsigned char, str[l1 - 1])); 99 | for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; 100 | o != NULL; 101 | o = o->gch.next) 102 | { 103 | TString *ts = rawgco2ts(o); 104 | if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) 105 | { 106 | /* string may be dead */ 107 | if (isdead(G(L), o)) changewhite(o); 108 | return ts; 109 | } 110 | } 111 | return newlstr(L, str, l, h, readonly); /* not found */ 112 | } 113 | 114 | static int lua_is_ptr_in_ro_area(const char *p) 115 | { 116 | #ifdef LUA_CROSS_COMPILER 117 | return 0; 118 | #else 119 | 120 | #include "compiler.h" 121 | 122 | return p >= RODATA_START_ADDRESS && p <= RODATA_END_ADDRESS; 123 | #endif 124 | } 125 | 126 | TString *luaS_newlstr(lua_State *L, const char *str, size_t l) 127 | { 128 | // If the pointer is in a read-only memory and the string is at least 4 chars in length, 129 | // create it as a read-only string instead 130 | if (lua_is_ptr_in_ro_area(str) && l + 1 > sizeof(char **) && l == strlen(str)) 131 | return luaS_newlstr_helper(L, str, l, LUAS_READONLY_STRING); 132 | else 133 | return luaS_newlstr_helper(L, str, l, LUAS_REGULAR_STRING); 134 | } 135 | 136 | 137 | LUAI_FUNC TString *luaS_newrolstr(lua_State *L, const char *str, size_t l) 138 | { 139 | if (l + 1 > sizeof(char **) && l == strlen(str)) 140 | return luaS_newlstr_helper(L, str, l, LUAS_READONLY_STRING); 141 | else // no point in creating a RO string, as it would actually be larger 142 | return luaS_newlstr_helper(L, str, l, LUAS_REGULAR_STRING); 143 | } 144 | 145 | 146 | Udata *luaS_newudata(lua_State *L, size_t s, Table *e) 147 | { 148 | Udata *u; 149 | if (s > MAX_SIZET - sizeof(Udata)) 150 | luaM_toobig(L); 151 | u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata))); 152 | u->uv.marked = luaC_white(G(L)); /* is not finalized */ 153 | u->uv.tt = LUA_TUSERDATA; 154 | u->uv.len = s; 155 | u->uv.metatable = NULL; 156 | u->uv.env = e; 157 | /* chain it on udata list (after main thread) */ 158 | u->uv.next = G(L)->mainthread->next; 159 | G(L)->mainthread->next = obj2gco(u); 160 | return u; 161 | } 162 | 163 | -------------------------------------------------------------------------------- /lua-5.1.4/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | 11 | #include "lgc.h" 12 | #include "lobject.h" 13 | #include "lstate.h" 14 | 15 | 16 | #define sizestring(s) (sizeof(union TString)+(luaS_isreadonly(s) ? sizeof(char **) : ((s)->len+1)*sizeof(char))) 17 | 18 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 19 | 20 | #define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) 21 | #define luaS_newro(L, s) (luaS_newrolstr(L, s, strlen(s))) 22 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 23 | (sizeof(s)/sizeof(char))-1)) 24 | 25 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 26 | #define luaS_readonly(s) l_setbit((s)->tsv.marked, READONLYBIT) 27 | #define luaS_isreadonly(s) testbit((s)->marked, READONLYBIT) 28 | 29 | LUAI_FUNC void luaS_resize(lua_State *L, int newsize); 30 | LUAI_FUNC Udata *luaS_newudata(lua_State *L, size_t s, Table *e); 31 | LUAI_FUNC TString *luaS_newlstr(lua_State *L, const char *str, size_t l); 32 | LUAI_FUNC TString *luaS_newrolstr(lua_State *L, const char *str, size_t l); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /lua-5.1.4/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.tvk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define key2tval(n) (&(n)->i_key.tvk) 19 | 20 | 21 | LUAI_FUNC const TValue *luaH_getnum(Table *t, int key); 22 | LUAI_FUNC const TValue *luaH_getnum_ro(void *t, int key); 23 | LUAI_FUNC TValue *luaH_setnum(lua_State *L, Table *t, int key); 24 | LUAI_FUNC const TValue *luaH_getstr(Table *t, TString *key); 25 | LUAI_FUNC const TValue *luaH_getstr_ro(void *t, TString *key); 26 | LUAI_FUNC TValue *luaH_setstr(lua_State *L, Table *t, TString *key); 27 | LUAI_FUNC const TValue *luaH_get(Table *t, const TValue *key); 28 | LUAI_FUNC const TValue *luaH_get_ro(void *t, const TValue *key); 29 | LUAI_FUNC TValue *luaH_set(lua_State *L, Table *t, const TValue *key); 30 | LUAI_FUNC Table *luaH_new(lua_State *L, int narray, int lnhash); 31 | LUAI_FUNC void luaH_resizearray(lua_State *L, Table *t, int nasize); 32 | LUAI_FUNC void luaH_free(lua_State *L, Table *t); 33 | LUAI_FUNC int luaH_next(lua_State *L, Table *t, StkId key); 34 | LUAI_FUNC int luaH_next_ro(lua_State *L, void *t, StkId key); 35 | LUAI_FUNC int luaH_getn(Table *t); 36 | LUAI_FUNC int luaH_getn_ro(void *t); 37 | 38 | #if defined(LUA_DEBUG) 39 | LUAI_FUNC Node *luaH_mainposition(const Table *t, const TValue *key); 40 | LUAI_FUNC int luaH_isdummy(Node *n); 41 | #endif 42 | 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /lua-5.1.4/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltm_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | #include "ltable.h" 19 | #include "ltm.h" 20 | #include "lrotable.h" 21 | 22 | 23 | 24 | const char *const luaT_typenames[] = 25 | { 26 | "nil", "boolean", "romtable", "lightfunction", "userdata", "number", 27 | "string", "table", "function", "userdata", "thread", 28 | "proto", "upval" 29 | }; 30 | 31 | 32 | void luaT_init(lua_State *L) 33 | { 34 | static const char *const luaT_eventname[] = /* ORDER TM */ 35 | { 36 | "__index", "__newindex", 37 | "__gc", "__mode", "__eq", 38 | "__add", "__sub", "__mul", "__div", "__mod", 39 | "__pow", "__unm", "__len", "__lt", "__le", 40 | "__concat", "__call" 41 | }; 42 | int i; 43 | for (i = 0; i < TM_N; i++) 44 | { 45 | G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); 46 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 47 | } 48 | } 49 | 50 | 51 | /* 52 | ** function to be used with macro "fasttm": optimized for absence of 53 | ** tag methods 54 | */ 55 | const TValue *luaT_gettm(Table *events, TMS event, TString *ename) 56 | { 57 | const TValue *tm = luaR_isrotable(events) ? luaH_getstr_ro(events, ename) : luaH_getstr(events, ename); 58 | lua_assert(event <= TM_EQ); 59 | if (ttisnil(tm)) /* no tag method? */ 60 | { 61 | if (!luaR_isrotable(events)) 62 | events->flags |= cast_byte(1u << event); /* cache this fact */ 63 | return NULL; 64 | } 65 | else return tm; 66 | } 67 | 68 | 69 | const TValue *luaT_gettmbyobj(lua_State *L, const TValue *o, TMS event) 70 | { 71 | Table *mt; 72 | switch (ttype(o)) 73 | { 74 | case LUA_TTABLE: 75 | mt = hvalue(o)->metatable; 76 | break; 77 | case LUA_TROTABLE: 78 | mt = (Table *)luaR_getmeta(rvalue(o)); 79 | break; 80 | case LUA_TUSERDATA: 81 | mt = uvalue(o)->metatable; 82 | break; 83 | default: 84 | mt = G(L)->mt[ttype(o)]; 85 | } 86 | if (!mt) 87 | return luaO_nilobject; 88 | else if (luaR_isrotable(mt)) 89 | return luaH_getstr_ro(mt, G(L)->tmname[event]); 90 | else 91 | return luaH_getstr(mt, G(L)->tmname[event]); 92 | } 93 | -------------------------------------------------------------------------------- /lua-5.1.4/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 18 | typedef enum 19 | { 20 | TM_INDEX, 21 | TM_NEWINDEX, 22 | TM_GC, 23 | TM_MODE, 24 | TM_EQ, /* last tag method with `fast' access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_DIV, 29 | TM_MOD, 30 | TM_POW, 31 | TM_UNM, 32 | TM_LEN, 33 | TM_LT, 34 | TM_LE, 35 | TM_CONCAT, 36 | TM_CALL, 37 | TM_N /* number of elements in the enum */ 38 | } TMS; 39 | 40 | 41 | 42 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 43 | !luaR_isrotable(et) && ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 44 | 45 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 46 | 47 | LUAI_DATA const char *const luaT_typenames[]; 48 | 49 | 50 | LUAI_FUNC const TValue *luaT_gettm(Table *events, TMS event, TString *ename); 51 | LUAI_FUNC const TValue *luaT_gettmbyobj(lua_State *L, const TValue *o, 52 | TMS event); 53 | LUAI_FUNC void luaT_init(lua_State *L); 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /lua-5.1.4/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 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 | /* Key to file-handle type */ 14 | #define LUA_FILEHANDLE "FILE*" 15 | 16 | #define LUA_COLIBNAME "coroutine" 17 | LUALIB_API int (luaopen_base)(lua_State *L); 18 | 19 | #define LUA_TABLIBNAME "table" 20 | LUALIB_API int (luaopen_table)(lua_State *L); 21 | 22 | #define LUA_IOLIBNAME "io" 23 | LUALIB_API int (luaopen_io)(lua_State *L); 24 | 25 | #define LUA_OSLIBNAME "os" 26 | LUALIB_API int (luaopen_os)(lua_State *L); 27 | 28 | #define LUA_STRLIBNAME "string" 29 | LUALIB_API int (luaopen_string)(lua_State *L); 30 | 31 | #define LUA_MATHLIBNAME "math" 32 | LUALIB_API int (luaopen_math)(lua_State *L); 33 | 34 | #define LUA_DBLIBNAME "debug" 35 | LUALIB_API int (luaopen_debug)(lua_State *L); 36 | 37 | #define LUA_LOADLIBNAME "package" 38 | LUALIB_API int (luaopen_package)(lua_State *L); 39 | 40 | /* open all previous libraries */ 41 | LUALIB_API void (luaL_openlibs)(lua_State *L); 42 | 43 | #ifndef lua_assert 44 | #define lua_assert(x) ((void)0) 45 | #endif 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /lua-5.1.4/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include 11 | 12 | #include "lobject.h" 13 | #include "lzio.h" 14 | 15 | typedef uint32_t strsize_t; 16 | 17 | /* info about target machine for cross-compilation */ 18 | typedef struct 19 | { 20 | int little_endian; 21 | int sizeof_int; 22 | int sizeof_strsize_t; 23 | int sizeof_lua_Number; 24 | int lua_Number_integral; 25 | int is_arm_fpa; 26 | } DumpTargetInfo; 27 | 28 | /* load one chunk; from lundump.c */ 29 | LUAI_FUNC Proto *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, const char *name); 30 | 31 | /* make header; from lundump.c */ 32 | LUAI_FUNC void luaU_header(char *h); 33 | 34 | /* dump one chunk to a different target; from ldump.c */ 35 | int luaU_dump_crosscompile(lua_State *L, const Proto *f, lua_Writer w, void *data, int strip, DumpTargetInfo target); 36 | 37 | /* dump one chunk; from ldump.c */ 38 | LUAI_FUNC int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, int strip); 39 | 40 | #ifdef luac_c 41 | /* print one chunk; from print.c */ 42 | LUAI_FUNC void luaU_print(const Proto *f, int full); 43 | #endif 44 | 45 | /* for header of binary files -- this is Lua 5.1 */ 46 | #define LUAC_VERSION 0x51 47 | 48 | /* for header of binary files -- this is the official format */ 49 | #define LUAC_FORMAT 0 50 | 51 | /* size of header of binary files */ 52 | #define LUAC_HEADERSIZE 12 53 | 54 | /* error codes from cross-compiler */ 55 | /* target integer is too small to hold a value */ 56 | #define LUA_ERR_CC_INTOVERFLOW 101 57 | 58 | /* target lua_Number is integral but a constant is non-integer */ 59 | #define LUA_ERR_CC_NOTINTEGER 102 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /lua-5.1.4/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \ 19 | (((o) = luaV_tonumber(o,n)) != NULL)) 20 | 21 | #define equalobj(L,o1,o2) \ 22 | (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) 23 | 24 | 25 | LUAI_FUNC int luaV_lessthan(lua_State *L, const TValue *l, const TValue *r); 26 | LUAI_FUNC int luaV_equalval(lua_State *L, const TValue *t1, const TValue *t2); 27 | LUAI_FUNC const TValue *luaV_tonumber(const TValue *obj, TValue *n); 28 | LUAI_FUNC int luaV_tostring(lua_State *L, StkId obj); 29 | LUAI_FUNC void luaV_gettable(lua_State *L, const TValue *t, TValue *key, 30 | StkId val); 31 | LUAI_FUNC void luaV_settable(lua_State *L, const TValue *t, TValue *key, 32 | StkId val); 33 | LUAI_FUNC void luaV_execute(lua_State *L, int nexeccalls); 34 | LUAI_FUNC void luaV_concat(lua_State *L, int total, int last); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /lua-5.1.4/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** a generic input stream interface 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lzio_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "llimits.h" 16 | #include "lmem.h" 17 | #include "lstate.h" 18 | #include "lzio.h" 19 | 20 | 21 | int luaZ_fill(ZIO *z) 22 | { 23 | size_t size; 24 | lua_State *L = z->L; 25 | const char *buff; 26 | lua_unlock(L); 27 | buff = z->reader(L, z->data, &size); 28 | lua_lock(L); 29 | if (buff == NULL || size == 0) return EOZ; 30 | z->n = size - 1; 31 | z->p = buff; 32 | return char2int(*(z->p++)); 33 | } 34 | 35 | 36 | int luaZ_lookahead(ZIO *z) 37 | { 38 | if (z->n == 0) 39 | { 40 | if (luaZ_fill(z) == EOZ) 41 | return EOZ; 42 | else 43 | { 44 | z->n++; /* luaZ_fill removed first byte; put back it */ 45 | z->p--; 46 | } 47 | } 48 | return char2int(*z->p); 49 | } 50 | 51 | 52 | void luaZ_init(lua_State *L, ZIO *z, lua_Reader reader, void *data) 53 | { 54 | z->L = L; 55 | z->reader = reader; 56 | z->data = data; 57 | z->n = z->i = 0; 58 | z->p = NULL; 59 | } 60 | 61 | 62 | /* --------------------------------------------------------------- read --- */ 63 | size_t luaZ_read(ZIO *z, void *b, size_t n) 64 | { 65 | while (n) 66 | { 67 | size_t m; 68 | if (luaZ_lookahead(z) == EOZ) 69 | return n; /* return number of missing bytes */ 70 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 71 | if (b) 72 | memcpy(b, z->p, m); 73 | z->n -= m; 74 | z->i += m; 75 | z->p += m; 76 | if (b) 77 | b = (char *)b + m; 78 | n -= m; 79 | } 80 | return 0; 81 | } 82 | 83 | /* ------------------------------------------------------------------------ */ 84 | char *luaZ_openspace(lua_State *L, Mbuffer *buff, size_t n) 85 | { 86 | if (n > buff->buffsize) 87 | { 88 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 89 | luaZ_resizebuffer(L, buff, n); 90 | } 91 | return buff->buffer; 92 | } 93 | 94 | 95 | -------------------------------------------------------------------------------- /lua-5.1.4/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define char2int(c) cast(int, cast(unsigned char, (c))) 21 | 22 | #define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) 23 | 24 | typedef struct Mbuffer 25 | { 26 | char *buffer; 27 | size_t n; 28 | size_t buffsize; 29 | } Mbuffer; 30 | 31 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->n = 0, (buff)->buffsize = 0) 32 | 33 | #define luaZ_buffer(buff) ((buff)->buffer) 34 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 35 | #define luaZ_bufflen(buff) ((buff)->n) 36 | 37 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 38 | 39 | 40 | #define luaZ_resizebuffer(L, buff, size) \ 41 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 47 | #define luaZ_get_base_address(zio) ((const char *)((zio)->reader(NULL, (zio)->data, NULL))) 48 | #define luaZ_direct_mode(zio) (luaZ_get_base_address(zio) != NULL) 49 | #define luaZ_get_crt_address(zio) (luaZ_get_base_address(zio) + (zio)->i) 50 | 51 | LUAI_FUNC char *luaZ_openspace(lua_State *L, Mbuffer *buff, size_t n); 52 | LUAI_FUNC void luaZ_init(lua_State *L, ZIO *z, lua_Reader reader, 53 | void *data); 54 | LUAI_FUNC size_t luaZ_read(ZIO *z, void *b, size_t n); /* read next n bytes */ 55 | LUAI_FUNC int luaZ_lookahead(ZIO *z); 56 | 57 | 58 | 59 | /* --------- Private Part ------------------ */ 60 | 61 | struct Zio 62 | { 63 | size_t n; /* bytes still unread */ 64 | size_t i; /* buffer offset */ 65 | const char *p; /* current position in buffer */ 66 | lua_Reader reader; 67 | void *data; /* additional data */ 68 | lua_State *L; /* Lua state (for reader) */ 69 | }; 70 | 71 | 72 | LUAI_FUNC int luaZ_fill(ZIO *z); 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /lua-5.3.4/SConscript: -------------------------------------------------------------------------------- 1 | Import('rtconfig') 2 | import os 3 | from building import * 4 | 5 | cwd = GetCurrentDir() 6 | src = Glob('*.c') 7 | CPPPATH = [cwd] 8 | 9 | SrcRemove(src, 'luac.c') 10 | 11 | group = DefineGroup('lua-5.3.4', src, depend = ['PKG_USING_LUA', 'LUA_USING_PORTING_V534'], CPPPATH = CPPPATH) 12 | 13 | Return('group') 14 | -------------------------------------------------------------------------------- /lua-5.3.4/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.9 2015/03/06 19:49:50 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /lua-5.3.4/lbitlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lbitlib.c,v 1.30 2015/11/11 19:08:09 roberto Exp $ 3 | ** Standard library for bitwise operations 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lbitlib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lua.h" 14 | 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | 18 | 19 | #if defined(LUA_COMPAT_BITLIB) /* { */ 20 | 21 | 22 | #define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) 23 | #define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i)) 24 | 25 | 26 | /* number of bits to consider in a number */ 27 | #if !defined(LUA_NBITS) 28 | #define LUA_NBITS 32 29 | #endif 30 | 31 | 32 | /* 33 | ** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must 34 | ** be made in two parts to avoid problems when LUA_NBITS is equal to the 35 | ** number of bits in a lua_Unsigned.) 36 | */ 37 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) 38 | 39 | 40 | /* macro to trim extra bits */ 41 | #define trim(x) ((x) & ALLONES) 42 | 43 | 44 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ 45 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) 46 | 47 | 48 | 49 | static lua_Unsigned andaux(lua_State *L) 50 | { 51 | int i, n = lua_gettop(L); 52 | lua_Unsigned r = ~(lua_Unsigned)0; 53 | for (i = 1; i <= n; i++) 54 | r &= checkunsigned(L, i); 55 | return trim(r); 56 | } 57 | 58 | 59 | static int b_and(lua_State *L) 60 | { 61 | lua_Unsigned r = andaux(L); 62 | pushunsigned(L, r); 63 | return 1; 64 | } 65 | 66 | 67 | static int b_test(lua_State *L) 68 | { 69 | lua_Unsigned r = andaux(L); 70 | lua_pushboolean(L, r != 0); 71 | return 1; 72 | } 73 | 74 | 75 | static int b_or(lua_State *L) 76 | { 77 | int i, n = lua_gettop(L); 78 | lua_Unsigned r = 0; 79 | for (i = 1; i <= n; i++) 80 | r |= checkunsigned(L, i); 81 | pushunsigned(L, trim(r)); 82 | return 1; 83 | } 84 | 85 | 86 | static int b_xor(lua_State *L) 87 | { 88 | int i, n = lua_gettop(L); 89 | lua_Unsigned r = 0; 90 | for (i = 1; i <= n; i++) 91 | r ^= checkunsigned(L, i); 92 | pushunsigned(L, trim(r)); 93 | return 1; 94 | } 95 | 96 | 97 | static int b_not(lua_State *L) 98 | { 99 | lua_Unsigned r = ~checkunsigned(L, 1); 100 | pushunsigned(L, trim(r)); 101 | return 1; 102 | } 103 | 104 | 105 | static int b_shift(lua_State *L, lua_Unsigned r, lua_Integer i) 106 | { 107 | if (i < 0) /* shift right? */ 108 | { 109 | i = -i; 110 | r = trim(r); 111 | if (i >= LUA_NBITS) r = 0; 112 | else r >>= i; 113 | } 114 | else /* shift left */ 115 | { 116 | if (i >= LUA_NBITS) r = 0; 117 | else r <<= i; 118 | r = trim(r); 119 | } 120 | pushunsigned(L, r); 121 | return 1; 122 | } 123 | 124 | 125 | static int b_lshift(lua_State *L) 126 | { 127 | return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2)); 128 | } 129 | 130 | 131 | static int b_rshift(lua_State *L) 132 | { 133 | return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2)); 134 | } 135 | 136 | 137 | static int b_arshift(lua_State *L) 138 | { 139 | lua_Unsigned r = checkunsigned(L, 1); 140 | lua_Integer i = luaL_checkinteger(L, 2); 141 | if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) 142 | return b_shift(L, r, -i); 143 | else /* arithmetic shift for 'negative' number */ 144 | { 145 | if (i >= LUA_NBITS) r = ALLONES; 146 | else 147 | r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ 148 | pushunsigned(L, r); 149 | return 1; 150 | } 151 | } 152 | 153 | 154 | static int b_rot(lua_State *L, lua_Integer d) 155 | { 156 | lua_Unsigned r = checkunsigned(L, 1); 157 | int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ 158 | r = trim(r); 159 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ 160 | r = (r << i) | (r >> (LUA_NBITS - i)); 161 | pushunsigned(L, trim(r)); 162 | return 1; 163 | } 164 | 165 | 166 | static int b_lrot(lua_State *L) 167 | { 168 | return b_rot(L, luaL_checkinteger(L, 2)); 169 | } 170 | 171 | 172 | static int b_rrot(lua_State *L) 173 | { 174 | return b_rot(L, -luaL_checkinteger(L, 2)); 175 | } 176 | 177 | 178 | /* 179 | ** get field and width arguments for field-manipulation functions, 180 | ** checking whether they are valid. 181 | ** ('luaL_error' called without 'return' to avoid later warnings about 182 | ** 'width' being used uninitialized.) 183 | */ 184 | static int fieldargs(lua_State *L, int farg, int *width) 185 | { 186 | lua_Integer f = luaL_checkinteger(L, farg); 187 | lua_Integer w = luaL_optinteger(L, farg + 1, 1); 188 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); 189 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); 190 | if (f + w > LUA_NBITS) 191 | luaL_error(L, "trying to access non-existent bits"); 192 | *width = (int)w; 193 | return (int)f; 194 | } 195 | 196 | 197 | static int b_extract(lua_State *L) 198 | { 199 | int w; 200 | lua_Unsigned r = trim(checkunsigned(L, 1)); 201 | int f = fieldargs(L, 2, &w); 202 | r = (r >> f) & mask(w); 203 | pushunsigned(L, r); 204 | return 1; 205 | } 206 | 207 | 208 | static int b_replace(lua_State *L) 209 | { 210 | int w; 211 | lua_Unsigned r = trim(checkunsigned(L, 1)); 212 | lua_Unsigned v = trim(checkunsigned(L, 2)); 213 | int f = fieldargs(L, 3, &w); 214 | lua_Unsigned m = mask(w); 215 | r = (r & ~(m << f)) | ((v & m) << f); 216 | pushunsigned(L, r); 217 | return 1; 218 | } 219 | 220 | 221 | static const luaL_Reg bitlib[] = 222 | { 223 | {"arshift", b_arshift}, 224 | {"band", b_and}, 225 | {"bnot", b_not}, 226 | {"bor", b_or}, 227 | {"bxor", b_xor}, 228 | {"btest", b_test}, 229 | {"extract", b_extract}, 230 | {"lrotate", b_lrot}, 231 | {"lshift", b_lshift}, 232 | {"replace", b_replace}, 233 | {"rrotate", b_rrot}, 234 | {"rshift", b_rshift}, 235 | {NULL, NULL} 236 | }; 237 | 238 | 239 | 240 | LUAMOD_API int luaopen_bit32(lua_State *L) 241 | { 242 | luaL_newlib(L, bitlib); 243 | return 1; 244 | } 245 | 246 | 247 | #else /* }{ */ 248 | 249 | 250 | LUAMOD_API int luaopen_bit32(lua_State *L) 251 | { 252 | return luaL_error(L, "library 'bit32' has been deprecated"); 253 | } 254 | 255 | #endif /* } */ 256 | -------------------------------------------------------------------------------- /lua-5.3.4/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.64 2016/01/05 16:22:37 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr 27 | { 28 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, 29 | OPR_DIV, 30 | OPR_IDIV, 31 | OPR_BAND, OPR_BOR, OPR_BXOR, 32 | OPR_SHL, OPR_SHR, 33 | OPR_CONCAT, 34 | OPR_EQ, OPR_LT, OPR_LE, 35 | OPR_NE, OPR_GT, OPR_GE, 36 | OPR_AND, OPR_OR, 37 | OPR_NOBINOPR 38 | } BinOpr; 39 | 40 | 41 | typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 42 | 43 | 44 | /* get (pointer to) instruction of given 'expdesc' */ 45 | #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) 46 | 47 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 48 | 49 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 50 | 51 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 52 | 53 | LUAI_FUNC int luaK_codeABx(FuncState *fs, OpCode o, int A, unsigned int Bx); 54 | LUAI_FUNC int luaK_codeABC(FuncState *fs, OpCode o, int A, int B, int C); 55 | LUAI_FUNC int luaK_codek(FuncState *fs, int reg, int k); 56 | LUAI_FUNC void luaK_fixline(FuncState *fs, int line); 57 | LUAI_FUNC void luaK_nil(FuncState *fs, int from, int n); 58 | LUAI_FUNC void luaK_reserveregs(FuncState *fs, int n); 59 | LUAI_FUNC void luaK_checkstack(FuncState *fs, int n); 60 | LUAI_FUNC int luaK_stringK(FuncState *fs, TString *s); 61 | LUAI_FUNC int luaK_intK(FuncState *fs, lua_Integer n); 62 | LUAI_FUNC void luaK_dischargevars(FuncState *fs, expdesc *e); 63 | LUAI_FUNC int luaK_exp2anyreg(FuncState *fs, expdesc *e); 64 | LUAI_FUNC void luaK_exp2anyregup(FuncState *fs, expdesc *e); 65 | LUAI_FUNC void luaK_exp2nextreg(FuncState *fs, expdesc *e); 66 | LUAI_FUNC void luaK_exp2val(FuncState *fs, expdesc *e); 67 | LUAI_FUNC int luaK_exp2RK(FuncState *fs, expdesc *e); 68 | LUAI_FUNC void luaK_self(FuncState *fs, expdesc *e, expdesc *key); 69 | LUAI_FUNC void luaK_indexed(FuncState *fs, expdesc *t, expdesc *k); 70 | LUAI_FUNC void luaK_goiftrue(FuncState *fs, expdesc *e); 71 | LUAI_FUNC void luaK_goiffalse(FuncState *fs, expdesc *e); 72 | LUAI_FUNC void luaK_storevar(FuncState *fs, expdesc *var, expdesc *e); 73 | LUAI_FUNC void luaK_setreturns(FuncState *fs, expdesc *e, int nresults); 74 | LUAI_FUNC void luaK_setoneret(FuncState *fs, expdesc *e); 75 | LUAI_FUNC int luaK_jump(FuncState *fs); 76 | LUAI_FUNC void luaK_ret(FuncState *fs, int first, int nret); 77 | LUAI_FUNC void luaK_patchlist(FuncState *fs, int list, int target); 78 | LUAI_FUNC void luaK_patchtohere(FuncState *fs, int list); 79 | LUAI_FUNC void luaK_patchclose(FuncState *fs, int list, int level); 80 | LUAI_FUNC void luaK_concat(FuncState *fs, int *l1, int l2); 81 | LUAI_FUNC int luaK_getlabel(FuncState *fs); 82 | LUAI_FUNC void luaK_prefix(FuncState *fs, UnOpr op, expdesc *v, int line); 83 | LUAI_FUNC void luaK_infix(FuncState *fs, BinOpr op, expdesc *v); 84 | LUAI_FUNC void luaK_posfix(FuncState *fs, BinOpr op, expdesc *v1, 85 | expdesc *v2, int line); 86 | LUAI_FUNC void luaK_setlist(FuncState *fs, int base, int nelems, int tostore); 87 | 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /lua-5.3.4/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.10 2016/04/11 19:19:55 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lcorolib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lauxlib.h" 18 | #include "lualib.h" 19 | 20 | 21 | static lua_State *getco(lua_State *L) 22 | { 23 | lua_State *co = lua_tothread(L, 1); 24 | luaL_argcheck(L, co, 1, "thread expected"); 25 | return co; 26 | } 27 | 28 | 29 | static int auxresume(lua_State *L, lua_State *co, int narg) 30 | { 31 | int status; 32 | if (!lua_checkstack(co, narg)) 33 | { 34 | lua_pushliteral(L, "too many arguments to resume"); 35 | return -1; /* error flag */ 36 | } 37 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) 38 | { 39 | lua_pushliteral(L, "cannot resume dead coroutine"); 40 | return -1; /* error flag */ 41 | } 42 | lua_xmove(L, co, narg); 43 | status = lua_resume(co, L, narg); 44 | if (status == LUA_OK || status == LUA_YIELD) 45 | { 46 | int nres = lua_gettop(co); 47 | if (!lua_checkstack(L, nres + 1)) 48 | { 49 | lua_pop(co, nres); /* remove results anyway */ 50 | lua_pushliteral(L, "too many results to resume"); 51 | return -1; /* error flag */ 52 | } 53 | lua_xmove(co, L, nres); /* move yielded values */ 54 | return nres; 55 | } 56 | else 57 | { 58 | lua_xmove(co, L, 1); /* move error message */ 59 | return -1; /* error flag */ 60 | } 61 | } 62 | 63 | 64 | static int luaB_coresume(lua_State *L) 65 | { 66 | lua_State *co = getco(L); 67 | int r; 68 | r = auxresume(L, co, lua_gettop(L) - 1); 69 | if (r < 0) 70 | { 71 | lua_pushboolean(L, 0); 72 | lua_insert(L, -2); 73 | return 2; /* return false + error message */ 74 | } 75 | else 76 | { 77 | lua_pushboolean(L, 1); 78 | lua_insert(L, -(r + 1)); 79 | return r + 1; /* return true + 'resume' returns */ 80 | } 81 | } 82 | 83 | 84 | static int luaB_auxwrap(lua_State *L) 85 | { 86 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 87 | int r = auxresume(L, co, lua_gettop(L)); 88 | if (r < 0) 89 | { 90 | if (lua_type(L, -1) == LUA_TSTRING) /* error object is a string? */ 91 | { 92 | luaL_where(L, 1); /* add extra info */ 93 | lua_insert(L, -2); 94 | lua_concat(L, 2); 95 | } 96 | return lua_error(L); /* propagate error */ 97 | } 98 | return r; 99 | } 100 | 101 | 102 | static int luaB_cocreate(lua_State *L) 103 | { 104 | lua_State *NL; 105 | luaL_checktype(L, 1, LUA_TFUNCTION); 106 | NL = lua_newthread(L); 107 | lua_pushvalue(L, 1); /* move function to top */ 108 | lua_xmove(L, NL, 1); /* move function from L to NL */ 109 | return 1; 110 | } 111 | 112 | 113 | static int luaB_cowrap(lua_State *L) 114 | { 115 | luaB_cocreate(L); 116 | lua_pushcclosure(L, luaB_auxwrap, 1); 117 | return 1; 118 | } 119 | 120 | 121 | static int luaB_yield(lua_State *L) 122 | { 123 | return lua_yield(L, lua_gettop(L)); 124 | } 125 | 126 | 127 | static int luaB_costatus(lua_State *L) 128 | { 129 | lua_State *co = getco(L); 130 | if (L == co) lua_pushliteral(L, "running"); 131 | else 132 | { 133 | switch (lua_status(co)) 134 | { 135 | case LUA_YIELD: 136 | lua_pushliteral(L, "suspended"); 137 | break; 138 | case LUA_OK: 139 | { 140 | lua_Debug ar; 141 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 142 | lua_pushliteral(L, "normal"); /* it is running */ 143 | else if (lua_gettop(co) == 0) 144 | lua_pushliteral(L, "dead"); 145 | else 146 | lua_pushliteral(L, "suspended"); /* initial state */ 147 | break; 148 | } 149 | default: /* some error occurred */ 150 | lua_pushliteral(L, "dead"); 151 | break; 152 | } 153 | } 154 | return 1; 155 | } 156 | 157 | 158 | static int luaB_yieldable(lua_State *L) 159 | { 160 | lua_pushboolean(L, lua_isyieldable(L)); 161 | return 1; 162 | } 163 | 164 | 165 | static int luaB_corunning(lua_State *L) 166 | { 167 | int ismain = lua_pushthread(L); 168 | lua_pushboolean(L, ismain); 169 | return 2; 170 | } 171 | 172 | 173 | static const luaL_Reg co_funcs[] = 174 | { 175 | {"create", luaB_cocreate}, 176 | {"resume", luaB_coresume}, 177 | {"running", luaB_corunning}, 178 | {"status", luaB_costatus}, 179 | {"wrap", luaB_cowrap}, 180 | {"yield", luaB_yield}, 181 | {"isyieldable", luaB_yieldable}, 182 | {NULL, NULL} 183 | }; 184 | 185 | 186 | 187 | LUAMOD_API int luaopen_coroutine(lua_State *L) 188 | { 189 | luaL_newlib(L, co_funcs); 190 | return 1; 191 | } 192 | 193 | -------------------------------------------------------------------------------- /lua-5.3.4/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.12 2014/11/02 19:19:04 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lctype.h" 14 | 15 | #if !LUA_USE_CTYPE /* { */ 16 | 17 | #include 18 | 19 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = 20 | { 21 | 0x00, /* EOZ */ 22 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 23 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 25 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 26 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 27 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 28 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 29 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 30 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 32 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 33 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 34 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 35 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 36 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 37 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 53 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 54 | }; 55 | 56 | #endif /* } */ 57 | -------------------------------------------------------------------------------- /lua-5.3.4/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /lua-5.3.4/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.14 2015/05/22 17:45:56 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | LUAI_FUNC l_noret luaG_typeerror(lua_State *L, const TValue *o, 22 | const char *opname); 23 | LUAI_FUNC l_noret luaG_concaterror(lua_State *L, const TValue *p1, 24 | const TValue *p2); 25 | LUAI_FUNC l_noret luaG_opinterror(lua_State *L, const TValue *p1, 26 | const TValue *p2, 27 | const char *msg); 28 | LUAI_FUNC l_noret luaG_tointerror(lua_State *L, const TValue *p1, 29 | const TValue *p2); 30 | LUAI_FUNC l_noret luaG_ordererror(lua_State *L, const TValue *p1, 31 | const TValue *p2); 32 | LUAI_FUNC l_noret luaG_runerror(lua_State *L, const char *fmt, ...); 33 | LUAI_FUNC const char *luaG_addinfo(lua_State *L, const char *msg, 34 | TString *src, int line); 35 | LUAI_FUNC l_noret luaG_errormsg(lua_State *L); 36 | LUAI_FUNC void luaG_traceexec(lua_State *L); 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /lua-5.3.4/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.29 2015/12/21 13:02:14 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | /* 17 | ** Macro to check stack size and grow stack if needed. Parameters 18 | ** 'pre'/'pos' allow the macro to preserve a pointer into the 19 | ** stack across reallocations, doing the work only when needed. 20 | ** 'condmovestack' is used in heavy tests to force a stack reallocation 21 | ** at every check. 22 | */ 23 | #define luaD_checkstackaux(L,n,pre,pos) \ 24 | if (L->stack_last - L->top <= (n)) \ 25 | { pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); } 26 | 27 | /* In general, 'pre'/'pos' are empty (nothing to save) */ 28 | #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) 29 | 30 | 31 | 32 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 33 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 34 | 35 | 36 | /* type of protected functions, to be ran by 'runprotected' */ 37 | typedef void (*Pfunc)(lua_State *L, void *ud); 38 | 39 | LUAI_FUNC int luaD_protectedparser(lua_State *L, ZIO *z, const char *name, 40 | const char *mode); 41 | LUAI_FUNC void luaD_hook(lua_State *L, int event, int line); 42 | LUAI_FUNC int luaD_precall(lua_State *L, StkId func, int nresults); 43 | LUAI_FUNC void luaD_call(lua_State *L, StkId func, int nResults); 44 | LUAI_FUNC void luaD_callnoyield(lua_State *L, StkId func, int nResults); 45 | LUAI_FUNC int luaD_pcall(lua_State *L, Pfunc func, void *u, 46 | ptrdiff_t oldtop, ptrdiff_t ef); 47 | LUAI_FUNC int luaD_poscall(lua_State *L, CallInfo *ci, StkId firstResult, 48 | int nres); 49 | LUAI_FUNC void luaD_reallocstack(lua_State *L, int newsize); 50 | LUAI_FUNC void luaD_growstack(lua_State *L, int n); 51 | LUAI_FUNC void luaD_shrinkstack(lua_State *L); 52 | LUAI_FUNC void luaD_inctop(lua_State *L); 53 | 54 | LUAI_FUNC l_noret luaD_throw(lua_State *L, int errcode); 55 | LUAI_FUNC int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud); 56 | 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /lua-5.3.4/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.37 2015/10/08 15:53:49 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ldump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lobject.h" 18 | #include "lstate.h" 19 | #include "lundump.h" 20 | 21 | 22 | typedef struct 23 | { 24 | lua_State *L; 25 | lua_Writer writer; 26 | void *data; 27 | int strip; 28 | int status; 29 | } DumpState; 30 | 31 | 32 | /* 33 | ** All high-level dumps go through DumpVector; you can change it to 34 | ** change the endianness of the result 35 | */ 36 | #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) 37 | 38 | #define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D) 39 | 40 | 41 | static void DumpBlock(const void *b, size_t size, DumpState *D) 42 | { 43 | if (D->status == 0 && size > 0) 44 | { 45 | lua_unlock(D->L); 46 | D->status = (*D->writer)(D->L, b, size, D->data); 47 | lua_lock(D->L); 48 | } 49 | } 50 | 51 | 52 | #define DumpVar(x,D) DumpVector(&x,1,D) 53 | 54 | 55 | static void DumpByte(int y, DumpState *D) 56 | { 57 | lu_byte x = (lu_byte)y; 58 | DumpVar(x, D); 59 | } 60 | 61 | 62 | static void DumpInt(int x, DumpState *D) 63 | { 64 | DumpVar(x, D); 65 | } 66 | 67 | 68 | static void DumpNumber(lua_Number x, DumpState *D) 69 | { 70 | DumpVar(x, D); 71 | } 72 | 73 | 74 | static void DumpInteger(lua_Integer x, DumpState *D) 75 | { 76 | DumpVar(x, D); 77 | } 78 | 79 | 80 | static void DumpString(const TString *s, DumpState *D) 81 | { 82 | if (s == NULL) 83 | DumpByte(0, D); 84 | else 85 | { 86 | size_t size = tsslen(s) + 1; /* include trailing '\0' */ 87 | const char *str = getstr(s); 88 | if (size < 0xFF) 89 | DumpByte(cast_int(size), D); 90 | else 91 | { 92 | DumpByte(0xFF, D); 93 | DumpVar(size, D); 94 | } 95 | DumpVector(str, size - 1, D); /* no need to save '\0' */ 96 | } 97 | } 98 | 99 | 100 | static void DumpCode(const Proto *f, DumpState *D) 101 | { 102 | DumpInt(f->sizecode, D); 103 | DumpVector(f->code, f->sizecode, D); 104 | } 105 | 106 | 107 | static void DumpFunction(const Proto *f, TString *psource, DumpState *D); 108 | 109 | static void DumpConstants(const Proto *f, DumpState *D) 110 | { 111 | int i; 112 | int n = f->sizek; 113 | DumpInt(n, D); 114 | for (i = 0; i < n; i++) 115 | { 116 | const TValue *o = &f->k[i]; 117 | DumpByte(ttype(o), D); 118 | switch (ttype(o)) 119 | { 120 | case LUA_TNIL: 121 | break; 122 | case LUA_TBOOLEAN: 123 | DumpByte(bvalue(o), D); 124 | break; 125 | case LUA_TNUMFLT: 126 | DumpNumber(fltvalue(o), D); 127 | break; 128 | case LUA_TNUMINT: 129 | DumpInteger(ivalue(o), D); 130 | break; 131 | case LUA_TSHRSTR: 132 | case LUA_TLNGSTR: 133 | DumpString(tsvalue(o), D); 134 | break; 135 | default: 136 | lua_assert(0); 137 | } 138 | } 139 | } 140 | 141 | 142 | static void DumpProtos(const Proto *f, DumpState *D) 143 | { 144 | int i; 145 | int n = f->sizep; 146 | DumpInt(n, D); 147 | for (i = 0; i < n; i++) 148 | DumpFunction(f->p[i], f->source, D); 149 | } 150 | 151 | 152 | static void DumpUpvalues(const Proto *f, DumpState *D) 153 | { 154 | int i, n = f->sizeupvalues; 155 | DumpInt(n, D); 156 | for (i = 0; i < n; i++) 157 | { 158 | DumpByte(f->upvalues[i].instack, D); 159 | DumpByte(f->upvalues[i].idx, D); 160 | } 161 | } 162 | 163 | 164 | static void DumpDebug(const Proto *f, DumpState *D) 165 | { 166 | int i, n; 167 | n = (D->strip) ? 0 : f->sizelineinfo; 168 | DumpInt(n, D); 169 | DumpVector(f->lineinfo, n, D); 170 | n = (D->strip) ? 0 : f->sizelocvars; 171 | DumpInt(n, D); 172 | for (i = 0; i < n; i++) 173 | { 174 | DumpString(f->locvars[i].varname, D); 175 | DumpInt(f->locvars[i].startpc, D); 176 | DumpInt(f->locvars[i].endpc, D); 177 | } 178 | n = (D->strip) ? 0 : f->sizeupvalues; 179 | DumpInt(n, D); 180 | for (i = 0; i < n; i++) 181 | DumpString(f->upvalues[i].name, D); 182 | } 183 | 184 | 185 | static void DumpFunction(const Proto *f, TString *psource, DumpState *D) 186 | { 187 | if (D->strip || f->source == psource) 188 | DumpString(NULL, D); /* no debug info or same source as its parent */ 189 | else 190 | DumpString(f->source, D); 191 | DumpInt(f->linedefined, D); 192 | DumpInt(f->lastlinedefined, D); 193 | DumpByte(f->numparams, D); 194 | DumpByte(f->is_vararg, D); 195 | DumpByte(f->maxstacksize, D); 196 | DumpCode(f, D); 197 | DumpConstants(f, D); 198 | DumpUpvalues(f, D); 199 | DumpProtos(f, D); 200 | DumpDebug(f, D); 201 | } 202 | 203 | 204 | static void DumpHeader(DumpState *D) 205 | { 206 | DumpLiteral(LUA_SIGNATURE, D); 207 | DumpByte(LUAC_VERSION, D); 208 | DumpByte(LUAC_FORMAT, D); 209 | DumpLiteral(LUAC_DATA, D); 210 | DumpByte(sizeof(int), D); 211 | DumpByte(sizeof(size_t), D); 212 | DumpByte(sizeof(Instruction), D); 213 | DumpByte(sizeof(lua_Integer), D); 214 | DumpByte(sizeof(lua_Number), D); 215 | DumpInteger(LUAC_INT, D); 216 | DumpNumber(LUAC_NUM, D); 217 | } 218 | 219 | 220 | /* 221 | ** dump Lua function as precompiled chunk 222 | */ 223 | int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, 224 | int strip) 225 | { 226 | DumpState D; 227 | D.L = L; 228 | D.writer = w; 229 | D.data = data; 230 | D.strip = strip; 231 | D.status = 0; 232 | DumpHeader(&D); 233 | DumpByte(f->sizeupvalues, &D); 234 | DumpFunction(f, NULL, &D); 235 | return D.status; 236 | } 237 | 238 | -------------------------------------------------------------------------------- /lua-5.3.4/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lfunc_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lfunc.h" 18 | #include "lgc.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | 23 | 24 | 25 | CClosure *luaF_newCclosure(lua_State *L, int n) 26 | { 27 | GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); 28 | CClosure *c = gco2ccl(o); 29 | c->nupvalues = cast_byte(n); 30 | return c; 31 | } 32 | 33 | 34 | LClosure *luaF_newLclosure(lua_State *L, int n) 35 | { 36 | GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); 37 | LClosure *c = gco2lcl(o); 38 | c->p = NULL; 39 | c->nupvalues = cast_byte(n); 40 | while (n--) c->upvals[n] = NULL; 41 | return c; 42 | } 43 | 44 | /* 45 | ** fill a closure with new closed upvalues 46 | */ 47 | void luaF_initupvals(lua_State *L, LClosure *cl) 48 | { 49 | int i; 50 | for (i = 0; i < cl->nupvalues; i++) 51 | { 52 | UpVal *uv = luaM_new(L, UpVal); 53 | uv->refcount = 1; 54 | uv->v = &uv->u.value; /* make it closed */ 55 | setnilvalue(uv->v); 56 | cl->upvals[i] = uv; 57 | } 58 | } 59 | 60 | 61 | UpVal *luaF_findupval(lua_State *L, StkId level) 62 | { 63 | UpVal **pp = &L->openupval; 64 | UpVal *p; 65 | UpVal *uv; 66 | lua_assert(isintwups(L) || L->openupval == NULL); 67 | while (*pp != NULL && (p = *pp)->v >= level) 68 | { 69 | lua_assert(upisopen(p)); 70 | if (p->v == level) /* found a corresponding upvalue? */ 71 | return p; /* return it */ 72 | pp = &p->u.open.next; 73 | } 74 | /* not found: create a new upvalue */ 75 | uv = luaM_new(L, UpVal); 76 | uv->refcount = 0; 77 | uv->u.open.next = *pp; /* link it to list of open upvalues */ 78 | uv->u.open.touched = 1; 79 | *pp = uv; 80 | uv->v = level; /* current value lives in the stack */ 81 | if (!isintwups(L)) /* thread not in list of threads with upvalues? */ 82 | { 83 | L->twups = G(L)->twups; /* link it to the list */ 84 | G(L)->twups = L; 85 | } 86 | return uv; 87 | } 88 | 89 | 90 | void luaF_close(lua_State *L, StkId level) 91 | { 92 | UpVal *uv; 93 | while (L->openupval != NULL && (uv = L->openupval)->v >= level) 94 | { 95 | lua_assert(upisopen(uv)); 96 | L->openupval = uv->u.open.next; /* remove from 'open' list */ 97 | if (uv->refcount == 0) /* no references? */ 98 | luaM_free(L, uv); /* free upvalue */ 99 | else 100 | { 101 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ 102 | uv->v = &uv->u.value; /* now current value lives here */ 103 | luaC_upvalbarrier(L, uv); 104 | } 105 | } 106 | } 107 | 108 | 109 | Proto *luaF_newproto(lua_State *L) 110 | { 111 | GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); 112 | Proto *f = gco2p(o); 113 | f->k = NULL; 114 | f->sizek = 0; 115 | f->p = NULL; 116 | f->sizep = 0; 117 | f->code = NULL; 118 | f->cache = NULL; 119 | f->sizecode = 0; 120 | f->lineinfo = NULL; 121 | f->sizelineinfo = 0; 122 | f->upvalues = NULL; 123 | f->sizeupvalues = 0; 124 | f->numparams = 0; 125 | f->is_vararg = 0; 126 | f->maxstacksize = 0; 127 | f->locvars = NULL; 128 | f->sizelocvars = 0; 129 | f->linedefined = 0; 130 | f->lastlinedefined = 0; 131 | f->source = NULL; 132 | return f; 133 | } 134 | 135 | 136 | void luaF_freeproto(lua_State *L, Proto *f) 137 | { 138 | luaM_freearray(L, f->code, f->sizecode); 139 | luaM_freearray(L, f->p, f->sizep); 140 | luaM_freearray(L, f->k, f->sizek); 141 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 142 | luaM_freearray(L, f->locvars, f->sizelocvars); 143 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 144 | luaM_free(L, f); 145 | } 146 | 147 | 148 | /* 149 | ** Look for n-th local variable at line 'line' in function 'func'. 150 | ** Returns NULL if not found. 151 | */ 152 | const char *luaF_getlocalname(const Proto *f, int local_number, int pc) 153 | { 154 | int i; 155 | for (i = 0; i < f->sizelocvars && f->locvars[i].startpc <= pc; i++) 156 | { 157 | if (pc < f->locvars[i].endpc) /* is variable active? */ 158 | { 159 | local_number--; 160 | if (local_number == 0) 161 | return getstr(f->locvars[i].varname); 162 | } 163 | } 164 | return NULL; /* not found */ 165 | } 166 | 167 | -------------------------------------------------------------------------------- /lua-5.3.4/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.15 2015/01/13 15:49:11 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | /* test whether thread is in 'twups' list */ 22 | #define isintwups(L) (L->twups != L) 23 | 24 | 25 | /* 26 | ** maximum number of upvalues in a closure (both C and Lua). (Value 27 | ** must fit in a VM register.) 28 | */ 29 | #define MAXUPVAL 255 30 | 31 | 32 | /* 33 | ** Upvalues for Lua closures 34 | */ 35 | struct UpVal 36 | { 37 | TValue *v; /* points to stack or to its own value */ 38 | lu_mem refcount; /* reference counter */ 39 | union 40 | { 41 | struct /* (when open) */ 42 | { 43 | UpVal *next; /* linked list */ 44 | int touched; /* mark to avoid cycles with dead threads */ 45 | } open; 46 | TValue value; /* the value (when closed) */ 47 | } u; 48 | }; 49 | 50 | #define upisopen(up) ((up)->v != &(up)->u.value) 51 | 52 | 53 | LUAI_FUNC Proto *luaF_newproto(lua_State *L); 54 | LUAI_FUNC CClosure *luaF_newCclosure(lua_State *L, int nelems); 55 | LUAI_FUNC LClosure *luaF_newLclosure(lua_State *L, int nelems); 56 | LUAI_FUNC void luaF_initupvals(lua_State *L, LClosure *cl); 57 | LUAI_FUNC UpVal *luaF_findupval(lua_State *L, StkId level); 58 | LUAI_FUNC void luaF_close(lua_State *L, StkId level); 59 | LUAI_FUNC void luaF_freeproto(lua_State *L, Proto *f); 60 | LUAI_FUNC const char *luaF_getlocalname(const Proto *func, int local_number, 61 | int pc); 62 | 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /lua-5.3.4/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.91 2015/12/21 13:02:14 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | /* 15 | ** Collectable objects may have one of three colors: white, which 16 | ** means the object is not marked; gray, which means the 17 | ** object is marked, but its references may be not marked; and 18 | ** black, which means that the object and all its references are marked. 19 | ** The main invariant of the garbage collector, while marking objects, 20 | ** is that a black object can never point to a white one. Moreover, 21 | ** any gray object must be in a "gray list" (gray, grayagain, weak, 22 | ** allweak, ephemeron) so that it can be visited again before finishing 23 | ** the collection cycle. These lists have no meaning when the invariant 24 | ** is not being enforced (e.g., sweep phase). 25 | */ 26 | 27 | 28 | 29 | /* how much to allocate before next GC step */ 30 | #if !defined(GCSTEPSIZE) 31 | /* ~100 small strings */ 32 | #define GCSTEPSIZE (cast_int(100 * sizeof(TString))) 33 | #endif 34 | 35 | 36 | /* 37 | ** Possible states of the Garbage Collector 38 | */ 39 | #define GCSpropagate 0 40 | #define GCSatomic 1 41 | #define GCSswpallgc 2 42 | #define GCSswpfinobj 3 43 | #define GCSswptobefnz 4 44 | #define GCSswpend 5 45 | #define GCScallfin 6 46 | #define GCSpause 7 47 | 48 | 49 | #define issweepphase(g) \ 50 | (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) 51 | 52 | 53 | /* 54 | ** macro to tell when main invariant (white objects cannot point to black 55 | ** ones) must be kept. During a collection, the sweep 56 | ** phase may break the invariant, as objects turned white may point to 57 | ** still-black objects. The invariant is restored when sweep ends and 58 | ** all objects are white again. 59 | */ 60 | 61 | #define keepinvariant(g) ((g)->gcstate <= GCSatomic) 62 | 63 | 64 | /* 65 | ** some useful bit tricks 66 | */ 67 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 68 | #define setbits(x,m) ((x) |= (m)) 69 | #define testbits(x,m) ((x) & (m)) 70 | #define bitmask(b) (1<<(b)) 71 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 72 | #define l_setbit(x,b) setbits(x, bitmask(b)) 73 | #define resetbit(x,b) resetbits(x, bitmask(b)) 74 | #define testbit(x,b) testbits(x, bitmask(b)) 75 | 76 | 77 | /* Layout for bit use in 'marked' field: */ 78 | #define WHITE0BIT 0 /* object is white (type 0) */ 79 | #define WHITE1BIT 1 /* object is white (type 1) */ 80 | #define BLACKBIT 2 /* object is black */ 81 | #define FINALIZEDBIT 3 /* object has been marked for finalization */ 82 | /* bit 7 is currently used by tests (luaL_checkmemory) */ 83 | 84 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 85 | 86 | 87 | #define iswhite(x) testbits((x)->marked, WHITEBITS) 88 | #define isblack(x) testbit((x)->marked, BLACKBIT) 89 | #define isgray(x) /* neither white nor black */ \ 90 | (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) 91 | 92 | #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) 93 | 94 | #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) 95 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) 96 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) 97 | 98 | #define changewhite(x) ((x)->marked ^= WHITEBITS) 99 | #define gray2black(x) l_setbit((x)->marked, BLACKBIT) 100 | 101 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 102 | 103 | 104 | /* 105 | ** Does one step of collection when debt becomes positive. 'pre'/'pos' 106 | ** allows some adjustments to be done only when needed. macro 107 | ** 'condchangemem' is used only for heavy tests (forcing a full 108 | ** GC cycle on every opportunity) 109 | */ 110 | #define luaC_condGC(L,pre,pos) \ 111 | { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ 112 | condchangemem(L,pre,pos); } 113 | 114 | /* more often than not, 'pre'/'pos' are empty */ 115 | #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) 116 | 117 | 118 | #define luaC_barrier(L,p,v) ( \ 119 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 120 | luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) 121 | 122 | #define luaC_barrierback(L,p,v) ( \ 123 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 124 | luaC_barrierback_(L,p) : cast_void(0)) 125 | 126 | #define luaC_objbarrier(L,p,o) ( \ 127 | (isblack(p) && iswhite(o)) ? \ 128 | luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) 129 | 130 | #define luaC_upvalbarrier(L,uv) ( \ 131 | (iscollectable((uv)->v) && !upisopen(uv)) ? \ 132 | luaC_upvalbarrier_(L,uv) : cast_void(0)) 133 | 134 | LUAI_FUNC void luaC_fix(lua_State *L, GCObject *o); 135 | LUAI_FUNC void luaC_freeallobjects(lua_State *L); 136 | LUAI_FUNC void luaC_step(lua_State *L); 137 | LUAI_FUNC void luaC_runtilstate(lua_State *L, int statesmask); 138 | LUAI_FUNC void luaC_fullgc(lua_State *L, int isemergency); 139 | LUAI_FUNC GCObject *luaC_newobj(lua_State *L, int tt, size_t sz); 140 | LUAI_FUNC void luaC_barrier_(lua_State *L, GCObject *o, GCObject *v); 141 | LUAI_FUNC void luaC_barrierback_(lua_State *L, Table *o); 142 | LUAI_FUNC void luaC_upvalbarrier_(lua_State *L, UpVal *uv); 143 | LUAI_FUNC void luaC_checkfinalizer(lua_State *L, GCObject *o, Table *mt); 144 | LUAI_FUNC void luaC_upvdeccount(lua_State *L, UpVal *uv); 145 | 146 | 147 | #endif 148 | -------------------------------------------------------------------------------- /lua-5.3.4/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.39 2016/12/04 20:17:24 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | /* 12 | ** If you embed Lua in your program and need to open the standard 13 | ** libraries, call luaL_openlibs in your program. If you need a 14 | ** different set of libraries, copy this file to your project and edit 15 | ** it to suit your needs. 16 | ** 17 | ** You can also *preload* libraries, so that a later 'require' can 18 | ** open the library, which is already linked to the application. 19 | ** For that, do the following code: 20 | ** 21 | ** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); 22 | ** lua_pushcfunction(L, luaopen_modname); 23 | ** lua_setfield(L, -2, modname); 24 | ** lua_pop(L, 1); // remove PRELOAD table 25 | */ 26 | 27 | #include "lprefix.h" 28 | 29 | 30 | #include 31 | 32 | #include "lua.h" 33 | 34 | #include "lualib.h" 35 | #include "lauxlib.h" 36 | 37 | 38 | /* 39 | ** these libs are loaded by lua.c and are readily available to any Lua 40 | ** program 41 | */ 42 | static const luaL_Reg loadedlibs[] = 43 | { 44 | {"_G", luaopen_base}, 45 | {LUA_LOADLIBNAME, luaopen_package}, 46 | {LUA_COLIBNAME, luaopen_coroutine}, 47 | {LUA_TABLIBNAME, luaopen_table}, 48 | {LUA_IOLIBNAME, luaopen_io}, 49 | {LUA_OSLIBNAME, luaopen_os}, 50 | {LUA_STRLIBNAME, luaopen_string}, 51 | {LUA_MATHLIBNAME, luaopen_math}, 52 | {LUA_UTF8LIBNAME, luaopen_utf8}, 53 | {LUA_DBLIBNAME, luaopen_debug}, 54 | #if defined(LUA_COMPAT_BITLIB) 55 | {LUA_BITLIBNAME, luaopen_bit32}, 56 | #endif 57 | {NULL, NULL} 58 | }; 59 | 60 | 61 | LUALIB_API void luaL_openlibs(lua_State *L) 62 | { 63 | const luaL_Reg *lib; 64 | /* "require" functions from 'loadedlibs' and set results to global table */ 65 | for (lib = loadedlibs; lib->func; lib++) 66 | { 67 | luaL_requiref(L, lib->name, lib->func, 1); 68 | lua_pop(L, 1); /* remove lib */ 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /lua-5.3.4/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.79 2016/05/02 14:02:12 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | #if !defined(LUA_ENV) 18 | #define LUA_ENV "_ENV" 19 | #endif 20 | 21 | 22 | /* 23 | * WARNING: if you change the order of this enumeration, 24 | * grep "ORDER RESERVED" 25 | */ 26 | enum RESERVED 27 | { 28 | /* terminal symbols denoted by reserved words */ 29 | TK_AND = FIRST_RESERVED, TK_BREAK, 30 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 31 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 32 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 33 | /* other terminal symbols */ 34 | TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, 35 | TK_SHL, TK_SHR, 36 | TK_DBCOLON, TK_EOS, 37 | TK_FLT, TK_INT, TK_NAME, TK_STRING 38 | }; 39 | 40 | /* number of reserved words */ 41 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 42 | 43 | 44 | typedef union 45 | { 46 | lua_Number r; 47 | lua_Integer i; 48 | TString *ts; 49 | } SemInfo; /* semantics information */ 50 | 51 | 52 | typedef struct Token 53 | { 54 | int token; 55 | SemInfo seminfo; 56 | } Token; 57 | 58 | 59 | /* state of the lexer plus state of the parser when shared by all 60 | functions */ 61 | typedef struct LexState 62 | { 63 | int current; /* current character (charint) */ 64 | int linenumber; /* input line counter */ 65 | int lastline; /* line of last token 'consumed' */ 66 | Token t; /* current token */ 67 | Token lookahead; /* look ahead token */ 68 | struct FuncState *fs; /* current function (parser) */ 69 | struct lua_State *L; 70 | ZIO *z; /* input stream */ 71 | Mbuffer *buff; /* buffer for tokens */ 72 | Table *h; /* to avoid collection/reuse strings */ 73 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 74 | TString *source; /* current source name */ 75 | TString *envn; /* environment variable name */ 76 | } LexState; 77 | 78 | 79 | LUAI_FUNC void luaX_init(lua_State *L); 80 | LUAI_FUNC void luaX_setinput(lua_State *L, LexState *ls, ZIO *z, 81 | TString *source, int firstchar); 82 | LUAI_FUNC TString *luaX_newstring(LexState *ls, const char *str, size_t l); 83 | LUAI_FUNC void luaX_next(LexState *ls); 84 | LUAI_FUNC int luaX_lookahead(LexState *ls); 85 | LUAI_FUNC l_noret luaX_syntaxerror(LexState *ls, const char *s); 86 | LUAI_FUNC const char *luaX_token2str(LexState *ls, int token); 87 | 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /lua-5.3.4/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lmem_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lgc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstate.h" 23 | 24 | 25 | 26 | /* 27 | ** About the realloc function: 28 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 29 | ** ('osize' is the old size, 'nsize' is the new size) 30 | ** 31 | ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no 32 | ** matter 'x'). 33 | ** 34 | ** * frealloc(ud, p, x, 0) frees the block 'p' 35 | ** (in this specific case, frealloc must return NULL); 36 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 37 | ** (which is equivalent to free(NULL) in ISO C) 38 | ** 39 | ** frealloc returns NULL if it cannot create or reallocate the area 40 | ** (any reallocation to an equal or smaller size cannot fail!) 41 | */ 42 | 43 | 44 | 45 | #define MINSIZEARRAY 4 46 | 47 | 48 | void *luaM_growaux_(lua_State *L, void *block, int *size, size_t size_elems, 49 | int limit, const char *what) 50 | { 51 | void *newblock; 52 | int newsize; 53 | if (*size >= limit / 2) /* cannot double it? */ 54 | { 55 | if (*size >= limit) /* cannot grow even a little? */ 56 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 57 | newsize = limit; /* still have at least one free place */ 58 | } 59 | else 60 | { 61 | newsize = (*size) * 2; 62 | if (newsize < MINSIZEARRAY) 63 | newsize = MINSIZEARRAY; /* minimum size */ 64 | } 65 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 66 | *size = newsize; /* update only when everything else is OK */ 67 | return newblock; 68 | } 69 | 70 | 71 | l_noret luaM_toobig(lua_State *L) 72 | { 73 | luaG_runerror(L, "memory allocation error: block too big"); 74 | } 75 | 76 | 77 | 78 | /* 79 | ** generic allocation routine. 80 | */ 81 | void *luaM_realloc_(lua_State *L, void *block, size_t osize, size_t nsize) 82 | { 83 | void *newblock; 84 | global_State *g = G(L); 85 | size_t realosize = (block) ? osize : 0; 86 | lua_assert((realosize == 0) == (block == NULL)); 87 | #if defined(HARDMEMTESTS) 88 | if (nsize > realosize && g->gcrunning) 89 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 90 | #endif 91 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 92 | if (newblock == NULL && nsize > 0) 93 | { 94 | lua_assert(nsize > realosize); /* cannot fail when shrinking a block */ 95 | if (g->version) /* is state fully built? */ 96 | { 97 | luaC_fullgc(L, 1); /* try to free some memory... */ 98 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 99 | } 100 | if (newblock == NULL) 101 | luaD_throw(L, LUA_ERRMEM); 102 | } 103 | lua_assert((nsize == 0) == (newblock == NULL)); 104 | g->GCdebt = (g->GCdebt + nsize) - realosize; 105 | return newblock; 106 | } 107 | 108 | -------------------------------------------------------------------------------- /lua-5.3.4/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | /* 18 | ** This macro reallocs a vector 'b' from 'on' to 'n' elements, where 19 | ** each element has size 'e'. In case of arithmetic overflow of the 20 | ** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because 21 | ** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). 22 | ** 23 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 24 | ** comparison avoids a runtime comparison when overflow cannot occur. 25 | ** The compiler should be able to optimize the real test by itself, but 26 | ** when it does it, it may give a warning about "comparison is always 27 | ** false due to limited range of data type"; the +1 tricks the compiler, 28 | ** avoiding this warning but also this optimization.) 29 | */ 30 | #define luaM_reallocv(L,b,on,n,e) \ 31 | (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ 32 | ? luaM_toobig(L) : cast_void(0)) , \ 33 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 34 | 35 | /* 36 | ** Arrays of chars do not need any test 37 | */ 38 | #define luaM_reallocvchar(L,b,on,n) \ 39 | cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 40 | 41 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 42 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 43 | #define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) 44 | 45 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 46 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 47 | #define luaM_newvector(L,n,t) \ 48 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 49 | 50 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 51 | 52 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 53 | if ((nelems)+1 > (size)) \ 54 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 55 | 56 | #define luaM_reallocvector(L, v,oldn,n,t) \ 57 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 58 | 59 | LUAI_FUNC l_noret luaM_toobig(lua_State *L); 60 | 61 | /* not to be called directly */ 62 | LUAI_FUNC void *luaM_realloc_(lua_State *L, void *block, size_t oldsize, 63 | size_t size); 64 | LUAI_FUNC void *luaM_growaux_(lua_State *L, void *block, int *size, 65 | size_t size_elem, int limit, 66 | const char *what); 67 | 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /lua-5.3.4/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lopcodes.h" 16 | 17 | 18 | /* ORDER OP */ 19 | 20 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES + 1] = 21 | { 22 | "MOVE", 23 | "LOADK", 24 | "LOADKX", 25 | "LOADBOOL", 26 | "LOADNIL", 27 | "GETUPVAL", 28 | "GETTABUP", 29 | "GETTABLE", 30 | "SETTABUP", 31 | "SETUPVAL", 32 | "SETTABLE", 33 | "NEWTABLE", 34 | "SELF", 35 | "ADD", 36 | "SUB", 37 | "MUL", 38 | "MOD", 39 | "POW", 40 | "DIV", 41 | "IDIV", 42 | "BAND", 43 | "BOR", 44 | "BXOR", 45 | "SHL", 46 | "SHR", 47 | "UNM", 48 | "BNOT", 49 | "NOT", 50 | "LEN", 51 | "CONCAT", 52 | "JMP", 53 | "EQ", 54 | "LT", 55 | "LE", 56 | "TEST", 57 | "TESTSET", 58 | "CALL", 59 | "TAILCALL", 60 | "RETURN", 61 | "FORLOOP", 62 | "FORPREP", 63 | "TFORCALL", 64 | "TFORLOOP", 65 | "SETLIST", 66 | "CLOSURE", 67 | "VARARG", 68 | "EXTRAARG", 69 | NULL 70 | }; 71 | 72 | 73 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 74 | 75 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = 76 | { 77 | /* T A B C mode opcode */ 78 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 79 | , opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 80 | , opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 81 | , opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 82 | , opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 83 | , opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 84 | , opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 85 | , opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 86 | , opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 87 | , opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 88 | , opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 89 | , opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 90 | , opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 91 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 92 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 93 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 94 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 95 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 96 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 97 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */ 98 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */ 99 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */ 100 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */ 101 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */ 102 | , opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */ 103 | , opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 104 | , opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */ 105 | , opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 106 | , opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 107 | , opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 108 | , opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 109 | , opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 110 | , opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 111 | , opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 112 | , opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 113 | , opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 114 | , opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 115 | , opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 116 | , opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 117 | , opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 118 | , opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 119 | , opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 120 | , opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 121 | , opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 122 | , opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 123 | , opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 124 | , opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 125 | }; 126 | 127 | -------------------------------------------------------------------------------- /lua-5.3.4/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.76 2015/12/30 18:16:13 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression and variable descriptor. 17 | ** Code generation for variables and expressions can be delayed to allow 18 | ** optimizations; An 'expdesc' structure describes a potentially-delayed 19 | ** variable/expression. It has a description of its "main" value plus a 20 | ** list of conditional jumps that can also produce its value (generated 21 | ** by short-circuit operators 'and'/'or'). 22 | */ 23 | 24 | /* kinds of variables/expressions */ 25 | typedef enum 26 | { 27 | VVOID, /* when 'expdesc' describes the last expression a list, 28 | this kind means an empty list (so, no expression) */ 29 | VNIL, /* constant nil */ 30 | VTRUE, /* constant true */ 31 | VFALSE, /* constant false */ 32 | VK, /* constant in 'k'; info = index of constant in 'k' */ 33 | VKFLT, /* floating constant; nval = numerical float value */ 34 | VKINT, /* integer constant; nval = numerical integer value */ 35 | VNONRELOC, /* expression has its value in a fixed register; 36 | info = result register */ 37 | VLOCAL, /* local variable; info = local register */ 38 | VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ 39 | VINDEXED, /* indexed variable; 40 | ind.vt = whether 't' is register or upvalue; 41 | ind.t = table register or upvalue; 42 | ind.idx = key's R/K index */ 43 | VJMP, /* expression is a test/comparison; 44 | info = pc of corresponding jump instruction */ 45 | VRELOCABLE, /* expression can put result in any register; 46 | info = instruction pc */ 47 | VCALL, /* expression is a function call; info = instruction pc */ 48 | VVARARG /* vararg expression; info = instruction pc */ 49 | } expkind; 50 | 51 | 52 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 53 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 54 | 55 | typedef struct expdesc 56 | { 57 | expkind k; 58 | union 59 | { 60 | lua_Integer ival; /* for VKINT */ 61 | lua_Number nval; /* for VKFLT */ 62 | int info; /* for generic use */ 63 | struct /* for indexed variables (VINDEXED) */ 64 | { 65 | short idx; /* index (R/K) */ 66 | lu_byte t; /* table (register or upvalue) */ 67 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 68 | } ind; 69 | } u; 70 | int t; /* patch list of 'exit when true' */ 71 | int f; /* patch list of 'exit when false' */ 72 | } expdesc; 73 | 74 | 75 | /* description of active local variable */ 76 | typedef struct Vardesc 77 | { 78 | short idx; /* variable index in stack */ 79 | } Vardesc; 80 | 81 | 82 | /* description of pending goto statements and label statements */ 83 | typedef struct Labeldesc 84 | { 85 | TString *name; /* label identifier */ 86 | int pc; /* position in code */ 87 | int line; /* line where it appeared */ 88 | lu_byte nactvar; /* local level where it appears in current block */ 89 | } Labeldesc; 90 | 91 | 92 | /* list of labels or gotos */ 93 | typedef struct Labellist 94 | { 95 | Labeldesc *arr; /* array */ 96 | int n; /* number of entries in use */ 97 | int size; /* array size */ 98 | } Labellist; 99 | 100 | 101 | /* dynamic structures used by the parser */ 102 | typedef struct Dyndata 103 | { 104 | struct /* list of active local variables */ 105 | { 106 | Vardesc *arr; 107 | int n; 108 | int size; 109 | } actvar; 110 | Labellist gt; /* list of pending gotos */ 111 | Labellist label; /* list of active labels */ 112 | } Dyndata; 113 | 114 | 115 | /* control of blocks */ 116 | struct BlockCnt; /* defined in lparser.c */ 117 | 118 | 119 | /* state needed to generate code for a given function */ 120 | typedef struct FuncState 121 | { 122 | Proto *f; /* current function header */ 123 | struct FuncState *prev; /* enclosing function */ 124 | struct LexState *ls; /* lexical state */ 125 | struct BlockCnt *bl; /* chain of current blocks */ 126 | int pc; /* next position to code (equivalent to 'ncode') */ 127 | int lasttarget; /* 'label' of last 'jump label' */ 128 | int jpc; /* list of pending jumps to 'pc' */ 129 | int nk; /* number of elements in 'k' */ 130 | int np; /* number of elements in 'p' */ 131 | int firstlocal; /* index of first local var (in Dyndata array) */ 132 | short nlocvars; /* number of elements in 'f->locvars' */ 133 | lu_byte nactvar; /* number of active local variables */ 134 | lu_byte nups; /* number of upvalues */ 135 | lu_byte freereg; /* first free register */ 136 | } FuncState; 137 | 138 | 139 | LUAI_FUNC LClosure *luaY_parser(lua_State *L, ZIO *z, Mbuffer *buff, 140 | Dyndata *dyd, const char *name, int firstchar); 141 | 142 | 143 | #endif 144 | -------------------------------------------------------------------------------- /lua-5.3.4/lprefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $ 3 | ** Definitions for Lua code that must come before any other header file 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lprefix_h 8 | #define lprefix_h 9 | 10 | 11 | /* 12 | ** Allows POSIX/XSI stuff 13 | */ 14 | #if !defined(LUA_USE_C89) /* { */ 15 | 16 | #if !defined(_XOPEN_SOURCE) 17 | #define _XOPEN_SOURCE 600 18 | #elif _XOPEN_SOURCE == 0 19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ 20 | #endif 21 | 22 | /* 23 | ** Allows manipulation of large files in gcc and some other compilers 24 | */ 25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) 26 | #define _LARGEFILE_SOURCE 1 27 | #define _FILE_OFFSET_BITS 64 28 | #endif 29 | 30 | #endif /* } */ 31 | 32 | 33 | /* 34 | ** Windows stuff 35 | */ 36 | #if defined(_WIN32) /* { */ 37 | 38 | #if !defined(_CRT_SECURE_NO_WARNINGS) 39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ 40 | #endif 41 | 42 | #endif /* } */ 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /lua-5.3.4/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.56 2015/11/23 11:32:51 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lstring_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | 24 | 25 | #define MEMERRMSG "not enough memory" 26 | 27 | 28 | /* 29 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to 30 | ** compute its hash 31 | */ 32 | #if !defined(LUAI_HASHLIMIT) 33 | #define LUAI_HASHLIMIT 5 34 | #endif 35 | 36 | 37 | /* 38 | ** equality for long strings 39 | */ 40 | int luaS_eqlngstr(TString *a, TString *b) 41 | { 42 | size_t len = a->u.lnglen; 43 | lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); 44 | return (a == b) || /* same instance or... */ 45 | ((len == b->u.lnglen) && /* equal length and ... */ 46 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ 47 | } 48 | 49 | 50 | unsigned int luaS_hash(const char *str, size_t l, unsigned int seed) 51 | { 52 | unsigned int h = seed ^ cast(unsigned int, l); 53 | size_t step = (l >> LUAI_HASHLIMIT) + 1; 54 | for (; l >= step; l -= step) 55 | h ^= ((h << 5) + (h >> 2) + cast_byte(str[l - 1])); 56 | return h; 57 | } 58 | 59 | 60 | unsigned int luaS_hashlongstr(TString *ts) 61 | { 62 | lua_assert(ts->tt == LUA_TLNGSTR); 63 | if (ts->extra == 0) /* no hash? */ 64 | { 65 | ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash); 66 | ts->extra = 1; /* now it has its hash */ 67 | } 68 | return ts->hash; 69 | } 70 | 71 | 72 | /* 73 | ** resizes the string table 74 | */ 75 | void luaS_resize(lua_State *L, int newsize) 76 | { 77 | int i; 78 | stringtable *tb = &G(L)->strt; 79 | if (newsize > tb->size) /* grow table if needed */ 80 | { 81 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); 82 | for (i = tb->size; i < newsize; i++) 83 | tb->hash[i] = NULL; 84 | } 85 | for (i = 0; i < tb->size; i++) /* rehash */ 86 | { 87 | TString *p = tb->hash[i]; 88 | tb->hash[i] = NULL; 89 | while (p) /* for each node in the list */ 90 | { 91 | TString *hnext = p->u.hnext; /* save next */ 92 | unsigned int h = lmod(p->hash, newsize); /* new position */ 93 | p->u.hnext = tb->hash[h]; /* chain it */ 94 | tb->hash[h] = p; 95 | p = hnext; 96 | } 97 | } 98 | if (newsize < tb->size) /* shrink table if needed */ 99 | { 100 | /* vanishing slice should be empty */ 101 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); 102 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); 103 | } 104 | tb->size = newsize; 105 | } 106 | 107 | 108 | /* 109 | ** Clear API string cache. (Entries cannot be empty, so fill them with 110 | ** a non-collectable string.) 111 | */ 112 | void luaS_clearcache(global_State *g) 113 | { 114 | int i, j; 115 | for (i = 0; i < STRCACHE_N; i++) 116 | for (j = 0; j < STRCACHE_M; j++) 117 | { 118 | if (iswhite(g->strcache[i][j])) /* will entry be collected? */ 119 | g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ 120 | } 121 | } 122 | 123 | 124 | /* 125 | ** Initialize the string table and the string cache 126 | */ 127 | void luaS_init(lua_State *L) 128 | { 129 | global_State *g = G(L); 130 | int i, j; 131 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 132 | /* pre-create memory-error message */ 133 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); 134 | luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ 135 | for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ 136 | for (j = 0; j < STRCACHE_M; j++) 137 | g->strcache[i][j] = g->memerrmsg; 138 | } 139 | 140 | 141 | 142 | /* 143 | ** creates a new string object 144 | */ 145 | static TString *createstrobj(lua_State *L, size_t l, int tag, unsigned int h) 146 | { 147 | TString *ts; 148 | GCObject *o; 149 | size_t totalsize; /* total size of TString object */ 150 | totalsize = sizelstring(l); 151 | o = luaC_newobj(L, tag, totalsize); 152 | ts = gco2ts(o); 153 | ts->hash = h; 154 | ts->extra = 0; 155 | getstr(ts)[l] = '\0'; /* ending 0 */ 156 | return ts; 157 | } 158 | 159 | 160 | TString *luaS_createlngstrobj(lua_State *L, size_t l) 161 | { 162 | TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed); 163 | ts->u.lnglen = l; 164 | return ts; 165 | } 166 | 167 | 168 | void luaS_remove(lua_State *L, TString *ts) 169 | { 170 | stringtable *tb = &G(L)->strt; 171 | TString **p = &tb->hash[lmod(ts->hash, tb->size)]; 172 | while (*p != ts) /* find previous element */ 173 | p = &(*p)->u.hnext; 174 | *p = (*p)->u.hnext; /* remove element from its list */ 175 | tb->nuse--; 176 | } 177 | 178 | 179 | /* 180 | ** checks whether short string exists and reuses it or creates a new one 181 | */ 182 | static TString *internshrstr(lua_State *L, const char *str, size_t l) 183 | { 184 | TString *ts; 185 | global_State *g = G(L); 186 | unsigned int h = luaS_hash(str, l, g->seed); 187 | TString **list = &g->strt.hash[lmod(h, g->strt.size)]; 188 | lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ 189 | for (ts = *list; ts != NULL; ts = ts->u.hnext) 190 | { 191 | if (l == ts->shrlen && 192 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) 193 | { 194 | /* found! */ 195 | if (isdead(g, ts)) /* dead (but not collected yet)? */ 196 | changewhite(ts); /* resurrect it */ 197 | return ts; 198 | } 199 | } 200 | if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT / 2) 201 | { 202 | luaS_resize(L, g->strt.size * 2); 203 | list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ 204 | } 205 | ts = createstrobj(L, l, LUA_TSHRSTR, h); 206 | memcpy(getstr(ts), str, l * sizeof(char)); 207 | ts->shrlen = cast_byte(l); 208 | ts->u.hnext = *list; 209 | *list = ts; 210 | g->strt.nuse++; 211 | return ts; 212 | } 213 | 214 | 215 | /* 216 | ** new string (with explicit length) 217 | */ 218 | TString *luaS_newlstr(lua_State *L, const char *str, size_t l) 219 | { 220 | if (l <= LUAI_MAXSHORTLEN) /* short string? */ 221 | return internshrstr(L, str, l); 222 | else 223 | { 224 | TString *ts; 225 | if (l >= (MAX_SIZE - sizeof(TString)) / sizeof(char)) 226 | luaM_toobig(L); 227 | ts = luaS_createlngstrobj(L, l); 228 | memcpy(getstr(ts), str, l * sizeof(char)); 229 | return ts; 230 | } 231 | } 232 | 233 | 234 | /* 235 | ** Create or reuse a zero-terminated string, first checking in the 236 | ** cache (using the string address as a key). The cache can contain 237 | ** only zero-terminated strings, so it is safe to use 'strcmp' to 238 | ** check hits. 239 | */ 240 | TString *luaS_new(lua_State *L, const char *str) 241 | { 242 | unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ 243 | int j; 244 | TString **p = G(L)->strcache[i]; 245 | for (j = 0; j < STRCACHE_M; j++) 246 | { 247 | if (strcmp(str, getstr(p[j])) == 0) /* hit? */ 248 | return p[j]; /* that is it */ 249 | } 250 | /* normal route */ 251 | for (j = STRCACHE_M - 1; j > 0; j--) 252 | p[j] = p[j - 1]; /* move out last element */ 253 | /* new element is first in the list */ 254 | p[0] = luaS_newlstr(L, str, strlen(str)); 255 | return p[0]; 256 | } 257 | 258 | 259 | Udata *luaS_newudata(lua_State *L, size_t s) 260 | { 261 | Udata *u; 262 | GCObject *o; 263 | if (s > MAX_SIZE - sizeof(Udata)) 264 | luaM_toobig(L); 265 | o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s)); 266 | u = gco2u(o); 267 | u->len = s; 268 | u->metatable = NULL; 269 | setuservalue(L, u, luaO_nilobject); 270 | return u; 271 | } 272 | 273 | -------------------------------------------------------------------------------- /lua-5.3.4/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.61 2015/11/03 15:36:01 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) 16 | 17 | #define sizeludata(l) (sizeof(union UUdata) + (l)) 18 | #define sizeudata(u) sizeludata((u)->len) 19 | 20 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 21 | (sizeof(s)/sizeof(char))-1)) 22 | 23 | 24 | /* 25 | ** test whether a string is a reserved word 26 | */ 27 | #define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) 28 | 29 | 30 | /* 31 | ** equality for short strings, which are always internalized 32 | */ 33 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) 34 | 35 | 36 | LUAI_FUNC unsigned int luaS_hash(const char *str, size_t l, unsigned int seed); 37 | LUAI_FUNC unsigned int luaS_hashlongstr(TString *ts); 38 | LUAI_FUNC int luaS_eqlngstr(TString *a, TString *b); 39 | LUAI_FUNC void luaS_resize(lua_State *L, int newsize); 40 | LUAI_FUNC void luaS_clearcache(global_State *g); 41 | LUAI_FUNC void luaS_init(lua_State *L); 42 | LUAI_FUNC void luaS_remove(lua_State *L, TString *ts); 43 | LUAI_FUNC Udata *luaS_newudata(lua_State *L, size_t s); 44 | LUAI_FUNC TString *luaS_newlstr(lua_State *L, const char *str, size_t l); 45 | LUAI_FUNC TString *luaS_new(lua_State *L, const char *str); 46 | LUAI_FUNC TString *luaS_createlngstrobj(lua_State *L, size_t l); 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /lua-5.3.4/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.23 2016/12/22 13:08:50 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gval(n) (&(n)->i_val) 15 | #define gnext(n) ((n)->i_key.nk.next) 16 | 17 | 18 | /* 'const' to avoid wrong writings that can mess up field 'next' */ 19 | #define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) 20 | 21 | /* 22 | ** writable version of 'gkey'; allows updates to individual fields, 23 | ** but not to the whole (which has incompatible type) 24 | */ 25 | #define wgkey(n) (&(n)->i_key.nk) 26 | 27 | #define invalidateTMcache(t) ((t)->flags = 0) 28 | 29 | 30 | /* true when 't' is using 'dummynode' as its hash part */ 31 | #define isdummy(t) ((t)->lastfree == NULL) 32 | 33 | 34 | /* allocated size for hash nodes */ 35 | #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) 36 | 37 | 38 | /* returns the key, given the value of a table entry */ 39 | #define keyfromval(v) \ 40 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) 41 | 42 | 43 | LUAI_FUNC const TValue *luaH_getint(Table *t, lua_Integer key); 44 | LUAI_FUNC void luaH_setint(lua_State *L, Table *t, lua_Integer key, 45 | TValue *value); 46 | LUAI_FUNC const TValue *luaH_getshortstr(Table *t, TString *key); 47 | LUAI_FUNC const TValue *luaH_getstr(Table *t, TString *key); 48 | LUAI_FUNC const TValue *luaH_get(Table *t, const TValue *key); 49 | LUAI_FUNC TValue *luaH_newkey(lua_State *L, Table *t, const TValue *key); 50 | LUAI_FUNC TValue *luaH_set(lua_State *L, Table *t, const TValue *key); 51 | LUAI_FUNC Table *luaH_new(lua_State *L); 52 | LUAI_FUNC void luaH_resize(lua_State *L, Table *t, unsigned int nasize, 53 | unsigned int nhsize); 54 | LUAI_FUNC void luaH_resizearray(lua_State *L, Table *t, unsigned int nasize); 55 | LUAI_FUNC void luaH_free(lua_State *L, Table *t); 56 | LUAI_FUNC int luaH_next(lua_State *L, Table *t, StkId key); 57 | LUAI_FUNC int luaH_getn(Table *t); 58 | 59 | 60 | #if defined(LUA_DEBUG) 61 | LUAI_FUNC Node *luaH_mainposition(const Table *t, const TValue *key); 62 | LUAI_FUNC int luaH_isdummy(const Table *t); 63 | #endif 64 | 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /lua-5.3.4/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.38 2016/12/22 13:08:50 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ltm_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | #include "lstring.h" 22 | #include "ltable.h" 23 | #include "ltm.h" 24 | #include "lvm.h" 25 | 26 | 27 | static const char udatatypename[] = "userdata"; 28 | 29 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = 30 | { 31 | "no value", 32 | "nil", "boolean", udatatypename, "number", 33 | "string", "table", "function", udatatypename, "thread", 34 | "proto" /* this last case is used for tests only */ 35 | }; 36 | 37 | 38 | void luaT_init(lua_State *L) 39 | { 40 | static const char *const luaT_eventname[] = /* ORDER TM */ 41 | { 42 | "__index", "__newindex", 43 | "__gc", "__mode", "__len", "__eq", 44 | "__add", "__sub", "__mul", "__mod", "__pow", 45 | "__div", "__idiv", 46 | "__band", "__bor", "__bxor", "__shl", "__shr", 47 | "__unm", "__bnot", "__lt", "__le", 48 | "__concat", "__call" 49 | }; 50 | int i; 51 | for (i = 0; i < TM_N; i++) 52 | { 53 | G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); 54 | luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ 55 | } 56 | } 57 | 58 | 59 | /* 60 | ** function to be used with macro "fasttm": optimized for absence of 61 | ** tag methods 62 | */ 63 | const TValue *luaT_gettm(Table *events, TMS event, TString *ename) 64 | { 65 | const TValue *tm = luaH_getshortstr(events, ename); 66 | lua_assert(event <= TM_EQ); 67 | if (ttisnil(tm)) /* no tag method? */ 68 | { 69 | events->flags |= cast_byte(1u << event); /* cache this fact */ 70 | return NULL; 71 | } 72 | else return tm; 73 | } 74 | 75 | 76 | const TValue *luaT_gettmbyobj(lua_State *L, const TValue *o, TMS event) 77 | { 78 | Table *mt; 79 | switch (ttnov(o)) 80 | { 81 | case LUA_TTABLE: 82 | mt = hvalue(o)->metatable; 83 | break; 84 | case LUA_TUSERDATA: 85 | mt = uvalue(o)->metatable; 86 | break; 87 | default: 88 | mt = G(L)->mt[ttnov(o)]; 89 | } 90 | return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject); 91 | } 92 | 93 | 94 | /* 95 | ** Return the name of the type of an object. For tables and userdata 96 | ** with metatable, use their '__name' metafield, if present. 97 | */ 98 | const char *luaT_objtypename(lua_State *L, const TValue *o) 99 | { 100 | Table *mt; 101 | if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || 102 | (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) 103 | { 104 | const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); 105 | if (ttisstring(name)) /* is '__name' a string? */ 106 | return getstr(tsvalue(name)); /* use it as type name */ 107 | } 108 | return ttypename(ttnov(o)); /* else use standard type name */ 109 | } 110 | 111 | 112 | void luaT_callTM(lua_State *L, const TValue *f, const TValue *p1, 113 | const TValue *p2, TValue *p3, int hasres) 114 | { 115 | ptrdiff_t result = savestack(L, p3); 116 | StkId func = L->top; 117 | setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ 118 | setobj2s(L, func + 1, p1); /* 1st argument */ 119 | setobj2s(L, func + 2, p2); /* 2nd argument */ 120 | L->top += 3; 121 | if (!hasres) /* no result? 'p3' is third argument */ 122 | setobj2s(L, L->top++, p3); /* 3rd argument */ 123 | /* metamethod may yield only when called from Lua code */ 124 | if (isLua(L->ci)) 125 | luaD_call(L, func, hasres); 126 | else 127 | luaD_callnoyield(L, func, hasres); 128 | if (hasres) /* if has result, move it to its place */ 129 | { 130 | p3 = restorestack(L, result); 131 | setobjs2s(L, p3, --L->top); 132 | } 133 | } 134 | 135 | 136 | int luaT_callbinTM(lua_State *L, const TValue *p1, const TValue *p2, 137 | StkId res, TMS event) 138 | { 139 | const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ 140 | if (ttisnil(tm)) 141 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 142 | if (ttisnil(tm)) return 0; 143 | luaT_callTM(L, tm, p1, p2, res, 1); 144 | return 1; 145 | } 146 | 147 | 148 | void luaT_trybinTM(lua_State *L, const TValue *p1, const TValue *p2, 149 | StkId res, TMS event) 150 | { 151 | if (!luaT_callbinTM(L, p1, p2, res, event)) 152 | { 153 | switch (event) 154 | { 155 | case TM_CONCAT: 156 | luaG_concaterror(L, p1, p2); 157 | /* call never returns, but to avoid warnings: *//* FALLTHROUGH */ 158 | case TM_BAND: 159 | case TM_BOR: 160 | case TM_BXOR: 161 | case TM_SHL: 162 | case TM_SHR: 163 | case TM_BNOT: 164 | { 165 | lua_Number dummy; 166 | if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) 167 | luaG_tointerror(L, p1, p2); 168 | else 169 | luaG_opinterror(L, p1, p2, "perform bitwise operation on"); 170 | } 171 | /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ 172 | default: 173 | luaG_opinterror(L, p1, p2, "perform arithmetic on"); 174 | } 175 | } 176 | } 177 | 178 | 179 | int luaT_callorderTM(lua_State *L, const TValue *p1, const TValue *p2, 180 | TMS event) 181 | { 182 | if (!luaT_callbinTM(L, p1, p2, L->top, event)) 183 | return -1; /* no metamethod */ 184 | else 185 | return !l_isfalse(L->top); 186 | } 187 | 188 | -------------------------------------------------------------------------------- /lua-5.3.4/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.22 2016/02/26 19:20:15 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" and "ORDER OP" 17 | */ 18 | typedef enum 19 | { 20 | TM_INDEX, 21 | TM_NEWINDEX, 22 | TM_GC, 23 | TM_MODE, 24 | TM_LEN, 25 | TM_EQ, /* last tag method with fast access */ 26 | TM_ADD, 27 | TM_SUB, 28 | TM_MUL, 29 | TM_MOD, 30 | TM_POW, 31 | TM_DIV, 32 | TM_IDIV, 33 | TM_BAND, 34 | TM_BOR, 35 | TM_BXOR, 36 | TM_SHL, 37 | TM_SHR, 38 | TM_UNM, 39 | TM_BNOT, 40 | TM_LT, 41 | TM_LE, 42 | TM_CONCAT, 43 | TM_CALL, 44 | TM_N /* number of elements in the enum */ 45 | } TMS; 46 | 47 | 48 | 49 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 50 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 51 | 52 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 53 | 54 | #define ttypename(x) luaT_typenames_[(x) + 1] 55 | 56 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 57 | 58 | 59 | LUAI_FUNC const char *luaT_objtypename(lua_State *L, const TValue *o); 60 | 61 | LUAI_FUNC const TValue *luaT_gettm(Table *events, TMS event, TString *ename); 62 | LUAI_FUNC const TValue *luaT_gettmbyobj(lua_State *L, const TValue *o, 63 | TMS event); 64 | LUAI_FUNC void luaT_init(lua_State *L); 65 | 66 | LUAI_FUNC void luaT_callTM(lua_State *L, const TValue *f, const TValue *p1, 67 | const TValue *p2, TValue *p3, int hasres); 68 | LUAI_FUNC int luaT_callbinTM(lua_State *L, const TValue *p1, const TValue *p2, 69 | StkId res, TMS event); 70 | LUAI_FUNC void luaT_trybinTM(lua_State *L, const TValue *p1, const TValue *p2, 71 | StkId res, TMS event); 72 | LUAI_FUNC int luaT_callorderTM(lua_State *L, const TValue *p1, 73 | const TValue *p2, TMS event); 74 | 75 | 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /lua-5.3.4/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 | -------------------------------------------------------------------------------- /lua-5.3.4/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 | -------------------------------------------------------------------------------- /lua-5.3.4/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 2.44 2015/11/02 16:09:30 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lundump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lfunc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstring.h" 23 | #include "lundump.h" 24 | #include "lzio.h" 25 | 26 | 27 | #if !defined(luai_verifycode) 28 | #define luai_verifycode(L,b,f) /* empty */ 29 | #endif 30 | 31 | 32 | typedef struct 33 | { 34 | lua_State *L; 35 | ZIO *Z; 36 | const char *name; 37 | } LoadState; 38 | 39 | 40 | static l_noret error(LoadState *S, const char *why) 41 | { 42 | luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); 43 | luaD_throw(S->L, LUA_ERRSYNTAX); 44 | } 45 | 46 | 47 | /* 48 | ** All high-level loads go through LoadVector; you can change it to 49 | ** adapt to the endianness of the input 50 | */ 51 | #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) 52 | 53 | static void LoadBlock(LoadState *S, void *b, size_t size) 54 | { 55 | if (luaZ_read(S->Z, b, size) != 0) 56 | error(S, "truncated"); 57 | } 58 | 59 | 60 | #define LoadVar(S,x) LoadVector(S,&x,1) 61 | 62 | 63 | static lu_byte LoadByte(LoadState *S) 64 | { 65 | lu_byte x; 66 | LoadVar(S, x); 67 | return x; 68 | } 69 | 70 | 71 | static int LoadInt(LoadState *S) 72 | { 73 | int x; 74 | LoadVar(S, x); 75 | return x; 76 | } 77 | 78 | 79 | static lua_Number LoadNumber(LoadState *S) 80 | { 81 | lua_Number x; 82 | LoadVar(S, x); 83 | return x; 84 | } 85 | 86 | 87 | static lua_Integer LoadInteger(LoadState *S) 88 | { 89 | lua_Integer x; 90 | LoadVar(S, x); 91 | return x; 92 | } 93 | 94 | 95 | static TString *LoadString(LoadState *S) 96 | { 97 | size_t size = LoadByte(S); 98 | if (size == 0xFF) 99 | LoadVar(S, size); 100 | if (size == 0) 101 | return NULL; 102 | else if (--size <= LUAI_MAXSHORTLEN) /* short string? */ 103 | { 104 | char buff[LUAI_MAXSHORTLEN]; 105 | LoadVector(S, buff, size); 106 | return luaS_newlstr(S->L, buff, size); 107 | } 108 | else /* long string */ 109 | { 110 | TString *ts = luaS_createlngstrobj(S->L, size); 111 | LoadVector(S, getstr(ts), size); /* load directly in final place */ 112 | return ts; 113 | } 114 | } 115 | 116 | 117 | static void LoadCode(LoadState *S, Proto *f) 118 | { 119 | int n = LoadInt(S); 120 | f->code = luaM_newvector(S->L, n, Instruction); 121 | f->sizecode = n; 122 | LoadVector(S, f->code, n); 123 | } 124 | 125 | 126 | static void LoadFunction(LoadState *S, Proto *f, TString *psource); 127 | 128 | 129 | static void LoadConstants(LoadState *S, Proto *f) 130 | { 131 | int i; 132 | int n = LoadInt(S); 133 | f->k = luaM_newvector(S->L, n, TValue); 134 | f->sizek = n; 135 | for (i = 0; i < n; i++) 136 | setnilvalue(&f->k[i]); 137 | for (i = 0; i < n; i++) 138 | { 139 | TValue *o = &f->k[i]; 140 | int t = LoadByte(S); 141 | switch (t) 142 | { 143 | case LUA_TNIL: 144 | setnilvalue(o); 145 | break; 146 | case LUA_TBOOLEAN: 147 | setbvalue(o, LoadByte(S)); 148 | break; 149 | case LUA_TNUMFLT: 150 | setfltvalue(o, LoadNumber(S)); 151 | break; 152 | case LUA_TNUMINT: 153 | setivalue(o, LoadInteger(S)); 154 | break; 155 | case LUA_TSHRSTR: 156 | case LUA_TLNGSTR: 157 | setsvalue2n(S->L, o, LoadString(S)); 158 | break; 159 | default: 160 | lua_assert(0); 161 | } 162 | } 163 | } 164 | 165 | 166 | static void LoadProtos(LoadState *S, Proto *f) 167 | { 168 | int i; 169 | int n = LoadInt(S); 170 | f->p = luaM_newvector(S->L, n, Proto *); 171 | f->sizep = n; 172 | for (i = 0; i < n; i++) 173 | f->p[i] = NULL; 174 | for (i = 0; i < n; i++) 175 | { 176 | f->p[i] = luaF_newproto(S->L); 177 | LoadFunction(S, f->p[i], f->source); 178 | } 179 | } 180 | 181 | 182 | static void LoadUpvalues(LoadState *S, Proto *f) 183 | { 184 | int i, n; 185 | n = LoadInt(S); 186 | f->upvalues = luaM_newvector(S->L, n, Upvaldesc); 187 | f->sizeupvalues = n; 188 | for (i = 0; i < n; i++) 189 | f->upvalues[i].name = NULL; 190 | for (i = 0; i < n; i++) 191 | { 192 | f->upvalues[i].instack = LoadByte(S); 193 | f->upvalues[i].idx = LoadByte(S); 194 | } 195 | } 196 | 197 | 198 | static void LoadDebug(LoadState *S, Proto *f) 199 | { 200 | int i, n; 201 | n = LoadInt(S); 202 | f->lineinfo = luaM_newvector(S->L, n, int); 203 | f->sizelineinfo = n; 204 | LoadVector(S, f->lineinfo, n); 205 | n = LoadInt(S); 206 | f->locvars = luaM_newvector(S->L, n, LocVar); 207 | f->sizelocvars = n; 208 | for (i = 0; i < n; i++) 209 | f->locvars[i].varname = NULL; 210 | for (i = 0; i < n; i++) 211 | { 212 | f->locvars[i].varname = LoadString(S); 213 | f->locvars[i].startpc = LoadInt(S); 214 | f->locvars[i].endpc = LoadInt(S); 215 | } 216 | n = LoadInt(S); 217 | for (i = 0; i < n; i++) 218 | f->upvalues[i].name = LoadString(S); 219 | } 220 | 221 | 222 | static void LoadFunction(LoadState *S, Proto *f, TString *psource) 223 | { 224 | f->source = LoadString(S); 225 | if (f->source == NULL) /* no source in dump? */ 226 | f->source = psource; /* reuse parent's source */ 227 | f->linedefined = LoadInt(S); 228 | f->lastlinedefined = LoadInt(S); 229 | f->numparams = LoadByte(S); 230 | f->is_vararg = LoadByte(S); 231 | f->maxstacksize = LoadByte(S); 232 | LoadCode(S, f); 233 | LoadConstants(S, f); 234 | LoadUpvalues(S, f); 235 | LoadProtos(S, f); 236 | LoadDebug(S, f); 237 | } 238 | 239 | 240 | static void checkliteral(LoadState *S, const char *s, const char *msg) 241 | { 242 | char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ 243 | size_t len = strlen(s); 244 | LoadVector(S, buff, len); 245 | if (memcmp(s, buff, len) != 0) 246 | error(S, msg); 247 | } 248 | 249 | 250 | static void fchecksize(LoadState *S, size_t size, const char *tname) 251 | { 252 | if (LoadByte(S) != size) 253 | error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); 254 | } 255 | 256 | 257 | #define checksize(S,t) fchecksize(S,sizeof(t),#t) 258 | 259 | static void checkHeader(LoadState *S) 260 | { 261 | checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ 262 | if (LoadByte(S) != LUAC_VERSION) 263 | error(S, "version mismatch in"); 264 | if (LoadByte(S) != LUAC_FORMAT) 265 | error(S, "format mismatch in"); 266 | checkliteral(S, LUAC_DATA, "corrupted"); 267 | checksize(S, int); 268 | checksize(S, size_t); 269 | checksize(S, Instruction); 270 | checksize(S, lua_Integer); 271 | checksize(S, lua_Number); 272 | if (LoadInteger(S) != LUAC_INT) 273 | error(S, "endianness mismatch in"); 274 | if (LoadNumber(S) != LUAC_NUM) 275 | error(S, "float format mismatch in"); 276 | } 277 | 278 | 279 | /* 280 | ** load precompiled chunk 281 | */ 282 | LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) 283 | { 284 | LoadState S; 285 | LClosure *cl; 286 | if (*name == '@' || *name == '=') 287 | S.name = name + 1; 288 | else if (*name == LUA_SIGNATURE[0]) 289 | S.name = "binary string"; 290 | else 291 | S.name = name; 292 | S.L = L; 293 | S.Z = Z; 294 | checkHeader(&S); 295 | cl = luaF_newLclosure(L, LoadByte(&S)); 296 | setclLvalue(L, L->top, cl); 297 | luaD_inctop(L); 298 | cl->p = luaF_newproto(L); 299 | LoadFunction(&S, cl->p, NULL); 300 | lua_assert(cl->nupvalues == cl->p->sizeupvalues); 301 | luai_verifycode(L, buff, cl->p); 302 | return cl; 303 | } 304 | 305 | -------------------------------------------------------------------------------- /lua-5.3.4/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.45 2015/09/08 15:41:05 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* data to catch conversion errors */ 16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n" 17 | 18 | #define LUAC_INT 0x5678 19 | #define LUAC_NUM cast_num(370.5) 20 | 21 | #define MYINT(s) (s[0]-'0') 22 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) 23 | #define LUAC_FORMAT 0 /* this is the official format */ 24 | 25 | /* load one chunk; from lundump.c */ 26 | LUAI_FUNC LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name); 27 | 28 | /* dump one chunk; from ldump.c */ 29 | LUAI_FUNC int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, 30 | void *data, int strip); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /lua-5.3.4/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.41 2016/12/22 13:08:50 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #if !defined(LUA_NOCVTN2S) 17 | #define cvt2str(o) ttisnumber(o) 18 | #else 19 | #define cvt2str(o) 0 /* no conversion from numbers to strings */ 20 | #endif 21 | 22 | 23 | #if !defined(LUA_NOCVTS2N) 24 | #define cvt2num(o) ttisstring(o) 25 | #else 26 | #define cvt2num(o) 0 /* no conversion from strings to numbers */ 27 | #endif 28 | 29 | 30 | /* 31 | ** You can define LUA_FLOORN2I if you want to convert floats to integers 32 | ** by flooring them (instead of raising an error if they are not 33 | ** integral values) 34 | */ 35 | #if !defined(LUA_FLOORN2I) 36 | #define LUA_FLOORN2I 0 37 | #endif 38 | 39 | 40 | #define tonumber(o,n) \ 41 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) 42 | 43 | #define tointeger(o,i) \ 44 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) 45 | 46 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) 47 | 48 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) 49 | 50 | 51 | /* 52 | ** fast track for 'gettable': if 't' is a table and 't[k]' is not nil, 53 | ** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise, 54 | ** return 0 (meaning it will have to check metamethod) with 'slot' 55 | ** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise). 56 | ** 'f' is the raw get function to use. 57 | */ 58 | #define luaV_fastget(L,t,k,slot,f) \ 59 | (!ttistable(t) \ 60 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 61 | : (slot = f(hvalue(t), k), /* else, do raw access */ \ 62 | !ttisnil(slot))) /* result not nil? */ 63 | 64 | /* 65 | ** standard implementation for 'gettable' 66 | */ 67 | #define luaV_gettable(L,t,k,v) { const TValue *slot; \ 68 | if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ 69 | else luaV_finishget(L,t,k,v,slot); } 70 | 71 | 72 | /* 73 | ** Fast track for set table. If 't' is a table and 't[k]' is not nil, 74 | ** call GC barrier, do a raw 't[k]=v', and return true; otherwise, 75 | ** return false with 'slot' equal to NULL (if 't' is not a table) or 76 | ** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro 77 | ** returns true, there is no need to 'invalidateTMcache', because the 78 | ** call is not creating a new entry. 79 | */ 80 | #define luaV_fastset(L,t,k,slot,f,v) \ 81 | (!ttistable(t) \ 82 | ? (slot = NULL, 0) \ 83 | : (slot = f(hvalue(t), k), \ 84 | ttisnil(slot) ? 0 \ 85 | : (luaC_barrierback(L, hvalue(t), v), \ 86 | setobj2t(L, cast(TValue *,slot), v), \ 87 | 1))) 88 | 89 | 90 | #define luaV_settable(L,t,k,v) { const TValue *slot; \ 91 | if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ 92 | luaV_finishset(L,t,k,v,slot); } 93 | 94 | 95 | 96 | LUAI_FUNC int luaV_equalobj(lua_State *L, const TValue *t1, const TValue *t2); 97 | LUAI_FUNC int luaV_lessthan(lua_State *L, const TValue *l, const TValue *r); 98 | LUAI_FUNC int luaV_lessequal(lua_State *L, const TValue *l, const TValue *r); 99 | LUAI_FUNC int luaV_tonumber_(const TValue *obj, lua_Number *n); 100 | LUAI_FUNC int luaV_tointeger(const TValue *obj, lua_Integer *p, int mode); 101 | LUAI_FUNC void luaV_finishget(lua_State *L, const TValue *t, TValue *key, 102 | StkId val, const TValue *slot); 103 | LUAI_FUNC void luaV_finishset(lua_State *L, const TValue *t, TValue *key, 104 | StkId val, const TValue *slot); 105 | LUAI_FUNC void luaV_finishOp(lua_State *L); 106 | LUAI_FUNC void luaV_execute(lua_State *L); 107 | LUAI_FUNC void luaV_concat(lua_State *L, int total); 108 | LUAI_FUNC lua_Integer luaV_div(lua_State *L, lua_Integer x, lua_Integer y); 109 | LUAI_FUNC lua_Integer luaV_mod(lua_State *L, lua_Integer x, lua_Integer y); 110 | LUAI_FUNC lua_Integer luaV_shiftl(lua_Integer x, lua_Integer y); 111 | LUAI_FUNC void luaV_objlen(lua_State *L, StkId ra, const TValue *rb); 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /lua-5.3.4/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.37 2015/09/08 15:41:05 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lzio_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "llimits.h" 18 | #include "lmem.h" 19 | #include "lstate.h" 20 | #include "lzio.h" 21 | 22 | 23 | int luaZ_fill(ZIO *z) 24 | { 25 | size_t size; 26 | lua_State *L = z->L; 27 | const char *buff; 28 | lua_unlock(L); 29 | buff = z->reader(L, z->data, &size); 30 | lua_lock(L); 31 | if (buff == NULL || size == 0) 32 | return EOZ; 33 | z->n = size - 1; /* discount char being returned */ 34 | z->p = buff; 35 | return cast_uchar(*(z->p++)); 36 | } 37 | 38 | 39 | void luaZ_init(lua_State *L, ZIO *z, lua_Reader reader, void *data) 40 | { 41 | z->L = L; 42 | z->reader = reader; 43 | z->data = data; 44 | z->n = 0; 45 | z->p = NULL; 46 | } 47 | 48 | 49 | /* --------------------------------------------------------------- read --- */ 50 | size_t luaZ_read(ZIO *z, void *b, size_t n) 51 | { 52 | while (n) 53 | { 54 | size_t m; 55 | if (z->n == 0) /* no bytes in buffer? */ 56 | { 57 | if (luaZ_fill(z) == EOZ) /* try to read more */ 58 | return n; /* no more input; return number of missing bytes */ 59 | else 60 | { 61 | z->n++; /* luaZ_fill consumed first byte; put it back */ 62 | z->p--; 63 | } 64 | } 65 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 66 | memcpy(b, z->p, m); 67 | z->n -= m; 68 | z->p += m; 69 | b = (char *)b + m; 70 | n -= m; 71 | } 72 | return 0; 73 | } 74 | 75 | -------------------------------------------------------------------------------- /lua-5.3.4/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.31 2015/09/08 15:41:05 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer 24 | { 25 | char *buffer; 26 | size_t n; 27 | size_t buffsize; 28 | } Mbuffer; 29 | 30 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 31 | 32 | #define luaZ_buffer(buff) ((buff)->buffer) 33 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 34 | #define luaZ_bufflen(buff) ((buff)->n) 35 | 36 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 37 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 38 | 39 | 40 | #define luaZ_resizebuffer(L, buff, size) \ 41 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 42 | (buff)->buffsize, size), \ 43 | (buff)->buffsize = size) 44 | 45 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 46 | 47 | 48 | LUAI_FUNC void luaZ_init(lua_State *L, ZIO *z, lua_Reader reader, 49 | void *data); 50 | LUAI_FUNC size_t luaZ_read(ZIO *z, void *b, size_t n); /* read next n bytes */ 51 | 52 | 53 | 54 | /* --------- Private Part ------------------ */ 55 | 56 | struct Zio 57 | { 58 | size_t n; /* bytes still unread */ 59 | const char *p; /* current position in buffer */ 60 | lua_Reader reader; /* reader function */ 61 | void *data; /* additional data */ 62 | lua_State *L; /* Lua state (for reader) */ 63 | }; 64 | 65 | 66 | LUAI_FUNC int luaZ_fill(ZIO *z); 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /lua2rtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @File: lua2rtt.c 3 | * @Author: liu2guang 4 | * @Date: 2018-05-06 09:16:56 5 | * 6 | * @LICENSE: https://github.com/liu2guang/lua2rtt/blob/master/LICENSE. 7 | * 8 | * Change Logs: 9 | * Date Author Notes 10 | * 2018-05-06 liu2guang The first version. 11 | */ 12 | #ifndef __LUA2RTT_H_ 13 | #define __LUA2RTT_H_ 14 | 15 | #include 16 | #include "luaconf.h" 17 | 18 | #define LUA2RTT_USING_DEBUG 19 | #ifndef LUA2RTT_USING_DEBUG 20 | #define LUA2RTT_DBG(fmt, ...) 21 | #else 22 | #define LUA2RTT_DBG(fmt, ...) \ 23 | do{ \ 24 | rt_kprintf("[\033[32mLua2RTT\033[0m] "); \ 25 | rt_kprintf(fmt, ##__VA_ARGS__); \ 26 | }while(0) 27 | #endif 28 | 29 | #ifndef LUA2RTT_THREAD_STACK_SIZE 30 | #define LUA2RTT_THREAD_STACK_SIZE 10240 31 | #endif 32 | #ifndef LUA2RTT_CMD_SIZE 33 | #define LUA2RTT_CMD_SIZE 512 34 | #endif 35 | #ifndef LUA2RTT_HISTORY_LINES 36 | #define LUA2RTT_HISTORY_LINES 5 37 | #endif 38 | 39 | enum lua2rtt_input_stat 40 | { 41 | LUA2RTT_WAIT_NORMAL, 42 | LUA2RTT_WAIT_SPEC_KEY, 43 | LUA2RTT_WAIT_FUNC_KEY, 44 | }; 45 | 46 | struct lua2rtt 47 | { 48 | rt_thread_t thread; 49 | struct rt_semaphore rx_sem; 50 | 51 | enum lua2rtt_input_stat stat; 52 | 53 | int argc; 54 | char *argv[3]; 55 | 56 | char lua_history[LUA2RTT_HISTORY_LINES][LUA2RTT_CMD_SIZE]; 57 | rt_uint16_t history_count; 58 | rt_uint16_t history_current; 59 | 60 | char line[LUA2RTT_CMD_SIZE]; 61 | rt_uint16_t line_position; 62 | rt_uint8_t line_curpos; 63 | 64 | rt_device_t device; 65 | rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size); /* msh??????? */ 66 | }; 67 | typedef struct lua2rtt *lua2rtt_t; 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /script/script_bspcfg: -------------------------------------------------------------------------------- 1 | #ifndef RT_CONFIG_H__ 2 | #define RT_CONFIG_H__ 3 | 4 | /* Automatically generated file; DO NOT EDIT. */ 5 | /* RT-Thread Configuration */ 6 | 7 | /* RT-Thread Kernel */ 8 | 9 | #define RT_NAME_MAX 8 10 | #define RT_ALIGN_SIZE 4 11 | #define RT_THREAD_PRIORITY_32 12 | #define RT_THREAD_PRIORITY_MAX 32 13 | #define RT_TICK_PER_SECOND 100 14 | #define RT_DEBUG 15 | #define RT_USING_OVERFLOW_CHECK 16 | #define RT_DEBUG_INIT 0 17 | #define RT_DEBUG_THREAD 0 18 | #define RT_USING_HOOK 19 | #define RT_IDEL_HOOK_LIST_SIZE 4 20 | #define IDLE_THREAD_STACK_SIZE 256 21 | 22 | /* Inter-Thread communication */ 23 | 24 | #define RT_USING_SEMAPHORE 25 | #define RT_USING_MUTEX 26 | #define RT_USING_EVENT 27 | #define RT_USING_MAILBOX 28 | #define RT_USING_MESSAGEQUEUE 29 | 30 | /* Memory Management */ 31 | 32 | #define RT_USING_MEMHEAP 33 | #define RT_USING_MEMHEAP_AS_HEAP 34 | #define RT_USING_HEAP 35 | 36 | /* Kernel Device Object */ 37 | 38 | #define RT_USING_DEVICE 39 | #define RT_USING_CONSOLE 40 | #define RT_CONSOLEBUF_SIZE 128 41 | #define RT_CONSOLE_DEVICE_NAME "uart1" 42 | #define ARCH_ARM 43 | #define ARCH_ARM_CORTEX_M 44 | #define ARCH_ARM_CORTEX_M7 45 | 46 | /* RT-Thread Components */ 47 | 48 | #define RT_USING_COMPONENTS_INIT 49 | #define RT_USING_USER_MAIN 50 | #define RT_MAIN_THREAD_STACK_SIZE 2048 51 | 52 | /* C++ features */ 53 | 54 | 55 | /* Command shell */ 56 | 57 | #define RT_USING_FINSH 58 | #define FINSH_THREAD_NAME "tshell" 59 | #define FINSH_USING_HISTORY 60 | #define FINSH_HISTORY_LINES 5 61 | #define FINSH_USING_SYMTAB 62 | #define FINSH_USING_DESCRIPTION 63 | #define FINSH_THREAD_PRIORITY 20 64 | #define FINSH_THREAD_STACK_SIZE 4096 65 | #define FINSH_CMD_SIZE 80 66 | #define FINSH_USING_MSH 67 | #define FINSH_USING_MSH_DEFAULT 68 | #define FINSH_ARG_MAX 10 69 | 70 | /* Device virtual file system */ 71 | 72 | #define RT_USING_DFS 73 | #define DFS_USING_WORKDIR 74 | #define DFS_FILESYSTEMS_MAX 2 75 | #define DFS_FILESYSTEM_TYPES_MAX 2 76 | #define DFS_FD_MAX 4 77 | #define RT_USING_DFS_ELMFAT 78 | 79 | /* elm-chan's FatFs, Generic FAT Filesystem Module */ 80 | 81 | #define RT_DFS_ELM_CODE_PAGE 437 82 | #define RT_DFS_ELM_WORD_ACCESS 83 | #define RT_DFS_ELM_USE_LFN_3 84 | #define RT_DFS_ELM_USE_LFN 3 85 | #define RT_DFS_ELM_MAX_LFN 255 86 | #define RT_DFS_ELM_DRIVES 2 87 | #define RT_DFS_ELM_MAX_SECTOR_SIZE 512 88 | #define RT_DFS_ELM_REENTRANT 89 | #define RT_USING_DFS_DEVFS 90 | 91 | /* Device Drivers */ 92 | 93 | #define RT_USING_DEVICE_IPC 94 | #define RT_PIPE_BUFSZ 512 95 | #define RT_USING_SERIAL 96 | #define RT_USING_I2C 97 | #define RT_USING_I2C_BITOPS 98 | #define RT_USING_PIN 99 | #define RT_USING_RTC 100 | #define RT_USING_SDIO 101 | #define RT_SDIO_STACK_SIZE 512 102 | #define RT_SDIO_THREAD_PRIORITY 15 103 | #define RT_MMCSD_STACK_SIZE 1024 104 | #define RT_MMCSD_THREAD_PREORITY 22 105 | #define RT_MMCSD_MAX_PARTITION 16 106 | #define RT_USING_SPI 107 | 108 | /* Using USB */ 109 | 110 | 111 | /* POSIX layer and C standard library */ 112 | 113 | #define RT_USING_LIBC 114 | 115 | /* Network stack */ 116 | 117 | /* light weight TCP/IP stack */ 118 | 119 | #define RT_USING_LWIP 120 | #define RT_USING_LWIP202 121 | #define RT_LWIP_ICMP 122 | #define RT_LWIP_DNS 123 | #define RT_LWIP_DHCP 124 | #define IP_SOF_BROADCAST 1 125 | #define IP_SOF_BROADCAST_RECV 1 126 | 127 | /* Static IPv4 Address */ 128 | 129 | #define RT_LWIP_IPADDR "192.168.1.30" 130 | #define RT_LWIP_GWADDR "192.168.1.1" 131 | #define RT_LWIP_MSKADDR "255.255.255.0" 132 | #define RT_LWIP_UDP 133 | #define RT_LWIP_TCP 134 | #define RT_MEMP_NUM_NETCONN 8 135 | #define RT_LWIP_PBUF_NUM 16 136 | #define RT_LWIP_RAW_PCB_NUM 4 137 | #define RT_LWIP_UDP_PCB_NUM 4 138 | #define RT_LWIP_TCP_PCB_NUM 4 139 | #define RT_LWIP_TCP_SEG_NUM 40 140 | #define RT_LWIP_TCP_SND_BUF 8196 141 | #define RT_LWIP_TCP_WND 8196 142 | #define RT_LWIP_TCPTHREAD_PRIORITY 10 143 | #define RT_LWIP_TCPTHREAD_MBOX_SIZE 8 144 | #define RT_LWIP_TCPTHREAD_STACKSIZE 1024 145 | #define RT_LWIP_ETHTHREAD_PRIORITY 12 146 | #define RT_LWIP_ETHTHREAD_STACKSIZE 1024 147 | #define RT_LWIP_ETHTHREAD_MBOX_SIZE 8 148 | #define LWIP_NETIF_STATUS_CALLBACK 1 149 | #define SO_REUSE 1 150 | #define LWIP_SO_RCVTIMEO 1 151 | #define LWIP_SO_SNDTIMEO 1 152 | #define LWIP_SO_RCVBUF 1 153 | #define LWIP_NETIF_LOOPBACK 0 154 | 155 | /* Modbus master and slave stack */ 156 | 157 | 158 | /* VBUS(Virtual Software BUS) */ 159 | 160 | 161 | /* Utilities */ 162 | 163 | 164 | /* ARM CMSIS */ 165 | 166 | 167 | /* RT-Thread online packages */ 168 | 169 | /* IoT - internet of things */ 170 | 171 | 172 | /* Wi-Fi */ 173 | 174 | /* Marvell WiFi */ 175 | 176 | 177 | /* Wiced WiFi */ 178 | 179 | 180 | /* security packages */ 181 | 182 | 183 | /* language packages */ 184 | 185 | #define PKG_USING_LUA 186 | #define PKG_USING_LUA_LATEST_VERSION 187 | #define LUA_USING_PORTING_V534 188 | 189 | /* multimedia packages */ 190 | 191 | 192 | /* tools packages */ 193 | 194 | 195 | /* system packages */ 196 | 197 | #define PKG_USING_LITTLEVGL2RTT 198 | #define PKG_USING_LITTLEVGL2RTT_V001 199 | 200 | /* LittlevGL2RTT Options */ 201 | 202 | #define LV_MEM_DYNAMIC 203 | #define LV_MEM_CUSTOM 1 204 | #define LV_COLOR_DEPTH_16 205 | #define LV_COLOR_DEPTH 16 206 | #define LV_HOR_RES 480 207 | #define LV_VER_RES 272 208 | #define LV_DPI 50 209 | #define LITTLEVGL2RTT_USING_DEMO 210 | 211 | /* peripheral libraries and drivers */ 212 | 213 | 214 | /* miscellaneous packages */ 215 | 216 | 217 | /* sample package */ 218 | 219 | 220 | /* example package: hello */ 221 | 222 | #define SOC_IMXRT1052 223 | #define BOARD_USING_QSPIFLASH 224 | #define BOARD_RT1050_FIRE 225 | 226 | /* RT1050 Bsp Config */ 227 | 228 | /* Select uart drivers */ 229 | 230 | #define RT_USING_UART1 231 | 232 | /* Select spi bus and dev drivers */ 233 | 234 | #define LPSPI_CLK_SOURCE_FROM_PLL3PFD1 235 | #define LPSPI_CLK_SOURCE 0 236 | #define LPSPI_CLK_SOURCE_DIVIDER 8 237 | 238 | /* Select iic bus drivers */ 239 | 240 | 241 | /* Select lcd driver */ 242 | 243 | /* Notice: Fire Board para: 800*480 4 4 8 2 40 10 58 45 */ 244 | 245 | #define RT_USING_LCD 246 | #define LCD_WIDTH 480 247 | #define LCD_HEIGHT 272 248 | #define LCD_HFP 4 249 | #define LCD_VFP 4 250 | #define LCD_HBP 8 251 | #define LCD_VBP 2 252 | #define LCD_HSW 40 253 | #define LCD_VSW 10 254 | #define LCD_BL_PIN 106 255 | #define LCD_RST_PIN 45 256 | #define RT_USING_SDRAM 257 | 258 | #endif 259 | -------------------------------------------------------------------------------- /script/script_scons: -------------------------------------------------------------------------------- 1 | import os 2 | from building import * 3 | 4 | objs = [] 5 | cwd = GetCurrentDir() 6 | list = os.listdir(cwd) 7 | 8 | for item in list: 9 | if os.path.isfile(os.path.join(cwd, item, 'SConscript')): 10 | objs = objs + SConscript(os.path.join(item, 'SConscript')) 11 | 12 | Return('objs') 13 | --------------------------------------------------------------------------------