├── Makefile ├── lua_src ├── lauxlib.h ├── lua.h └── luaconf.h ├── readme.md ├── sayhi.lua ├── test.lua └── time.c /Makefile: -------------------------------------------------------------------------------- 1 | all: time.so 2 | 3 | time.so: time.c 4 | cc -bundle -undefined dynamic_lookup -o time.so time.c -Ilua_src 5 | -------------------------------------------------------------------------------- /lua_src/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h,v 1.128 2014/10/29 16:11:17 roberto Exp $ 3 | ** Auxiliary functions for building Lua libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lauxlib_h 9 | #define lauxlib_h 10 | 11 | 12 | #include 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | 18 | 19 | /* extra error code for 'luaL_load' */ 20 | #define LUA_ERRFILE (LUA_ERRERR+1) 21 | 22 | 23 | typedef struct luaL_Reg { 24 | const char *name; 25 | lua_CFunction func; 26 | } luaL_Reg; 27 | 28 | 29 | #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) 30 | 31 | LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); 32 | #define luaL_checkversion(L) \ 33 | luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) 34 | 35 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 36 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 37 | LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 38 | LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); 39 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, 40 | size_t *l); 41 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, 42 | const char *def, size_t *l); 43 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); 44 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); 45 | 46 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); 47 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, 48 | lua_Integer def); 49 | 50 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 51 | LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); 52 | LUALIB_API void (luaL_checkany) (lua_State *L, int arg); 53 | 54 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 55 | LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 56 | LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 57 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 58 | 59 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); 60 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 61 | 62 | LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, 63 | const char *const lst[]); 64 | 65 | LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 66 | LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 67 | 68 | /* pre-defined references */ 69 | #define LUA_NOREF (-2) 70 | #define LUA_REFNIL (-1) 71 | 72 | LUALIB_API int (luaL_ref) (lua_State *L, int t); 73 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 74 | 75 | LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 76 | const char *mode); 77 | 78 | #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 79 | 80 | LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 81 | const char *name, const char *mode); 82 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 83 | 84 | LUALIB_API lua_State *(luaL_newstate) (void); 85 | 86 | LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); 87 | 88 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 89 | const char *r); 90 | 91 | LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 92 | 93 | LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 94 | 95 | LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 96 | const char *msg, int level); 97 | 98 | LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 99 | lua_CFunction openf, int glb); 100 | 101 | /* 102 | ** =============================================================== 103 | ** some useful macros 104 | ** =============================================================== 105 | */ 106 | 107 | 108 | #define luaL_newlibtable(L,l) \ 109 | lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 110 | 111 | #define luaL_newlib(L,l) \ 112 | (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 113 | 114 | #define luaL_argcheck(L, cond,arg,extramsg) \ 115 | ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) 116 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 117 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 118 | 119 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 120 | 121 | #define luaL_dofile(L, fn) \ 122 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 123 | 124 | #define luaL_dostring(L, s) \ 125 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 126 | 127 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 128 | 129 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 130 | 131 | #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 132 | 133 | 134 | /* 135 | ** {====================================================== 136 | ** Generic Buffer manipulation 137 | ** ======================================================= 138 | */ 139 | 140 | typedef struct luaL_Buffer { 141 | char *b; /* buffer address */ 142 | size_t size; /* buffer size */ 143 | size_t n; /* number of characters in buffer */ 144 | lua_State *L; 145 | char initb[LUAL_BUFFERSIZE]; /* initial buffer */ 146 | } luaL_Buffer; 147 | 148 | 149 | #define luaL_addchar(B,c) \ 150 | ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 151 | ((B)->b[(B)->n++] = (c))) 152 | 153 | #define luaL_addsize(B,s) ((B)->n += (s)) 154 | 155 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 156 | LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 157 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 158 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 159 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 160 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 161 | LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 162 | LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 163 | 164 | #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 165 | 166 | /* }====================================================== */ 167 | 168 | 169 | 170 | /* 171 | ** {====================================================== 172 | ** File handles for IO library 173 | ** ======================================================= 174 | */ 175 | 176 | /* 177 | ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 178 | ** initial structure 'luaL_Stream' (it may contain other fields 179 | ** after that initial structure). 180 | */ 181 | 182 | #define LUA_FILEHANDLE "FILE*" 183 | 184 | 185 | typedef struct luaL_Stream { 186 | FILE *f; /* stream (NULL for incompletely created streams) */ 187 | lua_CFunction closef; /* to close stream (NULL for closed streams) */ 188 | } luaL_Stream; 189 | 190 | /* }====================================================== */ 191 | 192 | 193 | 194 | /* compatibility with old module system */ 195 | #if defined(LUA_COMPAT_MODULE) 196 | 197 | LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, 198 | int sizehint); 199 | LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, 200 | const luaL_Reg *l, int nup); 201 | 202 | #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) 203 | 204 | #endif 205 | 206 | 207 | /* 208 | ** {================================================================== 209 | ** "Abstraction Layer" for basic report of messages and errors 210 | ** =================================================================== 211 | */ 212 | 213 | /* print a string */ 214 | #if !defined(lua_writestring) 215 | #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) 216 | #endif 217 | 218 | /* print a newline and flush the output */ 219 | #if !defined(lua_writeline) 220 | #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) 221 | #endif 222 | 223 | /* print an error message */ 224 | #if !defined(lua_writestringerror) 225 | #define lua_writestringerror(s,p) \ 226 | (fprintf(stderr, (s), (p)), fflush(stderr)) 227 | #endif 228 | 229 | /* }================================================================== */ 230 | 231 | 232 | /* 233 | ** {============================================================ 234 | ** Compatibility with deprecated conversions 235 | ** ============================================================= 236 | */ 237 | #if defined(LUA_COMPAT_APIINTCASTS) 238 | 239 | #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) 240 | #define luaL_optunsigned(L,a,d) \ 241 | ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) 242 | 243 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 244 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 245 | 246 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 247 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 248 | 249 | #endif 250 | /* }============================================================ */ 251 | 252 | 253 | 254 | #endif 255 | 256 | 257 | -------------------------------------------------------------------------------- /lua_src/lua.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lua.h,v 1.328 2015/06/03 13:03:38 roberto Exp $ 3 | ** Lua - A Scripting Language 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 5 | ** See Copyright Notice at the end of this file 6 | */ 7 | 8 | 9 | #ifndef lua_h 10 | #define lua_h 11 | 12 | #include 13 | #include 14 | 15 | 16 | #include "luaconf.h" 17 | 18 | 19 | #define LUA_VERSION_MAJOR "5" 20 | #define LUA_VERSION_MINOR "3" 21 | #define LUA_VERSION_NUM 503 22 | #define LUA_VERSION_RELEASE "1" 23 | 24 | #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR 25 | #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE 26 | #define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2015 Lua.org, PUC-Rio" 27 | #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" 28 | 29 | 30 | /* mark for precompiled code ('Lua') */ 31 | #define LUA_SIGNATURE "\x1bLua" 32 | 33 | /* option for multiple returns in 'lua_pcall' and 'lua_call' */ 34 | #define LUA_MULTRET (-1) 35 | 36 | 37 | /* 38 | ** Pseudo-indices 39 | ** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty 40 | ** space after that to help overflow detection) 41 | */ 42 | #define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) 43 | #define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) 44 | 45 | 46 | /* thread status */ 47 | #define LUA_OK 0 48 | #define LUA_YIELD 1 49 | #define LUA_ERRRUN 2 50 | #define LUA_ERRSYNTAX 3 51 | #define LUA_ERRMEM 4 52 | #define LUA_ERRGCMM 5 53 | #define LUA_ERRERR 6 54 | 55 | 56 | typedef struct lua_State lua_State; 57 | 58 | 59 | /* 60 | ** basic types 61 | */ 62 | #define LUA_TNONE (-1) 63 | 64 | #define LUA_TNIL 0 65 | #define LUA_TBOOLEAN 1 66 | #define LUA_TLIGHTUSERDATA 2 67 | #define LUA_TNUMBER 3 68 | #define LUA_TSTRING 4 69 | #define LUA_TTABLE 5 70 | #define LUA_TFUNCTION 6 71 | #define LUA_TUSERDATA 7 72 | #define LUA_TTHREAD 8 73 | 74 | #define LUA_NUMTAGS 9 75 | 76 | 77 | 78 | /* minimum Lua stack available to a C function */ 79 | #define LUA_MINSTACK 20 80 | 81 | 82 | /* predefined values in the registry */ 83 | #define LUA_RIDX_MAINTHREAD 1 84 | #define LUA_RIDX_GLOBALS 2 85 | #define LUA_RIDX_LAST LUA_RIDX_GLOBALS 86 | 87 | 88 | /* type of numbers in Lua */ 89 | typedef LUA_NUMBER lua_Number; 90 | 91 | 92 | /* type for integer functions */ 93 | typedef LUA_INTEGER lua_Integer; 94 | 95 | /* unsigned integer type */ 96 | typedef LUA_UNSIGNED lua_Unsigned; 97 | 98 | /* type for continuation-function contexts */ 99 | typedef LUA_KCONTEXT lua_KContext; 100 | 101 | 102 | /* 103 | ** Type for C functions registered with Lua 104 | */ 105 | typedef int (*lua_CFunction) (lua_State *L); 106 | 107 | /* 108 | ** Type for continuation functions 109 | */ 110 | typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); 111 | 112 | 113 | /* 114 | ** Type for functions that read/write blocks when loading/dumping Lua chunks 115 | */ 116 | typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); 117 | 118 | typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); 119 | 120 | 121 | /* 122 | ** Type for memory-allocation functions 123 | */ 124 | typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); 125 | 126 | 127 | 128 | /* 129 | ** generic extra include file 130 | */ 131 | #if defined(LUA_USER_H) 132 | #include LUA_USER_H 133 | #endif 134 | 135 | 136 | /* 137 | ** RCS ident string 138 | */ 139 | extern const char lua_ident[]; 140 | 141 | 142 | /* 143 | ** state manipulation 144 | */ 145 | LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); 146 | LUA_API void (lua_close) (lua_State *L); 147 | LUA_API lua_State *(lua_newthread) (lua_State *L); 148 | 149 | LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); 150 | 151 | 152 | LUA_API const lua_Number *(lua_version) (lua_State *L); 153 | 154 | 155 | /* 156 | ** basic stack manipulation 157 | */ 158 | LUA_API int (lua_absindex) (lua_State *L, int idx); 159 | LUA_API int (lua_gettop) (lua_State *L); 160 | LUA_API void (lua_settop) (lua_State *L, int idx); 161 | LUA_API void (lua_pushvalue) (lua_State *L, int idx); 162 | LUA_API void (lua_rotate) (lua_State *L, int idx, int n); 163 | LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); 164 | LUA_API int (lua_checkstack) (lua_State *L, int n); 165 | 166 | LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); 167 | 168 | 169 | /* 170 | ** access functions (stack -> C) 171 | */ 172 | 173 | LUA_API int (lua_isnumber) (lua_State *L, int idx); 174 | LUA_API int (lua_isstring) (lua_State *L, int idx); 175 | LUA_API int (lua_iscfunction) (lua_State *L, int idx); 176 | LUA_API int (lua_isinteger) (lua_State *L, int idx); 177 | LUA_API int (lua_isuserdata) (lua_State *L, int idx); 178 | LUA_API int (lua_type) (lua_State *L, int idx); 179 | LUA_API const char *(lua_typename) (lua_State *L, int tp); 180 | 181 | LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); 182 | LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); 183 | LUA_API int (lua_toboolean) (lua_State *L, int idx); 184 | LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); 185 | LUA_API size_t (lua_rawlen) (lua_State *L, int idx); 186 | LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); 187 | LUA_API void *(lua_touserdata) (lua_State *L, int idx); 188 | LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); 189 | LUA_API const void *(lua_topointer) (lua_State *L, int idx); 190 | 191 | 192 | /* 193 | ** Comparison and arithmetic functions 194 | */ 195 | 196 | #define LUA_OPADD 0 /* ORDER TM, ORDER OP */ 197 | #define LUA_OPSUB 1 198 | #define LUA_OPMUL 2 199 | #define LUA_OPMOD 3 200 | #define LUA_OPPOW 4 201 | #define LUA_OPDIV 5 202 | #define LUA_OPIDIV 6 203 | #define LUA_OPBAND 7 204 | #define LUA_OPBOR 8 205 | #define LUA_OPBXOR 9 206 | #define LUA_OPSHL 10 207 | #define LUA_OPSHR 11 208 | #define LUA_OPUNM 12 209 | #define LUA_OPBNOT 13 210 | 211 | LUA_API void (lua_arith) (lua_State *L, int op); 212 | 213 | #define LUA_OPEQ 0 214 | #define LUA_OPLT 1 215 | #define LUA_OPLE 2 216 | 217 | LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); 218 | LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); 219 | 220 | 221 | /* 222 | ** push functions (C -> stack) 223 | */ 224 | LUA_API void (lua_pushnil) (lua_State *L); 225 | LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); 226 | LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); 227 | LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); 228 | LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); 229 | LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, 230 | va_list argp); 231 | LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); 232 | LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); 233 | LUA_API void (lua_pushboolean) (lua_State *L, int b); 234 | LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); 235 | LUA_API int (lua_pushthread) (lua_State *L); 236 | 237 | 238 | /* 239 | ** get functions (Lua -> stack) 240 | */ 241 | LUA_API int (lua_getglobal) (lua_State *L, const char *name); 242 | LUA_API int (lua_gettable) (lua_State *L, int idx); 243 | LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k); 244 | LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n); 245 | LUA_API int (lua_rawget) (lua_State *L, int idx); 246 | LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); 247 | LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); 248 | 249 | LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); 250 | LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); 251 | LUA_API int (lua_getmetatable) (lua_State *L, int objindex); 252 | LUA_API int (lua_getuservalue) (lua_State *L, int idx); 253 | 254 | 255 | /* 256 | ** set functions (stack -> Lua) 257 | */ 258 | LUA_API void (lua_setglobal) (lua_State *L, const char *name); 259 | LUA_API void (lua_settable) (lua_State *L, int idx); 260 | LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); 261 | LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n); 262 | LUA_API void (lua_rawset) (lua_State *L, int idx); 263 | LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n); 264 | LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); 265 | LUA_API int (lua_setmetatable) (lua_State *L, int objindex); 266 | LUA_API void (lua_setuservalue) (lua_State *L, int idx); 267 | 268 | 269 | /* 270 | ** 'load' and 'call' functions (load and run Lua code) 271 | */ 272 | LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, 273 | lua_KContext ctx, lua_KFunction k); 274 | #define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) 275 | 276 | LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, 277 | lua_KContext ctx, lua_KFunction k); 278 | #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) 279 | 280 | LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, 281 | const char *chunkname, const char *mode); 282 | 283 | LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); 284 | 285 | 286 | /* 287 | ** coroutine functions 288 | */ 289 | LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, 290 | lua_KFunction k); 291 | LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); 292 | LUA_API int (lua_status) (lua_State *L); 293 | LUA_API int (lua_isyieldable) (lua_State *L); 294 | 295 | #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) 296 | 297 | 298 | /* 299 | ** garbage-collection function and options 300 | */ 301 | 302 | #define LUA_GCSTOP 0 303 | #define LUA_GCRESTART 1 304 | #define LUA_GCCOLLECT 2 305 | #define LUA_GCCOUNT 3 306 | #define LUA_GCCOUNTB 4 307 | #define LUA_GCSTEP 5 308 | #define LUA_GCSETPAUSE 6 309 | #define LUA_GCSETSTEPMUL 7 310 | #define LUA_GCISRUNNING 9 311 | 312 | LUA_API int (lua_gc) (lua_State *L, int what, int data); 313 | 314 | 315 | /* 316 | ** miscellaneous functions 317 | */ 318 | 319 | LUA_API int (lua_error) (lua_State *L); 320 | 321 | LUA_API int (lua_next) (lua_State *L, int idx); 322 | 323 | LUA_API void (lua_concat) (lua_State *L, int n); 324 | LUA_API void (lua_len) (lua_State *L, int idx); 325 | 326 | LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); 327 | 328 | LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); 329 | LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); 330 | 331 | 332 | 333 | /* 334 | ** {============================================================== 335 | ** some useful macros 336 | ** =============================================================== 337 | */ 338 | 339 | #define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) 340 | 341 | #define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) 342 | #define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) 343 | 344 | #define lua_pop(L,n) lua_settop(L, -(n)-1) 345 | 346 | #define lua_newtable(L) lua_createtable(L, 0, 0) 347 | 348 | #define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) 349 | 350 | #define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) 351 | 352 | #define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) 353 | #define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) 354 | #define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) 355 | #define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) 356 | #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) 357 | #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) 358 | #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) 359 | #define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) 360 | 361 | #define lua_pushliteral(L, s) lua_pushstring(L, "" s) 362 | 363 | #define lua_pushglobaltable(L) \ 364 | lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS) 365 | 366 | #define lua_tostring(L,i) lua_tolstring(L, (i), NULL) 367 | 368 | 369 | #define lua_insert(L,idx) lua_rotate(L, (idx), 1) 370 | 371 | #define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) 372 | 373 | #define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1)) 374 | 375 | /* }============================================================== */ 376 | 377 | 378 | /* 379 | ** {============================================================== 380 | ** compatibility macros for unsigned conversions 381 | ** =============================================================== 382 | */ 383 | #if defined(LUA_COMPAT_APIINTCASTS) 384 | 385 | #define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) 386 | #define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) 387 | #define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) 388 | 389 | #endif 390 | /* }============================================================== */ 391 | 392 | /* 393 | ** {====================================================================== 394 | ** Debug API 395 | ** ======================================================================= 396 | */ 397 | 398 | 399 | /* 400 | ** Event codes 401 | */ 402 | #define LUA_HOOKCALL 0 403 | #define LUA_HOOKRET 1 404 | #define LUA_HOOKLINE 2 405 | #define LUA_HOOKCOUNT 3 406 | #define LUA_HOOKTAILCALL 4 407 | 408 | 409 | /* 410 | ** Event masks 411 | */ 412 | #define LUA_MASKCALL (1 << LUA_HOOKCALL) 413 | #define LUA_MASKRET (1 << LUA_HOOKRET) 414 | #define LUA_MASKLINE (1 << LUA_HOOKLINE) 415 | #define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) 416 | 417 | typedef struct lua_Debug lua_Debug; /* activation record */ 418 | 419 | 420 | /* Functions to be called by the debugger in specific events */ 421 | typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); 422 | 423 | 424 | LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); 425 | LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); 426 | LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n); 427 | LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n); 428 | LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n); 429 | LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n); 430 | 431 | LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); 432 | LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, 433 | int fidx2, int n2); 434 | 435 | LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); 436 | LUA_API lua_Hook (lua_gethook) (lua_State *L); 437 | LUA_API int (lua_gethookmask) (lua_State *L); 438 | LUA_API int (lua_gethookcount) (lua_State *L); 439 | 440 | 441 | struct lua_Debug { 442 | int event; 443 | const char *name; /* (n) */ 444 | const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ 445 | const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ 446 | const char *source; /* (S) */ 447 | int currentline; /* (l) */ 448 | int linedefined; /* (S) */ 449 | int lastlinedefined; /* (S) */ 450 | unsigned char nups; /* (u) number of upvalues */ 451 | unsigned char nparams;/* (u) number of parameters */ 452 | char isvararg; /* (u) */ 453 | char istailcall; /* (t) */ 454 | char short_src[LUA_IDSIZE]; /* (S) */ 455 | /* private part */ 456 | struct CallInfo *i_ci; /* active function */ 457 | }; 458 | 459 | /* }====================================================================== */ 460 | 461 | 462 | /****************************************************************************** 463 | * Copyright (C) 1994-2015 Lua.org, PUC-Rio. 464 | * 465 | * Permission is hereby granted, free of charge, to any person obtaining 466 | * a copy of this software and associated documentation files (the 467 | * "Software"), to deal in the Software without restriction, including 468 | * without limitation the rights to use, copy, modify, merge, publish, 469 | * distribute, sublicense, and/or sell copies of the Software, and to 470 | * permit persons to whom the Software is furnished to do so, subject to 471 | * the following conditions: 472 | * 473 | * The above copyright notice and this permission notice shall be 474 | * included in all copies or substantial portions of the Software. 475 | * 476 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 477 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 478 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 479 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 480 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 481 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 482 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 483 | ******************************************************************************/ 484 | 485 | 486 | #endif 487 | -------------------------------------------------------------------------------- /lua_src/luaconf.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: luaconf.h,v 1.251 2015/05/20 17:39:23 roberto Exp $ 3 | ** Configuration file for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef luaconf_h 9 | #define luaconf_h 10 | 11 | #include 12 | #include 13 | 14 | 15 | /* 16 | ** =================================================================== 17 | ** Search for "@@" to find all configurable definitions. 18 | ** =================================================================== 19 | */ 20 | 21 | 22 | /* 23 | ** {==================================================================== 24 | ** System Configuration: macros to adapt (if needed) Lua to some 25 | ** particular platform, for instance compiling it with 32-bit numbers or 26 | ** restricting it to C89. 27 | ** ===================================================================== 28 | */ 29 | 30 | /* 31 | @@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You 32 | ** can also define LUA_32BITS in the make file, but changing here you 33 | ** ensure that all software connected to Lua will be compiled with the 34 | ** same configuration. 35 | */ 36 | /* #define LUA_32BITS */ 37 | 38 | 39 | /* 40 | @@ LUA_USE_C89 controls the use of non-ISO-C89 features. 41 | ** Define it if you want Lua to avoid the use of a few C99 features 42 | ** or Windows-specific features on Windows. 43 | */ 44 | /* #define LUA_USE_C89 */ 45 | 46 | 47 | /* 48 | ** By default, Lua on Windows use (some) specific Windows features 49 | */ 50 | #if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE) 51 | #define LUA_USE_WINDOWS /* enable goodies for regular Windows */ 52 | #endif 53 | 54 | 55 | #if defined(LUA_USE_WINDOWS) 56 | #define LUA_DL_DLL /* enable support for DLL */ 57 | #define LUA_USE_C89 /* broadly, Windows is C89 */ 58 | #endif 59 | 60 | 61 | #if defined(LUA_USE_LINUX) 62 | #define LUA_USE_POSIX 63 | #define LUA_USE_DLOPEN /* needs an extra library: -ldl */ 64 | #define LUA_USE_READLINE /* needs some extra libraries */ 65 | #endif 66 | 67 | 68 | #if defined(LUA_USE_MACOSX) 69 | #define LUA_USE_POSIX 70 | #define LUA_USE_DLOPEN /* MacOS does not need -ldl */ 71 | #define LUA_USE_READLINE /* needs an extra library: -lreadline */ 72 | #endif 73 | 74 | 75 | /* 76 | @@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for 77 | ** C89 ('long' and 'double'); Windows always has '__int64', so it does 78 | ** not need to use this case. 79 | */ 80 | #if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) 81 | #define LUA_C89_NUMBERS 82 | #endif 83 | 84 | 85 | 86 | /* 87 | @@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'. 88 | */ 89 | /* avoid undefined shifts */ 90 | #if ((INT_MAX >> 15) >> 15) >= 1 91 | #define LUAI_BITSINT 32 92 | #else 93 | /* 'int' always must have at least 16 bits */ 94 | #define LUAI_BITSINT 16 95 | #endif 96 | 97 | 98 | /* 99 | @@ LUA_INT_TYPE defines the type for Lua integers. 100 | @@ LUA_FLOAT_TYPE defines the type for Lua floats. 101 | ** Lua should work fine with any mix of these options (if supported 102 | ** by your C compiler). The usual configurations are 64-bit integers 103 | ** and 'double' (the default), 32-bit integers and 'float' (for 104 | ** restricted platforms), and 'long'/'double' (for C compilers not 105 | ** compliant with C99, which may not have support for 'long long'). 106 | */ 107 | 108 | /* predefined options for LUA_INT_TYPE */ 109 | #define LUA_INT_INT 1 110 | #define LUA_INT_LONG 2 111 | #define LUA_INT_LONGLONG 3 112 | 113 | /* predefined options for LUA_FLOAT_TYPE */ 114 | #define LUA_FLOAT_FLOAT 1 115 | #define LUA_FLOAT_DOUBLE 2 116 | #define LUA_FLOAT_LONGDOUBLE 3 117 | 118 | #if defined(LUA_32BITS) /* { */ 119 | /* 120 | ** 32-bit integers and 'float' 121 | */ 122 | #if LUAI_BITSINT >= 32 /* use 'int' if big enough */ 123 | #define LUA_INT_TYPE LUA_INT_INT 124 | #else /* otherwise use 'long' */ 125 | #define LUA_INT_TYPE LUA_INT_LONG 126 | #endif 127 | #define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT 128 | 129 | #elif defined(LUA_C89_NUMBERS) /* }{ */ 130 | /* 131 | ** largest types available for C89 ('long' and 'double') 132 | */ 133 | #define LUA_INT_TYPE LUA_INT_LONG 134 | #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE 135 | 136 | #endif /* } */ 137 | 138 | 139 | /* 140 | ** default configuration for 64-bit Lua ('long long' and 'double') 141 | */ 142 | #if !defined(LUA_INT_TYPE) 143 | #define LUA_INT_TYPE LUA_INT_LONGLONG 144 | #endif 145 | 146 | #if !defined(LUA_FLOAT_TYPE) 147 | #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE 148 | #endif /* } */ 149 | 150 | /* }================================================================== */ 151 | 152 | 153 | 154 | 155 | /* 156 | ** {================================================================== 157 | ** Configuration for Paths. 158 | ** =================================================================== 159 | */ 160 | 161 | /* 162 | @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for 163 | ** Lua libraries. 164 | @@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for 165 | ** C libraries. 166 | ** CHANGE them if your machine has a non-conventional directory 167 | ** hierarchy or if you want to install your libraries in 168 | ** non-conventional directories. 169 | */ 170 | #define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR 171 | #if defined(_WIN32) /* { */ 172 | /* 173 | ** In Windows, any exclamation mark ('!') in the path is replaced by the 174 | ** path of the directory of the executable file of the current process. 175 | */ 176 | #define LUA_LDIR "!\\lua\\" 177 | #define LUA_CDIR "!\\" 178 | #define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" 179 | #define LUA_PATH_DEFAULT \ 180 | LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ 181 | LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ 182 | LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ 183 | ".\\?.lua;" ".\\?\\init.lua" 184 | #define LUA_CPATH_DEFAULT \ 185 | LUA_CDIR"?.dll;" \ 186 | LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ 187 | LUA_CDIR"loadall.dll;" ".\\?.dll" 188 | 189 | #else /* }{ */ 190 | 191 | #define LUA_ROOT "/usr/local/" 192 | #define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" 193 | #define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" 194 | #define LUA_PATH_DEFAULT \ 195 | LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ 196 | LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ 197 | "./?.lua;" "./?/init.lua" 198 | #define LUA_CPATH_DEFAULT \ 199 | LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" 200 | #endif /* } */ 201 | 202 | 203 | /* 204 | @@ LUA_DIRSEP is the directory separator (for submodules). 205 | ** CHANGE it if your machine does not use "/" as the directory separator 206 | ** and is not Windows. (On Windows Lua automatically uses "\".) 207 | */ 208 | #if defined(_WIN32) 209 | #define LUA_DIRSEP "\\" 210 | #else 211 | #define LUA_DIRSEP "/" 212 | #endif 213 | 214 | /* }================================================================== */ 215 | 216 | 217 | /* 218 | ** {================================================================== 219 | ** Marks for exported symbols in the C code 220 | ** =================================================================== 221 | */ 222 | 223 | /* 224 | @@ LUA_API is a mark for all core API functions. 225 | @@ LUALIB_API is a mark for all auxiliary library functions. 226 | @@ LUAMOD_API is a mark for all standard library opening functions. 227 | ** CHANGE them if you need to define those functions in some special way. 228 | ** For instance, if you want to create one Windows DLL with the core and 229 | ** the libraries, you may want to use the following definition (define 230 | ** LUA_BUILD_AS_DLL to get it). 231 | */ 232 | #if defined(LUA_BUILD_AS_DLL) /* { */ 233 | 234 | #if defined(LUA_CORE) || defined(LUA_LIB) /* { */ 235 | #define LUA_API __declspec(dllexport) 236 | #else /* }{ */ 237 | #define LUA_API __declspec(dllimport) 238 | #endif /* } */ 239 | 240 | #else /* }{ */ 241 | 242 | #define LUA_API extern 243 | 244 | #endif /* } */ 245 | 246 | 247 | /* more often than not the libs go together with the core */ 248 | #define LUALIB_API LUA_API 249 | #define LUAMOD_API LUALIB_API 250 | 251 | 252 | /* 253 | @@ LUAI_FUNC is a mark for all extern functions that are not to be 254 | ** exported to outside modules. 255 | @@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables 256 | ** that are not to be exported to outside modules (LUAI_DDEF for 257 | ** definitions and LUAI_DDEC for declarations). 258 | ** CHANGE them if you need to mark them in some special way. Elf/gcc 259 | ** (versions 3.2 and later) mark them as "hidden" to optimize access 260 | ** when Lua is compiled as a shared library. Not all elf targets support 261 | ** this attribute. Unfortunately, gcc does not offer a way to check 262 | ** whether the target offers that support, and those without support 263 | ** give a warning about it. To avoid these warnings, change to the 264 | ** default definition. 265 | */ 266 | #if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ 267 | defined(__ELF__) /* { */ 268 | #define LUAI_FUNC __attribute__((visibility("hidden"))) extern 269 | #else /* }{ */ 270 | #define LUAI_FUNC extern 271 | #endif /* } */ 272 | 273 | #define LUAI_DDEC LUAI_FUNC 274 | #define LUAI_DDEF /* empty */ 275 | 276 | /* }================================================================== */ 277 | 278 | 279 | /* 280 | ** {================================================================== 281 | ** Compatibility with previous versions 282 | ** =================================================================== 283 | */ 284 | 285 | /* 286 | @@ LUA_COMPAT_5_2 controls other macros for compatibility with Lua 5.2. 287 | @@ LUA_COMPAT_5_1 controls other macros for compatibility with Lua 5.1. 288 | ** You can define it to get all options, or change specific options 289 | ** to fit your specific needs. 290 | */ 291 | #if defined(LUA_COMPAT_5_2) /* { */ 292 | 293 | /* 294 | @@ LUA_COMPAT_MATHLIB controls the presence of several deprecated 295 | ** functions in the mathematical library. 296 | */ 297 | #define LUA_COMPAT_MATHLIB 298 | 299 | /* 300 | @@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'. 301 | */ 302 | #define LUA_COMPAT_BITLIB 303 | 304 | /* 305 | @@ LUA_COMPAT_IPAIRS controls the effectiveness of the __ipairs metamethod. 306 | */ 307 | #define LUA_COMPAT_IPAIRS 308 | 309 | /* 310 | @@ LUA_COMPAT_APIINTCASTS controls the presence of macros for 311 | ** manipulating other integer types (lua_pushunsigned, lua_tounsigned, 312 | ** luaL_checkint, luaL_checklong, etc.) 313 | */ 314 | #define LUA_COMPAT_APIINTCASTS 315 | 316 | #endif /* } */ 317 | 318 | 319 | #if defined(LUA_COMPAT_5_1) /* { */ 320 | 321 | /* Incompatibilities from 5.2 -> 5.3 */ 322 | #define LUA_COMPAT_MATHLIB 323 | #define LUA_COMPAT_APIINTCASTS 324 | 325 | /* 326 | @@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'. 327 | ** You can replace it with 'table.unpack'. 328 | */ 329 | #define LUA_COMPAT_UNPACK 330 | 331 | /* 332 | @@ LUA_COMPAT_LOADERS controls the presence of table 'package.loaders'. 333 | ** You can replace it with 'package.searchers'. 334 | */ 335 | #define LUA_COMPAT_LOADERS 336 | 337 | /* 338 | @@ macro 'lua_cpcall' emulates deprecated function lua_cpcall. 339 | ** You can call your C function directly (with light C functions). 340 | */ 341 | #define lua_cpcall(L,f,u) \ 342 | (lua_pushcfunction(L, (f)), \ 343 | lua_pushlightuserdata(L,(u)), \ 344 | lua_pcall(L,1,0,0)) 345 | 346 | 347 | /* 348 | @@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library. 349 | ** You can rewrite 'log10(x)' as 'log(x, 10)'. 350 | */ 351 | #define LUA_COMPAT_LOG10 352 | 353 | /* 354 | @@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base 355 | ** library. You can rewrite 'loadstring(s)' as 'load(s)'. 356 | */ 357 | #define LUA_COMPAT_LOADSTRING 358 | 359 | /* 360 | @@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library. 361 | */ 362 | #define LUA_COMPAT_MAXN 363 | 364 | /* 365 | @@ The following macros supply trivial compatibility for some 366 | ** changes in the API. The macros themselves document how to 367 | ** change your code to avoid using them. 368 | */ 369 | #define lua_strlen(L,i) lua_rawlen(L, (i)) 370 | 371 | #define lua_objlen(L,i) lua_rawlen(L, (i)) 372 | 373 | #define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) 374 | #define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) 375 | 376 | /* 377 | @@ LUA_COMPAT_MODULE controls compatibility with previous 378 | ** module functions 'module' (Lua) and 'luaL_register' (C). 379 | */ 380 | #define LUA_COMPAT_MODULE 381 | 382 | #endif /* } */ 383 | 384 | 385 | /* 386 | @@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a 387 | @@ a float mark ('.0'). 388 | ** This macro is not on by default even in compatibility mode, 389 | ** because this is not really an incompatibility. 390 | */ 391 | /* #define LUA_COMPAT_FLOATSTRING */ 392 | 393 | /* }================================================================== */ 394 | 395 | 396 | 397 | /* 398 | ** {================================================================== 399 | ** Configuration for Numbers. 400 | ** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* 401 | ** satisfy your needs. 402 | ** =================================================================== 403 | */ 404 | 405 | /* 406 | @@ LUA_NUMBER is the floating-point type used by Lua. 407 | @@ LUAI_UACNUMBER is the result of an 'usual argument conversion' 408 | @@ over a floating number. 409 | @@ l_mathlim(x) corrects limit name 'x' to the proper float type 410 | ** by prefixing it with one of FLT/DBL/LDBL. 411 | @@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. 412 | @@ LUA_NUMBER_FMT is the format for writing floats. 413 | @@ lua_number2str converts a float to a string. 414 | @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. 415 | @@ lua_str2number converts a decimal numeric string to a number. 416 | */ 417 | 418 | #if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ 419 | 420 | #define LUA_NUMBER float 421 | 422 | #define l_mathlim(n) (FLT_##n) 423 | 424 | #define LUAI_UACNUMBER double 425 | 426 | #define LUA_NUMBER_FRMLEN "" 427 | #define LUA_NUMBER_FMT "%.7g" 428 | 429 | #define l_mathop(op) op##f 430 | 431 | #define lua_str2number(s,p) strtof((s), (p)) 432 | 433 | 434 | #elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */ 435 | 436 | #define LUA_NUMBER long double 437 | 438 | #define l_mathlim(n) (LDBL_##n) 439 | 440 | #define LUAI_UACNUMBER long double 441 | 442 | #define LUA_NUMBER_FRMLEN "L" 443 | #define LUA_NUMBER_FMT "%.19Lg" 444 | 445 | #define l_mathop(op) op##l 446 | 447 | #define lua_str2number(s,p) strtold((s), (p)) 448 | 449 | #elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */ 450 | 451 | #define LUA_NUMBER double 452 | 453 | #define l_mathlim(n) (DBL_##n) 454 | 455 | #define LUAI_UACNUMBER double 456 | 457 | #define LUA_NUMBER_FRMLEN "" 458 | #define LUA_NUMBER_FMT "%.14g" 459 | 460 | #define l_mathop(op) op 461 | 462 | #define lua_str2number(s,p) strtod((s), (p)) 463 | 464 | #else /* }{ */ 465 | 466 | #error "numeric float type not defined" 467 | 468 | #endif /* } */ 469 | 470 | 471 | #define l_floor(x) (l_mathop(floor)(x)) 472 | 473 | #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) 474 | 475 | 476 | /* 477 | @@ lua_numbertointeger converts a float number to an integer, or 478 | ** returns 0 if float is not within the range of a lua_Integer. 479 | ** (The range comparisons are tricky because of rounding. The tests 480 | ** here assume a two-complement representation, where MININTEGER always 481 | ** has an exact representation as a float; MAXINTEGER may not have one, 482 | ** and therefore its conversion to float may have an ill-defined value.) 483 | */ 484 | #define lua_numbertointeger(n,p) \ 485 | ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ 486 | (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ 487 | (*(p) = (LUA_INTEGER)(n), 1)) 488 | 489 | 490 | 491 | /* 492 | @@ LUA_INTEGER is the integer type used by Lua. 493 | ** 494 | @@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. 495 | ** 496 | @@ LUAI_UACINT is the result of an 'usual argument conversion' 497 | @@ over a lUA_INTEGER. 498 | @@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. 499 | @@ LUA_INTEGER_FMT is the format for writing integers. 500 | @@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. 501 | @@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. 502 | @@ lua_integer2str converts an integer to a string. 503 | */ 504 | 505 | 506 | /* The following definitions are good for most cases here */ 507 | 508 | #define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" 509 | #define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n)) 510 | 511 | #define LUAI_UACINT LUA_INTEGER 512 | 513 | /* 514 | ** use LUAI_UACINT here to avoid problems with promotions (which 515 | ** can turn a comparison between unsigneds into a signed comparison) 516 | */ 517 | #define LUA_UNSIGNED unsigned LUAI_UACINT 518 | 519 | 520 | /* now the variable definitions */ 521 | 522 | #if LUA_INT_TYPE == LUA_INT_INT /* { int */ 523 | 524 | #define LUA_INTEGER int 525 | #define LUA_INTEGER_FRMLEN "" 526 | 527 | #define LUA_MAXINTEGER INT_MAX 528 | #define LUA_MININTEGER INT_MIN 529 | 530 | #elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ 531 | 532 | #define LUA_INTEGER long 533 | #define LUA_INTEGER_FRMLEN "l" 534 | 535 | #define LUA_MAXINTEGER LONG_MAX 536 | #define LUA_MININTEGER LONG_MIN 537 | 538 | #elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ 539 | 540 | #if defined(LLONG_MAX) /* { */ 541 | /* use ISO C99 stuff */ 542 | 543 | #define LUA_INTEGER long long 544 | #define LUA_INTEGER_FRMLEN "ll" 545 | 546 | #define LUA_MAXINTEGER LLONG_MAX 547 | #define LUA_MININTEGER LLONG_MIN 548 | 549 | #elif defined(LUA_USE_WINDOWS) /* }{ */ 550 | /* in Windows, can use specific Windows types */ 551 | 552 | #define LUA_INTEGER __int64 553 | #define LUA_INTEGER_FRMLEN "I64" 554 | 555 | #define LUA_MAXINTEGER _I64_MAX 556 | #define LUA_MININTEGER _I64_MIN 557 | 558 | #else /* }{ */ 559 | 560 | #error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ 561 | or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)" 562 | 563 | #endif /* } */ 564 | 565 | #else /* }{ */ 566 | 567 | #error "numeric integer type not defined" 568 | 569 | #endif /* } */ 570 | 571 | /* }================================================================== */ 572 | 573 | 574 | /* 575 | ** {================================================================== 576 | ** Dependencies with C99 and other C details 577 | ** =================================================================== 578 | */ 579 | 580 | /* 581 | @@ lua_strx2number converts an hexadecimal numeric string to a number. 582 | ** In C99, 'strtod' does that conversion. Otherwise, you can 583 | ** leave 'lua_strx2number' undefined and Lua will provide its own 584 | ** implementation. 585 | */ 586 | #if !defined(LUA_USE_C89) 587 | #define lua_strx2number(s,p) lua_str2number(s,p) 588 | #endif 589 | 590 | 591 | /* 592 | @@ lua_number2strx converts a float to an hexadecimal numeric string. 593 | ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. 594 | ** Otherwise, you can leave 'lua_number2strx' undefined and Lua will 595 | ** provide its own implementation. 596 | */ 597 | #if !defined(LUA_USE_C89) 598 | #define lua_number2strx(L,b,f,n) sprintf(b,f,n) 599 | #endif 600 | 601 | 602 | /* 603 | ** 'strtof' and 'opf' variants for math functions are not valid in 604 | ** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the 605 | ** availability of these variants. ('math.h' is already included in 606 | ** all files that use these macros.) 607 | */ 608 | #if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) 609 | #undef l_mathop /* variants not available */ 610 | #undef lua_str2number 611 | #define l_mathop(op) (lua_Number)op /* no variant */ 612 | #define lua_str2number(s,p) ((lua_Number)strtod((s), (p))) 613 | #endif 614 | 615 | 616 | /* 617 | @@ LUA_KCONTEXT is the type of the context ('ctx') for continuation 618 | ** functions. It must be a numerical type; Lua will use 'intptr_t' if 619 | ** available, otherwise it will use 'ptrdiff_t' (the nearest thing to 620 | ** 'intptr_t' in C89) 621 | */ 622 | #define LUA_KCONTEXT ptrdiff_t 623 | 624 | #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ 625 | __STDC_VERSION__ >= 199901L 626 | #include 627 | #if defined(INTPTR_MAX) /* even in C99 this type is optional */ 628 | #undef LUA_KCONTEXT 629 | #define LUA_KCONTEXT intptr_t 630 | #endif 631 | #endif 632 | 633 | 634 | /* 635 | @@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). 636 | ** Change that if you do not want to use C locales. (Code using this 637 | ** macro must include header 'locale.h'.) 638 | */ 639 | #if !defined(lua_getlocaledecpoint) 640 | #define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) 641 | #endif 642 | 643 | /* }================================================================== */ 644 | 645 | 646 | /* 647 | ** {================================================================== 648 | ** Language Variations 649 | ** ===================================================================== 650 | */ 651 | 652 | /* 653 | @@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some 654 | ** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from 655 | ** numbers to strings. Define LUA_NOCVTS2N to turn off automatic 656 | ** coercion from strings to numbers. 657 | */ 658 | /* #define LUA_NOCVTN2S */ 659 | /* #define LUA_NOCVTS2N */ 660 | 661 | 662 | /* 663 | @@ LUA_USE_APICHECK turns on several consistency checks on the C API. 664 | ** Define it as a help when debugging C code. 665 | */ 666 | #if defined(LUA_USE_APICHECK) 667 | #include 668 | #define luai_apicheck(l,e) assert(e) 669 | #endif 670 | 671 | /* }================================================================== */ 672 | 673 | 674 | /* 675 | ** {================================================================== 676 | ** Macros that affect the API and must be stable (that is, must be the 677 | ** same when you compile Lua and when you compile code that links to 678 | ** Lua). You probably do not want/need to change them. 679 | ** ===================================================================== 680 | */ 681 | 682 | /* 683 | @@ LUAI_MAXSTACK limits the size of the Lua stack. 684 | ** CHANGE it if you need a different limit. This limit is arbitrary; 685 | ** its only purpose is to stop Lua from consuming unlimited stack 686 | ** space (and to reserve some numbers for pseudo-indices). 687 | */ 688 | #if LUAI_BITSINT >= 32 689 | #define LUAI_MAXSTACK 1000000 690 | #else 691 | #define LUAI_MAXSTACK 15000 692 | #endif 693 | 694 | 695 | /* 696 | @@ LUA_EXTRASPACE defines the size of a raw memory area associated with 697 | ** a Lua state with very fast access. 698 | ** CHANGE it if you need a different size. 699 | */ 700 | #define LUA_EXTRASPACE (sizeof(void *)) 701 | 702 | 703 | /* 704 | @@ LUA_IDSIZE gives the maximum size for the description of the source 705 | @@ of a function in debug information. 706 | ** CHANGE it if you want a different size. 707 | */ 708 | #define LUA_IDSIZE 60 709 | 710 | 711 | /* 712 | @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. 713 | ** CHANGE it if it uses too much C-stack space. (For long double, 714 | ** 'string.format("%.99f", 1e4932)' needs ~5030 bytes, so a 715 | ** smaller buffer would force a memory allocation for each call to 716 | ** 'string.format'.) 717 | */ 718 | #if defined(LUA_FLOAT_LONGDOUBLE) 719 | #define LUAL_BUFFERSIZE 8192 720 | #else 721 | #define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) 722 | #endif 723 | 724 | /* }================================================================== */ 725 | 726 | 727 | /* 728 | @@ LUA_QL describes how error messages quote program elements. 729 | ** Lua does not use these macros anymore; they are here for 730 | ** compatibility only. 731 | */ 732 | #define LUA_QL(x) "'" x "'" 733 | #define LUA_QS LUA_QL("%s") 734 | 735 | 736 | 737 | 738 | /* =================================================================== */ 739 | 740 | /* 741 | ** Local configuration. You can use this space to add your redefinitions 742 | ** without modifying the main part of the file. 743 | */ 744 | 745 | 746 | 747 | 748 | 749 | #endif 750 | 751 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Writing Lua modules in C 2 | 3 | This repo contains a small demo file, `time.c` which can be compiled on Mac OS X 4 | into the shared object file `time.so` and used as a module in Lua. If you're 5 | working on a different platform, the notes 6 | [here](http://lua-users.org/wiki/BuildingModules) explain how to compile in many 7 | different settings. 8 | 9 | ## Compiling and using 10 | 11 | Run 12 | 13 | $ make 14 | 15 | in the shell to build `time.so`. Then from Lua, use code like this: 16 | 17 | local time = require 'time' 18 | local start_time = time.get_time() 19 | -- Do some stuff you want to time. 20 | local end_time = time.get_time() 21 | print((end_time - start_time) .. ' seconds have passed) 22 | 23 | ## Video 24 | 25 | This small repo was built during a video tutorial of the basics of Lua's C 26 | interface. That video is available 27 | [here](https://www.youtube.com/watch?v=UiZ051A22h8). 28 | 29 | ## Notes on the time confusion in the video 30 | 31 | If you watch the video, at one point I'm confused about why the return value of 32 | `time.get_time()` doesn't match the return value of `os.time()`. This is because 33 | `os.time()` gives the number of seconds passed since the 34 | beginning of [the epoch](https://en.wikipedia.org/wiki/Unix_time), 35 | while `time.get_time()` returns the number of seconds elapsed from an 36 | *unspecified* initial point. 37 | 38 | Why would you care about an unspecified initial time point? 39 | This is useful for measuring time *intervals*; disassociating a 40 | high-resolution clock from the real-world time can get around tricky cases such 41 | as a user changing the system time. 42 | -------------------------------------------------------------------------------- /sayhi.lua: -------------------------------------------------------------------------------- 1 | -- sayhi.lua 2 | 3 | local sayhi = {} 4 | 5 | function sayhi.go() 6 | print('hiiiiiii!!!!') 7 | end 8 | 9 | return sayhi 10 | -------------------------------------------------------------------------------- /test.lua: -------------------------------------------------------------------------------- 1 | local s = require 'sayhi' 2 | 3 | 4 | s.go() 5 | -------------------------------------------------------------------------------- /time.c: -------------------------------------------------------------------------------- 1 | // time.c 2 | // 3 | // A Lua module written in C to provide the time in subsecond resolution. 4 | // 5 | 6 | #include "lua.h" 7 | #include "lauxlib.h" 8 | 9 | #include 10 | 11 | #ifdef __APPLE__ 12 | #include 13 | #include 14 | #else 15 | #include 16 | #endif 17 | 18 | static double now() { 19 | #ifdef __APPLE__ 20 | static int did_initialize = FALSE; 21 | static mach_timebase_info_data_t timebase_info; 22 | if (!did_initialize) { 23 | mach_timebase_info(&timebase_info); 24 | did_initialize = TRUE; 25 | } 26 | 27 | uint64_t abs_time = mach_absolute_time(); 28 | return (double)abs_time * timebase_info.numer / timebase_info.denom / 1e9f; 29 | #else 30 | struct timespec time; 31 | clock_gettime(CLOCK_MONOTONIC, &time); 32 | return (double)time.tv_sec + (double)time.tv_nsec / 1e9f; 33 | #endif 34 | } 35 | 36 | int get_time(lua_State *L) { 37 | double t = now(); 38 | lua_pushnumber(L, t); 39 | return 1; // Number of values to return that are on the stack. 40 | } 41 | 42 | int luaopen_time(lua_State *L) { 43 | luaL_Reg fns[] = { 44 | {"get_time", get_time}, 45 | {NULL, NULL} 46 | }; 47 | luaL_newlib(L, fns); 48 | //luaL_register(L, "time", fns); 49 | return 1; // Number of Lua-facing return values on the Lua stack in L. 50 | } 51 | --------------------------------------------------------------------------------