├── .github └── workflows │ └── run-tests.yml ├── .gitignore ├── .gitmodules ├── .travis.yml ├── README.md ├── compat.c ├── compat.h ├── compat_50103.c ├── compat_50200.c ├── compat_50204.c ├── compat_50300.c ├── compat_50301.c ├── compat_50302.c ├── compat_50400.c ├── compat_50401.c ├── compat_50402.c ├── compat_50403.c ├── getsize-scm-0.rockspec ├── getsize.c ├── ljdetect ├── .gitignore ├── ljdetect.h ├── mod.c └── test.lua ├── lua5.1 ├── README ├── lfunc.h ├── lgc.h ├── llimits.h ├── lmem.h ├── lobject.h ├── lstate.h ├── lstring.h ├── ltm.h └── lzio.h ├── lua5.2 ├── 4 │ ├── llimits.h │ └── lobject.h ├── README ├── lfunc.h ├── lgc.h ├── llimits.h ├── lmem.h ├── lobject.h ├── lstate.h ├── lstring.h ├── ltm.h └── lzio.h ├── lua5.3 ├── 1 │ ├── lgc.h │ ├── llimits.h │ ├── lmem.h │ ├── lobject.h │ ├── lstate.h │ ├── lstring.h │ ├── ltm.h │ └── lzio.h ├── 2 │ ├── llimits.h │ ├── lmem.h │ ├── lobject.h │ ├── lstate.h │ ├── ltm.h │ └── lzio.h ├── README ├── lfunc.h ├── lgc.h ├── llimits.h ├── lmem.h ├── lobject.h ├── lstate.h ├── lstring.h ├── ltm.h └── lzio.h ├── lua5.4 ├── 1 │ ├── llimits.h │ ├── lmem.h │ ├── lobject.h │ ├── lstate.h │ ├── ltm.h │ └── lzio.h ├── 2 │ ├── llimits.h │ ├── lmem.h │ ├── lobject.h │ ├── lstate.h │ ├── ltm.h │ └── lzio.h ├── 3 │ ├── llimits.h │ ├── lmem.h │ ├── lobject.h │ ├── lstate.h │ ├── ltm.h │ └── lzio.h ├── README ├── lfunc.h ├── lgc.h ├── llimits.h ├── lmem.h ├── lobject.h ├── lstate.h ├── lstring.h ├── ltm.h └── lzio.h ├── rockspecs ├── getsize-0.1-1.rockspec ├── getsize-0.2-1.rockspec ├── getsize-0.3-1.rockspec ├── getsize-0.4-1.rockspec ├── getsize-0.5-1.rockspec ├── getsize-0.6-1.rockspec ├── getsize-0.7-1.rockspec └── getsize-0.8-1.rockspec └── tests ├── build.sh ├── mem.lua └── test.lua /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | on: 3 | push: 4 | branches: ["master"] 5 | pull_request: 6 | jobs: 7 | test: 8 | strategy: 9 | fail-fast: false 10 | matrix: 11 | luaVersion: 12 | - "5.1.3" 13 | - "5.1.4" 14 | - "5.1.5" 15 | - "5.2.2" 16 | - "5.2.3" 17 | - "5.2.4" 18 | - "5.3.4" 19 | - "5.3.5" 20 | - "5.3.6" 21 | - "5.4.0" 22 | - "5.4.1" 23 | - "5.4.2" 24 | - "5.4.3" 25 | - "5.4.4" 26 | runs-on: ubuntu-latest 27 | steps: 28 | - uses: actions/checkout@v2 29 | - uses: leafo/gh-actions-lua@v8 30 | with: 31 | luaVersion: ${{ matrix.luaVersion }} 32 | - uses: leafo/gh-actions-luarocks@v4 33 | - run: luarocks make CFLAGS="-Wall -Wextra -O2 -fPIC" 34 | - run: lua tests/test.lua 35 | 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # docco output directory 2 | #docs/ 3 | 4 | # local files for remembering stuff 5 | HISTO 6 | TODO 7 | 8 | # temporary files 9 | .*.swp 10 | 11 | # object files 12 | *.o 13 | *.obj 14 | 15 | # libraries 16 | *.so 17 | *.dll 18 | *.a 19 | *.lib 20 | *.exp 21 | 22 | # executables 23 | *.exe 24 | 25 | # precompiled Lua bytecode files 26 | *.luac 27 | 28 | # LuaRocks packages 29 | *.rock 30 | 31 | # project specific files 32 | tests/lua-* 33 | 34 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule ".travis"] 2 | path = .travis 3 | url = https://github.com/siffiejoe/lua-travis 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | 3 | # select container based VM (no sudo available) 4 | sudo: false 5 | 6 | # use gcc as the C compiler 7 | compiler: gcc 8 | 9 | # specify operating systems to test on (linux and/or osx) 10 | os: 11 | - linux 12 | 13 | # environment variables for the build 14 | env: 15 | global: 16 | - LUAROCKS=3.1.3 17 | matrix: 18 | - LUA=5.1.3 19 | - LUA=5.1.4 20 | - LUA=5.1.5 21 | - LUA=5.2.2 22 | - LUA=5.2.3 23 | - LUA=5.2.4 24 | - LUA=5.3.0 25 | - LUA=5.3.1 26 | - LUA=5.3.2 27 | - LUA=5.3.3 28 | - LUA=5.3.4 29 | - LUA=5.3.5 30 | - LUA=5.3.6 31 | - LUA=5.4.0 32 | - LUA=5.4.1 33 | 34 | # only test changes to the master branch 35 | branches: 36 | only: 37 | - master 38 | 39 | # disable email notifications 40 | notifications: 41 | email: false 42 | 43 | # install dependencies (lua, luarocks, ...) 44 | install: 45 | - . .travis/install.sh 46 | 47 | # build (and test?) the project 48 | script: 49 | - luarocks make CFLAGS="-Wall -Wextra -O2 -fPIC" && lua tests/test.lua 50 | 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lua-getsize # 2 | 3 | [![Test Status](https://github.com/siffiejoe/lua-getsize/workflows/run-tests/badge.svg)](https://github.com/siffiejoe/lua-getsize/actions?workflow=run-tests) 4 | ![License](https://img.shields.io/badge/License-MIT%2FX11,%20MIT-blue.svg) 5 | ![Lua Versions](https://img.shields.io/badge/Lua-5.1%20|%205.2%20|%205.3%20|%205.4-blue.svg) 6 | ![No LuaJIT](https://img.shields.io/badge/No-LuaJIT-red.svg) 7 | 8 | 9 | ## Introduction ## 10 | 11 | This is a bundle of Matthew Wild's lua-getsize module with the 12 | necessary bits from Lua's sources (5.1, 5.2, 5.3, and 5.4) included to 13 | make it build as a standalone rock. 14 | 15 | lua-getsize is MIT/X11-licensed. Lua's source code is available 16 | under the MIT license. 17 | 18 | http://code.matthewwild.co.uk/lua-getsize/ 19 | http://www.lua.org/ 20 | 21 | At the moment the code in this repository includes the following 22 | changes to the original lua-getsize code: 23 | 24 | * ported to Lua 5.2, 5.3 and 5.4 25 | * fixed sizes of tables with empty hash part 26 | * report a userdata's full memory not just its payload 27 | * detect LuaJIT and raise an error 28 | * additionally report the sizes of a table's array/hash parts 29 | * additional option argument for counting shared internal objects 30 | like Lua upvalues and/or proto objects. 31 | "p" will count proto objects, "P" will _not_ count proto objects 32 | (the default), "u" will count Lua upvalues (the default), and "U" 33 | will _not_ count Lua upvalues. You can combine those options. 34 | 35 | 36 | ## Getting Started ## 37 | 38 | This module exports just one function: 39 | 40 | ```lua 41 | local getsize = require("getsize") 42 | ``` 43 | 44 | Calling this function with a Lua value as argument will return the 45 | size of the value in bytes. For tables it will return the lengths of 46 | array and hash part as additional return values. For functions you can 47 | decide whether you want to include the sizes of the proto object 48 | and/or the upvalues. 49 | 50 | ```lua 51 | print(getsize(getsize)) --> 64 52 | local function f() return getsize end 53 | print(getsize(f)) --> 80 54 | print(getsize(f, "uP")) --> 80 55 | print(getsize(f, "up")) --> 264 56 | print(getsize(f, "UP")) --> 40 57 | print(getsize({ 1, 2, 3, x = 4, y = 5 }) --> 152 3 2 58 | ``` 59 | -------------------------------------------------------------------------------- /compat.h: -------------------------------------------------------------------------------- 1 | #ifndef GETSIZE_COMPAT_H_ 2 | #define GETSIZE_COMPAT_H_ 3 | 4 | #include 5 | 6 | 7 | typedef /* TValue */ void* (*GetArgFunction)(lua_State*, int); 8 | typedef size_t (*BooleanSizeFunction)(/* TValue */ void const*); 9 | typedef size_t (*NumberSizeFunction)(/* TValue */ void const*); 10 | typedef size_t (*StringSizeFunction)(/* TValue */ void const*); 11 | typedef size_t (*FunctionSizeFunction)(/* TValue */ void const*, int, int, int); 12 | typedef /* Node */ void* (*TableNodeFunction)(/* TValue */ void const*); 13 | typedef size_t (*TableSizeFunction)(/* TValue */ void const*, 14 | /* Node */ void const*, 15 | unsigned*, 16 | unsigned*); 17 | typedef size_t (*UserdataSizeFunction)(/* TValue */ void const*); 18 | typedef size_t (*ThreadSizeFunction)(/* TValue */ void const*); 19 | 20 | typedef struct 21 | { 22 | GetArgFunction getArg; 23 | BooleanSizeFunction sizeBoolean; 24 | NumberSizeFunction sizeNumber; 25 | StringSizeFunction sizeString; 26 | FunctionSizeFunction sizeFunction; 27 | TableNodeFunction tableNode; 28 | TableSizeFunction sizeTable; 29 | UserdataSizeFunction sizeUserdata; 30 | ThreadSizeFunction sizeThread; 31 | } GetSizeVTable; 32 | 33 | 34 | GetSizeVTable* compat_init(lua_State* L); 35 | 36 | 37 | #endif /* GETSIZE_COMPAT_H_ */ 38 | 39 | -------------------------------------------------------------------------------- /compat_50103.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #if LUA_VERSION_NUM == 501 5 | #define LUA_CORE 6 | #include "lua5.1/lstate.h" 7 | #include "lua5.1/lobject.h" 8 | #include "lua5.1/lfunc.h" 9 | #include "lua5.1/lstring.h" 10 | 11 | 12 | void* getArg_50103(lua_State* L, int n) 13 | { 14 | return L->base + n - 1; 15 | } 16 | 17 | 18 | size_t sizeBoolean_50103(void const* o) 19 | { 20 | (void)o; 21 | return sizeof(int); 22 | } 23 | 24 | 25 | size_t sizeNumber_50103(void const* o) 26 | { 27 | (void)o; 28 | return sizeof(lua_Number); 29 | } 30 | 31 | 32 | size_t sizeString_50103(void const* o) 33 | { 34 | TValue const* v = o; 35 | return sizestring(tsvalue(v)); 36 | } 37 | 38 | 39 | static size_t sizeProto(Proto const* p) 40 | { 41 | return sizeof(Proto) + sizeof(Instruction) * p->sizecode + 42 | sizeof(Proto*) * p->sizep + 43 | sizeof(TValue) * p->sizek + 44 | sizeof(int) * p->sizelineinfo + 45 | sizeof(LocVar) * p->sizelocvars + 46 | sizeof(*(p->upvalues)) * p->sizeupvalues; 47 | } 48 | 49 | size_t sizeFunction_50103(void const* o, int count_protos, int count_upvalues, int count_values) 50 | { 51 | TValue const* v = o; 52 | Closure *cl = clvalue(v); 53 | (void)count_values; /* unused for Lua 5.1 */ 54 | if (cl->c.isC) 55 | return sizeCclosure(cl->c.nupvalues); 56 | else 57 | return sizeLclosure(cl->l.nupvalues) + 58 | (count_upvalues ? cl->l.nupvalues * sizeof(UpVal) : 0) + 59 | (count_protos ? sizeProto(cl->l.p) : 0); 60 | } 61 | 62 | 63 | void* tableNode_50103(void const* o) 64 | { 65 | TValue const* h = o; 66 | return hvalue(h)->node; 67 | } 68 | 69 | 70 | size_t sizeTable_50103(void const* o, void const* n, 71 | unsigned* narr, unsigned* nrec) 72 | { 73 | Table const* h = hvalue((TValue const*)o); 74 | Node const* dummynode = n; 75 | *narr = h->sizearray; 76 | *nrec = (h->node == dummynode ? 0 : sizenode(h)); 77 | return sizeof(Table) + sizeof(TValue) * *narr + 78 | sizeof(Node) * *nrec; 79 | } 80 | 81 | 82 | size_t sizeUserdata_50103(void const* o) 83 | { 84 | TValue const* v = o; 85 | return sizeudata(uvalue(v)); 86 | } 87 | 88 | 89 | size_t sizeThread_50103(void const* o) 90 | { 91 | lua_State const* th = thvalue((TValue const*)o); 92 | return sizeof(lua_State) + sizeof(TValue) * th->stacksize + 93 | sizeof(CallInfo) * th->size_ci; 94 | } 95 | 96 | #endif 97 | 98 | -------------------------------------------------------------------------------- /compat_50200.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #if LUA_VERSION_NUM == 502 5 | #define LUA_CORE 6 | #include "lua5.2/lstate.h" 7 | #include "lua5.2/lobject.h" 8 | #include "lua5.2/lfunc.h" 9 | #include "lua5.2/lstring.h" 10 | 11 | 12 | void* getArg_50200(lua_State* L, int n) 13 | { 14 | return L->ci->func+n; 15 | } 16 | 17 | 18 | size_t sizeBoolean_50200(void const* o) 19 | { 20 | (void)o; 21 | return sizeof(int); 22 | } 23 | 24 | 25 | size_t sizeNumber_50200(void const* o) 26 | { 27 | (void)o; 28 | return sizeof(lua_Number); 29 | } 30 | 31 | 32 | size_t sizeString_50200(void const* o) 33 | { 34 | TValue const* v = o; 35 | return sizestring(tsvalue(v)); 36 | } 37 | 38 | 39 | static size_t sizeProto(Proto const* p) 40 | { 41 | return sizeof(Proto) + sizeof(Instruction) * p->sizecode + 42 | sizeof(Proto*) * p->sizep + 43 | sizeof(TValue) * p->sizek + 44 | sizeof(int) * p->sizelineinfo + 45 | sizeof(LocVar) * p->sizelocvars + 46 | sizeof(*(p->upvalues)) * p->sizeupvalues; 47 | } 48 | 49 | size_t sizeFunction_50200(void const* o, int count_protos, int count_upvalues, int count_values) 50 | { 51 | TValue const* v = o; 52 | switch (ttype(v)) 53 | { 54 | case LUA_TLCL: /* Lua closure */ 55 | { 56 | Closure *cl = clvalue(v); 57 | return sizeLclosure(cl->l.nupvalues) + 58 | (count_upvalues ? cl->l.nupvalues * sizeof(UpVal) : 0) + 59 | (count_protos ? sizeProto(cl->l.p) : 0); 60 | } 61 | case LUA_TLCF: /* light C function */ 62 | return count_values ? sizeof(lua_CFunction) : 0; 63 | case LUA_TCCL: /* C closure */ 64 | return sizeCclosure(clvalue(v)->c.nupvalues); 65 | } 66 | return 0; 67 | } 68 | 69 | 70 | void* tableNode_50200(void const* o) 71 | { 72 | TValue const* h = o; 73 | return hvalue(h)->node; 74 | } 75 | 76 | 77 | size_t sizeTable_50200(void const* o, void const* n, 78 | unsigned* narr, unsigned* nrec) 79 | { 80 | Table const* h = hvalue((TValue const*)o); 81 | Node const* dummynode = n; 82 | *narr = h->sizearray; 83 | *nrec = (h->node == dummynode ? 0 : sizenode(h)); 84 | return sizeof(Table) + sizeof(TValue) * *narr + 85 | sizeof(Node) * *nrec; 86 | } 87 | 88 | 89 | size_t sizeUserdata_50200(void const* o) 90 | { 91 | TValue const* v = o; 92 | return sizeudata(uvalue(v)); 93 | } 94 | 95 | 96 | size_t sizeThread_50200(void const* o) 97 | { 98 | lua_State const* th = thvalue((TValue const*)o); 99 | CallInfo* ci = th->base_ci.next; 100 | size_t cisize = 0; 101 | for (; ci != NULL; ci = ci->next) 102 | cisize += sizeof(CallInfo); 103 | return sizeof(lua_State) + sizeof(TValue) * th->stacksize + cisize; 104 | } 105 | 106 | #endif 107 | 108 | -------------------------------------------------------------------------------- /compat_50204.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #if LUA_VERSION_NUM == 502 5 | #define LUA_CORE 6 | #include "lua5.2/4/lobject.h" 7 | #include "lua5.2/lstate.h" 8 | 9 | 10 | void* tableNode_50204(void const* o) 11 | { 12 | TValue const* h = o; 13 | return hvalue(h)->node; 14 | } 15 | 16 | 17 | size_t sizeTable_50204(void const* o, void const* n, 18 | unsigned* narr, unsigned* nrec) 19 | { 20 | Table const* h = hvalue((TValue const*)o); 21 | Node const* dummynode = n; 22 | *narr = h->sizearray; 23 | *nrec = (h->node == dummynode ? 0 : sizenode(h)); 24 | return sizeof(Table) + sizeof(TValue) * *narr + 25 | sizeof(Node) * *nrec; 26 | } 27 | 28 | #endif 29 | 30 | -------------------------------------------------------------------------------- /compat_50300.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #if LUA_VERSION_NUM == 503 5 | #define LUA_CORE 6 | #include "lua5.3/lstate.h" 7 | #include "lua5.3/lobject.h" 8 | #include "lua5.3/lfunc.h" 9 | #include "lua5.3/lstring.h" 10 | 11 | 12 | void* getArg_50300(lua_State* L, int n) 13 | { 14 | return L->ci->func+n; 15 | } 16 | 17 | 18 | size_t sizeBoolean_50300(void const* o) 19 | { 20 | (void)o; 21 | return sizeof(int); 22 | } 23 | 24 | 25 | size_t sizeNumber_50300(void const* o) 26 | { 27 | TValue const* v = o; 28 | if (ttype(v) == LUA_TNUMINT) 29 | return sizeof(lua_Integer); 30 | else 31 | return sizeof(lua_Number); 32 | } 33 | 34 | 35 | size_t sizeString_50300(void const* o) 36 | { 37 | TValue const* v = o; 38 | return sizestring(tsvalue(v)); 39 | } 40 | 41 | 42 | 43 | static size_t sizeProto(Proto const* p) 44 | { 45 | return sizeof(Proto) + sizeof(Instruction) * p->sizecode + 46 | sizeof(Proto*) * p->sizep + 47 | sizeof(TValue) * p->sizek + 48 | sizeof(int) * p->sizelineinfo + 49 | sizeof(LocVar) * p->sizelocvars + 50 | sizeof(*(p->upvalues)) * p->sizeupvalues; 51 | } 52 | 53 | 54 | size_t sizeFunction_50300(void const* o, int count_protos, int count_upvalues, int count_values) 55 | { 56 | TValue const* v = o; 57 | switch (ttype(v)) 58 | { 59 | case LUA_TLCL: /* Lua closure */ 60 | { 61 | Closure *cl = clvalue(v); 62 | return sizeLclosure(cl->l.nupvalues) + 63 | (count_upvalues ? cl->l.nupvalues * sizeof(UpVal) : 0) + 64 | (count_protos ? sizeProto(cl->l.p) : 0); 65 | } 66 | case LUA_TLCF: /* light C function */ 67 | return count_values ? sizeof(lua_CFunction) : 0; 68 | case LUA_TCCL: /* C closure */ 69 | return sizeCclosure(clvalue(v)->c.nupvalues); 70 | } 71 | return 0; 72 | } 73 | 74 | 75 | void* tableNode_50300(void const* o) 76 | { 77 | TValue const* h = o; 78 | return hvalue(h)->node; 79 | } 80 | 81 | 82 | size_t sizeTable_50300(void const* o, void const* n, 83 | unsigned* narr, unsigned* nrec) 84 | { 85 | Table const* h = hvalue((TValue const*)o); 86 | Node const* dummynode = n; 87 | *narr = h->sizearray; 88 | *nrec = (h->node == dummynode ? 0 : sizenode(h)); 89 | return sizeof(Table) + sizeof(TValue) * *narr + 90 | sizeof(Node) * *nrec; 91 | } 92 | 93 | 94 | size_t sizeUserdata_50300(void const* o) 95 | { 96 | TValue const* v = o; 97 | return sizeudata(uvalue(v)); 98 | } 99 | 100 | 101 | size_t sizeThread_50300(void const* o) 102 | { 103 | lua_State const* th = thvalue((TValue const*)o); 104 | CallInfo *ci = th->base_ci.next; 105 | size_t cisize = 0; 106 | for (; ci != NULL; ci = ci->next) 107 | cisize += sizeof(CallInfo); 108 | return sizeof(lua_State) + sizeof(TValue) * th->stacksize + cisize; 109 | } 110 | 111 | #endif 112 | 113 | -------------------------------------------------------------------------------- /compat_50301.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #if LUA_VERSION_NUM == 503 5 | #define LUA_CORE 6 | #include "lua5.3/1/lobject.h" 7 | #include "lua5.3/1/lstring.h" 8 | 9 | 10 | size_t sizeString_50301(void const* o) 11 | { 12 | TValue const* v = o; 13 | return sizelstring(tsslen(tsvalue(v))); 14 | } 15 | 16 | #endif 17 | 18 | -------------------------------------------------------------------------------- /compat_50302.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #if LUA_VERSION_NUM == 503 5 | #define LUA_CORE 6 | #include "lua5.3/2/lstate.h" 7 | 8 | 9 | size_t sizeThread_50302(void const* o) 10 | { 11 | lua_State const* th = thvalue((TValue const*)o); 12 | return sizeof(lua_State) + sizeof(TValue) * th->stacksize + 13 | sizeof(CallInfo) * th->nci; 14 | } 15 | 16 | #endif 17 | 18 | -------------------------------------------------------------------------------- /compat_50400.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #if LUA_VERSION_NUM == 504 5 | #define LUA_CORE 6 | #include "lua5.4/lstate.h" 7 | #include "lua5.4/lobject.h" 8 | #include "lua5.4/lfunc.h" 9 | #include "lua5.4/lstring.h" 10 | 11 | 12 | void* getArg_50400(lua_State* L, int n) 13 | { 14 | return L->ci->func+n; 15 | } 16 | 17 | 18 | size_t sizeBoolean_50400(void const* o) 19 | { 20 | (void)o; 21 | return 0; 22 | } 23 | 24 | 25 | size_t sizeNumber_50400(void const* o) 26 | { 27 | TValue const* v = o; 28 | if (ttypetag(v) == LUA_VNUMINT) 29 | return sizeof(lua_Integer); 30 | else 31 | return sizeof(lua_Number); 32 | } 33 | 34 | 35 | size_t sizeString_50400(void const* o) 36 | { 37 | TValue const* v = o; 38 | return sizelstring(vslen(v)); 39 | } 40 | 41 | 42 | static size_t sizeProto(Proto const* p) 43 | { 44 | return sizeof(Proto) + sizeof(Instruction) * p->sizecode + 45 | sizeof(Proto*) * p->sizep + 46 | sizeof(TValue) * p->sizek + 47 | sizeof(int) * p->sizelineinfo + 48 | sizeof(LocVar) * p->sizelocvars + 49 | sizeof(*(p->upvalues)) * p->sizeupvalues; 50 | } 51 | 52 | size_t sizeFunction_50400(void const* o, int count_protos, int count_upvalues, int count_values) 53 | { 54 | TValue const* v = o; 55 | switch (ttypetag(v)) 56 | { 57 | case LUA_VLCL: /* Lua closure */ 58 | { 59 | Closure *cl = clvalue(v); 60 | return sizeLclosure(cl->l.nupvalues) + 61 | (count_upvalues ? cl->l.nupvalues * sizeof(UpVal) : 0) + 62 | (count_protos ? sizeProto(cl->l.p) : 0); 63 | } 64 | case LUA_VLCF: /* light C function */ 65 | return count_values ? sizeof(lua_CFunction) : 0; 66 | case LUA_VCCL: /* C closure */ 67 | return sizeCclosure(clvalue(v)->c.nupvalues); 68 | } 69 | return 0; 70 | } 71 | 72 | 73 | void* tableNode_50400(void const* o) 74 | { 75 | TValue const* h = o; 76 | return hvalue(h)->node; 77 | } 78 | 79 | 80 | size_t sizeTable_50400(void const* o, void const* n, 81 | unsigned* narr, unsigned* nrec) 82 | { 83 | Table const* h = hvalue((TValue const*)o); 84 | Node const* dummynode = n; 85 | *narr = h->alimit; 86 | *nrec = (h->node == dummynode ? 0 : sizenode(h)); 87 | return sizeof(Table) + sizeof(TValue) * *narr + 88 | sizeof(Node) * *nrec; 89 | } 90 | 91 | 92 | size_t sizeUserdata_50400(void const* o) 93 | { 94 | TValue const* v = o; 95 | return sizeudata(uvalue(v)->nuvalue, uvalue(v)->len); 96 | } 97 | 98 | 99 | size_t sizeThread_50400(void const* o) 100 | { 101 | lua_State const* th = thvalue((TValue const*)o); 102 | CallInfo *ci = th->base_ci.next; 103 | size_t cisize = 0; 104 | for (; ci != NULL; ci = ci->next) 105 | cisize += sizeof(CallInfo); 106 | return sizeof(lua_State) + sizeof(TValue) * th->stacksize + cisize; 107 | } 108 | 109 | #endif 110 | 111 | -------------------------------------------------------------------------------- /compat_50401.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #if LUA_VERSION_NUM == 504 5 | #define LUA_CORE 6 | #include "lua5.4/1/lstate.h" 7 | 8 | 9 | size_t sizeThread_50401(void const* o) 10 | { 11 | lua_State const* th = thvalue((TValue const*)o); 12 | CallInfo *ci = th->base_ci.next; 13 | size_t cisize = 0; 14 | for (; ci != NULL; ci = ci->next) 15 | cisize += sizeof(CallInfo); 16 | return sizeof(lua_State) + sizeof(TValue) * th->stacksize + cisize; 17 | } 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /compat_50402.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #if LUA_VERSION_NUM == 504 5 | #define LUA_CORE 6 | #include "lua5.4/2/lstate.h" 7 | 8 | 9 | size_t sizeThread_50402(void const* o) 10 | { 11 | lua_State const* th = thvalue((TValue const*)o); 12 | CallInfo *ci = th->base_ci.next; 13 | size_t cisize = 0; 14 | for (; ci != NULL; ci = ci->next) 15 | cisize += sizeof(CallInfo); 16 | return sizeof(lua_State) + sizeof(TValue) * (stacksize(th) + EXTRA_STACK) + cisize; 17 | } 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /compat_50403.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #if LUA_VERSION_NUM == 504 5 | #define LUA_CORE 6 | #include "lua5.4/3/lstate.h" 7 | 8 | 9 | size_t sizeThread_50403(void const* o) 10 | { 11 | lua_State const* th = thvalue((TValue const*)o); 12 | CallInfo *ci = th->base_ci.next; 13 | size_t cisize = 0; 14 | for (; ci != NULL; ci = ci->next) 15 | cisize += sizeof(CallInfo); 16 | return sizeof(lua_State) + sizeof(TValue) * (stacksize(th) + EXTRA_STACK) + cisize; 17 | } 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /getsize-scm-0.rockspec: -------------------------------------------------------------------------------- 1 | package="getsize" 2 | version="scm-0" 3 | 4 | source = { 5 | url = "git://github.com/siffiejoe/lua-getsize.git", 6 | } 7 | 8 | description = { 9 | summary = "Calculates the size of a Lua object.", 10 | detailed = [[ 11 | Calculates the size of a Lua object by poking in the Lua 12 | internals. Works for PUC-Rio Lua 5.1, 5.2, 5.3, and 5.4, 13 | but _not_ for LuaJIT. 14 | ]], 15 | homepage = "http://code.matthewwild.co.uk/lua-getsize/", 16 | license = "MIT/X11, MIT", 17 | maintainer = "Philipp Janda ", 18 | } 19 | 20 | dependencies = { 21 | -- Uses Lua internals. Includes support for Lua 5.1, 5.2, 5.3, and 5.4. 22 | "lua >= 5.1, < 5.5" 23 | } 24 | 25 | build = { 26 | type = "builtin", 27 | modules = { 28 | getsize = { 29 | sources = { 30 | "getsize.c", 31 | "compat.c", 32 | "compat_50103.c", 33 | "compat_50200.c", 34 | "compat_50300.c", 35 | "compat_50400.c", 36 | "compat_50204.c", 37 | "compat_50301.c", 38 | "compat_50302.c", 39 | "compat_50401.c", 40 | "compat_50402.c", 41 | "compat_50403.c", 42 | } 43 | } 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /getsize.c: -------------------------------------------------------------------------------- 1 | /* lua-getsize 2 | Author: (C) 2009 Matthew Wild 3 | License: MIT/X11 license 4 | Description: Adds a debug.getsize() function which 5 | returns the size in bytes of a Lua object 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include "ljdetect/ljdetect.h" 12 | #include "compat.h" 13 | 14 | 15 | static int debug_getsize(lua_State* L) 16 | { 17 | GetSizeVTable const* const vtable = lua_touserdata(L, lua_upvalueindex(1)); 18 | /* TValue */ void const* const o = vtable->getArg(L, 1); 19 | size_t olen = 0; 20 | char const* options = luaL_optlstring(L, 2, "", &olen); 21 | int count_upvalues = 1; 22 | int count_protos = 0; 23 | int count_values = 1; 24 | size_t i = 0; 25 | for (i = 0; i < olen; ++i) 26 | { 27 | switch (options[i]) 28 | { 29 | case 'p': count_protos = 1; break; 30 | case 'P': count_protos = 0; break; 31 | case 'u': count_upvalues = 1; break; 32 | case 'U': count_upvalues = 0; break; 33 | case 'v': count_values = 1; break; 34 | case 'V': count_values = 0; break; 35 | default: 36 | luaL_error(L, "unknown option for 'getsize': %c", options[i]); 37 | break; 38 | } 39 | } 40 | switch (lua_type(L, 1)) 41 | { 42 | case LUA_TNIL: 43 | { 44 | lua_pushinteger(L, 0); 45 | return 1; 46 | } 47 | case LUA_TBOOLEAN: 48 | { 49 | lua_pushinteger(L, count_values ? vtable->sizeBoolean(o) : 0); 50 | return 1; 51 | } 52 | case LUA_TLIGHTUSERDATA: 53 | { 54 | lua_pushinteger(L, count_values ? sizeof(void*) : 0); 55 | return 1; 56 | } 57 | case LUA_TNUMBER: 58 | { 59 | lua_pushinteger(L, count_values ? vtable->sizeNumber(o) : 0); 60 | return 1; 61 | } 62 | case LUA_TSTRING: 63 | { 64 | lua_pushinteger(L, vtable->sizeString(o)); 65 | return 1; 66 | } 67 | case LUA_TTABLE: 68 | { 69 | /* Node */ void const* const dummynode = lua_touserdata(L, lua_upvalueindex(2)); 70 | unsigned narr = 0; 71 | unsigned nrec = 0; 72 | lua_pushinteger(L, vtable->sizeTable(o, dummynode, &narr, &nrec)); 73 | lua_pushinteger(L, narr); 74 | lua_pushinteger(L, nrec); 75 | return 3; 76 | } 77 | case LUA_TFUNCTION: 78 | { 79 | lua_pushinteger(L, vtable->sizeFunction(o, count_protos, count_upvalues, count_values)); 80 | return 1; 81 | } 82 | case LUA_TUSERDATA: 83 | { 84 | lua_pushinteger(L, vtable->sizeUserdata(o)); 85 | return 1; 86 | } 87 | case LUA_TTHREAD: 88 | { 89 | lua_pushinteger(L, vtable->sizeThread(o)); 90 | return 1; 91 | } 92 | } 93 | return 0; 94 | } 95 | 96 | 97 | int luaopen_getsize(lua_State* L) 98 | { 99 | GetSizeVTable const* vtable = NULL; 100 | if (isluajit(L)) 101 | luaL_error(L, "LuaJIT is not supported by getsize"); 102 | lua_settop(L, 0); 103 | lua_getglobal(L, "debug"); 104 | lua_createtable(L, 0, 0); /* to get dummynode pointer */ 105 | vtable = compat_init(L); 106 | lua_pushlightuserdata(L, vtable->tableNode(vtable->getArg(L, 2))); 107 | lua_pushcclosure(L, debug_getsize, 2); 108 | if (lua_type(L, 1) == LUA_TTABLE) 109 | { 110 | lua_pushvalue(L, -1); 111 | lua_setfield(L, 1, "getsize"); 112 | } 113 | return 1; 114 | } 115 | 116 | -------------------------------------------------------------------------------- /ljdetect/.gitignore: -------------------------------------------------------------------------------- 1 | # docco output directory 2 | #docs/ 3 | 4 | # local files for remembering stuff 5 | HISTO 6 | TODO 7 | 8 | # temporary files 9 | .*.swp 10 | 11 | # object files 12 | *.o 13 | *.obj 14 | 15 | # libraries 16 | *.so 17 | *.dll 18 | *.a 19 | *.lib 20 | *.exp 21 | 22 | # executables 23 | *.exe 24 | 25 | # precompiled Lua bytecode files 26 | *.luac 27 | 28 | # LuaRocks packages 29 | *.rock 30 | 31 | -------------------------------------------------------------------------------- /ljdetect/ljdetect.h: -------------------------------------------------------------------------------- 1 | #ifndef LJDETECT_H_ 2 | #define LJDETECT_H_ 3 | 4 | /* Small C header file that provides a helper function that can detect 5 | * LuaJIT at runtime. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | typedef struct { 15 | size_t written; 16 | char buffer[ 3 ]; 17 | } isluajit_state; 18 | 19 | 20 | static int isluajit_writer( lua_State* L, const void* p, 21 | size_t sz, void* ud ) { 22 | isluajit_state* s = (isluajit_state*)ud; 23 | (void)L; 24 | if( sz > 0 && s->written < sizeof( s->buffer ) ) { 25 | char const* cp = (char const*)p; 26 | if( sz > sizeof( s->buffer ) - s->written ) 27 | sz = sizeof( s->buffer ) - s->written; 28 | memcpy( s->buffer, cp, sz ); 29 | s->written += sz; 30 | return 0; 31 | } 32 | return s->written < sizeof( s->buffer ); 33 | } 34 | 35 | 36 | static int isluajit( lua_State* L ) { 37 | #if LUA_VERSION_NUM == 501 38 | isluajit_state st; 39 | /* create a Lua function */ 40 | if( luaL_loadstring( L, "" ) != 0 ) 41 | lua_error( L ); 42 | /* dump the Lua function as bytecode */ 43 | st.written = 0; 44 | lua_dump( L, isluajit_writer, &st ); 45 | /* remove Lua function from stack */ 46 | lua_pop( L, 1 ); 47 | /* inspect the bytecode header to detect the LuaJIT signature */ 48 | if( st.written < sizeof( st.buffer ) || 49 | 0 != memcmp( "\033LJ", st.buffer, sizeof( st.buffer ) ) ) 50 | return 0; 51 | else 52 | return 1; 53 | #else 54 | (void)L; 55 | (void)isluajit_writer; 56 | return 0; 57 | #endif 58 | } 59 | 60 | #endif /* LJDETECT_H_ */ 61 | 62 | -------------------------------------------------------------------------------- /ljdetect/mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "ljdetect.h" 4 | 5 | 6 | static int func( lua_State* L ) { 7 | if( isluajit( L ) ) { 8 | fprintf( stderr, "LuaJIT\n" ); 9 | } else { 10 | fprintf( stderr, "PUC-Rio Lua\n" ); 11 | } 12 | return 0; 13 | } 14 | 15 | 16 | int luaopen_mod( lua_State* L ) { 17 | lua_pushcfunction( L, func ); 18 | return 1; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /ljdetect/test.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | 3 | require( "mod" )() 4 | 5 | -------------------------------------------------------------------------------- /lua5.1/README: -------------------------------------------------------------------------------- 1 | Excerpt from MIT-licensed Lua 5.1.3 sources, required to build lua-getsize for Lua 5.1. 2 | http://www.lua.org/ 3 | -------------------------------------------------------------------------------- /lua5.1/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 | 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 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c); 29 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 30 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 31 | int pc); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /lua5.1/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 | /* 42 | ** Layout for bit use in `marked' field: 43 | ** bit 0 - object is white (type 0) 44 | ** bit 1 - object is white (type 1) 45 | ** bit 2 - object is black 46 | ** bit 3 - for userdata: has been finalized 47 | ** bit 3 - for tables: has weak keys 48 | ** bit 4 - for tables: has weak values 49 | ** bit 5 - object is fixed (should not be collected) 50 | ** bit 6 - object is "super" fixed (only the main thread) 51 | */ 52 | 53 | 54 | #define WHITE0BIT 0 55 | #define WHITE1BIT 1 56 | #define BLACKBIT 2 57 | #define FINALIZEDBIT 3 58 | #define KEYWEAKBIT 3 59 | #define VALUEWEAKBIT 4 60 | #define FIXEDBIT 5 61 | #define SFIXEDBIT 6 62 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 63 | 64 | 65 | #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) 66 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 67 | #define isgray(x) (!isblack(x) && !iswhite(x)) 68 | 69 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 70 | #define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) 71 | 72 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 73 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 74 | 75 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 76 | 77 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 78 | 79 | 80 | #define luaC_checkGC(L) { \ 81 | condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ 82 | if (G(L)->totalbytes >= G(L)->GCthreshold) \ 83 | luaC_step(L); } 84 | 85 | 86 | #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 87 | luaC_barrierf(L,obj2gco(p),gcvalue(v)); } 88 | 89 | #define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ 90 | luaC_barrierback(L,t); } 91 | 92 | #define luaC_objbarrier(L,p,o) \ 93 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 94 | luaC_barrierf(L,obj2gco(p),obj2gco(o)); } 95 | 96 | #define luaC_objbarriert(L,t,o) \ 97 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } 98 | 99 | LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all); 100 | LUAI_FUNC void luaC_callGCTM (lua_State *L); 101 | LUAI_FUNC void luaC_freeall (lua_State *L); 102 | LUAI_FUNC void luaC_step (lua_State *L); 103 | LUAI_FUNC void luaC_fullgc (lua_State *L); 104 | LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); 105 | LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); 106 | LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); 107 | LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); 108 | 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /lua5.1/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 | -------------------------------------------------------------------------------- /lua5.1/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 | -------------------------------------------------------------------------------- /lua5.1/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 | GCObject **hash; 40 | lu_int32 nuse; /* number of elements */ 41 | int size; 42 | } stringtable; 43 | 44 | 45 | /* 46 | ** informations about a call 47 | */ 48 | typedef struct CallInfo { 49 | StkId base; /* base for this function */ 50 | StkId func; /* function index in the stack */ 51 | StkId top; /* top for this function */ 52 | const Instruction *savedpc; 53 | int nresults; /* expected number of results from this function */ 54 | int tailcalls; /* number of tail calls lost under this entry */ 55 | } CallInfo; 56 | 57 | 58 | 59 | #define curr_func(L) (clvalue(L->ci->func)) 60 | #define ci_func(ci) (clvalue((ci)->func)) 61 | #define f_isLua(ci) (!ci_func(ci)->c.isC) 62 | #define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci)) 63 | 64 | 65 | /* 66 | ** `global state', shared by all threads of this state 67 | */ 68 | typedef struct global_State { 69 | stringtable strt; /* hash table for strings */ 70 | lua_Alloc frealloc; /* function to reallocate memory */ 71 | void *ud; /* auxiliary data to `frealloc' */ 72 | lu_byte currentwhite; 73 | lu_byte gcstate; /* state of garbage collector */ 74 | int sweepstrgc; /* position of sweep in `strt' */ 75 | GCObject *rootgc; /* list of all collectable objects */ 76 | GCObject **sweepgc; /* position of sweep in `rootgc' */ 77 | GCObject *gray; /* list of gray objects */ 78 | GCObject *grayagain; /* list of objects to be traversed atomically */ 79 | GCObject *weak; /* list of weak tables (to be cleared) */ 80 | GCObject *tmudata; /* last element of list of userdata to be GC */ 81 | Mbuffer buff; /* temporary buffer for string concatentation */ 82 | lu_mem GCthreshold; 83 | lu_mem totalbytes; /* number of bytes currently allocated */ 84 | lu_mem estimate; /* an estimate of number of bytes actually in use */ 85 | lu_mem gcdept; /* how much GC is `behind schedule' */ 86 | int gcpause; /* size of pause between successive GCs */ 87 | int gcstepmul; /* GC `granularity' */ 88 | lua_CFunction panic; /* to be called in unprotected errors */ 89 | TValue l_registry; 90 | struct lua_State *mainthread; 91 | UpVal uvhead; /* head of double-linked list of all open upvalues */ 92 | struct Table *mt[NUM_TAGS]; /* metatables for basic types */ 93 | TString *tmname[TM_N]; /* array with tag-method names */ 94 | } global_State; 95 | 96 | 97 | /* 98 | ** `per thread' state 99 | */ 100 | struct lua_State { 101 | CommonHeader; 102 | lu_byte status; 103 | StkId top; /* first free slot in the stack */ 104 | StkId base; /* base of current function */ 105 | global_State *l_G; 106 | CallInfo *ci; /* call info for current function */ 107 | const Instruction *savedpc; /* `savedpc' of current function */ 108 | StkId stack_last; /* last free slot in the stack */ 109 | StkId stack; /* stack base */ 110 | CallInfo *end_ci; /* points after end of ci array*/ 111 | CallInfo *base_ci; /* array of CallInfo's */ 112 | int stacksize; 113 | int size_ci; /* size of array `base_ci' */ 114 | unsigned short nCcalls; /* number of nested C calls */ 115 | unsigned short baseCcalls; /* nested C calls when resuming coroutine */ 116 | lu_byte hookmask; 117 | lu_byte allowhook; 118 | int basehookcount; 119 | int hookcount; 120 | lua_Hook hook; 121 | TValue l_gt; /* table of globals */ 122 | TValue env; /* temporary place for environments */ 123 | GCObject *openupval; /* list of open upvalues in this stack */ 124 | GCObject *gclist; 125 | struct lua_longjmp *errorJmp; /* current error recover point */ 126 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 127 | }; 128 | 129 | 130 | #define G(L) (L->l_G) 131 | 132 | 133 | /* 134 | ** Union of all collectable objects 135 | */ 136 | union GCObject { 137 | GCheader gch; 138 | union TString ts; 139 | union Udata u; 140 | union Closure cl; 141 | struct Table h; 142 | struct Proto p; 143 | struct UpVal uv; 144 | struct lua_State th; /* thread */ 145 | }; 146 | 147 | 148 | /* macros to convert a GCObject into a specific value */ 149 | #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) 150 | #define gco2ts(o) (&rawgco2ts(o)->tsv) 151 | #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 152 | #define gco2u(o) (&rawgco2u(o)->uv) 153 | #define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) 154 | #define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 155 | #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 156 | #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 157 | #define ngcotouv(o) \ 158 | check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 159 | #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 160 | 161 | /* macro to convert any Lua object into a GCObject */ 162 | #define obj2gco(v) (cast(GCObject *, (v))) 163 | 164 | 165 | LUAI_FUNC lua_State *luaE_newthread (lua_State *L); 166 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 167 | 168 | #endif 169 | 170 | -------------------------------------------------------------------------------- /lua5.1/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)+((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_newliteral(L, s) (luaS_newlstr(L, "" s, \ 22 | (sizeof(s)/sizeof(char))-1)) 23 | 24 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 25 | 26 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 27 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 28 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /lua5.1/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 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_EQ, /* last tag method with `fast' access */ 24 | TM_ADD, 25 | TM_SUB, 26 | TM_MUL, 27 | TM_DIV, 28 | TM_MOD, 29 | TM_POW, 30 | TM_UNM, 31 | TM_LEN, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | LUAI_DATA const char *const luaT_typenames[]; 47 | 48 | 49 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 50 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 51 | TMS event); 52 | LUAI_FUNC void luaT_init (lua_State *L); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /lua5.1/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 | 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_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 41 | (buff)->buffsize = size) 42 | 43 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 44 | 45 | 46 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 50 | LUAI_FUNC int luaZ_lookahead (ZIO *z); 51 | 52 | 53 | 54 | /* --------- Private Part ------------------ */ 55 | 56 | struct Zio { 57 | size_t n; /* bytes still unread */ 58 | const char *p; /* current position in buffer */ 59 | lua_Reader reader; 60 | void* data; /* additional data */ 61 | lua_State *L; /* Lua state (for reader) */ 62 | }; 63 | 64 | 65 | LUAI_FUNC int luaZ_fill (ZIO *z); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /lua5.2/4/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.103.1.1 2013/04/12 18:48:47 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 unsigned LUA_INT32 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 | #define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2)) 35 | 36 | 37 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 38 | 39 | /* 40 | ** conversion of pointer to integer 41 | ** this is for hashing only; there is no problem if the integer 42 | ** cannot hold the whole pointer value 43 | */ 44 | #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 45 | 46 | 47 | 48 | /* type to ensure maximum alignment */ 49 | #if !defined(LUAI_USER_ALIGNMENT_T) 50 | #define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } 51 | #endif 52 | 53 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 54 | 55 | 56 | /* result of a `usual argument conversion' over lua_Number */ 57 | typedef LUAI_UACNUMBER l_uacNumber; 58 | 59 | 60 | /* internal assertions for in-house debugging */ 61 | #if defined(lua_assert) 62 | #define check_exp(c,e) (lua_assert(c), (e)) 63 | /* to avoid problems with conditions too long */ 64 | #define lua_longassert(c) { if (!(c)) lua_assert(0); } 65 | #else 66 | #define lua_assert(c) ((void)0) 67 | #define check_exp(c,e) (e) 68 | #define lua_longassert(c) ((void)0) 69 | #endif 70 | 71 | /* 72 | ** assertion for checking API calls 73 | */ 74 | #if !defined(luai_apicheck) 75 | 76 | #if defined(LUA_USE_APICHECK) 77 | #include 78 | #define luai_apicheck(L,e) assert(e) 79 | #else 80 | #define luai_apicheck(L,e) lua_assert(e) 81 | #endif 82 | 83 | #endif 84 | 85 | #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) 86 | 87 | 88 | #if !defined(UNUSED) 89 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 90 | #endif 91 | 92 | 93 | #define cast(t, exp) ((t)(exp)) 94 | 95 | #define cast_byte(i) cast(lu_byte, (i)) 96 | #define cast_num(i) cast(lua_Number, (i)) 97 | #define cast_int(i) cast(int, (i)) 98 | #define cast_uchar(i) cast(unsigned char, (i)) 99 | 100 | 101 | /* 102 | ** non-return type 103 | */ 104 | #if defined(__GNUC__) 105 | #define l_noret void __attribute__((noreturn)) 106 | #elif defined(_MSC_VER) 107 | #define l_noret void __declspec(noreturn) 108 | #else 109 | #define l_noret void 110 | #endif 111 | 112 | 113 | 114 | /* 115 | ** maximum depth for nested C calls and syntactical nested non-terminals 116 | ** in a program. (Value must fit in an unsigned short int.) 117 | */ 118 | #if !defined(LUAI_MAXCCALLS) 119 | #define LUAI_MAXCCALLS 200 120 | #endif 121 | 122 | /* 123 | ** maximum number of upvalues in a closure (both C and Lua). (Value 124 | ** must fit in an unsigned char.) 125 | */ 126 | #define MAXUPVAL UCHAR_MAX 127 | 128 | 129 | /* 130 | ** type for virtual-machine instructions 131 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 132 | */ 133 | typedef lu_int32 Instruction; 134 | 135 | 136 | 137 | /* maximum stack for a Lua function */ 138 | #define MAXSTACK 250 139 | 140 | 141 | 142 | /* minimum size for the string table (must be power of 2) */ 143 | #if !defined(MINSTRTABSIZE) 144 | #define MINSTRTABSIZE 32 145 | #endif 146 | 147 | 148 | /* minimum size for string buffer */ 149 | #if !defined(LUA_MINBUFFER) 150 | #define LUA_MINBUFFER 32 151 | #endif 152 | 153 | 154 | #if !defined(lua_lock) 155 | #define lua_lock(L) ((void) 0) 156 | #define lua_unlock(L) ((void) 0) 157 | #endif 158 | 159 | #if !defined(luai_threadyield) 160 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 161 | #endif 162 | 163 | 164 | /* 165 | ** these macros allow user-specific actions on threads when you defined 166 | ** LUAI_EXTRASPACE and need to do something extra when a thread is 167 | ** created/deleted/resumed/yielded. 168 | */ 169 | #if !defined(luai_userstateopen) 170 | #define luai_userstateopen(L) ((void)L) 171 | #endif 172 | 173 | #if !defined(luai_userstateclose) 174 | #define luai_userstateclose(L) ((void)L) 175 | #endif 176 | 177 | #if !defined(luai_userstatethread) 178 | #define luai_userstatethread(L,L1) ((void)L) 179 | #endif 180 | 181 | #if !defined(luai_userstatefree) 182 | #define luai_userstatefree(L,L1) ((void)L) 183 | #endif 184 | 185 | #if !defined(luai_userstateresume) 186 | #define luai_userstateresume(L,n) ((void)L) 187 | #endif 188 | 189 | #if !defined(luai_userstateyield) 190 | #define luai_userstateyield(L,n) ((void)L) 191 | #endif 192 | 193 | /* 194 | ** lua_number2int is a macro to convert lua_Number to int. 195 | ** lua_number2integer is a macro to convert lua_Number to lua_Integer. 196 | ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. 197 | ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. 198 | ** luai_hashnum is a macro to hash a lua_Number value into an integer. 199 | ** The hash must be deterministic and give reasonable values for 200 | ** both small and large values (outside the range of integers). 201 | */ 202 | 203 | #if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */ 204 | /* trick with Microsoft assembler for X86 */ 205 | 206 | #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i} 207 | #define lua_number2integer(i,n) lua_number2int(i, n) 208 | #define lua_number2unsigned(i,n) \ 209 | {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;} 210 | 211 | 212 | #elif defined(LUA_IEEE754TRICK) /* }{ */ 213 | /* the next trick should work on any machine using IEEE754 with 214 | a 32-bit int type */ 215 | 216 | union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; 217 | 218 | #if !defined(LUA_IEEEENDIAN) /* { */ 219 | #define LUAI_EXTRAIEEE \ 220 | static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)}; 221 | #define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33) 222 | #else 223 | #define LUA_IEEEENDIANLOC LUA_IEEEENDIAN 224 | #define LUAI_EXTRAIEEE /* empty */ 225 | #endif /* } */ 226 | 227 | #define lua_number2int32(i,n,t) \ 228 | { LUAI_EXTRAIEEE \ 229 | volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ 230 | (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; } 231 | 232 | #define luai_hashnum(i,n) \ 233 | { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \ 234 | (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */ 235 | 236 | #define lua_number2int(i,n) lua_number2int32(i, n, int) 237 | #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned) 238 | 239 | /* the trick can be expanded to lua_Integer when it is a 32-bit value */ 240 | #if defined(LUA_IEEELL) 241 | #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer) 242 | #endif 243 | 244 | #endif /* } */ 245 | 246 | 247 | /* the following definitions always work, but may be slow */ 248 | 249 | #if !defined(lua_number2int) 250 | #define lua_number2int(i,n) ((i)=(int)(n)) 251 | #endif 252 | 253 | #if !defined(lua_number2integer) 254 | #define lua_number2integer(i,n) ((i)=(lua_Integer)(n)) 255 | #endif 256 | 257 | #if !defined(lua_number2unsigned) /* { */ 258 | /* the following definition assures proper modulo behavior */ 259 | #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT) 260 | #include 261 | #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1) 262 | #define lua_number2unsigned(i,n) \ 263 | ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED)) 264 | #else 265 | #define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n)) 266 | #endif 267 | #endif /* } */ 268 | 269 | 270 | #if !defined(lua_unsigned2number) 271 | /* on several machines, coercion from unsigned to double is slow, 272 | so it may be worth to avoid */ 273 | #define lua_unsigned2number(u) \ 274 | (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u)) 275 | #endif 276 | 277 | 278 | 279 | #if defined(ltable_c) && !defined(luai_hashnum) 280 | 281 | #include 282 | #include 283 | 284 | #define luai_hashnum(i,n) { int e; \ 285 | n = l_mathop(frexp)(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ 286 | lua_number2int(i, n); i += e; } 287 | 288 | #endif 289 | 290 | 291 | 292 | /* 293 | ** macro to control inclusion of some hard tests on stack reallocation 294 | */ 295 | #if !defined(HARDSTACKTESTS) 296 | #define condmovestack(L) ((void)0) 297 | #else 298 | /* realloc stack keeping its size */ 299 | #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) 300 | #endif 301 | 302 | #if !defined(HARDMEMTESTS) 303 | #define condchangemem(L) condmovestack(L) 304 | #else 305 | #define condchangemem(L) \ 306 | ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) 307 | #endif 308 | 309 | #endif 310 | -------------------------------------------------------------------------------- /lua5.2/README: -------------------------------------------------------------------------------- 1 | Excerpt from MIT-licensed Lua 5.2 sources, required to build lua-getsize for Lua 5.2. 2 | http://www.lua.org/ 3 | -------------------------------------------------------------------------------- /lua5.2/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.8 2012/05/08 13:53:33 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 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 29 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 30 | int pc); 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /lua5.2/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.56 2012/05/23 15:43: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 GCSsweepstring 2 42 | #define GCSsweepudata 3 43 | #define GCSsweep 4 44 | #define GCSpause 5 45 | 46 | 47 | #define issweepphase(g) \ 48 | (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep) 49 | 50 | #define isgenerational(g) ((g)->gckind == KGC_GEN) 51 | 52 | /* 53 | ** macro to tell when main invariant (white objects cannot point to black 54 | ** ones) must be kept. During a non-generational collection, the sweep 55 | ** phase may break the invariant, as objects turned white may point to 56 | ** still-black objects. The invariant is restored when sweep ends and 57 | ** all objects are white again. During a generational collection, the 58 | ** invariant must be kept all times. 59 | */ 60 | #define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic) 61 | 62 | 63 | /* 64 | ** some useful bit tricks 65 | */ 66 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 67 | #define setbits(x,m) ((x) |= (m)) 68 | #define testbits(x,m) ((x) & (m)) 69 | #define bitmask(b) (1<<(b)) 70 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 71 | #define l_setbit(x,b) setbits(x, bitmask(b)) 72 | #define resetbit(x,b) resetbits(x, bitmask(b)) 73 | #define testbit(x,b) testbits(x, bitmask(b)) 74 | 75 | 76 | /* Layout for bit use in `marked' field: */ 77 | #define WHITE0BIT 0 /* object is white (type 0) */ 78 | #define WHITE1BIT 1 /* object is white (type 1) */ 79 | #define BLACKBIT 2 /* object is black */ 80 | #define FINALIZEDBIT 3 /* object has been separated for finalization */ 81 | #define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */ 82 | #define FIXEDBIT 5 /* object is fixed (should not be collected) */ 83 | #define OLDBIT 6 /* object is old (only in generational mode) */ 84 | /* bit 7 is currently used by tests (luaL_checkmemory) */ 85 | 86 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 87 | 88 | 89 | #define iswhite(x) testbits((x)->gch.marked, WHITEBITS) 90 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 91 | #define isgray(x) /* neither white nor black */ \ 92 | (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT))) 93 | 94 | #define isold(x) testbit((x)->gch.marked, OLDBIT) 95 | 96 | /* MOVE OLD rule: whenever an object is moved to the beginning of 97 | a GC list, its old bit must be cleared */ 98 | #define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT) 99 | 100 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 101 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) 102 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked) 103 | 104 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 105 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 106 | 107 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 108 | 109 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 110 | 111 | 112 | #define luaC_condGC(L,c) \ 113 | {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} 114 | #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) 115 | 116 | 117 | #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 118 | luaC_barrier_(L,obj2gco(p),gcvalue(v)); } 119 | 120 | #define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 121 | luaC_barrierback_(L,p); } 122 | 123 | #define luaC_objbarrier(L,p,o) \ 124 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 125 | luaC_barrier_(L,obj2gco(p),obj2gco(o)); } 126 | 127 | #define luaC_objbarrierback(L,p,o) \ 128 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); } 129 | 130 | #define luaC_barrierproto(L,p,c) \ 131 | { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); } 132 | 133 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 134 | LUAI_FUNC void luaC_step (lua_State *L); 135 | LUAI_FUNC void luaC_forcestep (lua_State *L); 136 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 137 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 138 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, 139 | GCObject **list, int offset); 140 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 141 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); 142 | LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c); 143 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 144 | LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv); 145 | LUAI_FUNC void luaC_changemode (lua_State *L, int mode); 146 | 147 | #endif 148 | -------------------------------------------------------------------------------- /lua5.2/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.99 2012/05/28 20:32:28 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 unsigned LUA_INT32 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 | #define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2)) 35 | 36 | 37 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 38 | 39 | /* 40 | ** conversion of pointer to integer 41 | ** this is for hashing only; there is no problem if the integer 42 | ** cannot hold the whole pointer value 43 | */ 44 | #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 45 | 46 | 47 | 48 | /* type to ensure maximum alignment */ 49 | #if !defined(LUAI_USER_ALIGNMENT_T) 50 | #define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } 51 | #endif 52 | 53 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 54 | 55 | 56 | /* result of a `usual argument conversion' over lua_Number */ 57 | typedef LUAI_UACNUMBER l_uacNumber; 58 | 59 | 60 | /* internal assertions for in-house debugging */ 61 | #if defined(lua_assert) 62 | #define check_exp(c,e) (lua_assert(c), (e)) 63 | /* to avoid problems with conditions too long */ 64 | #define lua_longassert(c) { if (!(c)) lua_assert(0); } 65 | #else 66 | #define lua_assert(c) ((void)0) 67 | #define check_exp(c,e) (e) 68 | #define lua_longassert(c) ((void)0) 69 | #endif 70 | 71 | /* 72 | ** assertion for checking API calls 73 | */ 74 | #if !defined(luai_apicheck) 75 | 76 | #if defined(LUA_USE_APICHECK) 77 | #include 78 | #define luai_apicheck(L,e) assert(e) 79 | #else 80 | #define luai_apicheck(L,e) lua_assert(e) 81 | #endif 82 | 83 | #endif 84 | 85 | #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) 86 | 87 | 88 | #if !defined(UNUSED) 89 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 90 | #endif 91 | 92 | 93 | #define cast(t, exp) ((t)(exp)) 94 | 95 | #define cast_byte(i) cast(lu_byte, (i)) 96 | #define cast_num(i) cast(lua_Number, (i)) 97 | #define cast_int(i) cast(int, (i)) 98 | #define cast_uchar(i) cast(unsigned char, (i)) 99 | 100 | 101 | /* 102 | ** non-return type 103 | */ 104 | #if defined(__GNUC__) 105 | #define l_noret void __attribute__((noreturn)) 106 | #elif defined(_MSC_VER) 107 | #define l_noret void __declspec(noreturn) 108 | #else 109 | #define l_noret void 110 | #endif 111 | 112 | 113 | 114 | /* 115 | ** maximum depth for nested C calls and syntactical nested non-terminals 116 | ** in a program. (Value must fit in an unsigned short int.) 117 | */ 118 | #if !defined(LUAI_MAXCCALLS) 119 | #define LUAI_MAXCCALLS 200 120 | #endif 121 | 122 | /* 123 | ** maximum number of upvalues in a closure (both C and Lua). (Value 124 | ** must fit in an unsigned char.) 125 | */ 126 | #define MAXUPVAL UCHAR_MAX 127 | 128 | 129 | /* 130 | ** type for virtual-machine instructions 131 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 132 | */ 133 | typedef lu_int32 Instruction; 134 | 135 | 136 | 137 | /* maximum stack for a Lua function */ 138 | #define MAXSTACK 250 139 | 140 | 141 | 142 | /* minimum size for the string table (must be power of 2) */ 143 | #if !defined(MINSTRTABSIZE) 144 | #define MINSTRTABSIZE 32 145 | #endif 146 | 147 | 148 | /* minimum size for string buffer */ 149 | #if !defined(LUA_MINBUFFER) 150 | #define LUA_MINBUFFER 32 151 | #endif 152 | 153 | 154 | #if !defined(lua_lock) 155 | #define lua_lock(L) ((void) 0) 156 | #define lua_unlock(L) ((void) 0) 157 | #endif 158 | 159 | #if !defined(luai_threadyield) 160 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 161 | #endif 162 | 163 | 164 | /* 165 | ** these macros allow user-specific actions on threads when you defined 166 | ** LUAI_EXTRASPACE and need to do something extra when a thread is 167 | ** created/deleted/resumed/yielded. 168 | */ 169 | #if !defined(luai_userstateopen) 170 | #define luai_userstateopen(L) ((void)L) 171 | #endif 172 | 173 | #if !defined(luai_userstateclose) 174 | #define luai_userstateclose(L) ((void)L) 175 | #endif 176 | 177 | #if !defined(luai_userstatethread) 178 | #define luai_userstatethread(L,L1) ((void)L) 179 | #endif 180 | 181 | #if !defined(luai_userstatefree) 182 | #define luai_userstatefree(L,L1) ((void)L) 183 | #endif 184 | 185 | #if !defined(luai_userstateresume) 186 | #define luai_userstateresume(L,n) ((void)L) 187 | #endif 188 | 189 | #if !defined(luai_userstateyield) 190 | #define luai_userstateyield(L,n) ((void)L) 191 | #endif 192 | 193 | /* 194 | ** lua_number2int is a macro to convert lua_Number to int. 195 | ** lua_number2integer is a macro to convert lua_Number to lua_Integer. 196 | ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. 197 | ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. 198 | ** luai_hashnum is a macro to hash a lua_Number value into an integer. 199 | ** The hash must be deterministic and give reasonable values for 200 | ** both small and large values (outside the range of integers). 201 | */ 202 | 203 | #if defined(MS_ASMTRICK) /* { */ 204 | /* trick with Microsoft assembler for X86 */ 205 | 206 | #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i} 207 | #define lua_number2integer(i,n) lua_number2int(i, n) 208 | #define lua_number2unsigned(i,n) \ 209 | {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;} 210 | 211 | 212 | #elif defined(LUA_IEEE754TRICK) /* }{ */ 213 | /* the next trick should work on any machine using IEEE754 with 214 | a 32-bit int type */ 215 | 216 | union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; 217 | 218 | #if !defined(LUA_IEEEENDIAN) /* { */ 219 | #define LUAI_EXTRAIEEE \ 220 | static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)}; 221 | #define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33) 222 | #else 223 | #define LUA_IEEEENDIANLOC LUA_IEEEENDIAN 224 | #define LUAI_EXTRAIEEE /* empty */ 225 | #endif /* } */ 226 | 227 | #define lua_number2int32(i,n,t) \ 228 | { LUAI_EXTRAIEEE \ 229 | volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ 230 | (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; } 231 | 232 | #define luai_hashnum(i,n) \ 233 | { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \ 234 | (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */ 235 | 236 | #define lua_number2int(i,n) lua_number2int32(i, n, int) 237 | #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned) 238 | 239 | /* the trick can be expanded to lua_Integer when it is a 32-bit value */ 240 | #if defined(LUA_IEEELL) 241 | #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer) 242 | #endif 243 | 244 | #endif /* } */ 245 | 246 | 247 | /* the following definitions always work, but may be slow */ 248 | 249 | #if !defined(lua_number2int) 250 | #define lua_number2int(i,n) ((i)=(int)(n)) 251 | #endif 252 | 253 | #if !defined(lua_number2integer) 254 | #define lua_number2integer(i,n) ((i)=(lua_Integer)(n)) 255 | #endif 256 | 257 | #if !defined(lua_number2unsigned) /* { */ 258 | /* the following definition assures proper modulo behavior */ 259 | #if defined(LUA_NUMBER_DOUBLE) 260 | #include 261 | #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1) 262 | #define lua_number2unsigned(i,n) \ 263 | ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED)) 264 | #else 265 | #define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n)) 266 | #endif 267 | #endif /* } */ 268 | 269 | 270 | #if !defined(lua_unsigned2number) 271 | /* on several machines, coercion from unsigned to double is slow, 272 | so it may be worth to avoid */ 273 | #define lua_unsigned2number(u) \ 274 | (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u)) 275 | #endif 276 | 277 | 278 | 279 | #if defined(ltable_c) && !defined(luai_hashnum) 280 | 281 | #include 282 | #include 283 | 284 | #define luai_hashnum(i,n) { int e; \ 285 | n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ 286 | lua_number2int(i, n); i += e; } 287 | 288 | #endif 289 | 290 | 291 | 292 | /* 293 | ** macro to control inclusion of some hard tests on stack reallocation 294 | */ 295 | #if !defined(HARDSTACKTESTS) 296 | #define condmovestack(L) ((void)0) 297 | #else 298 | /* realloc stack keeping its size */ 299 | #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) 300 | #endif 301 | 302 | #if !defined(HARDMEMTESTS) 303 | #define condchangemem(L) condmovestack(L) 304 | #else 305 | #define condchangemem(L) \ 306 | ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) 307 | #endif 308 | 309 | #endif 310 | -------------------------------------------------------------------------------- /lua5.2/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.38 2011/12/02 13:26:54 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 | #define luaM_reallocv(L,b,on,n,e) \ 18 | ((cast(size_t, (n)+1) > MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 19 | (luaM_toobig(L), (void *)0) : \ 20 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 21 | 22 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 23 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 24 | #define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) 25 | 26 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 27 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 28 | #define luaM_newvector(L,n,t) \ 29 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 30 | 31 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 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 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 41 | 42 | /* not to be called directly */ 43 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 44 | size_t size); 45 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 46 | size_t size_elem, int limit, 47 | const char *what); 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /lua5.2/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.81 2012/06/08 15:14:04 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 | 19 | ** Some notes about garbage-collected objects: All objects in Lua must 20 | ** be kept somehow accessible until being freed. 21 | ** 22 | ** Lua keeps most objects linked in list g->allgc. The link uses field 23 | ** 'next' of the CommonHeader. 24 | ** 25 | ** Strings are kept in several lists headed by the array g->strt.hash. 26 | ** 27 | ** Open upvalues are not subject to independent garbage collection. They 28 | ** are collected together with their respective threads. Lua keeps a 29 | ** double-linked list with all open upvalues (g->uvhead) so that it can 30 | ** mark objects referred by them. (They are always gray, so they must 31 | ** be remarked in the atomic step. Usually their contents would be marked 32 | ** when traversing the respective threads, but the thread may already be 33 | ** dead, while the upvalue is still accessible through closures.) 34 | ** 35 | ** Objects with finalizers are kept in the list g->finobj. 36 | ** 37 | ** The list g->tobefnz links all objects being finalized. 38 | 39 | */ 40 | 41 | 42 | struct lua_longjmp; /* defined in ldo.c */ 43 | 44 | 45 | 46 | /* extra stack space to handle TM calls and some other extras */ 47 | #define EXTRA_STACK 5 48 | 49 | 50 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 51 | 52 | 53 | /* kinds of Garbage Collection */ 54 | #define KGC_NORMAL 0 55 | #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ 56 | #define KGC_GEN 2 /* generational collection */ 57 | 58 | 59 | typedef struct stringtable { 60 | GCObject **hash; 61 | lu_int32 nuse; /* number of elements */ 62 | int size; 63 | } stringtable; 64 | 65 | 66 | /* 67 | ** information about a call 68 | */ 69 | typedef struct CallInfo { 70 | StkId func; /* function index in the stack */ 71 | StkId top; /* top for this function */ 72 | struct CallInfo *previous, *next; /* dynamic call link */ 73 | short nresults; /* expected number of results from this function */ 74 | lu_byte callstatus; 75 | ptrdiff_t extra; 76 | union { 77 | struct { /* only for Lua functions */ 78 | StkId base; /* base for this function */ 79 | const Instruction *savedpc; 80 | } l; 81 | struct { /* only for C functions */ 82 | int ctx; /* context info. in case of yields */ 83 | lua_CFunction k; /* continuation in case of yields */ 84 | ptrdiff_t old_errfunc; 85 | lu_byte old_allowhook; 86 | lu_byte status; 87 | } c; 88 | } u; 89 | } CallInfo; 90 | 91 | 92 | /* 93 | ** Bits in CallInfo status 94 | */ 95 | #define CIST_LUA (1<<0) /* call is running a Lua function */ 96 | #define CIST_HOOKED (1<<1) /* call is running a debug hook */ 97 | #define CIST_REENTRY (1<<2) /* call is running on same invocation of 98 | luaV_execute of previous call */ 99 | #define CIST_YIELDED (1<<3) /* call reentered after suspension */ 100 | #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ 101 | #define CIST_STAT (1<<5) /* call has an error status (pcall) */ 102 | #define CIST_TAIL (1<<6) /* call was tail called */ 103 | #define CIST_HOOKYIELD (1<<7) /* last hook called yielded */ 104 | 105 | 106 | #define isLua(ci) ((ci)->callstatus & CIST_LUA) 107 | 108 | 109 | /* 110 | ** `global state', shared by all threads of this state 111 | */ 112 | typedef struct global_State { 113 | lua_Alloc frealloc; /* function to reallocate memory */ 114 | void *ud; /* auxiliary data to `frealloc' */ 115 | lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 116 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 117 | lu_mem GCmemtrav; /* memory traversed by the GC */ 118 | lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 119 | stringtable strt; /* hash table for strings */ 120 | TValue l_registry; 121 | unsigned int seed; /* randomized seed for hashes */ 122 | lu_byte currentwhite; 123 | lu_byte gcstate; /* state of garbage collector */ 124 | lu_byte gckind; /* kind of GC running */ 125 | lu_byte gcrunning; /* true if GC is running */ 126 | int sweepstrgc; /* position of sweep in `strt' */ 127 | GCObject *allgc; /* list of all collectable objects */ 128 | GCObject *finobj; /* list of collectable objects with finalizers */ 129 | GCObject **sweepgc; /* current position of sweep in list 'allgc' */ 130 | GCObject **sweepfin; /* current position of sweep in list 'finobj' */ 131 | GCObject *gray; /* list of gray objects */ 132 | GCObject *grayagain; /* list of objects to be traversed atomically */ 133 | GCObject *weak; /* list of tables with weak values */ 134 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 135 | GCObject *allweak; /* list of all-weak tables */ 136 | GCObject *tobefnz; /* list of userdata to be GC */ 137 | UpVal uvhead; /* head of double-linked list of all open upvalues */ 138 | Mbuffer buff; /* temporary buffer for string concatenation */ 139 | int gcpause; /* size of pause between successive GCs */ 140 | int gcmajorinc; /* how much to wait for a major GC (only in gen. mode) */ 141 | int gcstepmul; /* GC `granularity' */ 142 | lua_CFunction panic; /* to be called in unprotected errors */ 143 | struct lua_State *mainthread; 144 | const lua_Number *version; /* pointer to version number */ 145 | TString *memerrmsg; /* memory-error message */ 146 | TString *tmname[TM_N]; /* array with tag-method names */ 147 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 148 | } global_State; 149 | 150 | 151 | /* 152 | ** `per thread' state 153 | */ 154 | struct lua_State { 155 | CommonHeader; 156 | lu_byte status; 157 | StkId top; /* first free slot in the stack */ 158 | global_State *l_G; 159 | CallInfo *ci; /* call info for current function */ 160 | const Instruction *oldpc; /* last pc traced */ 161 | StkId stack_last; /* last free slot in the stack */ 162 | StkId stack; /* stack base */ 163 | int stacksize; 164 | unsigned short nny; /* number of non-yieldable calls in stack */ 165 | unsigned short nCcalls; /* number of nested C calls */ 166 | lu_byte hookmask; 167 | lu_byte allowhook; 168 | int basehookcount; 169 | int hookcount; 170 | lua_Hook hook; 171 | GCObject *openupval; /* list of open upvalues in this stack */ 172 | GCObject *gclist; 173 | struct lua_longjmp *errorJmp; /* current error recover point */ 174 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 175 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 176 | }; 177 | 178 | 179 | #define G(L) (L->l_G) 180 | 181 | 182 | /* 183 | ** Union of all collectable objects 184 | */ 185 | union GCObject { 186 | GCheader gch; /* common header */ 187 | union TString ts; 188 | union Udata u; 189 | union Closure cl; 190 | struct Table h; 191 | struct Proto p; 192 | struct UpVal uv; 193 | struct lua_State th; /* thread */ 194 | }; 195 | 196 | 197 | #define gch(o) (&(o)->gch) 198 | 199 | /* macros to convert a GCObject into a specific value */ 200 | #define rawgco2ts(o) \ 201 | check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts)) 202 | #define gco2ts(o) (&rawgco2ts(o)->tsv) 203 | #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 204 | #define gco2u(o) (&rawgco2u(o)->uv) 205 | #define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l)) 206 | #define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c)) 207 | #define gco2cl(o) \ 208 | check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl)) 209 | #define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 210 | #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 211 | #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 212 | #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 213 | 214 | /* macro to convert any Lua object into a GCObject */ 215 | #define obj2gco(v) (cast(GCObject *, (v))) 216 | 217 | 218 | /* actual number of total bytes allocated */ 219 | #define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) 220 | 221 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 222 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 223 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 224 | LUAI_FUNC void luaE_freeCI (lua_State *L); 225 | 226 | 227 | #endif 228 | 229 | -------------------------------------------------------------------------------- /lua5.2/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.49 2012/02/01 21:57:15 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 sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 16 | 17 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 18 | 19 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 20 | (sizeof(s)/sizeof(char))-1)) 21 | 22 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 23 | 24 | 25 | /* 26 | ** test whether a string is a reserved word 27 | */ 28 | #define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0) 29 | 30 | 31 | /* 32 | ** equality for short strings, which are always internalized 33 | */ 34 | #define eqshrstr(a,b) check_exp((a)->tsv.tt == LUA_TSHRSTR, (a) == (b)) 35 | 36 | 37 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC int luaS_eqstr (TString *a, TString *b); 40 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 41 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 42 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 43 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /lua5.2/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.11 2011/02/28 17:32:10 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 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 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_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | #define ttypename(x) luaT_typenames_[(x) + 1] 47 | #define objtypename(x) ttypename(ttypenv(x)) 48 | 49 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 50 | 51 | 52 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 53 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 54 | TMS event); 55 | LUAI_FUNC void luaT_init (lua_State *L); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /lua5.2/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.26 2011/07/15 12:48:03 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 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 36 | 37 | 38 | #define luaZ_resizebuffer(L, buff, size) \ 39 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 40 | (buff)->buffsize = size) 41 | 42 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 43 | 44 | 45 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 46 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 47 | void *data); 48 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 49 | 50 | 51 | 52 | /* --------- Private Part ------------------ */ 53 | 54 | struct Zio { 55 | size_t n; /* bytes still unread */ 56 | const char *p; /* current position in buffer */ 57 | lua_Reader reader; /* reader function */ 58 | void* data; /* additional data */ 59 | lua_State *L; /* Lua state (for reader) */ 60 | }; 61 | 62 | 63 | LUAI_FUNC int luaZ_fill (ZIO *z); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /lua5.3/1/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.86 2014/10/25 11:50:46 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 | #define luaC_condGC(L,c) \ 105 | {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} 106 | #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) 107 | 108 | 109 | #define luaC_barrier(L,p,v) { \ 110 | if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ 111 | luaC_barrier_(L,obj2gco(p),gcvalue(v)); } 112 | 113 | #define luaC_barrierback(L,p,v) { \ 114 | if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ 115 | luaC_barrierback_(L,p); } 116 | 117 | #define luaC_objbarrier(L,p,o) { \ 118 | if (isblack(p) && iswhite(o)) \ 119 | luaC_barrier_(L,obj2gco(p),obj2gco(o)); } 120 | 121 | #define luaC_upvalbarrier(L,uv) \ 122 | { if (iscollectable((uv)->v) && !upisopen(uv)) \ 123 | luaC_upvalbarrier_(L,uv); } 124 | 125 | LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); 126 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 127 | LUAI_FUNC void luaC_step (lua_State *L); 128 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 129 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 130 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); 131 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 132 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); 133 | LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); 134 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 135 | LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); 136 | 137 | 138 | #endif 139 | -------------------------------------------------------------------------------- /lua5.3/1/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.135 2015/06/09 14:21:00 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 | ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count 19 | ** the total memory used by Lua (in bytes). Usually, 'size_t' and 20 | ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. 21 | */ 22 | #if defined(LUAI_MEM) /* { external definitions? */ 23 | typedef LUAI_UMEM lu_mem; 24 | typedef LUAI_MEM l_mem; 25 | #elif LUAI_BITSINT >= 32 /* }{ */ 26 | typedef size_t lu_mem; 27 | typedef ptrdiff_t l_mem; 28 | #else /* 16-bit ints */ /* }{ */ 29 | typedef unsigned long lu_mem; 30 | typedef long l_mem; 31 | #endif /* } */ 32 | 33 | 34 | /* chars used as small naturals (so that 'char' is reserved for characters) */ 35 | typedef unsigned char lu_byte; 36 | 37 | 38 | /* maximum value for size_t */ 39 | #define MAX_SIZET ((size_t)(~(size_t)0)) 40 | 41 | /* maximum size visible for Lua (must be representable in a lua_Integer */ 42 | #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ 43 | : (size_t)(LUA_MAXINTEGER)) 44 | 45 | 46 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)) 47 | 48 | #define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1)) 49 | 50 | 51 | #define MAX_INT INT_MAX /* maximum value of an int */ 52 | 53 | 54 | /* 55 | ** conversion of pointer to unsigned integer: 56 | ** this is for hashing only; there is no problem if the integer 57 | ** cannot hold the whole pointer value 58 | */ 59 | #define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX)) 60 | 61 | 62 | 63 | /* type to ensure maximum alignment */ 64 | #if defined(LUAI_USER_ALIGNMENT_T) 65 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 66 | #else 67 | typedef union { double u; void *s; lua_Integer i; long l; } L_Umaxalign; 68 | #endif 69 | 70 | 71 | 72 | /* types of 'usual argument conversions' for lua_Number and lua_Integer */ 73 | typedef LUAI_UACNUMBER l_uacNumber; 74 | typedef LUAI_UACINT l_uacInt; 75 | 76 | 77 | /* internal assertions for in-house debugging */ 78 | #if defined(lua_assert) 79 | #define check_exp(c,e) (lua_assert(c), (e)) 80 | /* to avoid problems with conditions too long */ 81 | #define lua_longassert(c) { if (!(c)) lua_assert(0); } 82 | #else 83 | #define lua_assert(c) ((void)0) 84 | #define check_exp(c,e) (e) 85 | #define lua_longassert(c) ((void)0) 86 | #endif 87 | 88 | /* 89 | ** assertion for checking API calls 90 | */ 91 | #if !defined(luai_apicheck) 92 | #define luai_apicheck(l,e) lua_assert(e) 93 | #endif 94 | 95 | #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) 96 | 97 | 98 | /* macro to avoid warnings about unused variables */ 99 | #if !defined(UNUSED) 100 | #define UNUSED(x) ((void)(x)) 101 | #endif 102 | 103 | 104 | /* type casts (a macro highlights casts in the code) */ 105 | #define cast(t, exp) ((t)(exp)) 106 | 107 | #define cast_void(i) cast(void, (i)) 108 | #define cast_byte(i) cast(lu_byte, (i)) 109 | #define cast_num(i) cast(lua_Number, (i)) 110 | #define cast_int(i) cast(int, (i)) 111 | #define cast_uchar(i) cast(unsigned char, (i)) 112 | 113 | 114 | /* cast a signed lua_Integer to lua_Unsigned */ 115 | #if !defined(l_castS2U) 116 | #define l_castS2U(i) ((lua_Unsigned)(i)) 117 | #endif 118 | 119 | /* 120 | ** cast a lua_Unsigned to a signed lua_Integer; this cast is 121 | ** not strict ISO C, but two-complement architectures should 122 | ** work fine. 123 | */ 124 | #if !defined(l_castU2S) 125 | #define l_castU2S(i) ((lua_Integer)(i)) 126 | #endif 127 | 128 | 129 | /* 130 | ** non-return type 131 | */ 132 | #if defined(__GNUC__) 133 | #define l_noret void __attribute__((noreturn)) 134 | #elif defined(_MSC_VER) && _MSC_VER >= 1200 135 | #define l_noret void __declspec(noreturn) 136 | #else 137 | #define l_noret void 138 | #endif 139 | 140 | 141 | 142 | /* 143 | ** maximum depth for nested C calls and syntactical nested non-terminals 144 | ** in a program. (Value must fit in an unsigned short int.) 145 | */ 146 | #if !defined(LUAI_MAXCCALLS) 147 | #define LUAI_MAXCCALLS 200 148 | #endif 149 | 150 | 151 | 152 | /* 153 | ** type for virtual-machine instructions; 154 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 155 | */ 156 | #if LUAI_BITSINT >= 32 157 | typedef unsigned int Instruction; 158 | #else 159 | typedef unsigned long Instruction; 160 | #endif 161 | 162 | 163 | 164 | /* 165 | ** Maximum length for short strings, that is, strings that are 166 | ** internalized. (Cannot be smaller than reserved words or tags for 167 | ** metamethods, as these strings must be internalized; 168 | ** #("function") = 8, #("__newindex") = 10.) 169 | */ 170 | #if !defined(LUAI_MAXSHORTLEN) 171 | #define LUAI_MAXSHORTLEN 40 172 | #endif 173 | 174 | 175 | /* 176 | ** Initial size for the string table (must be power of 2). 177 | ** The Lua core alone registers ~50 strings (reserved words + 178 | ** metaevent keys + a few others). Libraries would typically add 179 | ** a few dozens more. 180 | */ 181 | #if !defined(MINSTRTABSIZE) 182 | #define MINSTRTABSIZE 128 183 | #endif 184 | 185 | 186 | /* 187 | ** Size of cache for strings in the API (better be a prime) 188 | */ 189 | #if !defined(STRCACHE_SIZE) 190 | #define STRCACHE_SIZE 127 191 | #endif 192 | 193 | 194 | /* minimum size for string buffer */ 195 | #if !defined(LUA_MINBUFFER) 196 | #define LUA_MINBUFFER 32 197 | #endif 198 | 199 | 200 | /* 201 | ** macros that are executed whenether program enters the Lua core 202 | ** ('lua_lock') and leaves the core ('lua_unlock') 203 | */ 204 | #if !defined(lua_lock) 205 | #define lua_lock(L) ((void) 0) 206 | #define lua_unlock(L) ((void) 0) 207 | #endif 208 | 209 | /* 210 | ** macro executed during Lua functions at points where the 211 | ** function can yield. 212 | */ 213 | #if !defined(luai_threadyield) 214 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 215 | #endif 216 | 217 | 218 | /* 219 | ** these macros allow user-specific actions on threads when you defined 220 | ** LUAI_EXTRASPACE and need to do something extra when a thread is 221 | ** created/deleted/resumed/yielded. 222 | */ 223 | #if !defined(luai_userstateopen) 224 | #define luai_userstateopen(L) ((void)L) 225 | #endif 226 | 227 | #if !defined(luai_userstateclose) 228 | #define luai_userstateclose(L) ((void)L) 229 | #endif 230 | 231 | #if !defined(luai_userstatethread) 232 | #define luai_userstatethread(L,L1) ((void)L) 233 | #endif 234 | 235 | #if !defined(luai_userstatefree) 236 | #define luai_userstatefree(L,L1) ((void)L) 237 | #endif 238 | 239 | #if !defined(luai_userstateresume) 240 | #define luai_userstateresume(L,n) ((void)L) 241 | #endif 242 | 243 | #if !defined(luai_userstateyield) 244 | #define luai_userstateyield(L,n) ((void)L) 245 | #endif 246 | 247 | 248 | 249 | /* 250 | ** The luai_num* macros define the primitive operations over numbers. 251 | */ 252 | 253 | /* floor division (defined as 'floor(a/b)') */ 254 | #if !defined(luai_numidiv) 255 | #define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b))) 256 | #endif 257 | 258 | /* float division */ 259 | #if !defined(luai_numdiv) 260 | #define luai_numdiv(L,a,b) ((a)/(b)) 261 | #endif 262 | 263 | /* 264 | ** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when 265 | ** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of 266 | ** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b) 267 | ** ~= floor(a/b)'. That happens when the division has a non-integer 268 | ** negative result, which is equivalent to the test below. 269 | */ 270 | #if !defined(luai_nummod) 271 | #define luai_nummod(L,a,b,m) \ 272 | { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); } 273 | #endif 274 | 275 | /* exponentiation */ 276 | #if !defined(luai_numpow) 277 | #define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) 278 | #endif 279 | 280 | /* the others are quite standard operations */ 281 | #if !defined(luai_numadd) 282 | #define luai_numadd(L,a,b) ((a)+(b)) 283 | #define luai_numsub(L,a,b) ((a)-(b)) 284 | #define luai_nummul(L,a,b) ((a)*(b)) 285 | #define luai_numunm(L,a) (-(a)) 286 | #define luai_numeq(a,b) ((a)==(b)) 287 | #define luai_numlt(a,b) ((a)<(b)) 288 | #define luai_numle(a,b) ((a)<=(b)) 289 | #define luai_numisnan(a) (!luai_numeq((a), (a))) 290 | #endif 291 | 292 | 293 | 294 | 295 | 296 | /* 297 | ** macro to control inclusion of some hard tests on stack reallocation 298 | */ 299 | #if !defined(HARDSTACKTESTS) 300 | #define condmovestack(L) ((void)0) 301 | #else 302 | /* realloc stack keeping its size */ 303 | #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) 304 | #endif 305 | 306 | #if !defined(HARDMEMTESTS) 307 | #define condchangemem(L) condmovestack(L) 308 | #else 309 | #define condchangemem(L) \ 310 | ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) 311 | #endif 312 | 313 | #endif 314 | -------------------------------------------------------------------------------- /lua5.3/1/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 | -------------------------------------------------------------------------------- /lua5.3/1/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.122 2015/06/01 16:34:37 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 | 19 | ** Some notes about garbage-collected objects: All objects in Lua must 20 | ** be kept somehow accessible until being freed, so all objects always 21 | ** belong to one (and only one) of these lists, using field 'next' of 22 | ** the 'CommonHeader' for the link: 23 | ** 24 | ** 'allgc': all objects not marked for finalization; 25 | ** 'finobj': all objects marked for finalization; 26 | ** 'tobefnz': all objects ready to be finalized; 27 | ** 'fixedgc': all objects that are not to be collected (currently 28 | ** only small strings, such as reserved words). 29 | 30 | */ 31 | 32 | 33 | struct lua_longjmp; /* defined in ldo.c */ 34 | 35 | 36 | 37 | /* extra stack space to handle TM calls and some other extras */ 38 | #define EXTRA_STACK 5 39 | 40 | 41 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 42 | 43 | 44 | /* kinds of Garbage Collection */ 45 | #define KGC_NORMAL 0 46 | #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ 47 | 48 | 49 | typedef struct stringtable { 50 | TString **hash; 51 | int nuse; /* number of elements */ 52 | int size; 53 | } stringtable; 54 | 55 | 56 | /* 57 | ** Information about a call. 58 | ** When a thread yields, 'func' is adjusted to pretend that the 59 | ** top function has only the yielded values in its stack; in that 60 | ** case, the actual 'func' value is saved in field 'extra'. 61 | ** When a function calls another with a continuation, 'extra' keeps 62 | ** the function index so that, in case of errors, the continuation 63 | ** function can be called with the correct top. 64 | */ 65 | typedef struct CallInfo { 66 | StkId func; /* function index in the stack */ 67 | StkId top; /* top for this function */ 68 | struct CallInfo *previous, *next; /* dynamic call link */ 69 | union { 70 | struct { /* only for Lua functions */ 71 | StkId base; /* base for this function */ 72 | const Instruction *savedpc; 73 | } l; 74 | struct { /* only for C functions */ 75 | lua_KFunction k; /* continuation in case of yields */ 76 | ptrdiff_t old_errfunc; 77 | lua_KContext ctx; /* context info. in case of yields */ 78 | } c; 79 | } u; 80 | ptrdiff_t extra; 81 | short nresults; /* expected number of results from this function */ 82 | lu_byte callstatus; 83 | } CallInfo; 84 | 85 | 86 | /* 87 | ** Bits in CallInfo status 88 | */ 89 | #define CIST_OAH (1<<0) /* original value of 'allowhook' */ 90 | #define CIST_LUA (1<<1) /* call is running a Lua function */ 91 | #define CIST_HOOKED (1<<2) /* call is running a debug hook */ 92 | #define CIST_REENTRY (1<<3) /* call is running on same invocation of 93 | luaV_execute of previous call */ 94 | #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ 95 | #define CIST_TAIL (1<<5) /* call was tail called */ 96 | #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ 97 | #define CIST_LEQ (1<<7) /* using __lt for __le */ 98 | 99 | #define isLua(ci) ((ci)->callstatus & CIST_LUA) 100 | 101 | /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ 102 | #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) 103 | #define getoah(st) ((st) & CIST_OAH) 104 | 105 | 106 | /* 107 | ** 'global state', shared by all threads of this state 108 | */ 109 | typedef struct global_State { 110 | lua_Alloc frealloc; /* function to reallocate memory */ 111 | void *ud; /* auxiliary data to 'frealloc' */ 112 | lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 113 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 114 | lu_mem GCmemtrav; /* memory traversed by the GC */ 115 | lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 116 | stringtable strt; /* hash table for strings */ 117 | TValue l_registry; 118 | unsigned int seed; /* randomized seed for hashes */ 119 | lu_byte currentwhite; 120 | lu_byte gcstate; /* state of garbage collector */ 121 | lu_byte gckind; /* kind of GC running */ 122 | lu_byte gcrunning; /* true if GC is running */ 123 | GCObject *allgc; /* list of all collectable objects */ 124 | GCObject **sweepgc; /* current position of sweep in list */ 125 | GCObject *finobj; /* list of collectable objects with finalizers */ 126 | GCObject *gray; /* list of gray objects */ 127 | GCObject *grayagain; /* list of objects to be traversed atomically */ 128 | GCObject *weak; /* list of tables with weak values */ 129 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 130 | GCObject *allweak; /* list of all-weak tables */ 131 | GCObject *tobefnz; /* list of userdata to be GC */ 132 | GCObject *fixedgc; /* list of objects not to be collected */ 133 | struct lua_State *twups; /* list of threads with open upvalues */ 134 | Mbuffer buff; /* temporary buffer for string concatenation */ 135 | unsigned int gcfinnum; /* number of finalizers to call in each GC step */ 136 | int gcpause; /* size of pause between successive GCs */ 137 | int gcstepmul; /* GC 'granularity' */ 138 | lua_CFunction panic; /* to be called in unprotected errors */ 139 | struct lua_State *mainthread; 140 | const lua_Number *version; /* pointer to version number */ 141 | TString *memerrmsg; /* memory-error message */ 142 | TString *tmname[TM_N]; /* array with tag-method names */ 143 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 144 | TString *strcache[STRCACHE_SIZE][1]; /* cache for strings in API */ 145 | } global_State; 146 | 147 | 148 | /* 149 | ** 'per thread' state 150 | */ 151 | struct lua_State { 152 | CommonHeader; 153 | lu_byte status; 154 | StkId top; /* first free slot in the stack */ 155 | global_State *l_G; 156 | CallInfo *ci; /* call info for current function */ 157 | const Instruction *oldpc; /* last pc traced */ 158 | StkId stack_last; /* last free slot in the stack */ 159 | StkId stack; /* stack base */ 160 | UpVal *openupval; /* list of open upvalues in this stack */ 161 | GCObject *gclist; 162 | struct lua_State *twups; /* list of threads with open upvalues */ 163 | struct lua_longjmp *errorJmp; /* current error recover point */ 164 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 165 | lua_Hook hook; 166 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 167 | int stacksize; 168 | int basehookcount; 169 | int hookcount; 170 | unsigned short nny; /* number of non-yieldable calls in stack */ 171 | unsigned short nCcalls; /* number of nested C calls */ 172 | lu_byte hookmask; 173 | lu_byte allowhook; 174 | }; 175 | 176 | 177 | #define G(L) (L->l_G) 178 | 179 | 180 | /* 181 | ** Union of all collectable objects (only for conversions) 182 | */ 183 | union GCUnion { 184 | GCObject gc; /* common header */ 185 | struct TString ts; 186 | struct Udata u; 187 | union Closure cl; 188 | struct Table h; 189 | struct Proto p; 190 | struct lua_State th; /* thread */ 191 | }; 192 | 193 | 194 | #define cast_u(o) cast(union GCUnion *, (o)) 195 | 196 | /* macros to convert a GCObject into a specific value */ 197 | #define gco2ts(o) \ 198 | check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) 199 | #define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u)) 200 | #define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l)) 201 | #define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c)) 202 | #define gco2cl(o) \ 203 | check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) 204 | #define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) 205 | #define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) 206 | #define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) 207 | 208 | 209 | /* macro to convert a Lua object into a GCObject */ 210 | #define obj2gco(v) \ 211 | check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc))) 212 | 213 | 214 | /* actual number of total bytes allocated */ 215 | #define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) 216 | 217 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 218 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 219 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 220 | LUAI_FUNC void luaE_freeCI (lua_State *L); 221 | LUAI_FUNC void luaE_shrinkCI (lua_State *L); 222 | 223 | 224 | #endif 225 | 226 | -------------------------------------------------------------------------------- /lua5.3/1/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.59 2015/03/25 13:42:19 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 int luaS_eqlngstr (TString *a, TString *b); 38 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 39 | LUAI_FUNC void luaS_clearcache (global_State *g); 40 | LUAI_FUNC void luaS_init (lua_State *L); 41 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); 42 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); 43 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 44 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 45 | 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /lua5.3/1/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.21 2014/10/25 11:50:46 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 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_N /* number of elements in the enum */ 44 | } TMS; 45 | 46 | 47 | 48 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 49 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 50 | 51 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 52 | 53 | #define ttypename(x) luaT_typenames_[(x) + 1] 54 | #define objtypename(x) ttypename(ttnov(x)) 55 | 56 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 57 | 58 | 59 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 60 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 61 | TMS event); 62 | LUAI_FUNC void luaT_init (lua_State *L); 63 | 64 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 65 | const TValue *p2, TValue *p3, int hasres); 66 | LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 67 | StkId res, TMS event); 68 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 69 | StkId res, TMS event); 70 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 71 | const TValue *p2, TMS event); 72 | 73 | 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /lua5.3/1/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.30 2014/12/19 17:26:14 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 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 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 | size_t n; /* bytes still unread */ 58 | const char *p; /* current position in buffer */ 59 | lua_Reader reader; /* reader function */ 60 | void *data; /* additional data */ 61 | lua_State *L; /* Lua state (for reader) */ 62 | }; 63 | 64 | 65 | LUAI_FUNC int luaZ_fill (ZIO *z); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /lua5.3/2/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 | -------------------------------------------------------------------------------- /lua5.3/2/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.128 2015/11/13 12:16:51 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 | 19 | ** Some notes about garbage-collected objects: All objects in Lua must 20 | ** be kept somehow accessible until being freed, so all objects always 21 | ** belong to one (and only one) of these lists, using field 'next' of 22 | ** the 'CommonHeader' for the link: 23 | ** 24 | ** 'allgc': all objects not marked for finalization; 25 | ** 'finobj': all objects marked for finalization; 26 | ** 'tobefnz': all objects ready to be finalized; 27 | ** 'fixedgc': all objects that are not to be collected (currently 28 | ** only small strings, such as reserved words). 29 | 30 | */ 31 | 32 | 33 | struct lua_longjmp; /* defined in ldo.c */ 34 | 35 | 36 | 37 | /* extra stack space to handle TM calls and some other extras */ 38 | #define EXTRA_STACK 5 39 | 40 | 41 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 42 | 43 | 44 | /* kinds of Garbage Collection */ 45 | #define KGC_NORMAL 0 46 | #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ 47 | 48 | 49 | typedef struct stringtable { 50 | TString **hash; 51 | int nuse; /* number of elements */ 52 | int size; 53 | } stringtable; 54 | 55 | 56 | /* 57 | ** Information about a call. 58 | ** When a thread yields, 'func' is adjusted to pretend that the 59 | ** top function has only the yielded values in its stack; in that 60 | ** case, the actual 'func' value is saved in field 'extra'. 61 | ** When a function calls another with a continuation, 'extra' keeps 62 | ** the function index so that, in case of errors, the continuation 63 | ** function can be called with the correct top. 64 | */ 65 | typedef struct CallInfo { 66 | StkId func; /* function index in the stack */ 67 | StkId top; /* top for this function */ 68 | struct CallInfo *previous, *next; /* dynamic call link */ 69 | union { 70 | struct { /* only for Lua functions */ 71 | StkId base; /* base for this function */ 72 | const Instruction *savedpc; 73 | } l; 74 | struct { /* only for C functions */ 75 | lua_KFunction k; /* continuation in case of yields */ 76 | ptrdiff_t old_errfunc; 77 | lua_KContext ctx; /* context info. in case of yields */ 78 | } c; 79 | } u; 80 | ptrdiff_t extra; 81 | short nresults; /* expected number of results from this function */ 82 | lu_byte callstatus; 83 | } CallInfo; 84 | 85 | 86 | /* 87 | ** Bits in CallInfo status 88 | */ 89 | #define CIST_OAH (1<<0) /* original value of 'allowhook' */ 90 | #define CIST_LUA (1<<1) /* call is running a Lua function */ 91 | #define CIST_HOOKED (1<<2) /* call is running a debug hook */ 92 | #define CIST_FRESH (1<<3) /* call is running on a fresh invocation 93 | of luaV_execute */ 94 | #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ 95 | #define CIST_TAIL (1<<5) /* call was tail called */ 96 | #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ 97 | #define CIST_LEQ (1<<7) /* using __lt for __le */ 98 | 99 | #define isLua(ci) ((ci)->callstatus & CIST_LUA) 100 | 101 | /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ 102 | #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) 103 | #define getoah(st) ((st) & CIST_OAH) 104 | 105 | 106 | /* 107 | ** 'global state', shared by all threads of this state 108 | */ 109 | typedef struct global_State { 110 | lua_Alloc frealloc; /* function to reallocate memory */ 111 | void *ud; /* auxiliary data to 'frealloc' */ 112 | l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 113 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 114 | lu_mem GCmemtrav; /* memory traversed by the GC */ 115 | lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 116 | stringtable strt; /* hash table for strings */ 117 | TValue l_registry; 118 | unsigned int seed; /* randomized seed for hashes */ 119 | lu_byte currentwhite; 120 | lu_byte gcstate; /* state of garbage collector */ 121 | lu_byte gckind; /* kind of GC running */ 122 | lu_byte gcrunning; /* true if GC is running */ 123 | GCObject *allgc; /* list of all collectable objects */ 124 | GCObject **sweepgc; /* current position of sweep in list */ 125 | GCObject *finobj; /* list of collectable objects with finalizers */ 126 | GCObject *gray; /* list of gray objects */ 127 | GCObject *grayagain; /* list of objects to be traversed atomically */ 128 | GCObject *weak; /* list of tables with weak values */ 129 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 130 | GCObject *allweak; /* list of all-weak tables */ 131 | GCObject *tobefnz; /* list of userdata to be GC */ 132 | GCObject *fixedgc; /* list of objects not to be collected */ 133 | struct lua_State *twups; /* list of threads with open upvalues */ 134 | unsigned int gcfinnum; /* number of finalizers to call in each GC step */ 135 | int gcpause; /* size of pause between successive GCs */ 136 | int gcstepmul; /* GC 'granularity' */ 137 | lua_CFunction panic; /* to be called in unprotected errors */ 138 | struct lua_State *mainthread; 139 | const lua_Number *version; /* pointer to version number */ 140 | TString *memerrmsg; /* memory-error message */ 141 | TString *tmname[TM_N]; /* array with tag-method names */ 142 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 143 | TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ 144 | } global_State; 145 | 146 | 147 | /* 148 | ** 'per thread' state 149 | */ 150 | struct lua_State { 151 | CommonHeader; 152 | unsigned short nci; /* number of items in 'ci' list */ 153 | lu_byte status; 154 | StkId top; /* first free slot in the stack */ 155 | global_State *l_G; 156 | CallInfo *ci; /* call info for current function */ 157 | const Instruction *oldpc; /* last pc traced */ 158 | StkId stack_last; /* last free slot in the stack */ 159 | StkId stack; /* stack base */ 160 | UpVal *openupval; /* list of open upvalues in this stack */ 161 | GCObject *gclist; 162 | struct lua_State *twups; /* list of threads with open upvalues */ 163 | struct lua_longjmp *errorJmp; /* current error recover point */ 164 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 165 | lua_Hook hook; 166 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 167 | int stacksize; 168 | int basehookcount; 169 | int hookcount; 170 | unsigned short nny; /* number of non-yieldable calls in stack */ 171 | unsigned short nCcalls; /* number of nested C calls */ 172 | lu_byte hookmask; 173 | lu_byte allowhook; 174 | }; 175 | 176 | 177 | #define G(L) (L->l_G) 178 | 179 | 180 | /* 181 | ** Union of all collectable objects (only for conversions) 182 | */ 183 | union GCUnion { 184 | GCObject gc; /* common header */ 185 | struct TString ts; 186 | struct Udata u; 187 | union Closure cl; 188 | struct Table h; 189 | struct Proto p; 190 | struct lua_State th; /* thread */ 191 | }; 192 | 193 | 194 | #define cast_u(o) cast(union GCUnion *, (o)) 195 | 196 | /* macros to convert a GCObject into a specific value */ 197 | #define gco2ts(o) \ 198 | check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) 199 | #define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u)) 200 | #define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l)) 201 | #define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c)) 202 | #define gco2cl(o) \ 203 | check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) 204 | #define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) 205 | #define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) 206 | #define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) 207 | 208 | 209 | /* macro to convert a Lua object into a GCObject */ 210 | #define obj2gco(v) \ 211 | check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc))) 212 | 213 | 214 | /* actual number of total bytes allocated */ 215 | #define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt) 216 | 217 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 218 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 219 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 220 | LUAI_FUNC void luaE_freeCI (lua_State *L); 221 | LUAI_FUNC void luaE_shrinkCI (lua_State *L); 222 | 223 | 224 | #endif 225 | 226 | -------------------------------------------------------------------------------- /lua5.3/2/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.21 2014/10/25 11:50:46 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 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_N /* number of elements in the enum */ 44 | } TMS; 45 | 46 | 47 | 48 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 49 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 50 | 51 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 52 | 53 | #define ttypename(x) luaT_typenames_[(x) + 1] 54 | #define objtypename(x) ttypename(ttnov(x)) 55 | 56 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 57 | 58 | 59 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 60 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 61 | TMS event); 62 | LUAI_FUNC void luaT_init (lua_State *L); 63 | 64 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 65 | const TValue *p2, TValue *p3, int hasres); 66 | LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 67 | StkId res, TMS event); 68 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 69 | StkId res, TMS event); 70 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 71 | const TValue *p2, TMS event); 72 | 73 | 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /lua5.3/2/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 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 50 | 51 | 52 | 53 | /* --------- Private Part ------------------ */ 54 | 55 | struct Zio { 56 | size_t n; /* bytes still unread */ 57 | const char *p; /* current position in buffer */ 58 | lua_Reader reader; /* reader function */ 59 | void *data; /* additional data */ 60 | lua_State *L; /* Lua state (for reader) */ 61 | }; 62 | 63 | 64 | LUAI_FUNC int luaZ_fill (ZIO *z); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /lua5.3/README: -------------------------------------------------------------------------------- 1 | Excerpt from MIT-licensed Lua 5.3 sources, required to build lua-getsize for Lua 5.3. 2 | http://www.lua.org/ 3 | -------------------------------------------------------------------------------- /lua5.3/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.14 2014/06/19 18:27:20 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 | ** Upvalues for Lua closures 27 | */ 28 | struct UpVal { 29 | TValue *v; /* points to stack or to its own value */ 30 | lu_mem refcount; /* reference counter */ 31 | union { 32 | struct { /* (when open) */ 33 | UpVal *next; /* linked list */ 34 | int touched; /* mark to avoid cycles with dead threads */ 35 | } open; 36 | TValue value; /* the value (when closed) */ 37 | } u; 38 | }; 39 | 40 | #define upisopen(up) ((up)->v != &(up)->u.value) 41 | 42 | 43 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 44 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); 45 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); 46 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); 47 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 48 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 49 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 50 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 51 | int pc); 52 | 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /lua5.3/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.86 2014/10/25 11:50:46 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 | #define luaC_condGC(L,c) \ 105 | {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} 106 | #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) 107 | 108 | 109 | #define luaC_barrier(L,p,v) { \ 110 | if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ 111 | luaC_barrier_(L,obj2gco(p),gcvalue(v)); } 112 | 113 | #define luaC_barrierback(L,p,v) { \ 114 | if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ 115 | luaC_barrierback_(L,p); } 116 | 117 | #define luaC_objbarrier(L,p,o) { \ 118 | if (isblack(p) && iswhite(o)) \ 119 | luaC_barrier_(L,obj2gco(p),obj2gco(o)); } 120 | 121 | #define luaC_upvalbarrier(L,uv) \ 122 | { if (iscollectable((uv)->v) && !upisopen(uv)) \ 123 | luaC_upvalbarrier_(L,uv); } 124 | 125 | LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); 126 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 127 | LUAI_FUNC void luaC_step (lua_State *L); 128 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 129 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 130 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); 131 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 132 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); 133 | LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); 134 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 135 | LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); 136 | 137 | 138 | #endif 139 | -------------------------------------------------------------------------------- /lua5.3/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.125 2014/12/19 13:30:23 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 | ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count 19 | ** the total memory used by Lua (in bytes). Usually, 'size_t' and 20 | ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. 21 | */ 22 | #if defined(LUAI_MEM) /* { external definitions? */ 23 | typedef LUAI_UMEM lu_mem; 24 | typedef LUAI_MEM l_mem; 25 | #elif LUAI_BITSINT >= 32 /* }{ */ 26 | typedef size_t lu_mem; 27 | typedef ptrdiff_t l_mem; 28 | #else /* 16-bit ints */ /* }{ */ 29 | typedef unsigned long lu_mem; 30 | typedef long l_mem; 31 | #endif /* } */ 32 | 33 | 34 | /* chars used as small naturals (so that 'char' is reserved for characters) */ 35 | typedef unsigned char lu_byte; 36 | 37 | 38 | /* maximum value for size_t */ 39 | #define MAX_SIZET ((size_t)(~(size_t)0)) 40 | 41 | /* maximum size visible for Lua (must be representable in a lua_Integer */ 42 | #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ 43 | : (size_t)(LUA_MAXINTEGER)) 44 | 45 | 46 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)) 47 | 48 | #define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1)) 49 | 50 | 51 | #define MAX_INT INT_MAX /* maximum value of an int */ 52 | 53 | 54 | /* 55 | ** conversion of pointer to integer: 56 | ** this is for hashing only; there is no problem if the integer 57 | ** cannot hold the whole pointer value 58 | */ 59 | #define point2int(p) ((unsigned int)((size_t)(p) & UINT_MAX)) 60 | 61 | 62 | 63 | /* type to ensure maximum alignment */ 64 | #if defined(LUAI_USER_ALIGNMENT_T) 65 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 66 | #else 67 | typedef union { double u; void *s; lua_Integer i; long l; } L_Umaxalign; 68 | #endif 69 | 70 | 71 | 72 | /* types of 'usual argument conversions' for lua_Number and lua_Integer */ 73 | typedef LUAI_UACNUMBER l_uacNumber; 74 | typedef LUAI_UACINT l_uacInt; 75 | 76 | 77 | /* internal assertions for in-house debugging */ 78 | #if defined(lua_assert) 79 | #define check_exp(c,e) (lua_assert(c), (e)) 80 | /* to avoid problems with conditions too long */ 81 | #define lua_longassert(c) { if (!(c)) lua_assert(0); } 82 | #else 83 | #define lua_assert(c) ((void)0) 84 | #define check_exp(c,e) (e) 85 | #define lua_longassert(c) ((void)0) 86 | #endif 87 | 88 | /* 89 | ** assertion for checking API calls 90 | */ 91 | #if defined(LUA_USE_APICHECK) 92 | #include 93 | #define luai_apicheck(e) assert(e) 94 | #else 95 | #define luai_apicheck(e) lua_assert(e) 96 | #endif 97 | 98 | 99 | #define api_check(e,msg) luai_apicheck((e) && msg) 100 | 101 | 102 | #if !defined(UNUSED) 103 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 104 | #endif 105 | 106 | 107 | #define cast(t, exp) ((t)(exp)) 108 | 109 | #define cast_void(i) cast(void, (i)) 110 | #define cast_byte(i) cast(lu_byte, (i)) 111 | #define cast_num(i) cast(lua_Number, (i)) 112 | #define cast_int(i) cast(int, (i)) 113 | #define cast_uchar(i) cast(unsigned char, (i)) 114 | 115 | 116 | /* cast a signed lua_Integer to lua_Unsigned */ 117 | #if !defined(l_castS2U) 118 | #define l_castS2U(i) ((lua_Unsigned)(i)) 119 | #endif 120 | 121 | /* 122 | ** cast a lua_Unsigned to a signed lua_Integer; this cast is 123 | ** not strict ISO C, but two-complement architectures should 124 | ** work fine. 125 | */ 126 | #if !defined(l_castU2S) 127 | #define l_castU2S(i) ((lua_Integer)(i)) 128 | #endif 129 | 130 | 131 | /* 132 | ** non-return type 133 | */ 134 | #if defined(__GNUC__) 135 | #define l_noret void __attribute__((noreturn)) 136 | #elif defined(_MSC_VER) && _MSC_VER >= 1200 137 | #define l_noret void __declspec(noreturn) 138 | #else 139 | #define l_noret void 140 | #endif 141 | 142 | 143 | 144 | /* 145 | ** maximum depth for nested C calls and syntactical nested non-terminals 146 | ** in a program. (Value must fit in an unsigned short int.) 147 | */ 148 | #if !defined(LUAI_MAXCCALLS) 149 | #define LUAI_MAXCCALLS 200 150 | #endif 151 | 152 | /* 153 | ** maximum number of upvalues in a closure (both C and Lua). (Value 154 | ** must fit in an unsigned char.) 155 | */ 156 | #define MAXUPVAL UCHAR_MAX 157 | 158 | 159 | /* 160 | ** type for virtual-machine instructions; 161 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 162 | */ 163 | #if LUAI_BITSINT >= 32 164 | typedef unsigned int Instruction; 165 | #else 166 | typedef unsigned long Instruction; 167 | #endif 168 | 169 | 170 | 171 | 172 | /* minimum size for the string table (must be power of 2) */ 173 | #if !defined(MINSTRTABSIZE) 174 | #define MINSTRTABSIZE 64 /* minimum size for "predefined" strings */ 175 | #endif 176 | 177 | 178 | /* minimum size for string buffer */ 179 | #if !defined(LUA_MINBUFFER) 180 | #define LUA_MINBUFFER 32 181 | #endif 182 | 183 | 184 | #if !defined(lua_lock) 185 | #define lua_lock(L) ((void) 0) 186 | #define lua_unlock(L) ((void) 0) 187 | #endif 188 | 189 | #if !defined(luai_threadyield) 190 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 191 | #endif 192 | 193 | 194 | /* 195 | ** these macros allow user-specific actions on threads when you defined 196 | ** LUAI_EXTRASPACE and need to do something extra when a thread is 197 | ** created/deleted/resumed/yielded. 198 | */ 199 | #if !defined(luai_userstateopen) 200 | #define luai_userstateopen(L) ((void)L) 201 | #endif 202 | 203 | #if !defined(luai_userstateclose) 204 | #define luai_userstateclose(L) ((void)L) 205 | #endif 206 | 207 | #if !defined(luai_userstatethread) 208 | #define luai_userstatethread(L,L1) ((void)L) 209 | #endif 210 | 211 | #if !defined(luai_userstatefree) 212 | #define luai_userstatefree(L,L1) ((void)L) 213 | #endif 214 | 215 | #if !defined(luai_userstateresume) 216 | #define luai_userstateresume(L,n) ((void)L) 217 | #endif 218 | 219 | #if !defined(luai_userstateyield) 220 | #define luai_userstateyield(L,n) ((void)L) 221 | #endif 222 | 223 | 224 | 225 | /* 226 | ** macro to control inclusion of some hard tests on stack reallocation 227 | */ 228 | #if !defined(HARDSTACKTESTS) 229 | #define condmovestack(L) ((void)0) 230 | #else 231 | /* realloc stack keeping its size */ 232 | #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) 233 | #endif 234 | 235 | #if !defined(HARDMEMTESTS) 236 | #define condchangemem(L) condmovestack(L) 237 | #else 238 | #define condchangemem(L) \ 239 | ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) 240 | #endif 241 | 242 | #endif 243 | -------------------------------------------------------------------------------- /lua5.3/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 | -------------------------------------------------------------------------------- /lua5.3/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.119 2014/10/30 18:53:28 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 | 19 | ** Some notes about garbage-collected objects: All objects in Lua must 20 | ** be kept somehow accessible until being freed, so all objects always 21 | ** belong to one (and only one) of these lists, using field 'next' of 22 | ** the 'CommonHeader' for the link: 23 | ** 24 | ** 'allgc': all objects not marked for finalization; 25 | ** 'finobj': all objects marked for finalization; 26 | ** 'tobefnz': all objects ready to be finalized; 27 | ** 'fixedgc': all objects that are not to be collected (currently 28 | ** only small strings, such as reserved words). 29 | 30 | */ 31 | 32 | 33 | struct lua_longjmp; /* defined in ldo.c */ 34 | 35 | 36 | 37 | /* extra stack space to handle TM calls and some other extras */ 38 | #define EXTRA_STACK 5 39 | 40 | 41 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 42 | 43 | 44 | /* kinds of Garbage Collection */ 45 | #define KGC_NORMAL 0 46 | #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ 47 | 48 | 49 | typedef struct stringtable { 50 | TString **hash; 51 | int nuse; /* number of elements */ 52 | int size; 53 | } stringtable; 54 | 55 | 56 | /* 57 | ** Information about a call. 58 | ** When a thread yields, 'func' is adjusted to pretend that the 59 | ** top function has only the yielded values in its stack; in that 60 | ** case, the actual 'func' value is saved in field 'extra'. 61 | ** When a function calls another with a continuation, 'extra' keeps 62 | ** the function index so that, in case of errors, the continuation 63 | ** function can be called with the correct top. 64 | */ 65 | typedef struct CallInfo { 66 | StkId func; /* function index in the stack */ 67 | StkId top; /* top for this function */ 68 | struct CallInfo *previous, *next; /* dynamic call link */ 69 | union { 70 | struct { /* only for Lua functions */ 71 | StkId base; /* base for this function */ 72 | const Instruction *savedpc; 73 | } l; 74 | struct { /* only for C functions */ 75 | lua_KFunction k; /* continuation in case of yields */ 76 | ptrdiff_t old_errfunc; 77 | lua_KContext ctx; /* context info. in case of yields */ 78 | } c; 79 | } u; 80 | ptrdiff_t extra; 81 | short nresults; /* expected number of results from this function */ 82 | lu_byte callstatus; 83 | } CallInfo; 84 | 85 | 86 | /* 87 | ** Bits in CallInfo status 88 | */ 89 | #define CIST_OAH (1<<0) /* original value of 'allowhook' */ 90 | #define CIST_LUA (1<<1) /* call is running a Lua function */ 91 | #define CIST_HOOKED (1<<2) /* call is running a debug hook */ 92 | #define CIST_REENTRY (1<<3) /* call is running on same invocation of 93 | luaV_execute of previous call */ 94 | #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ 95 | #define CIST_TAIL (1<<5) /* call was tail called */ 96 | #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ 97 | 98 | #define isLua(ci) ((ci)->callstatus & CIST_LUA) 99 | 100 | /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ 101 | #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) 102 | #define getoah(st) ((st) & CIST_OAH) 103 | 104 | 105 | /* 106 | ** 'global state', shared by all threads of this state 107 | */ 108 | typedef struct global_State { 109 | lua_Alloc frealloc; /* function to reallocate memory */ 110 | void *ud; /* auxiliary data to 'frealloc' */ 111 | lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 112 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 113 | lu_mem GCmemtrav; /* memory traversed by the GC */ 114 | lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 115 | stringtable strt; /* hash table for strings */ 116 | TValue l_registry; 117 | unsigned int seed; /* randomized seed for hashes */ 118 | lu_byte currentwhite; 119 | lu_byte gcstate; /* state of garbage collector */ 120 | lu_byte gckind; /* kind of GC running */ 121 | lu_byte gcrunning; /* true if GC is running */ 122 | GCObject *allgc; /* list of all collectable objects */ 123 | GCObject **sweepgc; /* current position of sweep in list */ 124 | GCObject *finobj; /* list of collectable objects with finalizers */ 125 | GCObject *gray; /* list of gray objects */ 126 | GCObject *grayagain; /* list of objects to be traversed atomically */ 127 | GCObject *weak; /* list of tables with weak values */ 128 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 129 | GCObject *allweak; /* list of all-weak tables */ 130 | GCObject *tobefnz; /* list of userdata to be GC */ 131 | GCObject *fixedgc; /* list of objects not to be collected */ 132 | struct lua_State *twups; /* list of threads with open upvalues */ 133 | Mbuffer buff; /* temporary buffer for string concatenation */ 134 | unsigned int gcfinnum; /* number of finalizers to call in each GC step */ 135 | int gcpause; /* size of pause between successive GCs */ 136 | int gcstepmul; /* GC 'granularity' */ 137 | lua_CFunction panic; /* to be called in unprotected errors */ 138 | struct lua_State *mainthread; 139 | const lua_Number *version; /* pointer to version number */ 140 | TString *memerrmsg; /* memory-error message */ 141 | TString *tmname[TM_N]; /* array with tag-method names */ 142 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 143 | } global_State; 144 | 145 | 146 | /* 147 | ** 'per thread' state 148 | */ 149 | struct lua_State { 150 | CommonHeader; 151 | lu_byte status; 152 | StkId top; /* first free slot in the stack */ 153 | global_State *l_G; 154 | CallInfo *ci; /* call info for current function */ 155 | const Instruction *oldpc; /* last pc traced */ 156 | StkId stack_last; /* last free slot in the stack */ 157 | StkId stack; /* stack base */ 158 | UpVal *openupval; /* list of open upvalues in this stack */ 159 | GCObject *gclist; 160 | struct lua_State *twups; /* list of threads with open upvalues */ 161 | struct lua_longjmp *errorJmp; /* current error recover point */ 162 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 163 | lua_Hook hook; 164 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 165 | int stacksize; 166 | int basehookcount; 167 | int hookcount; 168 | unsigned short nny; /* number of non-yieldable calls in stack */ 169 | unsigned short nCcalls; /* number of nested C calls */ 170 | lu_byte hookmask; 171 | lu_byte allowhook; 172 | }; 173 | 174 | 175 | #define G(L) (L->l_G) 176 | 177 | 178 | /* 179 | ** Union of all collectable objects (only for conversions) 180 | */ 181 | union GCUnion { 182 | GCObject gc; /* common header */ 183 | struct TString ts; 184 | struct Udata u; 185 | union Closure cl; 186 | struct Table h; 187 | struct Proto p; 188 | struct lua_State th; /* thread */ 189 | }; 190 | 191 | 192 | #define cast_u(o) cast(union GCUnion *, (o)) 193 | 194 | /* macros to convert a GCObject into a specific value */ 195 | #define gco2ts(o) \ 196 | check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) 197 | #define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u)) 198 | #define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l)) 199 | #define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c)) 200 | #define gco2cl(o) \ 201 | check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) 202 | #define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) 203 | #define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) 204 | #define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) 205 | 206 | 207 | /* macro to convert a Lua object into a GCObject */ 208 | #define obj2gco(v) \ 209 | check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc))) 210 | 211 | 212 | /* actual number of total bytes allocated */ 213 | #define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) 214 | 215 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 216 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 217 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 218 | LUAI_FUNC void luaE_freeCI (lua_State *L); 219 | LUAI_FUNC void luaE_shrinkCI (lua_State *L); 220 | 221 | 222 | #endif 223 | 224 | -------------------------------------------------------------------------------- /lua5.3/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.56 2014/07/18 14:46:47 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 | #define sizestring(s) sizelstring((s)->len) 17 | 18 | #define sizeludata(l) (sizeof(union UUdata) + (l)) 19 | #define sizeudata(u) sizeludata((u)->len) 20 | 21 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 22 | (sizeof(s)/sizeof(char))-1)) 23 | 24 | 25 | /* 26 | ** test whether a string is a reserved word 27 | */ 28 | #define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) 29 | 30 | 31 | /* 32 | ** equality for short strings, which are always internalized 33 | */ 34 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) 35 | 36 | 37 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 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_remove (lua_State *L, TString *ts); 41 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); 42 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 43 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /lua5.3/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.21 2014/10/25 11:50:46 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 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_N /* number of elements in the enum */ 44 | } TMS; 45 | 46 | 47 | 48 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 49 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 50 | 51 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 52 | 53 | #define ttypename(x) luaT_typenames_[(x) + 1] 54 | #define objtypename(x) ttypename(ttnov(x)) 55 | 56 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 57 | 58 | 59 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 60 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 61 | TMS event); 62 | LUAI_FUNC void luaT_init (lua_State *L); 63 | 64 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 65 | const TValue *p2, TValue *p3, int hasres); 66 | LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 67 | StkId res, TMS event); 68 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 69 | StkId res, TMS event); 70 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 71 | const TValue *p2, TMS event); 72 | 73 | 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /lua5.3/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.30 2014/12/19 17:26:14 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 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 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 | size_t n; /* bytes still unread */ 58 | const char *p; /* current position in buffer */ 59 | lua_Reader reader; /* reader function */ 60 | void *data; /* additional data */ 61 | lua_State *L; /* Lua state (for reader) */ 62 | }; 63 | 64 | 65 | LUAI_FUNC int luaZ_fill (ZIO *z); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /lua5.4/1/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h $ 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 | #define luaM_error(L) luaD_throw(L, LUA_ERRMEM) 18 | 19 | 20 | /* 21 | ** This macro tests whether it is safe to multiply 'n' by the size of 22 | ** type 't' without overflows. Because 'e' is always constant, it avoids 23 | ** the runtime division MAX_SIZET/(e). 24 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 25 | ** comparison avoids a runtime comparison when overflow cannot occur. 26 | ** The compiler should be able to optimize the real test by itself, but 27 | ** when it does it, it may give a warning about "comparison is always 28 | ** false due to limited range of data type"; the +1 tricks the compiler, 29 | ** avoiding this warning but also this optimization.) 30 | */ 31 | #define luaM_testsize(n,e) \ 32 | (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e)) 33 | 34 | #define luaM_checksize(L,n,e) \ 35 | (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) 36 | 37 | 38 | /* 39 | ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that 40 | ** the result is not larger than 'n' and cannot overflow a 'size_t' 41 | ** when multiplied by the size of type 't'. (Assumes that 'n' is an 42 | ** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) 43 | */ 44 | #define luaM_limitN(n,t) \ 45 | ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ 46 | cast_uint((MAX_SIZET/sizeof(t)))) 47 | 48 | 49 | /* 50 | ** Arrays of chars do not need any test 51 | */ 52 | #define luaM_reallocvchar(L,b,on,n) \ 53 | cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 54 | 55 | #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) 56 | #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) 57 | #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) 58 | 59 | #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) 60 | #define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) 61 | #define luaM_newvectorchecked(L,n,t) \ 62 | (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) 63 | 64 | #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) 65 | 66 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 67 | ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ 68 | luaM_limitN(limit,t),e))) 69 | 70 | #define luaM_reallocvector(L, v,oldn,n,t) \ 71 | (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \ 72 | cast_sizet(n) * sizeof(t)))) 73 | 74 | #define luaM_shrinkvector(L,v,size,fs,t) \ 75 | ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) 76 | 77 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 78 | 79 | /* not to be called directly */ 80 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 81 | size_t size); 82 | LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, 83 | size_t size); 84 | LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); 85 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, 86 | int *size, int size_elem, int limit, 87 | const char *what); 88 | LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, 89 | int final_n, int size_elem); 90 | LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); 91 | 92 | #endif 93 | 94 | -------------------------------------------------------------------------------- /lua5.4/1/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h $ 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 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_CLOSE, 44 | TM_N /* number of elements in the enum */ 45 | } TMS; 46 | 47 | 48 | /* 49 | ** Mask with 1 in all fast-access methods. A 1 in any of these bits 50 | ** in the flag of a (meta)table means the metatable does not have the 51 | ** corresponding metamethod field. (Bit 7 of the flag is used for 52 | ** 'isrealasize'.) 53 | */ 54 | #define maskflags (~(~0u << (TM_EQ + 1))) 55 | 56 | 57 | /* 58 | ** Test whether there is no tagmethod. 59 | ** (Because tagmethods use raw accesses, the result may be an "empty" nil.) 60 | */ 61 | #define notm(tm) ttisnil(tm) 62 | 63 | 64 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 65 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 66 | 67 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 68 | 69 | #define ttypename(x) luaT_typenames_[(x) + 1] 70 | 71 | LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];) 72 | 73 | 74 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); 75 | 76 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 77 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 78 | TMS event); 79 | LUAI_FUNC void luaT_init (lua_State *L); 80 | 81 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 82 | const TValue *p2, const TValue *p3); 83 | LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, 84 | const TValue *p1, const TValue *p2, StkId p3); 85 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 86 | StkId res, TMS event); 87 | LUAI_FUNC void luaT_tryconcatTM (lua_State *L); 88 | LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, 89 | const TValue *p2, int inv, StkId res, TMS event); 90 | LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, 91 | int inv, StkId res, TMS event); 92 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 93 | const TValue *p2, TMS event); 94 | LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, 95 | int inv, int isfloat, TMS event); 96 | 97 | LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, 98 | struct CallInfo *ci, const Proto *p); 99 | LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, 100 | StkId where, int wanted); 101 | 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /lua5.4/1/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h $ 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 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 50 | 51 | 52 | 53 | /* --------- Private Part ------------------ */ 54 | 55 | struct Zio { 56 | size_t n; /* bytes still unread */ 57 | const char *p; /* current position in buffer */ 58 | lua_Reader reader; /* reader function */ 59 | void *data; /* additional data */ 60 | lua_State *L; /* Lua state (for reader) */ 61 | }; 62 | 63 | 64 | LUAI_FUNC int luaZ_fill (ZIO *z); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /lua5.4/2/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h $ 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 | #define luaM_error(L) luaD_throw(L, LUA_ERRMEM) 18 | 19 | 20 | /* 21 | ** This macro tests whether it is safe to multiply 'n' by the size of 22 | ** type 't' without overflows. Because 'e' is always constant, it avoids 23 | ** the runtime division MAX_SIZET/(e). 24 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 25 | ** comparison avoids a runtime comparison when overflow cannot occur. 26 | ** The compiler should be able to optimize the real test by itself, but 27 | ** when it does it, it may give a warning about "comparison is always 28 | ** false due to limited range of data type"; the +1 tricks the compiler, 29 | ** avoiding this warning but also this optimization.) 30 | */ 31 | #define luaM_testsize(n,e) \ 32 | (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e)) 33 | 34 | #define luaM_checksize(L,n,e) \ 35 | (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) 36 | 37 | 38 | /* 39 | ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that 40 | ** the result is not larger than 'n' and cannot overflow a 'size_t' 41 | ** when multiplied by the size of type 't'. (Assumes that 'n' is an 42 | ** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) 43 | */ 44 | #define luaM_limitN(n,t) \ 45 | ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ 46 | cast_uint((MAX_SIZET/sizeof(t)))) 47 | 48 | 49 | /* 50 | ** Arrays of chars do not need any test 51 | */ 52 | #define luaM_reallocvchar(L,b,on,n) \ 53 | cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 54 | 55 | #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) 56 | #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) 57 | #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) 58 | 59 | #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) 60 | #define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) 61 | #define luaM_newvectorchecked(L,n,t) \ 62 | (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) 63 | 64 | #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) 65 | 66 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 67 | ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ 68 | luaM_limitN(limit,t),e))) 69 | 70 | #define luaM_reallocvector(L, v,oldn,n,t) \ 71 | (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \ 72 | cast_sizet(n) * sizeof(t)))) 73 | 74 | #define luaM_shrinkvector(L,v,size,fs,t) \ 75 | ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) 76 | 77 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 78 | 79 | /* not to be called directly */ 80 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 81 | size_t size); 82 | LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, 83 | size_t size); 84 | LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); 85 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, 86 | int *size, int size_elem, int limit, 87 | const char *what); 88 | LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, 89 | int final_n, int size_elem); 90 | LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); 91 | 92 | #endif 93 | 94 | -------------------------------------------------------------------------------- /lua5.4/2/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h $ 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 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_CLOSE, 44 | TM_N /* number of elements in the enum */ 45 | } TMS; 46 | 47 | 48 | /* 49 | ** Mask with 1 in all fast-access methods. A 1 in any of these bits 50 | ** in the flag of a (meta)table means the metatable does not have the 51 | ** corresponding metamethod field. (Bit 7 of the flag is used for 52 | ** 'isrealasize'.) 53 | */ 54 | #define maskflags (~(~0u << (TM_EQ + 1))) 55 | 56 | 57 | /* 58 | ** Test whether there is no tagmethod. 59 | ** (Because tagmethods use raw accesses, the result may be an "empty" nil.) 60 | */ 61 | #define notm(tm) ttisnil(tm) 62 | 63 | 64 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 65 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 66 | 67 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 68 | 69 | #define ttypename(x) luaT_typenames_[(x) + 1] 70 | 71 | LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];) 72 | 73 | 74 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); 75 | 76 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 77 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 78 | TMS event); 79 | LUAI_FUNC void luaT_init (lua_State *L); 80 | 81 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 82 | const TValue *p2, const TValue *p3); 83 | LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, 84 | const TValue *p1, const TValue *p2, StkId p3); 85 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 86 | StkId res, TMS event); 87 | LUAI_FUNC void luaT_tryconcatTM (lua_State *L); 88 | LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, 89 | const TValue *p2, int inv, StkId res, TMS event); 90 | LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, 91 | int inv, StkId res, TMS event); 92 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 93 | const TValue *p2, TMS event); 94 | LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, 95 | int inv, int isfloat, TMS event); 96 | 97 | LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, 98 | struct CallInfo *ci, const Proto *p); 99 | LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, 100 | StkId where, int wanted); 101 | 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /lua5.4/2/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h $ 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 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 50 | 51 | 52 | 53 | /* --------- Private Part ------------------ */ 54 | 55 | struct Zio { 56 | size_t n; /* bytes still unread */ 57 | const char *p; /* current position in buffer */ 58 | lua_Reader reader; /* reader function */ 59 | void *data; /* additional data */ 60 | lua_State *L; /* Lua state (for reader) */ 61 | }; 62 | 63 | 64 | LUAI_FUNC int luaZ_fill (ZIO *z); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /lua5.4/3/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h $ 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 | #define luaM_error(L) luaD_throw(L, LUA_ERRMEM) 18 | 19 | 20 | /* 21 | ** This macro tests whether it is safe to multiply 'n' by the size of 22 | ** type 't' without overflows. Because 'e' is always constant, it avoids 23 | ** the runtime division MAX_SIZET/(e). 24 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 25 | ** comparison avoids a runtime comparison when overflow cannot occur. 26 | ** The compiler should be able to optimize the real test by itself, but 27 | ** when it does it, it may give a warning about "comparison is always 28 | ** false due to limited range of data type"; the +1 tricks the compiler, 29 | ** avoiding this warning but also this optimization.) 30 | */ 31 | #define luaM_testsize(n,e) \ 32 | (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e)) 33 | 34 | #define luaM_checksize(L,n,e) \ 35 | (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) 36 | 37 | 38 | /* 39 | ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that 40 | ** the result is not larger than 'n' and cannot overflow a 'size_t' 41 | ** when multiplied by the size of type 't'. (Assumes that 'n' is an 42 | ** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) 43 | */ 44 | #define luaM_limitN(n,t) \ 45 | ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ 46 | cast_uint((MAX_SIZET/sizeof(t)))) 47 | 48 | 49 | /* 50 | ** Arrays of chars do not need any test 51 | */ 52 | #define luaM_reallocvchar(L,b,on,n) \ 53 | cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 54 | 55 | #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) 56 | #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) 57 | #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) 58 | 59 | #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) 60 | #define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) 61 | #define luaM_newvectorchecked(L,n,t) \ 62 | (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) 63 | 64 | #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) 65 | 66 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 67 | ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ 68 | luaM_limitN(limit,t),e))) 69 | 70 | #define luaM_reallocvector(L, v,oldn,n,t) \ 71 | (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \ 72 | cast_sizet(n) * sizeof(t)))) 73 | 74 | #define luaM_shrinkvector(L,v,size,fs,t) \ 75 | ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) 76 | 77 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 78 | 79 | /* not to be called directly */ 80 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 81 | size_t size); 82 | LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, 83 | size_t size); 84 | LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); 85 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, 86 | int *size, int size_elem, int limit, 87 | const char *what); 88 | LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, 89 | int final_n, int size_elem); 90 | LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); 91 | 92 | #endif 93 | 94 | -------------------------------------------------------------------------------- /lua5.4/3/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h $ 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 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_CLOSE, 44 | TM_N /* number of elements in the enum */ 45 | } TMS; 46 | 47 | 48 | /* 49 | ** Mask with 1 in all fast-access methods. A 1 in any of these bits 50 | ** in the flag of a (meta)table means the metatable does not have the 51 | ** corresponding metamethod field. (Bit 7 of the flag is used for 52 | ** 'isrealasize'.) 53 | */ 54 | #define maskflags (~(~0u << (TM_EQ + 1))) 55 | 56 | 57 | /* 58 | ** Test whether there is no tagmethod. 59 | ** (Because tagmethods use raw accesses, the result may be an "empty" nil.) 60 | */ 61 | #define notm(tm) ttisnil(tm) 62 | 63 | 64 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 65 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 66 | 67 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 68 | 69 | #define ttypename(x) luaT_typenames_[(x) + 1] 70 | 71 | LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];) 72 | 73 | 74 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); 75 | 76 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 77 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 78 | TMS event); 79 | LUAI_FUNC void luaT_init (lua_State *L); 80 | 81 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 82 | const TValue *p2, const TValue *p3); 83 | LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, 84 | const TValue *p1, const TValue *p2, StkId p3); 85 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 86 | StkId res, TMS event); 87 | LUAI_FUNC void luaT_tryconcatTM (lua_State *L); 88 | LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, 89 | const TValue *p2, int inv, StkId res, TMS event); 90 | LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, 91 | int inv, StkId res, TMS event); 92 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 93 | const TValue *p2, TMS event); 94 | LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, 95 | int inv, int isfloat, TMS event); 96 | 97 | LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, 98 | struct CallInfo *ci, const Proto *p); 99 | LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, 100 | StkId where, int wanted); 101 | 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /lua5.4/3/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h $ 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 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 50 | 51 | 52 | 53 | /* --------- Private Part ------------------ */ 54 | 55 | struct Zio { 56 | size_t n; /* bytes still unread */ 57 | const char *p; /* current position in buffer */ 58 | lua_Reader reader; /* reader function */ 59 | void *data; /* additional data */ 60 | lua_State *L; /* Lua state (for reader) */ 61 | }; 62 | 63 | 64 | LUAI_FUNC int luaZ_fill (ZIO *z); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /lua5.4/README: -------------------------------------------------------------------------------- 1 | Excerpt from MIT-licensed Lua 5.4 sources, required to build lua-getsize for Lua 5.4. 2 | http://www.lua.org/ 3 | -------------------------------------------------------------------------------- /lua5.4/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h $ 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(offsetof(CClosure, upvalue)) + \ 15 | cast_int(sizeof(TValue)) * (n)) 16 | 17 | #define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \ 18 | cast_int(sizeof(TValue *)) * (n)) 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 | #define upisopen(up) ((up)->v != &(up)->u.value) 33 | 34 | 35 | #define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v)) 36 | 37 | 38 | /* 39 | ** maximum number of misses before giving up the cache of closures 40 | ** in prototypes 41 | */ 42 | #define MAXMISS 10 43 | 44 | 45 | /* 46 | ** Special "status" for 'luaF_close' 47 | */ 48 | 49 | /* close upvalues without running their closing methods */ 50 | #define NOCLOSINGMETH (-1) 51 | 52 | /* close upvalues running all closing methods in protected mode */ 53 | #define CLOSEPROTECT (-2) 54 | 55 | 56 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 57 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals); 58 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals); 59 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); 60 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 61 | LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); 62 | LUAI_FUNC int luaF_close (lua_State *L, StkId level, int status); 63 | LUAI_FUNC void luaF_unlinkupval (UpVal *uv); 64 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 65 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 66 | int pc); 67 | 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /lua5.4/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h $ 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 | ** Possible states of the Garbage Collector 30 | */ 31 | #define GCSpropagate 0 32 | #define GCSenteratomic 1 33 | #define GCSatomic 2 34 | #define GCSswpallgc 3 35 | #define GCSswpfinobj 4 36 | #define GCSswptobefnz 5 37 | #define GCSswpend 6 38 | #define GCScallfin 7 39 | #define GCSpause 8 40 | 41 | 42 | #define issweepphase(g) \ 43 | (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) 44 | 45 | 46 | /* 47 | ** macro to tell when main invariant (white objects cannot point to black 48 | ** ones) must be kept. During a collection, the sweep 49 | ** phase may break the invariant, as objects turned white may point to 50 | ** still-black objects. The invariant is restored when sweep ends and 51 | ** all objects are white again. 52 | */ 53 | 54 | #define keepinvariant(g) ((g)->gcstate <= GCSatomic) 55 | 56 | 57 | /* 58 | ** some useful bit tricks 59 | */ 60 | #define resetbits(x,m) ((x) &= cast_byte(~(m))) 61 | #define setbits(x,m) ((x) |= (m)) 62 | #define testbits(x,m) ((x) & (m)) 63 | #define bitmask(b) (1<<(b)) 64 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 65 | #define l_setbit(x,b) setbits(x, bitmask(b)) 66 | #define resetbit(x,b) resetbits(x, bitmask(b)) 67 | #define testbit(x,b) testbits(x, bitmask(b)) 68 | 69 | 70 | /* 71 | ** Layout for bit use in 'marked' field. First three bits are 72 | ** used for object "age" in generational mode. Last bit is free 73 | ** to be used by respective objects. 74 | */ 75 | #define WHITE0BIT 3 /* object is white (type 0) */ 76 | #define WHITE1BIT 4 /* object is white (type 1) */ 77 | #define BLACKBIT 5 /* object is black */ 78 | #define FINALIZEDBIT 6 /* object has been marked for finalization */ 79 | 80 | 81 | 82 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 83 | 84 | 85 | #define iswhite(x) testbits((x)->marked, WHITEBITS) 86 | #define isblack(x) testbit((x)->marked, BLACKBIT) 87 | #define isgray(x) /* neither white nor black */ \ 88 | (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) 89 | 90 | #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) 91 | 92 | #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) 93 | #define isdeadm(ow,m) ((m) & (ow)) 94 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) 95 | 96 | #define changewhite(x) ((x)->marked ^= WHITEBITS) 97 | #define gray2black(x) l_setbit((x)->marked, BLACKBIT) 98 | 99 | #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS) 100 | 101 | 102 | /* object age in generational mode */ 103 | #define G_NEW 0 /* created in current cycle */ 104 | #define G_SURVIVAL 1 /* created in previous cycle */ 105 | #define G_OLD0 2 /* marked old by frw. barrier in this cycle */ 106 | #define G_OLD1 3 /* first full cycle as old */ 107 | #define G_OLD 4 /* really old object (not to be visited) */ 108 | #define G_TOUCHED1 5 /* old object touched this cycle */ 109 | #define G_TOUCHED2 6 /* old object touched in previous cycle */ 110 | 111 | #define AGEBITS 7 /* all age bits (111) */ 112 | 113 | #define getage(o) ((o)->marked & AGEBITS) 114 | #define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a)) 115 | #define isold(o) (getage(o) > G_SURVIVAL) 116 | 117 | #define changeage(o,f,t) \ 118 | check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) 119 | 120 | 121 | /* Default Values for GC parameters */ 122 | #define LUAI_GENMAJORMUL 100 123 | #define LUAI_GENMINORMUL 20 124 | 125 | /* wait memory to double before starting new cycle */ 126 | #define LUAI_GCPAUSE 200 127 | 128 | /* 129 | ** some gc parameters are stored divided by 4 to allow a maximum value 130 | ** up to 1023 in a 'lu_byte'. 131 | */ 132 | #define getgcparam(p) ((p) * 4) 133 | #define setgcparam(p,v) ((p) = (v) / 4) 134 | 135 | #define LUAI_GCMUL 100 136 | 137 | /* how much to allocate before next GC step (log2) */ 138 | #define LUAI_GCSTEPSIZE 13 /* 8 KB */ 139 | 140 | 141 | /* 142 | ** Check whether the declared GC mode is generational. While in 143 | ** generational mode, the collector can go temporarily to incremental 144 | ** mode to improve performance. This is signaled by 'g->lastatomic != 0'. 145 | */ 146 | #define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0) 147 | 148 | /* 149 | ** Does one step of collection when debt becomes positive. 'pre'/'pos' 150 | ** allows some adjustments to be done only when needed. macro 151 | ** 'condchangemem' is used only for heavy tests (forcing a full 152 | ** GC cycle on every opportunity) 153 | */ 154 | #define luaC_condGC(L,pre,pos) \ 155 | { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ 156 | condchangemem(L,pre,pos); } 157 | 158 | /* more often than not, 'pre'/'pos' are empty */ 159 | #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) 160 | 161 | 162 | #define luaC_barrier(L,p,v) ( \ 163 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 164 | luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) 165 | 166 | #define luaC_barrierback(L,p,v) ( \ 167 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ 168 | luaC_barrierback_(L,p) : cast_void(0)) 169 | 170 | #define luaC_objbarrier(L,p,o) ( \ 171 | (isblack(p) && iswhite(o)) ? \ 172 | luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) 173 | 174 | LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); 175 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 176 | LUAI_FUNC void luaC_step (lua_State *L); 177 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 178 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 179 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); 180 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 181 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); 182 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 183 | LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); 184 | 185 | 186 | #endif 187 | -------------------------------------------------------------------------------- /lua5.4/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h $ 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 | #define luaM_error(L) luaD_throw(L, LUA_ERRMEM) 18 | 19 | 20 | /* 21 | ** This macro tests whether it is safe to multiply 'n' by the size of 22 | ** type 't' without overflows. Because 'e' is always constant, it avoids 23 | ** the runtime division MAX_SIZET/(e). 24 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 25 | ** comparison avoids a runtime comparison when overflow cannot occur. 26 | ** The compiler should be able to optimize the real test by itself, but 27 | ** when it does it, it may give a warning about "comparison is always 28 | ** false due to limited range of data type"; the +1 tricks the compiler, 29 | ** avoiding this warning but also this optimization.) 30 | */ 31 | #define luaM_testsize(n,e) \ 32 | (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e)) 33 | 34 | #define luaM_checksize(L,n,e) \ 35 | (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) 36 | 37 | 38 | /* 39 | ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that 40 | ** the result is not larger than 'n' and cannot overflow a 'size_t' 41 | ** when multiplied by the size of type 't'. (Assumes that 'n' is an 42 | ** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) 43 | */ 44 | #define luaM_limitN(n,t) \ 45 | ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ 46 | cast_uint((MAX_SIZET/sizeof(t)))) 47 | 48 | 49 | /* 50 | ** Arrays of chars do not need any test 51 | */ 52 | #define luaM_reallocvchar(L,b,on,n) \ 53 | cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 54 | 55 | #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) 56 | #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) 57 | #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) 58 | 59 | #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) 60 | #define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) 61 | #define luaM_newvectorchecked(L,n,t) \ 62 | (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) 63 | 64 | #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) 65 | 66 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 67 | ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ 68 | luaM_limitN(limit,t),e))) 69 | 70 | #define luaM_reallocvector(L, v,oldn,n,t) \ 71 | (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \ 72 | cast_sizet(n) * sizeof(t)))) 73 | 74 | #define luaM_shrinkvector(L,v,size,fs,t) \ 75 | ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) 76 | 77 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 78 | 79 | /* not to be called directly */ 80 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 81 | size_t size); 82 | LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, 83 | size_t size); 84 | LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); 85 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, 86 | int *size, int size_elem, int limit, 87 | const char *what); 88 | LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, 89 | int final_n, int size_elem); 90 | LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); 91 | 92 | #endif 93 | 94 | -------------------------------------------------------------------------------- /lua5.4/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h $ 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 | /* 16 | ** Memory-allocation error message must be preallocated (it cannot 17 | ** be created after memory is exhausted) 18 | */ 19 | #define MEMERRMSG "not enough memory" 20 | 21 | 22 | /* 23 | ** Size of a TString: Size of the header plus space for the string 24 | ** itself (including final '\0'). 25 | */ 26 | #define sizelstring(l) (offsetof(TString, contents) + ((l) + 1) * sizeof(char)) 27 | 28 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 29 | (sizeof(s)/sizeof(char))-1)) 30 | 31 | 32 | /* 33 | ** test whether a string is a reserved word 34 | */ 35 | #define isreserved(s) ((s)->tt == LUA_VSHRSTR && (s)->extra > 0) 36 | 37 | 38 | /* 39 | ** equality for short strings, which are always internalized 40 | */ 41 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b)) 42 | 43 | 44 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, 45 | unsigned int seed, size_t step); 46 | LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); 47 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 48 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 49 | LUAI_FUNC void luaS_clearcache (global_State *g); 50 | LUAI_FUNC void luaS_init (lua_State *L); 51 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); 52 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue); 53 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 54 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 55 | LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); 56 | 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /lua5.4/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h $ 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 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_CLOSE, 44 | TM_N /* number of elements in the enum */ 45 | } TMS; 46 | 47 | 48 | /* 49 | ** Test whether there is no tagmethod. 50 | ** (Because tagmethods use raw accesses, the result may be an "empty" nil.) 51 | */ 52 | #define notm(tm) ttisnil(tm) 53 | 54 | 55 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 56 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 57 | 58 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 59 | 60 | #define ttypename(x) luaT_typenames_[(x) + 1] 61 | 62 | LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];) 63 | 64 | 65 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); 66 | 67 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 68 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 69 | TMS event); 70 | LUAI_FUNC void luaT_init (lua_State *L); 71 | 72 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 73 | const TValue *p2, const TValue *p3); 74 | LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, 75 | const TValue *p1, const TValue *p2, StkId p3); 76 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 77 | StkId res, TMS event); 78 | LUAI_FUNC void luaT_tryconcatTM (lua_State *L); 79 | LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, 80 | const TValue *p2, int inv, StkId res, TMS event); 81 | LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, 82 | int inv, StkId res, TMS event); 83 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 84 | const TValue *p2, TMS event); 85 | LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, 86 | int inv, int isfloat, TMS event); 87 | 88 | LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, 89 | struct CallInfo *ci, const Proto *p); 90 | LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, 91 | StkId where, int wanted); 92 | 93 | 94 | #endif 95 | -------------------------------------------------------------------------------- /lua5.4/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h $ 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 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 50 | 51 | 52 | 53 | /* --------- Private Part ------------------ */ 54 | 55 | struct Zio { 56 | size_t n; /* bytes still unread */ 57 | const char *p; /* current position in buffer */ 58 | lua_Reader reader; /* reader function */ 59 | void *data; /* additional data */ 60 | lua_State *L; /* Lua state (for reader) */ 61 | }; 62 | 63 | 64 | LUAI_FUNC int luaZ_fill (ZIO *z); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /rockspecs/getsize-0.1-1.rockspec: -------------------------------------------------------------------------------- 1 | package="getsize" 2 | version="0.1-1" 3 | 4 | source = { 5 | url = "https://github.com/siffiejoe/lua-getsize/archive/v0.1.zip", 6 | dir = "lua-getsize-0.1", 7 | } 8 | 9 | description = { 10 | summary = "Calculates the size of a Lua object.", 11 | detailed = [[ 12 | Calculates the size of a Lua object by poking in the Lua 13 | internals. Works for PUC-Rio Lua 5.1, 5.2, and 5.3, but 14 | _not_ for LuaJIT. 15 | ]], 16 | homepage = "http://code.matthewwild.co.uk/lua-getsize/", 17 | license = "MIT/X11, MIT", 18 | maintainer = "Philipp Janda ", 19 | } 20 | 21 | dependencies = { 22 | -- Uses Lua internals. Includes support for Lua 5.1, 5.2, and 5.3. 23 | "lua >= 5.1, < 5.4" 24 | } 25 | 26 | build = { 27 | type = "builtin", 28 | modules = { 29 | getsize = { 30 | sources = { 31 | "getsize_multi.c", 32 | "layout523.c", 33 | "layout530.c", 34 | }, 35 | incdirs = { 36 | "dummy" 37 | } 38 | } 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /rockspecs/getsize-0.2-1.rockspec: -------------------------------------------------------------------------------- 1 | package="getsize" 2 | version="0.2-1" 3 | 4 | source = { 5 | url = "https://github.com/siffiejoe/lua-getsize/archive/v0.2.zip", 6 | dir = "lua-getsize-0.2", 7 | } 8 | 9 | description = { 10 | summary = "Calculates the size of a Lua object.", 11 | detailed = [[ 12 | Calculates the size of a Lua object by poking in the Lua 13 | internals. Works for PUC-Rio Lua 5.1, 5.2, and 5.3, but 14 | _not_ for LuaJIT. 15 | ]], 16 | homepage = "http://code.matthewwild.co.uk/lua-getsize/", 17 | license = "MIT/X11, MIT", 18 | maintainer = "Philipp Janda ", 19 | } 20 | 21 | dependencies = { 22 | -- Uses Lua internals. Includes support for Lua 5.1, 5.2, and 5.3. 23 | "lua >= 5.1, < 5.4" 24 | } 25 | 26 | build = { 27 | type = "builtin", 28 | modules = { 29 | getsize = { 30 | sources = { 31 | "getsize_multi.c", 32 | "layout523.c", 33 | "layout530.c", 34 | "layout531.c", 35 | }, 36 | incdirs = { 37 | "dummy" 38 | } 39 | } 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /rockspecs/getsize-0.3-1.rockspec: -------------------------------------------------------------------------------- 1 | package="getsize" 2 | version="0.3-1" 3 | 4 | source = { 5 | url = "https://github.com/siffiejoe/lua-getsize/archive/v0.3.zip", 6 | dir = "lua-getsize-0.3", 7 | } 8 | 9 | description = { 10 | summary = "Calculates the size of a Lua object.", 11 | detailed = [[ 12 | Calculates the size of a Lua object by poking in the Lua 13 | internals. Works for PUC-Rio Lua 5.1, 5.2, 5.3, and 5.4, 14 | but _not_ for LuaJIT. 15 | ]], 16 | homepage = "http://code.matthewwild.co.uk/lua-getsize/", 17 | license = "MIT/X11, MIT", 18 | maintainer = "Philipp Janda ", 19 | } 20 | 21 | dependencies = { 22 | -- Uses Lua internals. Includes support for Lua 5.1, 5.2, 5.3, and 5.4. 23 | "lua >= 5.1, < 5.5" 24 | } 25 | 26 | build = { 27 | type = "builtin", 28 | modules = { 29 | getsize = { 30 | sources = { 31 | "getsize.c", 32 | "compat.c", 33 | "compat_50103.c", 34 | "compat_50200.c", 35 | "compat_50300.c", 36 | "compat_50400.c", 37 | "compat_50204.c", 38 | "compat_50301.c", 39 | "compat_50302.c", 40 | } 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /rockspecs/getsize-0.4-1.rockspec: -------------------------------------------------------------------------------- 1 | package="getsize" 2 | version="0.4-1" 3 | 4 | source = { 5 | url = "https://github.com/siffiejoe/lua-getsize/archive/v0.4.zip", 6 | dir = "lua-getsize-0.4", 7 | } 8 | 9 | description = { 10 | summary = "Calculates the size of a Lua object.", 11 | detailed = [[ 12 | Calculates the size of a Lua object by poking in the Lua 13 | internals. Works for PUC-Rio Lua 5.1, 5.2, 5.3, and 5.4, 14 | but _not_ for LuaJIT. 15 | ]], 16 | homepage = "http://code.matthewwild.co.uk/lua-getsize/", 17 | license = "MIT/X11, MIT", 18 | maintainer = "Philipp Janda ", 19 | } 20 | 21 | dependencies = { 22 | -- Uses Lua internals. Includes support for Lua 5.1, 5.2, 5.3, and 5.4. 23 | "lua >= 5.1, < 5.5" 24 | } 25 | 26 | build = { 27 | type = "builtin", 28 | modules = { 29 | getsize = { 30 | sources = { 31 | "getsize.c", 32 | "compat.c", 33 | "compat_50103.c", 34 | "compat_50200.c", 35 | "compat_50300.c", 36 | "compat_50400.c", 37 | "compat_50204.c", 38 | "compat_50301.c", 39 | "compat_50302.c", 40 | } 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /rockspecs/getsize-0.5-1.rockspec: -------------------------------------------------------------------------------- 1 | package="getsize" 2 | version="0.5-1" 3 | 4 | source = { 5 | url = "https://github.com/siffiejoe/lua-getsize/archive/v0.5.zip", 6 | dir = "lua-getsize-0.5", 7 | } 8 | 9 | description = { 10 | summary = "Calculates the size of a Lua object.", 11 | detailed = [[ 12 | Calculates the size of a Lua object by poking in the Lua 13 | internals. Works for PUC-Rio Lua 5.1, 5.2, 5.3, and 5.4, 14 | but _not_ for LuaJIT. 15 | ]], 16 | homepage = "http://code.matthewwild.co.uk/lua-getsize/", 17 | license = "MIT/X11, MIT", 18 | maintainer = "Philipp Janda ", 19 | } 20 | 21 | dependencies = { 22 | -- Uses Lua internals. Includes support for Lua 5.1, 5.2, 5.3, and 5.4. 23 | "lua >= 5.1, < 5.5" 24 | } 25 | 26 | build = { 27 | type = "builtin", 28 | modules = { 29 | getsize = { 30 | sources = { 31 | "getsize.c", 32 | "compat.c", 33 | "compat_50103.c", 34 | "compat_50200.c", 35 | "compat_50300.c", 36 | "compat_50400.c", 37 | "compat_50204.c", 38 | "compat_50301.c", 39 | "compat_50302.c", 40 | "compat_50401.c", 41 | } 42 | } 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /rockspecs/getsize-0.6-1.rockspec: -------------------------------------------------------------------------------- 1 | package="getsize" 2 | version="0.6-1" 3 | 4 | source = { 5 | url = "https://github.com/siffiejoe/lua-getsize/archive/v0.6.zip", 6 | dir = "lua-getsize-0.6", 7 | } 8 | 9 | description = { 10 | summary = "Calculates the size of a Lua object.", 11 | detailed = [[ 12 | Calculates the size of a Lua object by poking in the Lua 13 | internals. Works for PUC-Rio Lua 5.1, 5.2, 5.3, and 5.4, 14 | but _not_ for LuaJIT. 15 | ]], 16 | homepage = "http://code.matthewwild.co.uk/lua-getsize/", 17 | license = "MIT/X11, MIT", 18 | maintainer = "Philipp Janda ", 19 | } 20 | 21 | dependencies = { 22 | -- Uses Lua internals. Includes support for Lua 5.1, 5.2, 5.3, and 5.4. 23 | "lua >= 5.1, < 5.5" 24 | } 25 | 26 | build = { 27 | type = "builtin", 28 | modules = { 29 | getsize = { 30 | sources = { 31 | "getsize.c", 32 | "compat.c", 33 | "compat_50103.c", 34 | "compat_50200.c", 35 | "compat_50300.c", 36 | "compat_50400.c", 37 | "compat_50204.c", 38 | "compat_50301.c", 39 | "compat_50302.c", 40 | "compat_50401.c", 41 | "compat_50402.c", 42 | } 43 | } 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /rockspecs/getsize-0.7-1.rockspec: -------------------------------------------------------------------------------- 1 | package="getsize" 2 | version="0.7-1" 3 | 4 | source = { 5 | url = "https://github.com/siffiejoe/lua-getsize/archive/v0.7.zip", 6 | dir = "lua-getsize-0.7", 7 | } 8 | 9 | description = { 10 | summary = "Calculates the size of a Lua object.", 11 | detailed = [[ 12 | Calculates the size of a Lua object by poking in the Lua 13 | internals. Works for PUC-Rio Lua 5.1, 5.2, 5.3, and 5.4, 14 | but _not_ for LuaJIT. 15 | ]], 16 | homepage = "http://code.matthewwild.co.uk/lua-getsize/", 17 | license = "MIT/X11, MIT", 18 | maintainer = "Philipp Janda ", 19 | } 20 | 21 | dependencies = { 22 | -- Uses Lua internals. Includes support for Lua 5.1, 5.2, 5.3, and 5.4. 23 | "lua >= 5.1, < 5.5" 24 | } 25 | 26 | build = { 27 | type = "builtin", 28 | modules = { 29 | getsize = { 30 | sources = { 31 | "getsize.c", 32 | "compat.c", 33 | "compat_50103.c", 34 | "compat_50200.c", 35 | "compat_50300.c", 36 | "compat_50400.c", 37 | "compat_50204.c", 38 | "compat_50301.c", 39 | "compat_50302.c", 40 | "compat_50401.c", 41 | "compat_50402.c", 42 | "compat_50403.c", 43 | } 44 | } 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /rockspecs/getsize-0.8-1.rockspec: -------------------------------------------------------------------------------- 1 | package="getsize" 2 | version="0.8-1" 3 | 4 | source = { 5 | url = "https://github.com/siffiejoe/lua-getsize/archive/v0.8.zip", 6 | dir = "lua-getsize-0.8", 7 | } 8 | 9 | description = { 10 | summary = "Calculates the size of a Lua object.", 11 | detailed = [[ 12 | Calculates the size of a Lua object by poking in the Lua 13 | internals. Works for PUC-Rio Lua 5.1, 5.2, 5.3, and 5.4, 14 | but _not_ for LuaJIT. 15 | ]], 16 | homepage = "http://code.matthewwild.co.uk/lua-getsize/", 17 | license = "MIT/X11, MIT", 18 | maintainer = "Philipp Janda ", 19 | } 20 | 21 | dependencies = { 22 | -- Uses Lua internals. Includes support for Lua 5.1, 5.2, 5.3, and 5.4. 23 | "lua >= 5.1, < 5.5" 24 | } 25 | 26 | build = { 27 | type = "builtin", 28 | modules = { 29 | getsize = { 30 | sources = { 31 | "getsize.c", 32 | "compat.c", 33 | "compat_50103.c", 34 | "compat_50200.c", 35 | "compat_50300.c", 36 | "compat_50400.c", 37 | "compat_50204.c", 38 | "compat_50301.c", 39 | "compat_50302.c", 40 | "compat_50401.c", 41 | "compat_50402.c", 42 | "compat_50403.c", 43 | } 44 | } 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /tests/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # simple bash script to download and build multiple different lua 4 | # versions, and compile the getsize module for each of them 5 | 6 | LUA_VERSIONS=( 5.1.4 5.1.5 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.4.3 5.4.4 ) 7 | PLATFORM=linux 8 | 9 | log() { 10 | echo -n -e "\033[36m" >&2 11 | echo -n "# $@" >&2 12 | echo -e "\033[0m" >&2 13 | } 14 | 15 | x() { 16 | log "$@" 17 | "$@" 18 | } 19 | 20 | build() { 21 | x gcc -Wall -Wextra -O2 -fpic -I"tests/lua-$1" -shared -o "tests/lua-$1/getsize.so" \ 22 | getsize.c compat.c compat_*.c 23 | } 24 | 25 | for v in "${LUA_VERSIONS[@]}"; do 26 | x wget -q -nc http://www.lua.org/ftp/lua-"${v}".tar.gz 27 | x tar xzf lua-"${v}".tar.gz 28 | log "(cd lua-${v} && make $PLATFORM)" 29 | (cd lua-"${v}" && make "$PLATFORM" && cp src/lua src/lua.h src/lauxlib.h src/luaconf.h .) 30 | (cd .. && build "$v") 31 | done 32 | 33 | -------------------------------------------------------------------------------- /tests/mem.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | 3 | local N = 1024*128 4 | 5 | local maj, min = assert( _VERSION:match( "(%d)%.(%d)$" ) ) 6 | package.cpath = "./?-"..maj..min..".so;"..package.cpath 7 | 8 | local loadstring = loadstring or load 9 | local newproxy = newproxy 10 | if not newproxy then 11 | local ok, np = pcall( require, "newproxy" ) 12 | if ok then newproxy = np end 13 | end 14 | 15 | local function create( n, f, ... ) 16 | local t = {} 17 | for i = 1, n do 18 | t[ i ] = f( ... ) 19 | end 20 | return t 21 | end 22 | 23 | local function create_strings( n, len ) 24 | local t = {} 25 | local maxlen = #tostring( n ) 26 | assert( len > maxlen ) 27 | local fmt = "%"..len.."s" 28 | for i = 1, n do 29 | t[ i ] = string.format( fmt, i ) 30 | end 31 | return t 32 | end 33 | 34 | local function create_arrays( n, i ) 35 | local s = '' 36 | if i >= 1 then 37 | s = '""' .. string.rep( ', ""', i-1 ) 38 | end 39 | local code = "return { " .. s .. " }" 40 | local f = assert( loadstring( code ) ) 41 | return create( n, f ) 42 | end 43 | 44 | local function create_records( n, i ) 45 | local x = {} 46 | for j = 1, i do 47 | x[ j ] = '["'..j..'"]=true' 48 | end 49 | local code = "return { "..table.concat( x, ", " ).. " }" 50 | local f = assert( loadstring( code ) ) 51 | return create( n, f ) 52 | end 53 | 54 | local function create_closures( n, i ) 55 | local f 56 | if i < 1 then 57 | f = assert( loadstring( "return function() return end" ) ) 58 | else 59 | local locs = {} 60 | for j = 1, i do 61 | locs[ j ] = "_"..j 62 | end 63 | locs = table.concat( locs, ", " ) 64 | local code = [[ 65 | local ]] .. locs .. [[ = ... 66 | return function() return ]] .. locs .. [[ end ]] 67 | f = assert( loadstring( code ) ) 68 | end 69 | return create( n, f ) 70 | end 71 | 72 | local function create_threads( n ) 73 | local function f() end 74 | return create( n, coroutine.create, f ) 75 | end 76 | 77 | local function create_udatas( n ) 78 | return create( n, newproxy ) 79 | end 80 | 81 | 82 | local function measure( s, f, ... ) 83 | local gc = collectgarbage 84 | gc( "stop" ) 85 | local t = f( N, ... ) 86 | gc( "restart" ) 87 | gc() gc() 88 | local full = gc( "count" ) 89 | for i = 1, #t do 90 | t[ i ] = false 91 | end 92 | gc() gc() 93 | local clean = gc( "count" ) 94 | print( s, (full - clean)*1024/N, "bytes" ) 95 | end 96 | 97 | 98 | measure( "array(0):", create_arrays, 0 ) 99 | measure( "array(1):", create_arrays, 1 ) 100 | measure( "array(2):", create_arrays, 2 ) 101 | measure( "array(3):", create_arrays, 3 ) 102 | measure( "array(4):", create_arrays, 4 ) 103 | measure( "array(5):", create_arrays, 5 ) 104 | measure( "array(10):", create_arrays, 10 ) 105 | measure( "record(0):", create_records, 0 ) 106 | measure( "record(1):", create_records, 1 ) 107 | measure( "record(2):", create_records, 2 ) 108 | measure( "record(3):", create_records, 3 ) 109 | measure( "record(4):", create_records, 4 ) 110 | measure( "record(5):", create_records, 5 ) 111 | measure( "record(10):", create_records, 10 ) 112 | measure( "closure(0):", create_closures, 0 ) 113 | measure( "closure(1):", create_closures, 1 ) 114 | measure( "closure(2):", create_closures, 2 ) 115 | measure( "closure(3):", create_closures, 3 ) 116 | measure( "closure(4):", create_closures, 4 ) 117 | measure( "closure(5):", create_closures, 5 ) 118 | measure( "closure(10):", create_closures, 10 ) 119 | measure( "thread: ", create_threads ) 120 | if newproxy then 121 | measure( "udata: ", create_udatas ) 122 | end 123 | measure( "string(10):", create_strings, 10 ) 124 | measure( "string(15):", create_strings, 15 ) 125 | measure( "string(20):", create_strings, 20 ) 126 | measure( "string(25):", create_strings, 25 ) 127 | measure( "string(30):", create_strings, 30 ) 128 | measure( "string(35):", create_strings, 35 ) 129 | measure( "string(40):", create_strings, 40 ) 130 | measure( "string(45):", create_strings, 45 ) 131 | measure( "string(50):", create_strings, 50 ) 132 | measure( "string(60):", create_strings, 60 ) 133 | 134 | -------------------------------------------------------------------------------- /tests/test.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | 3 | local maj,min = assert( _VERSION:match( "(%d+)%.(%d+)$" ) ) 4 | package.cpath = "./?.so;../?-"..maj..min..".so;"..package.cpath 5 | local size = require( "getsize" ) 6 | local dummy 7 | 8 | local function f1() 9 | coroutine.yield() 10 | end 11 | 12 | local function f2() 13 | f1() 14 | end 15 | 16 | local function f3() 17 | return 18 | end 19 | 20 | local function f4() 21 | return size 22 | end 23 | 24 | local function f5() 25 | return size, dummy 26 | end 27 | 28 | local th = coroutine.create( function() f2() end ) 29 | coroutine.resume( th ) 30 | 31 | 32 | print( "nil", size( nil, "V" ), size( nil ) ) 33 | print( "2.5", size( 2.5, "V" ), size( 2.5 ) ) 34 | print( "300", size( 300, "V" ), size( 300 ) ) 35 | print( "true", size( true, "V" ), size( true ) ) 36 | print( "false", size( false, "V" ), size( false ) ) 37 | print( "{}", size( {} ) ) 38 | print( "{ 1 }", size( { 1 } ) ) 39 | print( "{ 1, 2 }", size( { 1, 2 } ) ) 40 | print( "{ x=1 }", size( { x=1 } ) ) 41 | print( "{ x=1, y=2 }", size( { x=1, y=2 } ) ) 42 | print( "{ x=1, y=2, z=3 }", size( { x=1, y=2, z=3 } ) ) 43 | print( "io.stdout", size( io.stdout ) ) 44 | print( "print", size( print, "V" ), size( print ) ) 45 | print( "size", size( size ) ) 46 | print( "[0 upvalues]", size( f3 ) ) 47 | print( "[1 upvalue]", size( f4 ) ) 48 | print( "[2 upvalues]", size( f5 ) ) 49 | print( "[0 upvalues(U)]", size( f3, "U" ) ) 50 | print( "[1 upvalue(U)]", size( f4, "U" ) ) 51 | print( "[2 upvalues(U)]", size( f5, "U" ) ) 52 | print( "[0 upvalues(p)]", size( f3, "p" ) ) 53 | print( "[1 upvalue(p)]", size( f4, "p" ) ) 54 | print( "[2 upvalues(p)]", size( f5, "p" ) ) 55 | print( "[used thread]", size( th ) ) 56 | th = coroutine.create( function() end ) 57 | print( "[new thread]", size( th ) ) 58 | print( "[30 chars]", size( ("x"):rep( 30 ) ) ) 59 | print( "[60 chars]", size( ("y"):rep( 60 ) ) ) 60 | 61 | --------------------------------------------------------------------------------