Mercurial > vba-clojure
changeset 11:27763b933818
raise lua sources up one level
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 11:07:39 -0600 |
parents | 48b74a4e4692 |
children | 3c287cb0ace6 |
files | src/lua/Makefile.am src/lua/lapi.c src/lua/lapi.h src/lua/lauxlib.c src/lua/lauxlib.h src/lua/lbaselib.c src/lua/lcode.c src/lua/lcode.h src/lua/ldblib.c src/lua/ldebug.c src/lua/ldebug.h src/lua/ldo.c src/lua/ldo.h src/lua/ldump.c src/lua/lfunc.c src/lua/lfunc.h src/lua/lgc.c src/lua/lgc.h src/lua/linit.c src/lua/liolib.c src/lua/llex.c src/lua/llex.h src/lua/llimits.h src/lua/lmathlib.c src/lua/lmem.c src/lua/lmem.h src/lua/loadlib.c src/lua/lobject.c src/lua/lobject.h src/lua/lopcodes.c src/lua/lopcodes.h src/lua/loslib.c src/lua/lparser.c src/lua/lparser.h src/lua/lstate.c src/lua/lstate.h src/lua/lstring.c src/lua/lstring.h src/lua/lstrlib.c src/lua/ltable.c src/lua/ltable.h src/lua/ltablib.c src/lua/ltm.c src/lua/ltm.h src/lua/lua.h src/lua/luaconf.h src/lua/lualib.h src/lua/lundump.c src/lua/lundump.h src/lua/lvm.c src/lua/lvm.h src/lua/lzio.c src/lua/lzio.h src/lua/print.c src/lua/src/lapi.c src/lua/src/lapi.h src/lua/src/lauxlib.c src/lua/src/lauxlib.h src/lua/src/lbaselib.c src/lua/src/lcode.c src/lua/src/lcode.h src/lua/src/ldblib.c src/lua/src/ldebug.c src/lua/src/ldebug.h src/lua/src/ldo.c src/lua/src/ldo.h src/lua/src/ldump.c src/lua/src/lfunc.c src/lua/src/lfunc.h src/lua/src/lgc.c src/lua/src/lgc.h src/lua/src/linit.c src/lua/src/liolib.c src/lua/src/llex.c src/lua/src/llex.h src/lua/src/llimits.h src/lua/src/lmathlib.c src/lua/src/lmem.c src/lua/src/lmem.h src/lua/src/loadlib.c src/lua/src/lobject.c src/lua/src/lobject.h src/lua/src/lopcodes.c src/lua/src/lopcodes.h src/lua/src/loslib.c src/lua/src/lparser.c src/lua/src/lparser.h src/lua/src/lstate.c src/lua/src/lstate.h src/lua/src/lstring.c src/lua/src/lstring.h src/lua/src/lstrlib.c src/lua/src/ltable.c src/lua/src/ltable.h src/lua/src/ltablib.c src/lua/src/ltm.c src/lua/src/ltm.h src/lua/src/lua.h src/lua/src/luaconf.h src/lua/src/lualib.h src/lua/src/lundump.c src/lua/src/lundump.h src/lua/src/lvm.c src/lua/src/lvm.h src/lua/src/lzio.c src/lua/src/lzio.h src/lua/src/print.c |
diffstat | 107 files changed, 16420 insertions(+), 16420 deletions(-) [+] |
line wrap: on
line diff
1.1 --- a/src/lua/Makefile.am Sat Mar 03 11:04:56 2012 -0600 1.2 +++ b/src/lua/Makefile.am Sat Mar 03 11:07:39 2012 -0600 1.3 @@ -1,56 +1,56 @@ 1.4 noinst_LIBRARIES = libgblua.a 1.5 1.6 libgblua_a_SOURCES = \ 1.7 - src/lapi.c \ 1.8 - src/lapi.h \ 1.9 - src/lauxlib.c \ 1.10 - src/lauxlib.h \ 1.11 - src/lbaselib.c \ 1.12 - src/lcode.c \ 1.13 - src/lcode.h \ 1.14 - src/ldblib.c \ 1.15 - src/ldebug.c \ 1.16 - src/ldebug.h \ 1.17 - src/ldo.c \ 1.18 - src/ldo.h \ 1.19 - src/ldump.c \ 1.20 - src/lfunc.c \ 1.21 - src/lfunc.h \ 1.22 - src/lgc.c \ 1.23 - src/lgc.h \ 1.24 - src/linit.c \ 1.25 - src/liolib.c \ 1.26 - src/llex.c \ 1.27 - src/llex.h \ 1.28 - src/llimit.h \ 1.29 - src/lmathlib.c \ 1.30 - src/lmem.c \ 1.31 - src/lmem.h \ 1.32 - src/loadlib.c \ 1.33 - src/lobject.c \ 1.34 - src/lobject.h \ 1.35 - src/lopcodes.c \ 1.36 - src/lopcodes.h \ 1.37 - src/loslib.c \ 1.38 - src/lparser.c \ 1.39 - src/lparser.h \ 1.40 - src/lstate.c \ 1.41 - src/lstate.h \ 1.42 - src/lstring.c \ 1.43 - src/lstring.h \ 1.44 - src/lstrlib.c \ 1.45 - src/ltable.c \ 1.46 - src/ltable.h \ 1.47 - src/ltablib.c \ 1.48 - src/ltm.c \ 1.49 - src/ltm.h \ 1.50 - src/lua.h \ 1.51 - src/luaconf.h \ 1.52 - src/lualib.h \ 1.53 - src/lundump.c \ 1.54 - src/lundump.h \ 1.55 - src/lvm.c \ 1.56 - src/lvm.h \ 1.57 - src/lzio.c \ 1.58 - src/lzio.h \ 1.59 - src/print.c 1.60 + lapi.c \ 1.61 + lapi.h \ 1.62 + lauxlib.c \ 1.63 + lauxlib.h \ 1.64 + lbaselib.c \ 1.65 + lcode.c \ 1.66 + lcode.h \ 1.67 + ldblib.c \ 1.68 + ldebug.c \ 1.69 + ldebug.h \ 1.70 + ldo.c \ 1.71 + ldo.h \ 1.72 + ldump.c \ 1.73 + lfunc.c \ 1.74 + lfunc.h \ 1.75 + lgc.c \ 1.76 + lgc.h \ 1.77 + linit.c \ 1.78 + liolib.c \ 1.79 + llex.c \ 1.80 + llex.h \ 1.81 + llimit.h \ 1.82 + lmathlib.c \ 1.83 + lmem.c \ 1.84 + lmem.h \ 1.85 + loadlib.c \ 1.86 + lobject.c \ 1.87 + lobject.h \ 1.88 + lopcodes.c \ 1.89 + lopcodes.h \ 1.90 + loslib.c \ 1.91 + lparser.c \ 1.92 + lparser.h \ 1.93 + lstate.c \ 1.94 + lstate.h \ 1.95 + lstring.c \ 1.96 + lstring.h \ 1.97 + lstrlib.c \ 1.98 + ltable.c \ 1.99 + ltable.h \ 1.100 + ltablib.c \ 1.101 + ltm.c \ 1.102 + ltm.h \ 1.103 + lua.h \ 1.104 + luaconf.h \ 1.105 + lualib.h \ 1.106 + lundump.c \ 1.107 + lundump.h \ 1.108 + lvm.c \ 1.109 + lvm.h \ 1.110 + lzio.c \ 1.111 + lzio.h \ 1.112 + print.c
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/lua/lapi.c Sat Mar 03 11:07:39 2012 -0600 2.3 @@ -0,0 +1,1087 @@ 2.4 +/* 2.5 +** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $ 2.6 +** Lua API 2.7 +** See Copyright Notice in lua.h 2.8 +*/ 2.9 + 2.10 + 2.11 +#include <assert.h> 2.12 +#include <math.h> 2.13 +#include <stdarg.h> 2.14 +#include <string.h> 2.15 + 2.16 +#define lapi_c 2.17 +#define LUA_CORE 2.18 + 2.19 +#include "lua.h" 2.20 + 2.21 +#include "lapi.h" 2.22 +#include "ldebug.h" 2.23 +#include "ldo.h" 2.24 +#include "lfunc.h" 2.25 +#include "lgc.h" 2.26 +#include "lmem.h" 2.27 +#include "lobject.h" 2.28 +#include "lstate.h" 2.29 +#include "lstring.h" 2.30 +#include "ltable.h" 2.31 +#include "ltm.h" 2.32 +#include "lundump.h" 2.33 +#include "lvm.h" 2.34 + 2.35 + 2.36 + 2.37 +const char lua_ident[] = 2.38 + "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n" 2.39 + "$Authors: " LUA_AUTHORS " $\n" 2.40 + "$URL: www.lua.org $\n"; 2.41 + 2.42 + 2.43 + 2.44 +#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base)) 2.45 + 2.46 +#define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject) 2.47 + 2.48 +#define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;} 2.49 + 2.50 + 2.51 + 2.52 +static TValue *index2adr (lua_State *L, int idx) { 2.53 + if (idx > 0) { 2.54 + TValue *o = L->base + (idx - 1); 2.55 + api_check(L, idx <= L->ci->top - L->base); 2.56 + if (o >= L->top) return cast(TValue *, luaO_nilobject); 2.57 + else return o; 2.58 + } 2.59 + else if (idx > LUA_REGISTRYINDEX) { 2.60 + api_check(L, idx != 0 && -idx <= L->top - L->base); 2.61 + return L->top + idx; 2.62 + } 2.63 + else switch (idx) { /* pseudo-indices */ 2.64 + case LUA_REGISTRYINDEX: return registry(L); 2.65 + case LUA_ENVIRONINDEX: { 2.66 + Closure *func = curr_func(L); 2.67 + sethvalue(L, &L->env, func->c.env); 2.68 + return &L->env; 2.69 + } 2.70 + case LUA_GLOBALSINDEX: return gt(L); 2.71 + default: { 2.72 + Closure *func = curr_func(L); 2.73 + idx = LUA_GLOBALSINDEX - idx; 2.74 + return (idx <= func->c.nupvalues) 2.75 + ? &func->c.upvalue[idx-1] 2.76 + : cast(TValue *, luaO_nilobject); 2.77 + } 2.78 + } 2.79 +} 2.80 + 2.81 + 2.82 +static Table *getcurrenv (lua_State *L) { 2.83 + if (L->ci == L->base_ci) /* no enclosing function? */ 2.84 + return hvalue(gt(L)); /* use global table as environment */ 2.85 + else { 2.86 + Closure *func = curr_func(L); 2.87 + return func->c.env; 2.88 + } 2.89 +} 2.90 + 2.91 + 2.92 +void luaA_pushobject (lua_State *L, const TValue *o) { 2.93 + setobj2s(L, L->top, o); 2.94 + api_incr_top(L); 2.95 +} 2.96 + 2.97 + 2.98 +LUA_API int lua_checkstack (lua_State *L, int size) { 2.99 + int res = 1; 2.100 + lua_lock(L); 2.101 + if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) 2.102 + res = 0; /* stack overflow */ 2.103 + else if (size > 0) { 2.104 + luaD_checkstack(L, size); 2.105 + if (L->ci->top < L->top + size) 2.106 + L->ci->top = L->top + size; 2.107 + } 2.108 + lua_unlock(L); 2.109 + return res; 2.110 +} 2.111 + 2.112 + 2.113 +LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { 2.114 + int i; 2.115 + if (from == to) return; 2.116 + lua_lock(to); 2.117 + api_checknelems(from, n); 2.118 + api_check(from, G(from) == G(to)); 2.119 + api_check(from, to->ci->top - to->top >= n); 2.120 + from->top -= n; 2.121 + for (i = 0; i < n; i++) { 2.122 + setobj2s(to, to->top++, from->top + i); 2.123 + } 2.124 + lua_unlock(to); 2.125 +} 2.126 + 2.127 + 2.128 +LUA_API void lua_setlevel (lua_State *from, lua_State *to) { 2.129 + to->nCcalls = from->nCcalls; 2.130 +} 2.131 + 2.132 + 2.133 +LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { 2.134 + lua_CFunction old; 2.135 + lua_lock(L); 2.136 + old = G(L)->panic; 2.137 + G(L)->panic = panicf; 2.138 + lua_unlock(L); 2.139 + return old; 2.140 +} 2.141 + 2.142 + 2.143 +LUA_API lua_State *lua_newthread (lua_State *L) { 2.144 + lua_State *L1; 2.145 + lua_lock(L); 2.146 + luaC_checkGC(L); 2.147 + L1 = luaE_newthread(L); 2.148 + setthvalue(L, L->top, L1); 2.149 + api_incr_top(L); 2.150 + lua_unlock(L); 2.151 + luai_userstatethread(L, L1); 2.152 + return L1; 2.153 +} 2.154 + 2.155 + 2.156 + 2.157 +/* 2.158 +** basic stack manipulation 2.159 +*/ 2.160 + 2.161 + 2.162 +LUA_API int lua_gettop (lua_State *L) { 2.163 + return cast_int(L->top - L->base); 2.164 +} 2.165 + 2.166 + 2.167 +LUA_API void lua_settop (lua_State *L, int idx) { 2.168 + lua_lock(L); 2.169 + if (idx >= 0) { 2.170 + api_check(L, idx <= L->stack_last - L->base); 2.171 + while (L->top < L->base + idx) 2.172 + setnilvalue(L->top++); 2.173 + L->top = L->base + idx; 2.174 + } 2.175 + else { 2.176 + api_check(L, -(idx+1) <= (L->top - L->base)); 2.177 + L->top += idx+1; /* `subtract' index (index is negative) */ 2.178 + } 2.179 + lua_unlock(L); 2.180 +} 2.181 + 2.182 + 2.183 +LUA_API void lua_remove (lua_State *L, int idx) { 2.184 + StkId p; 2.185 + lua_lock(L); 2.186 + p = index2adr(L, idx); 2.187 + api_checkvalidindex(L, p); 2.188 + while (++p < L->top) setobjs2s(L, p-1, p); 2.189 + L->top--; 2.190 + lua_unlock(L); 2.191 +} 2.192 + 2.193 + 2.194 +LUA_API void lua_insert (lua_State *L, int idx) { 2.195 + StkId p; 2.196 + StkId q; 2.197 + lua_lock(L); 2.198 + p = index2adr(L, idx); 2.199 + api_checkvalidindex(L, p); 2.200 + for (q = L->top; q>p; q--) setobjs2s(L, q, q-1); 2.201 + setobjs2s(L, p, L->top); 2.202 + lua_unlock(L); 2.203 +} 2.204 + 2.205 + 2.206 +LUA_API void lua_replace (lua_State *L, int idx) { 2.207 + StkId o; 2.208 + lua_lock(L); 2.209 + /* explicit test for incompatible code */ 2.210 + if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci) 2.211 + luaG_runerror(L, "no calling environment"); 2.212 + api_checknelems(L, 1); 2.213 + o = index2adr(L, idx); 2.214 + api_checkvalidindex(L, o); 2.215 + if (idx == LUA_ENVIRONINDEX) { 2.216 + Closure *func = curr_func(L); 2.217 + api_check(L, ttistable(L->top - 1)); 2.218 + func->c.env = hvalue(L->top - 1); 2.219 + luaC_barrier(L, func, L->top - 1); 2.220 + } 2.221 + else { 2.222 + setobj(L, o, L->top - 1); 2.223 + if (idx < LUA_GLOBALSINDEX) /* function upvalue? */ 2.224 + luaC_barrier(L, curr_func(L), L->top - 1); 2.225 + } 2.226 + L->top--; 2.227 + lua_unlock(L); 2.228 +} 2.229 + 2.230 + 2.231 +LUA_API void lua_pushvalue (lua_State *L, int idx) { 2.232 + lua_lock(L); 2.233 + setobj2s(L, L->top, index2adr(L, idx)); 2.234 + api_incr_top(L); 2.235 + lua_unlock(L); 2.236 +} 2.237 + 2.238 + 2.239 + 2.240 +/* 2.241 +** access functions (stack -> C) 2.242 +*/ 2.243 + 2.244 + 2.245 +LUA_API int lua_type (lua_State *L, int idx) { 2.246 + StkId o = index2adr(L, idx); 2.247 + return (o == luaO_nilobject) ? LUA_TNONE : ttype(o); 2.248 +} 2.249 + 2.250 + 2.251 +LUA_API const char *lua_typename (lua_State *L, int t) { 2.252 + UNUSED(L); 2.253 + return (t == LUA_TNONE) ? "no value" : luaT_typenames[t]; 2.254 +} 2.255 + 2.256 + 2.257 +LUA_API int lua_iscfunction (lua_State *L, int idx) { 2.258 + StkId o = index2adr(L, idx); 2.259 + return iscfunction(o); 2.260 +} 2.261 + 2.262 + 2.263 +LUA_API int lua_isnumber (lua_State *L, int idx) { 2.264 + TValue n; 2.265 + const TValue *o = index2adr(L, idx); 2.266 + return tonumber(o, &n); 2.267 +} 2.268 + 2.269 + 2.270 +LUA_API int lua_isstring (lua_State *L, int idx) { 2.271 + int t = lua_type(L, idx); 2.272 + return (t == LUA_TSTRING || t == LUA_TNUMBER); 2.273 +} 2.274 + 2.275 + 2.276 +LUA_API int lua_isuserdata (lua_State *L, int idx) { 2.277 + const TValue *o = index2adr(L, idx); 2.278 + return (ttisuserdata(o) || ttislightuserdata(o)); 2.279 +} 2.280 + 2.281 + 2.282 +LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { 2.283 + StkId o1 = index2adr(L, index1); 2.284 + StkId o2 = index2adr(L, index2); 2.285 + return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 2.286 + : luaO_rawequalObj(o1, o2); 2.287 +} 2.288 + 2.289 + 2.290 +LUA_API int lua_equal (lua_State *L, int index1, int index2) { 2.291 + StkId o1, o2; 2.292 + int i; 2.293 + lua_lock(L); /* may call tag method */ 2.294 + o1 = index2adr(L, index1); 2.295 + o2 = index2adr(L, index2); 2.296 + i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2); 2.297 + lua_unlock(L); 2.298 + return i; 2.299 +} 2.300 + 2.301 + 2.302 +LUA_API int lua_lessthan (lua_State *L, int index1, int index2) { 2.303 + StkId o1, o2; 2.304 + int i; 2.305 + lua_lock(L); /* may call tag method */ 2.306 + o1 = index2adr(L, index1); 2.307 + o2 = index2adr(L, index2); 2.308 + i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 2.309 + : luaV_lessthan(L, o1, o2); 2.310 + lua_unlock(L); 2.311 + return i; 2.312 +} 2.313 + 2.314 + 2.315 + 2.316 +LUA_API lua_Number lua_tonumber (lua_State *L, int idx) { 2.317 + TValue n; 2.318 + const TValue *o = index2adr(L, idx); 2.319 + if (tonumber(o, &n)) 2.320 + return nvalue(o); 2.321 + else 2.322 + return 0; 2.323 +} 2.324 + 2.325 + 2.326 +LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) { 2.327 + TValue n; 2.328 + const TValue *o = index2adr(L, idx); 2.329 + if (tonumber(o, &n)) { 2.330 + lua_Integer res; 2.331 + lua_Number num = nvalue(o); 2.332 + lua_number2integer(res, num); 2.333 + return res; 2.334 + } 2.335 + else 2.336 + return 0; 2.337 +} 2.338 + 2.339 + 2.340 +LUA_API int lua_toboolean (lua_State *L, int idx) { 2.341 + const TValue *o = index2adr(L, idx); 2.342 + return !l_isfalse(o); 2.343 +} 2.344 + 2.345 + 2.346 +LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { 2.347 + StkId o = index2adr(L, idx); 2.348 + if (!ttisstring(o)) { 2.349 + lua_lock(L); /* `luaV_tostring' may create a new string */ 2.350 + if (!luaV_tostring(L, o)) { /* conversion failed? */ 2.351 + if (len != NULL) *len = 0; 2.352 + lua_unlock(L); 2.353 + return NULL; 2.354 + } 2.355 + luaC_checkGC(L); 2.356 + o = index2adr(L, idx); /* previous call may reallocate the stack */ 2.357 + lua_unlock(L); 2.358 + } 2.359 + if (len != NULL) *len = tsvalue(o)->len; 2.360 + return svalue(o); 2.361 +} 2.362 + 2.363 + 2.364 +LUA_API size_t lua_objlen (lua_State *L, int idx) { 2.365 + StkId o = index2adr(L, idx); 2.366 + switch (ttype(o)) { 2.367 + case LUA_TSTRING: return tsvalue(o)->len; 2.368 + case LUA_TUSERDATA: return uvalue(o)->len; 2.369 + case LUA_TTABLE: return luaH_getn(hvalue(o)); 2.370 + case LUA_TNUMBER: { 2.371 + size_t l; 2.372 + lua_lock(L); /* `luaV_tostring' may create a new string */ 2.373 + l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0); 2.374 + lua_unlock(L); 2.375 + return l; 2.376 + } 2.377 + default: return 0; 2.378 + } 2.379 +} 2.380 + 2.381 + 2.382 +LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { 2.383 + StkId o = index2adr(L, idx); 2.384 + return (!iscfunction(o)) ? NULL : clvalue(o)->c.f; 2.385 +} 2.386 + 2.387 + 2.388 +LUA_API void *lua_touserdata (lua_State *L, int idx) { 2.389 + StkId o = index2adr(L, idx); 2.390 + switch (ttype(o)) { 2.391 + case LUA_TUSERDATA: return (rawuvalue(o) + 1); 2.392 + case LUA_TLIGHTUSERDATA: return pvalue(o); 2.393 + default: return NULL; 2.394 + } 2.395 +} 2.396 + 2.397 + 2.398 +LUA_API lua_State *lua_tothread (lua_State *L, int idx) { 2.399 + StkId o = index2adr(L, idx); 2.400 + return (!ttisthread(o)) ? NULL : thvalue(o); 2.401 +} 2.402 + 2.403 + 2.404 +LUA_API const void *lua_topointer (lua_State *L, int idx) { 2.405 + StkId o = index2adr(L, idx); 2.406 + switch (ttype(o)) { 2.407 + case LUA_TTABLE: return hvalue(o); 2.408 + case LUA_TFUNCTION: return clvalue(o); 2.409 + case LUA_TTHREAD: return thvalue(o); 2.410 + case LUA_TUSERDATA: 2.411 + case LUA_TLIGHTUSERDATA: 2.412 + return lua_touserdata(L, idx); 2.413 + default: return NULL; 2.414 + } 2.415 +} 2.416 + 2.417 + 2.418 + 2.419 +/* 2.420 +** push functions (C -> stack) 2.421 +*/ 2.422 + 2.423 + 2.424 +LUA_API void lua_pushnil (lua_State *L) { 2.425 + lua_lock(L); 2.426 + setnilvalue(L->top); 2.427 + api_incr_top(L); 2.428 + lua_unlock(L); 2.429 +} 2.430 + 2.431 + 2.432 +LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { 2.433 + lua_lock(L); 2.434 + setnvalue(L->top, n); 2.435 + api_incr_top(L); 2.436 + lua_unlock(L); 2.437 +} 2.438 + 2.439 + 2.440 +LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { 2.441 + lua_lock(L); 2.442 + setnvalue(L->top, cast_num(n)); 2.443 + api_incr_top(L); 2.444 + lua_unlock(L); 2.445 +} 2.446 + 2.447 + 2.448 +LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) { 2.449 + lua_lock(L); 2.450 + luaC_checkGC(L); 2.451 + setsvalue2s(L, L->top, luaS_newlstr(L, s, len)); 2.452 + api_incr_top(L); 2.453 + lua_unlock(L); 2.454 +} 2.455 + 2.456 + 2.457 +LUA_API void lua_pushstring (lua_State *L, const char *s) { 2.458 + if (s == NULL) 2.459 + lua_pushnil(L); 2.460 + else 2.461 + lua_pushlstring(L, s, strlen(s)); 2.462 +} 2.463 + 2.464 + 2.465 +LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, 2.466 + va_list argp) { 2.467 + const char *ret; 2.468 + lua_lock(L); 2.469 + luaC_checkGC(L); 2.470 + ret = luaO_pushvfstring(L, fmt, argp); 2.471 + lua_unlock(L); 2.472 + return ret; 2.473 +} 2.474 + 2.475 + 2.476 +LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { 2.477 + const char *ret; 2.478 + va_list argp; 2.479 + lua_lock(L); 2.480 + luaC_checkGC(L); 2.481 + va_start(argp, fmt); 2.482 + ret = luaO_pushvfstring(L, fmt, argp); 2.483 + va_end(argp); 2.484 + lua_unlock(L); 2.485 + return ret; 2.486 +} 2.487 + 2.488 + 2.489 +LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { 2.490 + Closure *cl; 2.491 + lua_lock(L); 2.492 + luaC_checkGC(L); 2.493 + api_checknelems(L, n); 2.494 + cl = luaF_newCclosure(L, n, getcurrenv(L)); 2.495 + cl->c.f = fn; 2.496 + L->top -= n; 2.497 + while (n--) 2.498 + setobj2n(L, &cl->c.upvalue[n], L->top+n); 2.499 + setclvalue(L, L->top, cl); 2.500 + lua_assert(iswhite(obj2gco(cl))); 2.501 + api_incr_top(L); 2.502 + lua_unlock(L); 2.503 +} 2.504 + 2.505 + 2.506 +LUA_API void lua_pushboolean (lua_State *L, int b) { 2.507 + lua_lock(L); 2.508 + setbvalue(L->top, (b != 0)); /* ensure that true is 1 */ 2.509 + api_incr_top(L); 2.510 + lua_unlock(L); 2.511 +} 2.512 + 2.513 + 2.514 +LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { 2.515 + lua_lock(L); 2.516 + setpvalue(L->top, p); 2.517 + api_incr_top(L); 2.518 + lua_unlock(L); 2.519 +} 2.520 + 2.521 + 2.522 +LUA_API int lua_pushthread (lua_State *L) { 2.523 + lua_lock(L); 2.524 + setthvalue(L, L->top, L); 2.525 + api_incr_top(L); 2.526 + lua_unlock(L); 2.527 + return (G(L)->mainthread == L); 2.528 +} 2.529 + 2.530 + 2.531 + 2.532 +/* 2.533 +** get functions (Lua -> stack) 2.534 +*/ 2.535 + 2.536 + 2.537 +LUA_API void lua_gettable (lua_State *L, int idx) { 2.538 + StkId t; 2.539 + lua_lock(L); 2.540 + t = index2adr(L, idx); 2.541 + api_checkvalidindex(L, t); 2.542 + luaV_gettable(L, t, L->top - 1, L->top - 1); 2.543 + lua_unlock(L); 2.544 +} 2.545 + 2.546 + 2.547 +LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { 2.548 + StkId t; 2.549 + TValue key; 2.550 + lua_lock(L); 2.551 + t = index2adr(L, idx); 2.552 + api_checkvalidindex(L, t); 2.553 + setsvalue(L, &key, luaS_new(L, k)); 2.554 + luaV_gettable(L, t, &key, L->top); 2.555 + api_incr_top(L); 2.556 + lua_unlock(L); 2.557 +} 2.558 + 2.559 + 2.560 +LUA_API void lua_rawget (lua_State *L, int idx) { 2.561 + StkId t; 2.562 + lua_lock(L); 2.563 + t = index2adr(L, idx); 2.564 + api_check(L, ttistable(t)); 2.565 + setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); 2.566 + lua_unlock(L); 2.567 +} 2.568 + 2.569 + 2.570 +LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { 2.571 + StkId o; 2.572 + lua_lock(L); 2.573 + o = index2adr(L, idx); 2.574 + api_check(L, ttistable(o)); 2.575 + setobj2s(L, L->top, luaH_getnum(hvalue(o), n)); 2.576 + api_incr_top(L); 2.577 + lua_unlock(L); 2.578 +} 2.579 + 2.580 + 2.581 +LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { 2.582 + lua_lock(L); 2.583 + luaC_checkGC(L); 2.584 + sethvalue(L, L->top, luaH_new(L, narray, nrec)); 2.585 + api_incr_top(L); 2.586 + lua_unlock(L); 2.587 +} 2.588 + 2.589 + 2.590 +LUA_API int lua_getmetatable (lua_State *L, int objindex) { 2.591 + const TValue *obj; 2.592 + Table *mt = NULL; 2.593 + int res; 2.594 + lua_lock(L); 2.595 + obj = index2adr(L, objindex); 2.596 + switch (ttype(obj)) { 2.597 + case LUA_TTABLE: 2.598 + mt = hvalue(obj)->metatable; 2.599 + break; 2.600 + case LUA_TUSERDATA: 2.601 + mt = uvalue(obj)->metatable; 2.602 + break; 2.603 + default: 2.604 + mt = G(L)->mt[ttype(obj)]; 2.605 + break; 2.606 + } 2.607 + if (mt == NULL) 2.608 + res = 0; 2.609 + else { 2.610 + sethvalue(L, L->top, mt); 2.611 + api_incr_top(L); 2.612 + res = 1; 2.613 + } 2.614 + lua_unlock(L); 2.615 + return res; 2.616 +} 2.617 + 2.618 + 2.619 +LUA_API void lua_getfenv (lua_State *L, int idx) { 2.620 + StkId o; 2.621 + lua_lock(L); 2.622 + o = index2adr(L, idx); 2.623 + api_checkvalidindex(L, o); 2.624 + switch (ttype(o)) { 2.625 + case LUA_TFUNCTION: 2.626 + sethvalue(L, L->top, clvalue(o)->c.env); 2.627 + break; 2.628 + case LUA_TUSERDATA: 2.629 + sethvalue(L, L->top, uvalue(o)->env); 2.630 + break; 2.631 + case LUA_TTHREAD: 2.632 + setobj2s(L, L->top, gt(thvalue(o))); 2.633 + break; 2.634 + default: 2.635 + setnilvalue(L->top); 2.636 + break; 2.637 + } 2.638 + api_incr_top(L); 2.639 + lua_unlock(L); 2.640 +} 2.641 + 2.642 + 2.643 +/* 2.644 +** set functions (stack -> Lua) 2.645 +*/ 2.646 + 2.647 + 2.648 +LUA_API void lua_settable (lua_State *L, int idx) { 2.649 + StkId t; 2.650 + lua_lock(L); 2.651 + api_checknelems(L, 2); 2.652 + t = index2adr(L, idx); 2.653 + api_checkvalidindex(L, t); 2.654 + luaV_settable(L, t, L->top - 2, L->top - 1); 2.655 + L->top -= 2; /* pop index and value */ 2.656 + lua_unlock(L); 2.657 +} 2.658 + 2.659 + 2.660 +LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { 2.661 + StkId t; 2.662 + TValue key; 2.663 + lua_lock(L); 2.664 + api_checknelems(L, 1); 2.665 + t = index2adr(L, idx); 2.666 + api_checkvalidindex(L, t); 2.667 + setsvalue(L, &key, luaS_new(L, k)); 2.668 + luaV_settable(L, t, &key, L->top - 1); 2.669 + L->top--; /* pop value */ 2.670 + lua_unlock(L); 2.671 +} 2.672 + 2.673 + 2.674 +LUA_API void lua_rawset (lua_State *L, int idx) { 2.675 + StkId t; 2.676 + lua_lock(L); 2.677 + api_checknelems(L, 2); 2.678 + t = index2adr(L, idx); 2.679 + api_check(L, ttistable(t)); 2.680 + setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); 2.681 + luaC_barriert(L, hvalue(t), L->top-1); 2.682 + L->top -= 2; 2.683 + lua_unlock(L); 2.684 +} 2.685 + 2.686 + 2.687 +LUA_API void lua_rawseti (lua_State *L, int idx, int n) { 2.688 + StkId o; 2.689 + lua_lock(L); 2.690 + api_checknelems(L, 1); 2.691 + o = index2adr(L, idx); 2.692 + api_check(L, ttistable(o)); 2.693 + setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1); 2.694 + luaC_barriert(L, hvalue(o), L->top-1); 2.695 + L->top--; 2.696 + lua_unlock(L); 2.697 +} 2.698 + 2.699 + 2.700 +LUA_API int lua_setmetatable (lua_State *L, int objindex) { 2.701 + TValue *obj; 2.702 + Table *mt; 2.703 + lua_lock(L); 2.704 + api_checknelems(L, 1); 2.705 + obj = index2adr(L, objindex); 2.706 + api_checkvalidindex(L, obj); 2.707 + if (ttisnil(L->top - 1)) 2.708 + mt = NULL; 2.709 + else { 2.710 + api_check(L, ttistable(L->top - 1)); 2.711 + mt = hvalue(L->top - 1); 2.712 + } 2.713 + switch (ttype(obj)) { 2.714 + case LUA_TTABLE: { 2.715 + hvalue(obj)->metatable = mt; 2.716 + if (mt) 2.717 + luaC_objbarriert(L, hvalue(obj), mt); 2.718 + break; 2.719 + } 2.720 + case LUA_TUSERDATA: { 2.721 + uvalue(obj)->metatable = mt; 2.722 + if (mt) 2.723 + luaC_objbarrier(L, rawuvalue(obj), mt); 2.724 + break; 2.725 + } 2.726 + default: { 2.727 + G(L)->mt[ttype(obj)] = mt; 2.728 + break; 2.729 + } 2.730 + } 2.731 + L->top--; 2.732 + lua_unlock(L); 2.733 + return 1; 2.734 +} 2.735 + 2.736 + 2.737 +LUA_API int lua_setfenv (lua_State *L, int idx) { 2.738 + StkId o; 2.739 + int res = 1; 2.740 + lua_lock(L); 2.741 + api_checknelems(L, 1); 2.742 + o = index2adr(L, idx); 2.743 + api_checkvalidindex(L, o); 2.744 + api_check(L, ttistable(L->top - 1)); 2.745 + switch (ttype(o)) { 2.746 + case LUA_TFUNCTION: 2.747 + clvalue(o)->c.env = hvalue(L->top - 1); 2.748 + break; 2.749 + case LUA_TUSERDATA: 2.750 + uvalue(o)->env = hvalue(L->top - 1); 2.751 + break; 2.752 + case LUA_TTHREAD: 2.753 + sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1)); 2.754 + break; 2.755 + default: 2.756 + res = 0; 2.757 + break; 2.758 + } 2.759 + if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); 2.760 + L->top--; 2.761 + lua_unlock(L); 2.762 + return res; 2.763 +} 2.764 + 2.765 + 2.766 +/* 2.767 +** `load' and `call' functions (run Lua code) 2.768 +*/ 2.769 + 2.770 + 2.771 +#define adjustresults(L,nres) \ 2.772 + { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; } 2.773 + 2.774 + 2.775 +#define checkresults(L,na,nr) \ 2.776 + api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na))) 2.777 + 2.778 + 2.779 +LUA_API void lua_call (lua_State *L, int nargs, int nresults) { 2.780 + StkId func; 2.781 + lua_lock(L); 2.782 + api_checknelems(L, nargs+1); 2.783 + checkresults(L, nargs, nresults); 2.784 + func = L->top - (nargs+1); 2.785 + luaD_call(L, func, nresults); 2.786 + adjustresults(L, nresults); 2.787 + lua_unlock(L); 2.788 +} 2.789 + 2.790 + 2.791 + 2.792 +/* 2.793 +** Execute a protected call. 2.794 +*/ 2.795 +struct CallS { /* data to `f_call' */ 2.796 + StkId func; 2.797 + int nresults; 2.798 +}; 2.799 + 2.800 + 2.801 +static void f_call (lua_State *L, void *ud) { 2.802 + struct CallS *c = cast(struct CallS *, ud); 2.803 + luaD_call(L, c->func, c->nresults); 2.804 +} 2.805 + 2.806 + 2.807 + 2.808 +LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { 2.809 + struct CallS c; 2.810 + int status; 2.811 + ptrdiff_t func; 2.812 + lua_lock(L); 2.813 + api_checknelems(L, nargs+1); 2.814 + checkresults(L, nargs, nresults); 2.815 + if (errfunc == 0) 2.816 + func = 0; 2.817 + else { 2.818 + StkId o = index2adr(L, errfunc); 2.819 + api_checkvalidindex(L, o); 2.820 + func = savestack(L, o); 2.821 + } 2.822 + c.func = L->top - (nargs+1); /* function to be called */ 2.823 + c.nresults = nresults; 2.824 + status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); 2.825 + adjustresults(L, nresults); 2.826 + lua_unlock(L); 2.827 + return status; 2.828 +} 2.829 + 2.830 + 2.831 +/* 2.832 +** Execute a protected C call. 2.833 +*/ 2.834 +struct CCallS { /* data to `f_Ccall' */ 2.835 + lua_CFunction func; 2.836 + void *ud; 2.837 +}; 2.838 + 2.839 + 2.840 +static void f_Ccall (lua_State *L, void *ud) { 2.841 + struct CCallS *c = cast(struct CCallS *, ud); 2.842 + Closure *cl; 2.843 + cl = luaF_newCclosure(L, 0, getcurrenv(L)); 2.844 + cl->c.f = c->func; 2.845 + setclvalue(L, L->top, cl); /* push function */ 2.846 + api_incr_top(L); 2.847 + setpvalue(L->top, c->ud); /* push only argument */ 2.848 + api_incr_top(L); 2.849 + luaD_call(L, L->top - 2, 0); 2.850 +} 2.851 + 2.852 + 2.853 +LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) { 2.854 + struct CCallS c; 2.855 + int status; 2.856 + lua_lock(L); 2.857 + c.func = func; 2.858 + c.ud = ud; 2.859 + status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0); 2.860 + lua_unlock(L); 2.861 + return status; 2.862 +} 2.863 + 2.864 + 2.865 +LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, 2.866 + const char *chunkname) { 2.867 + ZIO z; 2.868 + int status; 2.869 + lua_lock(L); 2.870 + if (!chunkname) chunkname = "?"; 2.871 + luaZ_init(L, &z, reader, data); 2.872 + status = luaD_protectedparser(L, &z, chunkname); 2.873 + lua_unlock(L); 2.874 + return status; 2.875 +} 2.876 + 2.877 + 2.878 +LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) { 2.879 + int status; 2.880 + TValue *o; 2.881 + lua_lock(L); 2.882 + api_checknelems(L, 1); 2.883 + o = L->top - 1; 2.884 + if (isLfunction(o)) 2.885 + status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0); 2.886 + else 2.887 + status = 1; 2.888 + lua_unlock(L); 2.889 + return status; 2.890 +} 2.891 + 2.892 + 2.893 +LUA_API int lua_status (lua_State *L) { 2.894 + return L->status; 2.895 +} 2.896 + 2.897 + 2.898 +/* 2.899 +** Garbage-collection function 2.900 +*/ 2.901 + 2.902 +LUA_API int lua_gc (lua_State *L, int what, int data) { 2.903 + int res = 0; 2.904 + global_State *g; 2.905 + lua_lock(L); 2.906 + g = G(L); 2.907 + switch (what) { 2.908 + case LUA_GCSTOP: { 2.909 + g->GCthreshold = MAX_LUMEM; 2.910 + break; 2.911 + } 2.912 + case LUA_GCRESTART: { 2.913 + g->GCthreshold = g->totalbytes; 2.914 + break; 2.915 + } 2.916 + case LUA_GCCOLLECT: { 2.917 + luaC_fullgc(L); 2.918 + break; 2.919 + } 2.920 + case LUA_GCCOUNT: { 2.921 + /* GC values are expressed in Kbytes: #bytes/2^10 */ 2.922 + res = cast_int(g->totalbytes >> 10); 2.923 + break; 2.924 + } 2.925 + case LUA_GCCOUNTB: { 2.926 + res = cast_int(g->totalbytes & 0x3ff); 2.927 + break; 2.928 + } 2.929 + case LUA_GCSTEP: { 2.930 + lu_mem a = (cast(lu_mem, data) << 10); 2.931 + if (a <= g->totalbytes) 2.932 + g->GCthreshold = g->totalbytes - a; 2.933 + else 2.934 + g->GCthreshold = 0; 2.935 + while (g->GCthreshold <= g->totalbytes) { 2.936 + luaC_step(L); 2.937 + if (g->gcstate == GCSpause) { /* end of cycle? */ 2.938 + res = 1; /* signal it */ 2.939 + break; 2.940 + } 2.941 + } 2.942 + break; 2.943 + } 2.944 + case LUA_GCSETPAUSE: { 2.945 + res = g->gcpause; 2.946 + g->gcpause = data; 2.947 + break; 2.948 + } 2.949 + case LUA_GCSETSTEPMUL: { 2.950 + res = g->gcstepmul; 2.951 + g->gcstepmul = data; 2.952 + break; 2.953 + } 2.954 + default: res = -1; /* invalid option */ 2.955 + } 2.956 + lua_unlock(L); 2.957 + return res; 2.958 +} 2.959 + 2.960 + 2.961 + 2.962 +/* 2.963 +** miscellaneous functions 2.964 +*/ 2.965 + 2.966 + 2.967 +LUA_API int lua_error (lua_State *L) { 2.968 + lua_lock(L); 2.969 + api_checknelems(L, 1); 2.970 + luaG_errormsg(L); 2.971 + lua_unlock(L); 2.972 + return 0; /* to avoid warnings */ 2.973 +} 2.974 + 2.975 + 2.976 +LUA_API int lua_next (lua_State *L, int idx) { 2.977 + StkId t; 2.978 + int more; 2.979 + lua_lock(L); 2.980 + t = index2adr(L, idx); 2.981 + api_check(L, ttistable(t)); 2.982 + more = luaH_next(L, hvalue(t), L->top - 1); 2.983 + if (more) { 2.984 + api_incr_top(L); 2.985 + } 2.986 + else /* no more elements */ 2.987 + L->top -= 1; /* remove key */ 2.988 + lua_unlock(L); 2.989 + return more; 2.990 +} 2.991 + 2.992 + 2.993 +LUA_API void lua_concat (lua_State *L, int n) { 2.994 + lua_lock(L); 2.995 + api_checknelems(L, n); 2.996 + if (n >= 2) { 2.997 + luaC_checkGC(L); 2.998 + luaV_concat(L, n, cast_int(L->top - L->base) - 1); 2.999 + L->top -= (n-1); 2.1000 + } 2.1001 + else if (n == 0) { /* push empty string */ 2.1002 + setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); 2.1003 + api_incr_top(L); 2.1004 + } 2.1005 + /* else n == 1; nothing to do */ 2.1006 + lua_unlock(L); 2.1007 +} 2.1008 + 2.1009 + 2.1010 +LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { 2.1011 + lua_Alloc f; 2.1012 + lua_lock(L); 2.1013 + if (ud) *ud = G(L)->ud; 2.1014 + f = G(L)->frealloc; 2.1015 + lua_unlock(L); 2.1016 + return f; 2.1017 +} 2.1018 + 2.1019 + 2.1020 +LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { 2.1021 + lua_lock(L); 2.1022 + G(L)->ud = ud; 2.1023 + G(L)->frealloc = f; 2.1024 + lua_unlock(L); 2.1025 +} 2.1026 + 2.1027 + 2.1028 +LUA_API void *lua_newuserdata (lua_State *L, size_t size) { 2.1029 + Udata *u; 2.1030 + lua_lock(L); 2.1031 + luaC_checkGC(L); 2.1032 + u = luaS_newudata(L, size, getcurrenv(L)); 2.1033 + setuvalue(L, L->top, u); 2.1034 + api_incr_top(L); 2.1035 + lua_unlock(L); 2.1036 + return u + 1; 2.1037 +} 2.1038 + 2.1039 + 2.1040 + 2.1041 + 2.1042 +static const char *aux_upvalue (StkId fi, int n, TValue **val) { 2.1043 + Closure *f; 2.1044 + if (!ttisfunction(fi)) return NULL; 2.1045 + f = clvalue(fi); 2.1046 + if (f->c.isC) { 2.1047 + if (!(1 <= n && n <= f->c.nupvalues)) return NULL; 2.1048 + *val = &f->c.upvalue[n-1]; 2.1049 + return ""; 2.1050 + } 2.1051 + else { 2.1052 + Proto *p = f->l.p; 2.1053 + if (!(1 <= n && n <= p->sizeupvalues)) return NULL; 2.1054 + *val = f->l.upvals[n-1]->v; 2.1055 + return getstr(p->upvalues[n-1]); 2.1056 + } 2.1057 +} 2.1058 + 2.1059 + 2.1060 +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { 2.1061 + const char *name; 2.1062 + TValue *val; 2.1063 + lua_lock(L); 2.1064 + name = aux_upvalue(index2adr(L, funcindex), n, &val); 2.1065 + if (name) { 2.1066 + setobj2s(L, L->top, val); 2.1067 + api_incr_top(L); 2.1068 + } 2.1069 + lua_unlock(L); 2.1070 + return name; 2.1071 +} 2.1072 + 2.1073 + 2.1074 +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { 2.1075 + const char *name; 2.1076 + TValue *val; 2.1077 + StkId fi; 2.1078 + lua_lock(L); 2.1079 + fi = index2adr(L, funcindex); 2.1080 + api_checknelems(L, 1); 2.1081 + name = aux_upvalue(fi, n, &val); 2.1082 + if (name) { 2.1083 + L->top--; 2.1084 + setobj(L, val, L->top); 2.1085 + luaC_barrier(L, clvalue(fi), L->top); 2.1086 + } 2.1087 + lua_unlock(L); 2.1088 + return name; 2.1089 +} 2.1090 +
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/lua/lapi.h Sat Mar 03 11:07:39 2012 -0600 3.3 @@ -0,0 +1,16 @@ 3.4 +/* 3.5 +** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $ 3.6 +** Auxiliary functions from Lua API 3.7 +** See Copyright Notice in lua.h 3.8 +*/ 3.9 + 3.10 +#ifndef lapi_h 3.11 +#define lapi_h 3.12 + 3.13 + 3.14 +#include "lobject.h" 3.15 + 3.16 + 3.17 +LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o); 3.18 + 3.19 +#endif
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/lua/lauxlib.c Sat Mar 03 11:07:39 2012 -0600 4.3 @@ -0,0 +1,652 @@ 4.4 +/* 4.5 +** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $ 4.6 +** Auxiliary functions for building Lua libraries 4.7 +** See Copyright Notice in lua.h 4.8 +*/ 4.9 + 4.10 + 4.11 +#include <ctype.h> 4.12 +#include <errno.h> 4.13 +#include <stdarg.h> 4.14 +#include <stdio.h> 4.15 +#include <stdlib.h> 4.16 +#include <string.h> 4.17 + 4.18 + 4.19 +/* This file uses only the official API of Lua. 4.20 +** Any function declared here could be written as an application function. 4.21 +*/ 4.22 + 4.23 +#define lauxlib_c 4.24 +#define LUA_LIB 4.25 + 4.26 +#include "lua.h" 4.27 + 4.28 +#include "lauxlib.h" 4.29 + 4.30 + 4.31 +#define FREELIST_REF 0 /* free list of references */ 4.32 + 4.33 + 4.34 +/* convert a stack index to positive */ 4.35 +#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \ 4.36 + lua_gettop(L) + (i) + 1) 4.37 + 4.38 + 4.39 +/* 4.40 +** {====================================================== 4.41 +** Error-report functions 4.42 +** ======================================================= 4.43 +*/ 4.44 + 4.45 + 4.46 +LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { 4.47 + lua_Debug ar; 4.48 + if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ 4.49 + return luaL_error(L, "bad argument #%d (%s)", narg, extramsg); 4.50 + lua_getinfo(L, "n", &ar); 4.51 + if (strcmp(ar.namewhat, "method") == 0) { 4.52 + narg--; /* do not count `self' */ 4.53 + if (narg == 0) /* error is in the self argument itself? */ 4.54 + return luaL_error(L, "calling " LUA_QS " on bad self (%s)", 4.55 + ar.name, extramsg); 4.56 + } 4.57 + if (ar.name == NULL) 4.58 + ar.name = "?"; 4.59 + return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)", 4.60 + narg, ar.name, extramsg); 4.61 +} 4.62 + 4.63 + 4.64 +LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) { 4.65 + const char *msg = lua_pushfstring(L, "%s expected, got %s", 4.66 + tname, luaL_typename(L, narg)); 4.67 + return luaL_argerror(L, narg, msg); 4.68 +} 4.69 + 4.70 + 4.71 +static void tag_error (lua_State *L, int narg, int tag) { 4.72 + luaL_typerror(L, narg, lua_typename(L, tag)); 4.73 +} 4.74 + 4.75 + 4.76 +LUALIB_API void luaL_where (lua_State *L, int level) { 4.77 + lua_Debug ar; 4.78 + if (lua_getstack(L, level, &ar)) { /* check function at level */ 4.79 + lua_getinfo(L, "Sl", &ar); /* get info about it */ 4.80 + if (ar.currentline > 0) { /* is there info? */ 4.81 + lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); 4.82 + return; 4.83 + } 4.84 + } 4.85 + lua_pushliteral(L, ""); /* else, no information available... */ 4.86 +} 4.87 + 4.88 + 4.89 +LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { 4.90 + va_list argp; 4.91 + va_start(argp, fmt); 4.92 + luaL_where(L, 1); 4.93 + lua_pushvfstring(L, fmt, argp); 4.94 + va_end(argp); 4.95 + lua_concat(L, 2); 4.96 + return lua_error(L); 4.97 +} 4.98 + 4.99 +/* }====================================================== */ 4.100 + 4.101 + 4.102 +LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def, 4.103 + const char *const lst[]) { 4.104 + const char *name = (def) ? luaL_optstring(L, narg, def) : 4.105 + luaL_checkstring(L, narg); 4.106 + int i; 4.107 + for (i=0; lst[i]; i++) 4.108 + if (strcmp(lst[i], name) == 0) 4.109 + return i; 4.110 + return luaL_argerror(L, narg, 4.111 + lua_pushfstring(L, "invalid option " LUA_QS, name)); 4.112 +} 4.113 + 4.114 + 4.115 +LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { 4.116 + lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */ 4.117 + if (!lua_isnil(L, -1)) /* name already in use? */ 4.118 + return 0; /* leave previous value on top, but return 0 */ 4.119 + lua_pop(L, 1); 4.120 + lua_newtable(L); /* create metatable */ 4.121 + lua_pushvalue(L, -1); 4.122 + lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ 4.123 + return 1; 4.124 +} 4.125 + 4.126 + 4.127 +LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { 4.128 + void *p = lua_touserdata(L, ud); 4.129 + if (p != NULL) { /* value is a userdata? */ 4.130 + if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ 4.131 + lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ 4.132 + if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ 4.133 + lua_pop(L, 2); /* remove both metatables */ 4.134 + return p; 4.135 + } 4.136 + } 4.137 + } 4.138 + luaL_typerror(L, ud, tname); /* else error */ 4.139 + return NULL; /* to avoid warnings */ 4.140 +} 4.141 + 4.142 + 4.143 +LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { 4.144 + if (!lua_checkstack(L, space)) 4.145 + luaL_error(L, "stack overflow (%s)", mes); 4.146 +} 4.147 + 4.148 + 4.149 +LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) { 4.150 + if (lua_type(L, narg) != t) 4.151 + tag_error(L, narg, t); 4.152 +} 4.153 + 4.154 + 4.155 +LUALIB_API void luaL_checkany (lua_State *L, int narg) { 4.156 + if (lua_type(L, narg) == LUA_TNONE) 4.157 + luaL_argerror(L, narg, "value expected"); 4.158 +} 4.159 + 4.160 + 4.161 +LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { 4.162 + const char *s = lua_tolstring(L, narg, len); 4.163 + if (!s) tag_error(L, narg, LUA_TSTRING); 4.164 + return s; 4.165 +} 4.166 + 4.167 + 4.168 +LUALIB_API const char *luaL_optlstring (lua_State *L, int narg, 4.169 + const char *def, size_t *len) { 4.170 + if (lua_isnoneornil(L, narg)) { 4.171 + if (len) 4.172 + *len = (def ? strlen(def) : 0); 4.173 + return def; 4.174 + } 4.175 + else return luaL_checklstring(L, narg, len); 4.176 +} 4.177 + 4.178 + 4.179 +LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { 4.180 + lua_Number d = lua_tonumber(L, narg); 4.181 + if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ 4.182 + tag_error(L, narg, LUA_TNUMBER); 4.183 + return d; 4.184 +} 4.185 + 4.186 + 4.187 +LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { 4.188 + return luaL_opt(L, luaL_checknumber, narg, def); 4.189 +} 4.190 + 4.191 + 4.192 +LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { 4.193 + lua_Integer d = lua_tointeger(L, narg); 4.194 + if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ 4.195 + tag_error(L, narg, LUA_TNUMBER); 4.196 + return d; 4.197 +} 4.198 + 4.199 + 4.200 +LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, 4.201 + lua_Integer def) { 4.202 + return luaL_opt(L, luaL_checkinteger, narg, def); 4.203 +} 4.204 + 4.205 + 4.206 +LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { 4.207 + if (!lua_getmetatable(L, obj)) /* no metatable? */ 4.208 + return 0; 4.209 + lua_pushstring(L, event); 4.210 + lua_rawget(L, -2); 4.211 + if (lua_isnil(L, -1)) { 4.212 + lua_pop(L, 2); /* remove metatable and metafield */ 4.213 + return 0; 4.214 + } 4.215 + else { 4.216 + lua_remove(L, -2); /* remove only metatable */ 4.217 + return 1; 4.218 + } 4.219 +} 4.220 + 4.221 + 4.222 +LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { 4.223 + obj = abs_index(L, obj); 4.224 + if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ 4.225 + return 0; 4.226 + lua_pushvalue(L, obj); 4.227 + lua_call(L, 1, 1); 4.228 + return 1; 4.229 +} 4.230 + 4.231 + 4.232 +LUALIB_API void (luaL_register) (lua_State *L, const char *libname, 4.233 + const luaL_Reg *l) { 4.234 + luaI_openlib(L, libname, l, 0); 4.235 +} 4.236 + 4.237 + 4.238 +static int libsize (const luaL_Reg *l) { 4.239 + int size = 0; 4.240 + for (; l->name; l++) size++; 4.241 + return size; 4.242 +} 4.243 + 4.244 + 4.245 +LUALIB_API void luaI_openlib (lua_State *L, const char *libname, 4.246 + const luaL_Reg *l, int nup) { 4.247 + if (libname) { 4.248 + int size = libsize(l); 4.249 + /* check whether lib already exists */ 4.250 + luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); 4.251 + lua_getfield(L, -1, libname); /* get _LOADED[libname] */ 4.252 + if (!lua_istable(L, -1)) { /* not found? */ 4.253 + lua_pop(L, 1); /* remove previous result */ 4.254 + /* try global variable (and create one if it does not exist) */ 4.255 + if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) 4.256 + luaL_error(L, "name conflict for module " LUA_QS, libname); 4.257 + lua_pushvalue(L, -1); 4.258 + lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ 4.259 + } 4.260 + lua_remove(L, -2); /* remove _LOADED table */ 4.261 + lua_insert(L, -(nup+1)); /* move library table to below upvalues */ 4.262 + } 4.263 + for (; l->name; l++) { 4.264 + int i; 4.265 + for (i=0; i<nup; i++) /* copy upvalues to the top */ 4.266 + lua_pushvalue(L, -nup); 4.267 + lua_pushcclosure(L, l->func, nup); 4.268 + lua_setfield(L, -(nup+2), l->name); 4.269 + } 4.270 + lua_pop(L, nup); /* remove upvalues */ 4.271 +} 4.272 + 4.273 + 4.274 + 4.275 +/* 4.276 +** {====================================================== 4.277 +** getn-setn: size for arrays 4.278 +** ======================================================= 4.279 +*/ 4.280 + 4.281 +#if defined(LUA_COMPAT_GETN) 4.282 + 4.283 +static int checkint (lua_State *L, int topop) { 4.284 + int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1; 4.285 + lua_pop(L, topop); 4.286 + return n; 4.287 +} 4.288 + 4.289 + 4.290 +static void getsizes (lua_State *L) { 4.291 + lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); 4.292 + if (lua_isnil(L, -1)) { /* no `size' table? */ 4.293 + lua_pop(L, 1); /* remove nil */ 4.294 + lua_newtable(L); /* create it */ 4.295 + lua_pushvalue(L, -1); /* `size' will be its own metatable */ 4.296 + lua_setmetatable(L, -2); 4.297 + lua_pushliteral(L, "kv"); 4.298 + lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */ 4.299 + lua_pushvalue(L, -1); 4.300 + lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */ 4.301 + } 4.302 +} 4.303 + 4.304 + 4.305 +LUALIB_API void luaL_setn (lua_State *L, int t, int n) { 4.306 + t = abs_index(L, t); 4.307 + lua_pushliteral(L, "n"); 4.308 + lua_rawget(L, t); 4.309 + if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */ 4.310 + lua_pushliteral(L, "n"); /* use it */ 4.311 + lua_pushinteger(L, n); 4.312 + lua_rawset(L, t); 4.313 + } 4.314 + else { /* use `sizes' */ 4.315 + getsizes(L); 4.316 + lua_pushvalue(L, t); 4.317 + lua_pushinteger(L, n); 4.318 + lua_rawset(L, -3); /* sizes[t] = n */ 4.319 + lua_pop(L, 1); /* remove `sizes' */ 4.320 + } 4.321 +} 4.322 + 4.323 + 4.324 +LUALIB_API int luaL_getn (lua_State *L, int t) { 4.325 + int n; 4.326 + t = abs_index(L, t); 4.327 + lua_pushliteral(L, "n"); /* try t.n */ 4.328 + lua_rawget(L, t); 4.329 + if ((n = checkint(L, 1)) >= 0) return n; 4.330 + getsizes(L); /* else try sizes[t] */ 4.331 + lua_pushvalue(L, t); 4.332 + lua_rawget(L, -2); 4.333 + if ((n = checkint(L, 2)) >= 0) return n; 4.334 + return (int)lua_objlen(L, t); 4.335 +} 4.336 + 4.337 +#endif 4.338 + 4.339 +/* }====================================================== */ 4.340 + 4.341 + 4.342 + 4.343 +LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, 4.344 + const char *r) { 4.345 + const char *wild; 4.346 + size_t l = strlen(p); 4.347 + luaL_Buffer b; 4.348 + luaL_buffinit(L, &b); 4.349 + while ((wild = strstr(s, p)) != NULL) { 4.350 + luaL_addlstring(&b, s, wild - s); /* push prefix */ 4.351 + luaL_addstring(&b, r); /* push replacement in place of pattern */ 4.352 + s = wild + l; /* continue after `p' */ 4.353 + } 4.354 + luaL_addstring(&b, s); /* push last suffix */ 4.355 + luaL_pushresult(&b); 4.356 + return lua_tostring(L, -1); 4.357 +} 4.358 + 4.359 + 4.360 +LUALIB_API const char *luaL_findtable (lua_State *L, int idx, 4.361 + const char *fname, int szhint) { 4.362 + const char *e; 4.363 + lua_pushvalue(L, idx); 4.364 + do { 4.365 + e = strchr(fname, '.'); 4.366 + if (e == NULL) e = fname + strlen(fname); 4.367 + lua_pushlstring(L, fname, e - fname); 4.368 + lua_rawget(L, -2); 4.369 + if (lua_isnil(L, -1)) { /* no such field? */ 4.370 + lua_pop(L, 1); /* remove this nil */ 4.371 + lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ 4.372 + lua_pushlstring(L, fname, e - fname); 4.373 + lua_pushvalue(L, -2); 4.374 + lua_settable(L, -4); /* set new table into field */ 4.375 + } 4.376 + else if (!lua_istable(L, -1)) { /* field has a non-table value? */ 4.377 + lua_pop(L, 2); /* remove table and value */ 4.378 + return fname; /* return problematic part of the name */ 4.379 + } 4.380 + lua_remove(L, -2); /* remove previous table */ 4.381 + fname = e + 1; 4.382 + } while (*e == '.'); 4.383 + return NULL; 4.384 +} 4.385 + 4.386 + 4.387 + 4.388 +/* 4.389 +** {====================================================== 4.390 +** Generic Buffer manipulation 4.391 +** ======================================================= 4.392 +*/ 4.393 + 4.394 + 4.395 +#define bufflen(B) ((B)->p - (B)->buffer) 4.396 +#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B))) 4.397 + 4.398 +#define LIMIT (LUA_MINSTACK/2) 4.399 + 4.400 + 4.401 +static int emptybuffer (luaL_Buffer *B) { 4.402 + size_t l = bufflen(B); 4.403 + if (l == 0) return 0; /* put nothing on stack */ 4.404 + else { 4.405 + lua_pushlstring(B->L, B->buffer, l); 4.406 + B->p = B->buffer; 4.407 + B->lvl++; 4.408 + return 1; 4.409 + } 4.410 +} 4.411 + 4.412 + 4.413 +static void adjuststack (luaL_Buffer *B) { 4.414 + if (B->lvl > 1) { 4.415 + lua_State *L = B->L; 4.416 + int toget = 1; /* number of levels to concat */ 4.417 + size_t toplen = lua_strlen(L, -1); 4.418 + do { 4.419 + size_t l = lua_strlen(L, -(toget+1)); 4.420 + if (B->lvl - toget + 1 >= LIMIT || toplen > l) { 4.421 + toplen += l; 4.422 + toget++; 4.423 + } 4.424 + else break; 4.425 + } while (toget < B->lvl); 4.426 + lua_concat(L, toget); 4.427 + B->lvl = B->lvl - toget + 1; 4.428 + } 4.429 +} 4.430 + 4.431 + 4.432 +LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) { 4.433 + if (emptybuffer(B)) 4.434 + adjuststack(B); 4.435 + return B->buffer; 4.436 +} 4.437 + 4.438 + 4.439 +LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { 4.440 + while (l--) 4.441 + luaL_addchar(B, *s++); 4.442 +} 4.443 + 4.444 + 4.445 +LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { 4.446 + luaL_addlstring(B, s, strlen(s)); 4.447 +} 4.448 + 4.449 + 4.450 +LUALIB_API void luaL_pushresult (luaL_Buffer *B) { 4.451 + emptybuffer(B); 4.452 + lua_concat(B->L, B->lvl); 4.453 + B->lvl = 1; 4.454 +} 4.455 + 4.456 + 4.457 +LUALIB_API void luaL_addvalue (luaL_Buffer *B) { 4.458 + lua_State *L = B->L; 4.459 + size_t vl; 4.460 + const char *s = lua_tolstring(L, -1, &vl); 4.461 + if (vl <= bufffree(B)) { /* fit into buffer? */ 4.462 + memcpy(B->p, s, vl); /* put it there */ 4.463 + B->p += vl; 4.464 + lua_pop(L, 1); /* remove from stack */ 4.465 + } 4.466 + else { 4.467 + if (emptybuffer(B)) 4.468 + lua_insert(L, -2); /* put buffer before new value */ 4.469 + B->lvl++; /* add new value into B stack */ 4.470 + adjuststack(B); 4.471 + } 4.472 +} 4.473 + 4.474 + 4.475 +LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { 4.476 + B->L = L; 4.477 + B->p = B->buffer; 4.478 + B->lvl = 0; 4.479 +} 4.480 + 4.481 +/* }====================================================== */ 4.482 + 4.483 + 4.484 +LUALIB_API int luaL_ref (lua_State *L, int t) { 4.485 + int ref; 4.486 + t = abs_index(L, t); 4.487 + if (lua_isnil(L, -1)) { 4.488 + lua_pop(L, 1); /* remove from stack */ 4.489 + return LUA_REFNIL; /* `nil' has a unique fixed reference */ 4.490 + } 4.491 + lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ 4.492 + ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ 4.493 + lua_pop(L, 1); /* remove it from stack */ 4.494 + if (ref != 0) { /* any free element? */ 4.495 + lua_rawgeti(L, t, ref); /* remove it from list */ 4.496 + lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */ 4.497 + } 4.498 + else { /* no free elements */ 4.499 + ref = (int)lua_objlen(L, t); 4.500 + ref++; /* create new reference */ 4.501 + } 4.502 + lua_rawseti(L, t, ref); 4.503 + return ref; 4.504 +} 4.505 + 4.506 + 4.507 +LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { 4.508 + if (ref >= 0) { 4.509 + t = abs_index(L, t); 4.510 + lua_rawgeti(L, t, FREELIST_REF); 4.511 + lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ 4.512 + lua_pushinteger(L, ref); 4.513 + lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ 4.514 + } 4.515 +} 4.516 + 4.517 + 4.518 + 4.519 +/* 4.520 +** {====================================================== 4.521 +** Load functions 4.522 +** ======================================================= 4.523 +*/ 4.524 + 4.525 +typedef struct LoadF { 4.526 + int extraline; 4.527 + FILE *f; 4.528 + char buff[LUAL_BUFFERSIZE]; 4.529 +} LoadF; 4.530 + 4.531 + 4.532 +static const char *getF (lua_State *L, void *ud, size_t *size) { 4.533 + LoadF *lf = (LoadF *)ud; 4.534 + (void)L; 4.535 + if (lf->extraline) { 4.536 + lf->extraline = 0; 4.537 + *size = 1; 4.538 + return "\n"; 4.539 + } 4.540 + if (feof(lf->f)) return NULL; 4.541 + *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); 4.542 + return (*size > 0) ? lf->buff : NULL; 4.543 +} 4.544 + 4.545 + 4.546 +static int errfile (lua_State *L, const char *what, int fnameindex) { 4.547 + const char *serr = strerror(errno); 4.548 + const char *filename = lua_tostring(L, fnameindex) + 1; 4.549 + lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); 4.550 + lua_remove(L, fnameindex); 4.551 + return LUA_ERRFILE; 4.552 +} 4.553 + 4.554 + 4.555 +LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { 4.556 + LoadF lf; 4.557 + int status, readstatus; 4.558 + int c; 4.559 + int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ 4.560 + lf.extraline = 0; 4.561 + if (filename == NULL) { 4.562 + lua_pushliteral(L, "=stdin"); 4.563 + lf.f = stdin; 4.564 + } 4.565 + else { 4.566 + lua_pushfstring(L, "@%s", filename); 4.567 + lf.f = fopen(filename, "r"); 4.568 + if (lf.f == NULL) return errfile(L, "open", fnameindex); 4.569 + } 4.570 + c = getc(lf.f); 4.571 + if (c == '#') { /* Unix exec. file? */ 4.572 + lf.extraline = 1; 4.573 + while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ 4.574 + if (c == '\n') c = getc(lf.f); 4.575 + } 4.576 + if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ 4.577 + lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ 4.578 + if (lf.f == NULL) return errfile(L, "reopen", fnameindex); 4.579 + /* skip eventual `#!...' */ 4.580 + while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; 4.581 + lf.extraline = 0; 4.582 + } 4.583 + ungetc(c, lf.f); 4.584 + status = lua_load(L, getF, &lf, lua_tostring(L, -1)); 4.585 + readstatus = ferror(lf.f); 4.586 + if (filename) fclose(lf.f); /* close file (even in case of errors) */ 4.587 + if (readstatus) { 4.588 + lua_settop(L, fnameindex); /* ignore results from `lua_load' */ 4.589 + return errfile(L, "read", fnameindex); 4.590 + } 4.591 + lua_remove(L, fnameindex); 4.592 + return status; 4.593 +} 4.594 + 4.595 + 4.596 +typedef struct LoadS { 4.597 + const char *s; 4.598 + size_t size; 4.599 +} LoadS; 4.600 + 4.601 + 4.602 +static const char *getS (lua_State *L, void *ud, size_t *size) { 4.603 + LoadS *ls = (LoadS *)ud; 4.604 + (void)L; 4.605 + if (ls->size == 0) return NULL; 4.606 + *size = ls->size; 4.607 + ls->size = 0; 4.608 + return ls->s; 4.609 +} 4.610 + 4.611 + 4.612 +LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size, 4.613 + const char *name) { 4.614 + LoadS ls; 4.615 + ls.s = buff; 4.616 + ls.size = size; 4.617 + return lua_load(L, getS, &ls, name); 4.618 +} 4.619 + 4.620 + 4.621 +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) { 4.622 + return luaL_loadbuffer(L, s, strlen(s), s); 4.623 +} 4.624 + 4.625 + 4.626 + 4.627 +/* }====================================================== */ 4.628 + 4.629 + 4.630 +static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { 4.631 + (void)ud; 4.632 + (void)osize; 4.633 + if (nsize == 0) { 4.634 + free(ptr); 4.635 + return NULL; 4.636 + } 4.637 + else 4.638 + return realloc(ptr, nsize); 4.639 +} 4.640 + 4.641 + 4.642 +static int panic (lua_State *L) { 4.643 + (void)L; /* to avoid warnings */ 4.644 + fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", 4.645 + lua_tostring(L, -1)); 4.646 + return 0; 4.647 +} 4.648 + 4.649 + 4.650 +LUALIB_API lua_State *luaL_newstate (void) { 4.651 + lua_State *L = lua_newstate(l_alloc, NULL); 4.652 + if (L) lua_atpanic(L, &panic); 4.653 + return L; 4.654 +} 4.655 +
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/lua/lauxlib.h Sat Mar 03 11:07:39 2012 -0600 5.3 @@ -0,0 +1,174 @@ 5.4 +/* 5.5 +** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ 5.6 +** Auxiliary functions for building Lua libraries 5.7 +** See Copyright Notice in lua.h 5.8 +*/ 5.9 + 5.10 + 5.11 +#ifndef lauxlib_h 5.12 +#define lauxlib_h 5.13 + 5.14 + 5.15 +#include <stddef.h> 5.16 +#include <stdio.h> 5.17 + 5.18 +#include "lua.h" 5.19 + 5.20 + 5.21 +#if defined(LUA_COMPAT_GETN) 5.22 +LUALIB_API int (luaL_getn) (lua_State *L, int t); 5.23 +LUALIB_API void (luaL_setn) (lua_State *L, int t, int n); 5.24 +#else 5.25 +#define luaL_getn(L,i) ((int)lua_objlen(L, i)) 5.26 +#define luaL_setn(L,i,j) ((void)0) /* no op! */ 5.27 +#endif 5.28 + 5.29 +#if defined(LUA_COMPAT_OPENLIB) 5.30 +#define luaI_openlib luaL_openlib 5.31 +#endif 5.32 + 5.33 + 5.34 +/* extra error code for `luaL_load' */ 5.35 +#define LUA_ERRFILE (LUA_ERRERR+1) 5.36 + 5.37 + 5.38 +typedef struct luaL_Reg { 5.39 + const char *name; 5.40 + lua_CFunction func; 5.41 +} luaL_Reg; 5.42 + 5.43 + 5.44 + 5.45 +LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname, 5.46 + const luaL_Reg *l, int nup); 5.47 +LUALIB_API void (luaL_register) (lua_State *L, const char *libname, 5.48 + const luaL_Reg *l); 5.49 +LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 5.50 +LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 5.51 +LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); 5.52 +LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); 5.53 +LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, 5.54 + size_t *l); 5.55 +LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, 5.56 + const char *def, size_t *l); 5.57 +LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); 5.58 +LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); 5.59 + 5.60 +LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); 5.61 +LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, 5.62 + lua_Integer def); 5.63 + 5.64 +LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 5.65 +LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); 5.66 +LUALIB_API void (luaL_checkany) (lua_State *L, int narg); 5.67 + 5.68 +LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 5.69 +LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 5.70 + 5.71 +LUALIB_API void (luaL_where) (lua_State *L, int lvl); 5.72 +LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 5.73 + 5.74 +LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, 5.75 + const char *const lst[]); 5.76 + 5.77 +LUALIB_API int (luaL_ref) (lua_State *L, int t); 5.78 +LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 5.79 + 5.80 +LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); 5.81 +LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, 5.82 + const char *name); 5.83 +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 5.84 + 5.85 +LUALIB_API lua_State *(luaL_newstate) (void); 5.86 + 5.87 + 5.88 +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 5.89 + const char *r); 5.90 + 5.91 +LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, 5.92 + const char *fname, int szhint); 5.93 + 5.94 + 5.95 + 5.96 + 5.97 +/* 5.98 +** =============================================================== 5.99 +** some useful macros 5.100 +** =============================================================== 5.101 +*/ 5.102 + 5.103 +#define luaL_argcheck(L, cond,numarg,extramsg) \ 5.104 + ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) 5.105 +#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 5.106 +#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 5.107 +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 5.108 +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 5.109 +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 5.110 +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 5.111 + 5.112 +#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 5.113 + 5.114 +#define luaL_dofile(L, fn) \ 5.115 + (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 5.116 + 5.117 +#define luaL_dostring(L, s) \ 5.118 + (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 5.119 + 5.120 +#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 5.121 + 5.122 +#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 5.123 + 5.124 +/* 5.125 +** {====================================================== 5.126 +** Generic Buffer manipulation 5.127 +** ======================================================= 5.128 +*/ 5.129 + 5.130 + 5.131 + 5.132 +typedef struct luaL_Buffer { 5.133 + char *p; /* current position in buffer */ 5.134 + int lvl; /* number of strings in the stack (level) */ 5.135 + lua_State *L; 5.136 + char buffer[LUAL_BUFFERSIZE]; 5.137 +} luaL_Buffer; 5.138 + 5.139 +#define luaL_addchar(B,c) \ 5.140 + ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ 5.141 + (*(B)->p++ = (char)(c))) 5.142 + 5.143 +/* compatibility only */ 5.144 +#define luaL_putchar(B,c) luaL_addchar(B,c) 5.145 + 5.146 +#define luaL_addsize(B,n) ((B)->p += (n)) 5.147 + 5.148 +LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 5.149 +LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); 5.150 +LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 5.151 +LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 5.152 +LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 5.153 +LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 5.154 + 5.155 + 5.156 +/* }====================================================== */ 5.157 + 5.158 + 5.159 +/* compatibility with ref system */ 5.160 + 5.161 +/* pre-defined references */ 5.162 +#define LUA_NOREF (-2) 5.163 +#define LUA_REFNIL (-1) 5.164 + 5.165 +#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ 5.166 + (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) 5.167 + 5.168 +#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) 5.169 + 5.170 +#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) 5.171 + 5.172 + 5.173 +#define luaL_reg luaL_Reg 5.174 + 5.175 +#endif 5.176 + 5.177 +
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/lua/lbaselib.c Sat Mar 03 11:07:39 2012 -0600 6.3 @@ -0,0 +1,653 @@ 6.4 +/* 6.5 +** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $ 6.6 +** Basic library 6.7 +** See Copyright Notice in lua.h 6.8 +*/ 6.9 + 6.10 + 6.11 + 6.12 +#include <ctype.h> 6.13 +#include <stdio.h> 6.14 +#include <stdlib.h> 6.15 +#include <string.h> 6.16 + 6.17 +#define lbaselib_c 6.18 +#define LUA_LIB 6.19 + 6.20 +#include "lua.h" 6.21 + 6.22 +#include "lauxlib.h" 6.23 +#include "lualib.h" 6.24 + 6.25 + 6.26 + 6.27 + 6.28 +/* 6.29 +** If your system does not support `stdout', you can just remove this function. 6.30 +** If you need, you can define your own `print' function, following this 6.31 +** model but changing `fputs' to put the strings at a proper place 6.32 +** (a console window or a log file, for instance). 6.33 +*/ 6.34 +static int luaB_print (lua_State *L) { 6.35 + int n = lua_gettop(L); /* number of arguments */ 6.36 + int i; 6.37 + lua_getglobal(L, "tostring"); 6.38 + for (i=1; i<=n; i++) { 6.39 + const char *s; 6.40 + lua_pushvalue(L, -1); /* function to be called */ 6.41 + lua_pushvalue(L, i); /* value to print */ 6.42 + lua_call(L, 1, 1); 6.43 + s = lua_tostring(L, -1); /* get result */ 6.44 + if (s == NULL) 6.45 + return luaL_error(L, LUA_QL("tostring") " must return a string to " 6.46 + LUA_QL("print")); 6.47 + if (i>1) fputs("\t", stdout); 6.48 + fputs(s, stdout); 6.49 + lua_pop(L, 1); /* pop result */ 6.50 + } 6.51 + fputs("\n", stdout); 6.52 + return 0; 6.53 +} 6.54 + 6.55 + 6.56 +static int luaB_tonumber (lua_State *L) { 6.57 + int base = luaL_optint(L, 2, 10); 6.58 + if (base == 10) { /* standard conversion */ 6.59 + luaL_checkany(L, 1); 6.60 + if (lua_isnumber(L, 1)) { 6.61 + lua_pushnumber(L, lua_tonumber(L, 1)); 6.62 + return 1; 6.63 + } 6.64 + } 6.65 + else { 6.66 + const char *s1 = luaL_checkstring(L, 1); 6.67 + char *s2; 6.68 + unsigned long n; 6.69 + luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); 6.70 + n = strtoul(s1, &s2, base); 6.71 + if (s1 != s2) { /* at least one valid digit? */ 6.72 + while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ 6.73 + if (*s2 == '\0') { /* no invalid trailing characters? */ 6.74 + lua_pushnumber(L, (lua_Number)n); 6.75 + return 1; 6.76 + } 6.77 + } 6.78 + } 6.79 + lua_pushnil(L); /* else not a number */ 6.80 + return 1; 6.81 +} 6.82 + 6.83 + 6.84 +static int luaB_error (lua_State *L) { 6.85 + int level = luaL_optint(L, 2, 1); 6.86 + lua_settop(L, 1); 6.87 + if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ 6.88 + luaL_where(L, level); 6.89 + lua_pushvalue(L, 1); 6.90 + lua_concat(L, 2); 6.91 + } 6.92 + return lua_error(L); 6.93 +} 6.94 + 6.95 + 6.96 +static int luaB_getmetatable (lua_State *L) { 6.97 + luaL_checkany(L, 1); 6.98 + if (!lua_getmetatable(L, 1)) { 6.99 + lua_pushnil(L); 6.100 + return 1; /* no metatable */ 6.101 + } 6.102 + luaL_getmetafield(L, 1, "__metatable"); 6.103 + return 1; /* returns either __metatable field (if present) or metatable */ 6.104 +} 6.105 + 6.106 + 6.107 +static int luaB_setmetatable (lua_State *L) { 6.108 + int t = lua_type(L, 2); 6.109 + luaL_checktype(L, 1, LUA_TTABLE); 6.110 + luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 6.111 + "nil or table expected"); 6.112 + if (luaL_getmetafield(L, 1, "__metatable")) 6.113 + luaL_error(L, "cannot change a protected metatable"); 6.114 + lua_settop(L, 2); 6.115 + lua_setmetatable(L, 1); 6.116 + return 1; 6.117 +} 6.118 + 6.119 + 6.120 +static void getfunc (lua_State *L, int opt) { 6.121 + if (lua_isfunction(L, 1)) lua_pushvalue(L, 1); 6.122 + else { 6.123 + lua_Debug ar; 6.124 + int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1); 6.125 + luaL_argcheck(L, level >= 0, 1, "level must be non-negative"); 6.126 + if (lua_getstack(L, level, &ar) == 0) 6.127 + luaL_argerror(L, 1, "invalid level"); 6.128 + lua_getinfo(L, "f", &ar); 6.129 + if (lua_isnil(L, -1)) 6.130 + luaL_error(L, "no function environment for tail call at level %d", 6.131 + level); 6.132 + } 6.133 +} 6.134 + 6.135 + 6.136 +static int luaB_getfenv (lua_State *L) { 6.137 + getfunc(L, 1); 6.138 + if (lua_iscfunction(L, -1)) /* is a C function? */ 6.139 + lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */ 6.140 + else 6.141 + lua_getfenv(L, -1); 6.142 + return 1; 6.143 +} 6.144 + 6.145 + 6.146 +static int luaB_setfenv (lua_State *L) { 6.147 + luaL_checktype(L, 2, LUA_TTABLE); 6.148 + getfunc(L, 0); 6.149 + lua_pushvalue(L, 2); 6.150 + if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) { 6.151 + /* change environment of current thread */ 6.152 + lua_pushthread(L); 6.153 + lua_insert(L, -2); 6.154 + lua_setfenv(L, -2); 6.155 + return 0; 6.156 + } 6.157 + else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0) 6.158 + luaL_error(L, 6.159 + LUA_QL("setfenv") " cannot change environment of given object"); 6.160 + return 1; 6.161 +} 6.162 + 6.163 + 6.164 +static int luaB_rawequal (lua_State *L) { 6.165 + luaL_checkany(L, 1); 6.166 + luaL_checkany(L, 2); 6.167 + lua_pushboolean(L, lua_rawequal(L, 1, 2)); 6.168 + return 1; 6.169 +} 6.170 + 6.171 + 6.172 +static int luaB_rawget (lua_State *L) { 6.173 + luaL_checktype(L, 1, LUA_TTABLE); 6.174 + luaL_checkany(L, 2); 6.175 + lua_settop(L, 2); 6.176 + lua_rawget(L, 1); 6.177 + return 1; 6.178 +} 6.179 + 6.180 +static int luaB_rawset (lua_State *L) { 6.181 + luaL_checktype(L, 1, LUA_TTABLE); 6.182 + luaL_checkany(L, 2); 6.183 + luaL_checkany(L, 3); 6.184 + lua_settop(L, 3); 6.185 + lua_rawset(L, 1); 6.186 + return 1; 6.187 +} 6.188 + 6.189 + 6.190 +static int luaB_gcinfo (lua_State *L) { 6.191 + lua_pushinteger(L, lua_getgccount(L)); 6.192 + return 1; 6.193 +} 6.194 + 6.195 + 6.196 +static int luaB_collectgarbage (lua_State *L) { 6.197 + static const char *const opts[] = {"stop", "restart", "collect", 6.198 + "count", "step", "setpause", "setstepmul", NULL}; 6.199 + static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, 6.200 + LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL}; 6.201 + int o = luaL_checkoption(L, 1, "collect", opts); 6.202 + int ex = luaL_optint(L, 2, 0); 6.203 + int res = lua_gc(L, optsnum[o], ex); 6.204 + switch (optsnum[o]) { 6.205 + case LUA_GCCOUNT: { 6.206 + int b = lua_gc(L, LUA_GCCOUNTB, 0); 6.207 + lua_pushnumber(L, res + ((lua_Number)b/1024)); 6.208 + return 1; 6.209 + } 6.210 + case LUA_GCSTEP: { 6.211 + lua_pushboolean(L, res); 6.212 + return 1; 6.213 + } 6.214 + default: { 6.215 + lua_pushnumber(L, res); 6.216 + return 1; 6.217 + } 6.218 + } 6.219 +} 6.220 + 6.221 + 6.222 +static int luaB_type (lua_State *L) { 6.223 + luaL_checkany(L, 1); 6.224 + lua_pushstring(L, luaL_typename(L, 1)); 6.225 + return 1; 6.226 +} 6.227 + 6.228 + 6.229 +static int luaB_next (lua_State *L) { 6.230 + luaL_checktype(L, 1, LUA_TTABLE); 6.231 + lua_settop(L, 2); /* create a 2nd argument if there isn't one */ 6.232 + if (lua_next(L, 1)) 6.233 + return 2; 6.234 + else { 6.235 + lua_pushnil(L); 6.236 + return 1; 6.237 + } 6.238 +} 6.239 + 6.240 + 6.241 +static int luaB_pairs (lua_State *L) { 6.242 + luaL_checktype(L, 1, LUA_TTABLE); 6.243 + lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ 6.244 + lua_pushvalue(L, 1); /* state, */ 6.245 + lua_pushnil(L); /* and initial value */ 6.246 + return 3; 6.247 +} 6.248 + 6.249 + 6.250 +static int ipairsaux (lua_State *L) { 6.251 + int i = luaL_checkint(L, 2); 6.252 + luaL_checktype(L, 1, LUA_TTABLE); 6.253 + i++; /* next value */ 6.254 + lua_pushinteger(L, i); 6.255 + lua_rawgeti(L, 1, i); 6.256 + return (lua_isnil(L, -1)) ? 0 : 2; 6.257 +} 6.258 + 6.259 + 6.260 +static int luaB_ipairs (lua_State *L) { 6.261 + luaL_checktype(L, 1, LUA_TTABLE); 6.262 + lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ 6.263 + lua_pushvalue(L, 1); /* state, */ 6.264 + lua_pushinteger(L, 0); /* and initial value */ 6.265 + return 3; 6.266 +} 6.267 + 6.268 + 6.269 +static int load_aux (lua_State *L, int status) { 6.270 + if (status == 0) /* OK? */ 6.271 + return 1; 6.272 + else { 6.273 + lua_pushnil(L); 6.274 + lua_insert(L, -2); /* put before error message */ 6.275 + return 2; /* return nil plus error message */ 6.276 + } 6.277 +} 6.278 + 6.279 + 6.280 +static int luaB_loadstring (lua_State *L) { 6.281 + size_t l; 6.282 + const char *s = luaL_checklstring(L, 1, &l); 6.283 + const char *chunkname = luaL_optstring(L, 2, s); 6.284 + return load_aux(L, luaL_loadbuffer(L, s, l, chunkname)); 6.285 +} 6.286 + 6.287 + 6.288 +static int luaB_loadfile (lua_State *L) { 6.289 + const char *fname = luaL_optstring(L, 1, NULL); 6.290 + return load_aux(L, luaL_loadfile(L, fname)); 6.291 +} 6.292 + 6.293 + 6.294 +/* 6.295 +** Reader for generic `load' function: `lua_load' uses the 6.296 +** stack for internal stuff, so the reader cannot change the 6.297 +** stack top. Instead, it keeps its resulting string in a 6.298 +** reserved slot inside the stack. 6.299 +*/ 6.300 +static const char *generic_reader (lua_State *L, void *ud, size_t *size) { 6.301 + (void)ud; /* to avoid warnings */ 6.302 + luaL_checkstack(L, 2, "too many nested functions"); 6.303 + lua_pushvalue(L, 1); /* get function */ 6.304 + lua_call(L, 0, 1); /* call it */ 6.305 + if (lua_isnil(L, -1)) { 6.306 + *size = 0; 6.307 + return NULL; 6.308 + } 6.309 + else if (lua_isstring(L, -1)) { 6.310 + lua_replace(L, 3); /* save string in a reserved stack slot */ 6.311 + return lua_tolstring(L, 3, size); 6.312 + } 6.313 + else luaL_error(L, "reader function must return a string"); 6.314 + return NULL; /* to avoid warnings */ 6.315 +} 6.316 + 6.317 + 6.318 +static int luaB_load (lua_State *L) { 6.319 + int status; 6.320 + const char *cname = luaL_optstring(L, 2, "=(load)"); 6.321 + luaL_checktype(L, 1, LUA_TFUNCTION); 6.322 + lua_settop(L, 3); /* function, eventual name, plus one reserved slot */ 6.323 + status = lua_load(L, generic_reader, NULL, cname); 6.324 + return load_aux(L, status); 6.325 +} 6.326 + 6.327 + 6.328 +static int luaB_dofile (lua_State *L) { 6.329 + const char *fname = luaL_optstring(L, 1, NULL); 6.330 + int n = lua_gettop(L); 6.331 + if (luaL_loadfile(L, fname) != 0) lua_error(L); 6.332 + lua_call(L, 0, LUA_MULTRET); 6.333 + return lua_gettop(L) - n; 6.334 +} 6.335 + 6.336 + 6.337 +static int luaB_assert (lua_State *L) { 6.338 + luaL_checkany(L, 1); 6.339 + if (!lua_toboolean(L, 1)) 6.340 + return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); 6.341 + return lua_gettop(L); 6.342 +} 6.343 + 6.344 + 6.345 +static int luaB_unpack (lua_State *L) { 6.346 + int i, e, n; 6.347 + luaL_checktype(L, 1, LUA_TTABLE); 6.348 + i = luaL_optint(L, 2, 1); 6.349 + e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); 6.350 + if (i > e) return 0; /* empty range */ 6.351 + n = e - i + 1; /* number of elements */ 6.352 + if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ 6.353 + return luaL_error(L, "too many results to unpack"); 6.354 + lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ 6.355 + while (i++ < e) /* push arg[i + 1...e] */ 6.356 + lua_rawgeti(L, 1, i); 6.357 + return n; 6.358 +} 6.359 + 6.360 + 6.361 +static int luaB_select (lua_State *L) { 6.362 + int n = lua_gettop(L); 6.363 + if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { 6.364 + lua_pushinteger(L, n-1); 6.365 + return 1; 6.366 + } 6.367 + else { 6.368 + int i = luaL_checkint(L, 1); 6.369 + if (i < 0) i = n + i; 6.370 + else if (i > n) i = n; 6.371 + luaL_argcheck(L, 1 <= i, 1, "index out of range"); 6.372 + return n - i; 6.373 + } 6.374 +} 6.375 + 6.376 + 6.377 +static int luaB_pcall (lua_State *L) { 6.378 + int status; 6.379 + luaL_checkany(L, 1); 6.380 + status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0); 6.381 + lua_pushboolean(L, (status == 0)); 6.382 + lua_insert(L, 1); 6.383 + return lua_gettop(L); /* return status + all results */ 6.384 +} 6.385 + 6.386 + 6.387 +static int luaB_xpcall (lua_State *L) { 6.388 + int status; 6.389 + luaL_checkany(L, 2); 6.390 + lua_settop(L, 2); 6.391 + lua_insert(L, 1); /* put error function under function to be called */ 6.392 + status = lua_pcall(L, 0, LUA_MULTRET, 1); 6.393 + lua_pushboolean(L, (status == 0)); 6.394 + lua_replace(L, 1); 6.395 + return lua_gettop(L); /* return status + all results */ 6.396 +} 6.397 + 6.398 + 6.399 +static int luaB_tostring (lua_State *L) { 6.400 + luaL_checkany(L, 1); 6.401 + if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */ 6.402 + return 1; /* use its value */ 6.403 + switch (lua_type(L, 1)) { 6.404 + case LUA_TNUMBER: 6.405 + lua_pushstring(L, lua_tostring(L, 1)); 6.406 + break; 6.407 + case LUA_TSTRING: 6.408 + lua_pushvalue(L, 1); 6.409 + break; 6.410 + case LUA_TBOOLEAN: 6.411 + lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); 6.412 + break; 6.413 + case LUA_TNIL: 6.414 + lua_pushliteral(L, "nil"); 6.415 + break; 6.416 + default: 6.417 + lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1)); 6.418 + break; 6.419 + } 6.420 + return 1; 6.421 +} 6.422 + 6.423 + 6.424 +static int luaB_newproxy (lua_State *L) { 6.425 + lua_settop(L, 1); 6.426 + lua_newuserdata(L, 0); /* create proxy */ 6.427 + if (lua_toboolean(L, 1) == 0) 6.428 + return 1; /* no metatable */ 6.429 + else if (lua_isboolean(L, 1)) { 6.430 + lua_newtable(L); /* create a new metatable `m' ... */ 6.431 + lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */ 6.432 + lua_pushboolean(L, 1); 6.433 + lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */ 6.434 + } 6.435 + else { 6.436 + int validproxy = 0; /* to check if weaktable[metatable(u)] == true */ 6.437 + if (lua_getmetatable(L, 1)) { 6.438 + lua_rawget(L, lua_upvalueindex(1)); 6.439 + validproxy = lua_toboolean(L, -1); 6.440 + lua_pop(L, 1); /* remove value */ 6.441 + } 6.442 + luaL_argcheck(L, validproxy, 1, "boolean or proxy expected"); 6.443 + lua_getmetatable(L, 1); /* metatable is valid; get it */ 6.444 + } 6.445 + lua_setmetatable(L, 2); 6.446 + return 1; 6.447 +} 6.448 + 6.449 + 6.450 +static const luaL_Reg base_funcs[] = { 6.451 + {"assert", luaB_assert}, 6.452 + {"collectgarbage", luaB_collectgarbage}, 6.453 + {"dofile", luaB_dofile}, 6.454 + {"error", luaB_error}, 6.455 + {"gcinfo", luaB_gcinfo}, 6.456 + {"getfenv", luaB_getfenv}, 6.457 + {"getmetatable", luaB_getmetatable}, 6.458 + {"loadfile", luaB_loadfile}, 6.459 + {"load", luaB_load}, 6.460 + {"loadstring", luaB_loadstring}, 6.461 + {"next", luaB_next}, 6.462 + {"pcall", luaB_pcall}, 6.463 + {"print", luaB_print}, 6.464 + {"rawequal", luaB_rawequal}, 6.465 + {"rawget", luaB_rawget}, 6.466 + {"rawset", luaB_rawset}, 6.467 + {"select", luaB_select}, 6.468 + {"setfenv", luaB_setfenv}, 6.469 + {"setmetatable", luaB_setmetatable}, 6.470 + {"tonumber", luaB_tonumber}, 6.471 + {"tostring", luaB_tostring}, 6.472 + {"type", luaB_type}, 6.473 + {"unpack", luaB_unpack}, 6.474 + {"xpcall", luaB_xpcall}, 6.475 + {NULL, NULL} 6.476 +}; 6.477 + 6.478 + 6.479 +/* 6.480 +** {====================================================== 6.481 +** Coroutine library 6.482 +** ======================================================= 6.483 +*/ 6.484 + 6.485 +#define CO_RUN 0 /* running */ 6.486 +#define CO_SUS 1 /* suspended */ 6.487 +#define CO_NOR 2 /* 'normal' (it resumed another coroutine) */ 6.488 +#define CO_DEAD 3 6.489 + 6.490 +static const char *const statnames[] = 6.491 + {"running", "suspended", "normal", "dead"}; 6.492 + 6.493 +static int costatus (lua_State *L, lua_State *co) { 6.494 + if (L == co) return CO_RUN; 6.495 + switch (lua_status(co)) { 6.496 + case LUA_YIELD: 6.497 + return CO_SUS; 6.498 + case 0: { 6.499 + lua_Debug ar; 6.500 + if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 6.501 + return CO_NOR; /* it is running */ 6.502 + else if (lua_gettop(co) == 0) 6.503 + return CO_DEAD; 6.504 + else 6.505 + return CO_SUS; /* initial state */ 6.506 + } 6.507 + default: /* some error occured */ 6.508 + return CO_DEAD; 6.509 + } 6.510 +} 6.511 + 6.512 + 6.513 +static int luaB_costatus (lua_State *L) { 6.514 + lua_State *co = lua_tothread(L, 1); 6.515 + luaL_argcheck(L, co, 1, "coroutine expected"); 6.516 + lua_pushstring(L, statnames[costatus(L, co)]); 6.517 + return 1; 6.518 +} 6.519 + 6.520 + 6.521 +static int auxresume (lua_State *L, lua_State *co, int narg) { 6.522 + int status = costatus(L, co); 6.523 + if (!lua_checkstack(co, narg)) 6.524 + luaL_error(L, "too many arguments to resume"); 6.525 + if (status != CO_SUS) { 6.526 + lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]); 6.527 + return -1; /* error flag */ 6.528 + } 6.529 + lua_xmove(L, co, narg); 6.530 + lua_setlevel(L, co); 6.531 + status = lua_resume(co, narg); 6.532 + if (status == 0 || status == LUA_YIELD) { 6.533 + int nres = lua_gettop(co); 6.534 + if (!lua_checkstack(L, nres + 1)) 6.535 + luaL_error(L, "too many results to resume"); 6.536 + lua_xmove(co, L, nres); /* move yielded values */ 6.537 + return nres; 6.538 + } 6.539 + else { 6.540 + lua_xmove(co, L, 1); /* move error message */ 6.541 + return -1; /* error flag */ 6.542 + } 6.543 +} 6.544 + 6.545 + 6.546 +static int luaB_coresume (lua_State *L) { 6.547 + lua_State *co = lua_tothread(L, 1); 6.548 + int r; 6.549 + luaL_argcheck(L, co, 1, "coroutine expected"); 6.550 + r = auxresume(L, co, lua_gettop(L) - 1); 6.551 + if (r < 0) { 6.552 + lua_pushboolean(L, 0); 6.553 + lua_insert(L, -2); 6.554 + return 2; /* return false + error message */ 6.555 + } 6.556 + else { 6.557 + lua_pushboolean(L, 1); 6.558 + lua_insert(L, -(r + 1)); 6.559 + return r + 1; /* return true + `resume' returns */ 6.560 + } 6.561 +} 6.562 + 6.563 + 6.564 +static int luaB_auxwrap (lua_State *L) { 6.565 + lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 6.566 + int r = auxresume(L, co, lua_gettop(L)); 6.567 + if (r < 0) { 6.568 + if (lua_isstring(L, -1)) { /* error object is a string? */ 6.569 + luaL_where(L, 1); /* add extra info */ 6.570 + lua_insert(L, -2); 6.571 + lua_concat(L, 2); 6.572 + } 6.573 + lua_error(L); /* propagate error */ 6.574 + } 6.575 + return r; 6.576 +} 6.577 + 6.578 + 6.579 +static int luaB_cocreate (lua_State *L) { 6.580 + lua_State *NL = lua_newthread(L); 6.581 + luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, 6.582 + "Lua function expected"); 6.583 + lua_pushvalue(L, 1); /* move function to top */ 6.584 + lua_xmove(L, NL, 1); /* move function from L to NL */ 6.585 + return 1; 6.586 +} 6.587 + 6.588 + 6.589 +static int luaB_cowrap (lua_State *L) { 6.590 + luaB_cocreate(L); 6.591 + lua_pushcclosure(L, luaB_auxwrap, 1); 6.592 + return 1; 6.593 +} 6.594 + 6.595 + 6.596 +static int luaB_yield (lua_State *L) { 6.597 + return lua_yield(L, lua_gettop(L)); 6.598 +} 6.599 + 6.600 + 6.601 +static int luaB_corunning (lua_State *L) { 6.602 + if (lua_pushthread(L)) 6.603 + lua_pushnil(L); /* main thread is not a coroutine */ 6.604 + return 1; 6.605 +} 6.606 + 6.607 + 6.608 +static const luaL_Reg co_funcs[] = { 6.609 + {"create", luaB_cocreate}, 6.610 + {"resume", luaB_coresume}, 6.611 + {"running", luaB_corunning}, 6.612 + {"status", luaB_costatus}, 6.613 + {"wrap", luaB_cowrap}, 6.614 + {"yield", luaB_yield}, 6.615 + {NULL, NULL} 6.616 +}; 6.617 + 6.618 +/* }====================================================== */ 6.619 + 6.620 + 6.621 +static void auxopen (lua_State *L, const char *name, 6.622 + lua_CFunction f, lua_CFunction u) { 6.623 + lua_pushcfunction(L, u); 6.624 + lua_pushcclosure(L, f, 1); 6.625 + lua_setfield(L, -2, name); 6.626 +} 6.627 + 6.628 + 6.629 +static void base_open (lua_State *L) { 6.630 + /* set global _G */ 6.631 + lua_pushvalue(L, LUA_GLOBALSINDEX); 6.632 + lua_setglobal(L, "_G"); 6.633 + /* open lib into global table */ 6.634 + luaL_register(L, "_G", base_funcs); 6.635 + lua_pushliteral(L, LUA_VERSION); 6.636 + lua_setglobal(L, "_VERSION"); /* set global _VERSION */ 6.637 + /* `ipairs' and `pairs' need auxliliary functions as upvalues */ 6.638 + auxopen(L, "ipairs", luaB_ipairs, ipairsaux); 6.639 + auxopen(L, "pairs", luaB_pairs, luaB_next); 6.640 + /* `newproxy' needs a weaktable as upvalue */ 6.641 + lua_createtable(L, 0, 1); /* new table `w' */ 6.642 + lua_pushvalue(L, -1); /* `w' will be its own metatable */ 6.643 + lua_setmetatable(L, -2); 6.644 + lua_pushliteral(L, "kv"); 6.645 + lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ 6.646 + lua_pushcclosure(L, luaB_newproxy, 1); 6.647 + lua_setglobal(L, "newproxy"); /* set global `newproxy' */ 6.648 +} 6.649 + 6.650 + 6.651 +LUALIB_API int luaopen_base (lua_State *L) { 6.652 + base_open(L); 6.653 + luaL_register(L, LUA_COLIBNAME, co_funcs); 6.654 + return 2; 6.655 +} 6.656 +
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/src/lua/lcode.c Sat Mar 03 11:07:39 2012 -0600 7.3 @@ -0,0 +1,839 @@ 7.4 +/* 7.5 +** $Id: lcode.c,v 2.25.1.3 2007/12/28 15:32:23 roberto Exp $ 7.6 +** Code generator for Lua 7.7 +** See Copyright Notice in lua.h 7.8 +*/ 7.9 + 7.10 + 7.11 +#include <stdlib.h> 7.12 + 7.13 +#define lcode_c 7.14 +#define LUA_CORE 7.15 + 7.16 +#include "lua.h" 7.17 + 7.18 +#include "lcode.h" 7.19 +#include "ldebug.h" 7.20 +#include "ldo.h" 7.21 +#include "lgc.h" 7.22 +#include "llex.h" 7.23 +#include "lmem.h" 7.24 +#include "lobject.h" 7.25 +#include "lopcodes.h" 7.26 +#include "lparser.h" 7.27 +#include "ltable.h" 7.28 + 7.29 + 7.30 +#define hasjumps(e) ((e)->t != (e)->f) 7.31 + 7.32 + 7.33 +static int isnumeral(expdesc *e) { 7.34 + return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP); 7.35 +} 7.36 + 7.37 + 7.38 +void luaK_nil (FuncState *fs, int from, int n) { 7.39 + Instruction *previous; 7.40 + if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ 7.41 + if (fs->pc == 0) { /* function start? */ 7.42 + if (from >= fs->nactvar) 7.43 + return; /* positions are already clean */ 7.44 + } 7.45 + else { 7.46 + previous = &fs->f->code[fs->pc-1]; 7.47 + if (GET_OPCODE(*previous) == OP_LOADNIL) { 7.48 + int pfrom = GETARG_A(*previous); 7.49 + int pto = GETARG_B(*previous); 7.50 + if (pfrom <= from && from <= pto+1) { /* can connect both? */ 7.51 + if (from+n-1 > pto) 7.52 + SETARG_B(*previous, from+n-1); 7.53 + return; 7.54 + } 7.55 + } 7.56 + } 7.57 + } 7.58 + luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */ 7.59 +} 7.60 + 7.61 + 7.62 +int luaK_jump (FuncState *fs) { 7.63 + int jpc = fs->jpc; /* save list of jumps to here */ 7.64 + int j; 7.65 + fs->jpc = NO_JUMP; 7.66 + j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); 7.67 + luaK_concat(fs, &j, jpc); /* keep them on hold */ 7.68 + return j; 7.69 +} 7.70 + 7.71 + 7.72 +void luaK_ret (FuncState *fs, int first, int nret) { 7.73 + luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); 7.74 +} 7.75 + 7.76 + 7.77 +static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { 7.78 + luaK_codeABC(fs, op, A, B, C); 7.79 + return luaK_jump(fs); 7.80 +} 7.81 + 7.82 + 7.83 +static void fixjump (FuncState *fs, int pc, int dest) { 7.84 + Instruction *jmp = &fs->f->code[pc]; 7.85 + int offset = dest-(pc+1); 7.86 + lua_assert(dest != NO_JUMP); 7.87 + if (abs(offset) > MAXARG_sBx) 7.88 + luaX_syntaxerror(fs->ls, "control structure too long"); 7.89 + SETARG_sBx(*jmp, offset); 7.90 +} 7.91 + 7.92 + 7.93 +/* 7.94 +** returns current `pc' and marks it as a jump target (to avoid wrong 7.95 +** optimizations with consecutive instructions not in the same basic block). 7.96 +*/ 7.97 +int luaK_getlabel (FuncState *fs) { 7.98 + fs->lasttarget = fs->pc; 7.99 + return fs->pc; 7.100 +} 7.101 + 7.102 + 7.103 +static int getjump (FuncState *fs, int pc) { 7.104 + int offset = GETARG_sBx(fs->f->code[pc]); 7.105 + if (offset == NO_JUMP) /* point to itself represents end of list */ 7.106 + return NO_JUMP; /* end of list */ 7.107 + else 7.108 + return (pc+1)+offset; /* turn offset into absolute position */ 7.109 +} 7.110 + 7.111 + 7.112 +static Instruction *getjumpcontrol (FuncState *fs, int pc) { 7.113 + Instruction *pi = &fs->f->code[pc]; 7.114 + if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) 7.115 + return pi-1; 7.116 + else 7.117 + return pi; 7.118 +} 7.119 + 7.120 + 7.121 +/* 7.122 +** check whether list has any jump that do not produce a value 7.123 +** (or produce an inverted value) 7.124 +*/ 7.125 +static int need_value (FuncState *fs, int list) { 7.126 + for (; list != NO_JUMP; list = getjump(fs, list)) { 7.127 + Instruction i = *getjumpcontrol(fs, list); 7.128 + if (GET_OPCODE(i) != OP_TESTSET) return 1; 7.129 + } 7.130 + return 0; /* not found */ 7.131 +} 7.132 + 7.133 + 7.134 +static int patchtestreg (FuncState *fs, int node, int reg) { 7.135 + Instruction *i = getjumpcontrol(fs, node); 7.136 + if (GET_OPCODE(*i) != OP_TESTSET) 7.137 + return 0; /* cannot patch other instructions */ 7.138 + if (reg != NO_REG && reg != GETARG_B(*i)) 7.139 + SETARG_A(*i, reg); 7.140 + else /* no register to put value or register already has the value */ 7.141 + *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); 7.142 + 7.143 + return 1; 7.144 +} 7.145 + 7.146 + 7.147 +static void removevalues (FuncState *fs, int list) { 7.148 + for (; list != NO_JUMP; list = getjump(fs, list)) 7.149 + patchtestreg(fs, list, NO_REG); 7.150 +} 7.151 + 7.152 + 7.153 +static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, 7.154 + int dtarget) { 7.155 + while (list != NO_JUMP) { 7.156 + int next = getjump(fs, list); 7.157 + if (patchtestreg(fs, list, reg)) 7.158 + fixjump(fs, list, vtarget); 7.159 + else 7.160 + fixjump(fs, list, dtarget); /* jump to default target */ 7.161 + list = next; 7.162 + } 7.163 +} 7.164 + 7.165 + 7.166 +static void dischargejpc (FuncState *fs) { 7.167 + patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); 7.168 + fs->jpc = NO_JUMP; 7.169 +} 7.170 + 7.171 + 7.172 +void luaK_patchlist (FuncState *fs, int list, int target) { 7.173 + if (target == fs->pc) 7.174 + luaK_patchtohere(fs, list); 7.175 + else { 7.176 + lua_assert(target < fs->pc); 7.177 + patchlistaux(fs, list, target, NO_REG, target); 7.178 + } 7.179 +} 7.180 + 7.181 + 7.182 +void luaK_patchtohere (FuncState *fs, int list) { 7.183 + luaK_getlabel(fs); 7.184 + luaK_concat(fs, &fs->jpc, list); 7.185 +} 7.186 + 7.187 + 7.188 +void luaK_concat (FuncState *fs, int *l1, int l2) { 7.189 + if (l2 == NO_JUMP) return; 7.190 + else if (*l1 == NO_JUMP) 7.191 + *l1 = l2; 7.192 + else { 7.193 + int list = *l1; 7.194 + int next; 7.195 + while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ 7.196 + list = next; 7.197 + fixjump(fs, list, l2); 7.198 + } 7.199 +} 7.200 + 7.201 + 7.202 +void luaK_checkstack (FuncState *fs, int n) { 7.203 + int newstack = fs->freereg + n; 7.204 + if (newstack > fs->f->maxstacksize) { 7.205 + if (newstack >= MAXSTACK) 7.206 + luaX_syntaxerror(fs->ls, "function or expression too complex"); 7.207 + fs->f->maxstacksize = cast_byte(newstack); 7.208 + } 7.209 +} 7.210 + 7.211 + 7.212 +void luaK_reserveregs (FuncState *fs, int n) { 7.213 + luaK_checkstack(fs, n); 7.214 + fs->freereg += n; 7.215 +} 7.216 + 7.217 + 7.218 +static void freereg (FuncState *fs, int reg) { 7.219 + if (!ISK(reg) && reg >= fs->nactvar) { 7.220 + fs->freereg--; 7.221 + lua_assert(reg == fs->freereg); 7.222 + } 7.223 +} 7.224 + 7.225 + 7.226 +static void freeexp (FuncState *fs, expdesc *e) { 7.227 + if (e->k == VNONRELOC) 7.228 + freereg(fs, e->u.s.info); 7.229 +} 7.230 + 7.231 + 7.232 +static int addk (FuncState *fs, TValue *k, TValue *v) { 7.233 + lua_State *L = fs->L; 7.234 + TValue *idx = luaH_set(L, fs->h, k); 7.235 + Proto *f = fs->f; 7.236 + int oldsize = f->sizek; 7.237 + if (ttisnumber(idx)) { 7.238 + lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v)); 7.239 + return cast_int(nvalue(idx)); 7.240 + } 7.241 + else { /* constant not found; create a new entry */ 7.242 + setnvalue(idx, cast_num(fs->nk)); 7.243 + luaM_growvector(L, f->k, fs->nk, f->sizek, TValue, 7.244 + MAXARG_Bx, "constant table overflow"); 7.245 + while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); 7.246 + setobj(L, &f->k[fs->nk], v); 7.247 + luaC_barrier(L, f, v); 7.248 + return fs->nk++; 7.249 + } 7.250 +} 7.251 + 7.252 + 7.253 +int luaK_stringK (FuncState *fs, TString *s) { 7.254 + TValue o; 7.255 + setsvalue(fs->L, &o, s); 7.256 + return addk(fs, &o, &o); 7.257 +} 7.258 + 7.259 + 7.260 +int luaK_numberK (FuncState *fs, lua_Number r) { 7.261 + TValue o; 7.262 + setnvalue(&o, r); 7.263 + return addk(fs, &o, &o); 7.264 +} 7.265 + 7.266 + 7.267 +static int boolK (FuncState *fs, int b) { 7.268 + TValue o; 7.269 + setbvalue(&o, b); 7.270 + return addk(fs, &o, &o); 7.271 +} 7.272 + 7.273 + 7.274 +static int nilK (FuncState *fs) { 7.275 + TValue k, v; 7.276 + setnilvalue(&v); 7.277 + /* cannot use nil as key; instead use table itself to represent nil */ 7.278 + sethvalue(fs->L, &k, fs->h); 7.279 + return addk(fs, &k, &v); 7.280 +} 7.281 + 7.282 + 7.283 +void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { 7.284 + if (e->k == VCALL) { /* expression is an open function call? */ 7.285 + SETARG_C(getcode(fs, e), nresults+1); 7.286 + } 7.287 + else if (e->k == VVARARG) { 7.288 + SETARG_B(getcode(fs, e), nresults+1); 7.289 + SETARG_A(getcode(fs, e), fs->freereg); 7.290 + luaK_reserveregs(fs, 1); 7.291 + } 7.292 +} 7.293 + 7.294 + 7.295 +void luaK_setoneret (FuncState *fs, expdesc *e) { 7.296 + if (e->k == VCALL) { /* expression is an open function call? */ 7.297 + e->k = VNONRELOC; 7.298 + e->u.s.info = GETARG_A(getcode(fs, e)); 7.299 + } 7.300 + else if (e->k == VVARARG) { 7.301 + SETARG_B(getcode(fs, e), 2); 7.302 + e->k = VRELOCABLE; /* can relocate its simple result */ 7.303 + } 7.304 +} 7.305 + 7.306 + 7.307 +void luaK_dischargevars (FuncState *fs, expdesc *e) { 7.308 + switch (e->k) { 7.309 + case VLOCAL: { 7.310 + e->k = VNONRELOC; 7.311 + break; 7.312 + } 7.313 + case VUPVAL: { 7.314 + e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0); 7.315 + e->k = VRELOCABLE; 7.316 + break; 7.317 + } 7.318 + case VGLOBAL: { 7.319 + e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info); 7.320 + e->k = VRELOCABLE; 7.321 + break; 7.322 + } 7.323 + case VINDEXED: { 7.324 + freereg(fs, e->u.s.aux); 7.325 + freereg(fs, e->u.s.info); 7.326 + e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux); 7.327 + e->k = VRELOCABLE; 7.328 + break; 7.329 + } 7.330 + case VVARARG: 7.331 + case VCALL: { 7.332 + luaK_setoneret(fs, e); 7.333 + break; 7.334 + } 7.335 + default: break; /* there is one value available (somewhere) */ 7.336 + } 7.337 +} 7.338 + 7.339 + 7.340 +static int code_label (FuncState *fs, int A, int b, int jump) { 7.341 + luaK_getlabel(fs); /* those instructions may be jump targets */ 7.342 + return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); 7.343 +} 7.344 + 7.345 + 7.346 +static void discharge2reg (FuncState *fs, expdesc *e, int reg) { 7.347 + luaK_dischargevars(fs, e); 7.348 + switch (e->k) { 7.349 + case VNIL: { 7.350 + luaK_nil(fs, reg, 1); 7.351 + break; 7.352 + } 7.353 + case VFALSE: case VTRUE: { 7.354 + luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); 7.355 + break; 7.356 + } 7.357 + case VK: { 7.358 + luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info); 7.359 + break; 7.360 + } 7.361 + case VKNUM: { 7.362 + luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval)); 7.363 + break; 7.364 + } 7.365 + case VRELOCABLE: { 7.366 + Instruction *pc = &getcode(fs, e); 7.367 + SETARG_A(*pc, reg); 7.368 + break; 7.369 + } 7.370 + case VNONRELOC: { 7.371 + if (reg != e->u.s.info) 7.372 + luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0); 7.373 + break; 7.374 + } 7.375 + default: { 7.376 + lua_assert(e->k == VVOID || e->k == VJMP); 7.377 + return; /* nothing to do... */ 7.378 + } 7.379 + } 7.380 + e->u.s.info = reg; 7.381 + e->k = VNONRELOC; 7.382 +} 7.383 + 7.384 + 7.385 +static void discharge2anyreg (FuncState *fs, expdesc *e) { 7.386 + if (e->k != VNONRELOC) { 7.387 + luaK_reserveregs(fs, 1); 7.388 + discharge2reg(fs, e, fs->freereg-1); 7.389 + } 7.390 +} 7.391 + 7.392 + 7.393 +static void exp2reg (FuncState *fs, expdesc *e, int reg) { 7.394 + discharge2reg(fs, e, reg); 7.395 + if (e->k == VJMP) 7.396 + luaK_concat(fs, &e->t, e->u.s.info); /* put this jump in `t' list */ 7.397 + if (hasjumps(e)) { 7.398 + int final; /* position after whole expression */ 7.399 + int p_f = NO_JUMP; /* position of an eventual LOAD false */ 7.400 + int p_t = NO_JUMP; /* position of an eventual LOAD true */ 7.401 + if (need_value(fs, e->t) || need_value(fs, e->f)) { 7.402 + int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); 7.403 + p_f = code_label(fs, reg, 0, 1); 7.404 + p_t = code_label(fs, reg, 1, 0); 7.405 + luaK_patchtohere(fs, fj); 7.406 + } 7.407 + final = luaK_getlabel(fs); 7.408 + patchlistaux(fs, e->f, final, reg, p_f); 7.409 + patchlistaux(fs, e->t, final, reg, p_t); 7.410 + } 7.411 + e->f = e->t = NO_JUMP; 7.412 + e->u.s.info = reg; 7.413 + e->k = VNONRELOC; 7.414 +} 7.415 + 7.416 + 7.417 +void luaK_exp2nextreg (FuncState *fs, expdesc *e) { 7.418 + luaK_dischargevars(fs, e); 7.419 + freeexp(fs, e); 7.420 + luaK_reserveregs(fs, 1); 7.421 + exp2reg(fs, e, fs->freereg - 1); 7.422 +} 7.423 + 7.424 + 7.425 +int luaK_exp2anyreg (FuncState *fs, expdesc *e) { 7.426 + luaK_dischargevars(fs, e); 7.427 + if (e->k == VNONRELOC) { 7.428 + if (!hasjumps(e)) return e->u.s.info; /* exp is already in a register */ 7.429 + if (e->u.s.info >= fs->nactvar) { /* reg. is not a local? */ 7.430 + exp2reg(fs, e, e->u.s.info); /* put value on it */ 7.431 + return e->u.s.info; 7.432 + } 7.433 + } 7.434 + luaK_exp2nextreg(fs, e); /* default */ 7.435 + return e->u.s.info; 7.436 +} 7.437 + 7.438 + 7.439 +void luaK_exp2val (FuncState *fs, expdesc *e) { 7.440 + if (hasjumps(e)) 7.441 + luaK_exp2anyreg(fs, e); 7.442 + else 7.443 + luaK_dischargevars(fs, e); 7.444 +} 7.445 + 7.446 + 7.447 +int luaK_exp2RK (FuncState *fs, expdesc *e) { 7.448 + luaK_exp2val(fs, e); 7.449 + switch (e->k) { 7.450 + case VKNUM: 7.451 + case VTRUE: 7.452 + case VFALSE: 7.453 + case VNIL: { 7.454 + if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */ 7.455 + e->u.s.info = (e->k == VNIL) ? nilK(fs) : 7.456 + (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) : 7.457 + boolK(fs, (e->k == VTRUE)); 7.458 + e->k = VK; 7.459 + return RKASK(e->u.s.info); 7.460 + } 7.461 + else break; 7.462 + } 7.463 + case VK: { 7.464 + if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */ 7.465 + return RKASK(e->u.s.info); 7.466 + else break; 7.467 + } 7.468 + default: break; 7.469 + } 7.470 + /* not a constant in the right range: put it in a register */ 7.471 + return luaK_exp2anyreg(fs, e); 7.472 +} 7.473 + 7.474 + 7.475 +void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { 7.476 + switch (var->k) { 7.477 + case VLOCAL: { 7.478 + freeexp(fs, ex); 7.479 + exp2reg(fs, ex, var->u.s.info); 7.480 + return; 7.481 + } 7.482 + case VUPVAL: { 7.483 + int e = luaK_exp2anyreg(fs, ex); 7.484 + luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0); 7.485 + break; 7.486 + } 7.487 + case VGLOBAL: { 7.488 + int e = luaK_exp2anyreg(fs, ex); 7.489 + luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info); 7.490 + break; 7.491 + } 7.492 + case VINDEXED: { 7.493 + int e = luaK_exp2RK(fs, ex); 7.494 + luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e); 7.495 + break; 7.496 + } 7.497 + default: { 7.498 + lua_assert(0); /* invalid var kind to store */ 7.499 + break; 7.500 + } 7.501 + } 7.502 + freeexp(fs, ex); 7.503 +} 7.504 + 7.505 + 7.506 +void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { 7.507 + int func; 7.508 + luaK_exp2anyreg(fs, e); 7.509 + freeexp(fs, e); 7.510 + func = fs->freereg; 7.511 + luaK_reserveregs(fs, 2); 7.512 + luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key)); 7.513 + freeexp(fs, key); 7.514 + e->u.s.info = func; 7.515 + e->k = VNONRELOC; 7.516 +} 7.517 + 7.518 + 7.519 +static void invertjump (FuncState *fs, expdesc *e) { 7.520 + Instruction *pc = getjumpcontrol(fs, e->u.s.info); 7.521 + lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && 7.522 + GET_OPCODE(*pc) != OP_TEST); 7.523 + SETARG_A(*pc, !(GETARG_A(*pc))); 7.524 +} 7.525 + 7.526 + 7.527 +static int jumponcond (FuncState *fs, expdesc *e, int cond) { 7.528 + if (e->k == VRELOCABLE) { 7.529 + Instruction ie = getcode(fs, e); 7.530 + if (GET_OPCODE(ie) == OP_NOT) { 7.531 + fs->pc--; /* remove previous OP_NOT */ 7.532 + return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); 7.533 + } 7.534 + /* else go through */ 7.535 + } 7.536 + discharge2anyreg(fs, e); 7.537 + freeexp(fs, e); 7.538 + return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond); 7.539 +} 7.540 + 7.541 + 7.542 +void luaK_goiftrue (FuncState *fs, expdesc *e) { 7.543 + int pc; /* pc of last jump */ 7.544 + luaK_dischargevars(fs, e); 7.545 + switch (e->k) { 7.546 + case VK: case VKNUM: case VTRUE: { 7.547 + pc = NO_JUMP; /* always true; do nothing */ 7.548 + break; 7.549 + } 7.550 + case VFALSE: { 7.551 + pc = luaK_jump(fs); /* always jump */ 7.552 + break; 7.553 + } 7.554 + case VJMP: { 7.555 + invertjump(fs, e); 7.556 + pc = e->u.s.info; 7.557 + break; 7.558 + } 7.559 + default: { 7.560 + pc = jumponcond(fs, e, 0); 7.561 + break; 7.562 + } 7.563 + } 7.564 + luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */ 7.565 + luaK_patchtohere(fs, e->t); 7.566 + e->t = NO_JUMP; 7.567 +} 7.568 + 7.569 + 7.570 +static void luaK_goiffalse (FuncState *fs, expdesc *e) { 7.571 + int pc; /* pc of last jump */ 7.572 + luaK_dischargevars(fs, e); 7.573 + switch (e->k) { 7.574 + case VNIL: case VFALSE: { 7.575 + pc = NO_JUMP; /* always false; do nothing */ 7.576 + break; 7.577 + } 7.578 + case VTRUE: { 7.579 + pc = luaK_jump(fs); /* always jump */ 7.580 + break; 7.581 + } 7.582 + case VJMP: { 7.583 + pc = e->u.s.info; 7.584 + break; 7.585 + } 7.586 + default: { 7.587 + pc = jumponcond(fs, e, 1); 7.588 + break; 7.589 + } 7.590 + } 7.591 + luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */ 7.592 + luaK_patchtohere(fs, e->f); 7.593 + e->f = NO_JUMP; 7.594 +} 7.595 + 7.596 + 7.597 +static void codenot (FuncState *fs, expdesc *e) { 7.598 + luaK_dischargevars(fs, e); 7.599 + switch (e->k) { 7.600 + case VNIL: case VFALSE: { 7.601 + e->k = VTRUE; 7.602 + break; 7.603 + } 7.604 + case VK: case VKNUM: case VTRUE: { 7.605 + e->k = VFALSE; 7.606 + break; 7.607 + } 7.608 + case VJMP: { 7.609 + invertjump(fs, e); 7.610 + break; 7.611 + } 7.612 + case VRELOCABLE: 7.613 + case VNONRELOC: { 7.614 + discharge2anyreg(fs, e); 7.615 + freeexp(fs, e); 7.616 + e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0); 7.617 + e->k = VRELOCABLE; 7.618 + break; 7.619 + } 7.620 + default: { 7.621 + lua_assert(0); /* cannot happen */ 7.622 + break; 7.623 + } 7.624 + } 7.625 + /* interchange true and false lists */ 7.626 + { int temp = e->f; e->f = e->t; e->t = temp; } 7.627 + removevalues(fs, e->f); 7.628 + removevalues(fs, e->t); 7.629 +} 7.630 + 7.631 + 7.632 +void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { 7.633 + t->u.s.aux = luaK_exp2RK(fs, k); 7.634 + t->k = VINDEXED; 7.635 +} 7.636 + 7.637 + 7.638 +static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { 7.639 + lua_Number v1, v2, r; 7.640 + if (!isnumeral(e1) || !isnumeral(e2)) return 0; 7.641 + v1 = e1->u.nval; 7.642 + v2 = e2->u.nval; 7.643 + switch (op) { 7.644 + case OP_ADD: r = luai_numadd(v1, v2); break; 7.645 + case OP_SUB: r = luai_numsub(v1, v2); break; 7.646 + case OP_MUL: r = luai_nummul(v1, v2); break; 7.647 + case OP_DIV: 7.648 + if (v2 == 0) return 0; /* do not attempt to divide by 0 */ 7.649 + r = luai_numdiv(v1, v2); break; 7.650 + case OP_MOD: 7.651 + if (v2 == 0) return 0; /* do not attempt to divide by 0 */ 7.652 + r = luai_nummod(v1, v2); break; 7.653 + case OP_POW: r = luai_numpow(v1, v2); break; 7.654 + case OP_UNM: r = luai_numunm(v1); break; 7.655 + case OP_LEN: return 0; /* no constant folding for 'len' */ 7.656 + default: lua_assert(0); r = 0; break; 7.657 + } 7.658 + if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */ 7.659 + e1->u.nval = r; 7.660 + return 1; 7.661 +} 7.662 + 7.663 + 7.664 +static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { 7.665 + if (constfolding(op, e1, e2)) 7.666 + return; 7.667 + else { 7.668 + int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; 7.669 + int o1 = luaK_exp2RK(fs, e1); 7.670 + if (o1 > o2) { 7.671 + freeexp(fs, e1); 7.672 + freeexp(fs, e2); 7.673 + } 7.674 + else { 7.675 + freeexp(fs, e2); 7.676 + freeexp(fs, e1); 7.677 + } 7.678 + e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2); 7.679 + e1->k = VRELOCABLE; 7.680 + } 7.681 +} 7.682 + 7.683 + 7.684 +static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, 7.685 + expdesc *e2) { 7.686 + int o1 = luaK_exp2RK(fs, e1); 7.687 + int o2 = luaK_exp2RK(fs, e2); 7.688 + freeexp(fs, e2); 7.689 + freeexp(fs, e1); 7.690 + if (cond == 0 && op != OP_EQ) { 7.691 + int temp; /* exchange args to replace by `<' or `<=' */ 7.692 + temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */ 7.693 + cond = 1; 7.694 + } 7.695 + e1->u.s.info = condjump(fs, op, cond, o1, o2); 7.696 + e1->k = VJMP; 7.697 +} 7.698 + 7.699 + 7.700 +void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { 7.701 + expdesc e2; 7.702 + e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; 7.703 + switch (op) { 7.704 + case OPR_MINUS: { 7.705 + if (!isnumeral(e)) 7.706 + luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */ 7.707 + codearith(fs, OP_UNM, e, &e2); 7.708 + break; 7.709 + } 7.710 + case OPR_NOT: codenot(fs, e); break; 7.711 + case OPR_LEN: { 7.712 + luaK_exp2anyreg(fs, e); /* cannot operate on constants */ 7.713 + codearith(fs, OP_LEN, e, &e2); 7.714 + break; 7.715 + } 7.716 + default: lua_assert(0); 7.717 + } 7.718 +} 7.719 + 7.720 + 7.721 +void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { 7.722 + switch (op) { 7.723 + case OPR_AND: { 7.724 + luaK_goiftrue(fs, v); 7.725 + break; 7.726 + } 7.727 + case OPR_OR: { 7.728 + luaK_goiffalse(fs, v); 7.729 + break; 7.730 + } 7.731 + case OPR_CONCAT: { 7.732 + luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */ 7.733 + break; 7.734 + } 7.735 + case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: 7.736 + case OPR_MOD: case OPR_POW: { 7.737 + if (!isnumeral(v)) luaK_exp2RK(fs, v); 7.738 + break; 7.739 + } 7.740 + default: { 7.741 + luaK_exp2RK(fs, v); 7.742 + break; 7.743 + } 7.744 + } 7.745 +} 7.746 + 7.747 + 7.748 +void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) { 7.749 + switch (op) { 7.750 + case OPR_AND: { 7.751 + lua_assert(e1->t == NO_JUMP); /* list must be closed */ 7.752 + luaK_dischargevars(fs, e2); 7.753 + luaK_concat(fs, &e2->f, e1->f); 7.754 + *e1 = *e2; 7.755 + break; 7.756 + } 7.757 + case OPR_OR: { 7.758 + lua_assert(e1->f == NO_JUMP); /* list must be closed */ 7.759 + luaK_dischargevars(fs, e2); 7.760 + luaK_concat(fs, &e2->t, e1->t); 7.761 + *e1 = *e2; 7.762 + break; 7.763 + } 7.764 + case OPR_CONCAT: { 7.765 + luaK_exp2val(fs, e2); 7.766 + if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) { 7.767 + lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1); 7.768 + freeexp(fs, e1); 7.769 + SETARG_B(getcode(fs, e2), e1->u.s.info); 7.770 + e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info; 7.771 + } 7.772 + else { 7.773 + luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ 7.774 + codearith(fs, OP_CONCAT, e1, e2); 7.775 + } 7.776 + break; 7.777 + } 7.778 + case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break; 7.779 + case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break; 7.780 + case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break; 7.781 + case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break; 7.782 + case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break; 7.783 + case OPR_POW: codearith(fs, OP_POW, e1, e2); break; 7.784 + case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break; 7.785 + case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break; 7.786 + case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break; 7.787 + case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break; 7.788 + case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break; 7.789 + case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break; 7.790 + default: lua_assert(0); 7.791 + } 7.792 +} 7.793 + 7.794 + 7.795 +void luaK_fixline (FuncState *fs, int line) { 7.796 + fs->f->lineinfo[fs->pc - 1] = line; 7.797 +} 7.798 + 7.799 + 7.800 +static int luaK_code (FuncState *fs, Instruction i, int line) { 7.801 + Proto *f = fs->f; 7.802 + dischargejpc(fs); /* `pc' will change */ 7.803 + /* put new instruction in code array */ 7.804 + luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction, 7.805 + MAX_INT, "code size overflow"); 7.806 + f->code[fs->pc] = i; 7.807 + /* save corresponding line information */ 7.808 + luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int, 7.809 + MAX_INT, "code size overflow"); 7.810 + f->lineinfo[fs->pc] = line; 7.811 + return fs->pc++; 7.812 +} 7.813 + 7.814 + 7.815 +int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { 7.816 + lua_assert(getOpMode(o) == iABC); 7.817 + lua_assert(getBMode(o) != OpArgN || b == 0); 7.818 + lua_assert(getCMode(o) != OpArgN || c == 0); 7.819 + return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline); 7.820 +} 7.821 + 7.822 + 7.823 +int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { 7.824 + lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); 7.825 + lua_assert(getCMode(o) == OpArgN); 7.826 + return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline); 7.827 +} 7.828 + 7.829 + 7.830 +void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { 7.831 + int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; 7.832 + int b = (tostore == LUA_MULTRET) ? 0 : tostore; 7.833 + lua_assert(tostore != 0); 7.834 + if (c <= MAXARG_C) 7.835 + luaK_codeABC(fs, OP_SETLIST, base, b, c); 7.836 + else { 7.837 + luaK_codeABC(fs, OP_SETLIST, base, b, 0); 7.838 + luaK_code(fs, cast(Instruction, c), fs->ls->lastline); 7.839 + } 7.840 + fs->freereg = base + 1; /* free registers with list values */ 7.841 +} 7.842 +
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/src/lua/lcode.h Sat Mar 03 11:07:39 2012 -0600 8.3 @@ -0,0 +1,76 @@ 8.4 +/* 8.5 +** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $ 8.6 +** Code generator for Lua 8.7 +** See Copyright Notice in lua.h 8.8 +*/ 8.9 + 8.10 +#ifndef lcode_h 8.11 +#define lcode_h 8.12 + 8.13 +#include "llex.h" 8.14 +#include "lobject.h" 8.15 +#include "lopcodes.h" 8.16 +#include "lparser.h" 8.17 + 8.18 + 8.19 +/* 8.20 +** Marks the end of a patch list. It is an invalid value both as an absolute 8.21 +** address, and as a list link (would link an element to itself). 8.22 +*/ 8.23 +#define NO_JUMP (-1) 8.24 + 8.25 + 8.26 +/* 8.27 +** grep "ORDER OPR" if you change these enums 8.28 +*/ 8.29 +typedef enum BinOpr { 8.30 + OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 8.31 + OPR_CONCAT, 8.32 + OPR_NE, OPR_EQ, 8.33 + OPR_LT, OPR_LE, OPR_GT, OPR_GE, 8.34 + OPR_AND, OPR_OR, 8.35 + OPR_NOBINOPR 8.36 +} BinOpr; 8.37 + 8.38 + 8.39 +typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 8.40 + 8.41 + 8.42 +#define getcode(fs,e) ((fs)->f->code[(e)->u.s.info]) 8.43 + 8.44 +#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 8.45 + 8.46 +#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 8.47 + 8.48 +LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 8.49 +LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 8.50 +LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 8.51 +LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 8.52 +LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 8.53 +LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 8.54 +LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 8.55 +LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 8.56 +LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 8.57 +LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 8.58 +LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 8.59 +LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 8.60 +LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 8.61 +LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 8.62 +LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 8.63 +LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 8.64 +LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 8.65 +LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 8.66 +LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 8.67 +LUAI_FUNC int luaK_jump (FuncState *fs); 8.68 +LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 8.69 +LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 8.70 +LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 8.71 +LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 8.72 +LUAI_FUNC int luaK_getlabel (FuncState *fs); 8.73 +LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v); 8.74 +LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 8.75 +LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); 8.76 +LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 8.77 + 8.78 + 8.79 +#endif
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/src/lua/ldblib.c Sat Mar 03 11:07:39 2012 -0600 9.3 @@ -0,0 +1,397 @@ 9.4 +/* 9.5 +** $Id: ldblib.c,v 1.104.1.3 2008/01/21 13:11:21 roberto Exp $ 9.6 +** Interface from Lua to its debug API 9.7 +** See Copyright Notice in lua.h 9.8 +*/ 9.9 + 9.10 + 9.11 +#include <stdio.h> 9.12 +#include <stdlib.h> 9.13 +#include <string.h> 9.14 + 9.15 +#define ldblib_c 9.16 +#define LUA_LIB 9.17 + 9.18 +#include "lua.h" 9.19 + 9.20 +#include "lauxlib.h" 9.21 +#include "lualib.h" 9.22 + 9.23 + 9.24 + 9.25 +static int db_getregistry (lua_State *L) { 9.26 + lua_pushvalue(L, LUA_REGISTRYINDEX); 9.27 + return 1; 9.28 +} 9.29 + 9.30 + 9.31 +static int db_getmetatable (lua_State *L) { 9.32 + luaL_checkany(L, 1); 9.33 + if (!lua_getmetatable(L, 1)) { 9.34 + lua_pushnil(L); /* no metatable */ 9.35 + } 9.36 + return 1; 9.37 +} 9.38 + 9.39 + 9.40 +static int db_setmetatable (lua_State *L) { 9.41 + int t = lua_type(L, 2); 9.42 + luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 9.43 + "nil or table expected"); 9.44 + lua_settop(L, 2); 9.45 + lua_pushboolean(L, lua_setmetatable(L, 1)); 9.46 + return 1; 9.47 +} 9.48 + 9.49 + 9.50 +static int db_getfenv (lua_State *L) { 9.51 + lua_getfenv(L, 1); 9.52 + return 1; 9.53 +} 9.54 + 9.55 + 9.56 +static int db_setfenv (lua_State *L) { 9.57 + luaL_checktype(L, 2, LUA_TTABLE); 9.58 + lua_settop(L, 2); 9.59 + if (lua_setfenv(L, 1) == 0) 9.60 + luaL_error(L, LUA_QL("setfenv") 9.61 + " cannot change environment of given object"); 9.62 + return 1; 9.63 +} 9.64 + 9.65 + 9.66 +static void settabss (lua_State *L, const char *i, const char *v) { 9.67 + lua_pushstring(L, v); 9.68 + lua_setfield(L, -2, i); 9.69 +} 9.70 + 9.71 + 9.72 +static void settabsi (lua_State *L, const char *i, int v) { 9.73 + lua_pushinteger(L, v); 9.74 + lua_setfield(L, -2, i); 9.75 +} 9.76 + 9.77 + 9.78 +static lua_State *getthread (lua_State *L, int *arg) { 9.79 + if (lua_isthread(L, 1)) { 9.80 + *arg = 1; 9.81 + return lua_tothread(L, 1); 9.82 + } 9.83 + else { 9.84 + *arg = 0; 9.85 + return L; 9.86 + } 9.87 +} 9.88 + 9.89 + 9.90 +static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { 9.91 + if (L == L1) { 9.92 + lua_pushvalue(L, -2); 9.93 + lua_remove(L, -3); 9.94 + } 9.95 + else 9.96 + lua_xmove(L1, L, 1); 9.97 + lua_setfield(L, -2, fname); 9.98 +} 9.99 + 9.100 + 9.101 +static int db_getinfo (lua_State *L) { 9.102 + lua_Debug ar; 9.103 + int arg; 9.104 + lua_State *L1 = getthread(L, &arg); 9.105 + const char *options = luaL_optstring(L, arg+2, "flnSu"); 9.106 + if (lua_isnumber(L, arg+1)) { 9.107 + if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { 9.108 + lua_pushnil(L); /* level out of range */ 9.109 + return 1; 9.110 + } 9.111 + } 9.112 + else if (lua_isfunction(L, arg+1)) { 9.113 + lua_pushfstring(L, ">%s", options); 9.114 + options = lua_tostring(L, -1); 9.115 + lua_pushvalue(L, arg+1); 9.116 + lua_xmove(L, L1, 1); 9.117 + } 9.118 + else 9.119 + return luaL_argerror(L, arg+1, "function or level expected"); 9.120 + if (!lua_getinfo(L1, options, &ar)) 9.121 + return luaL_argerror(L, arg+2, "invalid option"); 9.122 + lua_createtable(L, 0, 2); 9.123 + if (strchr(options, 'S')) { 9.124 + settabss(L, "source", ar.source); 9.125 + settabss(L, "short_src", ar.short_src); 9.126 + settabsi(L, "linedefined", ar.linedefined); 9.127 + settabsi(L, "lastlinedefined", ar.lastlinedefined); 9.128 + settabss(L, "what", ar.what); 9.129 + } 9.130 + if (strchr(options, 'l')) 9.131 + settabsi(L, "currentline", ar.currentline); 9.132 + if (strchr(options, 'u')) 9.133 + settabsi(L, "nups", ar.nups); 9.134 + if (strchr(options, 'n')) { 9.135 + settabss(L, "name", ar.name); 9.136 + settabss(L, "namewhat", ar.namewhat); 9.137 + } 9.138 + if (strchr(options, 'L')) 9.139 + treatstackoption(L, L1, "activelines"); 9.140 + if (strchr(options, 'f')) 9.141 + treatstackoption(L, L1, "func"); 9.142 + return 1; /* return table */ 9.143 +} 9.144 + 9.145 + 9.146 +static int db_getlocal (lua_State *L) { 9.147 + int arg; 9.148 + lua_State *L1 = getthread(L, &arg); 9.149 + lua_Debug ar; 9.150 + const char *name; 9.151 + if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ 9.152 + return luaL_argerror(L, arg+1, "level out of range"); 9.153 + name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2)); 9.154 + if (name) { 9.155 + lua_xmove(L1, L, 1); 9.156 + lua_pushstring(L, name); 9.157 + lua_pushvalue(L, -2); 9.158 + return 2; 9.159 + } 9.160 + else { 9.161 + lua_pushnil(L); 9.162 + return 1; 9.163 + } 9.164 +} 9.165 + 9.166 + 9.167 +static int db_setlocal (lua_State *L) { 9.168 + int arg; 9.169 + lua_State *L1 = getthread(L, &arg); 9.170 + lua_Debug ar; 9.171 + if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ 9.172 + return luaL_argerror(L, arg+1, "level out of range"); 9.173 + luaL_checkany(L, arg+3); 9.174 + lua_settop(L, arg+3); 9.175 + lua_xmove(L, L1, 1); 9.176 + lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2))); 9.177 + return 1; 9.178 +} 9.179 + 9.180 + 9.181 +static int auxupvalue (lua_State *L, int get) { 9.182 + const char *name; 9.183 + int n = luaL_checkint(L, 2); 9.184 + luaL_checktype(L, 1, LUA_TFUNCTION); 9.185 + if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */ 9.186 + name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); 9.187 + if (name == NULL) return 0; 9.188 + lua_pushstring(L, name); 9.189 + lua_insert(L, -(get+1)); 9.190 + return get + 1; 9.191 +} 9.192 + 9.193 + 9.194 +static int db_getupvalue (lua_State *L) { 9.195 + return auxupvalue(L, 1); 9.196 +} 9.197 + 9.198 + 9.199 +static int db_setupvalue (lua_State *L) { 9.200 + luaL_checkany(L, 3); 9.201 + return auxupvalue(L, 0); 9.202 +} 9.203 + 9.204 + 9.205 + 9.206 +static const char KEY_HOOK = 'h'; 9.207 + 9.208 + 9.209 +static void hookf (lua_State *L, lua_Debug *ar) { 9.210 + static const char *const hooknames[] = 9.211 + {"call", "return", "line", "count", "tail return"}; 9.212 + lua_pushlightuserdata(L, (void *)&KEY_HOOK); 9.213 + lua_rawget(L, LUA_REGISTRYINDEX); 9.214 + lua_pushlightuserdata(L, L); 9.215 + lua_rawget(L, -2); 9.216 + if (lua_isfunction(L, -1)) { 9.217 + lua_pushstring(L, hooknames[(int)ar->event]); 9.218 + if (ar->currentline >= 0) 9.219 + lua_pushinteger(L, ar->currentline); 9.220 + else lua_pushnil(L); 9.221 + lua_assert(lua_getinfo(L, "lS", ar)); 9.222 + lua_call(L, 2, 0); 9.223 + } 9.224 +} 9.225 + 9.226 + 9.227 +static int makemask (const char *smask, int count) { 9.228 + int mask = 0; 9.229 + if (strchr(smask, 'c')) mask |= LUA_MASKCALL; 9.230 + if (strchr(smask, 'r')) mask |= LUA_MASKRET; 9.231 + if (strchr(smask, 'l')) mask |= LUA_MASKLINE; 9.232 + if (count > 0) mask |= LUA_MASKCOUNT; 9.233 + return mask; 9.234 +} 9.235 + 9.236 + 9.237 +static char *unmakemask (int mask, char *smask) { 9.238 + int i = 0; 9.239 + if (mask & LUA_MASKCALL) smask[i++] = 'c'; 9.240 + if (mask & LUA_MASKRET) smask[i++] = 'r'; 9.241 + if (mask & LUA_MASKLINE) smask[i++] = 'l'; 9.242 + smask[i] = '\0'; 9.243 + return smask; 9.244 +} 9.245 + 9.246 + 9.247 +static void gethooktable (lua_State *L) { 9.248 + lua_pushlightuserdata(L, (void *)&KEY_HOOK); 9.249 + lua_rawget(L, LUA_REGISTRYINDEX); 9.250 + if (!lua_istable(L, -1)) { 9.251 + lua_pop(L, 1); 9.252 + lua_createtable(L, 0, 1); 9.253 + lua_pushlightuserdata(L, (void *)&KEY_HOOK); 9.254 + lua_pushvalue(L, -2); 9.255 + lua_rawset(L, LUA_REGISTRYINDEX); 9.256 + } 9.257 +} 9.258 + 9.259 + 9.260 +static int db_sethook (lua_State *L) { 9.261 + int arg, mask, count; 9.262 + lua_Hook func; 9.263 + lua_State *L1 = getthread(L, &arg); 9.264 + if (lua_isnoneornil(L, arg+1)) { 9.265 + lua_settop(L, arg+1); 9.266 + func = NULL; mask = 0; count = 0; /* turn off hooks */ 9.267 + } 9.268 + else { 9.269 + const char *smask = luaL_checkstring(L, arg+2); 9.270 + luaL_checktype(L, arg+1, LUA_TFUNCTION); 9.271 + count = luaL_optint(L, arg+3, 0); 9.272 + func = hookf; mask = makemask(smask, count); 9.273 + } 9.274 + gethooktable(L); 9.275 + lua_pushlightuserdata(L, L1); 9.276 + lua_pushvalue(L, arg+1); 9.277 + lua_rawset(L, -3); /* set new hook */ 9.278 + lua_pop(L, 1); /* remove hook table */ 9.279 + lua_sethook(L1, func, mask, count); /* set hooks */ 9.280 + return 0; 9.281 +} 9.282 + 9.283 + 9.284 +static int db_gethook (lua_State *L) { 9.285 + int arg; 9.286 + lua_State *L1 = getthread(L, &arg); 9.287 + char buff[5]; 9.288 + int mask = lua_gethookmask(L1); 9.289 + lua_Hook hook = lua_gethook(L1); 9.290 + if (hook != NULL && hook != hookf) /* external hook? */ 9.291 + lua_pushliteral(L, "external hook"); 9.292 + else { 9.293 + gethooktable(L); 9.294 + lua_pushlightuserdata(L, L1); 9.295 + lua_rawget(L, -2); /* get hook */ 9.296 + lua_remove(L, -2); /* remove hook table */ 9.297 + } 9.298 + lua_pushstring(L, unmakemask(mask, buff)); 9.299 + lua_pushinteger(L, lua_gethookcount(L1)); 9.300 + return 3; 9.301 +} 9.302 + 9.303 + 9.304 +static int db_debug (lua_State *L) { 9.305 + for (;;) { 9.306 + char buffer[250]; 9.307 + fputs("lua_debug> ", stderr); 9.308 + if (fgets(buffer, sizeof(buffer), stdin) == 0 || 9.309 + strcmp(buffer, "cont\n") == 0) 9.310 + return 0; 9.311 + if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || 9.312 + lua_pcall(L, 0, 0, 0)) { 9.313 + fputs(lua_tostring(L, -1), stderr); 9.314 + fputs("\n", stderr); 9.315 + } 9.316 + lua_settop(L, 0); /* remove eventual returns */ 9.317 + } 9.318 +} 9.319 + 9.320 + 9.321 +#define LEVELS1 12 /* size of the first part of the stack */ 9.322 +#define LEVELS2 10 /* size of the second part of the stack */ 9.323 + 9.324 +static int db_errorfb (lua_State *L) { 9.325 + int level; 9.326 + int firstpart = 1; /* still before eventual `...' */ 9.327 + int arg; 9.328 + lua_State *L1 = getthread(L, &arg); 9.329 + lua_Debug ar; 9.330 + if (lua_isnumber(L, arg+2)) { 9.331 + level = (int)lua_tointeger(L, arg+2); 9.332 + lua_pop(L, 1); 9.333 + } 9.334 + else 9.335 + level = (L == L1) ? 1 : 0; /* level 0 may be this own function */ 9.336 + if (lua_gettop(L) == arg) 9.337 + lua_pushliteral(L, ""); 9.338 + else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */ 9.339 + else lua_pushliteral(L, "\n"); 9.340 + lua_pushliteral(L, "stack traceback:"); 9.341 + while (lua_getstack(L1, level++, &ar)) { 9.342 + if (level > LEVELS1 && firstpart) { 9.343 + /* no more than `LEVELS2' more levels? */ 9.344 + if (!lua_getstack(L1, level+LEVELS2, &ar)) 9.345 + level--; /* keep going */ 9.346 + else { 9.347 + lua_pushliteral(L, "\n\t..."); /* too many levels */ 9.348 + while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */ 9.349 + level++; 9.350 + } 9.351 + firstpart = 0; 9.352 + continue; 9.353 + } 9.354 + lua_pushliteral(L, "\n\t"); 9.355 + lua_getinfo(L1, "Snl", &ar); 9.356 + lua_pushfstring(L, "%s:", ar.short_src); 9.357 + if (ar.currentline > 0) 9.358 + lua_pushfstring(L, "%d:", ar.currentline); 9.359 + if (*ar.namewhat != '\0') /* is there a name? */ 9.360 + lua_pushfstring(L, " in function " LUA_QS, ar.name); 9.361 + else { 9.362 + if (*ar.what == 'm') /* main? */ 9.363 + lua_pushfstring(L, " in main chunk"); 9.364 + else if (*ar.what == 'C' || *ar.what == 't') 9.365 + lua_pushliteral(L, " ?"); /* C function or tail call */ 9.366 + else 9.367 + lua_pushfstring(L, " in function <%s:%d>", 9.368 + ar.short_src, ar.linedefined); 9.369 + } 9.370 + lua_concat(L, lua_gettop(L) - arg); 9.371 + } 9.372 + lua_concat(L, lua_gettop(L) - arg); 9.373 + return 1; 9.374 +} 9.375 + 9.376 + 9.377 +static const luaL_Reg dblib[] = { 9.378 + {"debug", db_debug}, 9.379 + {"getfenv", db_getfenv}, 9.380 + {"gethook", db_gethook}, 9.381 + {"getinfo", db_getinfo}, 9.382 + {"getlocal", db_getlocal}, 9.383 + {"getregistry", db_getregistry}, 9.384 + {"getmetatable", db_getmetatable}, 9.385 + {"getupvalue", db_getupvalue}, 9.386 + {"setfenv", db_setfenv}, 9.387 + {"sethook", db_sethook}, 9.388 + {"setlocal", db_setlocal}, 9.389 + {"setmetatable", db_setmetatable}, 9.390 + {"setupvalue", db_setupvalue}, 9.391 + {"traceback", db_errorfb}, 9.392 + {NULL, NULL} 9.393 +}; 9.394 + 9.395 + 9.396 +LUALIB_API int luaopen_debug (lua_State *L) { 9.397 + luaL_register(L, LUA_DBLIBNAME, dblib); 9.398 + return 1; 9.399 +} 9.400 +
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/src/lua/ldebug.c Sat Mar 03 11:07:39 2012 -0600 10.3 @@ -0,0 +1,638 @@ 10.4 +/* 10.5 +** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $ 10.6 +** Debug Interface 10.7 +** See Copyright Notice in lua.h 10.8 +*/ 10.9 + 10.10 + 10.11 +#include <stdarg.h> 10.12 +#include <stddef.h> 10.13 +#include <string.h> 10.14 + 10.15 + 10.16 +#define ldebug_c 10.17 +#define LUA_CORE 10.18 + 10.19 +#include "lua.h" 10.20 + 10.21 +#include "lapi.h" 10.22 +#include "lcode.h" 10.23 +#include "ldebug.h" 10.24 +#include "ldo.h" 10.25 +#include "lfunc.h" 10.26 +#include "lobject.h" 10.27 +#include "lopcodes.h" 10.28 +#include "lstate.h" 10.29 +#include "lstring.h" 10.30 +#include "ltable.h" 10.31 +#include "ltm.h" 10.32 +#include "lvm.h" 10.33 + 10.34 + 10.35 + 10.36 +static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); 10.37 + 10.38 + 10.39 +static int currentpc (lua_State *L, CallInfo *ci) { 10.40 + if (!isLua(ci)) return -1; /* function is not a Lua function? */ 10.41 + if (ci == L->ci) 10.42 + ci->savedpc = L->savedpc; 10.43 + return pcRel(ci->savedpc, ci_func(ci)->l.p); 10.44 +} 10.45 + 10.46 + 10.47 +static int currentline (lua_State *L, CallInfo *ci) { 10.48 + int pc = currentpc(L, ci); 10.49 + if (pc < 0) 10.50 + return -1; /* only active lua functions have current-line information */ 10.51 + else 10.52 + return getline(ci_func(ci)->l.p, pc); 10.53 +} 10.54 + 10.55 + 10.56 +/* 10.57 +** this function can be called asynchronous (e.g. during a signal) 10.58 +*/ 10.59 +LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { 10.60 + if (func == NULL || mask == 0) { /* turn off hooks? */ 10.61 + mask = 0; 10.62 + func = NULL; 10.63 + } 10.64 + L->hook = func; 10.65 + L->basehookcount = count; 10.66 + resethookcount(L); 10.67 + L->hookmask = cast_byte(mask); 10.68 + return 1; 10.69 +} 10.70 + 10.71 + 10.72 +LUA_API lua_Hook lua_gethook (lua_State *L) { 10.73 + return L->hook; 10.74 +} 10.75 + 10.76 + 10.77 +LUA_API int lua_gethookmask (lua_State *L) { 10.78 + return L->hookmask; 10.79 +} 10.80 + 10.81 + 10.82 +LUA_API int lua_gethookcount (lua_State *L) { 10.83 + return L->basehookcount; 10.84 +} 10.85 + 10.86 + 10.87 +LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { 10.88 + int status; 10.89 + CallInfo *ci; 10.90 + lua_lock(L); 10.91 + for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) { 10.92 + level--; 10.93 + if (f_isLua(ci)) /* Lua function? */ 10.94 + level -= ci->tailcalls; /* skip lost tail calls */ 10.95 + } 10.96 + if (level == 0 && ci > L->base_ci) { /* level found? */ 10.97 + status = 1; 10.98 + ar->i_ci = cast_int(ci - L->base_ci); 10.99 + } 10.100 + else if (level < 0) { /* level is of a lost tail call? */ 10.101 + status = 1; 10.102 + ar->i_ci = 0; 10.103 + } 10.104 + else status = 0; /* no such level */ 10.105 + lua_unlock(L); 10.106 + return status; 10.107 +} 10.108 + 10.109 + 10.110 +static Proto *getluaproto (CallInfo *ci) { 10.111 + return (isLua(ci) ? ci_func(ci)->l.p : NULL); 10.112 +} 10.113 + 10.114 + 10.115 +static const char *findlocal (lua_State *L, CallInfo *ci, int n) { 10.116 + const char *name; 10.117 + Proto *fp = getluaproto(ci); 10.118 + if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL) 10.119 + return name; /* is a local variable in a Lua function */ 10.120 + else { 10.121 + StkId limit = (ci == L->ci) ? L->top : (ci+1)->func; 10.122 + if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */ 10.123 + return "(*temporary)"; 10.124 + else 10.125 + return NULL; 10.126 + } 10.127 +} 10.128 + 10.129 + 10.130 +LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { 10.131 + CallInfo *ci = L->base_ci + ar->i_ci; 10.132 + const char *name = findlocal(L, ci, n); 10.133 + lua_lock(L); 10.134 + if (name) 10.135 + luaA_pushobject(L, ci->base + (n - 1)); 10.136 + lua_unlock(L); 10.137 + return name; 10.138 +} 10.139 + 10.140 + 10.141 +LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { 10.142 + CallInfo *ci = L->base_ci + ar->i_ci; 10.143 + const char *name = findlocal(L, ci, n); 10.144 + lua_lock(L); 10.145 + if (name) 10.146 + setobjs2s(L, ci->base + (n - 1), L->top - 1); 10.147 + L->top--; /* pop value */ 10.148 + lua_unlock(L); 10.149 + return name; 10.150 +} 10.151 + 10.152 + 10.153 +static void funcinfo (lua_Debug *ar, Closure *cl) { 10.154 + if (cl->c.isC) { 10.155 + ar->source = "=[C]"; 10.156 + ar->linedefined = -1; 10.157 + ar->lastlinedefined = -1; 10.158 + ar->what = "C"; 10.159 + } 10.160 + else { 10.161 + ar->source = getstr(cl->l.p->source); 10.162 + ar->linedefined = cl->l.p->linedefined; 10.163 + ar->lastlinedefined = cl->l.p->lastlinedefined; 10.164 + ar->what = (ar->linedefined == 0) ? "main" : "Lua"; 10.165 + } 10.166 + luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); 10.167 +} 10.168 + 10.169 + 10.170 +static void info_tailcall (lua_Debug *ar) { 10.171 + ar->name = ar->namewhat = ""; 10.172 + ar->what = "tail"; 10.173 + ar->lastlinedefined = ar->linedefined = ar->currentline = -1; 10.174 + ar->source = "=(tail call)"; 10.175 + luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); 10.176 + ar->nups = 0; 10.177 +} 10.178 + 10.179 + 10.180 +static void collectvalidlines (lua_State *L, Closure *f) { 10.181 + if (f == NULL || f->c.isC) { 10.182 + setnilvalue(L->top); 10.183 + } 10.184 + else { 10.185 + Table *t = luaH_new(L, 0, 0); 10.186 + int *lineinfo = f->l.p->lineinfo; 10.187 + int i; 10.188 + for (i=0; i<f->l.p->sizelineinfo; i++) 10.189 + setbvalue(luaH_setnum(L, t, lineinfo[i]), 1); 10.190 + sethvalue(L, L->top, t); 10.191 + } 10.192 + incr_top(L); 10.193 +} 10.194 + 10.195 + 10.196 +static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, 10.197 + Closure *f, CallInfo *ci) { 10.198 + int status = 1; 10.199 + if (f == NULL) { 10.200 + info_tailcall(ar); 10.201 + return status; 10.202 + } 10.203 + for (; *what; what++) { 10.204 + switch (*what) { 10.205 + case 'S': { 10.206 + funcinfo(ar, f); 10.207 + break; 10.208 + } 10.209 + case 'l': { 10.210 + ar->currentline = (ci) ? currentline(L, ci) : -1; 10.211 + break; 10.212 + } 10.213 + case 'u': { 10.214 + ar->nups = f->c.nupvalues; 10.215 + break; 10.216 + } 10.217 + case 'n': { 10.218 + ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL; 10.219 + if (ar->namewhat == NULL) { 10.220 + ar->namewhat = ""; /* not found */ 10.221 + ar->name = NULL; 10.222 + } 10.223 + break; 10.224 + } 10.225 + case 'L': 10.226 + case 'f': /* handled by lua_getinfo */ 10.227 + break; 10.228 + default: status = 0; /* invalid option */ 10.229 + } 10.230 + } 10.231 + return status; 10.232 +} 10.233 + 10.234 + 10.235 +LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { 10.236 + int status; 10.237 + Closure *f = NULL; 10.238 + CallInfo *ci = NULL; 10.239 + lua_lock(L); 10.240 + if (*what == '>') { 10.241 + StkId func = L->top - 1; 10.242 + luai_apicheck(L, ttisfunction(func)); 10.243 + what++; /* skip the '>' */ 10.244 + f = clvalue(func); 10.245 + L->top--; /* pop function */ 10.246 + } 10.247 + else if (ar->i_ci != 0) { /* no tail call? */ 10.248 + ci = L->base_ci + ar->i_ci; 10.249 + lua_assert(ttisfunction(ci->func)); 10.250 + f = clvalue(ci->func); 10.251 + } 10.252 + status = auxgetinfo(L, what, ar, f, ci); 10.253 + if (strchr(what, 'f')) { 10.254 + if (f == NULL) setnilvalue(L->top); 10.255 + else setclvalue(L, L->top, f); 10.256 + incr_top(L); 10.257 + } 10.258 + if (strchr(what, 'L')) 10.259 + collectvalidlines(L, f); 10.260 + lua_unlock(L); 10.261 + return status; 10.262 +} 10.263 + 10.264 + 10.265 +/* 10.266 +** {====================================================== 10.267 +** Symbolic Execution and code checker 10.268 +** ======================================================= 10.269 +*/ 10.270 + 10.271 +#define check(x) if (!(x)) return 0; 10.272 + 10.273 +#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode) 10.274 + 10.275 +#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize) 10.276 + 10.277 + 10.278 + 10.279 +static int precheck (const Proto *pt) { 10.280 + check(pt->maxstacksize <= MAXSTACK); 10.281 + check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize); 10.282 + check(!(pt->is_vararg & VARARG_NEEDSARG) || 10.283 + (pt->is_vararg & VARARG_HASARG)); 10.284 + check(pt->sizeupvalues <= pt->nups); 10.285 + check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0); 10.286 + check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN); 10.287 + return 1; 10.288 +} 10.289 + 10.290 + 10.291 +#define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1]) 10.292 + 10.293 +int luaG_checkopenop (Instruction i) { 10.294 + switch (GET_OPCODE(i)) { 10.295 + case OP_CALL: 10.296 + case OP_TAILCALL: 10.297 + case OP_RETURN: 10.298 + case OP_SETLIST: { 10.299 + check(GETARG_B(i) == 0); 10.300 + return 1; 10.301 + } 10.302 + default: return 0; /* invalid instruction after an open call */ 10.303 + } 10.304 +} 10.305 + 10.306 + 10.307 +static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) { 10.308 + switch (mode) { 10.309 + case OpArgN: check(r == 0); break; 10.310 + case OpArgU: break; 10.311 + case OpArgR: checkreg(pt, r); break; 10.312 + case OpArgK: 10.313 + check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize); 10.314 + break; 10.315 + } 10.316 + return 1; 10.317 +} 10.318 + 10.319 + 10.320 +static Instruction symbexec (const Proto *pt, int lastpc, int reg) { 10.321 + int pc; 10.322 + int last; /* stores position of last instruction that changed `reg' */ 10.323 + last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */ 10.324 + check(precheck(pt)); 10.325 + for (pc = 0; pc < lastpc; pc++) { 10.326 + Instruction i = pt->code[pc]; 10.327 + OpCode op = GET_OPCODE(i); 10.328 + int a = GETARG_A(i); 10.329 + int b = 0; 10.330 + int c = 0; 10.331 + check(op < NUM_OPCODES); 10.332 + checkreg(pt, a); 10.333 + switch (getOpMode(op)) { 10.334 + case iABC: { 10.335 + b = GETARG_B(i); 10.336 + c = GETARG_C(i); 10.337 + check(checkArgMode(pt, b, getBMode(op))); 10.338 + check(checkArgMode(pt, c, getCMode(op))); 10.339 + break; 10.340 + } 10.341 + case iABx: { 10.342 + b = GETARG_Bx(i); 10.343 + if (getBMode(op) == OpArgK) check(b < pt->sizek); 10.344 + break; 10.345 + } 10.346 + case iAsBx: { 10.347 + b = GETARG_sBx(i); 10.348 + if (getBMode(op) == OpArgR) { 10.349 + int dest = pc+1+b; 10.350 + check(0 <= dest && dest < pt->sizecode); 10.351 + if (dest > 0) { 10.352 + int j; 10.353 + /* check that it does not jump to a setlist count; this 10.354 + is tricky, because the count from a previous setlist may 10.355 + have the same value of an invalid setlist; so, we must 10.356 + go all the way back to the first of them (if any) */ 10.357 + for (j = 0; j < dest; j++) { 10.358 + Instruction d = pt->code[dest-1-j]; 10.359 + if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break; 10.360 + } 10.361 + /* if 'j' is even, previous value is not a setlist (even if 10.362 + it looks like one) */ 10.363 + check((j&1) == 0); 10.364 + } 10.365 + } 10.366 + break; 10.367 + } 10.368 + } 10.369 + if (testAMode(op)) { 10.370 + if (a == reg) last = pc; /* change register `a' */ 10.371 + } 10.372 + if (testTMode(op)) { 10.373 + check(pc+2 < pt->sizecode); /* check skip */ 10.374 + check(GET_OPCODE(pt->code[pc+1]) == OP_JMP); 10.375 + } 10.376 + switch (op) { 10.377 + case OP_LOADBOOL: { 10.378 + if (c == 1) { /* does it jump? */ 10.379 + check(pc+2 < pt->sizecode); /* check its jump */ 10.380 + check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST || 10.381 + GETARG_C(pt->code[pc+1]) != 0); 10.382 + } 10.383 + break; 10.384 + } 10.385 + case OP_LOADNIL: { 10.386 + if (a <= reg && reg <= b) 10.387 + last = pc; /* set registers from `a' to `b' */ 10.388 + break; 10.389 + } 10.390 + case OP_GETUPVAL: 10.391 + case OP_SETUPVAL: { 10.392 + check(b < pt->nups); 10.393 + break; 10.394 + } 10.395 + case OP_GETGLOBAL: 10.396 + case OP_SETGLOBAL: { 10.397 + check(ttisstring(&pt->k[b])); 10.398 + break; 10.399 + } 10.400 + case OP_SELF: { 10.401 + checkreg(pt, a+1); 10.402 + if (reg == a+1) last = pc; 10.403 + break; 10.404 + } 10.405 + case OP_CONCAT: { 10.406 + check(b < c); /* at least two operands */ 10.407 + break; 10.408 + } 10.409 + case OP_TFORLOOP: { 10.410 + check(c >= 1); /* at least one result (control variable) */ 10.411 + checkreg(pt, a+2+c); /* space for results */ 10.412 + if (reg >= a+2) last = pc; /* affect all regs above its base */ 10.413 + break; 10.414 + } 10.415 + case OP_FORLOOP: 10.416 + case OP_FORPREP: 10.417 + checkreg(pt, a+3); 10.418 + /* go through */ 10.419 + case OP_JMP: { 10.420 + int dest = pc+1+b; 10.421 + /* not full check and jump is forward and do not skip `lastpc'? */ 10.422 + if (reg != NO_REG && pc < dest && dest <= lastpc) 10.423 + pc += b; /* do the jump */ 10.424 + break; 10.425 + } 10.426 + case OP_CALL: 10.427 + case OP_TAILCALL: { 10.428 + if (b != 0) { 10.429 + checkreg(pt, a+b-1); 10.430 + } 10.431 + c--; /* c = num. returns */ 10.432 + if (c == LUA_MULTRET) { 10.433 + check(checkopenop(pt, pc)); 10.434 + } 10.435 + else if (c != 0) 10.436 + checkreg(pt, a+c-1); 10.437 + if (reg >= a) last = pc; /* affect all registers above base */ 10.438 + break; 10.439 + } 10.440 + case OP_RETURN: { 10.441 + b--; /* b = num. returns */ 10.442 + if (b > 0) checkreg(pt, a+b-1); 10.443 + break; 10.444 + } 10.445 + case OP_SETLIST: { 10.446 + if (b > 0) checkreg(pt, a + b); 10.447 + if (c == 0) { 10.448 + pc++; 10.449 + check(pc < pt->sizecode - 1); 10.450 + } 10.451 + break; 10.452 + } 10.453 + case OP_CLOSURE: { 10.454 + int nup, j; 10.455 + check(b < pt->sizep); 10.456 + nup = pt->p[b]->nups; 10.457 + check(pc + nup < pt->sizecode); 10.458 + for (j = 1; j <= nup; j++) { 10.459 + OpCode op1 = GET_OPCODE(pt->code[pc + j]); 10.460 + check(op1 == OP_GETUPVAL || op1 == OP_MOVE); 10.461 + } 10.462 + if (reg != NO_REG) /* tracing? */ 10.463 + pc += nup; /* do not 'execute' these pseudo-instructions */ 10.464 + break; 10.465 + } 10.466 + case OP_VARARG: { 10.467 + check((pt->is_vararg & VARARG_ISVARARG) && 10.468 + !(pt->is_vararg & VARARG_NEEDSARG)); 10.469 + b--; 10.470 + if (b == LUA_MULTRET) check(checkopenop(pt, pc)); 10.471 + checkreg(pt, a+b-1); 10.472 + break; 10.473 + } 10.474 + default: break; 10.475 + } 10.476 + } 10.477 + return pt->code[last]; 10.478 +} 10.479 + 10.480 +#undef check 10.481 +#undef checkjump 10.482 +#undef checkreg 10.483 + 10.484 +/* }====================================================== */ 10.485 + 10.486 + 10.487 +int luaG_checkcode (const Proto *pt) { 10.488 + return (symbexec(pt, pt->sizecode, NO_REG) != 0); 10.489 +} 10.490 + 10.491 + 10.492 +static const char *kname (Proto *p, int c) { 10.493 + if (ISK(c) && ttisstring(&p->k[INDEXK(c)])) 10.494 + return svalue(&p->k[INDEXK(c)]); 10.495 + else 10.496 + return "?"; 10.497 +} 10.498 + 10.499 + 10.500 +static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos, 10.501 + const char **name) { 10.502 + if (isLua(ci)) { /* a Lua function? */ 10.503 + Proto *p = ci_func(ci)->l.p; 10.504 + int pc = currentpc(L, ci); 10.505 + Instruction i; 10.506 + *name = luaF_getlocalname(p, stackpos+1, pc); 10.507 + if (*name) /* is a local? */ 10.508 + return "local"; 10.509 + i = symbexec(p, pc, stackpos); /* try symbolic execution */ 10.510 + lua_assert(pc != -1); 10.511 + switch (GET_OPCODE(i)) { 10.512 + case OP_GETGLOBAL: { 10.513 + int g = GETARG_Bx(i); /* global index */ 10.514 + lua_assert(ttisstring(&p->k[g])); 10.515 + *name = svalue(&p->k[g]); 10.516 + return "global"; 10.517 + } 10.518 + case OP_MOVE: { 10.519 + int a = GETARG_A(i); 10.520 + int b = GETARG_B(i); /* move from `b' to `a' */ 10.521 + if (b < a) 10.522 + return getobjname(L, ci, b, name); /* get name for `b' */ 10.523 + break; 10.524 + } 10.525 + case OP_GETTABLE: { 10.526 + int k = GETARG_C(i); /* key index */ 10.527 + *name = kname(p, k); 10.528 + return "field"; 10.529 + } 10.530 + case OP_GETUPVAL: { 10.531 + int u = GETARG_B(i); /* upvalue index */ 10.532 + *name = p->upvalues ? getstr(p->upvalues[u]) : "?"; 10.533 + return "upvalue"; 10.534 + } 10.535 + case OP_SELF: { 10.536 + int k = GETARG_C(i); /* key index */ 10.537 + *name = kname(p, k); 10.538 + return "method"; 10.539 + } 10.540 + default: break; 10.541 + } 10.542 + } 10.543 + return NULL; /* no useful name found */ 10.544 +} 10.545 + 10.546 + 10.547 +static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { 10.548 + Instruction i; 10.549 + if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1)) 10.550 + return NULL; /* calling function is not Lua (or is unknown) */ 10.551 + ci--; /* calling function */ 10.552 + i = ci_func(ci)->l.p->code[currentpc(L, ci)]; 10.553 + if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL || 10.554 + GET_OPCODE(i) == OP_TFORLOOP) 10.555 + return getobjname(L, ci, GETARG_A(i), name); 10.556 + else 10.557 + return NULL; /* no useful name can be found */ 10.558 +} 10.559 + 10.560 + 10.561 +/* only ANSI way to check whether a pointer points to an array */ 10.562 +static int isinstack (CallInfo *ci, const TValue *o) { 10.563 + StkId p; 10.564 + for (p = ci->base; p < ci->top; p++) 10.565 + if (o == p) return 1; 10.566 + return 0; 10.567 +} 10.568 + 10.569 + 10.570 +void luaG_typeerror (lua_State *L, const TValue *o, const char *op) { 10.571 + const char *name = NULL; 10.572 + const char *t = luaT_typenames[ttype(o)]; 10.573 + const char *kind = (isinstack(L->ci, o)) ? 10.574 + getobjname(L, L->ci, cast_int(o - L->base), &name) : 10.575 + NULL; 10.576 + if (kind) 10.577 + luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", 10.578 + op, kind, name, t); 10.579 + else 10.580 + luaG_runerror(L, "attempt to %s a %s value", op, t); 10.581 +} 10.582 + 10.583 + 10.584 +void luaG_concaterror (lua_State *L, StkId p1, StkId p2) { 10.585 + if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; 10.586 + lua_assert(!ttisstring(p1) && !ttisnumber(p1)); 10.587 + luaG_typeerror(L, p1, "concatenate"); 10.588 +} 10.589 + 10.590 + 10.591 +void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { 10.592 + TValue temp; 10.593 + if (luaV_tonumber(p1, &temp) == NULL) 10.594 + p2 = p1; /* first operand is wrong */ 10.595 + luaG_typeerror(L, p2, "perform arithmetic on"); 10.596 +} 10.597 + 10.598 + 10.599 +int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { 10.600 + const char *t1 = luaT_typenames[ttype(p1)]; 10.601 + const char *t2 = luaT_typenames[ttype(p2)]; 10.602 + if (t1[2] == t2[2]) 10.603 + luaG_runerror(L, "attempt to compare two %s values", t1); 10.604 + else 10.605 + luaG_runerror(L, "attempt to compare %s with %s", t1, t2); 10.606 + return 0; 10.607 +} 10.608 + 10.609 + 10.610 +static void addinfo (lua_State *L, const char *msg) { 10.611 + CallInfo *ci = L->ci; 10.612 + if (isLua(ci)) { /* is Lua code? */ 10.613 + char buff[LUA_IDSIZE]; /* add file:line information */ 10.614 + int line = currentline(L, ci); 10.615 + luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE); 10.616 + luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); 10.617 + } 10.618 +} 10.619 + 10.620 + 10.621 +void luaG_errormsg (lua_State *L) { 10.622 + if (L->errfunc != 0) { /* is there an error handling function? */ 10.623 + StkId errfunc = restorestack(L, L->errfunc); 10.624 + if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); 10.625 + setobjs2s(L, L->top, L->top - 1); /* move argument */ 10.626 + setobjs2s(L, L->top - 1, errfunc); /* push function */ 10.627 + incr_top(L); 10.628 + luaD_call(L, L->top - 2, 1); /* call it */ 10.629 + } 10.630 + luaD_throw(L, LUA_ERRRUN); 10.631 +} 10.632 + 10.633 + 10.634 +void luaG_runerror (lua_State *L, const char *fmt, ...) { 10.635 + va_list argp; 10.636 + va_start(argp, fmt); 10.637 + addinfo(L, luaO_pushvfstring(L, fmt, argp)); 10.638 + va_end(argp); 10.639 + luaG_errormsg(L); 10.640 +} 10.641 +
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/src/lua/ldebug.h Sat Mar 03 11:07:39 2012 -0600 11.3 @@ -0,0 +1,33 @@ 11.4 +/* 11.5 +** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $ 11.6 +** Auxiliary functions from Debug Interface module 11.7 +** See Copyright Notice in lua.h 11.8 +*/ 11.9 + 11.10 +#ifndef ldebug_h 11.11 +#define ldebug_h 11.12 + 11.13 + 11.14 +#include "lstate.h" 11.15 + 11.16 + 11.17 +#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 11.18 + 11.19 +#define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 11.20 + 11.21 +#define resethookcount(L) (L->hookcount = L->basehookcount) 11.22 + 11.23 + 11.24 +LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o, 11.25 + const char *opname); 11.26 +LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2); 11.27 +LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1, 11.28 + const TValue *p2); 11.29 +LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1, 11.30 + const TValue *p2); 11.31 +LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...); 11.32 +LUAI_FUNC void luaG_errormsg (lua_State *L); 11.33 +LUAI_FUNC int luaG_checkcode (const Proto *pt); 11.34 +LUAI_FUNC int luaG_checkopenop (Instruction i); 11.35 + 11.36 +#endif
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/src/lua/ldo.c Sat Mar 03 11:07:39 2012 -0600 12.3 @@ -0,0 +1,518 @@ 12.4 +/* 12.5 +** $Id: ldo.c,v 2.38.1.3 2008/01/18 22:31:22 roberto Exp $ 12.6 +** Stack and Call structure of Lua 12.7 +** See Copyright Notice in lua.h 12.8 +*/ 12.9 + 12.10 + 12.11 +#include <setjmp.h> 12.12 +#include <stdlib.h> 12.13 +#include <string.h> 12.14 + 12.15 +#define ldo_c 12.16 +#define LUA_CORE 12.17 + 12.18 +#include "lua.h" 12.19 + 12.20 +#include "ldebug.h" 12.21 +#include "ldo.h" 12.22 +#include "lfunc.h" 12.23 +#include "lgc.h" 12.24 +#include "lmem.h" 12.25 +#include "lobject.h" 12.26 +#include "lopcodes.h" 12.27 +#include "lparser.h" 12.28 +#include "lstate.h" 12.29 +#include "lstring.h" 12.30 +#include "ltable.h" 12.31 +#include "ltm.h" 12.32 +#include "lundump.h" 12.33 +#include "lvm.h" 12.34 +#include "lzio.h" 12.35 + 12.36 + 12.37 + 12.38 + 12.39 +/* 12.40 +** {====================================================== 12.41 +** Error-recovery functions 12.42 +** ======================================================= 12.43 +*/ 12.44 + 12.45 + 12.46 +/* chain list of long jump buffers */ 12.47 +struct lua_longjmp { 12.48 + struct lua_longjmp *previous; 12.49 + luai_jmpbuf b; 12.50 + volatile int status; /* error code */ 12.51 +}; 12.52 + 12.53 + 12.54 +void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { 12.55 + switch (errcode) { 12.56 + case LUA_ERRMEM: { 12.57 + setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG)); 12.58 + break; 12.59 + } 12.60 + case LUA_ERRERR: { 12.61 + setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); 12.62 + break; 12.63 + } 12.64 + case LUA_ERRSYNTAX: 12.65 + case LUA_ERRRUN: { 12.66 + setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ 12.67 + break; 12.68 + } 12.69 + } 12.70 + L->top = oldtop + 1; 12.71 +} 12.72 + 12.73 + 12.74 +static void restore_stack_limit (lua_State *L) { 12.75 + lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); 12.76 + if (L->size_ci > LUAI_MAXCALLS) { /* there was an overflow? */ 12.77 + int inuse = cast_int(L->ci - L->base_ci); 12.78 + if (inuse + 1 < LUAI_MAXCALLS) /* can `undo' overflow? */ 12.79 + luaD_reallocCI(L, LUAI_MAXCALLS); 12.80 + } 12.81 +} 12.82 + 12.83 + 12.84 +static void resetstack (lua_State *L, int status) { 12.85 + L->ci = L->base_ci; 12.86 + L->base = L->ci->base; 12.87 + luaF_close(L, L->base); /* close eventual pending closures */ 12.88 + luaD_seterrorobj(L, status, L->base); 12.89 + L->nCcalls = L->baseCcalls; 12.90 + L->allowhook = 1; 12.91 + restore_stack_limit(L); 12.92 + L->errfunc = 0; 12.93 + L->errorJmp = NULL; 12.94 +} 12.95 + 12.96 + 12.97 +void luaD_throw (lua_State *L, int errcode) { 12.98 + if (L->errorJmp) { 12.99 + L->errorJmp->status = errcode; 12.100 + LUAI_THROW(L, L->errorJmp); 12.101 + } 12.102 + else { 12.103 + L->status = cast_byte(errcode); 12.104 + if (G(L)->panic) { 12.105 + resetstack(L, errcode); 12.106 + lua_unlock(L); 12.107 + G(L)->panic(L); 12.108 + } 12.109 + exit(EXIT_FAILURE); 12.110 + } 12.111 +} 12.112 + 12.113 + 12.114 +int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { 12.115 + struct lua_longjmp lj; 12.116 + lj.status = 0; 12.117 + lj.previous = L->errorJmp; /* chain new error handler */ 12.118 + L->errorJmp = &lj; 12.119 + LUAI_TRY(L, &lj, 12.120 + (*f)(L, ud); 12.121 + ); 12.122 + L->errorJmp = lj.previous; /* restore old error handler */ 12.123 + return lj.status; 12.124 +} 12.125 + 12.126 +/* }====================================================== */ 12.127 + 12.128 + 12.129 +static void correctstack (lua_State *L, TValue *oldstack) { 12.130 + CallInfo *ci; 12.131 + GCObject *up; 12.132 + L->top = (L->top - oldstack) + L->stack; 12.133 + for (up = L->openupval; up != NULL; up = up->gch.next) 12.134 + gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; 12.135 + for (ci = L->base_ci; ci <= L->ci; ci++) { 12.136 + ci->top = (ci->top - oldstack) + L->stack; 12.137 + ci->base = (ci->base - oldstack) + L->stack; 12.138 + ci->func = (ci->func - oldstack) + L->stack; 12.139 + } 12.140 + L->base = (L->base - oldstack) + L->stack; 12.141 +} 12.142 + 12.143 + 12.144 +void luaD_reallocstack (lua_State *L, int newsize) { 12.145 + TValue *oldstack = L->stack; 12.146 + int realsize = newsize + 1 + EXTRA_STACK; 12.147 + lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); 12.148 + luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue); 12.149 + L->stacksize = realsize; 12.150 + L->stack_last = L->stack+newsize; 12.151 + correctstack(L, oldstack); 12.152 +} 12.153 + 12.154 + 12.155 +void luaD_reallocCI (lua_State *L, int newsize) { 12.156 + CallInfo *oldci = L->base_ci; 12.157 + luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo); 12.158 + L->size_ci = newsize; 12.159 + L->ci = (L->ci - oldci) + L->base_ci; 12.160 + L->end_ci = L->base_ci + L->size_ci - 1; 12.161 +} 12.162 + 12.163 + 12.164 +void luaD_growstack (lua_State *L, int n) { 12.165 + if (n <= L->stacksize) /* double size is enough? */ 12.166 + luaD_reallocstack(L, 2*L->stacksize); 12.167 + else 12.168 + luaD_reallocstack(L, L->stacksize + n); 12.169 +} 12.170 + 12.171 + 12.172 +static CallInfo *growCI (lua_State *L) { 12.173 + if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */ 12.174 + luaD_throw(L, LUA_ERRERR); 12.175 + else { 12.176 + luaD_reallocCI(L, 2*L->size_ci); 12.177 + if (L->size_ci > LUAI_MAXCALLS) 12.178 + luaG_runerror(L, "stack overflow"); 12.179 + } 12.180 + return ++L->ci; 12.181 +} 12.182 + 12.183 + 12.184 +void luaD_callhook (lua_State *L, int event, int line) { 12.185 + lua_Hook hook = L->hook; 12.186 + if (hook && L->allowhook) { 12.187 + ptrdiff_t top = savestack(L, L->top); 12.188 + ptrdiff_t ci_top = savestack(L, L->ci->top); 12.189 + lua_Debug ar; 12.190 + ar.event = event; 12.191 + ar.currentline = line; 12.192 + if (event == LUA_HOOKTAILRET) 12.193 + ar.i_ci = 0; /* tail call; no debug information about it */ 12.194 + else 12.195 + ar.i_ci = cast_int(L->ci - L->base_ci); 12.196 + luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 12.197 + L->ci->top = L->top + LUA_MINSTACK; 12.198 + lua_assert(L->ci->top <= L->stack_last); 12.199 + L->allowhook = 0; /* cannot call hooks inside a hook */ 12.200 + lua_unlock(L); 12.201 + (*hook)(L, &ar); 12.202 + lua_lock(L); 12.203 + lua_assert(!L->allowhook); 12.204 + L->allowhook = 1; 12.205 + L->ci->top = restorestack(L, ci_top); 12.206 + L->top = restorestack(L, top); 12.207 + } 12.208 +} 12.209 + 12.210 + 12.211 +static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { 12.212 + int i; 12.213 + int nfixargs = p->numparams; 12.214 + Table *htab = NULL; 12.215 + StkId base, fixed; 12.216 + for (; actual < nfixargs; ++actual) 12.217 + setnilvalue(L->top++); 12.218 +#if defined(LUA_COMPAT_VARARG) 12.219 + if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */ 12.220 + int nvar = actual - nfixargs; /* number of extra arguments */ 12.221 + lua_assert(p->is_vararg & VARARG_HASARG); 12.222 + luaC_checkGC(L); 12.223 + htab = luaH_new(L, nvar, 1); /* create `arg' table */ 12.224 + for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */ 12.225 + setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i); 12.226 + /* store counter in field `n' */ 12.227 + setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar)); 12.228 + } 12.229 +#endif 12.230 + /* move fixed parameters to final position */ 12.231 + fixed = L->top - actual; /* first fixed argument */ 12.232 + base = L->top; /* final position of first argument */ 12.233 + for (i=0; i<nfixargs; i++) { 12.234 + setobjs2s(L, L->top++, fixed+i); 12.235 + setnilvalue(fixed+i); 12.236 + } 12.237 + /* add `arg' parameter */ 12.238 + if (htab) { 12.239 + sethvalue(L, L->top++, htab); 12.240 + lua_assert(iswhite(obj2gco(htab))); 12.241 + } 12.242 + return base; 12.243 +} 12.244 + 12.245 + 12.246 +static StkId tryfuncTM (lua_State *L, StkId func) { 12.247 + const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); 12.248 + StkId p; 12.249 + ptrdiff_t funcr = savestack(L, func); 12.250 + if (!ttisfunction(tm)) 12.251 + luaG_typeerror(L, func, "call"); 12.252 + /* Open a hole inside the stack at `func' */ 12.253 + for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); 12.254 + incr_top(L); 12.255 + func = restorestack(L, funcr); /* previous call may change stack */ 12.256 + setobj2s(L, func, tm); /* tag method is the new function to be called */ 12.257 + return func; 12.258 +} 12.259 + 12.260 + 12.261 + 12.262 +#define inc_ci(L) \ 12.263 + ((L->ci == L->end_ci) ? growCI(L) : \ 12.264 + (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci)) 12.265 + 12.266 + 12.267 +int luaD_precall (lua_State *L, StkId func, int nresults) { 12.268 + LClosure *cl; 12.269 + ptrdiff_t funcr; 12.270 + if (!ttisfunction(func)) /* `func' is not a function? */ 12.271 + func = tryfuncTM(L, func); /* check the `function' tag method */ 12.272 + funcr = savestack(L, func); 12.273 + cl = &clvalue(func)->l; 12.274 + L->ci->savedpc = L->savedpc; 12.275 + if (!cl->isC) { /* Lua function? prepare its call */ 12.276 + CallInfo *ci; 12.277 + StkId st, base; 12.278 + Proto *p = cl->p; 12.279 + luaD_checkstack(L, p->maxstacksize); 12.280 + func = restorestack(L, funcr); 12.281 + if (!p->is_vararg) { /* no varargs? */ 12.282 + base = func + 1; 12.283 + if (L->top > base + p->numparams) 12.284 + L->top = base + p->numparams; 12.285 + } 12.286 + else { /* vararg function */ 12.287 + int nargs = cast_int(L->top - func) - 1; 12.288 + base = adjust_varargs(L, p, nargs); 12.289 + func = restorestack(L, funcr); /* previous call may change the stack */ 12.290 + } 12.291 + ci = inc_ci(L); /* now `enter' new function */ 12.292 + ci->func = func; 12.293 + L->base = ci->base = base; 12.294 + ci->top = L->base + p->maxstacksize; 12.295 + lua_assert(ci->top <= L->stack_last); 12.296 + L->savedpc = p->code; /* starting point */ 12.297 + ci->tailcalls = 0; 12.298 + ci->nresults = nresults; 12.299 + for (st = L->top; st < ci->top; st++) 12.300 + setnilvalue(st); 12.301 + L->top = ci->top; 12.302 + if (L->hookmask & LUA_MASKCALL) { 12.303 + L->savedpc++; /* hooks assume 'pc' is already incremented */ 12.304 + luaD_callhook(L, LUA_HOOKCALL, -1); 12.305 + L->savedpc--; /* correct 'pc' */ 12.306 + } 12.307 + return PCRLUA; 12.308 + } 12.309 + else { /* if is a C function, call it */ 12.310 + CallInfo *ci; 12.311 + int n; 12.312 + luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 12.313 + ci = inc_ci(L); /* now `enter' new function */ 12.314 + ci->func = restorestack(L, funcr); 12.315 + L->base = ci->base = ci->func + 1; 12.316 + ci->top = L->top + LUA_MINSTACK; 12.317 + lua_assert(ci->top <= L->stack_last); 12.318 + ci->nresults = nresults; 12.319 + if (L->hookmask & LUA_MASKCALL) 12.320 + luaD_callhook(L, LUA_HOOKCALL, -1); 12.321 + lua_unlock(L); 12.322 + n = (*curr_func(L)->c.f)(L); /* do the actual call */ 12.323 + lua_lock(L); 12.324 + if (n < 0) /* yielding? */ 12.325 + return PCRYIELD; 12.326 + else { 12.327 + luaD_poscall(L, L->top - n); 12.328 + return PCRC; 12.329 + } 12.330 + } 12.331 +} 12.332 + 12.333 + 12.334 +static StkId callrethooks (lua_State *L, StkId firstResult) { 12.335 + ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */ 12.336 + luaD_callhook(L, LUA_HOOKRET, -1); 12.337 + if (f_isLua(L->ci)) { /* Lua function? */ 12.338 + while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */ 12.339 + luaD_callhook(L, LUA_HOOKTAILRET, -1); 12.340 + } 12.341 + return restorestack(L, fr); 12.342 +} 12.343 + 12.344 + 12.345 +int luaD_poscall (lua_State *L, StkId firstResult) { 12.346 + StkId res; 12.347 + int wanted, i; 12.348 + CallInfo *ci; 12.349 + if (L->hookmask & LUA_MASKRET) 12.350 + firstResult = callrethooks(L, firstResult); 12.351 + ci = L->ci--; 12.352 + res = ci->func; /* res == final position of 1st result */ 12.353 + wanted = ci->nresults; 12.354 + L->base = (ci - 1)->base; /* restore base */ 12.355 + L->savedpc = (ci - 1)->savedpc; /* restore savedpc */ 12.356 + /* move results to correct place */ 12.357 + for (i = wanted; i != 0 && firstResult < L->top; i--) 12.358 + setobjs2s(L, res++, firstResult++); 12.359 + while (i-- > 0) 12.360 + setnilvalue(res++); 12.361 + L->top = res; 12.362 + return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ 12.363 +} 12.364 + 12.365 + 12.366 +/* 12.367 +** Call a function (C or Lua). The function to be called is at *func. 12.368 +** The arguments are on the stack, right after the function. 12.369 +** When returns, all the results are on the stack, starting at the original 12.370 +** function position. 12.371 +*/ 12.372 +void luaD_call (lua_State *L, StkId func, int nResults) { 12.373 + if (++L->nCcalls >= LUAI_MAXCCALLS) { 12.374 + if (L->nCcalls == LUAI_MAXCCALLS) 12.375 + luaG_runerror(L, "C stack overflow"); 12.376 + else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) 12.377 + luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ 12.378 + } 12.379 + if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */ 12.380 + luaV_execute(L, 1); /* call it */ 12.381 + L->nCcalls--; 12.382 + luaC_checkGC(L); 12.383 +} 12.384 + 12.385 + 12.386 +static void resume (lua_State *L, void *ud) { 12.387 + StkId firstArg = cast(StkId, ud); 12.388 + CallInfo *ci = L->ci; 12.389 + if (L->status == 0) { /* start coroutine? */ 12.390 + lua_assert(ci == L->base_ci && firstArg > L->base); 12.391 + if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA) 12.392 + return; 12.393 + } 12.394 + else { /* resuming from previous yield */ 12.395 + lua_assert(L->status == LUA_YIELD); 12.396 + L->status = 0; 12.397 + if (!f_isLua(ci)) { /* `common' yield? */ 12.398 + /* finish interrupted execution of `OP_CALL' */ 12.399 + lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL || 12.400 + GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL); 12.401 + if (luaD_poscall(L, firstArg)) /* complete it... */ 12.402 + L->top = L->ci->top; /* and correct top if not multiple results */ 12.403 + } 12.404 + else /* yielded inside a hook: just continue its execution */ 12.405 + L->base = L->ci->base; 12.406 + } 12.407 + luaV_execute(L, cast_int(L->ci - L->base_ci)); 12.408 +} 12.409 + 12.410 + 12.411 +static int resume_error (lua_State *L, const char *msg) { 12.412 + L->top = L->ci->base; 12.413 + setsvalue2s(L, L->top, luaS_new(L, msg)); 12.414 + incr_top(L); 12.415 + lua_unlock(L); 12.416 + return LUA_ERRRUN; 12.417 +} 12.418 + 12.419 + 12.420 +LUA_API int lua_resume (lua_State *L, int nargs) { 12.421 + int status; 12.422 + lua_lock(L); 12.423 + if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci)) 12.424 + return resume_error(L, "cannot resume non-suspended coroutine"); 12.425 + if (L->nCcalls >= LUAI_MAXCCALLS) 12.426 + return resume_error(L, "C stack overflow"); 12.427 + luai_userstateresume(L, nargs); 12.428 + lua_assert(L->errfunc == 0); 12.429 + L->baseCcalls = ++L->nCcalls; 12.430 + status = luaD_rawrunprotected(L, resume, L->top - nargs); 12.431 + if (status != 0) { /* error? */ 12.432 + L->status = cast_byte(status); /* mark thread as `dead' */ 12.433 + luaD_seterrorobj(L, status, L->top); 12.434 + L->ci->top = L->top; 12.435 + } 12.436 + else { 12.437 + lua_assert(L->nCcalls == L->baseCcalls); 12.438 + status = L->status; 12.439 + } 12.440 + --L->nCcalls; 12.441 + lua_unlock(L); 12.442 + return status; 12.443 +} 12.444 + 12.445 + 12.446 +LUA_API int lua_yield (lua_State *L, int nresults) { 12.447 + luai_userstateyield(L, nresults); 12.448 + lua_lock(L); 12.449 + if (L->nCcalls > L->baseCcalls) 12.450 + luaG_runerror(L, "attempt to yield across metamethod/C-call boundary"); 12.451 + L->base = L->top - nresults; /* protect stack slots below */ 12.452 + L->status = LUA_YIELD; 12.453 + lua_unlock(L); 12.454 + return -1; 12.455 +} 12.456 + 12.457 + 12.458 +int luaD_pcall (lua_State *L, Pfunc func, void *u, 12.459 + ptrdiff_t old_top, ptrdiff_t ef) { 12.460 + int status; 12.461 + unsigned short oldnCcalls = L->nCcalls; 12.462 + ptrdiff_t old_ci = saveci(L, L->ci); 12.463 + lu_byte old_allowhooks = L->allowhook; 12.464 + ptrdiff_t old_errfunc = L->errfunc; 12.465 + L->errfunc = ef; 12.466 + status = luaD_rawrunprotected(L, func, u); 12.467 + if (status != 0) { /* an error occurred? */ 12.468 + StkId oldtop = restorestack(L, old_top); 12.469 + luaF_close(L, oldtop); /* close eventual pending closures */ 12.470 + luaD_seterrorobj(L, status, oldtop); 12.471 + L->nCcalls = oldnCcalls; 12.472 + L->ci = restoreci(L, old_ci); 12.473 + L->base = L->ci->base; 12.474 + L->savedpc = L->ci->savedpc; 12.475 + L->allowhook = old_allowhooks; 12.476 + restore_stack_limit(L); 12.477 + } 12.478 + L->errfunc = old_errfunc; 12.479 + return status; 12.480 +} 12.481 + 12.482 + 12.483 + 12.484 +/* 12.485 +** Execute a protected parser. 12.486 +*/ 12.487 +struct SParser { /* data to `f_parser' */ 12.488 + ZIO *z; 12.489 + Mbuffer buff; /* buffer to be used by the scanner */ 12.490 + const char *name; 12.491 +}; 12.492 + 12.493 +static void f_parser (lua_State *L, void *ud) { 12.494 + int i; 12.495 + Proto *tf; 12.496 + Closure *cl; 12.497 + struct SParser *p = cast(struct SParser *, ud); 12.498 + int c = luaZ_lookahead(p->z); 12.499 + luaC_checkGC(L); 12.500 + tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z, 12.501 + &p->buff, p->name); 12.502 + cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L))); 12.503 + cl->l.p = tf; 12.504 + for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */ 12.505 + cl->l.upvals[i] = luaF_newupval(L); 12.506 + setclvalue(L, L->top, cl); 12.507 + incr_top(L); 12.508 +} 12.509 + 12.510 + 12.511 +int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) { 12.512 + struct SParser p; 12.513 + int status; 12.514 + p.z = z; p.name = name; 12.515 + luaZ_initbuffer(L, &p.buff); 12.516 + status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); 12.517 + luaZ_freebuffer(L, &p.buff); 12.518 + return status; 12.519 +} 12.520 + 12.521 +
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 13.2 +++ b/src/lua/ldo.h Sat Mar 03 11:07:39 2012 -0600 13.3 @@ -0,0 +1,57 @@ 13.4 +/* 13.5 +** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $ 13.6 +** Stack and Call structure of Lua 13.7 +** See Copyright Notice in lua.h 13.8 +*/ 13.9 + 13.10 +#ifndef ldo_h 13.11 +#define ldo_h 13.12 + 13.13 + 13.14 +#include "lobject.h" 13.15 +#include "lstate.h" 13.16 +#include "lzio.h" 13.17 + 13.18 + 13.19 +#define luaD_checkstack(L,n) \ 13.20 + if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \ 13.21 + luaD_growstack(L, n); \ 13.22 + else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); 13.23 + 13.24 + 13.25 +#define incr_top(L) {luaD_checkstack(L,1); L->top++;} 13.26 + 13.27 +#define savestack(L,p) ((char *)(p) - (char *)L->stack) 13.28 +#define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 13.29 + 13.30 +#define saveci(L,p) ((char *)(p) - (char *)L->base_ci) 13.31 +#define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) 13.32 + 13.33 + 13.34 +/* results from luaD_precall */ 13.35 +#define PCRLUA 0 /* initiated a call to a Lua function */ 13.36 +#define PCRC 1 /* did a call to a C function */ 13.37 +#define PCRYIELD 2 /* C funtion yielded */ 13.38 + 13.39 + 13.40 +/* type of protected functions, to be ran by `runprotected' */ 13.41 +typedef void (*Pfunc) (lua_State *L, void *ud); 13.42 + 13.43 +LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name); 13.44 +LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line); 13.45 +LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 13.46 +LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 13.47 +LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 13.48 + ptrdiff_t oldtop, ptrdiff_t ef); 13.49 +LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 13.50 +LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize); 13.51 +LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 13.52 +LUAI_FUNC void luaD_growstack (lua_State *L, int n); 13.53 + 13.54 +LUAI_FUNC void luaD_throw (lua_State *L, int errcode); 13.55 +LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 13.56 + 13.57 +LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); 13.58 + 13.59 +#endif 13.60 +
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/src/lua/ldump.c Sat Mar 03 11:07:39 2012 -0600 14.3 @@ -0,0 +1,164 @@ 14.4 +/* 14.5 +** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 14.6 +** save precompiled Lua chunks 14.7 +** See Copyright Notice in lua.h 14.8 +*/ 14.9 + 14.10 +#include <stddef.h> 14.11 + 14.12 +#define ldump_c 14.13 +#define LUA_CORE 14.14 + 14.15 +#include "lua.h" 14.16 + 14.17 +#include "lobject.h" 14.18 +#include "lstate.h" 14.19 +#include "lundump.h" 14.20 + 14.21 +typedef struct { 14.22 + lua_State* L; 14.23 + lua_Writer writer; 14.24 + void* data; 14.25 + int strip; 14.26 + int status; 14.27 +} DumpState; 14.28 + 14.29 +#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 14.30 +#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 14.31 + 14.32 +static void DumpBlock(const void* b, size_t size, DumpState* D) 14.33 +{ 14.34 + if (D->status==0) 14.35 + { 14.36 + lua_unlock(D->L); 14.37 + D->status=(*D->writer)(D->L,b,size,D->data); 14.38 + lua_lock(D->L); 14.39 + } 14.40 +} 14.41 + 14.42 +static void DumpChar(int y, DumpState* D) 14.43 +{ 14.44 + char x=(char)y; 14.45 + DumpVar(x,D); 14.46 +} 14.47 + 14.48 +static void DumpInt(int x, DumpState* D) 14.49 +{ 14.50 + DumpVar(x,D); 14.51 +} 14.52 + 14.53 +static void DumpNumber(lua_Number x, DumpState* D) 14.54 +{ 14.55 + DumpVar(x,D); 14.56 +} 14.57 + 14.58 +static void DumpVector(const void* b, int n, size_t size, DumpState* D) 14.59 +{ 14.60 + DumpInt(n,D); 14.61 + DumpMem(b,n,size,D); 14.62 +} 14.63 + 14.64 +static void DumpString(const TString* s, DumpState* D) 14.65 +{ 14.66 + if (s==NULL || getstr(s)==NULL) 14.67 + { 14.68 + size_t size=0; 14.69 + DumpVar(size,D); 14.70 + } 14.71 + else 14.72 + { 14.73 + size_t size=s->tsv.len+1; /* include trailing '\0' */ 14.74 + DumpVar(size,D); 14.75 + DumpBlock(getstr(s),size,D); 14.76 + } 14.77 +} 14.78 + 14.79 +#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 14.80 + 14.81 +static void DumpFunction(const Proto* f, const TString* p, DumpState* D); 14.82 + 14.83 +static void DumpConstants(const Proto* f, DumpState* D) 14.84 +{ 14.85 + int i,n=f->sizek; 14.86 + DumpInt(n,D); 14.87 + for (i=0; i<n; i++) 14.88 + { 14.89 + const TValue* o=&f->k[i]; 14.90 + DumpChar(ttype(o),D); 14.91 + switch (ttype(o)) 14.92 + { 14.93 + case LUA_TNIL: 14.94 + break; 14.95 + case LUA_TBOOLEAN: 14.96 + DumpChar(bvalue(o),D); 14.97 + break; 14.98 + case LUA_TNUMBER: 14.99 + DumpNumber(nvalue(o),D); 14.100 + break; 14.101 + case LUA_TSTRING: 14.102 + DumpString(rawtsvalue(o),D); 14.103 + break; 14.104 + default: 14.105 + lua_assert(0); /* cannot happen */ 14.106 + break; 14.107 + } 14.108 + } 14.109 + n=f->sizep; 14.110 + DumpInt(n,D); 14.111 + for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D); 14.112 +} 14.113 + 14.114 +static void DumpDebug(const Proto* f, DumpState* D) 14.115 +{ 14.116 + int i,n; 14.117 + n= (D->strip) ? 0 : f->sizelineinfo; 14.118 + DumpVector(f->lineinfo,n,sizeof(int),D); 14.119 + n= (D->strip) ? 0 : f->sizelocvars; 14.120 + DumpInt(n,D); 14.121 + for (i=0; i<n; i++) 14.122 + { 14.123 + DumpString(f->locvars[i].varname,D); 14.124 + DumpInt(f->locvars[i].startpc,D); 14.125 + DumpInt(f->locvars[i].endpc,D); 14.126 + } 14.127 + n= (D->strip) ? 0 : f->sizeupvalues; 14.128 + DumpInt(n,D); 14.129 + for (i=0; i<n; i++) DumpString(f->upvalues[i],D); 14.130 +} 14.131 + 14.132 +static void DumpFunction(const Proto* f, const TString* p, DumpState* D) 14.133 +{ 14.134 + DumpString((f->source==p || D->strip) ? NULL : f->source,D); 14.135 + DumpInt(f->linedefined,D); 14.136 + DumpInt(f->lastlinedefined,D); 14.137 + DumpChar(f->nups,D); 14.138 + DumpChar(f->numparams,D); 14.139 + DumpChar(f->is_vararg,D); 14.140 + DumpChar(f->maxstacksize,D); 14.141 + DumpCode(f,D); 14.142 + DumpConstants(f,D); 14.143 + DumpDebug(f,D); 14.144 +} 14.145 + 14.146 +static void DumpHeader(DumpState* D) 14.147 +{ 14.148 + char h[LUAC_HEADERSIZE]; 14.149 + luaU_header(h); 14.150 + DumpBlock(h,LUAC_HEADERSIZE,D); 14.151 +} 14.152 + 14.153 +/* 14.154 +** dump Lua function as precompiled chunk 14.155 +*/ 14.156 +int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 14.157 +{ 14.158 + DumpState D; 14.159 + D.L=L; 14.160 + D.writer=w; 14.161 + D.data=data; 14.162 + D.strip=strip; 14.163 + D.status=0; 14.164 + DumpHeader(&D); 14.165 + DumpFunction(f,NULL,&D); 14.166 + return D.status; 14.167 +}
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 15.2 +++ b/src/lua/lfunc.c Sat Mar 03 11:07:39 2012 -0600 15.3 @@ -0,0 +1,174 @@ 15.4 +/* 15.5 +** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $ 15.6 +** Auxiliary functions to manipulate prototypes and closures 15.7 +** See Copyright Notice in lua.h 15.8 +*/ 15.9 + 15.10 + 15.11 +#include <stddef.h> 15.12 + 15.13 +#define lfunc_c 15.14 +#define LUA_CORE 15.15 + 15.16 +#include "lua.h" 15.17 + 15.18 +#include "lfunc.h" 15.19 +#include "lgc.h" 15.20 +#include "lmem.h" 15.21 +#include "lobject.h" 15.22 +#include "lstate.h" 15.23 + 15.24 + 15.25 + 15.26 +Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { 15.27 + Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); 15.28 + luaC_link(L, obj2gco(c), LUA_TFUNCTION); 15.29 + c->c.isC = 1; 15.30 + c->c.env = e; 15.31 + c->c.nupvalues = cast_byte(nelems); 15.32 + return c; 15.33 +} 15.34 + 15.35 + 15.36 +Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { 15.37 + Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); 15.38 + luaC_link(L, obj2gco(c), LUA_TFUNCTION); 15.39 + c->l.isC = 0; 15.40 + c->l.env = e; 15.41 + c->l.nupvalues = cast_byte(nelems); 15.42 + while (nelems--) c->l.upvals[nelems] = NULL; 15.43 + return c; 15.44 +} 15.45 + 15.46 + 15.47 +UpVal *luaF_newupval (lua_State *L) { 15.48 + UpVal *uv = luaM_new(L, UpVal); 15.49 + luaC_link(L, obj2gco(uv), LUA_TUPVAL); 15.50 + uv->v = &uv->u.value; 15.51 + setnilvalue(uv->v); 15.52 + return uv; 15.53 +} 15.54 + 15.55 + 15.56 +UpVal *luaF_findupval (lua_State *L, StkId level) { 15.57 + global_State *g = G(L); 15.58 + GCObject **pp = &L->openupval; 15.59 + UpVal *p; 15.60 + UpVal *uv; 15.61 + while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) { 15.62 + lua_assert(p->v != &p->u.value); 15.63 + if (p->v == level) { /* found a corresponding upvalue? */ 15.64 + if (isdead(g, obj2gco(p))) /* is it dead? */ 15.65 + changewhite(obj2gco(p)); /* ressurect it */ 15.66 + return p; 15.67 + } 15.68 + pp = &p->next; 15.69 + } 15.70 + uv = luaM_new(L, UpVal); /* not found: create a new one */ 15.71 + uv->tt = LUA_TUPVAL; 15.72 + uv->marked = luaC_white(g); 15.73 + uv->v = level; /* current value lives in the stack */ 15.74 + uv->next = *pp; /* chain it in the proper position */ 15.75 + *pp = obj2gco(uv); 15.76 + uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 15.77 + uv->u.l.next = g->uvhead.u.l.next; 15.78 + uv->u.l.next->u.l.prev = uv; 15.79 + g->uvhead.u.l.next = uv; 15.80 + lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 15.81 + return uv; 15.82 +} 15.83 + 15.84 + 15.85 +static void unlinkupval (UpVal *uv) { 15.86 + lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 15.87 + uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 15.88 + uv->u.l.prev->u.l.next = uv->u.l.next; 15.89 +} 15.90 + 15.91 + 15.92 +void luaF_freeupval (lua_State *L, UpVal *uv) { 15.93 + if (uv->v != &uv->u.value) /* is it open? */ 15.94 + unlinkupval(uv); /* remove from open list */ 15.95 + luaM_free(L, uv); /* free upvalue */ 15.96 +} 15.97 + 15.98 + 15.99 +void luaF_close (lua_State *L, StkId level) { 15.100 + UpVal *uv; 15.101 + global_State *g = G(L); 15.102 + while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) { 15.103 + GCObject *o = obj2gco(uv); 15.104 + lua_assert(!isblack(o) && uv->v != &uv->u.value); 15.105 + L->openupval = uv->next; /* remove from `open' list */ 15.106 + if (isdead(g, o)) 15.107 + luaF_freeupval(L, uv); /* free upvalue */ 15.108 + else { 15.109 + unlinkupval(uv); 15.110 + setobj(L, &uv->u.value, uv->v); 15.111 + uv->v = &uv->u.value; /* now current value lives here */ 15.112 + luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ 15.113 + } 15.114 + } 15.115 +} 15.116 + 15.117 + 15.118 +Proto *luaF_newproto (lua_State *L) { 15.119 + Proto *f = luaM_new(L, Proto); 15.120 + luaC_link(L, obj2gco(f), LUA_TPROTO); 15.121 + f->k = NULL; 15.122 + f->sizek = 0; 15.123 + f->p = NULL; 15.124 + f->sizep = 0; 15.125 + f->code = NULL; 15.126 + f->sizecode = 0; 15.127 + f->sizelineinfo = 0; 15.128 + f->sizeupvalues = 0; 15.129 + f->nups = 0; 15.130 + f->upvalues = NULL; 15.131 + f->numparams = 0; 15.132 + f->is_vararg = 0; 15.133 + f->maxstacksize = 0; 15.134 + f->lineinfo = NULL; 15.135 + f->sizelocvars = 0; 15.136 + f->locvars = NULL; 15.137 + f->linedefined = 0; 15.138 + f->lastlinedefined = 0; 15.139 + f->source = NULL; 15.140 + return f; 15.141 +} 15.142 + 15.143 + 15.144 +void luaF_freeproto (lua_State *L, Proto *f) { 15.145 + luaM_freearray(L, f->code, f->sizecode, Instruction); 15.146 + luaM_freearray(L, f->p, f->sizep, Proto *); 15.147 + luaM_freearray(L, f->k, f->sizek, TValue); 15.148 + luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); 15.149 + luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); 15.150 + luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); 15.151 + luaM_free(L, f); 15.152 +} 15.153 + 15.154 + 15.155 +void luaF_freeclosure (lua_State *L, Closure *c) { 15.156 + int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : 15.157 + sizeLclosure(c->l.nupvalues); 15.158 + luaM_freemem(L, c, size); 15.159 +} 15.160 + 15.161 + 15.162 +/* 15.163 +** Look for n-th local variable at line `line' in function `func'. 15.164 +** Returns NULL if not found. 15.165 +*/ 15.166 +const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 15.167 + int i; 15.168 + for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) { 15.169 + if (pc < f->locvars[i].endpc) { /* is variable active? */ 15.170 + local_number--; 15.171 + if (local_number == 0) 15.172 + return getstr(f->locvars[i].varname); 15.173 + } 15.174 + } 15.175 + return NULL; /* not found */ 15.176 +} 15.177 +
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 16.2 +++ b/src/lua/lfunc.h Sat Mar 03 11:07:39 2012 -0600 16.3 @@ -0,0 +1,34 @@ 16.4 +/* 16.5 +** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $ 16.6 +** Auxiliary functions to manipulate prototypes and closures 16.7 +** See Copyright Notice in lua.h 16.8 +*/ 16.9 + 16.10 +#ifndef lfunc_h 16.11 +#define lfunc_h 16.12 + 16.13 + 16.14 +#include "lobject.h" 16.15 + 16.16 + 16.17 +#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 16.18 + cast(int, sizeof(TValue)*((n)-1))) 16.19 + 16.20 +#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 16.21 + cast(int, sizeof(TValue *)*((n)-1))) 16.22 + 16.23 + 16.24 +LUAI_FUNC Proto *luaF_newproto (lua_State *L); 16.25 +LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); 16.26 +LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); 16.27 +LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 16.28 +LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 16.29 +LUAI_FUNC void luaF_close (lua_State *L, StkId level); 16.30 +LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 16.31 +LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c); 16.32 +LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 16.33 +LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 16.34 + int pc); 16.35 + 16.36 + 16.37 +#endif
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 17.2 +++ b/src/lua/lgc.c Sat Mar 03 11:07:39 2012 -0600 17.3 @@ -0,0 +1,711 @@ 17.4 +/* 17.5 +** $Id: lgc.c,v 2.38.1.1 2007/12/27 13:02:25 roberto Exp $ 17.6 +** Garbage Collector 17.7 +** See Copyright Notice in lua.h 17.8 +*/ 17.9 + 17.10 +#include <string.h> 17.11 + 17.12 +#define lgc_c 17.13 +#define LUA_CORE 17.14 + 17.15 +#include "lua.h" 17.16 + 17.17 +#include "ldebug.h" 17.18 +#include "ldo.h" 17.19 +#include "lfunc.h" 17.20 +#include "lgc.h" 17.21 +#include "lmem.h" 17.22 +#include "lobject.h" 17.23 +#include "lstate.h" 17.24 +#include "lstring.h" 17.25 +#include "ltable.h" 17.26 +#include "ltm.h" 17.27 + 17.28 + 17.29 +#define GCSTEPSIZE 1024u 17.30 +#define GCSWEEPMAX 40 17.31 +#define GCSWEEPCOST 10 17.32 +#define GCFINALIZECOST 100 17.33 + 17.34 + 17.35 +#define maskmarks cast_byte(~(bitmask(BLACKBIT)|WHITEBITS)) 17.36 + 17.37 +#define makewhite(g,x) \ 17.38 + ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g))) 17.39 + 17.40 +#define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) 17.41 +#define black2gray(x) resetbit((x)->gch.marked, BLACKBIT) 17.42 + 17.43 +#define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT) 17.44 + 17.45 + 17.46 +#define isfinalized(u) testbit((u)->marked, FINALIZEDBIT) 17.47 +#define markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT) 17.48 + 17.49 + 17.50 +#define KEYWEAK bitmask(KEYWEAKBIT) 17.51 +#define VALUEWEAK bitmask(VALUEWEAKBIT) 17.52 + 17.53 + 17.54 + 17.55 +#define markvalue(g,o) { checkconsistency(o); \ 17.56 + if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); } 17.57 + 17.58 +#define markobject(g,t) { if (iswhite(obj2gco(t))) \ 17.59 + reallymarkobject(g, obj2gco(t)); } 17.60 + 17.61 + 17.62 +#define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpause) 17.63 + 17.64 + 17.65 +static void removeentry (Node *n) { 17.66 + lua_assert(ttisnil(gval(n))); 17.67 + if (iscollectable(gkey(n))) 17.68 + setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */ 17.69 +} 17.70 + 17.71 + 17.72 +static void reallymarkobject (global_State *g, GCObject *o) { 17.73 + lua_assert(iswhite(o) && !isdead(g, o)); 17.74 + white2gray(o); 17.75 + switch (o->gch.tt) { 17.76 + case LUA_TSTRING: { 17.77 + return; 17.78 + } 17.79 + case LUA_TUSERDATA: { 17.80 + Table *mt = gco2u(o)->metatable; 17.81 + gray2black(o); /* udata are never gray */ 17.82 + if (mt) markobject(g, mt); 17.83 + markobject(g, gco2u(o)->env); 17.84 + return; 17.85 + } 17.86 + case LUA_TUPVAL: { 17.87 + UpVal *uv = gco2uv(o); 17.88 + markvalue(g, uv->v); 17.89 + if (uv->v == &uv->u.value) /* closed? */ 17.90 + gray2black(o); /* open upvalues are never black */ 17.91 + return; 17.92 + } 17.93 + case LUA_TFUNCTION: { 17.94 + gco2cl(o)->c.gclist = g->gray; 17.95 + g->gray = o; 17.96 + break; 17.97 + } 17.98 + case LUA_TTABLE: { 17.99 + gco2h(o)->gclist = g->gray; 17.100 + g->gray = o; 17.101 + break; 17.102 + } 17.103 + case LUA_TTHREAD: { 17.104 + gco2th(o)->gclist = g->gray; 17.105 + g->gray = o; 17.106 + break; 17.107 + } 17.108 + case LUA_TPROTO: { 17.109 + gco2p(o)->gclist = g->gray; 17.110 + g->gray = o; 17.111 + break; 17.112 + } 17.113 + default: lua_assert(0); 17.114 + } 17.115 +} 17.116 + 17.117 + 17.118 +static void marktmu (global_State *g) { 17.119 + GCObject *u = g->tmudata; 17.120 + if (u) { 17.121 + do { 17.122 + u = u->gch.next; 17.123 + makewhite(g, u); /* may be marked, if left from previous GC */ 17.124 + reallymarkobject(g, u); 17.125 + } while (u != g->tmudata); 17.126 + } 17.127 +} 17.128 + 17.129 + 17.130 +/* move `dead' udata that need finalization to list `tmudata' */ 17.131 +size_t luaC_separateudata (lua_State *L, int all) { 17.132 + global_State *g = G(L); 17.133 + size_t deadmem = 0; 17.134 + GCObject **p = &g->mainthread->next; 17.135 + GCObject *curr; 17.136 + while ((curr = *p) != NULL) { 17.137 + if (!(iswhite(curr) || all) || isfinalized(gco2u(curr))) 17.138 + p = &curr->gch.next; /* don't bother with them */ 17.139 + else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) { 17.140 + markfinalized(gco2u(curr)); /* don't need finalization */ 17.141 + p = &curr->gch.next; 17.142 + } 17.143 + else { /* must call its gc method */ 17.144 + deadmem += sizeudata(gco2u(curr)); 17.145 + markfinalized(gco2u(curr)); 17.146 + *p = curr->gch.next; 17.147 + /* link `curr' at the end of `tmudata' list */ 17.148 + if (g->tmudata == NULL) /* list is empty? */ 17.149 + g->tmudata = curr->gch.next = curr; /* creates a circular list */ 17.150 + else { 17.151 + curr->gch.next = g->tmudata->gch.next; 17.152 + g->tmudata->gch.next = curr; 17.153 + g->tmudata = curr; 17.154 + } 17.155 + } 17.156 + } 17.157 + return deadmem; 17.158 +} 17.159 + 17.160 + 17.161 +static int traversetable (global_State *g, Table *h) { 17.162 + int i; 17.163 + int weakkey = 0; 17.164 + int weakvalue = 0; 17.165 + const TValue *mode; 17.166 + if (h->metatable) 17.167 + markobject(g, h->metatable); 17.168 + mode = gfasttm(g, h->metatable, TM_MODE); 17.169 + if (mode && ttisstring(mode)) { /* is there a weak mode? */ 17.170 + weakkey = (strchr(svalue(mode), 'k') != NULL); 17.171 + weakvalue = (strchr(svalue(mode), 'v') != NULL); 17.172 + if (weakkey || weakvalue) { /* is really weak? */ 17.173 + h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */ 17.174 + h->marked |= cast_byte((weakkey << KEYWEAKBIT) | 17.175 + (weakvalue << VALUEWEAKBIT)); 17.176 + h->gclist = g->weak; /* must be cleared after GC, ... */ 17.177 + g->weak = obj2gco(h); /* ... so put in the appropriate list */ 17.178 + } 17.179 + } 17.180 + if (weakkey && weakvalue) return 1; 17.181 + if (!weakvalue) { 17.182 + i = h->sizearray; 17.183 + while (i--) 17.184 + markvalue(g, &h->array[i]); 17.185 + } 17.186 + i = sizenode(h); 17.187 + while (i--) { 17.188 + Node *n = gnode(h, i); 17.189 + lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n))); 17.190 + if (ttisnil(gval(n))) 17.191 + removeentry(n); /* remove empty entries */ 17.192 + else { 17.193 + lua_assert(!ttisnil(gkey(n))); 17.194 + if (!weakkey) markvalue(g, gkey(n)); 17.195 + if (!weakvalue) markvalue(g, gval(n)); 17.196 + } 17.197 + } 17.198 + return weakkey || weakvalue; 17.199 +} 17.200 + 17.201 + 17.202 +/* 17.203 +** All marks are conditional because a GC may happen while the 17.204 +** prototype is still being created 17.205 +*/ 17.206 +static void traverseproto (global_State *g, Proto *f) { 17.207 + int i; 17.208 + if (f->source) stringmark(f->source); 17.209 + for (i=0; i<f->sizek; i++) /* mark literals */ 17.210 + markvalue(g, &f->k[i]); 17.211 + for (i=0; i<f->sizeupvalues; i++) { /* mark upvalue names */ 17.212 + if (f->upvalues[i]) 17.213 + stringmark(f->upvalues[i]); 17.214 + } 17.215 + for (i=0; i<f->sizep; i++) { /* mark nested protos */ 17.216 + if (f->p[i]) 17.217 + markobject(g, f->p[i]); 17.218 + } 17.219 + for (i=0; i<f->sizelocvars; i++) { /* mark local-variable names */ 17.220 + if (f->locvars[i].varname) 17.221 + stringmark(f->locvars[i].varname); 17.222 + } 17.223 +} 17.224 + 17.225 + 17.226 + 17.227 +static void traverseclosure (global_State *g, Closure *cl) { 17.228 + markobject(g, cl->c.env); 17.229 + if (cl->c.isC) { 17.230 + int i; 17.231 + for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */ 17.232 + markvalue(g, &cl->c.upvalue[i]); 17.233 + } 17.234 + else { 17.235 + int i; 17.236 + lua_assert(cl->l.nupvalues == cl->l.p->nups); 17.237 + markobject(g, cl->l.p); 17.238 + for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */ 17.239 + markobject(g, cl->l.upvals[i]); 17.240 + } 17.241 +} 17.242 + 17.243 + 17.244 +static void checkstacksizes (lua_State *L, StkId max) { 17.245 + int ci_used = cast_int(L->ci - L->base_ci); /* number of `ci' in use */ 17.246 + int s_used = cast_int(max - L->stack); /* part of stack in use */ 17.247 + if (L->size_ci > LUAI_MAXCALLS) /* handling overflow? */ 17.248 + return; /* do not touch the stacks */ 17.249 + if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci) 17.250 + luaD_reallocCI(L, L->size_ci/2); /* still big enough... */ 17.251 + condhardstacktests(luaD_reallocCI(L, ci_used + 1)); 17.252 + if (4*s_used < L->stacksize && 17.253 + 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize) 17.254 + luaD_reallocstack(L, L->stacksize/2); /* still big enough... */ 17.255 + condhardstacktests(luaD_reallocstack(L, s_used)); 17.256 +} 17.257 + 17.258 + 17.259 +static void traversestack (global_State *g, lua_State *l) { 17.260 + StkId o, lim; 17.261 + CallInfo *ci; 17.262 + markvalue(g, gt(l)); 17.263 + lim = l->top; 17.264 + for (ci = l->base_ci; ci <= l->ci; ci++) { 17.265 + lua_assert(ci->top <= l->stack_last); 17.266 + if (lim < ci->top) lim = ci->top; 17.267 + } 17.268 + for (o = l->stack; o < l->top; o++) 17.269 + markvalue(g, o); 17.270 + for (; o <= lim; o++) 17.271 + setnilvalue(o); 17.272 + checkstacksizes(l, lim); 17.273 +} 17.274 + 17.275 + 17.276 +/* 17.277 +** traverse one gray object, turning it to black. 17.278 +** Returns `quantity' traversed. 17.279 +*/ 17.280 +static l_mem propagatemark (global_State *g) { 17.281 + GCObject *o = g->gray; 17.282 + lua_assert(isgray(o)); 17.283 + gray2black(o); 17.284 + switch (o->gch.tt) { 17.285 + case LUA_TTABLE: { 17.286 + Table *h = gco2h(o); 17.287 + g->gray = h->gclist; 17.288 + if (traversetable(g, h)) /* table is weak? */ 17.289 + black2gray(o); /* keep it gray */ 17.290 + return sizeof(Table) + sizeof(TValue) * h->sizearray + 17.291 + sizeof(Node) * sizenode(h); 17.292 + } 17.293 + case LUA_TFUNCTION: { 17.294 + Closure *cl = gco2cl(o); 17.295 + g->gray = cl->c.gclist; 17.296 + traverseclosure(g, cl); 17.297 + return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) : 17.298 + sizeLclosure(cl->l.nupvalues); 17.299 + } 17.300 + case LUA_TTHREAD: { 17.301 + lua_State *th = gco2th(o); 17.302 + g->gray = th->gclist; 17.303 + th->gclist = g->grayagain; 17.304 + g->grayagain = o; 17.305 + black2gray(o); 17.306 + traversestack(g, th); 17.307 + return sizeof(lua_State) + sizeof(TValue) * th->stacksize + 17.308 + sizeof(CallInfo) * th->size_ci; 17.309 + } 17.310 + case LUA_TPROTO: { 17.311 + Proto *p = gco2p(o); 17.312 + g->gray = p->gclist; 17.313 + traverseproto(g, p); 17.314 + return sizeof(Proto) + sizeof(Instruction) * p->sizecode + 17.315 + sizeof(Proto *) * p->sizep + 17.316 + sizeof(TValue) * p->sizek + 17.317 + sizeof(int) * p->sizelineinfo + 17.318 + sizeof(LocVar) * p->sizelocvars + 17.319 + sizeof(TString *) * p->sizeupvalues; 17.320 + } 17.321 + default: lua_assert(0); return 0; 17.322 + } 17.323 +} 17.324 + 17.325 + 17.326 +static size_t propagateall (global_State *g) { 17.327 + size_t m = 0; 17.328 + while (g->gray) m += propagatemark(g); 17.329 + return m; 17.330 +} 17.331 + 17.332 + 17.333 +/* 17.334 +** The next function tells whether a key or value can be cleared from 17.335 +** a weak table. Non-collectable objects are never removed from weak 17.336 +** tables. Strings behave as `values', so are never removed too. for 17.337 +** other objects: if really collected, cannot keep them; for userdata 17.338 +** being finalized, keep them in keys, but not in values 17.339 +*/ 17.340 +static int iscleared (const TValue *o, int iskey) { 17.341 + if (!iscollectable(o)) return 0; 17.342 + if (ttisstring(o)) { 17.343 + stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */ 17.344 + return 0; 17.345 + } 17.346 + return iswhite(gcvalue(o)) || 17.347 + (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o)))); 17.348 +} 17.349 + 17.350 + 17.351 +/* 17.352 +** clear collected entries from weaktables 17.353 +*/ 17.354 +static void cleartable (GCObject *l) { 17.355 + while (l) { 17.356 + Table *h = gco2h(l); 17.357 + int i = h->sizearray; 17.358 + lua_assert(testbit(h->marked, VALUEWEAKBIT) || 17.359 + testbit(h->marked, KEYWEAKBIT)); 17.360 + if (testbit(h->marked, VALUEWEAKBIT)) { 17.361 + while (i--) { 17.362 + TValue *o = &h->array[i]; 17.363 + if (iscleared(o, 0)) /* value was collected? */ 17.364 + setnilvalue(o); /* remove value */ 17.365 + } 17.366 + } 17.367 + i = sizenode(h); 17.368 + while (i--) { 17.369 + Node *n = gnode(h, i); 17.370 + if (!ttisnil(gval(n)) && /* non-empty entry? */ 17.371 + (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) { 17.372 + setnilvalue(gval(n)); /* remove value ... */ 17.373 + removeentry(n); /* remove entry from table */ 17.374 + } 17.375 + } 17.376 + l = h->gclist; 17.377 + } 17.378 +} 17.379 + 17.380 + 17.381 +static void freeobj (lua_State *L, GCObject *o) { 17.382 + switch (o->gch.tt) { 17.383 + case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; 17.384 + case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break; 17.385 + case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break; 17.386 + case LUA_TTABLE: luaH_free(L, gco2h(o)); break; 17.387 + case LUA_TTHREAD: { 17.388 + lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread); 17.389 + luaE_freethread(L, gco2th(o)); 17.390 + break; 17.391 + } 17.392 + case LUA_TSTRING: { 17.393 + G(L)->strt.nuse--; 17.394 + luaM_freemem(L, o, sizestring(gco2ts(o))); 17.395 + break; 17.396 + } 17.397 + case LUA_TUSERDATA: { 17.398 + luaM_freemem(L, o, sizeudata(gco2u(o))); 17.399 + break; 17.400 + } 17.401 + default: lua_assert(0); 17.402 + } 17.403 +} 17.404 + 17.405 + 17.406 + 17.407 +#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM) 17.408 + 17.409 + 17.410 +static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { 17.411 + GCObject *curr; 17.412 + global_State *g = G(L); 17.413 + int deadmask = otherwhite(g); 17.414 + while ((curr = *p) != NULL && count-- > 0) { 17.415 + if (curr->gch.tt == LUA_TTHREAD) /* sweep open upvalues of each thread */ 17.416 + sweepwholelist(L, &gco2th(curr)->openupval); 17.417 + if ((curr->gch.marked ^ WHITEBITS) & deadmask) { /* not dead? */ 17.418 + lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT)); 17.419 + makewhite(g, curr); /* make it white (for next cycle) */ 17.420 + p = &curr->gch.next; 17.421 + } 17.422 + else { /* must erase `curr' */ 17.423 + lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT)); 17.424 + *p = curr->gch.next; 17.425 + if (curr == g->rootgc) /* is the first element of the list? */ 17.426 + g->rootgc = curr->gch.next; /* adjust first */ 17.427 + freeobj(L, curr); 17.428 + } 17.429 + } 17.430 + return p; 17.431 +} 17.432 + 17.433 + 17.434 +static void checkSizes (lua_State *L) { 17.435 + global_State *g = G(L); 17.436 + /* check size of string hash */ 17.437 + if (g->strt.nuse < cast(lu_int32, g->strt.size/4) && 17.438 + g->strt.size > MINSTRTABSIZE*2) 17.439 + luaS_resize(L, g->strt.size/2); /* table is too big */ 17.440 + /* check size of buffer */ 17.441 + if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */ 17.442 + size_t newsize = luaZ_sizebuffer(&g->buff) / 2; 17.443 + luaZ_resizebuffer(L, &g->buff, newsize); 17.444 + } 17.445 +} 17.446 + 17.447 + 17.448 +static void GCTM (lua_State *L) { 17.449 + global_State *g = G(L); 17.450 + GCObject *o = g->tmudata->gch.next; /* get first element */ 17.451 + Udata *udata = rawgco2u(o); 17.452 + const TValue *tm; 17.453 + /* remove udata from `tmudata' */ 17.454 + if (o == g->tmudata) /* last element? */ 17.455 + g->tmudata = NULL; 17.456 + else 17.457 + g->tmudata->gch.next = udata->uv.next; 17.458 + udata->uv.next = g->mainthread->next; /* return it to `root' list */ 17.459 + g->mainthread->next = o; 17.460 + makewhite(g, o); 17.461 + tm = fasttm(L, udata->uv.metatable, TM_GC); 17.462 + if (tm != NULL) { 17.463 + lu_byte oldah = L->allowhook; 17.464 + lu_mem oldt = g->GCthreshold; 17.465 + L->allowhook = 0; /* stop debug hooks during GC tag method */ 17.466 + g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */ 17.467 + setobj2s(L, L->top, tm); 17.468 + setuvalue(L, L->top+1, udata); 17.469 + L->top += 2; 17.470 + luaD_call(L, L->top - 2, 0); 17.471 + L->allowhook = oldah; /* restore hooks */ 17.472 + g->GCthreshold = oldt; /* restore threshold */ 17.473 + } 17.474 +} 17.475 + 17.476 + 17.477 +/* 17.478 +** Call all GC tag methods 17.479 +*/ 17.480 +void luaC_callGCTM (lua_State *L) { 17.481 + while (G(L)->tmudata) 17.482 + GCTM(L); 17.483 +} 17.484 + 17.485 + 17.486 +void luaC_freeall (lua_State *L) { 17.487 + global_State *g = G(L); 17.488 + int i; 17.489 + g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* mask to collect all elements */ 17.490 + sweepwholelist(L, &g->rootgc); 17.491 + for (i = 0; i < g->strt.size; i++) /* free all string lists */ 17.492 + sweepwholelist(L, &g->strt.hash[i]); 17.493 +} 17.494 + 17.495 + 17.496 +static void markmt (global_State *g) { 17.497 + int i; 17.498 + for (i=0; i<NUM_TAGS; i++) 17.499 + if (g->mt[i]) markobject(g, g->mt[i]); 17.500 +} 17.501 + 17.502 + 17.503 +/* mark root set */ 17.504 +static void markroot (lua_State *L) { 17.505 + global_State *g = G(L); 17.506 + g->gray = NULL; 17.507 + g->grayagain = NULL; 17.508 + g->weak = NULL; 17.509 + markobject(g, g->mainthread); 17.510 + /* make global table be traversed before main stack */ 17.511 + markvalue(g, gt(g->mainthread)); 17.512 + markvalue(g, registry(L)); 17.513 + markmt(g); 17.514 + g->gcstate = GCSpropagate; 17.515 +} 17.516 + 17.517 + 17.518 +static void remarkupvals (global_State *g) { 17.519 + UpVal *uv; 17.520 + for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) { 17.521 + lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 17.522 + if (isgray(obj2gco(uv))) 17.523 + markvalue(g, uv->v); 17.524 + } 17.525 +} 17.526 + 17.527 + 17.528 +static void atomic (lua_State *L) { 17.529 + global_State *g = G(L); 17.530 + size_t udsize; /* total size of userdata to be finalized */ 17.531 + /* remark occasional upvalues of (maybe) dead threads */ 17.532 + remarkupvals(g); 17.533 + /* traverse objects cautch by write barrier and by 'remarkupvals' */ 17.534 + propagateall(g); 17.535 + /* remark weak tables */ 17.536 + g->gray = g->weak; 17.537 + g->weak = NULL; 17.538 + lua_assert(!iswhite(obj2gco(g->mainthread))); 17.539 + markobject(g, L); /* mark running thread */ 17.540 + markmt(g); /* mark basic metatables (again) */ 17.541 + propagateall(g); 17.542 + /* remark gray again */ 17.543 + g->gray = g->grayagain; 17.544 + g->grayagain = NULL; 17.545 + propagateall(g); 17.546 + udsize = luaC_separateudata(L, 0); /* separate userdata to be finalized */ 17.547 + marktmu(g); /* mark `preserved' userdata */ 17.548 + udsize += propagateall(g); /* remark, to propagate `preserveness' */ 17.549 + cleartable(g->weak); /* remove collected objects from weak tables */ 17.550 + /* flip current white */ 17.551 + g->currentwhite = cast_byte(otherwhite(g)); 17.552 + g->sweepstrgc = 0; 17.553 + g->sweepgc = &g->rootgc; 17.554 + g->gcstate = GCSsweepstring; 17.555 + g->estimate = g->totalbytes - udsize; /* first estimate */ 17.556 +} 17.557 + 17.558 + 17.559 +static l_mem singlestep (lua_State *L) { 17.560 + global_State *g = G(L); 17.561 + /*lua_checkmemory(L);*/ 17.562 + switch (g->gcstate) { 17.563 + case GCSpause: { 17.564 + markroot(L); /* start a new collection */ 17.565 + return 0; 17.566 + } 17.567 + case GCSpropagate: { 17.568 + if (g->gray) 17.569 + return propagatemark(g); 17.570 + else { /* no more `gray' objects */ 17.571 + atomic(L); /* finish mark phase */ 17.572 + return 0; 17.573 + } 17.574 + } 17.575 + case GCSsweepstring: { 17.576 + lu_mem old = g->totalbytes; 17.577 + sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]); 17.578 + if (g->sweepstrgc >= g->strt.size) /* nothing more to sweep? */ 17.579 + g->gcstate = GCSsweep; /* end sweep-string phase */ 17.580 + lua_assert(old >= g->totalbytes); 17.581 + g->estimate -= old - g->totalbytes; 17.582 + return GCSWEEPCOST; 17.583 + } 17.584 + case GCSsweep: { 17.585 + lu_mem old = g->totalbytes; 17.586 + g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); 17.587 + if (*g->sweepgc == NULL) { /* nothing more to sweep? */ 17.588 + checkSizes(L); 17.589 + g->gcstate = GCSfinalize; /* end sweep phase */ 17.590 + } 17.591 + lua_assert(old >= g->totalbytes); 17.592 + g->estimate -= old - g->totalbytes; 17.593 + return GCSWEEPMAX*GCSWEEPCOST; 17.594 + } 17.595 + case GCSfinalize: { 17.596 + if (g->tmudata) { 17.597 + GCTM(L); 17.598 + if (g->estimate > GCFINALIZECOST) 17.599 + g->estimate -= GCFINALIZECOST; 17.600 + return GCFINALIZECOST; 17.601 + } 17.602 + else { 17.603 + g->gcstate = GCSpause; /* end collection */ 17.604 + g->gcdept = 0; 17.605 + return 0; 17.606 + } 17.607 + } 17.608 + default: lua_assert(0); return 0; 17.609 + } 17.610 +} 17.611 + 17.612 + 17.613 +void luaC_step (lua_State *L) { 17.614 + global_State *g = G(L); 17.615 + l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul; 17.616 + if (lim == 0) 17.617 + lim = (MAX_LUMEM-1)/2; /* no limit */ 17.618 + g->gcdept += g->totalbytes - g->GCthreshold; 17.619 + do { 17.620 + lim -= singlestep(L); 17.621 + if (g->gcstate == GCSpause) 17.622 + break; 17.623 + } while (lim > 0); 17.624 + if (g->gcstate != GCSpause) { 17.625 + if (g->gcdept < GCSTEPSIZE) 17.626 + g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/g->gcstepmul;*/ 17.627 + else { 17.628 + g->gcdept -= GCSTEPSIZE; 17.629 + g->GCthreshold = g->totalbytes; 17.630 + } 17.631 + } 17.632 + else { 17.633 + lua_assert(g->totalbytes >= g->estimate); 17.634 + setthreshold(g); 17.635 + } 17.636 +} 17.637 + 17.638 + 17.639 +void luaC_fullgc (lua_State *L) { 17.640 + global_State *g = G(L); 17.641 + if (g->gcstate <= GCSpropagate) { 17.642 + /* reset sweep marks to sweep all elements (returning them to white) */ 17.643 + g->sweepstrgc = 0; 17.644 + g->sweepgc = &g->rootgc; 17.645 + /* reset other collector lists */ 17.646 + g->gray = NULL; 17.647 + g->grayagain = NULL; 17.648 + g->weak = NULL; 17.649 + g->gcstate = GCSsweepstring; 17.650 + } 17.651 + lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate); 17.652 + /* finish any pending sweep phase */ 17.653 + while (g->gcstate != GCSfinalize) { 17.654 + lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep); 17.655 + singlestep(L); 17.656 + } 17.657 + markroot(L); 17.658 + while (g->gcstate != GCSpause) { 17.659 + singlestep(L); 17.660 + } 17.661 + setthreshold(g); 17.662 +} 17.663 + 17.664 + 17.665 +void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) { 17.666 + global_State *g = G(L); 17.667 + lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); 17.668 + lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); 17.669 + lua_assert(ttype(&o->gch) != LUA_TTABLE); 17.670 + /* must keep invariant? */ 17.671 + if (g->gcstate == GCSpropagate) 17.672 + reallymarkobject(g, v); /* restore invariant */ 17.673 + else /* don't mind */ 17.674 + makewhite(g, o); /* mark as white just to avoid other barriers */ 17.675 +} 17.676 + 17.677 + 17.678 +void luaC_barrierback (lua_State *L, Table *t) { 17.679 + global_State *g = G(L); 17.680 + GCObject *o = obj2gco(t); 17.681 + lua_assert(isblack(o) && !isdead(g, o)); 17.682 + lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); 17.683 + black2gray(o); /* make table gray (again) */ 17.684 + t->gclist = g->grayagain; 17.685 + g->grayagain = o; 17.686 +} 17.687 + 17.688 + 17.689 +void luaC_link (lua_State *L, GCObject *o, lu_byte tt) { 17.690 + global_State *g = G(L); 17.691 + o->gch.next = g->rootgc; 17.692 + g->rootgc = o; 17.693 + o->gch.marked = luaC_white(g); 17.694 + o->gch.tt = tt; 17.695 +} 17.696 + 17.697 + 17.698 +void luaC_linkupval (lua_State *L, UpVal *uv) { 17.699 + global_State *g = G(L); 17.700 + GCObject *o = obj2gco(uv); 17.701 + o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */ 17.702 + g->rootgc = o; 17.703 + if (isgray(o)) { 17.704 + if (g->gcstate == GCSpropagate) { 17.705 + gray2black(o); /* closed upvalues need barrier */ 17.706 + luaC_barrier(L, uv, uv->v); 17.707 + } 17.708 + else { /* sweep phase: sweep it (turning it into white) */ 17.709 + makewhite(g, o); 17.710 + lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); 17.711 + } 17.712 + } 17.713 +} 17.714 +
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 18.2 +++ b/src/lua/lgc.h Sat Mar 03 11:07:39 2012 -0600 18.3 @@ -0,0 +1,110 @@ 18.4 +/* 18.5 +** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $ 18.6 +** Garbage Collector 18.7 +** See Copyright Notice in lua.h 18.8 +*/ 18.9 + 18.10 +#ifndef lgc_h 18.11 +#define lgc_h 18.12 + 18.13 + 18.14 +#include "lobject.h" 18.15 + 18.16 + 18.17 +/* 18.18 +** Possible states of the Garbage Collector 18.19 +*/ 18.20 +#define GCSpause 0 18.21 +#define GCSpropagate 1 18.22 +#define GCSsweepstring 2 18.23 +#define GCSsweep 3 18.24 +#define GCSfinalize 4 18.25 + 18.26 + 18.27 +/* 18.28 +** some userful bit tricks 18.29 +*/ 18.30 +#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 18.31 +#define setbits(x,m) ((x) |= (m)) 18.32 +#define testbits(x,m) ((x) & (m)) 18.33 +#define bitmask(b) (1<<(b)) 18.34 +#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 18.35 +#define l_setbit(x,b) setbits(x, bitmask(b)) 18.36 +#define resetbit(x,b) resetbits(x, bitmask(b)) 18.37 +#define testbit(x,b) testbits(x, bitmask(b)) 18.38 +#define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) 18.39 +#define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2))) 18.40 +#define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2))) 18.41 + 18.42 + 18.43 + 18.44 +/* 18.45 +** Layout for bit use in `marked' field: 18.46 +** bit 0 - object is white (type 0) 18.47 +** bit 1 - object is white (type 1) 18.48 +** bit 2 - object is black 18.49 +** bit 3 - for userdata: has been finalized 18.50 +** bit 3 - for tables: has weak keys 18.51 +** bit 4 - for tables: has weak values 18.52 +** bit 5 - object is fixed (should not be collected) 18.53 +** bit 6 - object is "super" fixed (only the main thread) 18.54 +*/ 18.55 + 18.56 + 18.57 +#define WHITE0BIT 0 18.58 +#define WHITE1BIT 1 18.59 +#define BLACKBIT 2 18.60 +#define FINALIZEDBIT 3 18.61 +#define KEYWEAKBIT 3 18.62 +#define VALUEWEAKBIT 4 18.63 +#define FIXEDBIT 5 18.64 +#define SFIXEDBIT 6 18.65 +#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 18.66 + 18.67 + 18.68 +#define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) 18.69 +#define isblack(x) testbit((x)->gch.marked, BLACKBIT) 18.70 +#define isgray(x) (!isblack(x) && !iswhite(x)) 18.71 + 18.72 +#define otherwhite(g) (g->currentwhite ^ WHITEBITS) 18.73 +#define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) 18.74 + 18.75 +#define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 18.76 +#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 18.77 + 18.78 +#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 18.79 + 18.80 +#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 18.81 + 18.82 + 18.83 +#define luaC_checkGC(L) { \ 18.84 + condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ 18.85 + if (G(L)->totalbytes >= G(L)->GCthreshold) \ 18.86 + luaC_step(L); } 18.87 + 18.88 + 18.89 +#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 18.90 + luaC_barrierf(L,obj2gco(p),gcvalue(v)); } 18.91 + 18.92 +#define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ 18.93 + luaC_barrierback(L,t); } 18.94 + 18.95 +#define luaC_objbarrier(L,p,o) \ 18.96 + { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 18.97 + luaC_barrierf(L,obj2gco(p),obj2gco(o)); } 18.98 + 18.99 +#define luaC_objbarriert(L,t,o) \ 18.100 + { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } 18.101 + 18.102 +LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all); 18.103 +LUAI_FUNC void luaC_callGCTM (lua_State *L); 18.104 +LUAI_FUNC void luaC_freeall (lua_State *L); 18.105 +LUAI_FUNC void luaC_step (lua_State *L); 18.106 +LUAI_FUNC void luaC_fullgc (lua_State *L); 18.107 +LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); 18.108 +LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); 18.109 +LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); 18.110 +LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); 18.111 + 18.112 + 18.113 +#endif
19.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 19.2 +++ b/src/lua/linit.c Sat Mar 03 11:07:39 2012 -0600 19.3 @@ -0,0 +1,38 @@ 19.4 +/* 19.5 +** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $ 19.6 +** Initialization of libraries for lua.c 19.7 +** See Copyright Notice in lua.h 19.8 +*/ 19.9 + 19.10 + 19.11 +#define linit_c 19.12 +#define LUA_LIB 19.13 + 19.14 +#include "lua.h" 19.15 + 19.16 +#include "lualib.h" 19.17 +#include "lauxlib.h" 19.18 + 19.19 + 19.20 +static const luaL_Reg lualibs[] = { 19.21 + {"", luaopen_base}, 19.22 + {LUA_LOADLIBNAME, luaopen_package}, 19.23 + {LUA_TABLIBNAME, luaopen_table}, 19.24 + {LUA_IOLIBNAME, luaopen_io}, 19.25 + {LUA_OSLIBNAME, luaopen_os}, 19.26 + {LUA_STRLIBNAME, luaopen_string}, 19.27 + {LUA_MATHLIBNAME, luaopen_math}, 19.28 + {LUA_DBLIBNAME, luaopen_debug}, 19.29 + {NULL, NULL} 19.30 +}; 19.31 + 19.32 + 19.33 +LUALIB_API void luaL_openlibs (lua_State *L) { 19.34 + const luaL_Reg *lib = lualibs; 19.35 + for (; lib->func; lib++) { 19.36 + lua_pushcfunction(L, lib->func); 19.37 + lua_pushstring(L, lib->name); 19.38 + lua_call(L, 1, 0); 19.39 + } 19.40 +} 19.41 +
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 20.2 +++ b/src/lua/liolib.c Sat Mar 03 11:07:39 2012 -0600 20.3 @@ -0,0 +1,553 @@ 20.4 +/* 20.5 +** $Id: liolib.c,v 2.73.1.3 2008/01/18 17:47:43 roberto Exp $ 20.6 +** Standard I/O (and system) library 20.7 +** See Copyright Notice in lua.h 20.8 +*/ 20.9 + 20.10 + 20.11 +#include <errno.h> 20.12 +#include <stdio.h> 20.13 +#include <stdlib.h> 20.14 +#include <string.h> 20.15 + 20.16 +#define liolib_c 20.17 +#define LUA_LIB 20.18 + 20.19 +#include "lua.h" 20.20 + 20.21 +#include "lauxlib.h" 20.22 +#include "lualib.h" 20.23 + 20.24 + 20.25 + 20.26 +#define IO_INPUT 1 20.27 +#define IO_OUTPUT 2 20.28 + 20.29 + 20.30 +static const char *const fnames[] = {"input", "output"}; 20.31 + 20.32 + 20.33 +static int pushresult (lua_State *L, int i, const char *filename) { 20.34 + int en = errno; /* calls to Lua API may change this value */ 20.35 + if (i) { 20.36 + lua_pushboolean(L, 1); 20.37 + return 1; 20.38 + } 20.39 + else { 20.40 + lua_pushnil(L); 20.41 + if (filename) 20.42 + lua_pushfstring(L, "%s: %s", filename, strerror(en)); 20.43 + else 20.44 + lua_pushfstring(L, "%s", strerror(en)); 20.45 + lua_pushinteger(L, en); 20.46 + return 3; 20.47 + } 20.48 +} 20.49 + 20.50 + 20.51 +static void fileerror (lua_State *L, int arg, const char *filename) { 20.52 + lua_pushfstring(L, "%s: %s", filename, strerror(errno)); 20.53 + luaL_argerror(L, arg, lua_tostring(L, -1)); 20.54 +} 20.55 + 20.56 + 20.57 +#define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE)) 20.58 + 20.59 + 20.60 +static int io_type (lua_State *L) { 20.61 + void *ud; 20.62 + luaL_checkany(L, 1); 20.63 + ud = lua_touserdata(L, 1); 20.64 + lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); 20.65 + if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1)) 20.66 + lua_pushnil(L); /* not a file */ 20.67 + else if (*((FILE **)ud) == NULL) 20.68 + lua_pushliteral(L, "closed file"); 20.69 + else 20.70 + lua_pushliteral(L, "file"); 20.71 + return 1; 20.72 +} 20.73 + 20.74 + 20.75 +static FILE *tofile (lua_State *L) { 20.76 + FILE **f = tofilep(L); 20.77 + if (*f == NULL) 20.78 + luaL_error(L, "attempt to use a closed file"); 20.79 + return *f; 20.80 +} 20.81 + 20.82 + 20.83 + 20.84 +/* 20.85 +** When creating file handles, always creates a `closed' file handle 20.86 +** before opening the actual file; so, if there is a memory error, the 20.87 +** file is not left opened. 20.88 +*/ 20.89 +static FILE **newfile (lua_State *L) { 20.90 + FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *)); 20.91 + *pf = NULL; /* file handle is currently `closed' */ 20.92 + luaL_getmetatable(L, LUA_FILEHANDLE); 20.93 + lua_setmetatable(L, -2); 20.94 + return pf; 20.95 +} 20.96 + 20.97 + 20.98 +/* 20.99 +** function to (not) close the standard files stdin, stdout, and stderr 20.100 +*/ 20.101 +static int io_noclose (lua_State *L) { 20.102 + lua_pushnil(L); 20.103 + lua_pushliteral(L, "cannot close standard file"); 20.104 + return 2; 20.105 +} 20.106 + 20.107 + 20.108 +/* 20.109 +** function to close 'popen' files 20.110 +*/ 20.111 +static int io_pclose (lua_State *L) { 20.112 + FILE **p = tofilep(L); 20.113 + int ok = lua_pclose(L, *p); 20.114 + *p = NULL; 20.115 + return pushresult(L, ok, NULL); 20.116 +} 20.117 + 20.118 + 20.119 +/* 20.120 +** function to close regular files 20.121 +*/ 20.122 +static int io_fclose (lua_State *L) { 20.123 + FILE **p = tofilep(L); 20.124 + int ok = (fclose(*p) == 0); 20.125 + *p = NULL; 20.126 + return pushresult(L, ok, NULL); 20.127 +} 20.128 + 20.129 + 20.130 +static int aux_close (lua_State *L) { 20.131 + lua_getfenv(L, 1); 20.132 + lua_getfield(L, -1, "__close"); 20.133 + return (lua_tocfunction(L, -1))(L); 20.134 +} 20.135 + 20.136 + 20.137 +static int io_close (lua_State *L) { 20.138 + if (lua_isnone(L, 1)) 20.139 + lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT); 20.140 + tofile(L); /* make sure argument is a file */ 20.141 + return aux_close(L); 20.142 +} 20.143 + 20.144 + 20.145 +static int io_gc (lua_State *L) { 20.146 + FILE *f = *tofilep(L); 20.147 + /* ignore closed files */ 20.148 + if (f != NULL) 20.149 + aux_close(L); 20.150 + return 0; 20.151 +} 20.152 + 20.153 + 20.154 +static int io_tostring (lua_State *L) { 20.155 + FILE *f = *tofilep(L); 20.156 + if (f == NULL) 20.157 + lua_pushliteral(L, "file (closed)"); 20.158 + else 20.159 + lua_pushfstring(L, "file (%p)", f); 20.160 + return 1; 20.161 +} 20.162 + 20.163 + 20.164 +static int io_open (lua_State *L) { 20.165 + const char *filename = luaL_checkstring(L, 1); 20.166 + const char *mode = luaL_optstring(L, 2, "r"); 20.167 + FILE **pf = newfile(L); 20.168 + *pf = fopen(filename, mode); 20.169 + return (*pf == NULL) ? pushresult(L, 0, filename) : 1; 20.170 +} 20.171 + 20.172 + 20.173 +/* 20.174 +** this function has a separated environment, which defines the 20.175 +** correct __close for 'popen' files 20.176 +*/ 20.177 +static int io_popen (lua_State *L) { 20.178 + const char *filename = luaL_checkstring(L, 1); 20.179 + const char *mode = luaL_optstring(L, 2, "r"); 20.180 + FILE **pf = newfile(L); 20.181 + *pf = lua_popen(L, filename, mode); 20.182 + return (*pf == NULL) ? pushresult(L, 0, filename) : 1; 20.183 +} 20.184 + 20.185 + 20.186 +static int io_tmpfile (lua_State *L) { 20.187 + FILE **pf = newfile(L); 20.188 + *pf = tmpfile(); 20.189 + return (*pf == NULL) ? pushresult(L, 0, NULL) : 1; 20.190 +} 20.191 + 20.192 + 20.193 +static FILE *getiofile (lua_State *L, int findex) { 20.194 + FILE *f; 20.195 + lua_rawgeti(L, LUA_ENVIRONINDEX, findex); 20.196 + f = *(FILE **)lua_touserdata(L, -1); 20.197 + if (f == NULL) 20.198 + luaL_error(L, "standard %s file is closed", fnames[findex - 1]); 20.199 + return f; 20.200 +} 20.201 + 20.202 + 20.203 +static int g_iofile (lua_State *L, int f, const char *mode) { 20.204 + if (!lua_isnoneornil(L, 1)) { 20.205 + const char *filename = lua_tostring(L, 1); 20.206 + if (filename) { 20.207 + FILE **pf = newfile(L); 20.208 + *pf = fopen(filename, mode); 20.209 + if (*pf == NULL) 20.210 + fileerror(L, 1, filename); 20.211 + } 20.212 + else { 20.213 + tofile(L); /* check that it's a valid file handle */ 20.214 + lua_pushvalue(L, 1); 20.215 + } 20.216 + lua_rawseti(L, LUA_ENVIRONINDEX, f); 20.217 + } 20.218 + /* return current value */ 20.219 + lua_rawgeti(L, LUA_ENVIRONINDEX, f); 20.220 + return 1; 20.221 +} 20.222 + 20.223 + 20.224 +static int io_input (lua_State *L) { 20.225 + return g_iofile(L, IO_INPUT, "r"); 20.226 +} 20.227 + 20.228 + 20.229 +static int io_output (lua_State *L) { 20.230 + return g_iofile(L, IO_OUTPUT, "w"); 20.231 +} 20.232 + 20.233 + 20.234 +static int io_readline (lua_State *L); 20.235 + 20.236 + 20.237 +static void aux_lines (lua_State *L, int idx, int toclose) { 20.238 + lua_pushvalue(L, idx); 20.239 + lua_pushboolean(L, toclose); /* close/not close file when finished */ 20.240 + lua_pushcclosure(L, io_readline, 2); 20.241 +} 20.242 + 20.243 + 20.244 +static int f_lines (lua_State *L) { 20.245 + tofile(L); /* check that it's a valid file handle */ 20.246 + aux_lines(L, 1, 0); 20.247 + return 1; 20.248 +} 20.249 + 20.250 + 20.251 +static int io_lines (lua_State *L) { 20.252 + if (lua_isnoneornil(L, 1)) { /* no arguments? */ 20.253 + /* will iterate over default input */ 20.254 + lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT); 20.255 + return f_lines(L); 20.256 + } 20.257 + else { 20.258 + const char *filename = luaL_checkstring(L, 1); 20.259 + FILE **pf = newfile(L); 20.260 + *pf = fopen(filename, "r"); 20.261 + if (*pf == NULL) 20.262 + fileerror(L, 1, filename); 20.263 + aux_lines(L, lua_gettop(L), 1); 20.264 + return 1; 20.265 + } 20.266 +} 20.267 + 20.268 + 20.269 +/* 20.270 +** {====================================================== 20.271 +** READ 20.272 +** ======================================================= 20.273 +*/ 20.274 + 20.275 + 20.276 +static int read_number (lua_State *L, FILE *f) { 20.277 + lua_Number d; 20.278 + if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { 20.279 + lua_pushnumber(L, d); 20.280 + return 1; 20.281 + } 20.282 + else return 0; /* read fails */ 20.283 +} 20.284 + 20.285 + 20.286 +static int test_eof (lua_State *L, FILE *f) { 20.287 + int c = getc(f); 20.288 + ungetc(c, f); 20.289 + lua_pushlstring(L, NULL, 0); 20.290 + return (c != EOF); 20.291 +} 20.292 + 20.293 + 20.294 +static int read_line (lua_State *L, FILE *f) { 20.295 + luaL_Buffer b; 20.296 + luaL_buffinit(L, &b); 20.297 + for (;;) { 20.298 + size_t l; 20.299 + char *p = luaL_prepbuffer(&b); 20.300 + if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */ 20.301 + luaL_pushresult(&b); /* close buffer */ 20.302 + return (lua_objlen(L, -1) > 0); /* check whether read something */ 20.303 + } 20.304 + l = strlen(p); 20.305 + if (l == 0 || p[l-1] != '\n') 20.306 + luaL_addsize(&b, l); 20.307 + else { 20.308 + luaL_addsize(&b, l - 1); /* do not include `eol' */ 20.309 + luaL_pushresult(&b); /* close buffer */ 20.310 + return 1; /* read at least an `eol' */ 20.311 + } 20.312 + } 20.313 +} 20.314 + 20.315 + 20.316 +static int read_chars (lua_State *L, FILE *f, size_t n) { 20.317 + size_t rlen; /* how much to read */ 20.318 + size_t nr; /* number of chars actually read */ 20.319 + luaL_Buffer b; 20.320 + luaL_buffinit(L, &b); 20.321 + rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ 20.322 + do { 20.323 + char *p = luaL_prepbuffer(&b); 20.324 + if (rlen > n) rlen = n; /* cannot read more than asked */ 20.325 + nr = fread(p, sizeof(char), rlen, f); 20.326 + luaL_addsize(&b, nr); 20.327 + n -= nr; /* still have to read `n' chars */ 20.328 + } while (n > 0 && nr == rlen); /* until end of count or eof */ 20.329 + luaL_pushresult(&b); /* close buffer */ 20.330 + return (n == 0 || lua_objlen(L, -1) > 0); 20.331 +} 20.332 + 20.333 + 20.334 +static int g_read (lua_State *L, FILE *f, int first) { 20.335 + int nargs = lua_gettop(L) - 1; 20.336 + int success; 20.337 + int n; 20.338 + clearerr(f); 20.339 + if (nargs == 0) { /* no arguments? */ 20.340 + success = read_line(L, f); 20.341 + n = first+1; /* to return 1 result */ 20.342 + } 20.343 + else { /* ensure stack space for all results and for auxlib's buffer */ 20.344 + luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); 20.345 + success = 1; 20.346 + for (n = first; nargs-- && success; n++) { 20.347 + if (lua_type(L, n) == LUA_TNUMBER) { 20.348 + size_t l = (size_t)lua_tointeger(L, n); 20.349 + success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); 20.350 + } 20.351 + else { 20.352 + const char *p = lua_tostring(L, n); 20.353 + luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); 20.354 + switch (p[1]) { 20.355 + case 'n': /* number */ 20.356 + success = read_number(L, f); 20.357 + break; 20.358 + case 'l': /* line */ 20.359 + success = read_line(L, f); 20.360 + break; 20.361 + case 'a': /* file */ 20.362 + read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */ 20.363 + success = 1; /* always success */ 20.364 + break; 20.365 + default: 20.366 + return luaL_argerror(L, n, "invalid format"); 20.367 + } 20.368 + } 20.369 + } 20.370 + } 20.371 + if (ferror(f)) 20.372 + return pushresult(L, 0, NULL); 20.373 + if (!success) { 20.374 + lua_pop(L, 1); /* remove last result */ 20.375 + lua_pushnil(L); /* push nil instead */ 20.376 + } 20.377 + return n - first; 20.378 +} 20.379 + 20.380 + 20.381 +static int io_read (lua_State *L) { 20.382 + return g_read(L, getiofile(L, IO_INPUT), 1); 20.383 +} 20.384 + 20.385 + 20.386 +static int f_read (lua_State *L) { 20.387 + return g_read(L, tofile(L), 2); 20.388 +} 20.389 + 20.390 + 20.391 +static int io_readline (lua_State *L) { 20.392 + FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1)); 20.393 + int sucess; 20.394 + if (f == NULL) /* file is already closed? */ 20.395 + luaL_error(L, "file is already closed"); 20.396 + sucess = read_line(L, f); 20.397 + if (ferror(f)) 20.398 + return luaL_error(L, "%s", strerror(errno)); 20.399 + if (sucess) return 1; 20.400 + else { /* EOF */ 20.401 + if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */ 20.402 + lua_settop(L, 0); 20.403 + lua_pushvalue(L, lua_upvalueindex(1)); 20.404 + aux_close(L); /* close it */ 20.405 + } 20.406 + return 0; 20.407 + } 20.408 +} 20.409 + 20.410 +/* }====================================================== */ 20.411 + 20.412 + 20.413 +static int g_write (lua_State *L, FILE *f, int arg) { 20.414 + int nargs = lua_gettop(L) - 1; 20.415 + int status = 1; 20.416 + for (; nargs--; arg++) { 20.417 + if (lua_type(L, arg) == LUA_TNUMBER) { 20.418 + /* optimization: could be done exactly as for strings */ 20.419 + status = status && 20.420 + fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; 20.421 + } 20.422 + else { 20.423 + size_t l; 20.424 + const char *s = luaL_checklstring(L, arg, &l); 20.425 + status = status && (fwrite(s, sizeof(char), l, f) == l); 20.426 + } 20.427 + } 20.428 + return pushresult(L, status, NULL); 20.429 +} 20.430 + 20.431 + 20.432 +static int io_write (lua_State *L) { 20.433 + return g_write(L, getiofile(L, IO_OUTPUT), 1); 20.434 +} 20.435 + 20.436 + 20.437 +static int f_write (lua_State *L) { 20.438 + return g_write(L, tofile(L), 2); 20.439 +} 20.440 + 20.441 + 20.442 +static int f_seek (lua_State *L) { 20.443 + static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; 20.444 + static const char *const modenames[] = {"set", "cur", "end", NULL}; 20.445 + FILE *f = tofile(L); 20.446 + int op = luaL_checkoption(L, 2, "cur", modenames); 20.447 + long offset = luaL_optlong(L, 3, 0); 20.448 + op = fseek(f, offset, mode[op]); 20.449 + if (op) 20.450 + return pushresult(L, 0, NULL); /* error */ 20.451 + else { 20.452 + lua_pushinteger(L, ftell(f)); 20.453 + return 1; 20.454 + } 20.455 +} 20.456 + 20.457 + 20.458 +static int f_setvbuf (lua_State *L) { 20.459 + static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; 20.460 + static const char *const modenames[] = {"no", "full", "line", NULL}; 20.461 + FILE *f = tofile(L); 20.462 + int op = luaL_checkoption(L, 2, NULL, modenames); 20.463 + lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); 20.464 + int res = setvbuf(f, NULL, mode[op], sz); 20.465 + return pushresult(L, res == 0, NULL); 20.466 +} 20.467 + 20.468 + 20.469 + 20.470 +static int io_flush (lua_State *L) { 20.471 + return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); 20.472 +} 20.473 + 20.474 + 20.475 +static int f_flush (lua_State *L) { 20.476 + return pushresult(L, fflush(tofile(L)) == 0, NULL); 20.477 +} 20.478 + 20.479 + 20.480 +static const luaL_Reg iolib[] = { 20.481 + {"close", io_close}, 20.482 + {"flush", io_flush}, 20.483 + {"input", io_input}, 20.484 + {"lines", io_lines}, 20.485 + {"open", io_open}, 20.486 + {"output", io_output}, 20.487 + {"popen", io_popen}, 20.488 + {"read", io_read}, 20.489 + {"tmpfile", io_tmpfile}, 20.490 + {"type", io_type}, 20.491 + {"write", io_write}, 20.492 + {NULL, NULL} 20.493 +}; 20.494 + 20.495 + 20.496 +static const luaL_Reg flib[] = { 20.497 + {"close", io_close}, 20.498 + {"flush", f_flush}, 20.499 + {"lines", f_lines}, 20.500 + {"read", f_read}, 20.501 + {"seek", f_seek}, 20.502 + {"setvbuf", f_setvbuf}, 20.503 + {"write", f_write}, 20.504 + {"__gc", io_gc}, 20.505 + {"__tostring", io_tostring}, 20.506 + {NULL, NULL} 20.507 +}; 20.508 + 20.509 + 20.510 +static void createmeta (lua_State *L) { 20.511 + luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ 20.512 + lua_pushvalue(L, -1); /* push metatable */ 20.513 + lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ 20.514 + luaL_register(L, NULL, flib); /* file methods */ 20.515 +} 20.516 + 20.517 + 20.518 +static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) { 20.519 + *newfile(L) = f; 20.520 + if (k > 0) { 20.521 + lua_pushvalue(L, -1); 20.522 + lua_rawseti(L, LUA_ENVIRONINDEX, k); 20.523 + } 20.524 + lua_pushvalue(L, -2); /* copy environment */ 20.525 + lua_setfenv(L, -2); /* set it */ 20.526 + lua_setfield(L, -3, fname); 20.527 +} 20.528 + 20.529 + 20.530 +static void newfenv (lua_State *L, lua_CFunction cls) { 20.531 + lua_createtable(L, 0, 1); 20.532 + lua_pushcfunction(L, cls); 20.533 + lua_setfield(L, -2, "__close"); 20.534 +} 20.535 + 20.536 + 20.537 +LUALIB_API int luaopen_io (lua_State *L) { 20.538 + createmeta(L); 20.539 + /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */ 20.540 + newfenv(L, io_fclose); 20.541 + lua_replace(L, LUA_ENVIRONINDEX); 20.542 + /* open library */ 20.543 + luaL_register(L, LUA_IOLIBNAME, iolib); 20.544 + /* create (and set) default files */ 20.545 + newfenv(L, io_noclose); /* close function for default files */ 20.546 + createstdfile(L, stdin, IO_INPUT, "stdin"); 20.547 + createstdfile(L, stdout, IO_OUTPUT, "stdout"); 20.548 + createstdfile(L, stderr, 0, "stderr"); 20.549 + lua_pop(L, 1); /* pop environment for default files */ 20.550 + lua_getfield(L, -1, "popen"); 20.551 + newfenv(L, io_pclose); /* create environment for 'popen' */ 20.552 + lua_setfenv(L, -2); /* set fenv for 'popen' */ 20.553 + lua_pop(L, 1); /* pop 'popen' */ 20.554 + return 1; 20.555 +} 20.556 +
21.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 21.2 +++ b/src/lua/llex.c Sat Mar 03 11:07:39 2012 -0600 21.3 @@ -0,0 +1,461 @@ 21.4 +/* 21.5 +** $Id: llex.c,v 2.20.1.1 2007/12/27 13:02:25 roberto Exp $ 21.6 +** Lexical Analyzer 21.7 +** See Copyright Notice in lua.h 21.8 +*/ 21.9 + 21.10 + 21.11 +#include <ctype.h> 21.12 +#include <locale.h> 21.13 +#include <string.h> 21.14 + 21.15 +#define llex_c 21.16 +#define LUA_CORE 21.17 + 21.18 +#include "lua.h" 21.19 + 21.20 +#include "ldo.h" 21.21 +#include "llex.h" 21.22 +#include "lobject.h" 21.23 +#include "lparser.h" 21.24 +#include "lstate.h" 21.25 +#include "lstring.h" 21.26 +#include "ltable.h" 21.27 +#include "lzio.h" 21.28 + 21.29 + 21.30 + 21.31 +#define next(ls) (ls->current = zgetc(ls->z)) 21.32 + 21.33 + 21.34 + 21.35 + 21.36 +#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') 21.37 + 21.38 + 21.39 +/* ORDER RESERVED */ 21.40 +const char *const luaX_tokens [] = { 21.41 + "and", "break", "do", "else", "elseif", 21.42 + "end", "false", "for", "function", "if", 21.43 + "in", "local", "nil", "not", "or", "repeat", 21.44 + "return", "then", "true", "until", "while", 21.45 + "..", "...", "==", ">=", "<=", "~=", 21.46 + "<number>", "<name>", "<string>", "<eof>", 21.47 + NULL 21.48 +}; 21.49 + 21.50 + 21.51 +#define save_and_next(ls) (save(ls, ls->current), next(ls)) 21.52 + 21.53 + 21.54 +static void save (LexState *ls, int c) { 21.55 + Mbuffer *b = ls->buff; 21.56 + if (b->n + 1 > b->buffsize) { 21.57 + size_t newsize; 21.58 + if (b->buffsize >= MAX_SIZET/2) 21.59 + luaX_lexerror(ls, "lexical element too long", 0); 21.60 + newsize = b->buffsize * 2; 21.61 + luaZ_resizebuffer(ls->L, b, newsize); 21.62 + } 21.63 + b->buffer[b->n++] = cast(char, c); 21.64 +} 21.65 + 21.66 + 21.67 +void luaX_init (lua_State *L) { 21.68 + int i; 21.69 + for (i=0; i<NUM_RESERVED; i++) { 21.70 + TString *ts = luaS_new(L, luaX_tokens[i]); 21.71 + luaS_fix(ts); /* reserved words are never collected */ 21.72 + lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN); 21.73 + ts->tsv.reserved = cast_byte(i+1); /* reserved word */ 21.74 + } 21.75 +} 21.76 + 21.77 + 21.78 +#define MAXSRC 80 21.79 + 21.80 + 21.81 +const char *luaX_token2str (LexState *ls, int token) { 21.82 + if (token < FIRST_RESERVED) { 21.83 + lua_assert(token == cast(unsigned char, token)); 21.84 + return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) : 21.85 + luaO_pushfstring(ls->L, "%c", token); 21.86 + } 21.87 + else 21.88 + return luaX_tokens[token-FIRST_RESERVED]; 21.89 +} 21.90 + 21.91 + 21.92 +static const char *txtToken (LexState *ls, int token) { 21.93 + switch (token) { 21.94 + case TK_NAME: 21.95 + case TK_STRING: 21.96 + case TK_NUMBER: 21.97 + save(ls, '\0'); 21.98 + return luaZ_buffer(ls->buff); 21.99 + default: 21.100 + return luaX_token2str(ls, token); 21.101 + } 21.102 +} 21.103 + 21.104 + 21.105 +void luaX_lexerror (LexState *ls, const char *msg, int token) { 21.106 + char buff[MAXSRC]; 21.107 + luaO_chunkid(buff, getstr(ls->source), MAXSRC); 21.108 + msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg); 21.109 + if (token) 21.110 + luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token)); 21.111 + luaD_throw(ls->L, LUA_ERRSYNTAX); 21.112 +} 21.113 + 21.114 + 21.115 +void luaX_syntaxerror (LexState *ls, const char *msg) { 21.116 + luaX_lexerror(ls, msg, ls->t.token); 21.117 +} 21.118 + 21.119 + 21.120 +TString *luaX_newstring (LexState *ls, const char *str, size_t l) { 21.121 + lua_State *L = ls->L; 21.122 + TString *ts = luaS_newlstr(L, str, l); 21.123 + TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */ 21.124 + if (ttisnil(o)) 21.125 + setbvalue(o, 1); /* make sure `str' will not be collected */ 21.126 + return ts; 21.127 +} 21.128 + 21.129 + 21.130 +static void inclinenumber (LexState *ls) { 21.131 + int old = ls->current; 21.132 + lua_assert(currIsNewline(ls)); 21.133 + next(ls); /* skip `\n' or `\r' */ 21.134 + if (currIsNewline(ls) && ls->current != old) 21.135 + next(ls); /* skip `\n\r' or `\r\n' */ 21.136 + if (++ls->linenumber >= MAX_INT) 21.137 + luaX_syntaxerror(ls, "chunk has too many lines"); 21.138 +} 21.139 + 21.140 + 21.141 +void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) { 21.142 + ls->decpoint = '.'; 21.143 + ls->L = L; 21.144 + ls->lookahead.token = TK_EOS; /* no look-ahead token */ 21.145 + ls->z = z; 21.146 + ls->fs = NULL; 21.147 + ls->linenumber = 1; 21.148 + ls->lastline = 1; 21.149 + ls->source = source; 21.150 + luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ 21.151 + next(ls); /* read first char */ 21.152 +} 21.153 + 21.154 + 21.155 + 21.156 +/* 21.157 +** ======================================================= 21.158 +** LEXICAL ANALYZER 21.159 +** ======================================================= 21.160 +*/ 21.161 + 21.162 + 21.163 + 21.164 +static int check_next (LexState *ls, const char *set) { 21.165 + if (!strchr(set, ls->current)) 21.166 + return 0; 21.167 + save_and_next(ls); 21.168 + return 1; 21.169 +} 21.170 + 21.171 + 21.172 +static void buffreplace (LexState *ls, char from, char to) { 21.173 + size_t n = luaZ_bufflen(ls->buff); 21.174 + char *p = luaZ_buffer(ls->buff); 21.175 + while (n--) 21.176 + if (p[n] == from) p[n] = to; 21.177 +} 21.178 + 21.179 + 21.180 +static void trydecpoint (LexState *ls, SemInfo *seminfo) { 21.181 + /* format error: try to update decimal point separator */ 21.182 + struct lconv *cv = localeconv(); 21.183 + char old = ls->decpoint; 21.184 + ls->decpoint = (cv ? cv->decimal_point[0] : '.'); 21.185 + buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */ 21.186 + if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) { 21.187 + /* format error with correct decimal point: no more options */ 21.188 + buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ 21.189 + luaX_lexerror(ls, "malformed number", TK_NUMBER); 21.190 + } 21.191 +} 21.192 + 21.193 + 21.194 +/* LUA_NUMBER */ 21.195 +static void read_numeral (LexState *ls, SemInfo *seminfo) { 21.196 + lua_assert(isdigit(ls->current)); 21.197 + do { 21.198 + save_and_next(ls); 21.199 + } while (isdigit(ls->current) || ls->current == '.'); 21.200 + if (check_next(ls, "Ee")) /* `E'? */ 21.201 + check_next(ls, "+-"); /* optional exponent sign */ 21.202 + while (isalnum(ls->current) || ls->current == '_') 21.203 + save_and_next(ls); 21.204 + save(ls, '\0'); 21.205 + buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ 21.206 + if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */ 21.207 + trydecpoint(ls, seminfo); /* try to update decimal point separator */ 21.208 +} 21.209 + 21.210 + 21.211 +static int skip_sep (LexState *ls) { 21.212 + int count = 0; 21.213 + int s = ls->current; 21.214 + lua_assert(s == '[' || s == ']'); 21.215 + save_and_next(ls); 21.216 + while (ls->current == '=') { 21.217 + save_and_next(ls); 21.218 + count++; 21.219 + } 21.220 + return (ls->current == s) ? count : (-count) - 1; 21.221 +} 21.222 + 21.223 + 21.224 +static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { 21.225 + int cont = 0; 21.226 + (void)(cont); /* avoid warnings when `cont' is not used */ 21.227 + save_and_next(ls); /* skip 2nd `[' */ 21.228 + if (currIsNewline(ls)) /* string starts with a newline? */ 21.229 + inclinenumber(ls); /* skip it */ 21.230 + for (;;) { 21.231 + switch (ls->current) { 21.232 + case EOZ: 21.233 + luaX_lexerror(ls, (seminfo) ? "unfinished long string" : 21.234 + "unfinished long comment", TK_EOS); 21.235 + break; /* to avoid warnings */ 21.236 +#if defined(LUA_COMPAT_LSTR) 21.237 + case '[': { 21.238 + if (skip_sep(ls) == sep) { 21.239 + save_and_next(ls); /* skip 2nd `[' */ 21.240 + cont++; 21.241 +#if LUA_COMPAT_LSTR == 1 21.242 + if (sep == 0) 21.243 + luaX_lexerror(ls, "nesting of [[...]] is deprecated", '['); 21.244 +#endif 21.245 + } 21.246 + break; 21.247 + } 21.248 +#endif 21.249 + case ']': { 21.250 + if (skip_sep(ls) == sep) { 21.251 + save_and_next(ls); /* skip 2nd `]' */ 21.252 +#if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2 21.253 + cont--; 21.254 + if (sep == 0 && cont >= 0) break; 21.255 +#endif 21.256 + goto endloop; 21.257 + } 21.258 + break; 21.259 + } 21.260 + case '\n': 21.261 + case '\r': { 21.262 + save(ls, '\n'); 21.263 + inclinenumber(ls); 21.264 + if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ 21.265 + break; 21.266 + } 21.267 + default: { 21.268 + if (seminfo) save_and_next(ls); 21.269 + else next(ls); 21.270 + } 21.271 + } 21.272 + } endloop: 21.273 + if (seminfo) 21.274 + seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep), 21.275 + luaZ_bufflen(ls->buff) - 2*(2 + sep)); 21.276 +} 21.277 + 21.278 + 21.279 +static void read_string (LexState *ls, int del, SemInfo *seminfo) { 21.280 + save_and_next(ls); 21.281 + while (ls->current != del) { 21.282 + switch (ls->current) { 21.283 + case EOZ: 21.284 + luaX_lexerror(ls, "unfinished string", TK_EOS); 21.285 + continue; /* to avoid warnings */ 21.286 + case '\n': 21.287 + case '\r': 21.288 + luaX_lexerror(ls, "unfinished string", TK_STRING); 21.289 + continue; /* to avoid warnings */ 21.290 + case '\\': { 21.291 + int c; 21.292 + next(ls); /* do not save the `\' */ 21.293 + switch (ls->current) { 21.294 + case 'a': c = '\a'; break; 21.295 + case 'b': c = '\b'; break; 21.296 + case 'f': c = '\f'; break; 21.297 + case 'n': c = '\n'; break; 21.298 + case 'r': c = '\r'; break; 21.299 + case 't': c = '\t'; break; 21.300 + case 'v': c = '\v'; break; 21.301 + case '\n': /* go through */ 21.302 + case '\r': save(ls, '\n'); inclinenumber(ls); continue; 21.303 + case EOZ: continue; /* will raise an error next loop */ 21.304 + default: { 21.305 + if (!isdigit(ls->current)) 21.306 + save_and_next(ls); /* handles \\, \", \', and \? */ 21.307 + else { /* \xxx */ 21.308 + int i = 0; 21.309 + c = 0; 21.310 + do { 21.311 + c = 10*c + (ls->current-'0'); 21.312 + next(ls); 21.313 + } while (++i<3 && isdigit(ls->current)); 21.314 + if (c > UCHAR_MAX) 21.315 + luaX_lexerror(ls, "escape sequence too large", TK_STRING); 21.316 + save(ls, c); 21.317 + } 21.318 + continue; 21.319 + } 21.320 + } 21.321 + save(ls, c); 21.322 + next(ls); 21.323 + continue; 21.324 + } 21.325 + default: 21.326 + save_and_next(ls); 21.327 + } 21.328 + } 21.329 + save_and_next(ls); /* skip delimiter */ 21.330 + seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, 21.331 + luaZ_bufflen(ls->buff) - 2); 21.332 +} 21.333 + 21.334 + 21.335 +static int llex (LexState *ls, SemInfo *seminfo) { 21.336 + luaZ_resetbuffer(ls->buff); 21.337 + for (;;) { 21.338 + switch (ls->current) { 21.339 + case '\n': 21.340 + case '\r': { 21.341 + inclinenumber(ls); 21.342 + continue; 21.343 + } 21.344 + case '-': { 21.345 + next(ls); 21.346 + if (ls->current != '-') return '-'; 21.347 + /* else is a comment */ 21.348 + next(ls); 21.349 + if (ls->current == '[') { 21.350 + int sep = skip_sep(ls); 21.351 + luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */ 21.352 + if (sep >= 0) { 21.353 + read_long_string(ls, NULL, sep); /* long comment */ 21.354 + luaZ_resetbuffer(ls->buff); 21.355 + continue; 21.356 + } 21.357 + } 21.358 + /* else short comment */ 21.359 + while (!currIsNewline(ls) && ls->current != EOZ) 21.360 + next(ls); 21.361 + continue; 21.362 + } 21.363 + case '[': { 21.364 + int sep = skip_sep(ls); 21.365 + if (sep >= 0) { 21.366 + read_long_string(ls, seminfo, sep); 21.367 + return TK_STRING; 21.368 + } 21.369 + else if (sep == -1) return '['; 21.370 + else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING); 21.371 + } 21.372 + case '=': { 21.373 + next(ls); 21.374 + if (ls->current != '=') return '='; 21.375 + else { next(ls); return TK_EQ; } 21.376 + } 21.377 + case '<': { 21.378 + next(ls); 21.379 + if (ls->current != '=') return '<'; 21.380 + else { next(ls); return TK_LE; } 21.381 + } 21.382 + case '>': { 21.383 + next(ls); 21.384 + if (ls->current != '=') return '>'; 21.385 + else { next(ls); return TK_GE; } 21.386 + } 21.387 + case '~': { 21.388 + next(ls); 21.389 + if (ls->current != '=') return '~'; 21.390 + else { next(ls); return TK_NE; } 21.391 + } 21.392 + case '"': 21.393 + case '\'': { 21.394 + read_string(ls, ls->current, seminfo); 21.395 + return TK_STRING; 21.396 + } 21.397 + case '.': { 21.398 + save_and_next(ls); 21.399 + if (check_next(ls, ".")) { 21.400 + if (check_next(ls, ".")) 21.401 + return TK_DOTS; /* ... */ 21.402 + else return TK_CONCAT; /* .. */ 21.403 + } 21.404 + else if (!isdigit(ls->current)) return '.'; 21.405 + else { 21.406 + read_numeral(ls, seminfo); 21.407 + return TK_NUMBER; 21.408 + } 21.409 + } 21.410 + case EOZ: { 21.411 + return TK_EOS; 21.412 + } 21.413 + default: { 21.414 + if (isspace(ls->current)) { 21.415 + lua_assert(!currIsNewline(ls)); 21.416 + next(ls); 21.417 + continue; 21.418 + } 21.419 + else if (isdigit(ls->current)) { 21.420 + read_numeral(ls, seminfo); 21.421 + return TK_NUMBER; 21.422 + } 21.423 + else if (isalpha(ls->current) || ls->current == '_') { 21.424 + /* identifier or reserved word */ 21.425 + TString *ts; 21.426 + do { 21.427 + save_and_next(ls); 21.428 + } while (isalnum(ls->current) || ls->current == '_'); 21.429 + ts = luaX_newstring(ls, luaZ_buffer(ls->buff), 21.430 + luaZ_bufflen(ls->buff)); 21.431 + if (ts->tsv.reserved > 0) /* reserved word? */ 21.432 + return ts->tsv.reserved - 1 + FIRST_RESERVED; 21.433 + else { 21.434 + seminfo->ts = ts; 21.435 + return TK_NAME; 21.436 + } 21.437 + } 21.438 + else { 21.439 + int c = ls->current; 21.440 + next(ls); 21.441 + return c; /* single-char tokens (+ - / ...) */ 21.442 + } 21.443 + } 21.444 + } 21.445 + } 21.446 +} 21.447 + 21.448 + 21.449 +void luaX_next (LexState *ls) { 21.450 + ls->lastline = ls->linenumber; 21.451 + if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ 21.452 + ls->t = ls->lookahead; /* use this one */ 21.453 + ls->lookahead.token = TK_EOS; /* and discharge it */ 21.454 + } 21.455 + else 21.456 + ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */ 21.457 +} 21.458 + 21.459 + 21.460 +void luaX_lookahead (LexState *ls) { 21.461 + lua_assert(ls->lookahead.token == TK_EOS); 21.462 + ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); 21.463 +} 21.464 +
22.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 22.2 +++ b/src/lua/llex.h Sat Mar 03 11:07:39 2012 -0600 22.3 @@ -0,0 +1,81 @@ 22.4 +/* 22.5 +** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $ 22.6 +** Lexical Analyzer 22.7 +** See Copyright Notice in lua.h 22.8 +*/ 22.9 + 22.10 +#ifndef llex_h 22.11 +#define llex_h 22.12 + 22.13 +#include "lobject.h" 22.14 +#include "lzio.h" 22.15 + 22.16 + 22.17 +#define FIRST_RESERVED 257 22.18 + 22.19 +/* maximum length of a reserved word */ 22.20 +#define TOKEN_LEN (sizeof("function")/sizeof(char)) 22.21 + 22.22 + 22.23 +/* 22.24 +* WARNING: if you change the order of this enumeration, 22.25 +* grep "ORDER RESERVED" 22.26 +*/ 22.27 +enum RESERVED { 22.28 + /* terminal symbols denoted by reserved words */ 22.29 + TK_AND = FIRST_RESERVED, TK_BREAK, 22.30 + TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 22.31 + TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 22.32 + TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 22.33 + /* other terminal symbols */ 22.34 + TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER, 22.35 + TK_NAME, TK_STRING, TK_EOS 22.36 +}; 22.37 + 22.38 +/* number of reserved words */ 22.39 +#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 22.40 + 22.41 + 22.42 +/* array with token `names' */ 22.43 +LUAI_DATA const char *const luaX_tokens []; 22.44 + 22.45 + 22.46 +typedef union { 22.47 + lua_Number r; 22.48 + TString *ts; 22.49 +} SemInfo; /* semantics information */ 22.50 + 22.51 + 22.52 +typedef struct Token { 22.53 + int token; 22.54 + SemInfo seminfo; 22.55 +} Token; 22.56 + 22.57 + 22.58 +typedef struct LexState { 22.59 + int current; /* current character (charint) */ 22.60 + int linenumber; /* input line counter */ 22.61 + int lastline; /* line of last token `consumed' */ 22.62 + Token t; /* current token */ 22.63 + Token lookahead; /* look ahead token */ 22.64 + struct FuncState *fs; /* `FuncState' is private to the parser */ 22.65 + struct lua_State *L; 22.66 + ZIO *z; /* input stream */ 22.67 + Mbuffer *buff; /* buffer for tokens */ 22.68 + TString *source; /* current source name */ 22.69 + char decpoint; /* locale decimal point */ 22.70 +} LexState; 22.71 + 22.72 + 22.73 +LUAI_FUNC void luaX_init (lua_State *L); 22.74 +LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 22.75 + TString *source); 22.76 +LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 22.77 +LUAI_FUNC void luaX_next (LexState *ls); 22.78 +LUAI_FUNC void luaX_lookahead (LexState *ls); 22.79 +LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token); 22.80 +LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s); 22.81 +LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 22.82 + 22.83 + 22.84 +#endif
23.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 23.2 +++ b/src/lua/llimits.h Sat Mar 03 11:07:39 2012 -0600 23.3 @@ -0,0 +1,128 @@ 23.4 +/* 23.5 +** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $ 23.6 +** Limits, basic types, and some other `installation-dependent' definitions 23.7 +** See Copyright Notice in lua.h 23.8 +*/ 23.9 + 23.10 +#ifndef llimits_h 23.11 +#define llimits_h 23.12 + 23.13 + 23.14 +#include <limits.h> 23.15 +#include <stddef.h> 23.16 + 23.17 + 23.18 +#include "lua.h" 23.19 + 23.20 + 23.21 +typedef LUAI_UINT32 lu_int32; 23.22 + 23.23 +typedef LUAI_UMEM lu_mem; 23.24 + 23.25 +typedef LUAI_MEM l_mem; 23.26 + 23.27 + 23.28 + 23.29 +/* chars used as small naturals (so that `char' is reserved for characters) */ 23.30 +typedef unsigned char lu_byte; 23.31 + 23.32 + 23.33 +#define MAX_SIZET ((size_t)(~(size_t)0)-2) 23.34 + 23.35 +#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) 23.36 + 23.37 + 23.38 +#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 23.39 + 23.40 +/* 23.41 +** conversion of pointer to integer 23.42 +** this is for hashing only; there is no problem if the integer 23.43 +** cannot hold the whole pointer value 23.44 +*/ 23.45 +#define IntPoint(p) ((unsigned int)(lu_mem)(p)) 23.46 + 23.47 + 23.48 + 23.49 +/* type to ensure maximum alignment */ 23.50 +typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 23.51 + 23.52 + 23.53 +/* result of a `usual argument conversion' over lua_Number */ 23.54 +typedef LUAI_UACNUMBER l_uacNumber; 23.55 + 23.56 + 23.57 +/* internal assertions for in-house debugging */ 23.58 +#ifdef lua_assert 23.59 + 23.60 +#define check_exp(c,e) (lua_assert(c), (e)) 23.61 +#define api_check(l,e) lua_assert(e) 23.62 + 23.63 +#else 23.64 + 23.65 +#define lua_assert(c) ((void)0) 23.66 +#define check_exp(c,e) (e) 23.67 +#define api_check luai_apicheck 23.68 + 23.69 +#endif 23.70 + 23.71 + 23.72 +#ifndef UNUSED 23.73 +#define UNUSED(x) ((void)(x)) /* to avoid warnings */ 23.74 +#endif 23.75 + 23.76 + 23.77 +#ifndef cast 23.78 +#define cast(t, exp) ((t)(exp)) 23.79 +#endif 23.80 + 23.81 +#define cast_byte(i) cast(lu_byte, (i)) 23.82 +#define cast_num(i) cast(lua_Number, (i)) 23.83 +#define cast_int(i) cast(int, (i)) 23.84 + 23.85 + 23.86 + 23.87 +/* 23.88 +** type for virtual-machine instructions 23.89 +** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 23.90 +*/ 23.91 +typedef lu_int32 Instruction; 23.92 + 23.93 + 23.94 + 23.95 +/* maximum stack for a Lua function */ 23.96 +#define MAXSTACK 250 23.97 + 23.98 + 23.99 + 23.100 +/* minimum size for the string table (must be power of 2) */ 23.101 +#ifndef MINSTRTABSIZE 23.102 +#define MINSTRTABSIZE 32 23.103 +#endif 23.104 + 23.105 + 23.106 +/* minimum size for string buffer */ 23.107 +#ifndef LUA_MINBUFFER 23.108 +#define LUA_MINBUFFER 32 23.109 +#endif 23.110 + 23.111 + 23.112 +#ifndef lua_lock 23.113 +#define lua_lock(L) ((void) 0) 23.114 +#define lua_unlock(L) ((void) 0) 23.115 +#endif 23.116 + 23.117 +#ifndef luai_threadyield 23.118 +#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 23.119 +#endif 23.120 + 23.121 + 23.122 +/* 23.123 +** macro to control inclusion of some hard tests on stack reallocation 23.124 +*/ 23.125 +#ifndef HARDSTACKTESTS 23.126 +#define condhardstacktests(x) ((void)0) 23.127 +#else 23.128 +#define condhardstacktests(x) x 23.129 +#endif 23.130 + 23.131 +#endif
24.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 24.2 +++ b/src/lua/lmathlib.c Sat Mar 03 11:07:39 2012 -0600 24.3 @@ -0,0 +1,263 @@ 24.4 +/* 24.5 +** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $ 24.6 +** Standard mathematical library 24.7 +** See Copyright Notice in lua.h 24.8 +*/ 24.9 + 24.10 + 24.11 +#include <stdlib.h> 24.12 +#include <math.h> 24.13 + 24.14 +#define lmathlib_c 24.15 +#define LUA_LIB 24.16 + 24.17 +#include "lua.h" 24.18 + 24.19 +#include "lauxlib.h" 24.20 +#include "lualib.h" 24.21 + 24.22 + 24.23 +#undef PI 24.24 +#define PI (3.14159265358979323846) 24.25 +#define RADIANS_PER_DEGREE (PI/180.0) 24.26 + 24.27 + 24.28 + 24.29 +static int math_abs (lua_State *L) { 24.30 + lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); 24.31 + return 1; 24.32 +} 24.33 + 24.34 +static int math_sin (lua_State *L) { 24.35 + lua_pushnumber(L, sin(luaL_checknumber(L, 1))); 24.36 + return 1; 24.37 +} 24.38 + 24.39 +static int math_sinh (lua_State *L) { 24.40 + lua_pushnumber(L, sinh(luaL_checknumber(L, 1))); 24.41 + return 1; 24.42 +} 24.43 + 24.44 +static int math_cos (lua_State *L) { 24.45 + lua_pushnumber(L, cos(luaL_checknumber(L, 1))); 24.46 + return 1; 24.47 +} 24.48 + 24.49 +static int math_cosh (lua_State *L) { 24.50 + lua_pushnumber(L, cosh(luaL_checknumber(L, 1))); 24.51 + return 1; 24.52 +} 24.53 + 24.54 +static int math_tan (lua_State *L) { 24.55 + lua_pushnumber(L, tan(luaL_checknumber(L, 1))); 24.56 + return 1; 24.57 +} 24.58 + 24.59 +static int math_tanh (lua_State *L) { 24.60 + lua_pushnumber(L, tanh(luaL_checknumber(L, 1))); 24.61 + return 1; 24.62 +} 24.63 + 24.64 +static int math_asin (lua_State *L) { 24.65 + lua_pushnumber(L, asin(luaL_checknumber(L, 1))); 24.66 + return 1; 24.67 +} 24.68 + 24.69 +static int math_acos (lua_State *L) { 24.70 + lua_pushnumber(L, acos(luaL_checknumber(L, 1))); 24.71 + return 1; 24.72 +} 24.73 + 24.74 +static int math_atan (lua_State *L) { 24.75 + lua_pushnumber(L, atan(luaL_checknumber(L, 1))); 24.76 + return 1; 24.77 +} 24.78 + 24.79 +static int math_atan2 (lua_State *L) { 24.80 + lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 24.81 + return 1; 24.82 +} 24.83 + 24.84 +static int math_ceil (lua_State *L) { 24.85 + lua_pushnumber(L, ceil(luaL_checknumber(L, 1))); 24.86 + return 1; 24.87 +} 24.88 + 24.89 +static int math_floor (lua_State *L) { 24.90 + lua_pushnumber(L, floor(luaL_checknumber(L, 1))); 24.91 + return 1; 24.92 +} 24.93 + 24.94 +static int math_fmod (lua_State *L) { 24.95 + lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 24.96 + return 1; 24.97 +} 24.98 + 24.99 +static int math_modf (lua_State *L) { 24.100 + double ip; 24.101 + double fp = modf(luaL_checknumber(L, 1), &ip); 24.102 + lua_pushnumber(L, ip); 24.103 + lua_pushnumber(L, fp); 24.104 + return 2; 24.105 +} 24.106 + 24.107 +static int math_sqrt (lua_State *L) { 24.108 + lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); 24.109 + return 1; 24.110 +} 24.111 + 24.112 +static int math_pow (lua_State *L) { 24.113 + lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 24.114 + return 1; 24.115 +} 24.116 + 24.117 +static int math_log (lua_State *L) { 24.118 + lua_pushnumber(L, log(luaL_checknumber(L, 1))); 24.119 + return 1; 24.120 +} 24.121 + 24.122 +static int math_log10 (lua_State *L) { 24.123 + lua_pushnumber(L, log10(luaL_checknumber(L, 1))); 24.124 + return 1; 24.125 +} 24.126 + 24.127 +static int math_exp (lua_State *L) { 24.128 + lua_pushnumber(L, exp(luaL_checknumber(L, 1))); 24.129 + return 1; 24.130 +} 24.131 + 24.132 +static int math_deg (lua_State *L) { 24.133 + lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); 24.134 + return 1; 24.135 +} 24.136 + 24.137 +static int math_rad (lua_State *L) { 24.138 + lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); 24.139 + return 1; 24.140 +} 24.141 + 24.142 +static int math_frexp (lua_State *L) { 24.143 + int e; 24.144 + lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e)); 24.145 + lua_pushinteger(L, e); 24.146 + return 2; 24.147 +} 24.148 + 24.149 +static int math_ldexp (lua_State *L) { 24.150 + lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2))); 24.151 + return 1; 24.152 +} 24.153 + 24.154 + 24.155 + 24.156 +static int math_min (lua_State *L) { 24.157 + int n = lua_gettop(L); /* number of arguments */ 24.158 + lua_Number dmin = luaL_checknumber(L, 1); 24.159 + int i; 24.160 + for (i=2; i<=n; i++) { 24.161 + lua_Number d = luaL_checknumber(L, i); 24.162 + if (d < dmin) 24.163 + dmin = d; 24.164 + } 24.165 + lua_pushnumber(L, dmin); 24.166 + return 1; 24.167 +} 24.168 + 24.169 + 24.170 +static int math_max (lua_State *L) { 24.171 + int n = lua_gettop(L); /* number of arguments */ 24.172 + lua_Number dmax = luaL_checknumber(L, 1); 24.173 + int i; 24.174 + for (i=2; i<=n; i++) { 24.175 + lua_Number d = luaL_checknumber(L, i); 24.176 + if (d > dmax) 24.177 + dmax = d; 24.178 + } 24.179 + lua_pushnumber(L, dmax); 24.180 + return 1; 24.181 +} 24.182 + 24.183 + 24.184 +static int math_random (lua_State *L) { 24.185 + /* the `%' avoids the (rare) case of r==1, and is needed also because on 24.186 + some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ 24.187 + lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; 24.188 + switch (lua_gettop(L)) { /* check number of arguments */ 24.189 + case 0: { /* no arguments */ 24.190 + lua_pushnumber(L, r); /* Number between 0 and 1 */ 24.191 + break; 24.192 + } 24.193 + case 1: { /* only upper limit */ 24.194 + int u = luaL_checkint(L, 1); 24.195 + luaL_argcheck(L, 1<=u, 1, "interval is empty"); 24.196 + lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */ 24.197 + break; 24.198 + } 24.199 + case 2: { /* lower and upper limits */ 24.200 + int l = luaL_checkint(L, 1); 24.201 + int u = luaL_checkint(L, 2); 24.202 + luaL_argcheck(L, l<=u, 2, "interval is empty"); 24.203 + lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */ 24.204 + break; 24.205 + } 24.206 + default: return luaL_error(L, "wrong number of arguments"); 24.207 + } 24.208 + return 1; 24.209 +} 24.210 + 24.211 + 24.212 +static int math_randomseed (lua_State *L) { 24.213 + srand(luaL_checkint(L, 1)); 24.214 + return 0; 24.215 +} 24.216 + 24.217 + 24.218 +static const luaL_Reg mathlib[] = { 24.219 + {"abs", math_abs}, 24.220 + {"acos", math_acos}, 24.221 + {"asin", math_asin}, 24.222 + {"atan2", math_atan2}, 24.223 + {"atan", math_atan}, 24.224 + {"ceil", math_ceil}, 24.225 + {"cosh", math_cosh}, 24.226 + {"cos", math_cos}, 24.227 + {"deg", math_deg}, 24.228 + {"exp", math_exp}, 24.229 + {"floor", math_floor}, 24.230 + {"fmod", math_fmod}, 24.231 + {"frexp", math_frexp}, 24.232 + {"ldexp", math_ldexp}, 24.233 + {"log10", math_log10}, 24.234 + {"log", math_log}, 24.235 + {"max", math_max}, 24.236 + {"min", math_min}, 24.237 + {"modf", math_modf}, 24.238 + {"pow", math_pow}, 24.239 + {"rad", math_rad}, 24.240 + {"random", math_random}, 24.241 + {"randomseed", math_randomseed}, 24.242 + {"sinh", math_sinh}, 24.243 + {"sin", math_sin}, 24.244 + {"sqrt", math_sqrt}, 24.245 + {"tanh", math_tanh}, 24.246 + {"tan", math_tan}, 24.247 + {NULL, NULL} 24.248 +}; 24.249 + 24.250 + 24.251 +/* 24.252 +** Open math library 24.253 +*/ 24.254 +LUALIB_API int luaopen_math (lua_State *L) { 24.255 + luaL_register(L, LUA_MATHLIBNAME, mathlib); 24.256 + lua_pushnumber(L, PI); 24.257 + lua_setfield(L, -2, "pi"); 24.258 + lua_pushnumber(L, HUGE_VAL); 24.259 + lua_setfield(L, -2, "huge"); 24.260 +#if defined(LUA_COMPAT_MOD) 24.261 + lua_getfield(L, -1, "fmod"); 24.262 + lua_setfield(L, -2, "mod"); 24.263 +#endif 24.264 + return 1; 24.265 +} 24.266 +
25.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 25.2 +++ b/src/lua/lmem.c Sat Mar 03 11:07:39 2012 -0600 25.3 @@ -0,0 +1,86 @@ 25.4 +/* 25.5 +** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $ 25.6 +** Interface to Memory Manager 25.7 +** See Copyright Notice in lua.h 25.8 +*/ 25.9 + 25.10 + 25.11 +#include <stddef.h> 25.12 + 25.13 +#define lmem_c 25.14 +#define LUA_CORE 25.15 + 25.16 +#include "lua.h" 25.17 + 25.18 +#include "ldebug.h" 25.19 +#include "ldo.h" 25.20 +#include "lmem.h" 25.21 +#include "lobject.h" 25.22 +#include "lstate.h" 25.23 + 25.24 + 25.25 + 25.26 +/* 25.27 +** About the realloc function: 25.28 +** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 25.29 +** (`osize' is the old size, `nsize' is the new size) 25.30 +** 25.31 +** Lua ensures that (ptr == NULL) iff (osize == 0). 25.32 +** 25.33 +** * frealloc(ud, NULL, 0, x) creates a new block of size `x' 25.34 +** 25.35 +** * frealloc(ud, p, x, 0) frees the block `p' 25.36 +** (in this specific case, frealloc must return NULL). 25.37 +** particularly, frealloc(ud, NULL, 0, 0) does nothing 25.38 +** (which is equivalent to free(NULL) in ANSI C) 25.39 +** 25.40 +** frealloc returns NULL if it cannot create or reallocate the area 25.41 +** (any reallocation to an equal or smaller size cannot fail!) 25.42 +*/ 25.43 + 25.44 + 25.45 + 25.46 +#define MINSIZEARRAY 4 25.47 + 25.48 + 25.49 +void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 25.50 + int limit, const char *errormsg) { 25.51 + void *newblock; 25.52 + int newsize; 25.53 + if (*size >= limit/2) { /* cannot double it? */ 25.54 + if (*size >= limit) /* cannot grow even a little? */ 25.55 + luaG_runerror(L, errormsg); 25.56 + newsize = limit; /* still have at least one free place */ 25.57 + } 25.58 + else { 25.59 + newsize = (*size)*2; 25.60 + if (newsize < MINSIZEARRAY) 25.61 + newsize = MINSIZEARRAY; /* minimum size */ 25.62 + } 25.63 + newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 25.64 + *size = newsize; /* update only when everything else is OK */ 25.65 + return newblock; 25.66 +} 25.67 + 25.68 + 25.69 +void *luaM_toobig (lua_State *L) { 25.70 + luaG_runerror(L, "memory allocation error: block too big"); 25.71 + return NULL; /* to avoid warnings */ 25.72 +} 25.73 + 25.74 + 25.75 + 25.76 +/* 25.77 +** generic allocation routine. 25.78 +*/ 25.79 +void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 25.80 + global_State *g = G(L); 25.81 + lua_assert((osize == 0) == (block == NULL)); 25.82 + block = (*g->frealloc)(g->ud, block, osize, nsize); 25.83 + if (block == NULL && nsize > 0) 25.84 + luaD_throw(L, LUA_ERRMEM); 25.85 + lua_assert((nsize == 0) == (block == NULL)); 25.86 + g->totalbytes = (g->totalbytes - osize) + nsize; 25.87 + return block; 25.88 +} 25.89 +
26.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 26.2 +++ b/src/lua/lmem.h Sat Mar 03 11:07:39 2012 -0600 26.3 @@ -0,0 +1,49 @@ 26.4 +/* 26.5 +** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 26.6 +** Interface to Memory Manager 26.7 +** See Copyright Notice in lua.h 26.8 +*/ 26.9 + 26.10 +#ifndef lmem_h 26.11 +#define lmem_h 26.12 + 26.13 + 26.14 +#include <stddef.h> 26.15 + 26.16 +#include "llimits.h" 26.17 +#include "lua.h" 26.18 + 26.19 +#define MEMERRMSG "not enough memory" 26.20 + 26.21 + 26.22 +#define luaM_reallocv(L,b,on,n,e) \ 26.23 + ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 26.24 + luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ 26.25 + luaM_toobig(L)) 26.26 + 26.27 +#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 26.28 +#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 26.29 +#define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) 26.30 + 26.31 +#define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) 26.32 +#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 26.33 +#define luaM_newvector(L,n,t) \ 26.34 + cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 26.35 + 26.36 +#define luaM_growvector(L,v,nelems,size,t,limit,e) \ 26.37 + if ((nelems)+1 > (size)) \ 26.38 + ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 26.39 + 26.40 +#define luaM_reallocvector(L, v,oldn,n,t) \ 26.41 + ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 26.42 + 26.43 + 26.44 +LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 26.45 + size_t size); 26.46 +LUAI_FUNC void *luaM_toobig (lua_State *L); 26.47 +LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 26.48 + size_t size_elem, int limit, 26.49 + const char *errormsg); 26.50 + 26.51 +#endif 26.52 +
27.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 27.2 +++ b/src/lua/loadlib.c Sat Mar 03 11:07:39 2012 -0600 27.3 @@ -0,0 +1,666 @@ 27.4 +/* 27.5 +** $Id: loadlib.c,v 1.52.1.3 2008/08/06 13:29:28 roberto Exp $ 27.6 +** Dynamic library loader for Lua 27.7 +** See Copyright Notice in lua.h 27.8 +** 27.9 +** This module contains an implementation of loadlib for Unix systems 27.10 +** that have dlfcn, an implementation for Darwin (Mac OS X), an 27.11 +** implementation for Windows, and a stub for other systems. 27.12 +*/ 27.13 + 27.14 + 27.15 +#include <stdlib.h> 27.16 +#include <string.h> 27.17 + 27.18 + 27.19 +#define loadlib_c 27.20 +#define LUA_LIB 27.21 + 27.22 +#include "lua.h" 27.23 + 27.24 +#include "lauxlib.h" 27.25 +#include "lualib.h" 27.26 + 27.27 + 27.28 +/* prefix for open functions in C libraries */ 27.29 +#define LUA_POF "luaopen_" 27.30 + 27.31 +/* separator for open functions in C libraries */ 27.32 +#define LUA_OFSEP "_" 27.33 + 27.34 + 27.35 +#define LIBPREFIX "LOADLIB: " 27.36 + 27.37 +#define POF LUA_POF 27.38 +#define LIB_FAIL "open" 27.39 + 27.40 + 27.41 +/* error codes for ll_loadfunc */ 27.42 +#define ERRLIB 1 27.43 +#define ERRFUNC 2 27.44 + 27.45 +#define setprogdir(L) ((void)0) 27.46 + 27.47 + 27.48 +static void ll_unloadlib (void *lib); 27.49 +static void *ll_load (lua_State *L, const char *path); 27.50 +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); 27.51 + 27.52 + 27.53 + 27.54 +#if defined(LUA_DL_DLOPEN) 27.55 +/* 27.56 +** {======================================================================== 27.57 +** This is an implementation of loadlib based on the dlfcn interface. 27.58 +** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, 27.59 +** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least 27.60 +** as an emulation layer on top of native functions. 27.61 +** ========================================================================= 27.62 +*/ 27.63 + 27.64 +#include <dlfcn.h> 27.65 + 27.66 +static void ll_unloadlib (void *lib) { 27.67 + dlclose(lib); 27.68 +} 27.69 + 27.70 + 27.71 +static void *ll_load (lua_State *L, const char *path) { 27.72 + void *lib = dlopen(path, RTLD_NOW); 27.73 + if (lib == NULL) lua_pushstring(L, dlerror()); 27.74 + return lib; 27.75 +} 27.76 + 27.77 + 27.78 +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { 27.79 + lua_CFunction f = (lua_CFunction)dlsym(lib, sym); 27.80 + if (f == NULL) lua_pushstring(L, dlerror()); 27.81 + return f; 27.82 +} 27.83 + 27.84 +/* }====================================================== */ 27.85 + 27.86 + 27.87 + 27.88 +#elif defined(LUA_DL_DLL) 27.89 +/* 27.90 +** {====================================================================== 27.91 +** This is an implementation of loadlib for Windows using native functions. 27.92 +** ======================================================================= 27.93 +*/ 27.94 + 27.95 +#include <windows.h> 27.96 + 27.97 + 27.98 +#undef setprogdir 27.99 + 27.100 +static void setprogdir (lua_State *L) { 27.101 + char buff[MAX_PATH + 1]; 27.102 + char *lb; 27.103 + DWORD nsize = sizeof(buff)/sizeof(char); 27.104 + DWORD n = GetModuleFileNameA(NULL, buff, nsize); 27.105 + if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) 27.106 + luaL_error(L, "unable to get ModuleFileName"); 27.107 + else { 27.108 + *lb = '\0'; 27.109 + luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); 27.110 + lua_remove(L, -2); /* remove original string */ 27.111 + } 27.112 +} 27.113 + 27.114 + 27.115 +static void pusherror (lua_State *L) { 27.116 + int error = GetLastError(); 27.117 + char buffer[128]; 27.118 + if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, 27.119 + NULL, error, 0, buffer, sizeof(buffer), NULL)) 27.120 + lua_pushstring(L, buffer); 27.121 + else 27.122 + lua_pushfstring(L, "system error %d\n", error); 27.123 +} 27.124 + 27.125 +static void ll_unloadlib (void *lib) { 27.126 + FreeLibrary((HINSTANCE)lib); 27.127 +} 27.128 + 27.129 + 27.130 +static void *ll_load (lua_State *L, const char *path) { 27.131 + HINSTANCE lib = LoadLibraryA(path); 27.132 + if (lib == NULL) pusherror(L); 27.133 + return lib; 27.134 +} 27.135 + 27.136 + 27.137 +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { 27.138 + lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym); 27.139 + if (f == NULL) pusherror(L); 27.140 + return f; 27.141 +} 27.142 + 27.143 +/* }====================================================== */ 27.144 + 27.145 + 27.146 + 27.147 +#elif defined(LUA_DL_DYLD) 27.148 +/* 27.149 +** {====================================================================== 27.150 +** Native Mac OS X / Darwin Implementation 27.151 +** ======================================================================= 27.152 +*/ 27.153 + 27.154 +#include <mach-o/dyld.h> 27.155 + 27.156 + 27.157 +/* Mac appends a `_' before C function names */ 27.158 +#undef POF 27.159 +#define POF "_" LUA_POF 27.160 + 27.161 + 27.162 +static void pusherror (lua_State *L) { 27.163 + const char *err_str; 27.164 + const char *err_file; 27.165 + NSLinkEditErrors err; 27.166 + int err_num; 27.167 + NSLinkEditError(&err, &err_num, &err_file, &err_str); 27.168 + lua_pushstring(L, err_str); 27.169 +} 27.170 + 27.171 + 27.172 +static const char *errorfromcode (NSObjectFileImageReturnCode ret) { 27.173 + switch (ret) { 27.174 + case NSObjectFileImageInappropriateFile: 27.175 + return "file is not a bundle"; 27.176 + case NSObjectFileImageArch: 27.177 + return "library is for wrong CPU type"; 27.178 + case NSObjectFileImageFormat: 27.179 + return "bad format"; 27.180 + case NSObjectFileImageAccess: 27.181 + return "cannot access file"; 27.182 + case NSObjectFileImageFailure: 27.183 + default: 27.184 + return "unable to load library"; 27.185 + } 27.186 +} 27.187 + 27.188 + 27.189 +static void ll_unloadlib (void *lib) { 27.190 + NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); 27.191 +} 27.192 + 27.193 + 27.194 +static void *ll_load (lua_State *L, const char *path) { 27.195 + NSObjectFileImage img; 27.196 + NSObjectFileImageReturnCode ret; 27.197 + /* this would be a rare case, but prevents crashing if it happens */ 27.198 + if(!_dyld_present()) { 27.199 + lua_pushliteral(L, "dyld not present"); 27.200 + return NULL; 27.201 + } 27.202 + ret = NSCreateObjectFileImageFromFile(path, &img); 27.203 + if (ret == NSObjectFileImageSuccess) { 27.204 + NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE | 27.205 + NSLINKMODULE_OPTION_RETURN_ON_ERROR); 27.206 + NSDestroyObjectFileImage(img); 27.207 + if (mod == NULL) pusherror(L); 27.208 + return mod; 27.209 + } 27.210 + lua_pushstring(L, errorfromcode(ret)); 27.211 + return NULL; 27.212 +} 27.213 + 27.214 + 27.215 +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { 27.216 + NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym); 27.217 + if (nss == NULL) { 27.218 + lua_pushfstring(L, "symbol " LUA_QS " not found", sym); 27.219 + return NULL; 27.220 + } 27.221 + return (lua_CFunction)NSAddressOfSymbol(nss); 27.222 +} 27.223 + 27.224 +/* }====================================================== */ 27.225 + 27.226 + 27.227 + 27.228 +#else 27.229 +/* 27.230 +** {====================================================== 27.231 +** Fallback for other systems 27.232 +** ======================================================= 27.233 +*/ 27.234 + 27.235 +#undef LIB_FAIL 27.236 +#define LIB_FAIL "absent" 27.237 + 27.238 + 27.239 +#define DLMSG "dynamic libraries not enabled; check your Lua installation" 27.240 + 27.241 + 27.242 +static void ll_unloadlib (void *lib) { 27.243 + (void)lib; /* to avoid warnings */ 27.244 +} 27.245 + 27.246 + 27.247 +static void *ll_load (lua_State *L, const char *path) { 27.248 + (void)path; /* to avoid warnings */ 27.249 + lua_pushliteral(L, DLMSG); 27.250 + return NULL; 27.251 +} 27.252 + 27.253 + 27.254 +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { 27.255 + (void)lib; (void)sym; /* to avoid warnings */ 27.256 + lua_pushliteral(L, DLMSG); 27.257 + return NULL; 27.258 +} 27.259 + 27.260 +/* }====================================================== */ 27.261 +#endif 27.262 + 27.263 + 27.264 + 27.265 +static void **ll_register (lua_State *L, const char *path) { 27.266 + void **plib; 27.267 + lua_pushfstring(L, "%s%s", LIBPREFIX, path); 27.268 + lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ 27.269 + if (!lua_isnil(L, -1)) /* is there an entry? */ 27.270 + plib = (void **)lua_touserdata(L, -1); 27.271 + else { /* no entry yet; create one */ 27.272 + lua_pop(L, 1); 27.273 + plib = (void **)lua_newuserdata(L, sizeof(const void *)); 27.274 + *plib = NULL; 27.275 + luaL_getmetatable(L, "_LOADLIB"); 27.276 + lua_setmetatable(L, -2); 27.277 + lua_pushfstring(L, "%s%s", LIBPREFIX, path); 27.278 + lua_pushvalue(L, -2); 27.279 + lua_settable(L, LUA_REGISTRYINDEX); 27.280 + } 27.281 + return plib; 27.282 +} 27.283 + 27.284 + 27.285 +/* 27.286 +** __gc tag method: calls library's `ll_unloadlib' function with the lib 27.287 +** handle 27.288 +*/ 27.289 +static int gctm (lua_State *L) { 27.290 + void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); 27.291 + if (*lib) ll_unloadlib(*lib); 27.292 + *lib = NULL; /* mark library as closed */ 27.293 + return 0; 27.294 +} 27.295 + 27.296 + 27.297 +static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { 27.298 + void **reg = ll_register(L, path); 27.299 + if (*reg == NULL) *reg = ll_load(L, path); 27.300 + if (*reg == NULL) 27.301 + return ERRLIB; /* unable to load library */ 27.302 + else { 27.303 + lua_CFunction f = ll_sym(L, *reg, sym); 27.304 + if (f == NULL) 27.305 + return ERRFUNC; /* unable to find function */ 27.306 + lua_pushcfunction(L, f); 27.307 + return 0; /* return function */ 27.308 + } 27.309 +} 27.310 + 27.311 + 27.312 +static int ll_loadlib (lua_State *L) { 27.313 + const char *path = luaL_checkstring(L, 1); 27.314 + const char *init = luaL_checkstring(L, 2); 27.315 + int stat = ll_loadfunc(L, path, init); 27.316 + if (stat == 0) /* no errors? */ 27.317 + return 1; /* return the loaded function */ 27.318 + else { /* error; error message is on stack top */ 27.319 + lua_pushnil(L); 27.320 + lua_insert(L, -2); 27.321 + lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); 27.322 + return 3; /* return nil, error message, and where */ 27.323 + } 27.324 +} 27.325 + 27.326 + 27.327 + 27.328 +/* 27.329 +** {====================================================== 27.330 +** 'require' function 27.331 +** ======================================================= 27.332 +*/ 27.333 + 27.334 + 27.335 +static int readable (const char *filename) { 27.336 + FILE *f = fopen(filename, "r"); /* try to open file */ 27.337 + if (f == NULL) return 0; /* open failed */ 27.338 + fclose(f); 27.339 + return 1; 27.340 +} 27.341 + 27.342 + 27.343 +static const char *pushnexttemplate (lua_State *L, const char *path) { 27.344 + const char *l; 27.345 + while (*path == *LUA_PATHSEP) path++; /* skip separators */ 27.346 + if (*path == '\0') return NULL; /* no more templates */ 27.347 + l = strchr(path, *LUA_PATHSEP); /* find next separator */ 27.348 + if (l == NULL) l = path + strlen(path); 27.349 + lua_pushlstring(L, path, l - path); /* template */ 27.350 + return l; 27.351 +} 27.352 + 27.353 + 27.354 +static const char *findfile (lua_State *L, const char *name, 27.355 + const char *pname) { 27.356 + const char *path; 27.357 + name = luaL_gsub(L, name, ".", LUA_DIRSEP); 27.358 + lua_getfield(L, LUA_ENVIRONINDEX, pname); 27.359 + path = lua_tostring(L, -1); 27.360 + if (path == NULL) 27.361 + luaL_error(L, LUA_QL("package.%s") " must be a string", pname); 27.362 + lua_pushliteral(L, ""); /* error accumulator */ 27.363 + while ((path = pushnexttemplate(L, path)) != NULL) { 27.364 + const char *filename; 27.365 + filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); 27.366 + lua_remove(L, -2); /* remove path template */ 27.367 + if (readable(filename)) /* does file exist and is readable? */ 27.368 + return filename; /* return that file name */ 27.369 + lua_pushfstring(L, "\n\tno file " LUA_QS, filename); 27.370 + lua_remove(L, -2); /* remove file name */ 27.371 + lua_concat(L, 2); /* add entry to possible error message */ 27.372 + } 27.373 + return NULL; /* not found */ 27.374 +} 27.375 + 27.376 + 27.377 +static void loaderror (lua_State *L, const char *filename) { 27.378 + luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", 27.379 + lua_tostring(L, 1), filename, lua_tostring(L, -1)); 27.380 +} 27.381 + 27.382 + 27.383 +static int loader_Lua (lua_State *L) { 27.384 + const char *filename; 27.385 + const char *name = luaL_checkstring(L, 1); 27.386 + filename = findfile(L, name, "path"); 27.387 + if (filename == NULL) return 1; /* library not found in this path */ 27.388 + if (luaL_loadfile(L, filename) != 0) 27.389 + loaderror(L, filename); 27.390 + return 1; /* library loaded successfully */ 27.391 +} 27.392 + 27.393 + 27.394 +static const char *mkfuncname (lua_State *L, const char *modname) { 27.395 + const char *funcname; 27.396 + const char *mark = strchr(modname, *LUA_IGMARK); 27.397 + if (mark) modname = mark + 1; 27.398 + funcname = luaL_gsub(L, modname, ".", LUA_OFSEP); 27.399 + funcname = lua_pushfstring(L, POF"%s", funcname); 27.400 + lua_remove(L, -2); /* remove 'gsub' result */ 27.401 + return funcname; 27.402 +} 27.403 + 27.404 + 27.405 +static int loader_C (lua_State *L) { 27.406 + const char *funcname; 27.407 + const char *name = luaL_checkstring(L, 1); 27.408 + const char *filename = findfile(L, name, "cpath"); 27.409 + if (filename == NULL) return 1; /* library not found in this path */ 27.410 + funcname = mkfuncname(L, name); 27.411 + if (ll_loadfunc(L, filename, funcname) != 0) 27.412 + loaderror(L, filename); 27.413 + return 1; /* library loaded successfully */ 27.414 +} 27.415 + 27.416 + 27.417 +static int loader_Croot (lua_State *L) { 27.418 + const char *funcname; 27.419 + const char *filename; 27.420 + const char *name = luaL_checkstring(L, 1); 27.421 + const char *p = strchr(name, '.'); 27.422 + int stat; 27.423 + if (p == NULL) return 0; /* is root */ 27.424 + lua_pushlstring(L, name, p - name); 27.425 + filename = findfile(L, lua_tostring(L, -1), "cpath"); 27.426 + if (filename == NULL) return 1; /* root not found */ 27.427 + funcname = mkfuncname(L, name); 27.428 + if ((stat = ll_loadfunc(L, filename, funcname)) != 0) { 27.429 + if (stat != ERRFUNC) loaderror(L, filename); /* real error */ 27.430 + lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, 27.431 + name, filename); 27.432 + return 1; /* function not found */ 27.433 + } 27.434 + return 1; 27.435 +} 27.436 + 27.437 + 27.438 +static int loader_preload (lua_State *L) { 27.439 + const char *name = luaL_checkstring(L, 1); 27.440 + lua_getfield(L, LUA_ENVIRONINDEX, "preload"); 27.441 + if (!lua_istable(L, -1)) 27.442 + luaL_error(L, LUA_QL("package.preload") " must be a table"); 27.443 + lua_getfield(L, -1, name); 27.444 + if (lua_isnil(L, -1)) /* not found? */ 27.445 + lua_pushfstring(L, "\n\tno field package.preload['%s']", name); 27.446 + return 1; 27.447 +} 27.448 + 27.449 + 27.450 +static const int sentinel_ = 0; 27.451 +#define sentinel ((void *)&sentinel_) 27.452 + 27.453 + 27.454 +static int ll_require (lua_State *L) { 27.455 + const char *name = luaL_checkstring(L, 1); 27.456 + int i; 27.457 + lua_settop(L, 1); /* _LOADED table will be at index 2 */ 27.458 + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); 27.459 + lua_getfield(L, 2, name); 27.460 + if (lua_toboolean(L, -1)) { /* is it there? */ 27.461 + if (lua_touserdata(L, -1) == sentinel) /* check loops */ 27.462 + luaL_error(L, "loop or previous error loading module " LUA_QS, name); 27.463 + return 1; /* package is already loaded */ 27.464 + } 27.465 + /* else must load it; iterate over available loaders */ 27.466 + lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); 27.467 + if (!lua_istable(L, -1)) 27.468 + luaL_error(L, LUA_QL("package.loaders") " must be a table"); 27.469 + lua_pushliteral(L, ""); /* error message accumulator */ 27.470 + for (i=1; ; i++) { 27.471 + lua_rawgeti(L, -2, i); /* get a loader */ 27.472 + if (lua_isnil(L, -1)) 27.473 + luaL_error(L, "module " LUA_QS " not found:%s", 27.474 + name, lua_tostring(L, -2)); 27.475 + lua_pushstring(L, name); 27.476 + lua_call(L, 1, 1); /* call it */ 27.477 + if (lua_isfunction(L, -1)) /* did it find module? */ 27.478 + break; /* module loaded successfully */ 27.479 + else if (lua_isstring(L, -1)) /* loader returned error message? */ 27.480 + lua_concat(L, 2); /* accumulate it */ 27.481 + else 27.482 + lua_pop(L, 1); 27.483 + } 27.484 + lua_pushlightuserdata(L, sentinel); 27.485 + lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ 27.486 + lua_pushstring(L, name); /* pass name as argument to module */ 27.487 + lua_call(L, 1, 1); /* run loaded module */ 27.488 + if (!lua_isnil(L, -1)) /* non-nil return? */ 27.489 + lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ 27.490 + lua_getfield(L, 2, name); 27.491 + if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ 27.492 + lua_pushboolean(L, 1); /* use true as result */ 27.493 + lua_pushvalue(L, -1); /* extra copy to be returned */ 27.494 + lua_setfield(L, 2, name); /* _LOADED[name] = true */ 27.495 + } 27.496 + return 1; 27.497 +} 27.498 + 27.499 +/* }====================================================== */ 27.500 + 27.501 + 27.502 + 27.503 +/* 27.504 +** {====================================================== 27.505 +** 'module' function 27.506 +** ======================================================= 27.507 +*/ 27.508 + 27.509 + 27.510 +static void setfenv (lua_State *L) { 27.511 + lua_Debug ar; 27.512 + if (lua_getstack(L, 1, &ar) == 0 || 27.513 + lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ 27.514 + lua_iscfunction(L, -1)) 27.515 + luaL_error(L, LUA_QL("module") " not called from a Lua function"); 27.516 + lua_pushvalue(L, -2); 27.517 + lua_setfenv(L, -2); 27.518 + lua_pop(L, 1); 27.519 +} 27.520 + 27.521 + 27.522 +static void dooptions (lua_State *L, int n) { 27.523 + int i; 27.524 + for (i = 2; i <= n; i++) { 27.525 + lua_pushvalue(L, i); /* get option (a function) */ 27.526 + lua_pushvalue(L, -2); /* module */ 27.527 + lua_call(L, 1, 0); 27.528 + } 27.529 +} 27.530 + 27.531 + 27.532 +static void modinit (lua_State *L, const char *modname) { 27.533 + const char *dot; 27.534 + lua_pushvalue(L, -1); 27.535 + lua_setfield(L, -2, "_M"); /* module._M = module */ 27.536 + lua_pushstring(L, modname); 27.537 + lua_setfield(L, -2, "_NAME"); 27.538 + dot = strrchr(modname, '.'); /* look for last dot in module name */ 27.539 + if (dot == NULL) dot = modname; 27.540 + else dot++; 27.541 + /* set _PACKAGE as package name (full module name minus last part) */ 27.542 + lua_pushlstring(L, modname, dot - modname); 27.543 + lua_setfield(L, -2, "_PACKAGE"); 27.544 +} 27.545 + 27.546 + 27.547 +static int ll_module (lua_State *L) { 27.548 + const char *modname = luaL_checkstring(L, 1); 27.549 + int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ 27.550 + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); 27.551 + lua_getfield(L, loaded, modname); /* get _LOADED[modname] */ 27.552 + if (!lua_istable(L, -1)) { /* not found? */ 27.553 + lua_pop(L, 1); /* remove previous result */ 27.554 + /* try global variable (and create one if it does not exist) */ 27.555 + if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL) 27.556 + return luaL_error(L, "name conflict for module " LUA_QS, modname); 27.557 + lua_pushvalue(L, -1); 27.558 + lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ 27.559 + } 27.560 + /* check whether table already has a _NAME field */ 27.561 + lua_getfield(L, -1, "_NAME"); 27.562 + if (!lua_isnil(L, -1)) /* is table an initialized module? */ 27.563 + lua_pop(L, 1); 27.564 + else { /* no; initialize it */ 27.565 + lua_pop(L, 1); 27.566 + modinit(L, modname); 27.567 + } 27.568 + lua_pushvalue(L, -1); 27.569 + setfenv(L); 27.570 + dooptions(L, loaded - 1); 27.571 + return 0; 27.572 +} 27.573 + 27.574 + 27.575 +static int ll_seeall (lua_State *L) { 27.576 + luaL_checktype(L, 1, LUA_TTABLE); 27.577 + if (!lua_getmetatable(L, 1)) { 27.578 + lua_createtable(L, 0, 1); /* create new metatable */ 27.579 + lua_pushvalue(L, -1); 27.580 + lua_setmetatable(L, 1); 27.581 + } 27.582 + lua_pushvalue(L, LUA_GLOBALSINDEX); 27.583 + lua_setfield(L, -2, "__index"); /* mt.__index = _G */ 27.584 + return 0; 27.585 +} 27.586 + 27.587 + 27.588 +/* }====================================================== */ 27.589 + 27.590 + 27.591 + 27.592 +/* auxiliary mark (for internal use) */ 27.593 +#define AUXMARK "\1" 27.594 + 27.595 +static void setpath (lua_State *L, const char *fieldname, const char *envname, 27.596 + const char *def) { 27.597 + const char *path = getenv(envname); 27.598 + if (path == NULL) /* no environment variable? */ 27.599 + lua_pushstring(L, def); /* use default */ 27.600 + else { 27.601 + /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ 27.602 + path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP, 27.603 + LUA_PATHSEP AUXMARK LUA_PATHSEP); 27.604 + luaL_gsub(L, path, AUXMARK, def); 27.605 + lua_remove(L, -2); 27.606 + } 27.607 + setprogdir(L); 27.608 + lua_setfield(L, -2, fieldname); 27.609 +} 27.610 + 27.611 + 27.612 +static const luaL_Reg pk_funcs[] = { 27.613 + {"loadlib", ll_loadlib}, 27.614 + {"seeall", ll_seeall}, 27.615 + {NULL, NULL} 27.616 +}; 27.617 + 27.618 + 27.619 +static const luaL_Reg ll_funcs[] = { 27.620 + {"module", ll_module}, 27.621 + {"require", ll_require}, 27.622 + {NULL, NULL} 27.623 +}; 27.624 + 27.625 + 27.626 +static const lua_CFunction loaders[] = 27.627 + {loader_preload, loader_Lua, loader_C, loader_Croot, NULL}; 27.628 + 27.629 + 27.630 +LUALIB_API int luaopen_package (lua_State *L) { 27.631 + int i; 27.632 + /* create new type _LOADLIB */ 27.633 + luaL_newmetatable(L, "_LOADLIB"); 27.634 + lua_pushcfunction(L, gctm); 27.635 + lua_setfield(L, -2, "__gc"); 27.636 + /* create `package' table */ 27.637 + luaL_register(L, LUA_LOADLIBNAME, pk_funcs); 27.638 +#if defined(LUA_COMPAT_LOADLIB) 27.639 + lua_getfield(L, -1, "loadlib"); 27.640 + lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); 27.641 +#endif 27.642 + lua_pushvalue(L, -1); 27.643 + lua_replace(L, LUA_ENVIRONINDEX); 27.644 + /* create `loaders' table */ 27.645 + lua_createtable(L, 0, sizeof(loaders)/sizeof(loaders[0]) - 1); 27.646 + /* fill it with pre-defined loaders */ 27.647 + for (i=0; loaders[i] != NULL; i++) { 27.648 + lua_pushcfunction(L, loaders[i]); 27.649 + lua_rawseti(L, -2, i+1); 27.650 + } 27.651 + lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ 27.652 + setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */ 27.653 + setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */ 27.654 + /* store config information */ 27.655 + lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" 27.656 + LUA_EXECDIR "\n" LUA_IGMARK); 27.657 + lua_setfield(L, -2, "config"); 27.658 + /* set field `loaded' */ 27.659 + luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); 27.660 + lua_setfield(L, -2, "loaded"); 27.661 + /* set field `preload' */ 27.662 + lua_newtable(L); 27.663 + lua_setfield(L, -2, "preload"); 27.664 + lua_pushvalue(L, LUA_GLOBALSINDEX); 27.665 + luaL_register(L, NULL, ll_funcs); /* open lib into global table */ 27.666 + lua_pop(L, 1); 27.667 + return 1; /* return 'package' table */ 27.668 +} 27.669 +
28.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 28.2 +++ b/src/lua/lobject.c Sat Mar 03 11:07:39 2012 -0600 28.3 @@ -0,0 +1,214 @@ 28.4 +/* 28.5 +** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $ 28.6 +** Some generic functions over Lua objects 28.7 +** See Copyright Notice in lua.h 28.8 +*/ 28.9 + 28.10 +#include <ctype.h> 28.11 +#include <stdarg.h> 28.12 +#include <stdio.h> 28.13 +#include <stdlib.h> 28.14 +#include <string.h> 28.15 + 28.16 +#define lobject_c 28.17 +#define LUA_CORE 28.18 + 28.19 +#include "lua.h" 28.20 + 28.21 +#include "ldo.h" 28.22 +#include "lmem.h" 28.23 +#include "lobject.h" 28.24 +#include "lstate.h" 28.25 +#include "lstring.h" 28.26 +#include "lvm.h" 28.27 + 28.28 + 28.29 + 28.30 +const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL}; 28.31 + 28.32 + 28.33 +/* 28.34 +** converts an integer to a "floating point byte", represented as 28.35 +** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if 28.36 +** eeeee != 0 and (xxx) otherwise. 28.37 +*/ 28.38 +int luaO_int2fb (unsigned int x) { 28.39 + int e = 0; /* expoent */ 28.40 + while (x >= 16) { 28.41 + x = (x+1) >> 1; 28.42 + e++; 28.43 + } 28.44 + if (x < 8) return x; 28.45 + else return ((e+1) << 3) | (cast_int(x) - 8); 28.46 +} 28.47 + 28.48 + 28.49 +/* converts back */ 28.50 +int luaO_fb2int (int x) { 28.51 + int e = (x >> 3) & 31; 28.52 + if (e == 0) return x; 28.53 + else return ((x & 7)+8) << (e - 1); 28.54 +} 28.55 + 28.56 + 28.57 +int luaO_log2 (unsigned int x) { 28.58 + static const lu_byte log_2[256] = { 28.59 + 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 28.60 + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 28.61 + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 28.62 + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 28.63 + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 28.64 + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 28.65 + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 28.66 + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 28.67 + }; 28.68 + int l = -1; 28.69 + while (x >= 256) { l += 8; x >>= 8; } 28.70 + return l + log_2[x]; 28.71 + 28.72 +} 28.73 + 28.74 + 28.75 +int luaO_rawequalObj (const TValue *t1, const TValue *t2) { 28.76 + if (ttype(t1) != ttype(t2)) return 0; 28.77 + else switch (ttype(t1)) { 28.78 + case LUA_TNIL: 28.79 + return 1; 28.80 + case LUA_TNUMBER: 28.81 + return luai_numeq(nvalue(t1), nvalue(t2)); 28.82 + case LUA_TBOOLEAN: 28.83 + return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ 28.84 + case LUA_TLIGHTUSERDATA: 28.85 + return pvalue(t1) == pvalue(t2); 28.86 + default: 28.87 + lua_assert(iscollectable(t1)); 28.88 + return gcvalue(t1) == gcvalue(t2); 28.89 + } 28.90 +} 28.91 + 28.92 + 28.93 +int luaO_str2d (const char *s, lua_Number *result) { 28.94 + char *endptr; 28.95 + *result = lua_str2number(s, &endptr); 28.96 + if (endptr == s) return 0; /* conversion failed */ 28.97 + if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */ 28.98 + *result = cast_num(strtoul(s, &endptr, 16)); 28.99 + if (*endptr == '\0') return 1; /* most common case */ 28.100 + while (isspace(cast(unsigned char, *endptr))) endptr++; 28.101 + if (*endptr != '\0') return 0; /* invalid trailing characters? */ 28.102 + return 1; 28.103 +} 28.104 + 28.105 + 28.106 + 28.107 +static void pushstr (lua_State *L, const char *str) { 28.108 + setsvalue2s(L, L->top, luaS_new(L, str)); 28.109 + incr_top(L); 28.110 +} 28.111 + 28.112 + 28.113 +/* this function handles only `%d', `%c', %f, %p, and `%s' formats */ 28.114 +const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { 28.115 + int n = 1; 28.116 + pushstr(L, ""); 28.117 + for (;;) { 28.118 + const char *e = strchr(fmt, '%'); 28.119 + if (e == NULL) break; 28.120 + setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); 28.121 + incr_top(L); 28.122 + switch (*(e+1)) { 28.123 + case 's': { 28.124 + const char *s = va_arg(argp, char *); 28.125 + if (s == NULL) s = "(null)"; 28.126 + pushstr(L, s); 28.127 + break; 28.128 + } 28.129 + case 'c': { 28.130 + char buff[2]; 28.131 + buff[0] = cast(char, va_arg(argp, int)); 28.132 + buff[1] = '\0'; 28.133 + pushstr(L, buff); 28.134 + break; 28.135 + } 28.136 + case 'd': { 28.137 + setnvalue(L->top, cast_num(va_arg(argp, int))); 28.138 + incr_top(L); 28.139 + break; 28.140 + } 28.141 + case 'f': { 28.142 + setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); 28.143 + incr_top(L); 28.144 + break; 28.145 + } 28.146 + case 'p': { 28.147 + char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ 28.148 + sprintf(buff, "%p", va_arg(argp, void *)); 28.149 + pushstr(L, buff); 28.150 + break; 28.151 + } 28.152 + case '%': { 28.153 + pushstr(L, "%"); 28.154 + break; 28.155 + } 28.156 + default: { 28.157 + char buff[3]; 28.158 + buff[0] = '%'; 28.159 + buff[1] = *(e+1); 28.160 + buff[2] = '\0'; 28.161 + pushstr(L, buff); 28.162 + break; 28.163 + } 28.164 + } 28.165 + n += 2; 28.166 + fmt = e+2; 28.167 + } 28.168 + pushstr(L, fmt); 28.169 + luaV_concat(L, n+1, cast_int(L->top - L->base) - 1); 28.170 + L->top -= n; 28.171 + return svalue(L->top - 1); 28.172 +} 28.173 + 28.174 + 28.175 +const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { 28.176 + const char *msg; 28.177 + va_list argp; 28.178 + va_start(argp, fmt); 28.179 + msg = luaO_pushvfstring(L, fmt, argp); 28.180 + va_end(argp); 28.181 + return msg; 28.182 +} 28.183 + 28.184 + 28.185 +void luaO_chunkid (char *out, const char *source, size_t bufflen) { 28.186 + if (*source == '=') { 28.187 + strncpy(out, source+1, bufflen); /* remove first char */ 28.188 + out[bufflen-1] = '\0'; /* ensures null termination */ 28.189 + } 28.190 + else { /* out = "source", or "...source" */ 28.191 + if (*source == '@') { 28.192 + size_t l; 28.193 + source++; /* skip the `@' */ 28.194 + bufflen -= sizeof(" '...' "); 28.195 + l = strlen(source); 28.196 + strcpy(out, ""); 28.197 + if (l > bufflen) { 28.198 + source += (l-bufflen); /* get last part of file name */ 28.199 + strcat(out, "..."); 28.200 + } 28.201 + strcat(out, source); 28.202 + } 28.203 + else { /* out = [string "string"] */ 28.204 + size_t len = strcspn(source, "\n\r"); /* stop at first newline */ 28.205 + bufflen -= sizeof(" [string \"...\"] "); 28.206 + if (len > bufflen) len = bufflen; 28.207 + strcpy(out, "[string \""); 28.208 + if (source[len] != '\0') { /* must truncate? */ 28.209 + strncat(out, source, len); 28.210 + strcat(out, "..."); 28.211 + } 28.212 + else 28.213 + strcat(out, source); 28.214 + strcat(out, "\"]"); 28.215 + } 28.216 + } 28.217 +}
29.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 29.2 +++ b/src/lua/lobject.h Sat Mar 03 11:07:39 2012 -0600 29.3 @@ -0,0 +1,381 @@ 29.4 +/* 29.5 +** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $ 29.6 +** Type definitions for Lua objects 29.7 +** See Copyright Notice in lua.h 29.8 +*/ 29.9 + 29.10 + 29.11 +#ifndef lobject_h 29.12 +#define lobject_h 29.13 + 29.14 + 29.15 +#include <stdarg.h> 29.16 + 29.17 + 29.18 +#include "llimits.h" 29.19 +#include "lua.h" 29.20 + 29.21 + 29.22 +/* tags for values visible from Lua */ 29.23 +#define LAST_TAG LUA_TTHREAD 29.24 + 29.25 +#define NUM_TAGS (LAST_TAG+1) 29.26 + 29.27 + 29.28 +/* 29.29 +** Extra tags for non-values 29.30 +*/ 29.31 +#define LUA_TPROTO (LAST_TAG+1) 29.32 +#define LUA_TUPVAL (LAST_TAG+2) 29.33 +#define LUA_TDEADKEY (LAST_TAG+3) 29.34 + 29.35 + 29.36 +/* 29.37 +** Union of all collectable objects 29.38 +*/ 29.39 +typedef union GCObject GCObject; 29.40 + 29.41 + 29.42 +/* 29.43 +** Common Header for all collectable objects (in macro form, to be 29.44 +** included in other objects) 29.45 +*/ 29.46 +#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked 29.47 + 29.48 + 29.49 +/* 29.50 +** Common header in struct form 29.51 +*/ 29.52 +typedef struct GCheader { 29.53 + CommonHeader; 29.54 +} GCheader; 29.55 + 29.56 + 29.57 + 29.58 + 29.59 +/* 29.60 +** Union of all Lua values 29.61 +*/ 29.62 +typedef union { 29.63 + GCObject *gc; 29.64 + void *p; 29.65 + lua_Number n; 29.66 + int b; 29.67 +} Value; 29.68 + 29.69 + 29.70 +/* 29.71 +** Tagged Values 29.72 +*/ 29.73 + 29.74 +#define TValuefields Value value; int tt 29.75 + 29.76 +typedef struct lua_TValue { 29.77 + TValuefields; 29.78 +} TValue; 29.79 + 29.80 + 29.81 +/* Macros to test type */ 29.82 +#define ttisnil(o) (ttype(o) == LUA_TNIL) 29.83 +#define ttisnumber(o) (ttype(o) == LUA_TNUMBER) 29.84 +#define ttisstring(o) (ttype(o) == LUA_TSTRING) 29.85 +#define ttistable(o) (ttype(o) == LUA_TTABLE) 29.86 +#define ttisfunction(o) (ttype(o) == LUA_TFUNCTION) 29.87 +#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN) 29.88 +#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA) 29.89 +#define ttisthread(o) (ttype(o) == LUA_TTHREAD) 29.90 +#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) 29.91 + 29.92 +/* Macros to access values */ 29.93 +#define ttype(o) ((o)->tt) 29.94 +#define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc) 29.95 +#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) 29.96 +#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) 29.97 +#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts) 29.98 +#define tsvalue(o) (&rawtsvalue(o)->tsv) 29.99 +#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u) 29.100 +#define uvalue(o) (&rawuvalue(o)->uv) 29.101 +#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl) 29.102 +#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h) 29.103 +#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) 29.104 +#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th) 29.105 + 29.106 +#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) 29.107 + 29.108 +/* 29.109 +** for internal debug only 29.110 +*/ 29.111 +#define checkconsistency(obj) \ 29.112 + lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt)) 29.113 + 29.114 +#define checkliveness(g,obj) \ 29.115 + lua_assert(!iscollectable(obj) || \ 29.116 + ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc))) 29.117 + 29.118 + 29.119 +/* Macros to set values */ 29.120 +#define setnilvalue(obj) ((obj)->tt=LUA_TNIL) 29.121 + 29.122 +#define setnvalue(obj,x) \ 29.123 + { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; } 29.124 + 29.125 +#define setpvalue(obj,x) \ 29.126 + { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; } 29.127 + 29.128 +#define setbvalue(obj,x) \ 29.129 + { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; } 29.130 + 29.131 +#define setsvalue(L,obj,x) \ 29.132 + { TValue *i_o=(obj); \ 29.133 + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \ 29.134 + checkliveness(G(L),i_o); } 29.135 + 29.136 +#define setuvalue(L,obj,x) \ 29.137 + { TValue *i_o=(obj); \ 29.138 + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \ 29.139 + checkliveness(G(L),i_o); } 29.140 + 29.141 +#define setthvalue(L,obj,x) \ 29.142 + { TValue *i_o=(obj); \ 29.143 + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \ 29.144 + checkliveness(G(L),i_o); } 29.145 + 29.146 +#define setclvalue(L,obj,x) \ 29.147 + { TValue *i_o=(obj); \ 29.148 + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \ 29.149 + checkliveness(G(L),i_o); } 29.150 + 29.151 +#define sethvalue(L,obj,x) \ 29.152 + { TValue *i_o=(obj); \ 29.153 + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \ 29.154 + checkliveness(G(L),i_o); } 29.155 + 29.156 +#define setptvalue(L,obj,x) \ 29.157 + { TValue *i_o=(obj); \ 29.158 + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \ 29.159 + checkliveness(G(L),i_o); } 29.160 + 29.161 + 29.162 + 29.163 + 29.164 +#define setobj(L,obj1,obj2) \ 29.165 + { const TValue *o2=(obj2); TValue *o1=(obj1); \ 29.166 + o1->value = o2->value; o1->tt=o2->tt; \ 29.167 + checkliveness(G(L),o1); } 29.168 + 29.169 + 29.170 +/* 29.171 +** different types of sets, according to destination 29.172 +*/ 29.173 + 29.174 +/* from stack to (same) stack */ 29.175 +#define setobjs2s setobj 29.176 +/* to stack (not from same stack) */ 29.177 +#define setobj2s setobj 29.178 +#define setsvalue2s setsvalue 29.179 +#define sethvalue2s sethvalue 29.180 +#define setptvalue2s setptvalue 29.181 +/* from table to same table */ 29.182 +#define setobjt2t setobj 29.183 +/* to table */ 29.184 +#define setobj2t setobj 29.185 +/* to new object */ 29.186 +#define setobj2n setobj 29.187 +#define setsvalue2n setsvalue 29.188 + 29.189 +#define setttype(obj, tt) (ttype(obj) = (tt)) 29.190 + 29.191 + 29.192 +#define iscollectable(o) (ttype(o) >= LUA_TSTRING) 29.193 + 29.194 + 29.195 + 29.196 +typedef TValue *StkId; /* index to stack elements */ 29.197 + 29.198 + 29.199 +/* 29.200 +** String headers for string table 29.201 +*/ 29.202 +typedef union TString { 29.203 + L_Umaxalign dummy; /* ensures maximum alignment for strings */ 29.204 + struct { 29.205 + CommonHeader; 29.206 + lu_byte reserved; 29.207 + unsigned int hash; 29.208 + size_t len; 29.209 + } tsv; 29.210 +} TString; 29.211 + 29.212 + 29.213 +#define getstr(ts) cast(const char *, (ts) + 1) 29.214 +#define svalue(o) getstr(rawtsvalue(o)) 29.215 + 29.216 + 29.217 + 29.218 +typedef union Udata { 29.219 + L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ 29.220 + struct { 29.221 + CommonHeader; 29.222 + struct Table *metatable; 29.223 + struct Table *env; 29.224 + size_t len; 29.225 + } uv; 29.226 +} Udata; 29.227 + 29.228 + 29.229 + 29.230 + 29.231 +/* 29.232 +** Function Prototypes 29.233 +*/ 29.234 +typedef struct Proto { 29.235 + CommonHeader; 29.236 + TValue *k; /* constants used by the function */ 29.237 + Instruction *code; 29.238 + struct Proto **p; /* functions defined inside the function */ 29.239 + int *lineinfo; /* map from opcodes to source lines */ 29.240 + struct LocVar *locvars; /* information about local variables */ 29.241 + TString **upvalues; /* upvalue names */ 29.242 + TString *source; 29.243 + int sizeupvalues; 29.244 + int sizek; /* size of `k' */ 29.245 + int sizecode; 29.246 + int sizelineinfo; 29.247 + int sizep; /* size of `p' */ 29.248 + int sizelocvars; 29.249 + int linedefined; 29.250 + int lastlinedefined; 29.251 + GCObject *gclist; 29.252 + lu_byte nups; /* number of upvalues */ 29.253 + lu_byte numparams; 29.254 + lu_byte is_vararg; 29.255 + lu_byte maxstacksize; 29.256 +} Proto; 29.257 + 29.258 + 29.259 +/* masks for new-style vararg */ 29.260 +#define VARARG_HASARG 1 29.261 +#define VARARG_ISVARARG 2 29.262 +#define VARARG_NEEDSARG 4 29.263 + 29.264 + 29.265 +typedef struct LocVar { 29.266 + TString *varname; 29.267 + int startpc; /* first point where variable is active */ 29.268 + int endpc; /* first point where variable is dead */ 29.269 +} LocVar; 29.270 + 29.271 + 29.272 + 29.273 +/* 29.274 +** Upvalues 29.275 +*/ 29.276 + 29.277 +typedef struct UpVal { 29.278 + CommonHeader; 29.279 + TValue *v; /* points to stack or to its own value */ 29.280 + union { 29.281 + TValue value; /* the value (when closed) */ 29.282 + struct { /* double linked list (when open) */ 29.283 + struct UpVal *prev; 29.284 + struct UpVal *next; 29.285 + } l; 29.286 + } u; 29.287 +} UpVal; 29.288 + 29.289 + 29.290 +/* 29.291 +** Closures 29.292 +*/ 29.293 + 29.294 +#define ClosureHeader \ 29.295 + CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \ 29.296 + struct Table *env 29.297 + 29.298 +typedef struct CClosure { 29.299 + ClosureHeader; 29.300 + lua_CFunction f; 29.301 + TValue upvalue[1]; 29.302 +} CClosure; 29.303 + 29.304 + 29.305 +typedef struct LClosure { 29.306 + ClosureHeader; 29.307 + struct Proto *p; 29.308 + UpVal *upvals[1]; 29.309 +} LClosure; 29.310 + 29.311 + 29.312 +typedef union Closure { 29.313 + CClosure c; 29.314 + LClosure l; 29.315 +} Closure; 29.316 + 29.317 + 29.318 +#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC) 29.319 +#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC) 29.320 + 29.321 + 29.322 +/* 29.323 +** Tables 29.324 +*/ 29.325 + 29.326 +typedef union TKey { 29.327 + struct { 29.328 + TValuefields; 29.329 + struct Node *next; /* for chaining */ 29.330 + } nk; 29.331 + TValue tvk; 29.332 +} TKey; 29.333 + 29.334 + 29.335 +typedef struct Node { 29.336 + TValue i_val; 29.337 + TKey i_key; 29.338 +} Node; 29.339 + 29.340 + 29.341 +typedef struct Table { 29.342 + CommonHeader; 29.343 + lu_byte flags; /* 1<<p means tagmethod(p) is not present */ 29.344 + lu_byte lsizenode; /* log2 of size of `node' array */ 29.345 + struct Table *metatable; 29.346 + TValue *array; /* array part */ 29.347 + Node *node; 29.348 + Node *lastfree; /* any free position is before this position */ 29.349 + GCObject *gclist; 29.350 + int sizearray; /* size of `array' array */ 29.351 +} Table; 29.352 + 29.353 + 29.354 + 29.355 +/* 29.356 +** `module' operation for hashing (size is always a power of 2) 29.357 +*/ 29.358 +#define lmod(s,size) \ 29.359 + (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1))))) 29.360 + 29.361 + 29.362 +#define twoto(x) (1<<(x)) 29.363 +#define sizenode(t) (twoto((t)->lsizenode)) 29.364 + 29.365 + 29.366 +#define luaO_nilobject (&luaO_nilobject_) 29.367 + 29.368 +LUAI_DATA const TValue luaO_nilobject_; 29.369 + 29.370 +#define ceillog2(x) (luaO_log2((x)-1) + 1) 29.371 + 29.372 +LUAI_FUNC int luaO_log2 (unsigned int x); 29.373 +LUAI_FUNC int luaO_int2fb (unsigned int x); 29.374 +LUAI_FUNC int luaO_fb2int (int x); 29.375 +LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2); 29.376 +LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result); 29.377 +LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, 29.378 + va_list argp); 29.379 +LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); 29.380 +LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); 29.381 + 29.382 + 29.383 +#endif 29.384 +
30.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 30.2 +++ b/src/lua/lopcodes.c Sat Mar 03 11:07:39 2012 -0600 30.3 @@ -0,0 +1,102 @@ 30.4 +/* 30.5 +** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 30.6 +** See Copyright Notice in lua.h 30.7 +*/ 30.8 + 30.9 + 30.10 +#define lopcodes_c 30.11 +#define LUA_CORE 30.12 + 30.13 + 30.14 +#include "lopcodes.h" 30.15 + 30.16 + 30.17 +/* ORDER OP */ 30.18 + 30.19 +const char *const luaP_opnames[NUM_OPCODES+1] = { 30.20 + "MOVE", 30.21 + "LOADK", 30.22 + "LOADBOOL", 30.23 + "LOADNIL", 30.24 + "GETUPVAL", 30.25 + "GETGLOBAL", 30.26 + "GETTABLE", 30.27 + "SETGLOBAL", 30.28 + "SETUPVAL", 30.29 + "SETTABLE", 30.30 + "NEWTABLE", 30.31 + "SELF", 30.32 + "ADD", 30.33 + "SUB", 30.34 + "MUL", 30.35 + "DIV", 30.36 + "MOD", 30.37 + "POW", 30.38 + "UNM", 30.39 + "NOT", 30.40 + "LEN", 30.41 + "CONCAT", 30.42 + "JMP", 30.43 + "EQ", 30.44 + "LT", 30.45 + "LE", 30.46 + "TEST", 30.47 + "TESTSET", 30.48 + "CALL", 30.49 + "TAILCALL", 30.50 + "RETURN", 30.51 + "FORLOOP", 30.52 + "FORPREP", 30.53 + "TFORLOOP", 30.54 + "SETLIST", 30.55 + "CLOSE", 30.56 + "CLOSURE", 30.57 + "VARARG", 30.58 + NULL 30.59 +}; 30.60 + 30.61 + 30.62 +#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 30.63 + 30.64 +const lu_byte luaP_opmodes[NUM_OPCODES] = { 30.65 +/* T A B C mode opcode */ 30.66 + opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 30.67 + ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 30.68 + ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 30.69 + ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */ 30.70 + ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 30.71 + ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */ 30.72 + ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 30.73 + ,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */ 30.74 + ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 30.75 + ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 30.76 + ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 30.77 + ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 30.78 + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 30.79 + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 30.80 + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 30.81 + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 30.82 + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 30.83 + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 30.84 + ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 30.85 + ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 30.86 + ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 30.87 + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 30.88 + ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 30.89 + ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 30.90 + ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 30.91 + ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 30.92 + ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */ 30.93 + ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 30.94 + ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 30.95 + ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 30.96 + ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 30.97 + ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 30.98 + ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 30.99 + ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */ 30.100 + ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 30.101 + ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ 30.102 + ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 30.103 + ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 30.104 +}; 30.105 +
31.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 31.2 +++ b/src/lua/lopcodes.h Sat Mar 03 11:07:39 2012 -0600 31.3 @@ -0,0 +1,268 @@ 31.4 +/* 31.5 +** $Id: lopcodes.h,v 1.125.1.1 2007/12/27 13:02:25 roberto Exp $ 31.6 +** Opcodes for Lua virtual machine 31.7 +** See Copyright Notice in lua.h 31.8 +*/ 31.9 + 31.10 +#ifndef lopcodes_h 31.11 +#define lopcodes_h 31.12 + 31.13 +#include "llimits.h" 31.14 + 31.15 + 31.16 +/*=========================================================================== 31.17 + We assume that instructions are unsigned numbers. 31.18 + All instructions have an opcode in the first 6 bits. 31.19 + Instructions can have the following fields: 31.20 + `A' : 8 bits 31.21 + `B' : 9 bits 31.22 + `C' : 9 bits 31.23 + `Bx' : 18 bits (`B' and `C' together) 31.24 + `sBx' : signed Bx 31.25 + 31.26 + A signed argument is represented in excess K; that is, the number 31.27 + value is the unsigned value minus K. K is exactly the maximum value 31.28 + for that argument (so that -max is represented by 0, and +max is 31.29 + represented by 2*max), which is half the maximum for the corresponding 31.30 + unsigned argument. 31.31 +===========================================================================*/ 31.32 + 31.33 + 31.34 +enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */ 31.35 + 31.36 + 31.37 +/* 31.38 +** size and position of opcode arguments. 31.39 +*/ 31.40 +#define SIZE_C 9 31.41 +#define SIZE_B 9 31.42 +#define SIZE_Bx (SIZE_C + SIZE_B) 31.43 +#define SIZE_A 8 31.44 + 31.45 +#define SIZE_OP 6 31.46 + 31.47 +#define POS_OP 0 31.48 +#define POS_A (POS_OP + SIZE_OP) 31.49 +#define POS_C (POS_A + SIZE_A) 31.50 +#define POS_B (POS_C + SIZE_C) 31.51 +#define POS_Bx POS_C 31.52 + 31.53 + 31.54 +/* 31.55 +** limits for opcode arguments. 31.56 +** we use (signed) int to manipulate most arguments, 31.57 +** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) 31.58 +*/ 31.59 +#if SIZE_Bx < LUAI_BITSINT-1 31.60 +#define MAXARG_Bx ((1<<SIZE_Bx)-1) 31.61 +#define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */ 31.62 +#else 31.63 +#define MAXARG_Bx MAX_INT 31.64 +#define MAXARG_sBx MAX_INT 31.65 +#endif 31.66 + 31.67 + 31.68 +#define MAXARG_A ((1<<SIZE_A)-1) 31.69 +#define MAXARG_B ((1<<SIZE_B)-1) 31.70 +#define MAXARG_C ((1<<SIZE_C)-1) 31.71 + 31.72 + 31.73 +/* creates a mask with `n' 1 bits at position `p' */ 31.74 +#define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p) 31.75 + 31.76 +/* creates a mask with `n' 0 bits at position `p' */ 31.77 +#define MASK0(n,p) (~MASK1(n,p)) 31.78 + 31.79 +/* 31.80 +** the following macros help to manipulate instructions 31.81 +*/ 31.82 + 31.83 +#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0))) 31.84 +#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ 31.85 + ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP)))) 31.86 + 31.87 +#define GETARG_A(i) (cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0))) 31.88 +#define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ 31.89 + ((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A)))) 31.90 + 31.91 +#define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0))) 31.92 +#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ 31.93 + ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B)))) 31.94 + 31.95 +#define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0))) 31.96 +#define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \ 31.97 + ((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C)))) 31.98 + 31.99 +#define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0))) 31.100 +#define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \ 31.101 + ((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx)))) 31.102 + 31.103 +#define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx) 31.104 +#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx)) 31.105 + 31.106 + 31.107 +#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \ 31.108 + | (cast(Instruction, a)<<POS_A) \ 31.109 + | (cast(Instruction, b)<<POS_B) \ 31.110 + | (cast(Instruction, c)<<POS_C)) 31.111 + 31.112 +#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \ 31.113 + | (cast(Instruction, a)<<POS_A) \ 31.114 + | (cast(Instruction, bc)<<POS_Bx)) 31.115 + 31.116 + 31.117 +/* 31.118 +** Macros to operate RK indices 31.119 +*/ 31.120 + 31.121 +/* this bit 1 means constant (0 means register) */ 31.122 +#define BITRK (1 << (SIZE_B - 1)) 31.123 + 31.124 +/* test whether value is a constant */ 31.125 +#define ISK(x) ((x) & BITRK) 31.126 + 31.127 +/* gets the index of the constant */ 31.128 +#define INDEXK(r) ((int)(r) & ~BITRK) 31.129 + 31.130 +#define MAXINDEXRK (BITRK - 1) 31.131 + 31.132 +/* code a constant index as a RK value */ 31.133 +#define RKASK(x) ((x) | BITRK) 31.134 + 31.135 + 31.136 +/* 31.137 +** invalid register that fits in 8 bits 31.138 +*/ 31.139 +#define NO_REG MAXARG_A 31.140 + 31.141 + 31.142 +/* 31.143 +** R(x) - register 31.144 +** Kst(x) - constant (in constant table) 31.145 +** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x) 31.146 +*/ 31.147 + 31.148 + 31.149 +/* 31.150 +** grep "ORDER OP" if you change these enums 31.151 +*/ 31.152 + 31.153 +typedef enum { 31.154 +/*---------------------------------------------------------------------- 31.155 +name args description 31.156 +------------------------------------------------------------------------*/ 31.157 +OP_MOVE,/* A B R(A) := R(B) */ 31.158 +OP_LOADK,/* A Bx R(A) := Kst(Bx) */ 31.159 +OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ 31.160 +OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */ 31.161 +OP_GETUPVAL,/* A B R(A) := UpValue[B] */ 31.162 + 31.163 +OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */ 31.164 +OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */ 31.165 + 31.166 +OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */ 31.167 +OP_SETUPVAL,/* A B UpValue[B] := R(A) */ 31.168 +OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */ 31.169 + 31.170 +OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ 31.171 + 31.172 +OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ 31.173 + 31.174 +OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ 31.175 +OP_SUB,/* A B C R(A) := RK(B) - RK(C) */ 31.176 +OP_MUL,/* A B C R(A) := RK(B) * RK(C) */ 31.177 +OP_DIV,/* A B C R(A) := RK(B) / RK(C) */ 31.178 +OP_MOD,/* A B C R(A) := RK(B) % RK(C) */ 31.179 +OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */ 31.180 +OP_UNM,/* A B R(A) := -R(B) */ 31.181 +OP_NOT,/* A B R(A) := not R(B) */ 31.182 +OP_LEN,/* A B R(A) := length of R(B) */ 31.183 + 31.184 +OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ 31.185 + 31.186 +OP_JMP,/* sBx pc+=sBx */ 31.187 + 31.188 +OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ 31.189 +OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ 31.190 +OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ 31.191 + 31.192 +OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ 31.193 +OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ 31.194 + 31.195 +OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ 31.196 +OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ 31.197 +OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ 31.198 + 31.199 +OP_FORLOOP,/* A sBx R(A)+=R(A+2); 31.200 + if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/ 31.201 +OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */ 31.202 + 31.203 +OP_TFORLOOP,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); 31.204 + if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */ 31.205 +OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ 31.206 + 31.207 +OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/ 31.208 +OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ 31.209 + 31.210 +OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ 31.211 +} OpCode; 31.212 + 31.213 + 31.214 +#define NUM_OPCODES (cast(int, OP_VARARG) + 1) 31.215 + 31.216 + 31.217 + 31.218 +/*=========================================================================== 31.219 + Notes: 31.220 + (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1, 31.221 + and can be 0: OP_CALL then sets `top' to last_result+1, so 31.222 + next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'. 31.223 + 31.224 + (*) In OP_VARARG, if (B == 0) then use actual number of varargs and 31.225 + set top (like in OP_CALL with C == 0). 31.226 + 31.227 + (*) In OP_RETURN, if (B == 0) then return up to `top' 31.228 + 31.229 + (*) In OP_SETLIST, if (B == 0) then B = `top'; 31.230 + if (C == 0) then next `instruction' is real C 31.231 + 31.232 + (*) For comparisons, A specifies what condition the test should accept 31.233 + (true or false). 31.234 + 31.235 + (*) All `skips' (pc++) assume that next instruction is a jump 31.236 +===========================================================================*/ 31.237 + 31.238 + 31.239 +/* 31.240 +** masks for instruction properties. The format is: 31.241 +** bits 0-1: op mode 31.242 +** bits 2-3: C arg mode 31.243 +** bits 4-5: B arg mode 31.244 +** bit 6: instruction set register A 31.245 +** bit 7: operator is a test 31.246 +*/ 31.247 + 31.248 +enum OpArgMask { 31.249 + OpArgN, /* argument is not used */ 31.250 + OpArgU, /* argument is used */ 31.251 + OpArgR, /* argument is a register or a jump offset */ 31.252 + OpArgK /* argument is a constant or register/constant */ 31.253 +}; 31.254 + 31.255 +LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES]; 31.256 + 31.257 +#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3)) 31.258 +#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3)) 31.259 +#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) 31.260 +#define testAMode(m) (luaP_opmodes[m] & (1 << 6)) 31.261 +#define testTMode(m) (luaP_opmodes[m] & (1 << 7)) 31.262 + 31.263 + 31.264 +LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ 31.265 + 31.266 + 31.267 +/* number of list items to accumulate before a SETLIST instruction */ 31.268 +#define LFIELDS_PER_FLUSH 50 31.269 + 31.270 + 31.271 +#endif
32.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 32.2 +++ b/src/lua/loslib.c Sat Mar 03 11:07:39 2012 -0600 32.3 @@ -0,0 +1,243 @@ 32.4 +/* 32.5 +** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $ 32.6 +** Standard Operating System library 32.7 +** See Copyright Notice in lua.h 32.8 +*/ 32.9 + 32.10 + 32.11 +#include <errno.h> 32.12 +#include <locale.h> 32.13 +#include <stdlib.h> 32.14 +#include <string.h> 32.15 +#include <time.h> 32.16 + 32.17 +#define loslib_c 32.18 +#define LUA_LIB 32.19 + 32.20 +#include "lua.h" 32.21 + 32.22 +#include "lauxlib.h" 32.23 +#include "lualib.h" 32.24 + 32.25 + 32.26 +static int os_pushresult (lua_State *L, int i, const char *filename) { 32.27 + int en = errno; /* calls to Lua API may change this value */ 32.28 + if (i) { 32.29 + lua_pushboolean(L, 1); 32.30 + return 1; 32.31 + } 32.32 + else { 32.33 + lua_pushnil(L); 32.34 + lua_pushfstring(L, "%s: %s", filename, strerror(en)); 32.35 + lua_pushinteger(L, en); 32.36 + return 3; 32.37 + } 32.38 +} 32.39 + 32.40 + 32.41 +static int os_execute (lua_State *L) { 32.42 + lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); 32.43 + return 1; 32.44 +} 32.45 + 32.46 + 32.47 +static int os_remove (lua_State *L) { 32.48 + const char *filename = luaL_checkstring(L, 1); 32.49 + return os_pushresult(L, remove(filename) == 0, filename); 32.50 +} 32.51 + 32.52 + 32.53 +static int os_rename (lua_State *L) { 32.54 + const char *fromname = luaL_checkstring(L, 1); 32.55 + const char *toname = luaL_checkstring(L, 2); 32.56 + return os_pushresult(L, rename(fromname, toname) == 0, fromname); 32.57 +} 32.58 + 32.59 + 32.60 +static int os_tmpname (lua_State *L) { 32.61 + char buff[LUA_TMPNAMBUFSIZE]; 32.62 + int err; 32.63 + lua_tmpnam(buff, err); 32.64 + if (err) 32.65 + return luaL_error(L, "unable to generate a unique filename"); 32.66 + lua_pushstring(L, buff); 32.67 + return 1; 32.68 +} 32.69 + 32.70 + 32.71 +static int os_getenv (lua_State *L) { 32.72 + lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ 32.73 + return 1; 32.74 +} 32.75 + 32.76 + 32.77 +static int os_clock (lua_State *L) { 32.78 + lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); 32.79 + return 1; 32.80 +} 32.81 + 32.82 + 32.83 +/* 32.84 +** {====================================================== 32.85 +** Time/Date operations 32.86 +** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, 32.87 +** wday=%w+1, yday=%j, isdst=? } 32.88 +** ======================================================= 32.89 +*/ 32.90 + 32.91 +static void setfield (lua_State *L, const char *key, int value) { 32.92 + lua_pushinteger(L, value); 32.93 + lua_setfield(L, -2, key); 32.94 +} 32.95 + 32.96 +static void setboolfield (lua_State *L, const char *key, int value) { 32.97 + if (value < 0) /* undefined? */ 32.98 + return; /* does not set field */ 32.99 + lua_pushboolean(L, value); 32.100 + lua_setfield(L, -2, key); 32.101 +} 32.102 + 32.103 +static int getboolfield (lua_State *L, const char *key) { 32.104 + int res; 32.105 + lua_getfield(L, -1, key); 32.106 + res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); 32.107 + lua_pop(L, 1); 32.108 + return res; 32.109 +} 32.110 + 32.111 + 32.112 +static int getfield (lua_State *L, const char *key, int d) { 32.113 + int res; 32.114 + lua_getfield(L, -1, key); 32.115 + if (lua_isnumber(L, -1)) 32.116 + res = (int)lua_tointeger(L, -1); 32.117 + else { 32.118 + if (d < 0) 32.119 + return luaL_error(L, "field " LUA_QS " missing in date table", key); 32.120 + res = d; 32.121 + } 32.122 + lua_pop(L, 1); 32.123 + return res; 32.124 +} 32.125 + 32.126 + 32.127 +static int os_date (lua_State *L) { 32.128 + const char *s = luaL_optstring(L, 1, "%c"); 32.129 + time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); 32.130 + struct tm *stm; 32.131 + if (*s == '!') { /* UTC? */ 32.132 + stm = gmtime(&t); 32.133 + s++; /* skip `!' */ 32.134 + } 32.135 + else 32.136 + stm = localtime(&t); 32.137 + if (stm == NULL) /* invalid date? */ 32.138 + lua_pushnil(L); 32.139 + else if (strcmp(s, "*t") == 0) { 32.140 + lua_createtable(L, 0, 9); /* 9 = number of fields */ 32.141 + setfield(L, "sec", stm->tm_sec); 32.142 + setfield(L, "min", stm->tm_min); 32.143 + setfield(L, "hour", stm->tm_hour); 32.144 + setfield(L, "day", stm->tm_mday); 32.145 + setfield(L, "month", stm->tm_mon+1); 32.146 + setfield(L, "year", stm->tm_year+1900); 32.147 + setfield(L, "wday", stm->tm_wday+1); 32.148 + setfield(L, "yday", stm->tm_yday+1); 32.149 + setboolfield(L, "isdst", stm->tm_isdst); 32.150 + } 32.151 + else { 32.152 + char cc[3]; 32.153 + luaL_Buffer b; 32.154 + cc[0] = '%'; cc[2] = '\0'; 32.155 + luaL_buffinit(L, &b); 32.156 + for (; *s; s++) { 32.157 + if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ 32.158 + luaL_addchar(&b, *s); 32.159 + else { 32.160 + size_t reslen; 32.161 + char buff[200]; /* should be big enough for any conversion result */ 32.162 + cc[1] = *(++s); 32.163 + reslen = strftime(buff, sizeof(buff), cc, stm); 32.164 + luaL_addlstring(&b, buff, reslen); 32.165 + } 32.166 + } 32.167 + luaL_pushresult(&b); 32.168 + } 32.169 + return 1; 32.170 +} 32.171 + 32.172 + 32.173 +static int os_time (lua_State *L) { 32.174 + time_t t; 32.175 + if (lua_isnoneornil(L, 1)) /* called without args? */ 32.176 + t = time(NULL); /* get current time */ 32.177 + else { 32.178 + struct tm ts; 32.179 + luaL_checktype(L, 1, LUA_TTABLE); 32.180 + lua_settop(L, 1); /* make sure table is at the top */ 32.181 + ts.tm_sec = getfield(L, "sec", 0); 32.182 + ts.tm_min = getfield(L, "min", 0); 32.183 + ts.tm_hour = getfield(L, "hour", 12); 32.184 + ts.tm_mday = getfield(L, "day", -1); 32.185 + ts.tm_mon = getfield(L, "month", -1) - 1; 32.186 + ts.tm_year = getfield(L, "year", -1) - 1900; 32.187 + ts.tm_isdst = getboolfield(L, "isdst"); 32.188 + t = mktime(&ts); 32.189 + } 32.190 + if (t == (time_t)(-1)) 32.191 + lua_pushnil(L); 32.192 + else 32.193 + lua_pushnumber(L, (lua_Number)t); 32.194 + return 1; 32.195 +} 32.196 + 32.197 + 32.198 +static int os_difftime (lua_State *L) { 32.199 + lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), 32.200 + (time_t)(luaL_optnumber(L, 2, 0)))); 32.201 + return 1; 32.202 +} 32.203 + 32.204 +/* }====================================================== */ 32.205 + 32.206 + 32.207 +static int os_setlocale (lua_State *L) { 32.208 + static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, 32.209 + LC_NUMERIC, LC_TIME}; 32.210 + static const char *const catnames[] = {"all", "collate", "ctype", "monetary", 32.211 + "numeric", "time", NULL}; 32.212 + const char *l = luaL_optstring(L, 1, NULL); 32.213 + int op = luaL_checkoption(L, 2, "all", catnames); 32.214 + lua_pushstring(L, setlocale(cat[op], l)); 32.215 + return 1; 32.216 +} 32.217 + 32.218 + 32.219 +static int os_exit (lua_State *L) { 32.220 + exit(luaL_optint(L, 1, EXIT_SUCCESS)); 32.221 +} 32.222 + 32.223 +static const luaL_Reg syslib[] = { 32.224 + {"clock", os_clock}, 32.225 + {"date", os_date}, 32.226 + {"difftime", os_difftime}, 32.227 + {"execute", os_execute}, 32.228 + {"exit", os_exit}, 32.229 + {"getenv", os_getenv}, 32.230 + {"remove", os_remove}, 32.231 + {"rename", os_rename}, 32.232 + {"setlocale", os_setlocale}, 32.233 + {"time", os_time}, 32.234 + {"tmpname", os_tmpname}, 32.235 + {NULL, NULL} 32.236 +}; 32.237 + 32.238 +/* }====================================================== */ 32.239 + 32.240 + 32.241 + 32.242 +LUALIB_API int luaopen_os (lua_State *L) { 32.243 + luaL_register(L, LUA_OSLIBNAME, syslib); 32.244 + return 1; 32.245 +} 32.246 +
33.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 33.2 +++ b/src/lua/lparser.c Sat Mar 03 11:07:39 2012 -0600 33.3 @@ -0,0 +1,1339 @@ 33.4 +/* 33.5 +** $Id: lparser.c,v 2.42.1.3 2007/12/28 15:32:23 roberto Exp $ 33.6 +** Lua Parser 33.7 +** See Copyright Notice in lua.h 33.8 +*/ 33.9 + 33.10 + 33.11 +#include <string.h> 33.12 + 33.13 +#define lparser_c 33.14 +#define LUA_CORE 33.15 + 33.16 +#include "lua.h" 33.17 + 33.18 +#include "lcode.h" 33.19 +#include "ldebug.h" 33.20 +#include "ldo.h" 33.21 +#include "lfunc.h" 33.22 +#include "llex.h" 33.23 +#include "lmem.h" 33.24 +#include "lobject.h" 33.25 +#include "lopcodes.h" 33.26 +#include "lparser.h" 33.27 +#include "lstate.h" 33.28 +#include "lstring.h" 33.29 +#include "ltable.h" 33.30 + 33.31 + 33.32 + 33.33 +#define hasmultret(k) ((k) == VCALL || (k) == VVARARG) 33.34 + 33.35 +#define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]]) 33.36 + 33.37 +#define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m) 33.38 + 33.39 + 33.40 +/* 33.41 +** nodes for block list (list of active blocks) 33.42 +*/ 33.43 +typedef struct BlockCnt { 33.44 + struct BlockCnt *previous; /* chain */ 33.45 + int breaklist; /* list of jumps out of this loop */ 33.46 + lu_byte nactvar; /* # active locals outside the breakable structure */ 33.47 + lu_byte upval; /* true if some variable in the block is an upvalue */ 33.48 + lu_byte isbreakable; /* true if `block' is a loop */ 33.49 +} BlockCnt; 33.50 + 33.51 + 33.52 + 33.53 +/* 33.54 +** prototypes for recursive non-terminal functions 33.55 +*/ 33.56 +static void chunk (LexState *ls); 33.57 +static void expr (LexState *ls, expdesc *v); 33.58 + 33.59 + 33.60 +static void anchor_token (LexState *ls) { 33.61 + if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { 33.62 + TString *ts = ls->t.seminfo.ts; 33.63 + luaX_newstring(ls, getstr(ts), ts->tsv.len); 33.64 + } 33.65 +} 33.66 + 33.67 + 33.68 +static void error_expected (LexState *ls, int token) { 33.69 + luaX_syntaxerror(ls, 33.70 + luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token))); 33.71 +} 33.72 + 33.73 + 33.74 +static void errorlimit (FuncState *fs, int limit, const char *what) { 33.75 + const char *msg = (fs->f->linedefined == 0) ? 33.76 + luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) : 33.77 + luaO_pushfstring(fs->L, "function at line %d has more than %d %s", 33.78 + fs->f->linedefined, limit, what); 33.79 + luaX_lexerror(fs->ls, msg, 0); 33.80 +} 33.81 + 33.82 + 33.83 +static int testnext (LexState *ls, int c) { 33.84 + if (ls->t.token == c) { 33.85 + luaX_next(ls); 33.86 + return 1; 33.87 + } 33.88 + else return 0; 33.89 +} 33.90 + 33.91 + 33.92 +static void check (LexState *ls, int c) { 33.93 + if (ls->t.token != c) 33.94 + error_expected(ls, c); 33.95 +} 33.96 + 33.97 +static void checknext (LexState *ls, int c) { 33.98 + check(ls, c); 33.99 + luaX_next(ls); 33.100 +} 33.101 + 33.102 + 33.103 +#define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } 33.104 + 33.105 + 33.106 + 33.107 +static void check_match (LexState *ls, int what, int who, int where) { 33.108 + if (!testnext(ls, what)) { 33.109 + if (where == ls->linenumber) 33.110 + error_expected(ls, what); 33.111 + else { 33.112 + luaX_syntaxerror(ls, luaO_pushfstring(ls->L, 33.113 + LUA_QS " expected (to close " LUA_QS " at line %d)", 33.114 + luaX_token2str(ls, what), luaX_token2str(ls, who), where)); 33.115 + } 33.116 + } 33.117 +} 33.118 + 33.119 + 33.120 +static TString *str_checkname (LexState *ls) { 33.121 + TString *ts; 33.122 + check(ls, TK_NAME); 33.123 + ts = ls->t.seminfo.ts; 33.124 + luaX_next(ls); 33.125 + return ts; 33.126 +} 33.127 + 33.128 + 33.129 +static void init_exp (expdesc *e, expkind k, int i) { 33.130 + e->f = e->t = NO_JUMP; 33.131 + e->k = k; 33.132 + e->u.s.info = i; 33.133 +} 33.134 + 33.135 + 33.136 +static void codestring (LexState *ls, expdesc *e, TString *s) { 33.137 + init_exp(e, VK, luaK_stringK(ls->fs, s)); 33.138 +} 33.139 + 33.140 + 33.141 +static void checkname(LexState *ls, expdesc *e) { 33.142 + codestring(ls, e, str_checkname(ls)); 33.143 +} 33.144 + 33.145 + 33.146 +static int registerlocalvar (LexState *ls, TString *varname) { 33.147 + FuncState *fs = ls->fs; 33.148 + Proto *f = fs->f; 33.149 + int oldsize = f->sizelocvars; 33.150 + luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, 33.151 + LocVar, SHRT_MAX, "too many local variables"); 33.152 + while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; 33.153 + f->locvars[fs->nlocvars].varname = varname; 33.154 + luaC_objbarrier(ls->L, f, varname); 33.155 + return fs->nlocvars++; 33.156 +} 33.157 + 33.158 + 33.159 +#define new_localvarliteral(ls,v,n) \ 33.160 + new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n) 33.161 + 33.162 + 33.163 +static void new_localvar (LexState *ls, TString *name, int n) { 33.164 + FuncState *fs = ls->fs; 33.165 + luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables"); 33.166 + fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name)); 33.167 +} 33.168 + 33.169 + 33.170 +static void adjustlocalvars (LexState *ls, int nvars) { 33.171 + FuncState *fs = ls->fs; 33.172 + fs->nactvar = cast_byte(fs->nactvar + nvars); 33.173 + for (; nvars; nvars--) { 33.174 + getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc; 33.175 + } 33.176 +} 33.177 + 33.178 + 33.179 +static void removevars (LexState *ls, int tolevel) { 33.180 + FuncState *fs = ls->fs; 33.181 + while (fs->nactvar > tolevel) 33.182 + getlocvar(fs, --fs->nactvar).endpc = fs->pc; 33.183 +} 33.184 + 33.185 + 33.186 +static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { 33.187 + int i; 33.188 + Proto *f = fs->f; 33.189 + int oldsize = f->sizeupvalues; 33.190 + for (i=0; i<f->nups; i++) { 33.191 + if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) { 33.192 + lua_assert(f->upvalues[i] == name); 33.193 + return i; 33.194 + } 33.195 + } 33.196 + /* new one */ 33.197 + luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues"); 33.198 + luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues, 33.199 + TString *, MAX_INT, ""); 33.200 + while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL; 33.201 + f->upvalues[f->nups] = name; 33.202 + luaC_objbarrier(fs->L, f, name); 33.203 + lua_assert(v->k == VLOCAL || v->k == VUPVAL); 33.204 + fs->upvalues[f->nups].k = cast_byte(v->k); 33.205 + fs->upvalues[f->nups].info = cast_byte(v->u.s.info); 33.206 + return f->nups++; 33.207 +} 33.208 + 33.209 + 33.210 +static int searchvar (FuncState *fs, TString *n) { 33.211 + int i; 33.212 + for (i=fs->nactvar-1; i >= 0; i--) { 33.213 + if (n == getlocvar(fs, i).varname) 33.214 + return i; 33.215 + } 33.216 + return -1; /* not found */ 33.217 +} 33.218 + 33.219 + 33.220 +static void markupval (FuncState *fs, int level) { 33.221 + BlockCnt *bl = fs->bl; 33.222 + while (bl && bl->nactvar > level) bl = bl->previous; 33.223 + if (bl) bl->upval = 1; 33.224 +} 33.225 + 33.226 + 33.227 +static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { 33.228 + if (fs == NULL) { /* no more levels? */ 33.229 + init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ 33.230 + return VGLOBAL; 33.231 + } 33.232 + else { 33.233 + int v = searchvar(fs, n); /* look up at current level */ 33.234 + if (v >= 0) { 33.235 + init_exp(var, VLOCAL, v); 33.236 + if (!base) 33.237 + markupval(fs, v); /* local will be used as an upval */ 33.238 + return VLOCAL; 33.239 + } 33.240 + else { /* not found at current level; try upper one */ 33.241 + if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL) 33.242 + return VGLOBAL; 33.243 + var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */ 33.244 + var->k = VUPVAL; /* upvalue in this level */ 33.245 + return VUPVAL; 33.246 + } 33.247 + } 33.248 +} 33.249 + 33.250 + 33.251 +static void singlevar (LexState *ls, expdesc *var) { 33.252 + TString *varname = str_checkname(ls); 33.253 + FuncState *fs = ls->fs; 33.254 + if (singlevaraux(fs, varname, var, 1) == VGLOBAL) 33.255 + var->u.s.info = luaK_stringK(fs, varname); /* info points to global name */ 33.256 +} 33.257 + 33.258 + 33.259 +static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { 33.260 + FuncState *fs = ls->fs; 33.261 + int extra = nvars - nexps; 33.262 + if (hasmultret(e->k)) { 33.263 + extra++; /* includes call itself */ 33.264 + if (extra < 0) extra = 0; 33.265 + luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ 33.266 + if (extra > 1) luaK_reserveregs(fs, extra-1); 33.267 + } 33.268 + else { 33.269 + if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ 33.270 + if (extra > 0) { 33.271 + int reg = fs->freereg; 33.272 + luaK_reserveregs(fs, extra); 33.273 + luaK_nil(fs, reg, extra); 33.274 + } 33.275 + } 33.276 +} 33.277 + 33.278 + 33.279 +static void enterlevel (LexState *ls) { 33.280 + if (++ls->L->nCcalls > LUAI_MAXCCALLS) 33.281 + luaX_lexerror(ls, "chunk has too many syntax levels", 0); 33.282 +} 33.283 + 33.284 + 33.285 +#define leavelevel(ls) ((ls)->L->nCcalls--) 33.286 + 33.287 + 33.288 +static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) { 33.289 + bl->breaklist = NO_JUMP; 33.290 + bl->isbreakable = isbreakable; 33.291 + bl->nactvar = fs->nactvar; 33.292 + bl->upval = 0; 33.293 + bl->previous = fs->bl; 33.294 + fs->bl = bl; 33.295 + lua_assert(fs->freereg == fs->nactvar); 33.296 +} 33.297 + 33.298 + 33.299 +static void leaveblock (FuncState *fs) { 33.300 + BlockCnt *bl = fs->bl; 33.301 + fs->bl = bl->previous; 33.302 + removevars(fs->ls, bl->nactvar); 33.303 + if (bl->upval) 33.304 + luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); 33.305 + /* a block either controls scope or breaks (never both) */ 33.306 + lua_assert(!bl->isbreakable || !bl->upval); 33.307 + lua_assert(bl->nactvar == fs->nactvar); 33.308 + fs->freereg = fs->nactvar; /* free registers */ 33.309 + luaK_patchtohere(fs, bl->breaklist); 33.310 +} 33.311 + 33.312 + 33.313 +static void pushclosure (LexState *ls, FuncState *func, expdesc *v) { 33.314 + FuncState *fs = ls->fs; 33.315 + Proto *f = fs->f; 33.316 + int oldsize = f->sizep; 33.317 + int i; 33.318 + luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *, 33.319 + MAXARG_Bx, "constant table overflow"); 33.320 + while (oldsize < f->sizep) f->p[oldsize++] = NULL; 33.321 + f->p[fs->np++] = func->f; 33.322 + luaC_objbarrier(ls->L, f, func->f); 33.323 + init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1)); 33.324 + for (i=0; i<func->f->nups; i++) { 33.325 + OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; 33.326 + luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0); 33.327 + } 33.328 +} 33.329 + 33.330 + 33.331 +static void open_func (LexState *ls, FuncState *fs) { 33.332 + lua_State *L = ls->L; 33.333 + Proto *f = luaF_newproto(L); 33.334 + fs->f = f; 33.335 + fs->prev = ls->fs; /* linked list of funcstates */ 33.336 + fs->ls = ls; 33.337 + fs->L = L; 33.338 + ls->fs = fs; 33.339 + fs->pc = 0; 33.340 + fs->lasttarget = -1; 33.341 + fs->jpc = NO_JUMP; 33.342 + fs->freereg = 0; 33.343 + fs->nk = 0; 33.344 + fs->np = 0; 33.345 + fs->nlocvars = 0; 33.346 + fs->nactvar = 0; 33.347 + fs->bl = NULL; 33.348 + f->source = ls->source; 33.349 + f->maxstacksize = 2; /* registers 0/1 are always valid */ 33.350 + fs->h = luaH_new(L, 0, 0); 33.351 + /* anchor table of constants and prototype (to avoid being collected) */ 33.352 + sethvalue2s(L, L->top, fs->h); 33.353 + incr_top(L); 33.354 + setptvalue2s(L, L->top, f); 33.355 + incr_top(L); 33.356 +} 33.357 + 33.358 + 33.359 +static void close_func (LexState *ls) { 33.360 + lua_State *L = ls->L; 33.361 + FuncState *fs = ls->fs; 33.362 + Proto *f = fs->f; 33.363 + removevars(ls, 0); 33.364 + luaK_ret(fs, 0, 0); /* final return */ 33.365 + luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); 33.366 + f->sizecode = fs->pc; 33.367 + luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); 33.368 + f->sizelineinfo = fs->pc; 33.369 + luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); 33.370 + f->sizek = fs->nk; 33.371 + luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); 33.372 + f->sizep = fs->np; 33.373 + luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); 33.374 + f->sizelocvars = fs->nlocvars; 33.375 + luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *); 33.376 + f->sizeupvalues = f->nups; 33.377 + lua_assert(luaG_checkcode(f)); 33.378 + lua_assert(fs->bl == NULL); 33.379 + ls->fs = fs->prev; 33.380 + L->top -= 2; /* remove table and prototype from the stack */ 33.381 + /* last token read was anchored in defunct function; must reanchor it */ 33.382 + if (fs) anchor_token(ls); 33.383 +} 33.384 + 33.385 + 33.386 +Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { 33.387 + struct LexState lexstate; 33.388 + struct FuncState funcstate; 33.389 + lexstate.buff = buff; 33.390 + luaX_setinput(L, &lexstate, z, luaS_new(L, name)); 33.391 + open_func(&lexstate, &funcstate); 33.392 + funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ 33.393 + luaX_next(&lexstate); /* read first token */ 33.394 + chunk(&lexstate); 33.395 + check(&lexstate, TK_EOS); 33.396 + close_func(&lexstate); 33.397 + lua_assert(funcstate.prev == NULL); 33.398 + lua_assert(funcstate.f->nups == 0); 33.399 + lua_assert(lexstate.fs == NULL); 33.400 + return funcstate.f; 33.401 +} 33.402 + 33.403 + 33.404 + 33.405 +/*============================================================*/ 33.406 +/* GRAMMAR RULES */ 33.407 +/*============================================================*/ 33.408 + 33.409 + 33.410 +static void field (LexState *ls, expdesc *v) { 33.411 + /* field -> ['.' | ':'] NAME */ 33.412 + FuncState *fs = ls->fs; 33.413 + expdesc key; 33.414 + luaK_exp2anyreg(fs, v); 33.415 + luaX_next(ls); /* skip the dot or colon */ 33.416 + checkname(ls, &key); 33.417 + luaK_indexed(fs, v, &key); 33.418 +} 33.419 + 33.420 + 33.421 +static void yindex (LexState *ls, expdesc *v) { 33.422 + /* index -> '[' expr ']' */ 33.423 + luaX_next(ls); /* skip the '[' */ 33.424 + expr(ls, v); 33.425 + luaK_exp2val(ls->fs, v); 33.426 + checknext(ls, ']'); 33.427 +} 33.428 + 33.429 + 33.430 +/* 33.431 +** {====================================================================== 33.432 +** Rules for Constructors 33.433 +** ======================================================================= 33.434 +*/ 33.435 + 33.436 + 33.437 +struct ConsControl { 33.438 + expdesc v; /* last list item read */ 33.439 + expdesc *t; /* table descriptor */ 33.440 + int nh; /* total number of `record' elements */ 33.441 + int na; /* total number of array elements */ 33.442 + int tostore; /* number of array elements pending to be stored */ 33.443 +}; 33.444 + 33.445 + 33.446 +static void recfield (LexState *ls, struct ConsControl *cc) { 33.447 + /* recfield -> (NAME | `['exp1`]') = exp1 */ 33.448 + FuncState *fs = ls->fs; 33.449 + int reg = ls->fs->freereg; 33.450 + expdesc key, val; 33.451 + int rkkey; 33.452 + if (ls->t.token == TK_NAME) { 33.453 + luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); 33.454 + checkname(ls, &key); 33.455 + } 33.456 + else /* ls->t.token == '[' */ 33.457 + yindex(ls, &key); 33.458 + cc->nh++; 33.459 + checknext(ls, '='); 33.460 + rkkey = luaK_exp2RK(fs, &key); 33.461 + expr(ls, &val); 33.462 + luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val)); 33.463 + fs->freereg = reg; /* free registers */ 33.464 +} 33.465 + 33.466 + 33.467 +static void closelistfield (FuncState *fs, struct ConsControl *cc) { 33.468 + if (cc->v.k == VVOID) return; /* there is no list item */ 33.469 + luaK_exp2nextreg(fs, &cc->v); 33.470 + cc->v.k = VVOID; 33.471 + if (cc->tostore == LFIELDS_PER_FLUSH) { 33.472 + luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); /* flush */ 33.473 + cc->tostore = 0; /* no more items pending */ 33.474 + } 33.475 +} 33.476 + 33.477 + 33.478 +static void lastlistfield (FuncState *fs, struct ConsControl *cc) { 33.479 + if (cc->tostore == 0) return; 33.480 + if (hasmultret(cc->v.k)) { 33.481 + luaK_setmultret(fs, &cc->v); 33.482 + luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET); 33.483 + cc->na--; /* do not count last expression (unknown number of elements) */ 33.484 + } 33.485 + else { 33.486 + if (cc->v.k != VVOID) 33.487 + luaK_exp2nextreg(fs, &cc->v); 33.488 + luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); 33.489 + } 33.490 +} 33.491 + 33.492 + 33.493 +static void listfield (LexState *ls, struct ConsControl *cc) { 33.494 + expr(ls, &cc->v); 33.495 + luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); 33.496 + cc->na++; 33.497 + cc->tostore++; 33.498 +} 33.499 + 33.500 + 33.501 +static void constructor (LexState *ls, expdesc *t) { 33.502 + /* constructor -> ?? */ 33.503 + FuncState *fs = ls->fs; 33.504 + int line = ls->linenumber; 33.505 + int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); 33.506 + struct ConsControl cc; 33.507 + cc.na = cc.nh = cc.tostore = 0; 33.508 + cc.t = t; 33.509 + init_exp(t, VRELOCABLE, pc); 33.510 + init_exp(&cc.v, VVOID, 0); /* no value (yet) */ 33.511 + luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ 33.512 + checknext(ls, '{'); 33.513 + do { 33.514 + lua_assert(cc.v.k == VVOID || cc.tostore > 0); 33.515 + if (ls->t.token == '}') break; 33.516 + closelistfield(fs, &cc); 33.517 + switch(ls->t.token) { 33.518 + case TK_NAME: { /* may be listfields or recfields */ 33.519 + luaX_lookahead(ls); 33.520 + if (ls->lookahead.token != '=') /* expression? */ 33.521 + listfield(ls, &cc); 33.522 + else 33.523 + recfield(ls, &cc); 33.524 + break; 33.525 + } 33.526 + case '[': { /* constructor_item -> recfield */ 33.527 + recfield(ls, &cc); 33.528 + break; 33.529 + } 33.530 + default: { /* constructor_part -> listfield */ 33.531 + listfield(ls, &cc); 33.532 + break; 33.533 + } 33.534 + } 33.535 + } while (testnext(ls, ',') || testnext(ls, ';')); 33.536 + check_match(ls, '}', '{', line); 33.537 + lastlistfield(fs, &cc); 33.538 + SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ 33.539 + SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ 33.540 +} 33.541 + 33.542 +/* }====================================================================== */ 33.543 + 33.544 + 33.545 + 33.546 +static void parlist (LexState *ls) { 33.547 + /* parlist -> [ param { `,' param } ] */ 33.548 + FuncState *fs = ls->fs; 33.549 + Proto *f = fs->f; 33.550 + int nparams = 0; 33.551 + f->is_vararg = 0; 33.552 + if (ls->t.token != ')') { /* is `parlist' not empty? */ 33.553 + do { 33.554 + switch (ls->t.token) { 33.555 + case TK_NAME: { /* param -> NAME */ 33.556 + new_localvar(ls, str_checkname(ls), nparams++); 33.557 + break; 33.558 + } 33.559 + case TK_DOTS: { /* param -> `...' */ 33.560 + luaX_next(ls); 33.561 +#if defined(LUA_COMPAT_VARARG) 33.562 + /* use `arg' as default name */ 33.563 + new_localvarliteral(ls, "arg", nparams++); 33.564 + f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG; 33.565 +#endif 33.566 + f->is_vararg |= VARARG_ISVARARG; 33.567 + break; 33.568 + } 33.569 + default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected"); 33.570 + } 33.571 + } while (!f->is_vararg && testnext(ls, ',')); 33.572 + } 33.573 + adjustlocalvars(ls, nparams); 33.574 + f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG)); 33.575 + luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ 33.576 +} 33.577 + 33.578 + 33.579 +static void body (LexState *ls, expdesc *e, int needself, int line) { 33.580 + /* body -> `(' parlist `)' chunk END */ 33.581 + FuncState new_fs; 33.582 + open_func(ls, &new_fs); 33.583 + new_fs.f->linedefined = line; 33.584 + checknext(ls, '('); 33.585 + if (needself) { 33.586 + new_localvarliteral(ls, "self", 0); 33.587 + adjustlocalvars(ls, 1); 33.588 + } 33.589 + parlist(ls); 33.590 + checknext(ls, ')'); 33.591 + chunk(ls); 33.592 + new_fs.f->lastlinedefined = ls->linenumber; 33.593 + check_match(ls, TK_END, TK_FUNCTION, line); 33.594 + close_func(ls); 33.595 + pushclosure(ls, &new_fs, e); 33.596 +} 33.597 + 33.598 + 33.599 +static int explist1 (LexState *ls, expdesc *v) { 33.600 + /* explist1 -> expr { `,' expr } */ 33.601 + int n = 1; /* at least one expression */ 33.602 + expr(ls, v); 33.603 + while (testnext(ls, ',')) { 33.604 + luaK_exp2nextreg(ls->fs, v); 33.605 + expr(ls, v); 33.606 + n++; 33.607 + } 33.608 + return n; 33.609 +} 33.610 + 33.611 + 33.612 +static void funcargs (LexState *ls, expdesc *f) { 33.613 + FuncState *fs = ls->fs; 33.614 + expdesc args; 33.615 + int base, nparams; 33.616 + int line = ls->linenumber; 33.617 + switch (ls->t.token) { 33.618 + case '(': { /* funcargs -> `(' [ explist1 ] `)' */ 33.619 + if (line != ls->lastline) 33.620 + luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); 33.621 + luaX_next(ls); 33.622 + if (ls->t.token == ')') /* arg list is empty? */ 33.623 + args.k = VVOID; 33.624 + else { 33.625 + explist1(ls, &args); 33.626 + luaK_setmultret(fs, &args); 33.627 + } 33.628 + check_match(ls, ')', '(', line); 33.629 + break; 33.630 + } 33.631 + case '{': { /* funcargs -> constructor */ 33.632 + constructor(ls, &args); 33.633 + break; 33.634 + } 33.635 + case TK_STRING: { /* funcargs -> STRING */ 33.636 + codestring(ls, &args, ls->t.seminfo.ts); 33.637 + luaX_next(ls); /* must use `seminfo' before `next' */ 33.638 + break; 33.639 + } 33.640 + default: { 33.641 + luaX_syntaxerror(ls, "function arguments expected"); 33.642 + return; 33.643 + } 33.644 + } 33.645 + lua_assert(f->k == VNONRELOC); 33.646 + base = f->u.s.info; /* base register for call */ 33.647 + if (hasmultret(args.k)) 33.648 + nparams = LUA_MULTRET; /* open call */ 33.649 + else { 33.650 + if (args.k != VVOID) 33.651 + luaK_exp2nextreg(fs, &args); /* close last argument */ 33.652 + nparams = fs->freereg - (base+1); 33.653 + } 33.654 + init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); 33.655 + luaK_fixline(fs, line); 33.656 + fs->freereg = base+1; /* call remove function and arguments and leaves 33.657 + (unless changed) one result */ 33.658 +} 33.659 + 33.660 + 33.661 + 33.662 + 33.663 +/* 33.664 +** {====================================================================== 33.665 +** Expression parsing 33.666 +** ======================================================================= 33.667 +*/ 33.668 + 33.669 + 33.670 +static void prefixexp (LexState *ls, expdesc *v) { 33.671 + /* prefixexp -> NAME | '(' expr ')' */ 33.672 + switch (ls->t.token) { 33.673 + case '(': { 33.674 + int line = ls->linenumber; 33.675 + luaX_next(ls); 33.676 + expr(ls, v); 33.677 + check_match(ls, ')', '(', line); 33.678 + luaK_dischargevars(ls->fs, v); 33.679 + return; 33.680 + } 33.681 + case TK_NAME: { 33.682 + singlevar(ls, v); 33.683 + return; 33.684 + } 33.685 + default: { 33.686 + luaX_syntaxerror(ls, "unexpected symbol"); 33.687 + return; 33.688 + } 33.689 + } 33.690 +} 33.691 + 33.692 + 33.693 +static void primaryexp (LexState *ls, expdesc *v) { 33.694 + /* primaryexp -> 33.695 + prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */ 33.696 + FuncState *fs = ls->fs; 33.697 + prefixexp(ls, v); 33.698 + for (;;) { 33.699 + switch (ls->t.token) { 33.700 + case '.': { /* field */ 33.701 + field(ls, v); 33.702 + break; 33.703 + } 33.704 + case '[': { /* `[' exp1 `]' */ 33.705 + expdesc key; 33.706 + luaK_exp2anyreg(fs, v); 33.707 + yindex(ls, &key); 33.708 + luaK_indexed(fs, v, &key); 33.709 + break; 33.710 + } 33.711 + case ':': { /* `:' NAME funcargs */ 33.712 + expdesc key; 33.713 + luaX_next(ls); 33.714 + checkname(ls, &key); 33.715 + luaK_self(fs, v, &key); 33.716 + funcargs(ls, v); 33.717 + break; 33.718 + } 33.719 + case '(': case TK_STRING: case '{': { /* funcargs */ 33.720 + luaK_exp2nextreg(fs, v); 33.721 + funcargs(ls, v); 33.722 + break; 33.723 + } 33.724 + default: return; 33.725 + } 33.726 + } 33.727 +} 33.728 + 33.729 + 33.730 +static void simpleexp (LexState *ls, expdesc *v) { 33.731 + /* simpleexp -> NUMBER | STRING | NIL | true | false | ... | 33.732 + constructor | FUNCTION body | primaryexp */ 33.733 + switch (ls->t.token) { 33.734 + case TK_NUMBER: { 33.735 + init_exp(v, VKNUM, 0); 33.736 + v->u.nval = ls->t.seminfo.r; 33.737 + break; 33.738 + } 33.739 + case TK_STRING: { 33.740 + codestring(ls, v, ls->t.seminfo.ts); 33.741 + break; 33.742 + } 33.743 + case TK_NIL: { 33.744 + init_exp(v, VNIL, 0); 33.745 + break; 33.746 + } 33.747 + case TK_TRUE: { 33.748 + init_exp(v, VTRUE, 0); 33.749 + break; 33.750 + } 33.751 + case TK_FALSE: { 33.752 + init_exp(v, VFALSE, 0); 33.753 + break; 33.754 + } 33.755 + case TK_DOTS: { /* vararg */ 33.756 + FuncState *fs = ls->fs; 33.757 + check_condition(ls, fs->f->is_vararg, 33.758 + "cannot use " LUA_QL("...") " outside a vararg function"); 33.759 + fs->f->is_vararg &= ~VARARG_NEEDSARG; /* don't need 'arg' */ 33.760 + init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); 33.761 + break; 33.762 + } 33.763 + case '{': { /* constructor */ 33.764 + constructor(ls, v); 33.765 + return; 33.766 + } 33.767 + case TK_FUNCTION: { 33.768 + luaX_next(ls); 33.769 + body(ls, v, 0, ls->linenumber); 33.770 + return; 33.771 + } 33.772 + default: { 33.773 + primaryexp(ls, v); 33.774 + return; 33.775 + } 33.776 + } 33.777 + luaX_next(ls); 33.778 +} 33.779 + 33.780 + 33.781 +static UnOpr getunopr (int op) { 33.782 + switch (op) { 33.783 + case TK_NOT: return OPR_NOT; 33.784 + case '-': return OPR_MINUS; 33.785 + case '#': return OPR_LEN; 33.786 + default: return OPR_NOUNOPR; 33.787 + } 33.788 +} 33.789 + 33.790 + 33.791 +static BinOpr getbinopr (int op) { 33.792 + switch (op) { 33.793 + case '+': return OPR_ADD; 33.794 + case '-': return OPR_SUB; 33.795 + case '*': return OPR_MUL; 33.796 + case '/': return OPR_DIV; 33.797 + case '%': return OPR_MOD; 33.798 + case '^': return OPR_POW; 33.799 + case TK_CONCAT: return OPR_CONCAT; 33.800 + case TK_NE: return OPR_NE; 33.801 + case TK_EQ: return OPR_EQ; 33.802 + case '<': return OPR_LT; 33.803 + case TK_LE: return OPR_LE; 33.804 + case '>': return OPR_GT; 33.805 + case TK_GE: return OPR_GE; 33.806 + case TK_AND: return OPR_AND; 33.807 + case TK_OR: return OPR_OR; 33.808 + default: return OPR_NOBINOPR; 33.809 + } 33.810 +} 33.811 + 33.812 + 33.813 +static const struct { 33.814 + lu_byte left; /* left priority for each binary operator */ 33.815 + lu_byte right; /* right priority */ 33.816 +} priority[] = { /* ORDER OPR */ 33.817 + {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */ 33.818 + {10, 9}, {5, 4}, /* power and concat (right associative) */ 33.819 + {3, 3}, {3, 3}, /* equality and inequality */ 33.820 + {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */ 33.821 + {2, 2}, {1, 1} /* logical (and/or) */ 33.822 +}; 33.823 + 33.824 +#define UNARY_PRIORITY 8 /* priority for unary operators */ 33.825 + 33.826 + 33.827 +/* 33.828 +** subexpr -> (simpleexp | unop subexpr) { binop subexpr } 33.829 +** where `binop' is any binary operator with a priority higher than `limit' 33.830 +*/ 33.831 +static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { 33.832 + BinOpr op; 33.833 + UnOpr uop; 33.834 + enterlevel(ls); 33.835 + uop = getunopr(ls->t.token); 33.836 + if (uop != OPR_NOUNOPR) { 33.837 + luaX_next(ls); 33.838 + subexpr(ls, v, UNARY_PRIORITY); 33.839 + luaK_prefix(ls->fs, uop, v); 33.840 + } 33.841 + else simpleexp(ls, v); 33.842 + /* expand while operators have priorities higher than `limit' */ 33.843 + op = getbinopr(ls->t.token); 33.844 + while (op != OPR_NOBINOPR && priority[op].left > limit) { 33.845 + expdesc v2; 33.846 + BinOpr nextop; 33.847 + luaX_next(ls); 33.848 + luaK_infix(ls->fs, op, v); 33.849 + /* read sub-expression with higher priority */ 33.850 + nextop = subexpr(ls, &v2, priority[op].right); 33.851 + luaK_posfix(ls->fs, op, v, &v2); 33.852 + op = nextop; 33.853 + } 33.854 + leavelevel(ls); 33.855 + return op; /* return first untreated operator */ 33.856 +} 33.857 + 33.858 + 33.859 +static void expr (LexState *ls, expdesc *v) { 33.860 + subexpr(ls, v, 0); 33.861 +} 33.862 + 33.863 +/* }==================================================================== */ 33.864 + 33.865 + 33.866 + 33.867 +/* 33.868 +** {====================================================================== 33.869 +** Rules for Statements 33.870 +** ======================================================================= 33.871 +*/ 33.872 + 33.873 + 33.874 +static int block_follow (int token) { 33.875 + switch (token) { 33.876 + case TK_ELSE: case TK_ELSEIF: case TK_END: 33.877 + case TK_UNTIL: case TK_EOS: 33.878 + return 1; 33.879 + default: return 0; 33.880 + } 33.881 +} 33.882 + 33.883 + 33.884 +static void block (LexState *ls) { 33.885 + /* block -> chunk */ 33.886 + FuncState *fs = ls->fs; 33.887 + BlockCnt bl; 33.888 + enterblock(fs, &bl, 0); 33.889 + chunk(ls); 33.890 + lua_assert(bl.breaklist == NO_JUMP); 33.891 + leaveblock(fs); 33.892 +} 33.893 + 33.894 + 33.895 +/* 33.896 +** structure to chain all variables in the left-hand side of an 33.897 +** assignment 33.898 +*/ 33.899 +struct LHS_assign { 33.900 + struct LHS_assign *prev; 33.901 + expdesc v; /* variable (global, local, upvalue, or indexed) */ 33.902 +}; 33.903 + 33.904 + 33.905 +/* 33.906 +** check whether, in an assignment to a local variable, the local variable 33.907 +** is needed in a previous assignment (to a table). If so, save original 33.908 +** local value in a safe place and use this safe copy in the previous 33.909 +** assignment. 33.910 +*/ 33.911 +static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { 33.912 + FuncState *fs = ls->fs; 33.913 + int extra = fs->freereg; /* eventual position to save local variable */ 33.914 + int conflict = 0; 33.915 + for (; lh; lh = lh->prev) { 33.916 + if (lh->v.k == VINDEXED) { 33.917 + if (lh->v.u.s.info == v->u.s.info) { /* conflict? */ 33.918 + conflict = 1; 33.919 + lh->v.u.s.info = extra; /* previous assignment will use safe copy */ 33.920 + } 33.921 + if (lh->v.u.s.aux == v->u.s.info) { /* conflict? */ 33.922 + conflict = 1; 33.923 + lh->v.u.s.aux = extra; /* previous assignment will use safe copy */ 33.924 + } 33.925 + } 33.926 + } 33.927 + if (conflict) { 33.928 + luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0); /* make copy */ 33.929 + luaK_reserveregs(fs, 1); 33.930 + } 33.931 +} 33.932 + 33.933 + 33.934 +static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { 33.935 + expdesc e; 33.936 + check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, 33.937 + "syntax error"); 33.938 + if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */ 33.939 + struct LHS_assign nv; 33.940 + nv.prev = lh; 33.941 + primaryexp(ls, &nv.v); 33.942 + if (nv.v.k == VLOCAL) 33.943 + check_conflict(ls, lh, &nv.v); 33.944 + luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls, 33.945 + "variables in assignment"); 33.946 + assignment(ls, &nv, nvars+1); 33.947 + } 33.948 + else { /* assignment -> `=' explist1 */ 33.949 + int nexps; 33.950 + checknext(ls, '='); 33.951 + nexps = explist1(ls, &e); 33.952 + if (nexps != nvars) { 33.953 + adjust_assign(ls, nvars, nexps, &e); 33.954 + if (nexps > nvars) 33.955 + ls->fs->freereg -= nexps - nvars; /* remove extra values */ 33.956 + } 33.957 + else { 33.958 + luaK_setoneret(ls->fs, &e); /* close last expression */ 33.959 + luaK_storevar(ls->fs, &lh->v, &e); 33.960 + return; /* avoid default */ 33.961 + } 33.962 + } 33.963 + init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ 33.964 + luaK_storevar(ls->fs, &lh->v, &e); 33.965 +} 33.966 + 33.967 + 33.968 +static int cond (LexState *ls) { 33.969 + /* cond -> exp */ 33.970 + expdesc v; 33.971 + expr(ls, &v); /* read condition */ 33.972 + if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */ 33.973 + luaK_goiftrue(ls->fs, &v); 33.974 + return v.f; 33.975 +} 33.976 + 33.977 + 33.978 +static void breakstat (LexState *ls) { 33.979 + FuncState *fs = ls->fs; 33.980 + BlockCnt *bl = fs->bl; 33.981 + int upval = 0; 33.982 + while (bl && !bl->isbreakable) { 33.983 + upval |= bl->upval; 33.984 + bl = bl->previous; 33.985 + } 33.986 + if (!bl) 33.987 + luaX_syntaxerror(ls, "no loop to break"); 33.988 + if (upval) 33.989 + luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); 33.990 + luaK_concat(fs, &bl->breaklist, luaK_jump(fs)); 33.991 +} 33.992 + 33.993 + 33.994 +static void whilestat (LexState *ls, int line) { 33.995 + /* whilestat -> WHILE cond DO block END */ 33.996 + FuncState *fs = ls->fs; 33.997 + int whileinit; 33.998 + int condexit; 33.999 + BlockCnt bl; 33.1000 + luaX_next(ls); /* skip WHILE */ 33.1001 + whileinit = luaK_getlabel(fs); 33.1002 + condexit = cond(ls); 33.1003 + enterblock(fs, &bl, 1); 33.1004 + checknext(ls, TK_DO); 33.1005 + block(ls); 33.1006 + luaK_patchlist(fs, luaK_jump(fs), whileinit); 33.1007 + check_match(ls, TK_END, TK_WHILE, line); 33.1008 + leaveblock(fs); 33.1009 + luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ 33.1010 +} 33.1011 + 33.1012 + 33.1013 +static void repeatstat (LexState *ls, int line) { 33.1014 + /* repeatstat -> REPEAT block UNTIL cond */ 33.1015 + int condexit; 33.1016 + FuncState *fs = ls->fs; 33.1017 + int repeat_init = luaK_getlabel(fs); 33.1018 + BlockCnt bl1, bl2; 33.1019 + enterblock(fs, &bl1, 1); /* loop block */ 33.1020 + enterblock(fs, &bl2, 0); /* scope block */ 33.1021 + luaX_next(ls); /* skip REPEAT */ 33.1022 + chunk(ls); 33.1023 + check_match(ls, TK_UNTIL, TK_REPEAT, line); 33.1024 + condexit = cond(ls); /* read condition (inside scope block) */ 33.1025 + if (!bl2.upval) { /* no upvalues? */ 33.1026 + leaveblock(fs); /* finish scope */ 33.1027 + luaK_patchlist(ls->fs, condexit, repeat_init); /* close the loop */ 33.1028 + } 33.1029 + else { /* complete semantics when there are upvalues */ 33.1030 + breakstat(ls); /* if condition then break */ 33.1031 + luaK_patchtohere(ls->fs, condexit); /* else... */ 33.1032 + leaveblock(fs); /* finish scope... */ 33.1033 + luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init); /* and repeat */ 33.1034 + } 33.1035 + leaveblock(fs); /* finish loop */ 33.1036 +} 33.1037 + 33.1038 + 33.1039 +static int exp1 (LexState *ls) { 33.1040 + expdesc e; 33.1041 + int k; 33.1042 + expr(ls, &e); 33.1043 + k = e.k; 33.1044 + luaK_exp2nextreg(ls->fs, &e); 33.1045 + return k; 33.1046 +} 33.1047 + 33.1048 + 33.1049 +static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { 33.1050 + /* forbody -> DO block */ 33.1051 + BlockCnt bl; 33.1052 + FuncState *fs = ls->fs; 33.1053 + int prep, endfor; 33.1054 + adjustlocalvars(ls, 3); /* control variables */ 33.1055 + checknext(ls, TK_DO); 33.1056 + prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); 33.1057 + enterblock(fs, &bl, 0); /* scope for declared variables */ 33.1058 + adjustlocalvars(ls, nvars); 33.1059 + luaK_reserveregs(fs, nvars); 33.1060 + block(ls); 33.1061 + leaveblock(fs); /* end of scope for declared variables */ 33.1062 + luaK_patchtohere(fs, prep); 33.1063 + endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) : 33.1064 + luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars); 33.1065 + luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */ 33.1066 + luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1); 33.1067 +} 33.1068 + 33.1069 + 33.1070 +static void fornum (LexState *ls, TString *varname, int line) { 33.1071 + /* fornum -> NAME = exp1,exp1[,exp1] forbody */ 33.1072 + FuncState *fs = ls->fs; 33.1073 + int base = fs->freereg; 33.1074 + new_localvarliteral(ls, "(for index)", 0); 33.1075 + new_localvarliteral(ls, "(for limit)", 1); 33.1076 + new_localvarliteral(ls, "(for step)", 2); 33.1077 + new_localvar(ls, varname, 3); 33.1078 + checknext(ls, '='); 33.1079 + exp1(ls); /* initial value */ 33.1080 + checknext(ls, ','); 33.1081 + exp1(ls); /* limit */ 33.1082 + if (testnext(ls, ',')) 33.1083 + exp1(ls); /* optional step */ 33.1084 + else { /* default step = 1 */ 33.1085 + luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1)); 33.1086 + luaK_reserveregs(fs, 1); 33.1087 + } 33.1088 + forbody(ls, base, line, 1, 1); 33.1089 +} 33.1090 + 33.1091 + 33.1092 +static void forlist (LexState *ls, TString *indexname) { 33.1093 + /* forlist -> NAME {,NAME} IN explist1 forbody */ 33.1094 + FuncState *fs = ls->fs; 33.1095 + expdesc e; 33.1096 + int nvars = 0; 33.1097 + int line; 33.1098 + int base = fs->freereg; 33.1099 + /* create control variables */ 33.1100 + new_localvarliteral(ls, "(for generator)", nvars++); 33.1101 + new_localvarliteral(ls, "(for state)", nvars++); 33.1102 + new_localvarliteral(ls, "(for control)", nvars++); 33.1103 + /* create declared variables */ 33.1104 + new_localvar(ls, indexname, nvars++); 33.1105 + while (testnext(ls, ',')) 33.1106 + new_localvar(ls, str_checkname(ls), nvars++); 33.1107 + checknext(ls, TK_IN); 33.1108 + line = ls->linenumber; 33.1109 + adjust_assign(ls, 3, explist1(ls, &e), &e); 33.1110 + luaK_checkstack(fs, 3); /* extra space to call generator */ 33.1111 + forbody(ls, base, line, nvars - 3, 0); 33.1112 +} 33.1113 + 33.1114 + 33.1115 +static void forstat (LexState *ls, int line) { 33.1116 + /* forstat -> FOR (fornum | forlist) END */ 33.1117 + FuncState *fs = ls->fs; 33.1118 + TString *varname; 33.1119 + BlockCnt bl; 33.1120 + enterblock(fs, &bl, 1); /* scope for loop and control variables */ 33.1121 + luaX_next(ls); /* skip `for' */ 33.1122 + varname = str_checkname(ls); /* first variable name */ 33.1123 + switch (ls->t.token) { 33.1124 + case '=': fornum(ls, varname, line); break; 33.1125 + case ',': case TK_IN: forlist(ls, varname); break; 33.1126 + default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected"); 33.1127 + } 33.1128 + check_match(ls, TK_END, TK_FOR, line); 33.1129 + leaveblock(fs); /* loop scope (`break' jumps to this point) */ 33.1130 +} 33.1131 + 33.1132 + 33.1133 +static int test_then_block (LexState *ls) { 33.1134 + /* test_then_block -> [IF | ELSEIF] cond THEN block */ 33.1135 + int condexit; 33.1136 + luaX_next(ls); /* skip IF or ELSEIF */ 33.1137 + condexit = cond(ls); 33.1138 + checknext(ls, TK_THEN); 33.1139 + block(ls); /* `then' part */ 33.1140 + return condexit; 33.1141 +} 33.1142 + 33.1143 + 33.1144 +static void ifstat (LexState *ls, int line) { 33.1145 + /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ 33.1146 + FuncState *fs = ls->fs; 33.1147 + int flist; 33.1148 + int escapelist = NO_JUMP; 33.1149 + flist = test_then_block(ls); /* IF cond THEN block */ 33.1150 + while (ls->t.token == TK_ELSEIF) { 33.1151 + luaK_concat(fs, &escapelist, luaK_jump(fs)); 33.1152 + luaK_patchtohere(fs, flist); 33.1153 + flist = test_then_block(ls); /* ELSEIF cond THEN block */ 33.1154 + } 33.1155 + if (ls->t.token == TK_ELSE) { 33.1156 + luaK_concat(fs, &escapelist, luaK_jump(fs)); 33.1157 + luaK_patchtohere(fs, flist); 33.1158 + luaX_next(ls); /* skip ELSE (after patch, for correct line info) */ 33.1159 + block(ls); /* `else' part */ 33.1160 + } 33.1161 + else 33.1162 + luaK_concat(fs, &escapelist, flist); 33.1163 + luaK_patchtohere(fs, escapelist); 33.1164 + check_match(ls, TK_END, TK_IF, line); 33.1165 +} 33.1166 + 33.1167 + 33.1168 +static void localfunc (LexState *ls) { 33.1169 + expdesc v, b; 33.1170 + FuncState *fs = ls->fs; 33.1171 + new_localvar(ls, str_checkname(ls), 0); 33.1172 + init_exp(&v, VLOCAL, fs->freereg); 33.1173 + luaK_reserveregs(fs, 1); 33.1174 + adjustlocalvars(ls, 1); 33.1175 + body(ls, &b, 0, ls->linenumber); 33.1176 + luaK_storevar(fs, &v, &b); 33.1177 + /* debug information will only see the variable after this point! */ 33.1178 + getlocvar(fs, fs->nactvar - 1).startpc = fs->pc; 33.1179 +} 33.1180 + 33.1181 + 33.1182 +static void localstat (LexState *ls) { 33.1183 + /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */ 33.1184 + int nvars = 0; 33.1185 + int nexps; 33.1186 + expdesc e; 33.1187 + do { 33.1188 + new_localvar(ls, str_checkname(ls), nvars++); 33.1189 + } while (testnext(ls, ',')); 33.1190 + if (testnext(ls, '=')) 33.1191 + nexps = explist1(ls, &e); 33.1192 + else { 33.1193 + e.k = VVOID; 33.1194 + nexps = 0; 33.1195 + } 33.1196 + adjust_assign(ls, nvars, nexps, &e); 33.1197 + adjustlocalvars(ls, nvars); 33.1198 +} 33.1199 + 33.1200 + 33.1201 +static int funcname (LexState *ls, expdesc *v) { 33.1202 + /* funcname -> NAME {field} [`:' NAME] */ 33.1203 + int needself = 0; 33.1204 + singlevar(ls, v); 33.1205 + while (ls->t.token == '.') 33.1206 + field(ls, v); 33.1207 + if (ls->t.token == ':') { 33.1208 + needself = 1; 33.1209 + field(ls, v); 33.1210 + } 33.1211 + return needself; 33.1212 +} 33.1213 + 33.1214 + 33.1215 +static void funcstat (LexState *ls, int line) { 33.1216 + /* funcstat -> FUNCTION funcname body */ 33.1217 + int needself; 33.1218 + expdesc v, b; 33.1219 + luaX_next(ls); /* skip FUNCTION */ 33.1220 + needself = funcname(ls, &v); 33.1221 + body(ls, &b, needself, line); 33.1222 + luaK_storevar(ls->fs, &v, &b); 33.1223 + luaK_fixline(ls->fs, line); /* definition `happens' in the first line */ 33.1224 +} 33.1225 + 33.1226 + 33.1227 +static void exprstat (LexState *ls) { 33.1228 + /* stat -> func | assignment */ 33.1229 + FuncState *fs = ls->fs; 33.1230 + struct LHS_assign v; 33.1231 + primaryexp(ls, &v.v); 33.1232 + if (v.v.k == VCALL) /* stat -> func */ 33.1233 + SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ 33.1234 + else { /* stat -> assignment */ 33.1235 + v.prev = NULL; 33.1236 + assignment(ls, &v, 1); 33.1237 + } 33.1238 +} 33.1239 + 33.1240 + 33.1241 +static void retstat (LexState *ls) { 33.1242 + /* stat -> RETURN explist */ 33.1243 + FuncState *fs = ls->fs; 33.1244 + expdesc e; 33.1245 + int first, nret; /* registers with returned values */ 33.1246 + luaX_next(ls); /* skip RETURN */ 33.1247 + if (block_follow(ls->t.token) || ls->t.token == ';') 33.1248 + first = nret = 0; /* return no values */ 33.1249 + else { 33.1250 + nret = explist1(ls, &e); /* optional return values */ 33.1251 + if (hasmultret(e.k)) { 33.1252 + luaK_setmultret(fs, &e); 33.1253 + if (e.k == VCALL && nret == 1) { /* tail call? */ 33.1254 + SET_OPCODE(getcode(fs,&e), OP_TAILCALL); 33.1255 + lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar); 33.1256 + } 33.1257 + first = fs->nactvar; 33.1258 + nret = LUA_MULTRET; /* return all values */ 33.1259 + } 33.1260 + else { 33.1261 + if (nret == 1) /* only one single value? */ 33.1262 + first = luaK_exp2anyreg(fs, &e); 33.1263 + else { 33.1264 + luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */ 33.1265 + first = fs->nactvar; /* return all `active' values */ 33.1266 + lua_assert(nret == fs->freereg - first); 33.1267 + } 33.1268 + } 33.1269 + } 33.1270 + luaK_ret(fs, first, nret); 33.1271 +} 33.1272 + 33.1273 + 33.1274 +static int statement (LexState *ls) { 33.1275 + int line = ls->linenumber; /* may be needed for error messages */ 33.1276 + switch (ls->t.token) { 33.1277 + case TK_IF: { /* stat -> ifstat */ 33.1278 + ifstat(ls, line); 33.1279 + return 0; 33.1280 + } 33.1281 + case TK_WHILE: { /* stat -> whilestat */ 33.1282 + whilestat(ls, line); 33.1283 + return 0; 33.1284 + } 33.1285 + case TK_DO: { /* stat -> DO block END */ 33.1286 + luaX_next(ls); /* skip DO */ 33.1287 + block(ls); 33.1288 + check_match(ls, TK_END, TK_DO, line); 33.1289 + return 0; 33.1290 + } 33.1291 + case TK_FOR: { /* stat -> forstat */ 33.1292 + forstat(ls, line); 33.1293 + return 0; 33.1294 + } 33.1295 + case TK_REPEAT: { /* stat -> repeatstat */ 33.1296 + repeatstat(ls, line); 33.1297 + return 0; 33.1298 + } 33.1299 + case TK_FUNCTION: { 33.1300 + funcstat(ls, line); /* stat -> funcstat */ 33.1301 + return 0; 33.1302 + } 33.1303 + case TK_LOCAL: { /* stat -> localstat */ 33.1304 + luaX_next(ls); /* skip LOCAL */ 33.1305 + if (testnext(ls, TK_FUNCTION)) /* local function? */ 33.1306 + localfunc(ls); 33.1307 + else 33.1308 + localstat(ls); 33.1309 + return 0; 33.1310 + } 33.1311 + case TK_RETURN: { /* stat -> retstat */ 33.1312 + retstat(ls); 33.1313 + return 1; /* must be last statement */ 33.1314 + } 33.1315 + case TK_BREAK: { /* stat -> breakstat */ 33.1316 + luaX_next(ls); /* skip BREAK */ 33.1317 + breakstat(ls); 33.1318 + return 1; /* must be last statement */ 33.1319 + } 33.1320 + default: { 33.1321 + exprstat(ls); 33.1322 + return 0; /* to avoid warnings */ 33.1323 + } 33.1324 + } 33.1325 +} 33.1326 + 33.1327 + 33.1328 +static void chunk (LexState *ls) { 33.1329 + /* chunk -> { stat [`;'] } */ 33.1330 + int islast = 0; 33.1331 + enterlevel(ls); 33.1332 + while (!islast && !block_follow(ls->t.token)) { 33.1333 + islast = statement(ls); 33.1334 + testnext(ls, ';'); 33.1335 + lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && 33.1336 + ls->fs->freereg >= ls->fs->nactvar); 33.1337 + ls->fs->freereg = ls->fs->nactvar; /* free registers */ 33.1338 + } 33.1339 + leavelevel(ls); 33.1340 +} 33.1341 + 33.1342 +/* }====================================================================== */
34.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 34.2 +++ b/src/lua/lparser.h Sat Mar 03 11:07:39 2012 -0600 34.3 @@ -0,0 +1,82 @@ 34.4 +/* 34.5 +** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $ 34.6 +** Lua Parser 34.7 +** See Copyright Notice in lua.h 34.8 +*/ 34.9 + 34.10 +#ifndef lparser_h 34.11 +#define lparser_h 34.12 + 34.13 +#include "llimits.h" 34.14 +#include "lobject.h" 34.15 +#include "lzio.h" 34.16 + 34.17 + 34.18 +/* 34.19 +** Expression descriptor 34.20 +*/ 34.21 + 34.22 +typedef enum { 34.23 + VVOID, /* no value */ 34.24 + VNIL, 34.25 + VTRUE, 34.26 + VFALSE, 34.27 + VK, /* info = index of constant in `k' */ 34.28 + VKNUM, /* nval = numerical value */ 34.29 + VLOCAL, /* info = local register */ 34.30 + VUPVAL, /* info = index of upvalue in `upvalues' */ 34.31 + VGLOBAL, /* info = index of table; aux = index of global name in `k' */ 34.32 + VINDEXED, /* info = table register; aux = index register (or `k') */ 34.33 + VJMP, /* info = instruction pc */ 34.34 + VRELOCABLE, /* info = instruction pc */ 34.35 + VNONRELOC, /* info = result register */ 34.36 + VCALL, /* info = instruction pc */ 34.37 + VVARARG /* info = instruction pc */ 34.38 +} expkind; 34.39 + 34.40 +typedef struct expdesc { 34.41 + expkind k; 34.42 + union { 34.43 + struct { int info, aux; } s; 34.44 + lua_Number nval; 34.45 + } u; 34.46 + int t; /* patch list of `exit when true' */ 34.47 + int f; /* patch list of `exit when false' */ 34.48 +} expdesc; 34.49 + 34.50 + 34.51 +typedef struct upvaldesc { 34.52 + lu_byte k; 34.53 + lu_byte info; 34.54 +} upvaldesc; 34.55 + 34.56 + 34.57 +struct BlockCnt; /* defined in lparser.c */ 34.58 + 34.59 + 34.60 +/* state needed to generate code for a given function */ 34.61 +typedef struct FuncState { 34.62 + Proto *f; /* current function header */ 34.63 + Table *h; /* table to find (and reuse) elements in `k' */ 34.64 + struct FuncState *prev; /* enclosing function */ 34.65 + struct LexState *ls; /* lexical state */ 34.66 + struct lua_State *L; /* copy of the Lua state */ 34.67 + struct BlockCnt *bl; /* chain of current blocks */ 34.68 + int pc; /* next position to code (equivalent to `ncode') */ 34.69 + int lasttarget; /* `pc' of last `jump target' */ 34.70 + int jpc; /* list of pending jumps to `pc' */ 34.71 + int freereg; /* first free register */ 34.72 + int nk; /* number of elements in `k' */ 34.73 + int np; /* number of elements in `p' */ 34.74 + short nlocvars; /* number of elements in `locvars' */ 34.75 + lu_byte nactvar; /* number of active local variables */ 34.76 + upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */ 34.77 + unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */ 34.78 +} FuncState; 34.79 + 34.80 + 34.81 +LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 34.82 + const char *name); 34.83 + 34.84 + 34.85 +#endif
35.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 35.2 +++ b/src/lua/lstate.c Sat Mar 03 11:07:39 2012 -0600 35.3 @@ -0,0 +1,214 @@ 35.4 +/* 35.5 +** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $ 35.6 +** Global State 35.7 +** See Copyright Notice in lua.h 35.8 +*/ 35.9 + 35.10 + 35.11 +#include <stddef.h> 35.12 + 35.13 +#define lstate_c 35.14 +#define LUA_CORE 35.15 + 35.16 +#include "lua.h" 35.17 + 35.18 +#include "ldebug.h" 35.19 +#include "ldo.h" 35.20 +#include "lfunc.h" 35.21 +#include "lgc.h" 35.22 +#include "llex.h" 35.23 +#include "lmem.h" 35.24 +#include "lstate.h" 35.25 +#include "lstring.h" 35.26 +#include "ltable.h" 35.27 +#include "ltm.h" 35.28 + 35.29 + 35.30 +#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) 35.31 +#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) 35.32 +#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) 35.33 + 35.34 + 35.35 +/* 35.36 +** Main thread combines a thread state and the global state 35.37 +*/ 35.38 +typedef struct LG { 35.39 + lua_State l; 35.40 + global_State g; 35.41 +} LG; 35.42 + 35.43 + 35.44 + 35.45 +static void stack_init (lua_State *L1, lua_State *L) { 35.46 + /* initialize CallInfo array */ 35.47 + L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo); 35.48 + L1->ci = L1->base_ci; 35.49 + L1->size_ci = BASIC_CI_SIZE; 35.50 + L1->end_ci = L1->base_ci + L1->size_ci - 1; 35.51 + /* initialize stack array */ 35.52 + L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue); 35.53 + L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; 35.54 + L1->top = L1->stack; 35.55 + L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1; 35.56 + /* initialize first ci */ 35.57 + L1->ci->func = L1->top; 35.58 + setnilvalue(L1->top++); /* `function' entry for this `ci' */ 35.59 + L1->base = L1->ci->base = L1->top; 35.60 + L1->ci->top = L1->top + LUA_MINSTACK; 35.61 +} 35.62 + 35.63 + 35.64 +static void freestack (lua_State *L, lua_State *L1) { 35.65 + luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo); 35.66 + luaM_freearray(L, L1->stack, L1->stacksize, TValue); 35.67 +} 35.68 + 35.69 + 35.70 +/* 35.71 +** open parts that may cause memory-allocation errors 35.72 +*/ 35.73 +static void f_luaopen (lua_State *L, void *ud) { 35.74 + global_State *g = G(L); 35.75 + UNUSED(ud); 35.76 + stack_init(L, L); /* init stack */ 35.77 + sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ 35.78 + sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ 35.79 + luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 35.80 + luaT_init(L); 35.81 + luaX_init(L); 35.82 + luaS_fix(luaS_newliteral(L, MEMERRMSG)); 35.83 + g->GCthreshold = 4*g->totalbytes; 35.84 +} 35.85 + 35.86 + 35.87 +static void preinit_state (lua_State *L, global_State *g) { 35.88 + G(L) = g; 35.89 + L->stack = NULL; 35.90 + L->stacksize = 0; 35.91 + L->errorJmp = NULL; 35.92 + L->hook = NULL; 35.93 + L->hookmask = 0; 35.94 + L->basehookcount = 0; 35.95 + L->allowhook = 1; 35.96 + resethookcount(L); 35.97 + L->openupval = NULL; 35.98 + L->size_ci = 0; 35.99 + L->nCcalls = L->baseCcalls = 0; 35.100 + L->status = 0; 35.101 + L->base_ci = L->ci = NULL; 35.102 + L->savedpc = NULL; 35.103 + L->errfunc = 0; 35.104 + setnilvalue(gt(L)); 35.105 +} 35.106 + 35.107 + 35.108 +static void close_state (lua_State *L) { 35.109 + global_State *g = G(L); 35.110 + luaF_close(L, L->stack); /* close all upvalues for this thread */ 35.111 + luaC_freeall(L); /* collect all objects */ 35.112 + lua_assert(g->rootgc == obj2gco(L)); 35.113 + lua_assert(g->strt.nuse == 0); 35.114 + luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); 35.115 + luaZ_freebuffer(L, &g->buff); 35.116 + freestack(L, L); 35.117 + lua_assert(g->totalbytes == sizeof(LG)); 35.118 + (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); 35.119 +} 35.120 + 35.121 + 35.122 +lua_State *luaE_newthread (lua_State *L) { 35.123 + lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); 35.124 + luaC_link(L, obj2gco(L1), LUA_TTHREAD); 35.125 + preinit_state(L1, G(L)); 35.126 + stack_init(L1, L); /* init stack */ 35.127 + setobj2n(L, gt(L1), gt(L)); /* share table of globals */ 35.128 + L1->hookmask = L->hookmask; 35.129 + L1->basehookcount = L->basehookcount; 35.130 + L1->hook = L->hook; 35.131 + resethookcount(L1); 35.132 + lua_assert(iswhite(obj2gco(L1))); 35.133 + return L1; 35.134 +} 35.135 + 35.136 + 35.137 +void luaE_freethread (lua_State *L, lua_State *L1) { 35.138 + luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 35.139 + lua_assert(L1->openupval == NULL); 35.140 + luai_userstatefree(L1); 35.141 + freestack(L, L1); 35.142 + luaM_freemem(L, fromstate(L1), state_size(lua_State)); 35.143 +} 35.144 + 35.145 + 35.146 +LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { 35.147 + int i; 35.148 + lua_State *L; 35.149 + global_State *g; 35.150 + void *l = (*f)(ud, NULL, 0, state_size(LG)); 35.151 + if (l == NULL) return NULL; 35.152 + L = tostate(l); 35.153 + g = &((LG *)L)->g; 35.154 + L->next = NULL; 35.155 + L->tt = LUA_TTHREAD; 35.156 + g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); 35.157 + L->marked = luaC_white(g); 35.158 + set2bits(L->marked, FIXEDBIT, SFIXEDBIT); 35.159 + preinit_state(L, g); 35.160 + g->frealloc = f; 35.161 + g->ud = ud; 35.162 + g->mainthread = L; 35.163 + g->uvhead.u.l.prev = &g->uvhead; 35.164 + g->uvhead.u.l.next = &g->uvhead; 35.165 + g->GCthreshold = 0; /* mark it as unfinished state */ 35.166 + g->strt.size = 0; 35.167 + g->strt.nuse = 0; 35.168 + g->strt.hash = NULL; 35.169 + setnilvalue(registry(L)); 35.170 + luaZ_initbuffer(L, &g->buff); 35.171 + g->panic = NULL; 35.172 + g->gcstate = GCSpause; 35.173 + g->rootgc = obj2gco(L); 35.174 + g->sweepstrgc = 0; 35.175 + g->sweepgc = &g->rootgc; 35.176 + g->gray = NULL; 35.177 + g->grayagain = NULL; 35.178 + g->weak = NULL; 35.179 + g->tmudata = NULL; 35.180 + g->totalbytes = sizeof(LG); 35.181 + g->gcpause = LUAI_GCPAUSE; 35.182 + g->gcstepmul = LUAI_GCMUL; 35.183 + g->gcdept = 0; 35.184 + for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL; 35.185 + if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { 35.186 + /* memory allocation error: free partial state */ 35.187 + close_state(L); 35.188 + L = NULL; 35.189 + } 35.190 + else 35.191 + luai_userstateopen(L); 35.192 + return L; 35.193 +} 35.194 + 35.195 + 35.196 +static void callallgcTM (lua_State *L, void *ud) { 35.197 + UNUSED(ud); 35.198 + luaC_callGCTM(L); /* call GC metamethods for all udata */ 35.199 +} 35.200 + 35.201 + 35.202 +LUA_API void lua_close (lua_State *L) { 35.203 + L = G(L)->mainthread; /* only the main thread can be closed */ 35.204 + lua_lock(L); 35.205 + luaF_close(L, L->stack); /* close all upvalues for this thread */ 35.206 + luaC_separateudata(L, 1); /* separate udata that have GC metamethods */ 35.207 + L->errfunc = 0; /* no error function during GC metamethods */ 35.208 + do { /* repeat until no more errors */ 35.209 + L->ci = L->base_ci; 35.210 + L->base = L->top = L->ci->base; 35.211 + L->nCcalls = L->baseCcalls = 0; 35.212 + } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); 35.213 + lua_assert(G(L)->tmudata == NULL); 35.214 + luai_userstateclose(L); 35.215 + close_state(L); 35.216 +} 35.217 +
36.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 36.2 +++ b/src/lua/lstate.h Sat Mar 03 11:07:39 2012 -0600 36.3 @@ -0,0 +1,169 @@ 36.4 +/* 36.5 +** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $ 36.6 +** Global State 36.7 +** See Copyright Notice in lua.h 36.8 +*/ 36.9 + 36.10 +#ifndef lstate_h 36.11 +#define lstate_h 36.12 + 36.13 +#include "lua.h" 36.14 + 36.15 +#include "lobject.h" 36.16 +#include "ltm.h" 36.17 +#include "lzio.h" 36.18 + 36.19 + 36.20 + 36.21 +struct lua_longjmp; /* defined in ldo.c */ 36.22 + 36.23 + 36.24 +/* table of globals */ 36.25 +#define gt(L) (&L->l_gt) 36.26 + 36.27 +/* registry */ 36.28 +#define registry(L) (&G(L)->l_registry) 36.29 + 36.30 + 36.31 +/* extra stack space to handle TM calls and some other extras */ 36.32 +#define EXTRA_STACK 5 36.33 + 36.34 + 36.35 +#define BASIC_CI_SIZE 8 36.36 + 36.37 +#define BASIC_STACK_SIZE (2*LUA_MINSTACK) 36.38 + 36.39 + 36.40 + 36.41 +typedef struct stringtable { 36.42 + GCObject **hash; 36.43 + lu_int32 nuse; /* number of elements */ 36.44 + int size; 36.45 +} stringtable; 36.46 + 36.47 + 36.48 +/* 36.49 +** informations about a call 36.50 +*/ 36.51 +typedef struct CallInfo { 36.52 + StkId base; /* base for this function */ 36.53 + StkId func; /* function index in the stack */ 36.54 + StkId top; /* top for this function */ 36.55 + const Instruction *savedpc; 36.56 + int nresults; /* expected number of results from this function */ 36.57 + int tailcalls; /* number of tail calls lost under this entry */ 36.58 +} CallInfo; 36.59 + 36.60 + 36.61 + 36.62 +#define curr_func(L) (clvalue(L->ci->func)) 36.63 +#define ci_func(ci) (clvalue((ci)->func)) 36.64 +#define f_isLua(ci) (!ci_func(ci)->c.isC) 36.65 +#define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci)) 36.66 + 36.67 + 36.68 +/* 36.69 +** `global state', shared by all threads of this state 36.70 +*/ 36.71 +typedef struct global_State { 36.72 + stringtable strt; /* hash table for strings */ 36.73 + lua_Alloc frealloc; /* function to reallocate memory */ 36.74 + void *ud; /* auxiliary data to `frealloc' */ 36.75 + lu_byte currentwhite; 36.76 + lu_byte gcstate; /* state of garbage collector */ 36.77 + int sweepstrgc; /* position of sweep in `strt' */ 36.78 + GCObject *rootgc; /* list of all collectable objects */ 36.79 + GCObject **sweepgc; /* position of sweep in `rootgc' */ 36.80 + GCObject *gray; /* list of gray objects */ 36.81 + GCObject *grayagain; /* list of objects to be traversed atomically */ 36.82 + GCObject *weak; /* list of weak tables (to be cleared) */ 36.83 + GCObject *tmudata; /* last element of list of userdata to be GC */ 36.84 + Mbuffer buff; /* temporary buffer for string concatentation */ 36.85 + lu_mem GCthreshold; 36.86 + lu_mem totalbytes; /* number of bytes currently allocated */ 36.87 + lu_mem estimate; /* an estimate of number of bytes actually in use */ 36.88 + lu_mem gcdept; /* how much GC is `behind schedule' */ 36.89 + int gcpause; /* size of pause between successive GCs */ 36.90 + int gcstepmul; /* GC `granularity' */ 36.91 + lua_CFunction panic; /* to be called in unprotected errors */ 36.92 + TValue l_registry; 36.93 + struct lua_State *mainthread; 36.94 + UpVal uvhead; /* head of double-linked list of all open upvalues */ 36.95 + struct Table *mt[NUM_TAGS]; /* metatables for basic types */ 36.96 + TString *tmname[TM_N]; /* array with tag-method names */ 36.97 +} global_State; 36.98 + 36.99 + 36.100 +/* 36.101 +** `per thread' state 36.102 +*/ 36.103 +struct lua_State { 36.104 + CommonHeader; 36.105 + lu_byte status; 36.106 + StkId top; /* first free slot in the stack */ 36.107 + StkId base; /* base of current function */ 36.108 + global_State *l_G; 36.109 + CallInfo *ci; /* call info for current function */ 36.110 + const Instruction *savedpc; /* `savedpc' of current function */ 36.111 + StkId stack_last; /* last free slot in the stack */ 36.112 + StkId stack; /* stack base */ 36.113 + CallInfo *end_ci; /* points after end of ci array*/ 36.114 + CallInfo *base_ci; /* array of CallInfo's */ 36.115 + int stacksize; 36.116 + int size_ci; /* size of array `base_ci' */ 36.117 + unsigned short nCcalls; /* number of nested C calls */ 36.118 + unsigned short baseCcalls; /* nested C calls when resuming coroutine */ 36.119 + lu_byte hookmask; 36.120 + lu_byte allowhook; 36.121 + int basehookcount; 36.122 + int hookcount; 36.123 + lua_Hook hook; 36.124 + TValue l_gt; /* table of globals */ 36.125 + TValue env; /* temporary place for environments */ 36.126 + GCObject *openupval; /* list of open upvalues in this stack */ 36.127 + GCObject *gclist; 36.128 + struct lua_longjmp *errorJmp; /* current error recover point */ 36.129 + ptrdiff_t errfunc; /* current error handling function (stack index) */ 36.130 +}; 36.131 + 36.132 + 36.133 +#define G(L) (L->l_G) 36.134 + 36.135 + 36.136 +/* 36.137 +** Union of all collectable objects 36.138 +*/ 36.139 +union GCObject { 36.140 + GCheader gch; 36.141 + union TString ts; 36.142 + union Udata u; 36.143 + union Closure cl; 36.144 + struct Table h; 36.145 + struct Proto p; 36.146 + struct UpVal uv; 36.147 + struct lua_State th; /* thread */ 36.148 +}; 36.149 + 36.150 + 36.151 +/* macros to convert a GCObject into a specific value */ 36.152 +#define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) 36.153 +#define gco2ts(o) (&rawgco2ts(o)->tsv) 36.154 +#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 36.155 +#define gco2u(o) (&rawgco2u(o)->uv) 36.156 +#define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) 36.157 +#define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 36.158 +#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 36.159 +#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 36.160 +#define ngcotouv(o) \ 36.161 + check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 36.162 +#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 36.163 + 36.164 +/* macro to convert any Lua object into a GCObject */ 36.165 +#define obj2gco(v) (cast(GCObject *, (v))) 36.166 + 36.167 + 36.168 +LUAI_FUNC lua_State *luaE_newthread (lua_State *L); 36.169 +LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 36.170 + 36.171 +#endif 36.172 +
37.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 37.2 +++ b/src/lua/lstring.c Sat Mar 03 11:07:39 2012 -0600 37.3 @@ -0,0 +1,111 @@ 37.4 +/* 37.5 +** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 37.6 +** String table (keeps all strings handled by Lua) 37.7 +** See Copyright Notice in lua.h 37.8 +*/ 37.9 + 37.10 + 37.11 +#include <string.h> 37.12 + 37.13 +#define lstring_c 37.14 +#define LUA_CORE 37.15 + 37.16 +#include "lua.h" 37.17 + 37.18 +#include "lmem.h" 37.19 +#include "lobject.h" 37.20 +#include "lstate.h" 37.21 +#include "lstring.h" 37.22 + 37.23 + 37.24 + 37.25 +void luaS_resize (lua_State *L, int newsize) { 37.26 + GCObject **newhash; 37.27 + stringtable *tb; 37.28 + int i; 37.29 + if (G(L)->gcstate == GCSsweepstring) 37.30 + return; /* cannot resize during GC traverse */ 37.31 + newhash = luaM_newvector(L, newsize, GCObject *); 37.32 + tb = &G(L)->strt; 37.33 + for (i=0; i<newsize; i++) newhash[i] = NULL; 37.34 + /* rehash */ 37.35 + for (i=0; i<tb->size; i++) { 37.36 + GCObject *p = tb->hash[i]; 37.37 + while (p) { /* for each node in the list */ 37.38 + GCObject *next = p->gch.next; /* save next */ 37.39 + unsigned int h = gco2ts(p)->hash; 37.40 + int h1 = lmod(h, newsize); /* new position */ 37.41 + lua_assert(cast_int(h%newsize) == lmod(h, newsize)); 37.42 + p->gch.next = newhash[h1]; /* chain it */ 37.43 + newhash[h1] = p; 37.44 + p = next; 37.45 + } 37.46 + } 37.47 + luaM_freearray(L, tb->hash, tb->size, TString *); 37.48 + tb->size = newsize; 37.49 + tb->hash = newhash; 37.50 +} 37.51 + 37.52 + 37.53 +static TString *newlstr (lua_State *L, const char *str, size_t l, 37.54 + unsigned int h) { 37.55 + TString *ts; 37.56 + stringtable *tb; 37.57 + if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 37.58 + luaM_toobig(L); 37.59 + ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString))); 37.60 + ts->tsv.len = l; 37.61 + ts->tsv.hash = h; 37.62 + ts->tsv.marked = luaC_white(G(L)); 37.63 + ts->tsv.tt = LUA_TSTRING; 37.64 + ts->tsv.reserved = 0; 37.65 + memcpy(ts+1, str, l*sizeof(char)); 37.66 + ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 37.67 + tb = &G(L)->strt; 37.68 + h = lmod(h, tb->size); 37.69 + ts->tsv.next = tb->hash[h]; /* chain new entry */ 37.70 + tb->hash[h] = obj2gco(ts); 37.71 + tb->nuse++; 37.72 + if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 37.73 + luaS_resize(L, tb->size*2); /* too crowded */ 37.74 + return ts; 37.75 +} 37.76 + 37.77 + 37.78 +TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 37.79 + GCObject *o; 37.80 + unsigned int h = cast(unsigned int, l); /* seed */ 37.81 + size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ 37.82 + size_t l1; 37.83 + for (l1=l; l1>=step; l1-=step) /* compute hash */ 37.84 + h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1])); 37.85 + for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; 37.86 + o != NULL; 37.87 + o = o->gch.next) { 37.88 + TString *ts = rawgco2ts(o); 37.89 + if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) { 37.90 + /* string may be dead */ 37.91 + if (isdead(G(L), o)) changewhite(o); 37.92 + return ts; 37.93 + } 37.94 + } 37.95 + return newlstr(L, str, l, h); /* not found */ 37.96 +} 37.97 + 37.98 + 37.99 +Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { 37.100 + Udata *u; 37.101 + if (s > MAX_SIZET - sizeof(Udata)) 37.102 + luaM_toobig(L); 37.103 + u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata))); 37.104 + u->uv.marked = luaC_white(G(L)); /* is not finalized */ 37.105 + u->uv.tt = LUA_TUSERDATA; 37.106 + u->uv.len = s; 37.107 + u->uv.metatable = NULL; 37.108 + u->uv.env = e; 37.109 + /* chain it on udata list (after main thread) */ 37.110 + u->uv.next = G(L)->mainthread->next; 37.111 + G(L)->mainthread->next = obj2gco(u); 37.112 + return u; 37.113 +} 37.114 +
38.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 38.2 +++ b/src/lua/lstring.h Sat Mar 03 11:07:39 2012 -0600 38.3 @@ -0,0 +1,31 @@ 38.4 +/* 38.5 +** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $ 38.6 +** String table (keep all strings handled by Lua) 38.7 +** See Copyright Notice in lua.h 38.8 +*/ 38.9 + 38.10 +#ifndef lstring_h 38.11 +#define lstring_h 38.12 + 38.13 + 38.14 +#include "lgc.h" 38.15 +#include "lobject.h" 38.16 +#include "lstate.h" 38.17 + 38.18 + 38.19 +#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 38.20 + 38.21 +#define sizeudata(u) (sizeof(union Udata)+(u)->len) 38.22 + 38.23 +#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) 38.24 +#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 38.25 + (sizeof(s)/sizeof(char))-1)) 38.26 + 38.27 +#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 38.28 + 38.29 +LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 38.30 +LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 38.31 +LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 38.32 + 38.33 + 38.34 +#endif
39.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 39.2 +++ b/src/lua/lstrlib.c Sat Mar 03 11:07:39 2012 -0600 39.3 @@ -0,0 +1,869 @@ 39.4 +/* 39.5 +** $Id: lstrlib.c,v 1.132.1.4 2008/07/11 17:27:21 roberto Exp $ 39.6 +** Standard library for string operations and pattern-matching 39.7 +** See Copyright Notice in lua.h 39.8 +*/ 39.9 + 39.10 + 39.11 +#include <ctype.h> 39.12 +#include <stddef.h> 39.13 +#include <stdio.h> 39.14 +#include <stdlib.h> 39.15 +#include <string.h> 39.16 + 39.17 +#define lstrlib_c 39.18 +#define LUA_LIB 39.19 + 39.20 +#include "lua.h" 39.21 + 39.22 +#include "lauxlib.h" 39.23 +#include "lualib.h" 39.24 + 39.25 + 39.26 +/* macro to `unsign' a character */ 39.27 +#define uchar(c) ((unsigned char)(c)) 39.28 + 39.29 + 39.30 + 39.31 +static int str_len (lua_State *L) { 39.32 + size_t l; 39.33 + luaL_checklstring(L, 1, &l); 39.34 + lua_pushinteger(L, l); 39.35 + return 1; 39.36 +} 39.37 + 39.38 + 39.39 +static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) { 39.40 + /* relative string position: negative means back from end */ 39.41 + if (pos < 0) pos += (ptrdiff_t)len + 1; 39.42 + return (pos >= 0) ? pos : 0; 39.43 +} 39.44 + 39.45 + 39.46 +static int str_sub (lua_State *L) { 39.47 + size_t l; 39.48 + const char *s = luaL_checklstring(L, 1, &l); 39.49 + ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l); 39.50 + ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l); 39.51 + if (start < 1) start = 1; 39.52 + if (end > (ptrdiff_t)l) end = (ptrdiff_t)l; 39.53 + if (start <= end) 39.54 + lua_pushlstring(L, s+start-1, end-start+1); 39.55 + else lua_pushliteral(L, ""); 39.56 + return 1; 39.57 +} 39.58 + 39.59 + 39.60 +static int str_reverse (lua_State *L) { 39.61 + size_t l; 39.62 + luaL_Buffer b; 39.63 + const char *s = luaL_checklstring(L, 1, &l); 39.64 + luaL_buffinit(L, &b); 39.65 + while (l--) luaL_addchar(&b, s[l]); 39.66 + luaL_pushresult(&b); 39.67 + return 1; 39.68 +} 39.69 + 39.70 + 39.71 +static int str_lower (lua_State *L) { 39.72 + size_t l; 39.73 + size_t i; 39.74 + luaL_Buffer b; 39.75 + const char *s = luaL_checklstring(L, 1, &l); 39.76 + luaL_buffinit(L, &b); 39.77 + for (i=0; i<l; i++) 39.78 + luaL_addchar(&b, tolower(uchar(s[i]))); 39.79 + luaL_pushresult(&b); 39.80 + return 1; 39.81 +} 39.82 + 39.83 + 39.84 +static int str_upper (lua_State *L) { 39.85 + size_t l; 39.86 + size_t i; 39.87 + luaL_Buffer b; 39.88 + const char *s = luaL_checklstring(L, 1, &l); 39.89 + luaL_buffinit(L, &b); 39.90 + for (i=0; i<l; i++) 39.91 + luaL_addchar(&b, toupper(uchar(s[i]))); 39.92 + luaL_pushresult(&b); 39.93 + return 1; 39.94 +} 39.95 + 39.96 +static int str_rep (lua_State *L) { 39.97 + size_t l; 39.98 + luaL_Buffer b; 39.99 + const char *s = luaL_checklstring(L, 1, &l); 39.100 + int n = luaL_checkint(L, 2); 39.101 + luaL_buffinit(L, &b); 39.102 + while (n-- > 0) 39.103 + luaL_addlstring(&b, s, l); 39.104 + luaL_pushresult(&b); 39.105 + return 1; 39.106 +} 39.107 + 39.108 + 39.109 +static int str_byte (lua_State *L) { 39.110 + size_t l; 39.111 + const char *s = luaL_checklstring(L, 1, &l); 39.112 + ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l); 39.113 + ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l); 39.114 + int n, i; 39.115 + if (posi <= 0) posi = 1; 39.116 + if ((size_t)pose > l) pose = l; 39.117 + if (posi > pose) return 0; /* empty interval; return no values */ 39.118 + n = (int)(pose - posi + 1); 39.119 + if (posi + n <= pose) /* overflow? */ 39.120 + luaL_error(L, "string slice too long"); 39.121 + luaL_checkstack(L, n, "string slice too long"); 39.122 + for (i=0; i<n; i++) 39.123 + lua_pushinteger(L, uchar(s[posi+i-1])); 39.124 + return n; 39.125 +} 39.126 + 39.127 + 39.128 +static int str_char (lua_State *L) { 39.129 + int n = lua_gettop(L); /* number of arguments */ 39.130 + int i; 39.131 + luaL_Buffer b; 39.132 + luaL_buffinit(L, &b); 39.133 + for (i=1; i<=n; i++) { 39.134 + int c = luaL_checkint(L, i); 39.135 + luaL_argcheck(L, uchar(c) == c, i, "invalid value"); 39.136 + luaL_addchar(&b, uchar(c)); 39.137 + } 39.138 + luaL_pushresult(&b); 39.139 + return 1; 39.140 +} 39.141 + 39.142 + 39.143 +static int writer (lua_State *L, const void* b, size_t size, void* B) { 39.144 + (void)L; 39.145 + luaL_addlstring((luaL_Buffer*) B, (const char *)b, size); 39.146 + return 0; 39.147 +} 39.148 + 39.149 + 39.150 +static int str_dump (lua_State *L) { 39.151 + luaL_Buffer b; 39.152 + luaL_checktype(L, 1, LUA_TFUNCTION); 39.153 + lua_settop(L, 1); 39.154 + luaL_buffinit(L,&b); 39.155 + if (lua_dump(L, writer, &b) != 0) 39.156 + luaL_error(L, "unable to dump given function"); 39.157 + luaL_pushresult(&b); 39.158 + return 1; 39.159 +} 39.160 + 39.161 + 39.162 + 39.163 +/* 39.164 +** {====================================================== 39.165 +** PATTERN MATCHING 39.166 +** ======================================================= 39.167 +*/ 39.168 + 39.169 + 39.170 +#define CAP_UNFINISHED (-1) 39.171 +#define CAP_POSITION (-2) 39.172 + 39.173 +typedef struct MatchState { 39.174 + const char *src_init; /* init of source string */ 39.175 + const char *src_end; /* end (`\0') of source string */ 39.176 + lua_State *L; 39.177 + int level; /* total number of captures (finished or unfinished) */ 39.178 + struct { 39.179 + const char *init; 39.180 + ptrdiff_t len; 39.181 + } capture[LUA_MAXCAPTURES]; 39.182 +} MatchState; 39.183 + 39.184 + 39.185 +#define L_ESC '%' 39.186 +#define SPECIALS "^$*+?.([%-" 39.187 + 39.188 + 39.189 +static int check_capture (MatchState *ms, int l) { 39.190 + l -= '1'; 39.191 + if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) 39.192 + return luaL_error(ms->L, "invalid capture index"); 39.193 + return l; 39.194 +} 39.195 + 39.196 + 39.197 +static int capture_to_close (MatchState *ms) { 39.198 + int level = ms->level; 39.199 + for (level--; level>=0; level--) 39.200 + if (ms->capture[level].len == CAP_UNFINISHED) return level; 39.201 + return luaL_error(ms->L, "invalid pattern capture"); 39.202 +} 39.203 + 39.204 + 39.205 +static const char *classend (MatchState *ms, const char *p) { 39.206 + switch (*p++) { 39.207 + case L_ESC: { 39.208 + if (*p == '\0') 39.209 + luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); 39.210 + return p+1; 39.211 + } 39.212 + case '[': { 39.213 + if (*p == '^') p++; 39.214 + do { /* look for a `]' */ 39.215 + if (*p == '\0') 39.216 + luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); 39.217 + if (*(p++) == L_ESC && *p != '\0') 39.218 + p++; /* skip escapes (e.g. `%]') */ 39.219 + } while (*p != ']'); 39.220 + return p+1; 39.221 + } 39.222 + default: { 39.223 + return p; 39.224 + } 39.225 + } 39.226 +} 39.227 + 39.228 + 39.229 +static int match_class (int c, int cl) { 39.230 + int res; 39.231 + switch (tolower(cl)) { 39.232 + case 'a' : res = isalpha(c); break; 39.233 + case 'c' : res = iscntrl(c); break; 39.234 + case 'd' : res = isdigit(c); break; 39.235 + case 'l' : res = islower(c); break; 39.236 + case 'p' : res = ispunct(c); break; 39.237 + case 's' : res = isspace(c); break; 39.238 + case 'u' : res = isupper(c); break; 39.239 + case 'w' : res = isalnum(c); break; 39.240 + case 'x' : res = isxdigit(c); break; 39.241 + case 'z' : res = (c == 0); break; 39.242 + default: return (cl == c); 39.243 + } 39.244 + return (islower(cl) ? res : !res); 39.245 +} 39.246 + 39.247 + 39.248 +static int matchbracketclass (int c, const char *p, const char *ec) { 39.249 + int sig = 1; 39.250 + if (*(p+1) == '^') { 39.251 + sig = 0; 39.252 + p++; /* skip the `^' */ 39.253 + } 39.254 + while (++p < ec) { 39.255 + if (*p == L_ESC) { 39.256 + p++; 39.257 + if (match_class(c, uchar(*p))) 39.258 + return sig; 39.259 + } 39.260 + else if ((*(p+1) == '-') && (p+2 < ec)) { 39.261 + p+=2; 39.262 + if (uchar(*(p-2)) <= c && c <= uchar(*p)) 39.263 + return sig; 39.264 + } 39.265 + else if (uchar(*p) == c) return sig; 39.266 + } 39.267 + return !sig; 39.268 +} 39.269 + 39.270 + 39.271 +static int singlematch (int c, const char *p, const char *ep) { 39.272 + switch (*p) { 39.273 + case '.': return 1; /* matches any char */ 39.274 + case L_ESC: return match_class(c, uchar(*(p+1))); 39.275 + case '[': return matchbracketclass(c, p, ep-1); 39.276 + default: return (uchar(*p) == c); 39.277 + } 39.278 +} 39.279 + 39.280 + 39.281 +static const char *match (MatchState *ms, const char *s, const char *p); 39.282 + 39.283 + 39.284 +static const char *matchbalance (MatchState *ms, const char *s, 39.285 + const char *p) { 39.286 + if (*p == 0 || *(p+1) == 0) 39.287 + luaL_error(ms->L, "unbalanced pattern"); 39.288 + if (*s != *p) return NULL; 39.289 + else { 39.290 + int b = *p; 39.291 + int e = *(p+1); 39.292 + int cont = 1; 39.293 + while (++s < ms->src_end) { 39.294 + if (*s == e) { 39.295 + if (--cont == 0) return s+1; 39.296 + } 39.297 + else if (*s == b) cont++; 39.298 + } 39.299 + } 39.300 + return NULL; /* string ends out of balance */ 39.301 +} 39.302 + 39.303 + 39.304 +static const char *max_expand (MatchState *ms, const char *s, 39.305 + const char *p, const char *ep) { 39.306 + ptrdiff_t i = 0; /* counts maximum expand for item */ 39.307 + while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep)) 39.308 + i++; 39.309 + /* keeps trying to match with the maximum repetitions */ 39.310 + while (i>=0) { 39.311 + const char *res = match(ms, (s+i), ep+1); 39.312 + if (res) return res; 39.313 + i--; /* else didn't match; reduce 1 repetition to try again */ 39.314 + } 39.315 + return NULL; 39.316 +} 39.317 + 39.318 + 39.319 +static const char *min_expand (MatchState *ms, const char *s, 39.320 + const char *p, const char *ep) { 39.321 + for (;;) { 39.322 + const char *res = match(ms, s, ep+1); 39.323 + if (res != NULL) 39.324 + return res; 39.325 + else if (s<ms->src_end && singlematch(uchar(*s), p, ep)) 39.326 + s++; /* try with one more repetition */ 39.327 + else return NULL; 39.328 + } 39.329 +} 39.330 + 39.331 + 39.332 +static const char *start_capture (MatchState *ms, const char *s, 39.333 + const char *p, int what) { 39.334 + const char *res; 39.335 + int level = ms->level; 39.336 + if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); 39.337 + ms->capture[level].init = s; 39.338 + ms->capture[level].len = what; 39.339 + ms->level = level+1; 39.340 + if ((res=match(ms, s, p)) == NULL) /* match failed? */ 39.341 + ms->level--; /* undo capture */ 39.342 + return res; 39.343 +} 39.344 + 39.345 + 39.346 +static const char *end_capture (MatchState *ms, const char *s, 39.347 + const char *p) { 39.348 + int l = capture_to_close(ms); 39.349 + const char *res; 39.350 + ms->capture[l].len = s - ms->capture[l].init; /* close capture */ 39.351 + if ((res = match(ms, s, p)) == NULL) /* match failed? */ 39.352 + ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ 39.353 + return res; 39.354 +} 39.355 + 39.356 + 39.357 +static const char *match_capture (MatchState *ms, const char *s, int l) { 39.358 + size_t len; 39.359 + l = check_capture(ms, l); 39.360 + len = ms->capture[l].len; 39.361 + if ((size_t)(ms->src_end-s) >= len && 39.362 + memcmp(ms->capture[l].init, s, len) == 0) 39.363 + return s+len; 39.364 + else return NULL; 39.365 +} 39.366 + 39.367 + 39.368 +static const char *match (MatchState *ms, const char *s, const char *p) { 39.369 + init: /* using goto's to optimize tail recursion */ 39.370 + switch (*p) { 39.371 + case '(': { /* start capture */ 39.372 + if (*(p+1) == ')') /* position capture? */ 39.373 + return start_capture(ms, s, p+2, CAP_POSITION); 39.374 + else 39.375 + return start_capture(ms, s, p+1, CAP_UNFINISHED); 39.376 + } 39.377 + case ')': { /* end capture */ 39.378 + return end_capture(ms, s, p+1); 39.379 + } 39.380 + case L_ESC: { 39.381 + switch (*(p+1)) { 39.382 + case 'b': { /* balanced string? */ 39.383 + s = matchbalance(ms, s, p+2); 39.384 + if (s == NULL) return NULL; 39.385 + p+=4; goto init; /* else return match(ms, s, p+4); */ 39.386 + } 39.387 + case 'f': { /* frontier? */ 39.388 + const char *ep; char previous; 39.389 + p += 2; 39.390 + if (*p != '[') 39.391 + luaL_error(ms->L, "missing " LUA_QL("[") " after " 39.392 + LUA_QL("%%f") " in pattern"); 39.393 + ep = classend(ms, p); /* points to what is next */ 39.394 + previous = (s == ms->src_init) ? '\0' : *(s-1); 39.395 + if (matchbracketclass(uchar(previous), p, ep-1) || 39.396 + !matchbracketclass(uchar(*s), p, ep-1)) return NULL; 39.397 + p=ep; goto init; /* else return match(ms, s, ep); */ 39.398 + } 39.399 + default: { 39.400 + if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */ 39.401 + s = match_capture(ms, s, uchar(*(p+1))); 39.402 + if (s == NULL) return NULL; 39.403 + p+=2; goto init; /* else return match(ms, s, p+2) */ 39.404 + } 39.405 + goto dflt; /* case default */ 39.406 + } 39.407 + } 39.408 + } 39.409 + case '\0': { /* end of pattern */ 39.410 + return s; /* match succeeded */ 39.411 + } 39.412 + case '$': { 39.413 + if (*(p+1) == '\0') /* is the `$' the last char in pattern? */ 39.414 + return (s == ms->src_end) ? s : NULL; /* check end of string */ 39.415 + else goto dflt; 39.416 + } 39.417 + default: dflt: { /* it is a pattern item */ 39.418 + const char *ep = classend(ms, p); /* points to what is next */ 39.419 + int m = s<ms->src_end && singlematch(uchar(*s), p, ep); 39.420 + switch (*ep) { 39.421 + case '?': { /* optional */ 39.422 + const char *res; 39.423 + if (m && ((res=match(ms, s+1, ep+1)) != NULL)) 39.424 + return res; 39.425 + p=ep+1; goto init; /* else return match(ms, s, ep+1); */ 39.426 + } 39.427 + case '*': { /* 0 or more repetitions */ 39.428 + return max_expand(ms, s, p, ep); 39.429 + } 39.430 + case '+': { /* 1 or more repetitions */ 39.431 + return (m ? max_expand(ms, s+1, p, ep) : NULL); 39.432 + } 39.433 + case '-': { /* 0 or more repetitions (minimum) */ 39.434 + return min_expand(ms, s, p, ep); 39.435 + } 39.436 + default: { 39.437 + if (!m) return NULL; 39.438 + s++; p=ep; goto init; /* else return match(ms, s+1, ep); */ 39.439 + } 39.440 + } 39.441 + } 39.442 + } 39.443 +} 39.444 + 39.445 + 39.446 + 39.447 +static const char *lmemfind (const char *s1, size_t l1, 39.448 + const char *s2, size_t l2) { 39.449 + if (l2 == 0) return s1; /* empty strings are everywhere */ 39.450 + else if (l2 > l1) return NULL; /* avoids a negative `l1' */ 39.451 + else { 39.452 + const char *init; /* to search for a `*s2' inside `s1' */ 39.453 + l2--; /* 1st char will be checked by `memchr' */ 39.454 + l1 = l1-l2; /* `s2' cannot be found after that */ 39.455 + while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { 39.456 + init++; /* 1st char is already checked */ 39.457 + if (memcmp(init, s2+1, l2) == 0) 39.458 + return init-1; 39.459 + else { /* correct `l1' and `s1' to try again */ 39.460 + l1 -= init-s1; 39.461 + s1 = init; 39.462 + } 39.463 + } 39.464 + return NULL; /* not found */ 39.465 + } 39.466 +} 39.467 + 39.468 + 39.469 +static void push_onecapture (MatchState *ms, int i, const char *s, 39.470 + const char *e) { 39.471 + if (i >= ms->level) { 39.472 + if (i == 0) /* ms->level == 0, too */ 39.473 + lua_pushlstring(ms->L, s, e - s); /* add whole match */ 39.474 + else 39.475 + luaL_error(ms->L, "invalid capture index"); 39.476 + } 39.477 + else { 39.478 + ptrdiff_t l = ms->capture[i].len; 39.479 + if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); 39.480 + if (l == CAP_POSITION) 39.481 + lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); 39.482 + else 39.483 + lua_pushlstring(ms->L, ms->capture[i].init, l); 39.484 + } 39.485 +} 39.486 + 39.487 + 39.488 +static int push_captures (MatchState *ms, const char *s, const char *e) { 39.489 + int i; 39.490 + int nlevels = (ms->level == 0 && s) ? 1 : ms->level; 39.491 + luaL_checkstack(ms->L, nlevels, "too many captures"); 39.492 + for (i = 0; i < nlevels; i++) 39.493 + push_onecapture(ms, i, s, e); 39.494 + return nlevels; /* number of strings pushed */ 39.495 +} 39.496 + 39.497 + 39.498 +static int str_find_aux (lua_State *L, int find) { 39.499 + size_t l1, l2; 39.500 + const char *s = luaL_checklstring(L, 1, &l1); 39.501 + const char *p = luaL_checklstring(L, 2, &l2); 39.502 + ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; 39.503 + if (init < 0) init = 0; 39.504 + else if ((size_t)(init) > l1) init = (ptrdiff_t)l1; 39.505 + if (find && (lua_toboolean(L, 4) || /* explicit request? */ 39.506 + strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ 39.507 + /* do a plain search */ 39.508 + const char *s2 = lmemfind(s+init, l1-init, p, l2); 39.509 + if (s2) { 39.510 + lua_pushinteger(L, s2-s+1); 39.511 + lua_pushinteger(L, s2-s+l2); 39.512 + return 2; 39.513 + } 39.514 + } 39.515 + else { 39.516 + MatchState ms; 39.517 + int anchor = (*p == '^') ? (p++, 1) : 0; 39.518 + const char *s1=s+init; 39.519 + ms.L = L; 39.520 + ms.src_init = s; 39.521 + ms.src_end = s+l1; 39.522 + do { 39.523 + const char *res; 39.524 + ms.level = 0; 39.525 + if ((res=match(&ms, s1, p)) != NULL) { 39.526 + if (find) { 39.527 + lua_pushinteger(L, s1-s+1); /* start */ 39.528 + lua_pushinteger(L, res-s); /* end */ 39.529 + return push_captures(&ms, NULL, 0) + 2; 39.530 + } 39.531 + else 39.532 + return push_captures(&ms, s1, res); 39.533 + } 39.534 + } while (s1++ < ms.src_end && !anchor); 39.535 + } 39.536 + lua_pushnil(L); /* not found */ 39.537 + return 1; 39.538 +} 39.539 + 39.540 + 39.541 +static int str_find (lua_State *L) { 39.542 + return str_find_aux(L, 1); 39.543 +} 39.544 + 39.545 + 39.546 +static int str_match (lua_State *L) { 39.547 + return str_find_aux(L, 0); 39.548 +} 39.549 + 39.550 + 39.551 +static int gmatch_aux (lua_State *L) { 39.552 + MatchState ms; 39.553 + size_t ls; 39.554 + const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); 39.555 + const char *p = lua_tostring(L, lua_upvalueindex(2)); 39.556 + const char *src; 39.557 + ms.L = L; 39.558 + ms.src_init = s; 39.559 + ms.src_end = s+ls; 39.560 + for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3)); 39.561 + src <= ms.src_end; 39.562 + src++) { 39.563 + const char *e; 39.564 + ms.level = 0; 39.565 + if ((e = match(&ms, src, p)) != NULL) { 39.566 + lua_Integer newstart = e-s; 39.567 + if (e == src) newstart++; /* empty match? go at least one position */ 39.568 + lua_pushinteger(L, newstart); 39.569 + lua_replace(L, lua_upvalueindex(3)); 39.570 + return push_captures(&ms, src, e); 39.571 + } 39.572 + } 39.573 + return 0; /* not found */ 39.574 +} 39.575 + 39.576 + 39.577 +static int gmatch (lua_State *L) { 39.578 + luaL_checkstring(L, 1); 39.579 + luaL_checkstring(L, 2); 39.580 + lua_settop(L, 2); 39.581 + lua_pushinteger(L, 0); 39.582 + lua_pushcclosure(L, gmatch_aux, 3); 39.583 + return 1; 39.584 +} 39.585 + 39.586 + 39.587 +static int gfind_nodef (lua_State *L) { 39.588 + return luaL_error(L, LUA_QL("string.gfind") " was renamed to " 39.589 + LUA_QL("string.gmatch")); 39.590 +} 39.591 + 39.592 + 39.593 +static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, 39.594 + const char *e) { 39.595 + size_t l, i; 39.596 + const char *news = lua_tolstring(ms->L, 3, &l); 39.597 + for (i = 0; i < l; i++) { 39.598 + if (news[i] != L_ESC) 39.599 + luaL_addchar(b, news[i]); 39.600 + else { 39.601 + i++; /* skip ESC */ 39.602 + if (!isdigit(uchar(news[i]))) 39.603 + luaL_addchar(b, news[i]); 39.604 + else if (news[i] == '0') 39.605 + luaL_addlstring(b, s, e - s); 39.606 + else { 39.607 + push_onecapture(ms, news[i] - '1', s, e); 39.608 + luaL_addvalue(b); /* add capture to accumulated result */ 39.609 + } 39.610 + } 39.611 + } 39.612 +} 39.613 + 39.614 + 39.615 +static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, 39.616 + const char *e) { 39.617 + lua_State *L = ms->L; 39.618 + switch (lua_type(L, 3)) { 39.619 + case LUA_TNUMBER: 39.620 + case LUA_TSTRING: { 39.621 + add_s(ms, b, s, e); 39.622 + return; 39.623 + } 39.624 + case LUA_TFUNCTION: { 39.625 + int n; 39.626 + lua_pushvalue(L, 3); 39.627 + n = push_captures(ms, s, e); 39.628 + lua_call(L, n, 1); 39.629 + break; 39.630 + } 39.631 + case LUA_TTABLE: { 39.632 + push_onecapture(ms, 0, s, e); 39.633 + lua_gettable(L, 3); 39.634 + break; 39.635 + } 39.636 + } 39.637 + if (!lua_toboolean(L, -1)) { /* nil or false? */ 39.638 + lua_pop(L, 1); 39.639 + lua_pushlstring(L, s, e - s); /* keep original text */ 39.640 + } 39.641 + else if (!lua_isstring(L, -1)) 39.642 + luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); 39.643 + luaL_addvalue(b); /* add result to accumulator */ 39.644 +} 39.645 + 39.646 + 39.647 +static int str_gsub (lua_State *L) { 39.648 + size_t srcl; 39.649 + const char *src = luaL_checklstring(L, 1, &srcl); 39.650 + const char *p = luaL_checkstring(L, 2); 39.651 + int tr = lua_type(L, 3); 39.652 + int max_s = luaL_optint(L, 4, srcl+1); 39.653 + int anchor = (*p == '^') ? (p++, 1) : 0; 39.654 + int n = 0; 39.655 + MatchState ms; 39.656 + luaL_Buffer b; 39.657 + luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || 39.658 + tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, 39.659 + "string/function/table expected"); 39.660 + luaL_buffinit(L, &b); 39.661 + ms.L = L; 39.662 + ms.src_init = src; 39.663 + ms.src_end = src+srcl; 39.664 + while (n < max_s) { 39.665 + const char *e; 39.666 + ms.level = 0; 39.667 + e = match(&ms, src, p); 39.668 + if (e) { 39.669 + n++; 39.670 + add_value(&ms, &b, src, e); 39.671 + } 39.672 + if (e && e>src) /* non empty match? */ 39.673 + src = e; /* skip it */ 39.674 + else if (src < ms.src_end) 39.675 + luaL_addchar(&b, *src++); 39.676 + else break; 39.677 + if (anchor) break; 39.678 + } 39.679 + luaL_addlstring(&b, src, ms.src_end-src); 39.680 + luaL_pushresult(&b); 39.681 + lua_pushinteger(L, n); /* number of substitutions */ 39.682 + return 2; 39.683 +} 39.684 + 39.685 +/* }====================================================== */ 39.686 + 39.687 + 39.688 +/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ 39.689 +#define MAX_ITEM 512 39.690 +/* valid flags in a format specification */ 39.691 +#define FLAGS "-+ #0" 39.692 +/* 39.693 +** maximum size of each format specification (such as '%-099.99d') 39.694 +** (+10 accounts for %99.99x plus margin of error) 39.695 +*/ 39.696 +#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10) 39.697 + 39.698 + 39.699 +static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { 39.700 + size_t l; 39.701 + const char *s = luaL_checklstring(L, arg, &l); 39.702 + luaL_addchar(b, '"'); 39.703 + while (l--) { 39.704 + switch (*s) { 39.705 + case '"': case '\\': case '\n': { 39.706 + luaL_addchar(b, '\\'); 39.707 + luaL_addchar(b, *s); 39.708 + break; 39.709 + } 39.710 + case '\r': { 39.711 + luaL_addlstring(b, "\\r", 2); 39.712 + break; 39.713 + } 39.714 + case '\0': { 39.715 + luaL_addlstring(b, "\\000", 4); 39.716 + break; 39.717 + } 39.718 + default: { 39.719 + luaL_addchar(b, *s); 39.720 + break; 39.721 + } 39.722 + } 39.723 + s++; 39.724 + } 39.725 + luaL_addchar(b, '"'); 39.726 +} 39.727 + 39.728 +static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { 39.729 + const char *p = strfrmt; 39.730 + while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ 39.731 + if ((size_t)(p - strfrmt) >= sizeof(FLAGS)) 39.732 + luaL_error(L, "invalid format (repeated flags)"); 39.733 + if (isdigit(uchar(*p))) p++; /* skip width */ 39.734 + if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ 39.735 + if (*p == '.') { 39.736 + p++; 39.737 + if (isdigit(uchar(*p))) p++; /* skip precision */ 39.738 + if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ 39.739 + } 39.740 + if (isdigit(uchar(*p))) 39.741 + luaL_error(L, "invalid format (width or precision too long)"); 39.742 + *(form++) = '%'; 39.743 + strncpy(form, strfrmt, p - strfrmt + 1); 39.744 + form += p - strfrmt + 1; 39.745 + *form = '\0'; 39.746 + return p; 39.747 +} 39.748 + 39.749 + 39.750 +static void addintlen (char *form) { 39.751 + size_t l = strlen(form); 39.752 + char spec = form[l - 1]; 39.753 + strcpy(form + l - 1, LUA_INTFRMLEN); 39.754 + form[l + sizeof(LUA_INTFRMLEN) - 2] = spec; 39.755 + form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0'; 39.756 +} 39.757 + 39.758 + 39.759 +static int str_format (lua_State *L) { 39.760 + int arg = 1; 39.761 + size_t sfl; 39.762 + const char *strfrmt = luaL_checklstring(L, arg, &sfl); 39.763 + const char *strfrmt_end = strfrmt+sfl; 39.764 + luaL_Buffer b; 39.765 + luaL_buffinit(L, &b); 39.766 + while (strfrmt < strfrmt_end) { 39.767 + if (*strfrmt != L_ESC) 39.768 + luaL_addchar(&b, *strfrmt++); 39.769 + else if (*++strfrmt == L_ESC) 39.770 + luaL_addchar(&b, *strfrmt++); /* %% */ 39.771 + else { /* format item */ 39.772 + char form[MAX_FORMAT]; /* to store the format (`%...') */ 39.773 + char buff[MAX_ITEM]; /* to store the formatted item */ 39.774 + arg++; 39.775 + strfrmt = scanformat(L, strfrmt, form); 39.776 + switch (*strfrmt++) { 39.777 + case 'c': { 39.778 + sprintf(buff, form, (int)luaL_checknumber(L, arg)); 39.779 + break; 39.780 + } 39.781 + case 'd': case 'i': { 39.782 + addintlen(form); 39.783 + sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); 39.784 + break; 39.785 + } 39.786 + case 'o': case 'u': case 'x': case 'X': { 39.787 + addintlen(form); 39.788 + sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); 39.789 + break; 39.790 + } 39.791 + case 'e': case 'E': case 'f': 39.792 + case 'g': case 'G': { 39.793 + sprintf(buff, form, (double)luaL_checknumber(L, arg)); 39.794 + break; 39.795 + } 39.796 + case 'q': { 39.797 + addquoted(L, &b, arg); 39.798 + continue; /* skip the 'addsize' at the end */ 39.799 + } 39.800 + case 's': { 39.801 + size_t l; 39.802 + const char *s = luaL_checklstring(L, arg, &l); 39.803 + if (!strchr(form, '.') && l >= 100) { 39.804 + /* no precision and string is too long to be formatted; 39.805 + keep original string */ 39.806 + lua_pushvalue(L, arg); 39.807 + luaL_addvalue(&b); 39.808 + continue; /* skip the `addsize' at the end */ 39.809 + } 39.810 + else { 39.811 + sprintf(buff, form, s); 39.812 + break; 39.813 + } 39.814 + } 39.815 + default: { /* also treat cases `pnLlh' */ 39.816 + return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " 39.817 + LUA_QL("format"), *(strfrmt - 1)); 39.818 + } 39.819 + } 39.820 + luaL_addlstring(&b, buff, strlen(buff)); 39.821 + } 39.822 + } 39.823 + luaL_pushresult(&b); 39.824 + return 1; 39.825 +} 39.826 + 39.827 + 39.828 +static const luaL_Reg strlib[] = { 39.829 + {"byte", str_byte}, 39.830 + {"char", str_char}, 39.831 + {"dump", str_dump}, 39.832 + {"find", str_find}, 39.833 + {"format", str_format}, 39.834 + {"gfind", gfind_nodef}, 39.835 + {"gmatch", gmatch}, 39.836 + {"gsub", str_gsub}, 39.837 + {"len", str_len}, 39.838 + {"lower", str_lower}, 39.839 + {"match", str_match}, 39.840 + {"rep", str_rep}, 39.841 + {"reverse", str_reverse}, 39.842 + {"sub", str_sub}, 39.843 + {"upper", str_upper}, 39.844 + {NULL, NULL} 39.845 +}; 39.846 + 39.847 + 39.848 +static void createmetatable (lua_State *L) { 39.849 + lua_createtable(L, 0, 1); /* create metatable for strings */ 39.850 + lua_pushliteral(L, ""); /* dummy string */ 39.851 + lua_pushvalue(L, -2); 39.852 + lua_setmetatable(L, -2); /* set string metatable */ 39.853 + lua_pop(L, 1); /* pop dummy string */ 39.854 + lua_pushvalue(L, -2); /* string library... */ 39.855 + lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */ 39.856 + lua_pop(L, 1); /* pop metatable */ 39.857 +} 39.858 + 39.859 + 39.860 +/* 39.861 +** Open string library 39.862 +*/ 39.863 +LUALIB_API int luaopen_string (lua_State *L) { 39.864 + luaL_register(L, LUA_STRLIBNAME, strlib); 39.865 +#if defined(LUA_COMPAT_GFIND) 39.866 + lua_getfield(L, -1, "gmatch"); 39.867 + lua_setfield(L, -2, "gfind"); 39.868 +#endif 39.869 + createmetatable(L); 39.870 + return 1; 39.871 +} 39.872 +
40.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 40.2 +++ b/src/lua/ltable.c Sat Mar 03 11:07:39 2012 -0600 40.3 @@ -0,0 +1,588 @@ 40.4 +/* 40.5 +** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $ 40.6 +** Lua tables (hash) 40.7 +** See Copyright Notice in lua.h 40.8 +*/ 40.9 + 40.10 + 40.11 +/* 40.12 +** Implementation of tables (aka arrays, objects, or hash tables). 40.13 +** Tables keep its elements in two parts: an array part and a hash part. 40.14 +** Non-negative integer keys are all candidates to be kept in the array 40.15 +** part. The actual size of the array is the largest `n' such that at 40.16 +** least half the slots between 0 and n are in use. 40.17 +** Hash uses a mix of chained scatter table with Brent's variation. 40.18 +** A main invariant of these tables is that, if an element is not 40.19 +** in its main position (i.e. the `original' position that its hash gives 40.20 +** to it), then the colliding element is in its own main position. 40.21 +** Hence even when the load factor reaches 100%, performance remains good. 40.22 +*/ 40.23 + 40.24 +#include <math.h> 40.25 +#include <string.h> 40.26 + 40.27 +#define ltable_c 40.28 +#define LUA_CORE 40.29 + 40.30 +#include "lua.h" 40.31 + 40.32 +#include "ldebug.h" 40.33 +#include "ldo.h" 40.34 +#include "lgc.h" 40.35 +#include "lmem.h" 40.36 +#include "lobject.h" 40.37 +#include "lstate.h" 40.38 +#include "ltable.h" 40.39 + 40.40 + 40.41 +/* 40.42 +** max size of array part is 2^MAXBITS 40.43 +*/ 40.44 +#if LUAI_BITSINT > 26 40.45 +#define MAXBITS 26 40.46 +#else 40.47 +#define MAXBITS (LUAI_BITSINT-2) 40.48 +#endif 40.49 + 40.50 +#define MAXASIZE (1 << MAXBITS) 40.51 + 40.52 + 40.53 +#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) 40.54 + 40.55 +#define hashstr(t,str) hashpow2(t, (str)->tsv.hash) 40.56 +#define hashboolean(t,p) hashpow2(t, p) 40.57 + 40.58 + 40.59 +/* 40.60 +** for some types, it is better to avoid modulus by power of 2, as 40.61 +** they tend to have many 2 factors. 40.62 +*/ 40.63 +#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) 40.64 + 40.65 + 40.66 +#define hashpointer(t,p) hashmod(t, IntPoint(p)) 40.67 + 40.68 + 40.69 +/* 40.70 +** number of ints inside a lua_Number 40.71 +*/ 40.72 +#define numints cast_int(sizeof(lua_Number)/sizeof(int)) 40.73 + 40.74 + 40.75 + 40.76 +#define dummynode (&dummynode_) 40.77 + 40.78 +static const Node dummynode_ = { 40.79 + {{NULL}, LUA_TNIL}, /* value */ 40.80 + {{{NULL}, LUA_TNIL, NULL}} /* key */ 40.81 +}; 40.82 + 40.83 + 40.84 +/* 40.85 +** hash for lua_Numbers 40.86 +*/ 40.87 +static Node *hashnum (const Table *t, lua_Number n) { 40.88 + unsigned int a[numints]; 40.89 + int i; 40.90 + if (luai_numeq(n, 0)) /* avoid problems with -0 */ 40.91 + return gnode(t, 0); 40.92 + memcpy(a, &n, sizeof(a)); 40.93 + for (i = 1; i < numints; i++) a[0] += a[i]; 40.94 + return hashmod(t, a[0]); 40.95 +} 40.96 + 40.97 + 40.98 + 40.99 +/* 40.100 +** returns the `main' position of an element in a table (that is, the index 40.101 +** of its hash value) 40.102 +*/ 40.103 +static Node *mainposition (const Table *t, const TValue *key) { 40.104 + switch (ttype(key)) { 40.105 + case LUA_TNUMBER: 40.106 + return hashnum(t, nvalue(key)); 40.107 + case LUA_TSTRING: 40.108 + return hashstr(t, rawtsvalue(key)); 40.109 + case LUA_TBOOLEAN: 40.110 + return hashboolean(t, bvalue(key)); 40.111 + case LUA_TLIGHTUSERDATA: 40.112 + return hashpointer(t, pvalue(key)); 40.113 + default: 40.114 + return hashpointer(t, gcvalue(key)); 40.115 + } 40.116 +} 40.117 + 40.118 + 40.119 +/* 40.120 +** returns the index for `key' if `key' is an appropriate key to live in 40.121 +** the array part of the table, -1 otherwise. 40.122 +*/ 40.123 +static int arrayindex (const TValue *key) { 40.124 + if (ttisnumber(key)) { 40.125 + lua_Number n = nvalue(key); 40.126 + int k; 40.127 + lua_number2int(k, n); 40.128 + if (luai_numeq(cast_num(k), n)) 40.129 + return k; 40.130 + } 40.131 + return -1; /* `key' did not match some condition */ 40.132 +} 40.133 + 40.134 + 40.135 +/* 40.136 +** returns the index of a `key' for table traversals. First goes all 40.137 +** elements in the array part, then elements in the hash part. The 40.138 +** beginning of a traversal is signalled by -1. 40.139 +*/ 40.140 +static int findindex (lua_State *L, Table *t, StkId key) { 40.141 + int i; 40.142 + if (ttisnil(key)) return -1; /* first iteration */ 40.143 + i = arrayindex(key); 40.144 + if (0 < i && i <= t->sizearray) /* is `key' inside array part? */ 40.145 + return i-1; /* yes; that's the index (corrected to C) */ 40.146 + else { 40.147 + Node *n = mainposition(t, key); 40.148 + do { /* check whether `key' is somewhere in the chain */ 40.149 + /* key may be dead already, but it is ok to use it in `next' */ 40.150 + if (luaO_rawequalObj(key2tval(n), key) || 40.151 + (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) && 40.152 + gcvalue(gkey(n)) == gcvalue(key))) { 40.153 + i = cast_int(n - gnode(t, 0)); /* key index in hash table */ 40.154 + /* hash elements are numbered after array ones */ 40.155 + return i + t->sizearray; 40.156 + } 40.157 + else n = gnext(n); 40.158 + } while (n); 40.159 + luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */ 40.160 + return 0; /* to avoid warnings */ 40.161 + } 40.162 +} 40.163 + 40.164 + 40.165 +int luaH_next (lua_State *L, Table *t, StkId key) { 40.166 + int i = findindex(L, t, key); /* find original element */ 40.167 + for (i++; i < t->sizearray; i++) { /* try first array part */ 40.168 + if (!ttisnil(&t->array[i])) { /* a non-nil value? */ 40.169 + setnvalue(key, cast_num(i+1)); 40.170 + setobj2s(L, key+1, &t->array[i]); 40.171 + return 1; 40.172 + } 40.173 + } 40.174 + for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ 40.175 + if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ 40.176 + setobj2s(L, key, key2tval(gnode(t, i))); 40.177 + setobj2s(L, key+1, gval(gnode(t, i))); 40.178 + return 1; 40.179 + } 40.180 + } 40.181 + return 0; /* no more elements */ 40.182 +} 40.183 + 40.184 + 40.185 +/* 40.186 +** {============================================================= 40.187 +** Rehash 40.188 +** ============================================================== 40.189 +*/ 40.190 + 40.191 + 40.192 +static int computesizes (int nums[], int *narray) { 40.193 + int i; 40.194 + int twotoi; /* 2^i */ 40.195 + int a = 0; /* number of elements smaller than 2^i */ 40.196 + int na = 0; /* number of elements to go to array part */ 40.197 + int n = 0; /* optimal size for array part */ 40.198 + for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { 40.199 + if (nums[i] > 0) { 40.200 + a += nums[i]; 40.201 + if (a > twotoi/2) { /* more than half elements present? */ 40.202 + n = twotoi; /* optimal size (till now) */ 40.203 + na = a; /* all elements smaller than n will go to array part */ 40.204 + } 40.205 + } 40.206 + if (a == *narray) break; /* all elements already counted */ 40.207 + } 40.208 + *narray = n; 40.209 + lua_assert(*narray/2 <= na && na <= *narray); 40.210 + return na; 40.211 +} 40.212 + 40.213 + 40.214 +static int countint (const TValue *key, int *nums) { 40.215 + int k = arrayindex(key); 40.216 + if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ 40.217 + nums[ceillog2(k)]++; /* count as such */ 40.218 + return 1; 40.219 + } 40.220 + else 40.221 + return 0; 40.222 +} 40.223 + 40.224 + 40.225 +static int numusearray (const Table *t, int *nums) { 40.226 + int lg; 40.227 + int ttlg; /* 2^lg */ 40.228 + int ause = 0; /* summation of `nums' */ 40.229 + int i = 1; /* count to traverse all array keys */ 40.230 + for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */ 40.231 + int lc = 0; /* counter */ 40.232 + int lim = ttlg; 40.233 + if (lim > t->sizearray) { 40.234 + lim = t->sizearray; /* adjust upper limit */ 40.235 + if (i > lim) 40.236 + break; /* no more elements to count */ 40.237 + } 40.238 + /* count elements in range (2^(lg-1), 2^lg] */ 40.239 + for (; i <= lim; i++) { 40.240 + if (!ttisnil(&t->array[i-1])) 40.241 + lc++; 40.242 + } 40.243 + nums[lg] += lc; 40.244 + ause += lc; 40.245 + } 40.246 + return ause; 40.247 +} 40.248 + 40.249 + 40.250 +static int numusehash (const Table *t, int *nums, int *pnasize) { 40.251 + int totaluse = 0; /* total number of elements */ 40.252 + int ause = 0; /* summation of `nums' */ 40.253 + int i = sizenode(t); 40.254 + while (i--) { 40.255 + Node *n = &t->node[i]; 40.256 + if (!ttisnil(gval(n))) { 40.257 + ause += countint(key2tval(n), nums); 40.258 + totaluse++; 40.259 + } 40.260 + } 40.261 + *pnasize += ause; 40.262 + return totaluse; 40.263 +} 40.264 + 40.265 + 40.266 +static void setarrayvector (lua_State *L, Table *t, int size) { 40.267 + int i; 40.268 + luaM_reallocvector(L, t->array, t->sizearray, size, TValue); 40.269 + for (i=t->sizearray; i<size; i++) 40.270 + setnilvalue(&t->array[i]); 40.271 + t->sizearray = size; 40.272 +} 40.273 + 40.274 + 40.275 +static void setnodevector (lua_State *L, Table *t, int size) { 40.276 + int lsize; 40.277 + if (size == 0) { /* no elements to hash part? */ 40.278 + t->node = cast(Node *, dummynode); /* use common `dummynode' */ 40.279 + lsize = 0; 40.280 + } 40.281 + else { 40.282 + int i; 40.283 + lsize = ceillog2(size); 40.284 + if (lsize > MAXBITS) 40.285 + luaG_runerror(L, "table overflow"); 40.286 + size = twoto(lsize); 40.287 + t->node = luaM_newvector(L, size, Node); 40.288 + for (i=0; i<size; i++) { 40.289 + Node *n = gnode(t, i); 40.290 + gnext(n) = NULL; 40.291 + setnilvalue(gkey(n)); 40.292 + setnilvalue(gval(n)); 40.293 + } 40.294 + } 40.295 + t->lsizenode = cast_byte(lsize); 40.296 + t->lastfree = gnode(t, size); /* all positions are free */ 40.297 +} 40.298 + 40.299 + 40.300 +static void resize (lua_State *L, Table *t, int nasize, int nhsize) { 40.301 + int i; 40.302 + int oldasize = t->sizearray; 40.303 + int oldhsize = t->lsizenode; 40.304 + Node *nold = t->node; /* save old hash ... */ 40.305 + if (nasize > oldasize) /* array part must grow? */ 40.306 + setarrayvector(L, t, nasize); 40.307 + /* create new hash part with appropriate size */ 40.308 + setnodevector(L, t, nhsize); 40.309 + if (nasize < oldasize) { /* array part must shrink? */ 40.310 + t->sizearray = nasize; 40.311 + /* re-insert elements from vanishing slice */ 40.312 + for (i=nasize; i<oldasize; i++) { 40.313 + if (!ttisnil(&t->array[i])) 40.314 + setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]); 40.315 + } 40.316 + /* shrink array */ 40.317 + luaM_reallocvector(L, t->array, oldasize, nasize, TValue); 40.318 + } 40.319 + /* re-insert elements from hash part */ 40.320 + for (i = twoto(oldhsize) - 1; i >= 0; i--) { 40.321 + Node *old = nold+i; 40.322 + if (!ttisnil(gval(old))) 40.323 + setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); 40.324 + } 40.325 + if (nold != dummynode) 40.326 + luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ 40.327 +} 40.328 + 40.329 + 40.330 +void luaH_resizearray (lua_State *L, Table *t, int nasize) { 40.331 + int nsize = (t->node == dummynode) ? 0 : sizenode(t); 40.332 + resize(L, t, nasize, nsize); 40.333 +} 40.334 + 40.335 + 40.336 +static void rehash (lua_State *L, Table *t, const TValue *ek) { 40.337 + int nasize, na; 40.338 + int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */ 40.339 + int i; 40.340 + int totaluse; 40.341 + for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ 40.342 + nasize = numusearray(t, nums); /* count keys in array part */ 40.343 + totaluse = nasize; /* all those keys are integer keys */ 40.344 + totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */ 40.345 + /* count extra key */ 40.346 + nasize += countint(ek, nums); 40.347 + totaluse++; 40.348 + /* compute new size for array part */ 40.349 + na = computesizes(nums, &nasize); 40.350 + /* resize the table to new computed sizes */ 40.351 + resize(L, t, nasize, totaluse - na); 40.352 +} 40.353 + 40.354 + 40.355 + 40.356 +/* 40.357 +** }============================================================= 40.358 +*/ 40.359 + 40.360 + 40.361 +Table *luaH_new (lua_State *L, int narray, int nhash) { 40.362 + Table *t = luaM_new(L, Table); 40.363 + luaC_link(L, obj2gco(t), LUA_TTABLE); 40.364 + t->metatable = NULL; 40.365 + t->flags = cast_byte(~0); 40.366 + /* temporary values (kept only if some malloc fails) */ 40.367 + t->array = NULL; 40.368 + t->sizearray = 0; 40.369 + t->lsizenode = 0; 40.370 + t->node = cast(Node *, dummynode); 40.371 + setarrayvector(L, t, narray); 40.372 + setnodevector(L, t, nhash); 40.373 + return t; 40.374 +} 40.375 + 40.376 + 40.377 +void luaH_free (lua_State *L, Table *t) { 40.378 + if (t->node != dummynode) 40.379 + luaM_freearray(L, t->node, sizenode(t), Node); 40.380 + luaM_freearray(L, t->array, t->sizearray, TValue); 40.381 + luaM_free(L, t); 40.382 +} 40.383 + 40.384 + 40.385 +static Node *getfreepos (Table *t) { 40.386 + while (t->lastfree-- > t->node) { 40.387 + if (ttisnil(gkey(t->lastfree))) 40.388 + return t->lastfree; 40.389 + } 40.390 + return NULL; /* could not find a free place */ 40.391 +} 40.392 + 40.393 + 40.394 + 40.395 +/* 40.396 +** inserts a new key into a hash table; first, check whether key's main 40.397 +** position is free. If not, check whether colliding node is in its main 40.398 +** position or not: if it is not, move colliding node to an empty place and 40.399 +** put new key in its main position; otherwise (colliding node is in its main 40.400 +** position), new key goes to an empty position. 40.401 +*/ 40.402 +static TValue *newkey (lua_State *L, Table *t, const TValue *key) { 40.403 + Node *mp = mainposition(t, key); 40.404 + if (!ttisnil(gval(mp)) || mp == dummynode) { 40.405 + Node *othern; 40.406 + Node *n = getfreepos(t); /* get a free place */ 40.407 + if (n == NULL) { /* cannot find a free place? */ 40.408 + rehash(L, t, key); /* grow table */ 40.409 + return luaH_set(L, t, key); /* re-insert key into grown table */ 40.410 + } 40.411 + lua_assert(n != dummynode); 40.412 + othern = mainposition(t, key2tval(mp)); 40.413 + if (othern != mp) { /* is colliding node out of its main position? */ 40.414 + /* yes; move colliding node into free position */ 40.415 + while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ 40.416 + gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ 40.417 + *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ 40.418 + gnext(mp) = NULL; /* now `mp' is free */ 40.419 + setnilvalue(gval(mp)); 40.420 + } 40.421 + else { /* colliding node is in its own main position */ 40.422 + /* new node will go into free position */ 40.423 + gnext(n) = gnext(mp); /* chain new position */ 40.424 + gnext(mp) = n; 40.425 + mp = n; 40.426 + } 40.427 + } 40.428 + gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; 40.429 + luaC_barriert(L, t, key); 40.430 + lua_assert(ttisnil(gval(mp))); 40.431 + return gval(mp); 40.432 +} 40.433 + 40.434 + 40.435 +/* 40.436 +** search function for integers 40.437 +*/ 40.438 +const TValue *luaH_getnum (Table *t, int key) { 40.439 + /* (1 <= key && key <= t->sizearray) */ 40.440 + if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) 40.441 + return &t->array[key-1]; 40.442 + else { 40.443 + lua_Number nk = cast_num(key); 40.444 + Node *n = hashnum(t, nk); 40.445 + do { /* check whether `key' is somewhere in the chain */ 40.446 + if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk)) 40.447 + return gval(n); /* that's it */ 40.448 + else n = gnext(n); 40.449 + } while (n); 40.450 + return luaO_nilobject; 40.451 + } 40.452 +} 40.453 + 40.454 + 40.455 +/* 40.456 +** search function for strings 40.457 +*/ 40.458 +const TValue *luaH_getstr (Table *t, TString *key) { 40.459 + Node *n = hashstr(t, key); 40.460 + do { /* check whether `key' is somewhere in the chain */ 40.461 + if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key) 40.462 + return gval(n); /* that's it */ 40.463 + else n = gnext(n); 40.464 + } while (n); 40.465 + return luaO_nilobject; 40.466 +} 40.467 + 40.468 + 40.469 +/* 40.470 +** main search function 40.471 +*/ 40.472 +const TValue *luaH_get (Table *t, const TValue *key) { 40.473 + switch (ttype(key)) { 40.474 + case LUA_TNIL: return luaO_nilobject; 40.475 + case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key)); 40.476 + case LUA_TNUMBER: { 40.477 + int k; 40.478 + lua_Number n = nvalue(key); 40.479 + lua_number2int(k, n); 40.480 + if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */ 40.481 + return luaH_getnum(t, k); /* use specialized version */ 40.482 + /* else go through */ 40.483 + } 40.484 + default: { 40.485 + Node *n = mainposition(t, key); 40.486 + do { /* check whether `key' is somewhere in the chain */ 40.487 + if (luaO_rawequalObj(key2tval(n), key)) 40.488 + return gval(n); /* that's it */ 40.489 + else n = gnext(n); 40.490 + } while (n); 40.491 + return luaO_nilobject; 40.492 + } 40.493 + } 40.494 +} 40.495 + 40.496 + 40.497 +TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { 40.498 + const TValue *p = luaH_get(t, key); 40.499 + t->flags = 0; 40.500 + if (p != luaO_nilobject) 40.501 + return cast(TValue *, p); 40.502 + else { 40.503 + if (ttisnil(key)) luaG_runerror(L, "table index is nil"); 40.504 + else if (ttisnumber(key) && luai_numisnan(nvalue(key))) 40.505 + luaG_runerror(L, "table index is NaN"); 40.506 + return newkey(L, t, key); 40.507 + } 40.508 +} 40.509 + 40.510 + 40.511 +TValue *luaH_setnum (lua_State *L, Table *t, int key) { 40.512 + const TValue *p = luaH_getnum(t, key); 40.513 + if (p != luaO_nilobject) 40.514 + return cast(TValue *, p); 40.515 + else { 40.516 + TValue k; 40.517 + setnvalue(&k, cast_num(key)); 40.518 + return newkey(L, t, &k); 40.519 + } 40.520 +} 40.521 + 40.522 + 40.523 +TValue *luaH_setstr (lua_State *L, Table *t, TString *key) { 40.524 + const TValue *p = luaH_getstr(t, key); 40.525 + if (p != luaO_nilobject) 40.526 + return cast(TValue *, p); 40.527 + else { 40.528 + TValue k; 40.529 + setsvalue(L, &k, key); 40.530 + return newkey(L, t, &k); 40.531 + } 40.532 +} 40.533 + 40.534 + 40.535 +static int unbound_search (Table *t, unsigned int j) { 40.536 + unsigned int i = j; /* i is zero or a present index */ 40.537 + j++; 40.538 + /* find `i' and `j' such that i is present and j is not */ 40.539 + while (!ttisnil(luaH_getnum(t, j))) { 40.540 + i = j; 40.541 + j *= 2; 40.542 + if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ 40.543 + /* table was built with bad purposes: resort to linear search */ 40.544 + i = 1; 40.545 + while (!ttisnil(luaH_getnum(t, i))) i++; 40.546 + return i - 1; 40.547 + } 40.548 + } 40.549 + /* now do a binary search between them */ 40.550 + while (j - i > 1) { 40.551 + unsigned int m = (i+j)/2; 40.552 + if (ttisnil(luaH_getnum(t, m))) j = m; 40.553 + else i = m; 40.554 + } 40.555 + return i; 40.556 +} 40.557 + 40.558 + 40.559 +/* 40.560 +** Try to find a boundary in table `t'. A `boundary' is an integer index 40.561 +** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). 40.562 +*/ 40.563 +int luaH_getn (Table *t) { 40.564 + unsigned int j = t->sizearray; 40.565 + if (j > 0 && ttisnil(&t->array[j - 1])) { 40.566 + /* there is a boundary in the array part: (binary) search for it */ 40.567 + unsigned int i = 0; 40.568 + while (j - i > 1) { 40.569 + unsigned int m = (i+j)/2; 40.570 + if (ttisnil(&t->array[m - 1])) j = m; 40.571 + else i = m; 40.572 + } 40.573 + return i; 40.574 + } 40.575 + /* else must find a boundary in hash part */ 40.576 + else if (t->node == dummynode) /* hash part is empty? */ 40.577 + return j; /* that is easy... */ 40.578 + else return unbound_search(t, j); 40.579 +} 40.580 + 40.581 + 40.582 + 40.583 +#if defined(LUA_DEBUG) 40.584 + 40.585 +Node *luaH_mainposition (const Table *t, const TValue *key) { 40.586 + return mainposition(t, key); 40.587 +} 40.588 + 40.589 +int luaH_isdummy (Node *n) { return n == dummynode; } 40.590 + 40.591 +#endif
41.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 41.2 +++ b/src/lua/ltable.h Sat Mar 03 11:07:39 2012 -0600 41.3 @@ -0,0 +1,40 @@ 41.4 +/* 41.5 +** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $ 41.6 +** Lua tables (hash) 41.7 +** See Copyright Notice in lua.h 41.8 +*/ 41.9 + 41.10 +#ifndef ltable_h 41.11 +#define ltable_h 41.12 + 41.13 +#include "lobject.h" 41.14 + 41.15 + 41.16 +#define gnode(t,i) (&(t)->node[i]) 41.17 +#define gkey(n) (&(n)->i_key.nk) 41.18 +#define gval(n) (&(n)->i_val) 41.19 +#define gnext(n) ((n)->i_key.nk.next) 41.20 + 41.21 +#define key2tval(n) (&(n)->i_key.tvk) 41.22 + 41.23 + 41.24 +LUAI_FUNC const TValue *luaH_getnum (Table *t, int key); 41.25 +LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key); 41.26 +LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 41.27 +LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key); 41.28 +LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 41.29 +LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 41.30 +LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash); 41.31 +LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 41.32 +LUAI_FUNC void luaH_free (lua_State *L, Table *t); 41.33 +LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 41.34 +LUAI_FUNC int luaH_getn (Table *t); 41.35 + 41.36 + 41.37 +#if defined(LUA_DEBUG) 41.38 +LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 41.39 +LUAI_FUNC int luaH_isdummy (Node *n); 41.40 +#endif 41.41 + 41.42 + 41.43 +#endif
42.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 42.2 +++ b/src/lua/ltablib.c Sat Mar 03 11:07:39 2012 -0600 42.3 @@ -0,0 +1,287 @@ 42.4 +/* 42.5 +** $Id: ltablib.c,v 1.38.1.3 2008/02/14 16:46:58 roberto Exp $ 42.6 +** Library for Table Manipulation 42.7 +** See Copyright Notice in lua.h 42.8 +*/ 42.9 + 42.10 + 42.11 +#include <stddef.h> 42.12 + 42.13 +#define ltablib_c 42.14 +#define LUA_LIB 42.15 + 42.16 +#include "lua.h" 42.17 + 42.18 +#include "lauxlib.h" 42.19 +#include "lualib.h" 42.20 + 42.21 + 42.22 +#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n)) 42.23 + 42.24 + 42.25 +static int foreachi (lua_State *L) { 42.26 + int i; 42.27 + int n = aux_getn(L, 1); 42.28 + luaL_checktype(L, 2, LUA_TFUNCTION); 42.29 + for (i=1; i <= n; i++) { 42.30 + lua_pushvalue(L, 2); /* function */ 42.31 + lua_pushinteger(L, i); /* 1st argument */ 42.32 + lua_rawgeti(L, 1, i); /* 2nd argument */ 42.33 + lua_call(L, 2, 1); 42.34 + if (!lua_isnil(L, -1)) 42.35 + return 1; 42.36 + lua_pop(L, 1); /* remove nil result */ 42.37 + } 42.38 + return 0; 42.39 +} 42.40 + 42.41 + 42.42 +static int foreach (lua_State *L) { 42.43 + luaL_checktype(L, 1, LUA_TTABLE); 42.44 + luaL_checktype(L, 2, LUA_TFUNCTION); 42.45 + lua_pushnil(L); /* first key */ 42.46 + while (lua_next(L, 1)) { 42.47 + lua_pushvalue(L, 2); /* function */ 42.48 + lua_pushvalue(L, -3); /* key */ 42.49 + lua_pushvalue(L, -3); /* value */ 42.50 + lua_call(L, 2, 1); 42.51 + if (!lua_isnil(L, -1)) 42.52 + return 1; 42.53 + lua_pop(L, 2); /* remove value and result */ 42.54 + } 42.55 + return 0; 42.56 +} 42.57 + 42.58 + 42.59 +static int maxn (lua_State *L) { 42.60 + lua_Number max = 0; 42.61 + luaL_checktype(L, 1, LUA_TTABLE); 42.62 + lua_pushnil(L); /* first key */ 42.63 + while (lua_next(L, 1)) { 42.64 + lua_pop(L, 1); /* remove value */ 42.65 + if (lua_type(L, -1) == LUA_TNUMBER) { 42.66 + lua_Number v = lua_tonumber(L, -1); 42.67 + if (v > max) max = v; 42.68 + } 42.69 + } 42.70 + lua_pushnumber(L, max); 42.71 + return 1; 42.72 +} 42.73 + 42.74 + 42.75 +static int getn (lua_State *L) { 42.76 + lua_pushinteger(L, aux_getn(L, 1)); 42.77 + return 1; 42.78 +} 42.79 + 42.80 + 42.81 +static int setn (lua_State *L) { 42.82 + luaL_checktype(L, 1, LUA_TTABLE); 42.83 +#ifndef luaL_setn 42.84 + luaL_setn(L, 1, luaL_checkint(L, 2)); 42.85 +#else 42.86 + luaL_error(L, LUA_QL("setn") " is obsolete"); 42.87 +#endif 42.88 + lua_pushvalue(L, 1); 42.89 + return 1; 42.90 +} 42.91 + 42.92 + 42.93 +static int tinsert (lua_State *L) { 42.94 + int e = aux_getn(L, 1) + 1; /* first empty element */ 42.95 + int pos; /* where to insert new element */ 42.96 + switch (lua_gettop(L)) { 42.97 + case 2: { /* called with only 2 arguments */ 42.98 + pos = e; /* insert new element at the end */ 42.99 + break; 42.100 + } 42.101 + case 3: { 42.102 + int i; 42.103 + pos = luaL_checkint(L, 2); /* 2nd argument is the position */ 42.104 + if (pos > e) e = pos; /* `grow' array if necessary */ 42.105 + for (i = e; i > pos; i--) { /* move up elements */ 42.106 + lua_rawgeti(L, 1, i-1); 42.107 + lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ 42.108 + } 42.109 + break; 42.110 + } 42.111 + default: { 42.112 + return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); 42.113 + } 42.114 + } 42.115 + luaL_setn(L, 1, e); /* new size */ 42.116 + lua_rawseti(L, 1, pos); /* t[pos] = v */ 42.117 + return 0; 42.118 +} 42.119 + 42.120 + 42.121 +static int tremove (lua_State *L) { 42.122 + int e = aux_getn(L, 1); 42.123 + int pos = luaL_optint(L, 2, e); 42.124 + if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ 42.125 + return 0; /* nothing to remove */ 42.126 + luaL_setn(L, 1, e - 1); /* t.n = n-1 */ 42.127 + lua_rawgeti(L, 1, pos); /* result = t[pos] */ 42.128 + for ( ;pos<e; pos++) { 42.129 + lua_rawgeti(L, 1, pos+1); 42.130 + lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ 42.131 + } 42.132 + lua_pushnil(L); 42.133 + lua_rawseti(L, 1, e); /* t[e] = nil */ 42.134 + return 1; 42.135 +} 42.136 + 42.137 + 42.138 +static void addfield (lua_State *L, luaL_Buffer *b, int i) { 42.139 + lua_rawgeti(L, 1, i); 42.140 + if (!lua_isstring(L, -1)) 42.141 + luaL_error(L, "invalid value (%s) at index %d in table for " 42.142 + LUA_QL("concat"), luaL_typename(L, -1), i); 42.143 + luaL_addvalue(b); 42.144 +} 42.145 + 42.146 + 42.147 +static int tconcat (lua_State *L) { 42.148 + luaL_Buffer b; 42.149 + size_t lsep; 42.150 + int i, last; 42.151 + const char *sep = luaL_optlstring(L, 2, "", &lsep); 42.152 + luaL_checktype(L, 1, LUA_TTABLE); 42.153 + i = luaL_optint(L, 3, 1); 42.154 + last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1)); 42.155 + luaL_buffinit(L, &b); 42.156 + for (; i < last; i++) { 42.157 + addfield(L, &b, i); 42.158 + luaL_addlstring(&b, sep, lsep); 42.159 + } 42.160 + if (i == last) /* add last value (if interval was not empty) */ 42.161 + addfield(L, &b, i); 42.162 + luaL_pushresult(&b); 42.163 + return 1; 42.164 +} 42.165 + 42.166 + 42.167 + 42.168 +/* 42.169 +** {====================================================== 42.170 +** Quicksort 42.171 +** (based on `Algorithms in MODULA-3', Robert Sedgewick; 42.172 +** Addison-Wesley, 1993.) 42.173 +*/ 42.174 + 42.175 + 42.176 +static void set2 (lua_State *L, int i, int j) { 42.177 + lua_rawseti(L, 1, i); 42.178 + lua_rawseti(L, 1, j); 42.179 +} 42.180 + 42.181 +static int sort_comp (lua_State *L, int a, int b) { 42.182 + if (!lua_isnil(L, 2)) { /* function? */ 42.183 + int res; 42.184 + lua_pushvalue(L, 2); 42.185 + lua_pushvalue(L, a-1); /* -1 to compensate function */ 42.186 + lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ 42.187 + lua_call(L, 2, 1); 42.188 + res = lua_toboolean(L, -1); 42.189 + lua_pop(L, 1); 42.190 + return res; 42.191 + } 42.192 + else /* a < b? */ 42.193 + return lua_lessthan(L, a, b); 42.194 +} 42.195 + 42.196 +static void auxsort (lua_State *L, int l, int u) { 42.197 + while (l < u) { /* for tail recursion */ 42.198 + int i, j; 42.199 + /* sort elements a[l], a[(l+u)/2] and a[u] */ 42.200 + lua_rawgeti(L, 1, l); 42.201 + lua_rawgeti(L, 1, u); 42.202 + if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ 42.203 + set2(L, l, u); /* swap a[l] - a[u] */ 42.204 + else 42.205 + lua_pop(L, 2); 42.206 + if (u-l == 1) break; /* only 2 elements */ 42.207 + i = (l+u)/2; 42.208 + lua_rawgeti(L, 1, i); 42.209 + lua_rawgeti(L, 1, l); 42.210 + if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */ 42.211 + set2(L, i, l); 42.212 + else { 42.213 + lua_pop(L, 1); /* remove a[l] */ 42.214 + lua_rawgeti(L, 1, u); 42.215 + if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */ 42.216 + set2(L, i, u); 42.217 + else 42.218 + lua_pop(L, 2); 42.219 + } 42.220 + if (u-l == 2) break; /* only 3 elements */ 42.221 + lua_rawgeti(L, 1, i); /* Pivot */ 42.222 + lua_pushvalue(L, -1); 42.223 + lua_rawgeti(L, 1, u-1); 42.224 + set2(L, i, u-1); 42.225 + /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */ 42.226 + i = l; j = u-1; 42.227 + for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ 42.228 + /* repeat ++i until a[i] >= P */ 42.229 + while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { 42.230 + if (i>u) luaL_error(L, "invalid order function for sorting"); 42.231 + lua_pop(L, 1); /* remove a[i] */ 42.232 + } 42.233 + /* repeat --j until a[j] <= P */ 42.234 + while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { 42.235 + if (j<l) luaL_error(L, "invalid order function for sorting"); 42.236 + lua_pop(L, 1); /* remove a[j] */ 42.237 + } 42.238 + if (j<i) { 42.239 + lua_pop(L, 3); /* pop pivot, a[i], a[j] */ 42.240 + break; 42.241 + } 42.242 + set2(L, i, j); 42.243 + } 42.244 + lua_rawgeti(L, 1, u-1); 42.245 + lua_rawgeti(L, 1, i); 42.246 + set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */ 42.247 + /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ 42.248 + /* adjust so that smaller half is in [j..i] and larger one in [l..u] */ 42.249 + if (i-l < u-i) { 42.250 + j=l; i=i-1; l=i+2; 42.251 + } 42.252 + else { 42.253 + j=i+1; i=u; u=j-2; 42.254 + } 42.255 + auxsort(L, j, i); /* call recursively the smaller one */ 42.256 + } /* repeat the routine for the larger one */ 42.257 +} 42.258 + 42.259 +static int sort (lua_State *L) { 42.260 + int n = aux_getn(L, 1); 42.261 + luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ 42.262 + if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ 42.263 + luaL_checktype(L, 2, LUA_TFUNCTION); 42.264 + lua_settop(L, 2); /* make sure there is two arguments */ 42.265 + auxsort(L, 1, n); 42.266 + return 0; 42.267 +} 42.268 + 42.269 +/* }====================================================== */ 42.270 + 42.271 + 42.272 +static const luaL_Reg tab_funcs[] = { 42.273 + {"concat", tconcat}, 42.274 + {"foreach", foreach}, 42.275 + {"foreachi", foreachi}, 42.276 + {"getn", getn}, 42.277 + {"maxn", maxn}, 42.278 + {"insert", tinsert}, 42.279 + {"remove", tremove}, 42.280 + {"setn", setn}, 42.281 + {"sort", sort}, 42.282 + {NULL, NULL} 42.283 +}; 42.284 + 42.285 + 42.286 +LUALIB_API int luaopen_table (lua_State *L) { 42.287 + luaL_register(L, LUA_TABLIBNAME, tab_funcs); 42.288 + return 1; 42.289 +} 42.290 +
43.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 43.2 +++ b/src/lua/ltm.c Sat Mar 03 11:07:39 2012 -0600 43.3 @@ -0,0 +1,75 @@ 43.4 +/* 43.5 +** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 43.6 +** Tag methods 43.7 +** See Copyright Notice in lua.h 43.8 +*/ 43.9 + 43.10 + 43.11 +#include <string.h> 43.12 + 43.13 +#define ltm_c 43.14 +#define LUA_CORE 43.15 + 43.16 +#include "lua.h" 43.17 + 43.18 +#include "lobject.h" 43.19 +#include "lstate.h" 43.20 +#include "lstring.h" 43.21 +#include "ltable.h" 43.22 +#include "ltm.h" 43.23 + 43.24 + 43.25 + 43.26 +const char *const luaT_typenames[] = { 43.27 + "nil", "boolean", "userdata", "number", 43.28 + "string", "table", "function", "userdata", "thread", 43.29 + "proto", "upval" 43.30 +}; 43.31 + 43.32 + 43.33 +void luaT_init (lua_State *L) { 43.34 + static const char *const luaT_eventname[] = { /* ORDER TM */ 43.35 + "__index", "__newindex", 43.36 + "__gc", "__mode", "__eq", 43.37 + "__add", "__sub", "__mul", "__div", "__mod", 43.38 + "__pow", "__unm", "__len", "__lt", "__le", 43.39 + "__concat", "__call" 43.40 + }; 43.41 + int i; 43.42 + for (i=0; i<TM_N; i++) { 43.43 + G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); 43.44 + luaS_fix(G(L)->tmname[i]); /* never collect these names */ 43.45 + } 43.46 +} 43.47 + 43.48 + 43.49 +/* 43.50 +** function to be used with macro "fasttm": optimized for absence of 43.51 +** tag methods 43.52 +*/ 43.53 +const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 43.54 + const TValue *tm = luaH_getstr(events, ename); 43.55 + lua_assert(event <= TM_EQ); 43.56 + if (ttisnil(tm)) { /* no tag method? */ 43.57 + events->flags |= cast_byte(1u<<event); /* cache this fact */ 43.58 + return NULL; 43.59 + } 43.60 + else return tm; 43.61 +} 43.62 + 43.63 + 43.64 +const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { 43.65 + Table *mt; 43.66 + switch (ttype(o)) { 43.67 + case LUA_TTABLE: 43.68 + mt = hvalue(o)->metatable; 43.69 + break; 43.70 + case LUA_TUSERDATA: 43.71 + mt = uvalue(o)->metatable; 43.72 + break; 43.73 + default: 43.74 + mt = G(L)->mt[ttype(o)]; 43.75 + } 43.76 + return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 43.77 +} 43.78 +
44.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 44.2 +++ b/src/lua/ltm.h Sat Mar 03 11:07:39 2012 -0600 44.3 @@ -0,0 +1,54 @@ 44.4 +/* 44.5 +** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $ 44.6 +** Tag methods 44.7 +** See Copyright Notice in lua.h 44.8 +*/ 44.9 + 44.10 +#ifndef ltm_h 44.11 +#define ltm_h 44.12 + 44.13 + 44.14 +#include "lobject.h" 44.15 + 44.16 + 44.17 +/* 44.18 +* WARNING: if you change the order of this enumeration, 44.19 +* grep "ORDER TM" 44.20 +*/ 44.21 +typedef enum { 44.22 + TM_INDEX, 44.23 + TM_NEWINDEX, 44.24 + TM_GC, 44.25 + TM_MODE, 44.26 + TM_EQ, /* last tag method with `fast' access */ 44.27 + TM_ADD, 44.28 + TM_SUB, 44.29 + TM_MUL, 44.30 + TM_DIV, 44.31 + TM_MOD, 44.32 + TM_POW, 44.33 + TM_UNM, 44.34 + TM_LEN, 44.35 + TM_LT, 44.36 + TM_LE, 44.37 + TM_CONCAT, 44.38 + TM_CALL, 44.39 + TM_N /* number of elements in the enum */ 44.40 +} TMS; 44.41 + 44.42 + 44.43 + 44.44 +#define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 44.45 + ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 44.46 + 44.47 +#define fasttm(l,et,e) gfasttm(G(l), et, e) 44.48 + 44.49 +LUAI_DATA const char *const luaT_typenames[]; 44.50 + 44.51 + 44.52 +LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 44.53 +LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 44.54 + TMS event); 44.55 +LUAI_FUNC void luaT_init (lua_State *L); 44.56 + 44.57 +#endif
45.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 45.2 +++ b/src/lua/lua.h Sat Mar 03 11:07:39 2012 -0600 45.3 @@ -0,0 +1,388 @@ 45.4 +/* 45.5 +** $Id: lua.h,v 1.218.1.5 2008/08/06 13:30:12 roberto Exp $ 45.6 +** Lua - An Extensible Extension Language 45.7 +** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 45.8 +** See Copyright Notice at the end of this file 45.9 +*/ 45.10 + 45.11 + 45.12 +#ifndef lua_h 45.13 +#define lua_h 45.14 + 45.15 +#include <stdarg.h> 45.16 +#include <stddef.h> 45.17 + 45.18 + 45.19 +#include "luaconf.h" 45.20 + 45.21 + 45.22 +#define LUA_VERSION "Lua 5.1" 45.23 +#define LUA_RELEASE "Lua 5.1.4" 45.24 +#define LUA_VERSION_NUM 501 45.25 +#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio" 45.26 +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" 45.27 + 45.28 + 45.29 +/* mark for precompiled code (`<esc>Lua') */ 45.30 +#define LUA_SIGNATURE "\033Lua" 45.31 + 45.32 +/* option for multiple returns in `lua_pcall' and `lua_call' */ 45.33 +#define LUA_MULTRET (-1) 45.34 + 45.35 + 45.36 +/* 45.37 +** pseudo-indices 45.38 +*/ 45.39 +#define LUA_REGISTRYINDEX (-10000) 45.40 +#define LUA_ENVIRONINDEX (-10001) 45.41 +#define LUA_GLOBALSINDEX (-10002) 45.42 +#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) 45.43 + 45.44 + 45.45 +/* thread status; 0 is OK */ 45.46 +#define LUA_YIELD 1 45.47 +#define LUA_ERRRUN 2 45.48 +#define LUA_ERRSYNTAX 3 45.49 +#define LUA_ERRMEM 4 45.50 +#define LUA_ERRERR 5 45.51 + 45.52 + 45.53 +typedef struct lua_State lua_State; 45.54 + 45.55 +typedef int (*lua_CFunction) (lua_State *L); 45.56 + 45.57 + 45.58 +/* 45.59 +** functions that read/write blocks when loading/dumping Lua chunks 45.60 +*/ 45.61 +typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); 45.62 + 45.63 +typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); 45.64 + 45.65 + 45.66 +/* 45.67 +** prototype for memory-allocation functions 45.68 +*/ 45.69 +typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); 45.70 + 45.71 + 45.72 +/* 45.73 +** basic types 45.74 +*/ 45.75 +#define LUA_TNONE (-1) 45.76 + 45.77 +#define LUA_TNIL 0 45.78 +#define LUA_TBOOLEAN 1 45.79 +#define LUA_TLIGHTUSERDATA 2 45.80 +#define LUA_TNUMBER 3 45.81 +#define LUA_TSTRING 4 45.82 +#define LUA_TTABLE 5 45.83 +#define LUA_TFUNCTION 6 45.84 +#define LUA_TUSERDATA 7 45.85 +#define LUA_TTHREAD 8 45.86 + 45.87 + 45.88 + 45.89 +/* minimum Lua stack available to a C function */ 45.90 +#define LUA_MINSTACK 20 45.91 + 45.92 + 45.93 +/* 45.94 +** generic extra include file 45.95 +*/ 45.96 +#if defined(LUA_USER_H) 45.97 +#include LUA_USER_H 45.98 +#endif 45.99 + 45.100 + 45.101 +/* type of numbers in Lua */ 45.102 +typedef LUA_NUMBER lua_Number; 45.103 + 45.104 + 45.105 +/* type for integer functions */ 45.106 +typedef LUA_INTEGER lua_Integer; 45.107 + 45.108 + 45.109 + 45.110 +/* 45.111 +** state manipulation 45.112 +*/ 45.113 +LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); 45.114 +LUA_API void (lua_close) (lua_State *L); 45.115 +LUA_API lua_State *(lua_newthread) (lua_State *L); 45.116 + 45.117 +LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); 45.118 + 45.119 + 45.120 +/* 45.121 +** basic stack manipulation 45.122 +*/ 45.123 +LUA_API int (lua_gettop) (lua_State *L); 45.124 +LUA_API void (lua_settop) (lua_State *L, int idx); 45.125 +LUA_API void (lua_pushvalue) (lua_State *L, int idx); 45.126 +LUA_API void (lua_remove) (lua_State *L, int idx); 45.127 +LUA_API void (lua_insert) (lua_State *L, int idx); 45.128 +LUA_API void (lua_replace) (lua_State *L, int idx); 45.129 +LUA_API int (lua_checkstack) (lua_State *L, int sz); 45.130 + 45.131 +LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); 45.132 + 45.133 + 45.134 +/* 45.135 +** access functions (stack -> C) 45.136 +*/ 45.137 + 45.138 +LUA_API int (lua_isnumber) (lua_State *L, int idx); 45.139 +LUA_API int (lua_isstring) (lua_State *L, int idx); 45.140 +LUA_API int (lua_iscfunction) (lua_State *L, int idx); 45.141 +LUA_API int (lua_isuserdata) (lua_State *L, int idx); 45.142 +LUA_API int (lua_type) (lua_State *L, int idx); 45.143 +LUA_API const char *(lua_typename) (lua_State *L, int tp); 45.144 + 45.145 +LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2); 45.146 +LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); 45.147 +LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2); 45.148 + 45.149 +LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); 45.150 +LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); 45.151 +LUA_API int (lua_toboolean) (lua_State *L, int idx); 45.152 +LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); 45.153 +LUA_API size_t (lua_objlen) (lua_State *L, int idx); 45.154 +LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); 45.155 +LUA_API void *(lua_touserdata) (lua_State *L, int idx); 45.156 +LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); 45.157 +LUA_API const void *(lua_topointer) (lua_State *L, int idx); 45.158 + 45.159 + 45.160 +/* 45.161 +** push functions (C -> stack) 45.162 +*/ 45.163 +LUA_API void (lua_pushnil) (lua_State *L); 45.164 +LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); 45.165 +LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); 45.166 +LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); 45.167 +LUA_API void (lua_pushstring) (lua_State *L, const char *s); 45.168 +LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, 45.169 + va_list argp); 45.170 +LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); 45.171 +LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); 45.172 +LUA_API void (lua_pushboolean) (lua_State *L, int b); 45.173 +LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); 45.174 +LUA_API int (lua_pushthread) (lua_State *L); 45.175 + 45.176 + 45.177 +/* 45.178 +** get functions (Lua -> stack) 45.179 +*/ 45.180 +LUA_API void (lua_gettable) (lua_State *L, int idx); 45.181 +LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); 45.182 +LUA_API void (lua_rawget) (lua_State *L, int idx); 45.183 +LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); 45.184 +LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); 45.185 +LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); 45.186 +LUA_API int (lua_getmetatable) (lua_State *L, int objindex); 45.187 +LUA_API void (lua_getfenv) (lua_State *L, int idx); 45.188 + 45.189 + 45.190 +/* 45.191 +** set functions (stack -> Lua) 45.192 +*/ 45.193 +LUA_API void (lua_settable) (lua_State *L, int idx); 45.194 +LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); 45.195 +LUA_API void (lua_rawset) (lua_State *L, int idx); 45.196 +LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); 45.197 +LUA_API int (lua_setmetatable) (lua_State *L, int objindex); 45.198 +LUA_API int (lua_setfenv) (lua_State *L, int idx); 45.199 + 45.200 + 45.201 +/* 45.202 +** `load' and `call' functions (load and run Lua code) 45.203 +*/ 45.204 +LUA_API void (lua_call) (lua_State *L, int nargs, int nresults); 45.205 +LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); 45.206 +LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); 45.207 +LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, 45.208 + const char *chunkname); 45.209 + 45.210 +LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); 45.211 + 45.212 + 45.213 +/* 45.214 +** coroutine functions 45.215 +*/ 45.216 +LUA_API int (lua_yield) (lua_State *L, int nresults); 45.217 +LUA_API int (lua_resume) (lua_State *L, int narg); 45.218 +LUA_API int (lua_status) (lua_State *L); 45.219 + 45.220 +/* 45.221 +** garbage-collection function and options 45.222 +*/ 45.223 + 45.224 +#define LUA_GCSTOP 0 45.225 +#define LUA_GCRESTART 1 45.226 +#define LUA_GCCOLLECT 2 45.227 +#define LUA_GCCOUNT 3 45.228 +#define LUA_GCCOUNTB 4 45.229 +#define LUA_GCSTEP 5 45.230 +#define LUA_GCSETPAUSE 6 45.231 +#define LUA_GCSETSTEPMUL 7 45.232 + 45.233 +LUA_API int (lua_gc) (lua_State *L, int what, int data); 45.234 + 45.235 + 45.236 +/* 45.237 +** miscellaneous functions 45.238 +*/ 45.239 + 45.240 +LUA_API int (lua_error) (lua_State *L); 45.241 + 45.242 +LUA_API int (lua_next) (lua_State *L, int idx); 45.243 + 45.244 +LUA_API void (lua_concat) (lua_State *L, int n); 45.245 + 45.246 +LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); 45.247 +LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); 45.248 + 45.249 + 45.250 + 45.251 +/* 45.252 +** =============================================================== 45.253 +** some useful macros 45.254 +** =============================================================== 45.255 +*/ 45.256 + 45.257 +#define lua_pop(L,n) lua_settop(L, -(n)-1) 45.258 + 45.259 +#define lua_newtable(L) lua_createtable(L, 0, 0) 45.260 + 45.261 +#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) 45.262 + 45.263 +#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) 45.264 + 45.265 +#define lua_strlen(L,i) lua_objlen(L, (i)) 45.266 + 45.267 +#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) 45.268 +#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) 45.269 +#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) 45.270 +#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) 45.271 +#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) 45.272 +#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) 45.273 +#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) 45.274 +#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) 45.275 + 45.276 +#define lua_pushliteral(L, s) \ 45.277 + lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) 45.278 + 45.279 +#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) 45.280 +#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) 45.281 + 45.282 +#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) 45.283 + 45.284 + 45.285 + 45.286 +/* 45.287 +** compatibility macros and functions 45.288 +*/ 45.289 + 45.290 +#define lua_open() luaL_newstate() 45.291 + 45.292 +#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) 45.293 + 45.294 +#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0) 45.295 + 45.296 +#define lua_Chunkreader lua_Reader 45.297 +#define lua_Chunkwriter lua_Writer 45.298 + 45.299 + 45.300 +/* hack */ 45.301 +LUA_API void lua_setlevel (lua_State *from, lua_State *to); 45.302 + 45.303 + 45.304 +/* 45.305 +** {====================================================================== 45.306 +** Debug API 45.307 +** ======================================================================= 45.308 +*/ 45.309 + 45.310 + 45.311 +/* 45.312 +** Event codes 45.313 +*/ 45.314 +#define LUA_HOOKCALL 0 45.315 +#define LUA_HOOKRET 1 45.316 +#define LUA_HOOKLINE 2 45.317 +#define LUA_HOOKCOUNT 3 45.318 +#define LUA_HOOKTAILRET 4 45.319 + 45.320 + 45.321 +/* 45.322 +** Event masks 45.323 +*/ 45.324 +#define LUA_MASKCALL (1 << LUA_HOOKCALL) 45.325 +#define LUA_MASKRET (1 << LUA_HOOKRET) 45.326 +#define LUA_MASKLINE (1 << LUA_HOOKLINE) 45.327 +#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) 45.328 + 45.329 +typedef struct lua_Debug lua_Debug; /* activation record */ 45.330 + 45.331 + 45.332 +/* Functions to be called by the debuger in specific events */ 45.333 +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); 45.334 + 45.335 + 45.336 +LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar); 45.337 +LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); 45.338 +LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); 45.339 +LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); 45.340 +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n); 45.341 +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n); 45.342 + 45.343 +LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); 45.344 +LUA_API lua_Hook lua_gethook (lua_State *L); 45.345 +LUA_API int lua_gethookmask (lua_State *L); 45.346 +LUA_API int lua_gethookcount (lua_State *L); 45.347 + 45.348 + 45.349 +struct lua_Debug { 45.350 + int event; 45.351 + const char *name; /* (n) */ 45.352 + const char *namewhat; /* (n) `global', `local', `field', `method' */ 45.353 + const char *what; /* (S) `Lua', `C', `main', `tail' */ 45.354 + const char *source; /* (S) */ 45.355 + int currentline; /* (l) */ 45.356 + int nups; /* (u) number of upvalues */ 45.357 + int linedefined; /* (S) */ 45.358 + int lastlinedefined; /* (S) */ 45.359 + char short_src[LUA_IDSIZE]; /* (S) */ 45.360 + /* private part */ 45.361 + int i_ci; /* active function */ 45.362 +}; 45.363 + 45.364 +/* }====================================================================== */ 45.365 + 45.366 + 45.367 +/****************************************************************************** 45.368 +* Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved. 45.369 +* 45.370 +* Permission is hereby granted, free of charge, to any person obtaining 45.371 +* a copy of this software and associated documentation files (the 45.372 +* "Software"), to deal in the Software without restriction, including 45.373 +* without limitation the rights to use, copy, modify, merge, publish, 45.374 +* distribute, sublicense, and/or sell copies of the Software, and to 45.375 +* permit persons to whom the Software is furnished to do so, subject to 45.376 +* the following conditions: 45.377 +* 45.378 +* The above copyright notice and this permission notice shall be 45.379 +* included in all copies or substantial portions of the Software. 45.380 +* 45.381 +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 45.382 +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 45.383 +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 45.384 +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 45.385 +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 45.386 +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 45.387 +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 45.388 +******************************************************************************/ 45.389 + 45.390 + 45.391 +#endif
46.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 46.2 +++ b/src/lua/luaconf.h Sat Mar 03 11:07:39 2012 -0600 46.3 @@ -0,0 +1,763 @@ 46.4 +/* 46.5 +** $Id: luaconf.h,v 1.82.1.7 2008/02/11 16:25:08 roberto Exp $ 46.6 +** Configuration file for Lua 46.7 +** See Copyright Notice in lua.h 46.8 +*/ 46.9 + 46.10 + 46.11 +#ifndef lconfig_h 46.12 +#define lconfig_h 46.13 + 46.14 +#include <limits.h> 46.15 +#include <stddef.h> 46.16 + 46.17 + 46.18 +/* 46.19 +** ================================================================== 46.20 +** Search for "@@" to find all configurable definitions. 46.21 +** =================================================================== 46.22 +*/ 46.23 + 46.24 + 46.25 +/* 46.26 +@@ LUA_ANSI controls the use of non-ansi features. 46.27 +** CHANGE it (define it) if you want Lua to avoid the use of any 46.28 +** non-ansi feature or library. 46.29 +*/ 46.30 +#if defined(__STRICT_ANSI__) 46.31 +#define LUA_ANSI 46.32 +#endif 46.33 + 46.34 + 46.35 +#if !defined(LUA_ANSI) && defined(_WIN32) 46.36 +#define LUA_WIN 46.37 +#endif 46.38 + 46.39 +#if defined(LUA_USE_LINUX) 46.40 +#define LUA_USE_POSIX 46.41 +#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ 46.42 +#define LUA_USE_READLINE /* needs some extra libraries */ 46.43 +#endif 46.44 + 46.45 +#if defined(LUA_USE_MACOSX) 46.46 +#define LUA_USE_POSIX 46.47 +#define LUA_DL_DYLD /* does not need extra library */ 46.48 +#endif 46.49 + 46.50 + 46.51 + 46.52 +/* 46.53 +@@ LUA_USE_POSIX includes all functionallity listed as X/Open System 46.54 +@* Interfaces Extension (XSI). 46.55 +** CHANGE it (define it) if your system is XSI compatible. 46.56 +*/ 46.57 +#if defined(LUA_USE_POSIX) 46.58 +#define LUA_USE_MKSTEMP 46.59 +#define LUA_USE_ISATTY 46.60 +#define LUA_USE_POPEN 46.61 +#define LUA_USE_ULONGJMP 46.62 +#endif 46.63 + 46.64 + 46.65 +/* 46.66 +@@ LUA_PATH and LUA_CPATH are the names of the environment variables that 46.67 +@* Lua check to set its paths. 46.68 +@@ LUA_INIT is the name of the environment variable that Lua 46.69 +@* checks for initialization code. 46.70 +** CHANGE them if you want different names. 46.71 +*/ 46.72 +#define LUA_PATH "LUA_PATH" 46.73 +#define LUA_CPATH "LUA_CPATH" 46.74 +#define LUA_INIT "LUA_INIT" 46.75 + 46.76 + 46.77 +/* 46.78 +@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for 46.79 +@* Lua libraries. 46.80 +@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for 46.81 +@* C libraries. 46.82 +** CHANGE them if your machine has a non-conventional directory 46.83 +** hierarchy or if you want to install your libraries in 46.84 +** non-conventional directories. 46.85 +*/ 46.86 +#if defined(_WIN32) 46.87 +/* 46.88 +** In Windows, any exclamation mark ('!') in the path is replaced by the 46.89 +** path of the directory of the executable file of the current process. 46.90 +*/ 46.91 +#define LUA_LDIR "!\\lua\\" 46.92 +#define LUA_CDIR "!\\" 46.93 +#define LUA_PATH_DEFAULT \ 46.94 + ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ 46.95 + LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua" 46.96 +#define LUA_CPATH_DEFAULT \ 46.97 + ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll" 46.98 + 46.99 +#else 46.100 +#define LUA_ROOT "/usr/local/" 46.101 +#define LUA_LDIR LUA_ROOT "share/lua/5.1/" 46.102 +#define LUA_CDIR LUA_ROOT "lib/lua/5.1/" 46.103 +#define LUA_PATH_DEFAULT \ 46.104 + "./?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ 46.105 + LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua" 46.106 +#define LUA_CPATH_DEFAULT \ 46.107 + "./?.so;" LUA_CDIR"?.so;" LUA_CDIR"loadall.so" 46.108 +#endif 46.109 + 46.110 + 46.111 +/* 46.112 +@@ LUA_DIRSEP is the directory separator (for submodules). 46.113 +** CHANGE it if your machine does not use "/" as the directory separator 46.114 +** and is not Windows. (On Windows Lua automatically uses "\".) 46.115 +*/ 46.116 +#if defined(_WIN32) 46.117 +#define LUA_DIRSEP "\\" 46.118 +#else 46.119 +#define LUA_DIRSEP "/" 46.120 +#endif 46.121 + 46.122 + 46.123 +/* 46.124 +@@ LUA_PATHSEP is the character that separates templates in a path. 46.125 +@@ LUA_PATH_MARK is the string that marks the substitution points in a 46.126 +@* template. 46.127 +@@ LUA_EXECDIR in a Windows path is replaced by the executable's 46.128 +@* directory. 46.129 +@@ LUA_IGMARK is a mark to ignore all before it when bulding the 46.130 +@* luaopen_ function name. 46.131 +** CHANGE them if for some reason your system cannot use those 46.132 +** characters. (E.g., if one of those characters is a common character 46.133 +** in file/directory names.) Probably you do not need to change them. 46.134 +*/ 46.135 +#define LUA_PATHSEP ";" 46.136 +#define LUA_PATH_MARK "?" 46.137 +#define LUA_EXECDIR "!" 46.138 +#define LUA_IGMARK "-" 46.139 + 46.140 + 46.141 +/* 46.142 +@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger. 46.143 +** CHANGE that if ptrdiff_t is not adequate on your machine. (On most 46.144 +** machines, ptrdiff_t gives a good choice between int or long.) 46.145 +*/ 46.146 +#define LUA_INTEGER ptrdiff_t 46.147 + 46.148 + 46.149 +/* 46.150 +@@ LUA_API is a mark for all core API functions. 46.151 +@@ LUALIB_API is a mark for all standard library functions. 46.152 +** CHANGE them if you need to define those functions in some special way. 46.153 +** For instance, if you want to create one Windows DLL with the core and 46.154 +** the libraries, you may want to use the following definition (define 46.155 +** LUA_BUILD_AS_DLL to get it). 46.156 +*/ 46.157 +#if defined(LUA_BUILD_AS_DLL) 46.158 + 46.159 +#if defined(LUA_CORE) || defined(LUA_LIB) 46.160 +#define LUA_API __declspec(dllexport) 46.161 +#else 46.162 +#define LUA_API __declspec(dllimport) 46.163 +#endif 46.164 + 46.165 +#else 46.166 + 46.167 +#define LUA_API extern 46.168 + 46.169 +#endif 46.170 + 46.171 +/* more often than not the libs go together with the core */ 46.172 +#define LUALIB_API LUA_API 46.173 + 46.174 + 46.175 +/* 46.176 +@@ LUAI_FUNC is a mark for all extern functions that are not to be 46.177 +@* exported to outside modules. 46.178 +@@ LUAI_DATA is a mark for all extern (const) variables that are not to 46.179 +@* be exported to outside modules. 46.180 +** CHANGE them if you need to mark them in some special way. Elf/gcc 46.181 +** (versions 3.2 and later) mark them as "hidden" to optimize access 46.182 +** when Lua is compiled as a shared library. 46.183 +*/ 46.184 +#if defined(luaall_c) 46.185 +#define LUAI_FUNC static 46.186 +#define LUAI_DATA /* empty */ 46.187 + 46.188 +#elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ 46.189 + defined(__ELF__) 46.190 +#define LUAI_FUNC __attribute__((visibility("hidden"))) extern 46.191 +#define LUAI_DATA LUAI_FUNC 46.192 + 46.193 +#else 46.194 +#define LUAI_FUNC extern 46.195 +#define LUAI_DATA extern 46.196 +#endif 46.197 + 46.198 + 46.199 + 46.200 +/* 46.201 +@@ LUA_QL describes how error messages quote program elements. 46.202 +** CHANGE it if you want a different appearance. 46.203 +*/ 46.204 +#define LUA_QL(x) "'" x "'" 46.205 +#define LUA_QS LUA_QL("%s") 46.206 + 46.207 + 46.208 +/* 46.209 +@@ LUA_IDSIZE gives the maximum size for the description of the source 46.210 +@* of a function in debug information. 46.211 +** CHANGE it if you want a different size. 46.212 +*/ 46.213 +#define LUA_IDSIZE 60 46.214 + 46.215 + 46.216 +/* 46.217 +** {================================================================== 46.218 +** Stand-alone configuration 46.219 +** =================================================================== 46.220 +*/ 46.221 + 46.222 +#if defined(lua_c) || defined(luaall_c) 46.223 + 46.224 +/* 46.225 +@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that 46.226 +@* is, whether we're running lua interactively). 46.227 +** CHANGE it if you have a better definition for non-POSIX/non-Windows 46.228 +** systems. 46.229 +*/ 46.230 +#if defined(LUA_USE_ISATTY) 46.231 +#include <unistd.h> 46.232 +#define lua_stdin_is_tty() isatty(0) 46.233 +#elif defined(LUA_WIN) 46.234 +#include <io.h> 46.235 +#include <stdio.h> 46.236 +#define lua_stdin_is_tty() _isatty(_fileno(stdin)) 46.237 +#else 46.238 +#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ 46.239 +#endif 46.240 + 46.241 + 46.242 +/* 46.243 +@@ LUA_PROMPT is the default prompt used by stand-alone Lua. 46.244 +@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua. 46.245 +** CHANGE them if you want different prompts. (You can also change the 46.246 +** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.) 46.247 +*/ 46.248 +#define LUA_PROMPT "> " 46.249 +#define LUA_PROMPT2 ">> " 46.250 + 46.251 + 46.252 +/* 46.253 +@@ LUA_PROGNAME is the default name for the stand-alone Lua program. 46.254 +** CHANGE it if your stand-alone interpreter has a different name and 46.255 +** your system is not able to detect that name automatically. 46.256 +*/ 46.257 +#define LUA_PROGNAME "lua" 46.258 + 46.259 + 46.260 +/* 46.261 +@@ LUA_MAXINPUT is the maximum length for an input line in the 46.262 +@* stand-alone interpreter. 46.263 +** CHANGE it if you need longer lines. 46.264 +*/ 46.265 +#define LUA_MAXINPUT 512 46.266 + 46.267 + 46.268 +/* 46.269 +@@ lua_readline defines how to show a prompt and then read a line from 46.270 +@* the standard input. 46.271 +@@ lua_saveline defines how to "save" a read line in a "history". 46.272 +@@ lua_freeline defines how to free a line read by lua_readline. 46.273 +** CHANGE them if you want to improve this functionality (e.g., by using 46.274 +** GNU readline and history facilities). 46.275 +*/ 46.276 +#if defined(LUA_USE_READLINE) 46.277 +#include <stdio.h> 46.278 +#include <readline/readline.h> 46.279 +#include <readline/history.h> 46.280 +#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) 46.281 +#define lua_saveline(L,idx) \ 46.282 + if (lua_strlen(L,idx) > 0) /* non-empty line? */ \ 46.283 + add_history(lua_tostring(L, idx)); /* add it to history */ 46.284 +#define lua_freeline(L,b) ((void)L, free(b)) 46.285 +#else 46.286 +#define lua_readline(L,b,p) \ 46.287 + ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ 46.288 + fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ 46.289 +#define lua_saveline(L,idx) { (void)L; (void)idx; } 46.290 +#define lua_freeline(L,b) { (void)L; (void)b; } 46.291 +#endif 46.292 + 46.293 +#endif 46.294 + 46.295 +/* }================================================================== */ 46.296 + 46.297 + 46.298 +/* 46.299 +@@ LUAI_GCPAUSE defines the default pause between garbage-collector cycles 46.300 +@* as a percentage. 46.301 +** CHANGE it if you want the GC to run faster or slower (higher values 46.302 +** mean larger pauses which mean slower collection.) You can also change 46.303 +** this value dynamically. 46.304 +*/ 46.305 +#define LUAI_GCPAUSE 200 /* 200% (wait memory to double before next GC) */ 46.306 + 46.307 + 46.308 +/* 46.309 +@@ LUAI_GCMUL defines the default speed of garbage collection relative to 46.310 +@* memory allocation as a percentage. 46.311 +** CHANGE it if you want to change the granularity of the garbage 46.312 +** collection. (Higher values mean coarser collections. 0 represents 46.313 +** infinity, where each step performs a full collection.) You can also 46.314 +** change this value dynamically. 46.315 +*/ 46.316 +#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ 46.317 + 46.318 + 46.319 + 46.320 +/* 46.321 +@@ LUA_COMPAT_GETN controls compatibility with old getn behavior. 46.322 +** CHANGE it (define it) if you want exact compatibility with the 46.323 +** behavior of setn/getn in Lua 5.0. 46.324 +*/ 46.325 +#undef LUA_COMPAT_GETN 46.326 + 46.327 +/* 46.328 +@@ LUA_COMPAT_LOADLIB controls compatibility about global loadlib. 46.329 +** CHANGE it to undefined as soon as you do not need a global 'loadlib' 46.330 +** function (the function is still available as 'package.loadlib'). 46.331 +*/ 46.332 +#undef LUA_COMPAT_LOADLIB 46.333 + 46.334 +/* 46.335 +@@ LUA_COMPAT_VARARG controls compatibility with old vararg feature. 46.336 +** CHANGE it to undefined as soon as your programs use only '...' to 46.337 +** access vararg parameters (instead of the old 'arg' table). 46.338 +*/ 46.339 +#define LUA_COMPAT_VARARG 46.340 + 46.341 +/* 46.342 +@@ LUA_COMPAT_MOD controls compatibility with old math.mod function. 46.343 +** CHANGE it to undefined as soon as your programs use 'math.fmod' or 46.344 +** the new '%' operator instead of 'math.mod'. 46.345 +*/ 46.346 +#define LUA_COMPAT_MOD 46.347 + 46.348 +/* 46.349 +@@ LUA_COMPAT_LSTR controls compatibility with old long string nesting 46.350 +@* facility. 46.351 +** CHANGE it to 2 if you want the old behaviour, or undefine it to turn 46.352 +** off the advisory error when nesting [[...]]. 46.353 +*/ 46.354 +#define LUA_COMPAT_LSTR 1 46.355 + 46.356 +/* 46.357 +@@ LUA_COMPAT_GFIND controls compatibility with old 'string.gfind' name. 46.358 +** CHANGE it to undefined as soon as you rename 'string.gfind' to 46.359 +** 'string.gmatch'. 46.360 +*/ 46.361 +#define LUA_COMPAT_GFIND 46.362 + 46.363 +/* 46.364 +@@ LUA_COMPAT_OPENLIB controls compatibility with old 'luaL_openlib' 46.365 +@* behavior. 46.366 +** CHANGE it to undefined as soon as you replace to 'luaL_register' 46.367 +** your uses of 'luaL_openlib' 46.368 +*/ 46.369 +#define LUA_COMPAT_OPENLIB 46.370 + 46.371 + 46.372 + 46.373 +/* 46.374 +@@ luai_apicheck is the assert macro used by the Lua-C API. 46.375 +** CHANGE luai_apicheck if you want Lua to perform some checks in the 46.376 +** parameters it gets from API calls. This may slow down the interpreter 46.377 +** a bit, but may be quite useful when debugging C code that interfaces 46.378 +** with Lua. A useful redefinition is to use assert.h. 46.379 +*/ 46.380 +#if defined(LUA_USE_APICHECK) 46.381 +#include <assert.h> 46.382 +#define luai_apicheck(L,o) { (void)L; assert(o); } 46.383 +#else 46.384 +#define luai_apicheck(L,o) { (void)L; } 46.385 +#endif 46.386 + 46.387 + 46.388 +/* 46.389 +@@ LUAI_BITSINT defines the number of bits in an int. 46.390 +** CHANGE here if Lua cannot automatically detect the number of bits of 46.391 +** your machine. Probably you do not need to change this. 46.392 +*/ 46.393 +/* avoid overflows in comparison */ 46.394 +#if INT_MAX-20 < 32760 46.395 +#define LUAI_BITSINT 16 46.396 +#elif INT_MAX > 2147483640L 46.397 +/* int has at least 32 bits */ 46.398 +#define LUAI_BITSINT 32 46.399 +#else 46.400 +#error "you must define LUA_BITSINT with number of bits in an integer" 46.401 +#endif 46.402 + 46.403 + 46.404 +/* 46.405 +@@ LUAI_UINT32 is an unsigned integer with at least 32 bits. 46.406 +@@ LUAI_INT32 is an signed integer with at least 32 bits. 46.407 +@@ LUAI_UMEM is an unsigned integer big enough to count the total 46.408 +@* memory used by Lua. 46.409 +@@ LUAI_MEM is a signed integer big enough to count the total memory 46.410 +@* used by Lua. 46.411 +** CHANGE here if for some weird reason the default definitions are not 46.412 +** good enough for your machine. (The definitions in the 'else' 46.413 +** part always works, but may waste space on machines with 64-bit 46.414 +** longs.) Probably you do not need to change this. 46.415 +*/ 46.416 +#if LUAI_BITSINT >= 32 46.417 +#define LUAI_UINT32 unsigned int 46.418 +#define LUAI_INT32 int 46.419 +#define LUAI_MAXINT32 INT_MAX 46.420 +#define LUAI_UMEM size_t 46.421 +#define LUAI_MEM ptrdiff_t 46.422 +#else 46.423 +/* 16-bit ints */ 46.424 +#define LUAI_UINT32 unsigned long 46.425 +#define LUAI_INT32 long 46.426 +#define LUAI_MAXINT32 LONG_MAX 46.427 +#define LUAI_UMEM unsigned long 46.428 +#define LUAI_MEM long 46.429 +#endif 46.430 + 46.431 + 46.432 +/* 46.433 +@@ LUAI_MAXCALLS limits the number of nested calls. 46.434 +** CHANGE it if you need really deep recursive calls. This limit is 46.435 +** arbitrary; its only purpose is to stop infinite recursion before 46.436 +** exhausting memory. 46.437 +*/ 46.438 +#define LUAI_MAXCALLS 20000 46.439 + 46.440 + 46.441 +/* 46.442 +@@ LUAI_MAXCSTACK limits the number of Lua stack slots that a C function 46.443 +@* can use. 46.444 +** CHANGE it if you need lots of (Lua) stack space for your C 46.445 +** functions. This limit is arbitrary; its only purpose is to stop C 46.446 +** functions to consume unlimited stack space. (must be smaller than 46.447 +** -LUA_REGISTRYINDEX) 46.448 +*/ 46.449 +#define LUAI_MAXCSTACK 8000 46.450 + 46.451 + 46.452 + 46.453 +/* 46.454 +** {================================================================== 46.455 +** CHANGE (to smaller values) the following definitions if your system 46.456 +** has a small C stack. (Or you may want to change them to larger 46.457 +** values if your system has a large C stack and these limits are 46.458 +** too rigid for you.) Some of these constants control the size of 46.459 +** stack-allocated arrays used by the compiler or the interpreter, while 46.460 +** others limit the maximum number of recursive calls that the compiler 46.461 +** or the interpreter can perform. Values too large may cause a C stack 46.462 +** overflow for some forms of deep constructs. 46.463 +** =================================================================== 46.464 +*/ 46.465 + 46.466 + 46.467 +/* 46.468 +@@ LUAI_MAXCCALLS is the maximum depth for nested C calls (short) and 46.469 +@* syntactical nested non-terminals in a program. 46.470 +*/ 46.471 +#define LUAI_MAXCCALLS 200 46.472 + 46.473 + 46.474 +/* 46.475 +@@ LUAI_MAXVARS is the maximum number of local variables per function 46.476 +@* (must be smaller than 250). 46.477 +*/ 46.478 +#define LUAI_MAXVARS 200 46.479 + 46.480 + 46.481 +/* 46.482 +@@ LUAI_MAXUPVALUES is the maximum number of upvalues per function 46.483 +@* (must be smaller than 250). 46.484 +*/ 46.485 +#define LUAI_MAXUPVALUES 60 46.486 + 46.487 + 46.488 +/* 46.489 +@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. 46.490 +*/ 46.491 +#define LUAL_BUFFERSIZE BUFSIZ 46.492 + 46.493 +/* }================================================================== */ 46.494 + 46.495 + 46.496 + 46.497 + 46.498 +/* 46.499 +** {================================================================== 46.500 +@@ LUA_NUMBER is the type of numbers in Lua. 46.501 +** CHANGE the following definitions only if you want to build Lua 46.502 +** with a number type different from double. You may also need to 46.503 +** change lua_number2int & lua_number2integer. 46.504 +** =================================================================== 46.505 +*/ 46.506 + 46.507 +#define LUA_NUMBER_DOUBLE 46.508 +#define LUA_NUMBER double 46.509 + 46.510 +/* 46.511 +@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' 46.512 +@* over a number. 46.513 +*/ 46.514 +#define LUAI_UACNUMBER double 46.515 + 46.516 + 46.517 +/* 46.518 +@@ LUA_NUMBER_SCAN is the format for reading numbers. 46.519 +@@ LUA_NUMBER_FMT is the format for writing numbers. 46.520 +@@ lua_number2str converts a number to a string. 46.521 +@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. 46.522 +@@ lua_str2number converts a string to a number. 46.523 +*/ 46.524 +#define LUA_NUMBER_SCAN "%lf" 46.525 +#define LUA_NUMBER_FMT "%.14g" 46.526 +#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) 46.527 +#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ 46.528 +#define lua_str2number(s,p) strtod((s), (p)) 46.529 + 46.530 + 46.531 +/* 46.532 +@@ The luai_num* macros define the primitive operations over numbers. 46.533 +*/ 46.534 +#if defined(LUA_CORE) 46.535 +#include <math.h> 46.536 +#define luai_numadd(a,b) ((a)+(b)) 46.537 +#define luai_numsub(a,b) ((a)-(b)) 46.538 +#define luai_nummul(a,b) ((a)*(b)) 46.539 +#define luai_numdiv(a,b) ((a)/(b)) 46.540 +#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b)) 46.541 +#define luai_numpow(a,b) (pow(a,b)) 46.542 +#define luai_numunm(a) (-(a)) 46.543 +#define luai_numeq(a,b) ((a)==(b)) 46.544 +#define luai_numlt(a,b) ((a)<(b)) 46.545 +#define luai_numle(a,b) ((a)<=(b)) 46.546 +#define luai_numisnan(a) (!luai_numeq((a), (a))) 46.547 +#endif 46.548 + 46.549 + 46.550 +/* 46.551 +@@ lua_number2int is a macro to convert lua_Number to int. 46.552 +@@ lua_number2integer is a macro to convert lua_Number to lua_Integer. 46.553 +** CHANGE them if you know a faster way to convert a lua_Number to 46.554 +** int (with any rounding method and without throwing errors) in your 46.555 +** system. In Pentium machines, a naive typecast from double to int 46.556 +** in C is extremely slow, so any alternative is worth trying. 46.557 +*/ 46.558 + 46.559 +/* On a Pentium, resort to a trick */ 46.560 +#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \ 46.561 + (defined(__i386) || defined (_M_IX86) || defined(__i386__)) 46.562 + 46.563 +/* On a Microsoft compiler, use assembler */ 46.564 +#if 0//defined(_MSC_VER) // nitsuja: the "using assembly" method doesn't correctly handle integers between 0xFF000000 and and 0xFFFFFFFF, while the other branch does handle it correctly and it's not slower. 46.565 + 46.566 +#define lua_number2int(i,d) __asm fld d __asm fistp i 46.567 +#define lua_number2integer(i,n) lua_number2int(i, n) 46.568 + 46.569 +/* the next trick should work on any Pentium, but sometimes clashes 46.570 + with a DirectX idiosyncrasy */ 46.571 +#else 46.572 + 46.573 +union luai_Cast { double l_d; long l_l; }; 46.574 +#define lua_number2int(i,d) \ 46.575 + { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; } 46.576 +#define lua_number2integer(i,n) lua_number2int(i, n) 46.577 + 46.578 +#endif 46.579 + 46.580 + 46.581 +/* this option always works, but may be slow */ 46.582 +#else 46.583 +#define lua_number2int(i,d) ((i)=(int)(d)) 46.584 +#define lua_number2integer(i,d) ((i)=(lua_Integer)(d)) 46.585 + 46.586 +#endif 46.587 + 46.588 +/* }================================================================== */ 46.589 + 46.590 + 46.591 +/* 46.592 +@@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment. 46.593 +** CHANGE it if your system requires alignments larger than double. (For 46.594 +** instance, if your system supports long doubles and they must be 46.595 +** aligned in 16-byte boundaries, then you should add long double in the 46.596 +** union.) Probably you do not need to change this. 46.597 +*/ 46.598 +#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } 46.599 + 46.600 + 46.601 +/* 46.602 +@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling. 46.603 +** CHANGE them if you prefer to use longjmp/setjmp even with C++ 46.604 +** or if want/don't to use _longjmp/_setjmp instead of regular 46.605 +** longjmp/setjmp. By default, Lua handles errors with exceptions when 46.606 +** compiling as C++ code, with _longjmp/_setjmp when asked to use them, 46.607 +** and with longjmp/setjmp otherwise. 46.608 +*/ 46.609 +#if defined(__cplusplus) 46.610 +/* C++ exceptions */ 46.611 +#define LUAI_THROW(L,c) throw(c) 46.612 +#define LUAI_TRY(L,c,a) try { a } catch(...) \ 46.613 + { if ((c)->status == 0) (c)->status = -1; } 46.614 +#define luai_jmpbuf int /* dummy variable */ 46.615 + 46.616 +#elif defined(LUA_USE_ULONGJMP) 46.617 +/* in Unix, try _longjmp/_setjmp (more efficient) */ 46.618 +#define LUAI_THROW(L,c) _longjmp((c)->b, 1) 46.619 +#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } 46.620 +#define luai_jmpbuf jmp_buf 46.621 + 46.622 +#else 46.623 +/* default handling with long jumps */ 46.624 +#define LUAI_THROW(L,c) longjmp((c)->b, 1) 46.625 +#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } 46.626 +#define luai_jmpbuf jmp_buf 46.627 + 46.628 +#endif 46.629 + 46.630 + 46.631 +/* 46.632 +@@ LUA_MAXCAPTURES is the maximum number of captures that a pattern 46.633 +@* can do during pattern-matching. 46.634 +** CHANGE it if you need more captures. This limit is arbitrary. 46.635 +*/ 46.636 +#define LUA_MAXCAPTURES 32 46.637 + 46.638 + 46.639 +/* 46.640 +@@ lua_tmpnam is the function that the OS library uses to create a 46.641 +@* temporary name. 46.642 +@@ LUA_TMPNAMBUFSIZE is the maximum size of a name created by lua_tmpnam. 46.643 +** CHANGE them if you have an alternative to tmpnam (which is considered 46.644 +** insecure) or if you want the original tmpnam anyway. By default, Lua 46.645 +** uses tmpnam except when POSIX is available, where it uses mkstemp. 46.646 +*/ 46.647 +#if defined(loslib_c) || defined(luaall_c) 46.648 + 46.649 +#if defined(LUA_USE_MKSTEMP) 46.650 +#include <unistd.h> 46.651 +#define LUA_TMPNAMBUFSIZE 32 46.652 +#define lua_tmpnam(b,e) { \ 46.653 + strcpy(b, "/tmp/lua_XXXXXX"); \ 46.654 + e = mkstemp(b); \ 46.655 + if (e != -1) close(e); \ 46.656 + e = (e == -1); } 46.657 + 46.658 +#else 46.659 +#define LUA_TMPNAMBUFSIZE L_tmpnam 46.660 +#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } 46.661 +#endif 46.662 + 46.663 +#endif 46.664 + 46.665 + 46.666 +/* 46.667 +@@ lua_popen spawns a new process connected to the current one through 46.668 +@* the file streams. 46.669 +** CHANGE it if you have a way to implement it in your system. 46.670 +*/ 46.671 +#if defined(LUA_USE_POPEN) 46.672 + 46.673 +#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) 46.674 +#define lua_pclose(L,file) ((void)L, (pclose(file) != -1)) 46.675 + 46.676 +#elif defined(LUA_WIN) 46.677 + 46.678 +#define lua_popen(L,c,m) ((void)L, _popen(c,m)) 46.679 +#define lua_pclose(L,file) ((void)L, (_pclose(file) != -1)) 46.680 + 46.681 +#else 46.682 + 46.683 +#define lua_popen(L,c,m) ((void)((void)c, m), \ 46.684 + luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0) 46.685 +#define lua_pclose(L,file) ((void)((void)L, file), 0) 46.686 + 46.687 +#endif 46.688 + 46.689 +/* 46.690 +@@ LUA_DL_* define which dynamic-library system Lua should use. 46.691 +** CHANGE here if Lua has problems choosing the appropriate 46.692 +** dynamic-library system for your platform (either Windows' DLL, Mac's 46.693 +** dyld, or Unix's dlopen). If your system is some kind of Unix, there 46.694 +** is a good chance that it has dlopen, so LUA_DL_DLOPEN will work for 46.695 +** it. To use dlopen you also need to adapt the src/Makefile (probably 46.696 +** adding -ldl to the linker options), so Lua does not select it 46.697 +** automatically. (When you change the makefile to add -ldl, you must 46.698 +** also add -DLUA_USE_DLOPEN.) 46.699 +** If you do not want any kind of dynamic library, undefine all these 46.700 +** options. 46.701 +** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD. 46.702 +*/ 46.703 +#if defined(LUA_USE_DLOPEN) 46.704 +#define LUA_DL_DLOPEN 46.705 +#endif 46.706 + 46.707 +#if defined(LUA_WIN) 46.708 +#define LUA_DL_DLL 46.709 +#endif 46.710 + 46.711 + 46.712 +/* 46.713 +@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State 46.714 +@* (the data goes just *before* the lua_State pointer). 46.715 +** CHANGE (define) this if you really need that. This value must be 46.716 +** a multiple of the maximum alignment required for your machine. 46.717 +*/ 46.718 +#define LUAI_EXTRASPACE 0 46.719 + 46.720 + 46.721 +/* 46.722 +@@ luai_userstate* allow user-specific actions on threads. 46.723 +** CHANGE them if you defined LUAI_EXTRASPACE and need to do something 46.724 +** extra when a thread is created/deleted/resumed/yielded. 46.725 +*/ 46.726 +#define luai_userstateopen(L) ((void)L) 46.727 +#define luai_userstateclose(L) ((void)L) 46.728 +#define luai_userstatethread(L,L1) ((void)L) 46.729 +#define luai_userstatefree(L) ((void)L) 46.730 +#define luai_userstateresume(L,n) ((void)L) 46.731 +#define luai_userstateyield(L,n) ((void)L) 46.732 + 46.733 + 46.734 +/* 46.735 +@@ LUA_INTFRMLEN is the length modifier for integer conversions 46.736 +@* in 'string.format'. 46.737 +@@ LUA_INTFRM_T is the integer type correspoding to the previous length 46.738 +@* modifier. 46.739 +** CHANGE them if your system supports long long or does not support long. 46.740 +*/ 46.741 + 46.742 +#if defined(LUA_USELONGLONG) 46.743 + 46.744 +#define LUA_INTFRMLEN "ll" 46.745 +#define LUA_INTFRM_T long long 46.746 + 46.747 +#else 46.748 + 46.749 +#define LUA_INTFRMLEN "l" 46.750 +#define LUA_INTFRM_T long 46.751 + 46.752 +#endif 46.753 + 46.754 + 46.755 + 46.756 +/* =================================================================== */ 46.757 + 46.758 +/* 46.759 +** Local configuration. You can use this space to add your redefinitions 46.760 +** without modifying the main part of the file. 46.761 +*/ 46.762 + 46.763 + 46.764 + 46.765 +#endif 46.766 +
47.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 47.2 +++ b/src/lua/lualib.h Sat Mar 03 11:07:39 2012 -0600 47.3 @@ -0,0 +1,53 @@ 47.4 +/* 47.5 +** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $ 47.6 +** Lua standard libraries 47.7 +** See Copyright Notice in lua.h 47.8 +*/ 47.9 + 47.10 + 47.11 +#ifndef lualib_h 47.12 +#define lualib_h 47.13 + 47.14 +#include "lua.h" 47.15 + 47.16 + 47.17 +/* Key to file-handle type */ 47.18 +#define LUA_FILEHANDLE "FILE*" 47.19 + 47.20 + 47.21 +#define LUA_COLIBNAME "coroutine" 47.22 +LUALIB_API int (luaopen_base) (lua_State *L); 47.23 + 47.24 +#define LUA_TABLIBNAME "table" 47.25 +LUALIB_API int (luaopen_table) (lua_State *L); 47.26 + 47.27 +#define LUA_IOLIBNAME "io" 47.28 +LUALIB_API int (luaopen_io) (lua_State *L); 47.29 + 47.30 +#define LUA_OSLIBNAME "os" 47.31 +LUALIB_API int (luaopen_os) (lua_State *L); 47.32 + 47.33 +#define LUA_STRLIBNAME "string" 47.34 +LUALIB_API int (luaopen_string) (lua_State *L); 47.35 + 47.36 +#define LUA_MATHLIBNAME "math" 47.37 +LUALIB_API int (luaopen_math) (lua_State *L); 47.38 + 47.39 +#define LUA_DBLIBNAME "debug" 47.40 +LUALIB_API int (luaopen_debug) (lua_State *L); 47.41 + 47.42 +#define LUA_LOADLIBNAME "package" 47.43 +LUALIB_API int (luaopen_package) (lua_State *L); 47.44 + 47.45 + 47.46 +/* open all previous libraries */ 47.47 +LUALIB_API void (luaL_openlibs) (lua_State *L); 47.48 + 47.49 + 47.50 + 47.51 +#ifndef lua_assert 47.52 +#define lua_assert(x) ((void)0) 47.53 +#endif 47.54 + 47.55 + 47.56 +#endif
48.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 48.2 +++ b/src/lua/lundump.c Sat Mar 03 11:07:39 2012 -0600 48.3 @@ -0,0 +1,227 @@ 48.4 +/* 48.5 +** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $ 48.6 +** load precompiled Lua chunks 48.7 +** See Copyright Notice in lua.h 48.8 +*/ 48.9 + 48.10 +#include <string.h> 48.11 + 48.12 +#define lundump_c 48.13 +#define LUA_CORE 48.14 + 48.15 +#include "lua.h" 48.16 + 48.17 +#include "ldebug.h" 48.18 +#include "ldo.h" 48.19 +#include "lfunc.h" 48.20 +#include "lmem.h" 48.21 +#include "lobject.h" 48.22 +#include "lstring.h" 48.23 +#include "lundump.h" 48.24 +#include "lzio.h" 48.25 + 48.26 +typedef struct { 48.27 + lua_State* L; 48.28 + ZIO* Z; 48.29 + Mbuffer* b; 48.30 + const char* name; 48.31 +} LoadState; 48.32 + 48.33 +#ifdef LUAC_TRUST_BINARIES 48.34 +#define IF(c,s) 48.35 +#define error(S,s) 48.36 +#else 48.37 +#define IF(c,s) if (c) error(S,s) 48.38 + 48.39 +static void error(LoadState* S, const char* why) 48.40 +{ 48.41 + luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); 48.42 + luaD_throw(S->L,LUA_ERRSYNTAX); 48.43 +} 48.44 +#endif 48.45 + 48.46 +#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 48.47 +#define LoadByte(S) (lu_byte)LoadChar(S) 48.48 +#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 48.49 +#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 48.50 + 48.51 +static void LoadBlock(LoadState* S, void* b, size_t size) 48.52 +{ 48.53 + size_t r=luaZ_read(S->Z,b,size); 48.54 + IF (r!=0, "unexpected end"); 48.55 +} 48.56 + 48.57 +static int LoadChar(LoadState* S) 48.58 +{ 48.59 + char x; 48.60 + LoadVar(S,x); 48.61 + return x; 48.62 +} 48.63 + 48.64 +static int LoadInt(LoadState* S) 48.65 +{ 48.66 + int x; 48.67 + LoadVar(S,x); 48.68 + IF (x<0, "bad integer"); 48.69 + return x; 48.70 +} 48.71 + 48.72 +static lua_Number LoadNumber(LoadState* S) 48.73 +{ 48.74 + lua_Number x; 48.75 + LoadVar(S,x); 48.76 + return x; 48.77 +} 48.78 + 48.79 +static TString* LoadString(LoadState* S) 48.80 +{ 48.81 + size_t size; 48.82 + LoadVar(S,size); 48.83 + if (size==0) 48.84 + return NULL; 48.85 + else 48.86 + { 48.87 + char* s=luaZ_openspace(S->L,S->b,size); 48.88 + LoadBlock(S,s,size); 48.89 + return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 48.90 + } 48.91 +} 48.92 + 48.93 +static void LoadCode(LoadState* S, Proto* f) 48.94 +{ 48.95 + int n=LoadInt(S); 48.96 + f->code=luaM_newvector(S->L,n,Instruction); 48.97 + f->sizecode=n; 48.98 + LoadVector(S,f->code,n,sizeof(Instruction)); 48.99 +} 48.100 + 48.101 +static Proto* LoadFunction(LoadState* S, TString* p); 48.102 + 48.103 +static void LoadConstants(LoadState* S, Proto* f) 48.104 +{ 48.105 + int i,n; 48.106 + n=LoadInt(S); 48.107 + f->k=luaM_newvector(S->L,n,TValue); 48.108 + f->sizek=n; 48.109 + for (i=0; i<n; i++) setnilvalue(&f->k[i]); 48.110 + for (i=0; i<n; i++) 48.111 + { 48.112 + TValue* o=&f->k[i]; 48.113 + int t=LoadChar(S); 48.114 + switch (t) 48.115 + { 48.116 + case LUA_TNIL: 48.117 + setnilvalue(o); 48.118 + break; 48.119 + case LUA_TBOOLEAN: 48.120 + setbvalue(o,LoadChar(S)!=0); 48.121 + break; 48.122 + case LUA_TNUMBER: 48.123 + setnvalue(o,LoadNumber(S)); 48.124 + break; 48.125 + case LUA_TSTRING: 48.126 + setsvalue2n(S->L,o,LoadString(S)); 48.127 + break; 48.128 + default: 48.129 + error(S,"bad constant"); 48.130 + break; 48.131 + } 48.132 + } 48.133 + n=LoadInt(S); 48.134 + f->p=luaM_newvector(S->L,n,Proto*); 48.135 + f->sizep=n; 48.136 + for (i=0; i<n; i++) f->p[i]=NULL; 48.137 + for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source); 48.138 +} 48.139 + 48.140 +static void LoadDebug(LoadState* S, Proto* f) 48.141 +{ 48.142 + int i,n; 48.143 + n=LoadInt(S); 48.144 + f->lineinfo=luaM_newvector(S->L,n,int); 48.145 + f->sizelineinfo=n; 48.146 + LoadVector(S,f->lineinfo,n,sizeof(int)); 48.147 + n=LoadInt(S); 48.148 + f->locvars=luaM_newvector(S->L,n,LocVar); 48.149 + f->sizelocvars=n; 48.150 + for (i=0; i<n; i++) f->locvars[i].varname=NULL; 48.151 + for (i=0; i<n; i++) 48.152 + { 48.153 + f->locvars[i].varname=LoadString(S); 48.154 + f->locvars[i].startpc=LoadInt(S); 48.155 + f->locvars[i].endpc=LoadInt(S); 48.156 + } 48.157 + n=LoadInt(S); 48.158 + f->upvalues=luaM_newvector(S->L,n,TString*); 48.159 + f->sizeupvalues=n; 48.160 + for (i=0; i<n; i++) f->upvalues[i]=NULL; 48.161 + for (i=0; i<n; i++) f->upvalues[i]=LoadString(S); 48.162 +} 48.163 + 48.164 +static Proto* LoadFunction(LoadState* S, TString* p) 48.165 +{ 48.166 + Proto* f; 48.167 + if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); 48.168 + f=luaF_newproto(S->L); 48.169 + setptvalue2s(S->L,S->L->top,f); incr_top(S->L); 48.170 + f->source=LoadString(S); if (f->source==NULL) f->source=p; 48.171 + f->linedefined=LoadInt(S); 48.172 + f->lastlinedefined=LoadInt(S); 48.173 + f->nups=LoadByte(S); 48.174 + f->numparams=LoadByte(S); 48.175 + f->is_vararg=LoadByte(S); 48.176 + f->maxstacksize=LoadByte(S); 48.177 + LoadCode(S,f); 48.178 + LoadConstants(S,f); 48.179 + LoadDebug(S,f); 48.180 + IF (!luaG_checkcode(f), "bad code"); 48.181 + S->L->top--; 48.182 + S->L->nCcalls--; 48.183 + return f; 48.184 +} 48.185 + 48.186 +static void LoadHeader(LoadState* S) 48.187 +{ 48.188 + char h[LUAC_HEADERSIZE]; 48.189 + char s[LUAC_HEADERSIZE]; 48.190 + luaU_header(h); 48.191 + LoadBlock(S,s,LUAC_HEADERSIZE); 48.192 + IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); 48.193 +} 48.194 + 48.195 +/* 48.196 +** load precompiled chunk 48.197 +*/ 48.198 +Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 48.199 +{ 48.200 + LoadState S; 48.201 + if (*name=='@' || *name=='=') 48.202 + S.name=name+1; 48.203 + else if (*name==LUA_SIGNATURE[0]) 48.204 + S.name="binary string"; 48.205 + else 48.206 + S.name=name; 48.207 + S.L=L; 48.208 + S.Z=Z; 48.209 + S.b=buff; 48.210 + LoadHeader(&S); 48.211 + return LoadFunction(&S,luaS_newliteral(L,"=?")); 48.212 +} 48.213 + 48.214 +/* 48.215 +* make header 48.216 +*/ 48.217 +void luaU_header (char* h) 48.218 +{ 48.219 + int x=1; 48.220 + memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); 48.221 + h+=sizeof(LUA_SIGNATURE)-1; 48.222 + *h++=(char)LUAC_VERSION; 48.223 + *h++=(char)LUAC_FORMAT; 48.224 + *h++=(char)*(char*)&x; /* endianness */ 48.225 + *h++=(char)sizeof(int); 48.226 + *h++=(char)sizeof(size_t); 48.227 + *h++=(char)sizeof(Instruction); 48.228 + *h++=(char)sizeof(lua_Number); 48.229 + *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ 48.230 +}
49.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 49.2 +++ b/src/lua/lundump.h Sat Mar 03 11:07:39 2012 -0600 49.3 @@ -0,0 +1,36 @@ 49.4 +/* 49.5 +** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 49.6 +** load precompiled Lua chunks 49.7 +** See Copyright Notice in lua.h 49.8 +*/ 49.9 + 49.10 +#ifndef lundump_h 49.11 +#define lundump_h 49.12 + 49.13 +#include "lobject.h" 49.14 +#include "lzio.h" 49.15 + 49.16 +/* load one chunk; from lundump.c */ 49.17 +LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 49.18 + 49.19 +/* make header; from lundump.c */ 49.20 +LUAI_FUNC void luaU_header (char* h); 49.21 + 49.22 +/* dump one chunk; from ldump.c */ 49.23 +LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 49.24 + 49.25 +#ifdef luac_c 49.26 +/* print one chunk; from print.c */ 49.27 +LUAI_FUNC void luaU_print (const Proto* f, int full); 49.28 +#endif 49.29 + 49.30 +/* for header of binary files -- this is Lua 5.1 */ 49.31 +#define LUAC_VERSION 0x51 49.32 + 49.33 +/* for header of binary files -- this is the official format */ 49.34 +#define LUAC_FORMAT 0 49.35 + 49.36 +/* size of header of binary files */ 49.37 +#define LUAC_HEADERSIZE 12 49.38 + 49.39 +#endif
50.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 50.2 +++ b/src/lua/lvm.c Sat Mar 03 11:07:39 2012 -0600 50.3 @@ -0,0 +1,763 @@ 50.4 +/* 50.5 +** $Id: lvm.c,v 2.63.1.3 2007/12/28 15:32:23 roberto Exp $ 50.6 +** Lua virtual machine 50.7 +** See Copyright Notice in lua.h 50.8 +*/ 50.9 + 50.10 + 50.11 +#include <stdio.h> 50.12 +#include <stdlib.h> 50.13 +#include <string.h> 50.14 + 50.15 +#define lvm_c 50.16 +#define LUA_CORE 50.17 + 50.18 +#include "lua.h" 50.19 + 50.20 +#include "ldebug.h" 50.21 +#include "ldo.h" 50.22 +#include "lfunc.h" 50.23 +#include "lgc.h" 50.24 +#include "lobject.h" 50.25 +#include "lopcodes.h" 50.26 +#include "lstate.h" 50.27 +#include "lstring.h" 50.28 +#include "ltable.h" 50.29 +#include "ltm.h" 50.30 +#include "lvm.h" 50.31 + 50.32 + 50.33 + 50.34 +/* limit for table tag-method chains (to avoid loops) */ 50.35 +#define MAXTAGLOOP 100 50.36 + 50.37 + 50.38 +const TValue *luaV_tonumber (const TValue *obj, TValue *n) { 50.39 + lua_Number num; 50.40 + if (ttisnumber(obj)) return obj; 50.41 + if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) { 50.42 + setnvalue(n, num); 50.43 + return n; 50.44 + } 50.45 + else 50.46 + return NULL; 50.47 +} 50.48 + 50.49 + 50.50 +int luaV_tostring (lua_State *L, StkId obj) { 50.51 + if (!ttisnumber(obj)) 50.52 + return 0; 50.53 + else { 50.54 + char s[LUAI_MAXNUMBER2STR]; 50.55 + lua_Number n = nvalue(obj); 50.56 + lua_number2str(s, n); 50.57 + setsvalue2s(L, obj, luaS_new(L, s)); 50.58 + return 1; 50.59 + } 50.60 +} 50.61 + 50.62 + 50.63 +static void traceexec (lua_State *L, const Instruction *pc) { 50.64 + lu_byte mask = L->hookmask; 50.65 + const Instruction *oldpc = L->savedpc; 50.66 + L->savedpc = pc; 50.67 + if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) { 50.68 + resethookcount(L); 50.69 + luaD_callhook(L, LUA_HOOKCOUNT, -1); 50.70 + } 50.71 + if (mask & LUA_MASKLINE) { 50.72 + Proto *p = ci_func(L->ci)->l.p; 50.73 + int npc = pcRel(pc, p); 50.74 + int newline = getline(p, npc); 50.75 + /* call linehook when enter a new function, when jump back (loop), 50.76 + or when enter a new line */ 50.77 + if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p))) 50.78 + luaD_callhook(L, LUA_HOOKLINE, newline); 50.79 + } 50.80 +} 50.81 + 50.82 + 50.83 +static void callTMres (lua_State *L, StkId res, const TValue *f, 50.84 + const TValue *p1, const TValue *p2) { 50.85 + ptrdiff_t result = savestack(L, res); 50.86 + setobj2s(L, L->top, f); /* push function */ 50.87 + setobj2s(L, L->top+1, p1); /* 1st argument */ 50.88 + setobj2s(L, L->top+2, p2); /* 2nd argument */ 50.89 + luaD_checkstack(L, 3); 50.90 + L->top += 3; 50.91 + luaD_call(L, L->top - 3, 1); 50.92 + res = restorestack(L, result); 50.93 + L->top--; 50.94 + setobjs2s(L, res, L->top); 50.95 +} 50.96 + 50.97 + 50.98 + 50.99 +static void callTM (lua_State *L, const TValue *f, const TValue *p1, 50.100 + const TValue *p2, const TValue *p3) { 50.101 + setobj2s(L, L->top, f); /* push function */ 50.102 + setobj2s(L, L->top+1, p1); /* 1st argument */ 50.103 + setobj2s(L, L->top+2, p2); /* 2nd argument */ 50.104 + setobj2s(L, L->top+3, p3); /* 3th argument */ 50.105 + luaD_checkstack(L, 4); 50.106 + L->top += 4; 50.107 + luaD_call(L, L->top - 4, 0); 50.108 +} 50.109 + 50.110 + 50.111 +void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { 50.112 + int loop; 50.113 + for (loop = 0; loop < MAXTAGLOOP; loop++) { 50.114 + const TValue *tm; 50.115 + if (ttistable(t)) { /* `t' is a table? */ 50.116 + Table *h = hvalue(t); 50.117 + const TValue *res = luaH_get(h, key); /* do a primitive get */ 50.118 + if (!ttisnil(res) || /* result is no nil? */ 50.119 + (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ 50.120 + setobj2s(L, val, res); 50.121 + return; 50.122 + } 50.123 + /* else will try the tag method */ 50.124 + } 50.125 + else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) 50.126 + luaG_typeerror(L, t, "index"); 50.127 + if (ttisfunction(tm)) { 50.128 + callTMres(L, val, tm, t, key); 50.129 + return; 50.130 + } 50.131 + t = tm; /* else repeat with `tm' */ 50.132 + } 50.133 + luaG_runerror(L, "loop in gettable"); 50.134 +} 50.135 + 50.136 + 50.137 +void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { 50.138 + int loop; 50.139 + for (loop = 0; loop < MAXTAGLOOP; loop++) { 50.140 + const TValue *tm; 50.141 + if (ttistable(t)) { /* `t' is a table? */ 50.142 + Table *h = hvalue(t); 50.143 + TValue *oldval = luaH_set(L, h, key); /* do a primitive set */ 50.144 + if (!ttisnil(oldval) || /* result is no nil? */ 50.145 + (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */ 50.146 + setobj2t(L, oldval, val); 50.147 + luaC_barriert(L, h, val); 50.148 + return; 50.149 + } 50.150 + /* else will try the tag method */ 50.151 + } 50.152 + else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) 50.153 + luaG_typeerror(L, t, "index"); 50.154 + if (ttisfunction(tm)) { 50.155 + callTM(L, tm, t, key, val); 50.156 + return; 50.157 + } 50.158 + t = tm; /* else repeat with `tm' */ 50.159 + } 50.160 + luaG_runerror(L, "loop in settable"); 50.161 +} 50.162 + 50.163 + 50.164 +static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2, 50.165 + StkId res, TMS event) { 50.166 + const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ 50.167 + if (ttisnil(tm)) 50.168 + tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 50.169 + if (ttisnil(tm)) return 0; 50.170 + callTMres(L, res, tm, p1, p2); 50.171 + return 1; 50.172 +} 50.173 + 50.174 + 50.175 +static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2, 50.176 + TMS event) { 50.177 + const TValue *tm1 = fasttm(L, mt1, event); 50.178 + const TValue *tm2; 50.179 + if (tm1 == NULL) return NULL; /* no metamethod */ 50.180 + if (mt1 == mt2) return tm1; /* same metatables => same metamethods */ 50.181 + tm2 = fasttm(L, mt2, event); 50.182 + if (tm2 == NULL) return NULL; /* no metamethod */ 50.183 + if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */ 50.184 + return tm1; 50.185 + return NULL; 50.186 +} 50.187 + 50.188 + 50.189 +static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2, 50.190 + TMS event) { 50.191 + const TValue *tm1 = luaT_gettmbyobj(L, p1, event); 50.192 + const TValue *tm2; 50.193 + if (ttisnil(tm1)) return -1; /* no metamethod? */ 50.194 + tm2 = luaT_gettmbyobj(L, p2, event); 50.195 + if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */ 50.196 + return -1; 50.197 + callTMres(L, L->top, tm1, p1, p2); 50.198 + return !l_isfalse(L->top); 50.199 +} 50.200 + 50.201 + 50.202 +static int l_strcmp (const TString *ls, const TString *rs) { 50.203 + const char *l = getstr(ls); 50.204 + size_t ll = ls->tsv.len; 50.205 + const char *r = getstr(rs); 50.206 + size_t lr = rs->tsv.len; 50.207 + for (;;) { 50.208 + int temp = strcoll(l, r); 50.209 + if (temp != 0) return temp; 50.210 + else { /* strings are equal up to a `\0' */ 50.211 + size_t len = strlen(l); /* index of first `\0' in both strings */ 50.212 + if (len == lr) /* r is finished? */ 50.213 + return (len == ll) ? 0 : 1; 50.214 + else if (len == ll) /* l is finished? */ 50.215 + return -1; /* l is smaller than r (because r is not finished) */ 50.216 + /* both strings longer than `len'; go on comparing (after the `\0') */ 50.217 + len++; 50.218 + l += len; ll -= len; r += len; lr -= len; 50.219 + } 50.220 + } 50.221 +} 50.222 + 50.223 + 50.224 +int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { 50.225 + int res; 50.226 + if (ttype(l) != ttype(r)) 50.227 + return luaG_ordererror(L, l, r); 50.228 + else if (ttisnumber(l)) 50.229 + return luai_numlt(nvalue(l), nvalue(r)); 50.230 + else if (ttisstring(l)) 50.231 + return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; 50.232 + else if ((res = call_orderTM(L, l, r, TM_LT)) != -1) 50.233 + return res; 50.234 + return luaG_ordererror(L, l, r); 50.235 +} 50.236 + 50.237 + 50.238 +static int lessequal (lua_State *L, const TValue *l, const TValue *r) { 50.239 + int res; 50.240 + if (ttype(l) != ttype(r)) 50.241 + return luaG_ordererror(L, l, r); 50.242 + else if (ttisnumber(l)) 50.243 + return luai_numle(nvalue(l), nvalue(r)); 50.244 + else if (ttisstring(l)) 50.245 + return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; 50.246 + else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */ 50.247 + return res; 50.248 + else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */ 50.249 + return !res; 50.250 + return luaG_ordererror(L, l, r); 50.251 +} 50.252 + 50.253 + 50.254 +int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) { 50.255 + const TValue *tm; 50.256 + lua_assert(ttype(t1) == ttype(t2)); 50.257 + switch (ttype(t1)) { 50.258 + case LUA_TNIL: return 1; 50.259 + case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2)); 50.260 + case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ 50.261 + case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); 50.262 + case LUA_TUSERDATA: { 50.263 + if (uvalue(t1) == uvalue(t2)) return 1; 50.264 + tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, 50.265 + TM_EQ); 50.266 + break; /* will try TM */ 50.267 + } 50.268 + case LUA_TTABLE: { 50.269 + if (hvalue(t1) == hvalue(t2)) return 1; 50.270 + tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ); 50.271 + break; /* will try TM */ 50.272 + } 50.273 + default: return gcvalue(t1) == gcvalue(t2); 50.274 + } 50.275 + if (tm == NULL) return 0; /* no TM? */ 50.276 + callTMres(L, L->top, tm, t1, t2); /* call TM */ 50.277 + return !l_isfalse(L->top); 50.278 +} 50.279 + 50.280 + 50.281 +void luaV_concat (lua_State *L, int total, int last) { 50.282 + do { 50.283 + StkId top = L->base + last + 1; 50.284 + int n = 2; /* number of elements handled in this pass (at least 2) */ 50.285 + if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { 50.286 + if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) 50.287 + luaG_concaterror(L, top-2, top-1); 50.288 + } else if (tsvalue(top-1)->len == 0) /* second op is empty? */ 50.289 + (void)tostring(L, top - 2); /* result is first op (as string) */ 50.290 + else { 50.291 + /* at least two string values; get as many as possible */ 50.292 + size_t tl = tsvalue(top-1)->len; 50.293 + char *buffer; 50.294 + int i; 50.295 + /* collect total length */ 50.296 + for (n = 1; n < total && tostring(L, top-n-1); n++) { 50.297 + size_t l = tsvalue(top-n-1)->len; 50.298 + if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow"); 50.299 + tl += l; 50.300 + } 50.301 + buffer = luaZ_openspace(L, &G(L)->buff, tl); 50.302 + tl = 0; 50.303 + for (i=n; i>0; i--) { /* concat all strings */ 50.304 + size_t l = tsvalue(top-i)->len; 50.305 + memcpy(buffer+tl, svalue(top-i), l); 50.306 + tl += l; 50.307 + } 50.308 + setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); 50.309 + } 50.310 + total -= n-1; /* got `n' strings to create 1 new */ 50.311 + last -= n-1; 50.312 + } while (total > 1); /* repeat until only 1 result left */ 50.313 +} 50.314 + 50.315 + 50.316 +static void Arith (lua_State *L, StkId ra, const TValue *rb, 50.317 + const TValue *rc, TMS op) { 50.318 + TValue tempb, tempc; 50.319 + const TValue *b, *c; 50.320 + if ((b = luaV_tonumber(rb, &tempb)) != NULL && 50.321 + (c = luaV_tonumber(rc, &tempc)) != NULL) { 50.322 + lua_Number nb = nvalue(b), nc = nvalue(c); 50.323 + switch (op) { 50.324 + case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break; 50.325 + case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break; 50.326 + case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break; 50.327 + case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break; 50.328 + case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break; 50.329 + case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break; 50.330 + case TM_UNM: setnvalue(ra, luai_numunm(nb)); break; 50.331 + default: lua_assert(0); break; 50.332 + } 50.333 + } 50.334 + else if (!call_binTM(L, rb, rc, ra, op)) 50.335 + luaG_aritherror(L, rb, rc); 50.336 +} 50.337 + 50.338 + 50.339 + 50.340 +/* 50.341 +** some macros for common tasks in `luaV_execute' 50.342 +*/ 50.343 + 50.344 +#define runtime_check(L, c) { if (!(c)) break; } 50.345 + 50.346 +#define RA(i) (base+GETARG_A(i)) 50.347 +/* to be used after possible stack reallocation */ 50.348 +#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) 50.349 +#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) 50.350 +#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ 50.351 + ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) 50.352 +#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ 50.353 + ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) 50.354 +#define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i)) 50.355 + 50.356 + 50.357 +#define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);} 50.358 + 50.359 + 50.360 +#define Protect(x) { L->savedpc = pc; {x;}; base = L->base; } 50.361 + 50.362 + 50.363 +#define arith_op(op,tm) { \ 50.364 + TValue *rb = RKB(i); \ 50.365 + TValue *rc = RKC(i); \ 50.366 + if (ttisnumber(rb) && ttisnumber(rc)) { \ 50.367 + lua_Number nb = nvalue(rb), nc = nvalue(rc); \ 50.368 + setnvalue(ra, op(nb, nc)); \ 50.369 + } \ 50.370 + else \ 50.371 + Protect(Arith(L, ra, rb, rc, tm)); \ 50.372 + } 50.373 + 50.374 + 50.375 + 50.376 +void luaV_execute (lua_State *L, int nexeccalls) { 50.377 + LClosure *cl; 50.378 + StkId base; 50.379 + TValue *k; 50.380 + const Instruction *pc; 50.381 + reentry: /* entry point */ 50.382 + lua_assert(isLua(L->ci)); 50.383 + pc = L->savedpc; 50.384 + cl = &clvalue(L->ci->func)->l; 50.385 + base = L->base; 50.386 + k = cl->p->k; 50.387 + /* main loop of interpreter */ 50.388 + for (;;) { 50.389 + const Instruction i = *pc++; 50.390 + StkId ra; 50.391 + if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && 50.392 + (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { 50.393 + traceexec(L, pc); 50.394 + if (L->status == LUA_YIELD) { /* did hook yield? */ 50.395 + L->savedpc = pc - 1; 50.396 + return; 50.397 + } 50.398 + base = L->base; 50.399 + } 50.400 + /* warning!! several calls may realloc the stack and invalidate `ra' */ 50.401 + ra = RA(i); 50.402 + lua_assert(base == L->base && L->base == L->ci->base); 50.403 + lua_assert(base <= L->top && L->top <= L->stack + L->stacksize); 50.404 + lua_assert(L->top == L->ci->top || luaG_checkopenop(i)); 50.405 + switch (GET_OPCODE(i)) { 50.406 + case OP_MOVE: { 50.407 + setobjs2s(L, ra, RB(i)); 50.408 + continue; 50.409 + } 50.410 + case OP_LOADK: { 50.411 + setobj2s(L, ra, KBx(i)); 50.412 + continue; 50.413 + } 50.414 + case OP_LOADBOOL: { 50.415 + setbvalue(ra, GETARG_B(i)); 50.416 + if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ 50.417 + continue; 50.418 + } 50.419 + case OP_LOADNIL: { 50.420 + TValue *rb = RB(i); 50.421 + do { 50.422 + setnilvalue(rb--); 50.423 + } while (rb >= ra); 50.424 + continue; 50.425 + } 50.426 + case OP_GETUPVAL: { 50.427 + int b = GETARG_B(i); 50.428 + setobj2s(L, ra, cl->upvals[b]->v); 50.429 + continue; 50.430 + } 50.431 + case OP_GETGLOBAL: { 50.432 + TValue g; 50.433 + TValue *rb = KBx(i); 50.434 + sethvalue(L, &g, cl->env); 50.435 + lua_assert(ttisstring(rb)); 50.436 + Protect(luaV_gettable(L, &g, rb, ra)); 50.437 + continue; 50.438 + } 50.439 + case OP_GETTABLE: { 50.440 + Protect(luaV_gettable(L, RB(i), RKC(i), ra)); 50.441 + continue; 50.442 + } 50.443 + case OP_SETGLOBAL: { 50.444 + TValue g; 50.445 + sethvalue(L, &g, cl->env); 50.446 + lua_assert(ttisstring(KBx(i))); 50.447 + Protect(luaV_settable(L, &g, KBx(i), ra)); 50.448 + continue; 50.449 + } 50.450 + case OP_SETUPVAL: { 50.451 + UpVal *uv = cl->upvals[GETARG_B(i)]; 50.452 + setobj(L, uv->v, ra); 50.453 + luaC_barrier(L, uv, ra); 50.454 + continue; 50.455 + } 50.456 + case OP_SETTABLE: { 50.457 + Protect(luaV_settable(L, ra, RKB(i), RKC(i))); 50.458 + continue; 50.459 + } 50.460 + case OP_NEWTABLE: { 50.461 + int b = GETARG_B(i); 50.462 + int c = GETARG_C(i); 50.463 + sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); 50.464 + Protect(luaC_checkGC(L)); 50.465 + continue; 50.466 + } 50.467 + case OP_SELF: { 50.468 + StkId rb = RB(i); 50.469 + setobjs2s(L, ra+1, rb); 50.470 + Protect(luaV_gettable(L, rb, RKC(i), ra)); 50.471 + continue; 50.472 + } 50.473 + case OP_ADD: { 50.474 + arith_op(luai_numadd, TM_ADD); 50.475 + continue; 50.476 + } 50.477 + case OP_SUB: { 50.478 + arith_op(luai_numsub, TM_SUB); 50.479 + continue; 50.480 + } 50.481 + case OP_MUL: { 50.482 + arith_op(luai_nummul, TM_MUL); 50.483 + continue; 50.484 + } 50.485 + case OP_DIV: { 50.486 + arith_op(luai_numdiv, TM_DIV); 50.487 + continue; 50.488 + } 50.489 + case OP_MOD: { 50.490 + arith_op(luai_nummod, TM_MOD); 50.491 + continue; 50.492 + } 50.493 + case OP_POW: { 50.494 + arith_op(luai_numpow, TM_POW); 50.495 + continue; 50.496 + } 50.497 + case OP_UNM: { 50.498 + TValue *rb = RB(i); 50.499 + if (ttisnumber(rb)) { 50.500 + lua_Number nb = nvalue(rb); 50.501 + setnvalue(ra, luai_numunm(nb)); 50.502 + } 50.503 + else { 50.504 + Protect(Arith(L, ra, rb, rb, TM_UNM)); 50.505 + } 50.506 + continue; 50.507 + } 50.508 + case OP_NOT: { 50.509 + int res = l_isfalse(RB(i)); /* next assignment may change this value */ 50.510 + setbvalue(ra, res); 50.511 + continue; 50.512 + } 50.513 + case OP_LEN: { 50.514 + const TValue *rb = RB(i); 50.515 + switch (ttype(rb)) { 50.516 + case LUA_TTABLE: { 50.517 + setnvalue(ra, cast_num(luaH_getn(hvalue(rb)))); 50.518 + break; 50.519 + } 50.520 + case LUA_TSTRING: { 50.521 + setnvalue(ra, cast_num(tsvalue(rb)->len)); 50.522 + break; 50.523 + } 50.524 + default: { /* try metamethod */ 50.525 + Protect( 50.526 + if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN)) 50.527 + luaG_typeerror(L, rb, "get length of"); 50.528 + ) 50.529 + } 50.530 + } 50.531 + continue; 50.532 + } 50.533 + case OP_CONCAT: { 50.534 + int b = GETARG_B(i); 50.535 + int c = GETARG_C(i); 50.536 + Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L)); 50.537 + setobjs2s(L, RA(i), base+b); 50.538 + continue; 50.539 + } 50.540 + case OP_JMP: { 50.541 + dojump(L, pc, GETARG_sBx(i)); 50.542 + continue; 50.543 + } 50.544 + case OP_EQ: { 50.545 + TValue *rb = RKB(i); 50.546 + TValue *rc = RKC(i); 50.547 + Protect( 50.548 + if (equalobj(L, rb, rc) == GETARG_A(i)) 50.549 + dojump(L, pc, GETARG_sBx(*pc)); 50.550 + ) 50.551 + pc++; 50.552 + continue; 50.553 + } 50.554 + case OP_LT: { 50.555 + Protect( 50.556 + if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i)) 50.557 + dojump(L, pc, GETARG_sBx(*pc)); 50.558 + ) 50.559 + pc++; 50.560 + continue; 50.561 + } 50.562 + case OP_LE: { 50.563 + Protect( 50.564 + if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i)) 50.565 + dojump(L, pc, GETARG_sBx(*pc)); 50.566 + ) 50.567 + pc++; 50.568 + continue; 50.569 + } 50.570 + case OP_TEST: { 50.571 + if (l_isfalse(ra) != GETARG_C(i)) 50.572 + dojump(L, pc, GETARG_sBx(*pc)); 50.573 + pc++; 50.574 + continue; 50.575 + } 50.576 + case OP_TESTSET: { 50.577 + TValue *rb = RB(i); 50.578 + if (l_isfalse(rb) != GETARG_C(i)) { 50.579 + setobjs2s(L, ra, rb); 50.580 + dojump(L, pc, GETARG_sBx(*pc)); 50.581 + } 50.582 + pc++; 50.583 + continue; 50.584 + } 50.585 + case OP_CALL: { 50.586 + int b = GETARG_B(i); 50.587 + int nresults = GETARG_C(i) - 1; 50.588 + if (b != 0) L->top = ra+b; /* else previous instruction set top */ 50.589 + L->savedpc = pc; 50.590 + switch (luaD_precall(L, ra, nresults)) { 50.591 + case PCRLUA: { 50.592 + nexeccalls++; 50.593 + goto reentry; /* restart luaV_execute over new Lua function */ 50.594 + } 50.595 + case PCRC: { 50.596 + /* it was a C function (`precall' called it); adjust results */ 50.597 + if (nresults >= 0) L->top = L->ci->top; 50.598 + base = L->base; 50.599 + continue; 50.600 + } 50.601 + default: { 50.602 + return; /* yield */ 50.603 + } 50.604 + } 50.605 + } 50.606 + case OP_TAILCALL: { 50.607 + int b = GETARG_B(i); 50.608 + if (b != 0) L->top = ra+b; /* else previous instruction set top */ 50.609 + L->savedpc = pc; 50.610 + lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); 50.611 + switch (luaD_precall(L, ra, LUA_MULTRET)) { 50.612 + case PCRLUA: { 50.613 + /* tail call: put new frame in place of previous one */ 50.614 + CallInfo *ci = L->ci - 1; /* previous frame */ 50.615 + int aux; 50.616 + StkId func = ci->func; 50.617 + StkId pfunc = (ci+1)->func; /* previous function index */ 50.618 + if (L->openupval) luaF_close(L, ci->base); 50.619 + L->base = ci->base = ci->func + ((ci+1)->base - pfunc); 50.620 + for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */ 50.621 + setobjs2s(L, func+aux, pfunc+aux); 50.622 + ci->top = L->top = func+aux; /* correct top */ 50.623 + lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize); 50.624 + ci->savedpc = L->savedpc; 50.625 + ci->tailcalls++; /* one more call lost */ 50.626 + L->ci--; /* remove new frame */ 50.627 + goto reentry; 50.628 + } 50.629 + case PCRC: { /* it was a C function (`precall' called it) */ 50.630 + base = L->base; 50.631 + continue; 50.632 + } 50.633 + default: { 50.634 + return; /* yield */ 50.635 + } 50.636 + } 50.637 + } 50.638 + case OP_RETURN: { 50.639 + int b = GETARG_B(i); 50.640 + if (b != 0) L->top = ra+b-1; 50.641 + if (L->openupval) luaF_close(L, base); 50.642 + L->savedpc = pc; 50.643 + b = luaD_poscall(L, ra); 50.644 + if (--nexeccalls == 0) /* was previous function running `here'? */ 50.645 + return; /* no: return */ 50.646 + else { /* yes: continue its execution */ 50.647 + if (b) L->top = L->ci->top; 50.648 + lua_assert(isLua(L->ci)); 50.649 + lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL); 50.650 + goto reentry; 50.651 + } 50.652 + } 50.653 + case OP_FORLOOP: { 50.654 + lua_Number step = nvalue(ra+2); 50.655 + lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */ 50.656 + lua_Number limit = nvalue(ra+1); 50.657 + if (luai_numlt(0, step) ? luai_numle(idx, limit) 50.658 + : luai_numle(limit, idx)) { 50.659 + dojump(L, pc, GETARG_sBx(i)); /* jump back */ 50.660 + setnvalue(ra, idx); /* update internal index... */ 50.661 + setnvalue(ra+3, idx); /* ...and external index */ 50.662 + } 50.663 + continue; 50.664 + } 50.665 + case OP_FORPREP: { 50.666 + const TValue *init = ra; 50.667 + const TValue *plimit = ra+1; 50.668 + const TValue *pstep = ra+2; 50.669 + L->savedpc = pc; /* next steps may throw errors */ 50.670 + if (!tonumber(init, ra)) 50.671 + luaG_runerror(L, LUA_QL("for") " initial value must be a number"); 50.672 + else if (!tonumber(plimit, ra+1)) 50.673 + luaG_runerror(L, LUA_QL("for") " limit must be a number"); 50.674 + else if (!tonumber(pstep, ra+2)) 50.675 + luaG_runerror(L, LUA_QL("for") " step must be a number"); 50.676 + setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep))); 50.677 + dojump(L, pc, GETARG_sBx(i)); 50.678 + continue; 50.679 + } 50.680 + case OP_TFORLOOP: { 50.681 + StkId cb = ra + 3; /* call base */ 50.682 + setobjs2s(L, cb+2, ra+2); 50.683 + setobjs2s(L, cb+1, ra+1); 50.684 + setobjs2s(L, cb, ra); 50.685 + L->top = cb+3; /* func. + 2 args (state and index) */ 50.686 + Protect(luaD_call(L, cb, GETARG_C(i))); 50.687 + L->top = L->ci->top; 50.688 + cb = RA(i) + 3; /* previous call may change the stack */ 50.689 + if (!ttisnil(cb)) { /* continue loop? */ 50.690 + setobjs2s(L, cb-1, cb); /* save control variable */ 50.691 + dojump(L, pc, GETARG_sBx(*pc)); /* jump back */ 50.692 + } 50.693 + pc++; 50.694 + continue; 50.695 + } 50.696 + case OP_SETLIST: { 50.697 + int n = GETARG_B(i); 50.698 + int c = GETARG_C(i); 50.699 + int last; 50.700 + Table *h; 50.701 + if (n == 0) { 50.702 + n = cast_int(L->top - ra) - 1; 50.703 + L->top = L->ci->top; 50.704 + } 50.705 + if (c == 0) c = cast_int(*pc++); 50.706 + runtime_check(L, ttistable(ra)); 50.707 + h = hvalue(ra); 50.708 + last = ((c-1)*LFIELDS_PER_FLUSH) + n; 50.709 + if (last > h->sizearray) /* needs more space? */ 50.710 + luaH_resizearray(L, h, last); /* pre-alloc it at once */ 50.711 + for (; n > 0; n--) { 50.712 + TValue *val = ra+n; 50.713 + setobj2t(L, luaH_setnum(L, h, last--), val); 50.714 + luaC_barriert(L, h, val); 50.715 + } 50.716 + continue; 50.717 + } 50.718 + case OP_CLOSE: { 50.719 + luaF_close(L, ra); 50.720 + continue; 50.721 + } 50.722 + case OP_CLOSURE: { 50.723 + Proto *p; 50.724 + Closure *ncl; 50.725 + int nup, j; 50.726 + p = cl->p->p[GETARG_Bx(i)]; 50.727 + nup = p->nups; 50.728 + ncl = luaF_newLclosure(L, nup, cl->env); 50.729 + ncl->l.p = p; 50.730 + for (j=0; j<nup; j++, pc++) { 50.731 + if (GET_OPCODE(*pc) == OP_GETUPVAL) 50.732 + ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)]; 50.733 + else { 50.734 + lua_assert(GET_OPCODE(*pc) == OP_MOVE); 50.735 + ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc)); 50.736 + } 50.737 + } 50.738 + setclvalue(L, ra, ncl); 50.739 + Protect(luaC_checkGC(L)); 50.740 + continue; 50.741 + } 50.742 + case OP_VARARG: { 50.743 + int b = GETARG_B(i) - 1; 50.744 + int j; 50.745 + CallInfo *ci = L->ci; 50.746 + int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1; 50.747 + if (b == LUA_MULTRET) { 50.748 + Protect(luaD_checkstack(L, n)); 50.749 + ra = RA(i); /* previous call may change the stack */ 50.750 + b = n; 50.751 + L->top = ra + n; 50.752 + } 50.753 + for (j = 0; j < b; j++) { 50.754 + if (j < n) { 50.755 + setobjs2s(L, ra + j, ci->base - n + j); 50.756 + } 50.757 + else { 50.758 + setnilvalue(ra + j); 50.759 + } 50.760 + } 50.761 + continue; 50.762 + } 50.763 + } 50.764 + } 50.765 +} 50.766 +
51.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 51.2 +++ b/src/lua/lvm.h Sat Mar 03 11:07:39 2012 -0600 51.3 @@ -0,0 +1,36 @@ 51.4 +/* 51.5 +** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $ 51.6 +** Lua virtual machine 51.7 +** See Copyright Notice in lua.h 51.8 +*/ 51.9 + 51.10 +#ifndef lvm_h 51.11 +#define lvm_h 51.12 + 51.13 + 51.14 +#include "ldo.h" 51.15 +#include "lobject.h" 51.16 +#include "ltm.h" 51.17 + 51.18 + 51.19 +#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) 51.20 + 51.21 +#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \ 51.22 + (((o) = luaV_tonumber(o,n)) != NULL)) 51.23 + 51.24 +#define equalobj(L,o1,o2) \ 51.25 + (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) 51.26 + 51.27 + 51.28 +LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 51.29 +LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); 51.30 +LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 51.31 +LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 51.32 +LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 51.33 + StkId val); 51.34 +LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 51.35 + StkId val); 51.36 +LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls); 51.37 +LUAI_FUNC void luaV_concat (lua_State *L, int total, int last); 51.38 + 51.39 +#endif
52.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 52.2 +++ b/src/lua/lzio.c Sat Mar 03 11:07:39 2012 -0600 52.3 @@ -0,0 +1,82 @@ 52.4 +/* 52.5 +** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 52.6 +** a generic input stream interface 52.7 +** See Copyright Notice in lua.h 52.8 +*/ 52.9 + 52.10 + 52.11 +#include <string.h> 52.12 + 52.13 +#define lzio_c 52.14 +#define LUA_CORE 52.15 + 52.16 +#include "lua.h" 52.17 + 52.18 +#include "llimits.h" 52.19 +#include "lmem.h" 52.20 +#include "lstate.h" 52.21 +#include "lzio.h" 52.22 + 52.23 + 52.24 +int luaZ_fill (ZIO *z) { 52.25 + size_t size; 52.26 + lua_State *L = z->L; 52.27 + const char *buff; 52.28 + lua_unlock(L); 52.29 + buff = z->reader(L, z->data, &size); 52.30 + lua_lock(L); 52.31 + if (buff == NULL || size == 0) return EOZ; 52.32 + z->n = size - 1; 52.33 + z->p = buff; 52.34 + return char2int(*(z->p++)); 52.35 +} 52.36 + 52.37 + 52.38 +int luaZ_lookahead (ZIO *z) { 52.39 + if (z->n == 0) { 52.40 + if (luaZ_fill(z) == EOZ) 52.41 + return EOZ; 52.42 + else { 52.43 + z->n++; /* luaZ_fill removed first byte; put back it */ 52.44 + z->p--; 52.45 + } 52.46 + } 52.47 + return char2int(*z->p); 52.48 +} 52.49 + 52.50 + 52.51 +void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 52.52 + z->L = L; 52.53 + z->reader = reader; 52.54 + z->data = data; 52.55 + z->n = 0; 52.56 + z->p = NULL; 52.57 +} 52.58 + 52.59 + 52.60 +/* --------------------------------------------------------------- read --- */ 52.61 +size_t luaZ_read (ZIO *z, void *b, size_t n) { 52.62 + while (n) { 52.63 + size_t m; 52.64 + if (luaZ_lookahead(z) == EOZ) 52.65 + return n; /* return number of missing bytes */ 52.66 + m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 52.67 + memcpy(b, z->p, m); 52.68 + z->n -= m; 52.69 + z->p += m; 52.70 + b = (char *)b + m; 52.71 + n -= m; 52.72 + } 52.73 + return 0; 52.74 +} 52.75 + 52.76 +/* ------------------------------------------------------------------------ */ 52.77 +char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 52.78 + if (n > buff->buffsize) { 52.79 + if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 52.80 + luaZ_resizebuffer(L, buff, n); 52.81 + } 52.82 + return buff->buffer; 52.83 +} 52.84 + 52.85 +
53.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 53.2 +++ b/src/lua/lzio.h Sat Mar 03 11:07:39 2012 -0600 53.3 @@ -0,0 +1,67 @@ 53.4 +/* 53.5 +** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $ 53.6 +** Buffered streams 53.7 +** See Copyright Notice in lua.h 53.8 +*/ 53.9 + 53.10 + 53.11 +#ifndef lzio_h 53.12 +#define lzio_h 53.13 + 53.14 +#include "lua.h" 53.15 + 53.16 +#include "lmem.h" 53.17 + 53.18 + 53.19 +#define EOZ (-1) /* end of stream */ 53.20 + 53.21 +typedef struct Zio ZIO; 53.22 + 53.23 +#define char2int(c) cast(int, cast(unsigned char, (c))) 53.24 + 53.25 +#define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) 53.26 + 53.27 +typedef struct Mbuffer { 53.28 + char *buffer; 53.29 + size_t n; 53.30 + size_t buffsize; 53.31 +} Mbuffer; 53.32 + 53.33 +#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 53.34 + 53.35 +#define luaZ_buffer(buff) ((buff)->buffer) 53.36 +#define luaZ_sizebuffer(buff) ((buff)->buffsize) 53.37 +#define luaZ_bufflen(buff) ((buff)->n) 53.38 + 53.39 +#define luaZ_resetbuffer(buff) ((buff)->n = 0) 53.40 + 53.41 + 53.42 +#define luaZ_resizebuffer(L, buff, size) \ 53.43 + (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 53.44 + (buff)->buffsize = size) 53.45 + 53.46 +#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 53.47 + 53.48 + 53.49 +LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 53.50 +LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 53.51 + void *data); 53.52 +LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 53.53 +LUAI_FUNC int luaZ_lookahead (ZIO *z); 53.54 + 53.55 + 53.56 + 53.57 +/* --------- Private Part ------------------ */ 53.58 + 53.59 +struct Zio { 53.60 + size_t n; /* bytes still unread */ 53.61 + const char *p; /* current position in buffer */ 53.62 + lua_Reader reader; 53.63 + void* data; /* additional data */ 53.64 + lua_State *L; /* Lua state (for reader) */ 53.65 +}; 53.66 + 53.67 + 53.68 +LUAI_FUNC int luaZ_fill (ZIO *z); 53.69 + 53.70 +#endif
54.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 54.2 +++ b/src/lua/print.c Sat Mar 03 11:07:39 2012 -0600 54.3 @@ -0,0 +1,227 @@ 54.4 +/* 54.5 +** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $ 54.6 +** print bytecodes 54.7 +** See Copyright Notice in lua.h 54.8 +*/ 54.9 + 54.10 +#include <ctype.h> 54.11 +#include <stdio.h> 54.12 + 54.13 +#define luac_c 54.14 +#define LUA_CORE 54.15 + 54.16 +#include "ldebug.h" 54.17 +#include "lobject.h" 54.18 +#include "lopcodes.h" 54.19 +#include "lundump.h" 54.20 + 54.21 +#define PrintFunction luaU_print 54.22 + 54.23 +#define Sizeof(x) ((int)sizeof(x)) 54.24 +#define VOID(p) ((const void*)(p)) 54.25 + 54.26 +static void PrintString(const TString* ts) 54.27 +{ 54.28 + const char* s=getstr(ts); 54.29 + size_t i,n=ts->tsv.len; 54.30 + putchar('"'); 54.31 + for (i=0; i<n; i++) 54.32 + { 54.33 + int c=s[i]; 54.34 + switch (c) 54.35 + { 54.36 + case '"': printf("\\\""); break; 54.37 + case '\\': printf("\\\\"); break; 54.38 + case '\a': printf("\\a"); break; 54.39 + case '\b': printf("\\b"); break; 54.40 + case '\f': printf("\\f"); break; 54.41 + case '\n': printf("\\n"); break; 54.42 + case '\r': printf("\\r"); break; 54.43 + case '\t': printf("\\t"); break; 54.44 + case '\v': printf("\\v"); break; 54.45 + default: if (isprint((unsigned char)c)) 54.46 + putchar(c); 54.47 + else 54.48 + printf("\\%03u",(unsigned char)c); 54.49 + } 54.50 + } 54.51 + putchar('"'); 54.52 +} 54.53 + 54.54 +static void PrintConstant(const Proto* f, int i) 54.55 +{ 54.56 + const TValue* o=&f->k[i]; 54.57 + switch (ttype(o)) 54.58 + { 54.59 + case LUA_TNIL: 54.60 + printf("nil"); 54.61 + break; 54.62 + case LUA_TBOOLEAN: 54.63 + printf(bvalue(o) ? "true" : "false"); 54.64 + break; 54.65 + case LUA_TNUMBER: 54.66 + printf(LUA_NUMBER_FMT,nvalue(o)); 54.67 + break; 54.68 + case LUA_TSTRING: 54.69 + PrintString(rawtsvalue(o)); 54.70 + break; 54.71 + default: /* cannot happen */ 54.72 + printf("? type=%d",ttype(o)); 54.73 + break; 54.74 + } 54.75 +} 54.76 + 54.77 +static void PrintCode(const Proto* f) 54.78 +{ 54.79 + const Instruction* code=f->code; 54.80 + int pc,n=f->sizecode; 54.81 + for (pc=0; pc<n; pc++) 54.82 + { 54.83 + Instruction i=code[pc]; 54.84 + OpCode o=GET_OPCODE(i); 54.85 + int a=GETARG_A(i); 54.86 + int b=GETARG_B(i); 54.87 + int c=GETARG_C(i); 54.88 + int bx=GETARG_Bx(i); 54.89 + int sbx=GETARG_sBx(i); 54.90 + int line=getline(f,pc); 54.91 + printf("\t%d\t",pc+1); 54.92 + if (line>0) printf("[%d]\t",line); else printf("[-]\t"); 54.93 + printf("%-9s\t",luaP_opnames[o]); 54.94 + switch (getOpMode(o)) 54.95 + { 54.96 + case iABC: 54.97 + printf("%d",a); 54.98 + if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b); 54.99 + if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c); 54.100 + break; 54.101 + case iABx: 54.102 + if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx); 54.103 + break; 54.104 + case iAsBx: 54.105 + if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx); 54.106 + break; 54.107 + } 54.108 + switch (o) 54.109 + { 54.110 + case OP_LOADK: 54.111 + printf("\t; "); PrintConstant(f,bx); 54.112 + break; 54.113 + case OP_GETUPVAL: 54.114 + case OP_SETUPVAL: 54.115 + printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-"); 54.116 + break; 54.117 + case OP_GETGLOBAL: 54.118 + case OP_SETGLOBAL: 54.119 + printf("\t; %s",svalue(&f->k[bx])); 54.120 + break; 54.121 + case OP_GETTABLE: 54.122 + case OP_SELF: 54.123 + if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } 54.124 + break; 54.125 + case OP_SETTABLE: 54.126 + case OP_ADD: 54.127 + case OP_SUB: 54.128 + case OP_MUL: 54.129 + case OP_DIV: 54.130 + case OP_POW: 54.131 + case OP_EQ: 54.132 + case OP_LT: 54.133 + case OP_LE: 54.134 + if (ISK(b) || ISK(c)) 54.135 + { 54.136 + printf("\t; "); 54.137 + if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); 54.138 + printf(" "); 54.139 + if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); 54.140 + } 54.141 + break; 54.142 + case OP_JMP: 54.143 + case OP_FORLOOP: 54.144 + case OP_FORPREP: 54.145 + printf("\t; to %d",sbx+pc+2); 54.146 + break; 54.147 + case OP_CLOSURE: 54.148 + printf("\t; %p",VOID(f->p[bx])); 54.149 + break; 54.150 + case OP_SETLIST: 54.151 + if (c==0) printf("\t; %d",(int)code[++pc]); 54.152 + else printf("\t; %d",c); 54.153 + break; 54.154 + default: 54.155 + break; 54.156 + } 54.157 + printf("\n"); 54.158 + } 54.159 +} 54.160 + 54.161 +#define SS(x) (x==1)?"":"s" 54.162 +#define S(x) x,SS(x) 54.163 + 54.164 +static void PrintHeader(const Proto* f) 54.165 +{ 54.166 + const char* s=getstr(f->source); 54.167 + if (*s=='@' || *s=='=') 54.168 + s++; 54.169 + else if (*s==LUA_SIGNATURE[0]) 54.170 + s="(bstring)"; 54.171 + else 54.172 + s="(string)"; 54.173 + printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n", 54.174 + (f->linedefined==0)?"main":"function",s, 54.175 + f->linedefined,f->lastlinedefined, 54.176 + S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f)); 54.177 + printf("%d%s param%s, %d slot%s, %d upvalue%s, ", 54.178 + f->numparams,f->is_vararg?"+":"",SS(f->numparams), 54.179 + S(f->maxstacksize),S(f->nups)); 54.180 + printf("%d local%s, %d constant%s, %d function%s\n", 54.181 + S(f->sizelocvars),S(f->sizek),S(f->sizep)); 54.182 +} 54.183 + 54.184 +static void PrintConstants(const Proto* f) 54.185 +{ 54.186 + int i,n=f->sizek; 54.187 + printf("constants (%d) for %p:\n",n,VOID(f)); 54.188 + for (i=0; i<n; i++) 54.189 + { 54.190 + printf("\t%d\t",i+1); 54.191 + PrintConstant(f,i); 54.192 + printf("\n"); 54.193 + } 54.194 +} 54.195 + 54.196 +static void PrintLocals(const Proto* f) 54.197 +{ 54.198 + int i,n=f->sizelocvars; 54.199 + printf("locals (%d) for %p:\n",n,VOID(f)); 54.200 + for (i=0; i<n; i++) 54.201 + { 54.202 + printf("\t%d\t%s\t%d\t%d\n", 54.203 + i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); 54.204 + } 54.205 +} 54.206 + 54.207 +static void PrintUpvalues(const Proto* f) 54.208 +{ 54.209 + int i,n=f->sizeupvalues; 54.210 + printf("upvalues (%d) for %p:\n",n,VOID(f)); 54.211 + if (f->upvalues==NULL) return; 54.212 + for (i=0; i<n; i++) 54.213 + { 54.214 + printf("\t%d\t%s\n",i,getstr(f->upvalues[i])); 54.215 + } 54.216 +} 54.217 + 54.218 +void PrintFunction(const Proto* f, int full) 54.219 +{ 54.220 + int i,n=f->sizep; 54.221 + PrintHeader(f); 54.222 + PrintCode(f); 54.223 + if (full) 54.224 + { 54.225 + PrintConstants(f); 54.226 + PrintLocals(f); 54.227 + PrintUpvalues(f); 54.228 + } 54.229 + for (i=0; i<n; i++) PrintFunction(f->p[i],full); 54.230 +}
55.1 --- a/src/lua/src/lapi.c Sat Mar 03 11:04:56 2012 -0600 55.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 55.3 @@ -1,1087 +0,0 @@ 55.4 -/* 55.5 -** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $ 55.6 -** Lua API 55.7 -** See Copyright Notice in lua.h 55.8 -*/ 55.9 - 55.10 - 55.11 -#include <assert.h> 55.12 -#include <math.h> 55.13 -#include <stdarg.h> 55.14 -#include <string.h> 55.15 - 55.16 -#define lapi_c 55.17 -#define LUA_CORE 55.18 - 55.19 -#include "lua.h" 55.20 - 55.21 -#include "lapi.h" 55.22 -#include "ldebug.h" 55.23 -#include "ldo.h" 55.24 -#include "lfunc.h" 55.25 -#include "lgc.h" 55.26 -#include "lmem.h" 55.27 -#include "lobject.h" 55.28 -#include "lstate.h" 55.29 -#include "lstring.h" 55.30 -#include "ltable.h" 55.31 -#include "ltm.h" 55.32 -#include "lundump.h" 55.33 -#include "lvm.h" 55.34 - 55.35 - 55.36 - 55.37 -const char lua_ident[] = 55.38 - "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n" 55.39 - "$Authors: " LUA_AUTHORS " $\n" 55.40 - "$URL: www.lua.org $\n"; 55.41 - 55.42 - 55.43 - 55.44 -#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base)) 55.45 - 55.46 -#define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject) 55.47 - 55.48 -#define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;} 55.49 - 55.50 - 55.51 - 55.52 -static TValue *index2adr (lua_State *L, int idx) { 55.53 - if (idx > 0) { 55.54 - TValue *o = L->base + (idx - 1); 55.55 - api_check(L, idx <= L->ci->top - L->base); 55.56 - if (o >= L->top) return cast(TValue *, luaO_nilobject); 55.57 - else return o; 55.58 - } 55.59 - else if (idx > LUA_REGISTRYINDEX) { 55.60 - api_check(L, idx != 0 && -idx <= L->top - L->base); 55.61 - return L->top + idx; 55.62 - } 55.63 - else switch (idx) { /* pseudo-indices */ 55.64 - case LUA_REGISTRYINDEX: return registry(L); 55.65 - case LUA_ENVIRONINDEX: { 55.66 - Closure *func = curr_func(L); 55.67 - sethvalue(L, &L->env, func->c.env); 55.68 - return &L->env; 55.69 - } 55.70 - case LUA_GLOBALSINDEX: return gt(L); 55.71 - default: { 55.72 - Closure *func = curr_func(L); 55.73 - idx = LUA_GLOBALSINDEX - idx; 55.74 - return (idx <= func->c.nupvalues) 55.75 - ? &func->c.upvalue[idx-1] 55.76 - : cast(TValue *, luaO_nilobject); 55.77 - } 55.78 - } 55.79 -} 55.80 - 55.81 - 55.82 -static Table *getcurrenv (lua_State *L) { 55.83 - if (L->ci == L->base_ci) /* no enclosing function? */ 55.84 - return hvalue(gt(L)); /* use global table as environment */ 55.85 - else { 55.86 - Closure *func = curr_func(L); 55.87 - return func->c.env; 55.88 - } 55.89 -} 55.90 - 55.91 - 55.92 -void luaA_pushobject (lua_State *L, const TValue *o) { 55.93 - setobj2s(L, L->top, o); 55.94 - api_incr_top(L); 55.95 -} 55.96 - 55.97 - 55.98 -LUA_API int lua_checkstack (lua_State *L, int size) { 55.99 - int res = 1; 55.100 - lua_lock(L); 55.101 - if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) 55.102 - res = 0; /* stack overflow */ 55.103 - else if (size > 0) { 55.104 - luaD_checkstack(L, size); 55.105 - if (L->ci->top < L->top + size) 55.106 - L->ci->top = L->top + size; 55.107 - } 55.108 - lua_unlock(L); 55.109 - return res; 55.110 -} 55.111 - 55.112 - 55.113 -LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { 55.114 - int i; 55.115 - if (from == to) return; 55.116 - lua_lock(to); 55.117 - api_checknelems(from, n); 55.118 - api_check(from, G(from) == G(to)); 55.119 - api_check(from, to->ci->top - to->top >= n); 55.120 - from->top -= n; 55.121 - for (i = 0; i < n; i++) { 55.122 - setobj2s(to, to->top++, from->top + i); 55.123 - } 55.124 - lua_unlock(to); 55.125 -} 55.126 - 55.127 - 55.128 -LUA_API void lua_setlevel (lua_State *from, lua_State *to) { 55.129 - to->nCcalls = from->nCcalls; 55.130 -} 55.131 - 55.132 - 55.133 -LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { 55.134 - lua_CFunction old; 55.135 - lua_lock(L); 55.136 - old = G(L)->panic; 55.137 - G(L)->panic = panicf; 55.138 - lua_unlock(L); 55.139 - return old; 55.140 -} 55.141 - 55.142 - 55.143 -LUA_API lua_State *lua_newthread (lua_State *L) { 55.144 - lua_State *L1; 55.145 - lua_lock(L); 55.146 - luaC_checkGC(L); 55.147 - L1 = luaE_newthread(L); 55.148 - setthvalue(L, L->top, L1); 55.149 - api_incr_top(L); 55.150 - lua_unlock(L); 55.151 - luai_userstatethread(L, L1); 55.152 - return L1; 55.153 -} 55.154 - 55.155 - 55.156 - 55.157 -/* 55.158 -** basic stack manipulation 55.159 -*/ 55.160 - 55.161 - 55.162 -LUA_API int lua_gettop (lua_State *L) { 55.163 - return cast_int(L->top - L->base); 55.164 -} 55.165 - 55.166 - 55.167 -LUA_API void lua_settop (lua_State *L, int idx) { 55.168 - lua_lock(L); 55.169 - if (idx >= 0) { 55.170 - api_check(L, idx <= L->stack_last - L->base); 55.171 - while (L->top < L->base + idx) 55.172 - setnilvalue(L->top++); 55.173 - L->top = L->base + idx; 55.174 - } 55.175 - else { 55.176 - api_check(L, -(idx+1) <= (L->top - L->base)); 55.177 - L->top += idx+1; /* `subtract' index (index is negative) */ 55.178 - } 55.179 - lua_unlock(L); 55.180 -} 55.181 - 55.182 - 55.183 -LUA_API void lua_remove (lua_State *L, int idx) { 55.184 - StkId p; 55.185 - lua_lock(L); 55.186 - p = index2adr(L, idx); 55.187 - api_checkvalidindex(L, p); 55.188 - while (++p < L->top) setobjs2s(L, p-1, p); 55.189 - L->top--; 55.190 - lua_unlock(L); 55.191 -} 55.192 - 55.193 - 55.194 -LUA_API void lua_insert (lua_State *L, int idx) { 55.195 - StkId p; 55.196 - StkId q; 55.197 - lua_lock(L); 55.198 - p = index2adr(L, idx); 55.199 - api_checkvalidindex(L, p); 55.200 - for (q = L->top; q>p; q--) setobjs2s(L, q, q-1); 55.201 - setobjs2s(L, p, L->top); 55.202 - lua_unlock(L); 55.203 -} 55.204 - 55.205 - 55.206 -LUA_API void lua_replace (lua_State *L, int idx) { 55.207 - StkId o; 55.208 - lua_lock(L); 55.209 - /* explicit test for incompatible code */ 55.210 - if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci) 55.211 - luaG_runerror(L, "no calling environment"); 55.212 - api_checknelems(L, 1); 55.213 - o = index2adr(L, idx); 55.214 - api_checkvalidindex(L, o); 55.215 - if (idx == LUA_ENVIRONINDEX) { 55.216 - Closure *func = curr_func(L); 55.217 - api_check(L, ttistable(L->top - 1)); 55.218 - func->c.env = hvalue(L->top - 1); 55.219 - luaC_barrier(L, func, L->top - 1); 55.220 - } 55.221 - else { 55.222 - setobj(L, o, L->top - 1); 55.223 - if (idx < LUA_GLOBALSINDEX) /* function upvalue? */ 55.224 - luaC_barrier(L, curr_func(L), L->top - 1); 55.225 - } 55.226 - L->top--; 55.227 - lua_unlock(L); 55.228 -} 55.229 - 55.230 - 55.231 -LUA_API void lua_pushvalue (lua_State *L, int idx) { 55.232 - lua_lock(L); 55.233 - setobj2s(L, L->top, index2adr(L, idx)); 55.234 - api_incr_top(L); 55.235 - lua_unlock(L); 55.236 -} 55.237 - 55.238 - 55.239 - 55.240 -/* 55.241 -** access functions (stack -> C) 55.242 -*/ 55.243 - 55.244 - 55.245 -LUA_API int lua_type (lua_State *L, int idx) { 55.246 - StkId o = index2adr(L, idx); 55.247 - return (o == luaO_nilobject) ? LUA_TNONE : ttype(o); 55.248 -} 55.249 - 55.250 - 55.251 -LUA_API const char *lua_typename (lua_State *L, int t) { 55.252 - UNUSED(L); 55.253 - return (t == LUA_TNONE) ? "no value" : luaT_typenames[t]; 55.254 -} 55.255 - 55.256 - 55.257 -LUA_API int lua_iscfunction (lua_State *L, int idx) { 55.258 - StkId o = index2adr(L, idx); 55.259 - return iscfunction(o); 55.260 -} 55.261 - 55.262 - 55.263 -LUA_API int lua_isnumber (lua_State *L, int idx) { 55.264 - TValue n; 55.265 - const TValue *o = index2adr(L, idx); 55.266 - return tonumber(o, &n); 55.267 -} 55.268 - 55.269 - 55.270 -LUA_API int lua_isstring (lua_State *L, int idx) { 55.271 - int t = lua_type(L, idx); 55.272 - return (t == LUA_TSTRING || t == LUA_TNUMBER); 55.273 -} 55.274 - 55.275 - 55.276 -LUA_API int lua_isuserdata (lua_State *L, int idx) { 55.277 - const TValue *o = index2adr(L, idx); 55.278 - return (ttisuserdata(o) || ttislightuserdata(o)); 55.279 -} 55.280 - 55.281 - 55.282 -LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { 55.283 - StkId o1 = index2adr(L, index1); 55.284 - StkId o2 = index2adr(L, index2); 55.285 - return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 55.286 - : luaO_rawequalObj(o1, o2); 55.287 -} 55.288 - 55.289 - 55.290 -LUA_API int lua_equal (lua_State *L, int index1, int index2) { 55.291 - StkId o1, o2; 55.292 - int i; 55.293 - lua_lock(L); /* may call tag method */ 55.294 - o1 = index2adr(L, index1); 55.295 - o2 = index2adr(L, index2); 55.296 - i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2); 55.297 - lua_unlock(L); 55.298 - return i; 55.299 -} 55.300 - 55.301 - 55.302 -LUA_API int lua_lessthan (lua_State *L, int index1, int index2) { 55.303 - StkId o1, o2; 55.304 - int i; 55.305 - lua_lock(L); /* may call tag method */ 55.306 - o1 = index2adr(L, index1); 55.307 - o2 = index2adr(L, index2); 55.308 - i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 55.309 - : luaV_lessthan(L, o1, o2); 55.310 - lua_unlock(L); 55.311 - return i; 55.312 -} 55.313 - 55.314 - 55.315 - 55.316 -LUA_API lua_Number lua_tonumber (lua_State *L, int idx) { 55.317 - TValue n; 55.318 - const TValue *o = index2adr(L, idx); 55.319 - if (tonumber(o, &n)) 55.320 - return nvalue(o); 55.321 - else 55.322 - return 0; 55.323 -} 55.324 - 55.325 - 55.326 -LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) { 55.327 - TValue n; 55.328 - const TValue *o = index2adr(L, idx); 55.329 - if (tonumber(o, &n)) { 55.330 - lua_Integer res; 55.331 - lua_Number num = nvalue(o); 55.332 - lua_number2integer(res, num); 55.333 - return res; 55.334 - } 55.335 - else 55.336 - return 0; 55.337 -} 55.338 - 55.339 - 55.340 -LUA_API int lua_toboolean (lua_State *L, int idx) { 55.341 - const TValue *o = index2adr(L, idx); 55.342 - return !l_isfalse(o); 55.343 -} 55.344 - 55.345 - 55.346 -LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { 55.347 - StkId o = index2adr(L, idx); 55.348 - if (!ttisstring(o)) { 55.349 - lua_lock(L); /* `luaV_tostring' may create a new string */ 55.350 - if (!luaV_tostring(L, o)) { /* conversion failed? */ 55.351 - if (len != NULL) *len = 0; 55.352 - lua_unlock(L); 55.353 - return NULL; 55.354 - } 55.355 - luaC_checkGC(L); 55.356 - o = index2adr(L, idx); /* previous call may reallocate the stack */ 55.357 - lua_unlock(L); 55.358 - } 55.359 - if (len != NULL) *len = tsvalue(o)->len; 55.360 - return svalue(o); 55.361 -} 55.362 - 55.363 - 55.364 -LUA_API size_t lua_objlen (lua_State *L, int idx) { 55.365 - StkId o = index2adr(L, idx); 55.366 - switch (ttype(o)) { 55.367 - case LUA_TSTRING: return tsvalue(o)->len; 55.368 - case LUA_TUSERDATA: return uvalue(o)->len; 55.369 - case LUA_TTABLE: return luaH_getn(hvalue(o)); 55.370 - case LUA_TNUMBER: { 55.371 - size_t l; 55.372 - lua_lock(L); /* `luaV_tostring' may create a new string */ 55.373 - l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0); 55.374 - lua_unlock(L); 55.375 - return l; 55.376 - } 55.377 - default: return 0; 55.378 - } 55.379 -} 55.380 - 55.381 - 55.382 -LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { 55.383 - StkId o = index2adr(L, idx); 55.384 - return (!iscfunction(o)) ? NULL : clvalue(o)->c.f; 55.385 -} 55.386 - 55.387 - 55.388 -LUA_API void *lua_touserdata (lua_State *L, int idx) { 55.389 - StkId o = index2adr(L, idx); 55.390 - switch (ttype(o)) { 55.391 - case LUA_TUSERDATA: return (rawuvalue(o) + 1); 55.392 - case LUA_TLIGHTUSERDATA: return pvalue(o); 55.393 - default: return NULL; 55.394 - } 55.395 -} 55.396 - 55.397 - 55.398 -LUA_API lua_State *lua_tothread (lua_State *L, int idx) { 55.399 - StkId o = index2adr(L, idx); 55.400 - return (!ttisthread(o)) ? NULL : thvalue(o); 55.401 -} 55.402 - 55.403 - 55.404 -LUA_API const void *lua_topointer (lua_State *L, int idx) { 55.405 - StkId o = index2adr(L, idx); 55.406 - switch (ttype(o)) { 55.407 - case LUA_TTABLE: return hvalue(o); 55.408 - case LUA_TFUNCTION: return clvalue(o); 55.409 - case LUA_TTHREAD: return thvalue(o); 55.410 - case LUA_TUSERDATA: 55.411 - case LUA_TLIGHTUSERDATA: 55.412 - return lua_touserdata(L, idx); 55.413 - default: return NULL; 55.414 - } 55.415 -} 55.416 - 55.417 - 55.418 - 55.419 -/* 55.420 -** push functions (C -> stack) 55.421 -*/ 55.422 - 55.423 - 55.424 -LUA_API void lua_pushnil (lua_State *L) { 55.425 - lua_lock(L); 55.426 - setnilvalue(L->top); 55.427 - api_incr_top(L); 55.428 - lua_unlock(L); 55.429 -} 55.430 - 55.431 - 55.432 -LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { 55.433 - lua_lock(L); 55.434 - setnvalue(L->top, n); 55.435 - api_incr_top(L); 55.436 - lua_unlock(L); 55.437 -} 55.438 - 55.439 - 55.440 -LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { 55.441 - lua_lock(L); 55.442 - setnvalue(L->top, cast_num(n)); 55.443 - api_incr_top(L); 55.444 - lua_unlock(L); 55.445 -} 55.446 - 55.447 - 55.448 -LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) { 55.449 - lua_lock(L); 55.450 - luaC_checkGC(L); 55.451 - setsvalue2s(L, L->top, luaS_newlstr(L, s, len)); 55.452 - api_incr_top(L); 55.453 - lua_unlock(L); 55.454 -} 55.455 - 55.456 - 55.457 -LUA_API void lua_pushstring (lua_State *L, const char *s) { 55.458 - if (s == NULL) 55.459 - lua_pushnil(L); 55.460 - else 55.461 - lua_pushlstring(L, s, strlen(s)); 55.462 -} 55.463 - 55.464 - 55.465 -LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, 55.466 - va_list argp) { 55.467 - const char *ret; 55.468 - lua_lock(L); 55.469 - luaC_checkGC(L); 55.470 - ret = luaO_pushvfstring(L, fmt, argp); 55.471 - lua_unlock(L); 55.472 - return ret; 55.473 -} 55.474 - 55.475 - 55.476 -LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { 55.477 - const char *ret; 55.478 - va_list argp; 55.479 - lua_lock(L); 55.480 - luaC_checkGC(L); 55.481 - va_start(argp, fmt); 55.482 - ret = luaO_pushvfstring(L, fmt, argp); 55.483 - va_end(argp); 55.484 - lua_unlock(L); 55.485 - return ret; 55.486 -} 55.487 - 55.488 - 55.489 -LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { 55.490 - Closure *cl; 55.491 - lua_lock(L); 55.492 - luaC_checkGC(L); 55.493 - api_checknelems(L, n); 55.494 - cl = luaF_newCclosure(L, n, getcurrenv(L)); 55.495 - cl->c.f = fn; 55.496 - L->top -= n; 55.497 - while (n--) 55.498 - setobj2n(L, &cl->c.upvalue[n], L->top+n); 55.499 - setclvalue(L, L->top, cl); 55.500 - lua_assert(iswhite(obj2gco(cl))); 55.501 - api_incr_top(L); 55.502 - lua_unlock(L); 55.503 -} 55.504 - 55.505 - 55.506 -LUA_API void lua_pushboolean (lua_State *L, int b) { 55.507 - lua_lock(L); 55.508 - setbvalue(L->top, (b != 0)); /* ensure that true is 1 */ 55.509 - api_incr_top(L); 55.510 - lua_unlock(L); 55.511 -} 55.512 - 55.513 - 55.514 -LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { 55.515 - lua_lock(L); 55.516 - setpvalue(L->top, p); 55.517 - api_incr_top(L); 55.518 - lua_unlock(L); 55.519 -} 55.520 - 55.521 - 55.522 -LUA_API int lua_pushthread (lua_State *L) { 55.523 - lua_lock(L); 55.524 - setthvalue(L, L->top, L); 55.525 - api_incr_top(L); 55.526 - lua_unlock(L); 55.527 - return (G(L)->mainthread == L); 55.528 -} 55.529 - 55.530 - 55.531 - 55.532 -/* 55.533 -** get functions (Lua -> stack) 55.534 -*/ 55.535 - 55.536 - 55.537 -LUA_API void lua_gettable (lua_State *L, int idx) { 55.538 - StkId t; 55.539 - lua_lock(L); 55.540 - t = index2adr(L, idx); 55.541 - api_checkvalidindex(L, t); 55.542 - luaV_gettable(L, t, L->top - 1, L->top - 1); 55.543 - lua_unlock(L); 55.544 -} 55.545 - 55.546 - 55.547 -LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { 55.548 - StkId t; 55.549 - TValue key; 55.550 - lua_lock(L); 55.551 - t = index2adr(L, idx); 55.552 - api_checkvalidindex(L, t); 55.553 - setsvalue(L, &key, luaS_new(L, k)); 55.554 - luaV_gettable(L, t, &key, L->top); 55.555 - api_incr_top(L); 55.556 - lua_unlock(L); 55.557 -} 55.558 - 55.559 - 55.560 -LUA_API void lua_rawget (lua_State *L, int idx) { 55.561 - StkId t; 55.562 - lua_lock(L); 55.563 - t = index2adr(L, idx); 55.564 - api_check(L, ttistable(t)); 55.565 - setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); 55.566 - lua_unlock(L); 55.567 -} 55.568 - 55.569 - 55.570 -LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { 55.571 - StkId o; 55.572 - lua_lock(L); 55.573 - o = index2adr(L, idx); 55.574 - api_check(L, ttistable(o)); 55.575 - setobj2s(L, L->top, luaH_getnum(hvalue(o), n)); 55.576 - api_incr_top(L); 55.577 - lua_unlock(L); 55.578 -} 55.579 - 55.580 - 55.581 -LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { 55.582 - lua_lock(L); 55.583 - luaC_checkGC(L); 55.584 - sethvalue(L, L->top, luaH_new(L, narray, nrec)); 55.585 - api_incr_top(L); 55.586 - lua_unlock(L); 55.587 -} 55.588 - 55.589 - 55.590 -LUA_API int lua_getmetatable (lua_State *L, int objindex) { 55.591 - const TValue *obj; 55.592 - Table *mt = NULL; 55.593 - int res; 55.594 - lua_lock(L); 55.595 - obj = index2adr(L, objindex); 55.596 - switch (ttype(obj)) { 55.597 - case LUA_TTABLE: 55.598 - mt = hvalue(obj)->metatable; 55.599 - break; 55.600 - case LUA_TUSERDATA: 55.601 - mt = uvalue(obj)->metatable; 55.602 - break; 55.603 - default: 55.604 - mt = G(L)->mt[ttype(obj)]; 55.605 - break; 55.606 - } 55.607 - if (mt == NULL) 55.608 - res = 0; 55.609 - else { 55.610 - sethvalue(L, L->top, mt); 55.611 - api_incr_top(L); 55.612 - res = 1; 55.613 - } 55.614 - lua_unlock(L); 55.615 - return res; 55.616 -} 55.617 - 55.618 - 55.619 -LUA_API void lua_getfenv (lua_State *L, int idx) { 55.620 - StkId o; 55.621 - lua_lock(L); 55.622 - o = index2adr(L, idx); 55.623 - api_checkvalidindex(L, o); 55.624 - switch (ttype(o)) { 55.625 - case LUA_TFUNCTION: 55.626 - sethvalue(L, L->top, clvalue(o)->c.env); 55.627 - break; 55.628 - case LUA_TUSERDATA: 55.629 - sethvalue(L, L->top, uvalue(o)->env); 55.630 - break; 55.631 - case LUA_TTHREAD: 55.632 - setobj2s(L, L->top, gt(thvalue(o))); 55.633 - break; 55.634 - default: 55.635 - setnilvalue(L->top); 55.636 - break; 55.637 - } 55.638 - api_incr_top(L); 55.639 - lua_unlock(L); 55.640 -} 55.641 - 55.642 - 55.643 -/* 55.644 -** set functions (stack -> Lua) 55.645 -*/ 55.646 - 55.647 - 55.648 -LUA_API void lua_settable (lua_State *L, int idx) { 55.649 - StkId t; 55.650 - lua_lock(L); 55.651 - api_checknelems(L, 2); 55.652 - t = index2adr(L, idx); 55.653 - api_checkvalidindex(L, t); 55.654 - luaV_settable(L, t, L->top - 2, L->top - 1); 55.655 - L->top -= 2; /* pop index and value */ 55.656 - lua_unlock(L); 55.657 -} 55.658 - 55.659 - 55.660 -LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { 55.661 - StkId t; 55.662 - TValue key; 55.663 - lua_lock(L); 55.664 - api_checknelems(L, 1); 55.665 - t = index2adr(L, idx); 55.666 - api_checkvalidindex(L, t); 55.667 - setsvalue(L, &key, luaS_new(L, k)); 55.668 - luaV_settable(L, t, &key, L->top - 1); 55.669 - L->top--; /* pop value */ 55.670 - lua_unlock(L); 55.671 -} 55.672 - 55.673 - 55.674 -LUA_API void lua_rawset (lua_State *L, int idx) { 55.675 - StkId t; 55.676 - lua_lock(L); 55.677 - api_checknelems(L, 2); 55.678 - t = index2adr(L, idx); 55.679 - api_check(L, ttistable(t)); 55.680 - setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); 55.681 - luaC_barriert(L, hvalue(t), L->top-1); 55.682 - L->top -= 2; 55.683 - lua_unlock(L); 55.684 -} 55.685 - 55.686 - 55.687 -LUA_API void lua_rawseti (lua_State *L, int idx, int n) { 55.688 - StkId o; 55.689 - lua_lock(L); 55.690 - api_checknelems(L, 1); 55.691 - o = index2adr(L, idx); 55.692 - api_check(L, ttistable(o)); 55.693 - setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1); 55.694 - luaC_barriert(L, hvalue(o), L->top-1); 55.695 - L->top--; 55.696 - lua_unlock(L); 55.697 -} 55.698 - 55.699 - 55.700 -LUA_API int lua_setmetatable (lua_State *L, int objindex) { 55.701 - TValue *obj; 55.702 - Table *mt; 55.703 - lua_lock(L); 55.704 - api_checknelems(L, 1); 55.705 - obj = index2adr(L, objindex); 55.706 - api_checkvalidindex(L, obj); 55.707 - if (ttisnil(L->top - 1)) 55.708 - mt = NULL; 55.709 - else { 55.710 - api_check(L, ttistable(L->top - 1)); 55.711 - mt = hvalue(L->top - 1); 55.712 - } 55.713 - switch (ttype(obj)) { 55.714 - case LUA_TTABLE: { 55.715 - hvalue(obj)->metatable = mt; 55.716 - if (mt) 55.717 - luaC_objbarriert(L, hvalue(obj), mt); 55.718 - break; 55.719 - } 55.720 - case LUA_TUSERDATA: { 55.721 - uvalue(obj)->metatable = mt; 55.722 - if (mt) 55.723 - luaC_objbarrier(L, rawuvalue(obj), mt); 55.724 - break; 55.725 - } 55.726 - default: { 55.727 - G(L)->mt[ttype(obj)] = mt; 55.728 - break; 55.729 - } 55.730 - } 55.731 - L->top--; 55.732 - lua_unlock(L); 55.733 - return 1; 55.734 -} 55.735 - 55.736 - 55.737 -LUA_API int lua_setfenv (lua_State *L, int idx) { 55.738 - StkId o; 55.739 - int res = 1; 55.740 - lua_lock(L); 55.741 - api_checknelems(L, 1); 55.742 - o = index2adr(L, idx); 55.743 - api_checkvalidindex(L, o); 55.744 - api_check(L, ttistable(L->top - 1)); 55.745 - switch (ttype(o)) { 55.746 - case LUA_TFUNCTION: 55.747 - clvalue(o)->c.env = hvalue(L->top - 1); 55.748 - break; 55.749 - case LUA_TUSERDATA: 55.750 - uvalue(o)->env = hvalue(L->top - 1); 55.751 - break; 55.752 - case LUA_TTHREAD: 55.753 - sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1)); 55.754 - break; 55.755 - default: 55.756 - res = 0; 55.757 - break; 55.758 - } 55.759 - if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); 55.760 - L->top--; 55.761 - lua_unlock(L); 55.762 - return res; 55.763 -} 55.764 - 55.765 - 55.766 -/* 55.767 -** `load' and `call' functions (run Lua code) 55.768 -*/ 55.769 - 55.770 - 55.771 -#define adjustresults(L,nres) \ 55.772 - { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; } 55.773 - 55.774 - 55.775 -#define checkresults(L,na,nr) \ 55.776 - api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na))) 55.777 - 55.778 - 55.779 -LUA_API void lua_call (lua_State *L, int nargs, int nresults) { 55.780 - StkId func; 55.781 - lua_lock(L); 55.782 - api_checknelems(L, nargs+1); 55.783 - checkresults(L, nargs, nresults); 55.784 - func = L->top - (nargs+1); 55.785 - luaD_call(L, func, nresults); 55.786 - adjustresults(L, nresults); 55.787 - lua_unlock(L); 55.788 -} 55.789 - 55.790 - 55.791 - 55.792 -/* 55.793 -** Execute a protected call. 55.794 -*/ 55.795 -struct CallS { /* data to `f_call' */ 55.796 - StkId func; 55.797 - int nresults; 55.798 -}; 55.799 - 55.800 - 55.801 -static void f_call (lua_State *L, void *ud) { 55.802 - struct CallS *c = cast(struct CallS *, ud); 55.803 - luaD_call(L, c->func, c->nresults); 55.804 -} 55.805 - 55.806 - 55.807 - 55.808 -LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { 55.809 - struct CallS c; 55.810 - int status; 55.811 - ptrdiff_t func; 55.812 - lua_lock(L); 55.813 - api_checknelems(L, nargs+1); 55.814 - checkresults(L, nargs, nresults); 55.815 - if (errfunc == 0) 55.816 - func = 0; 55.817 - else { 55.818 - StkId o = index2adr(L, errfunc); 55.819 - api_checkvalidindex(L, o); 55.820 - func = savestack(L, o); 55.821 - } 55.822 - c.func = L->top - (nargs+1); /* function to be called */ 55.823 - c.nresults = nresults; 55.824 - status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); 55.825 - adjustresults(L, nresults); 55.826 - lua_unlock(L); 55.827 - return status; 55.828 -} 55.829 - 55.830 - 55.831 -/* 55.832 -** Execute a protected C call. 55.833 -*/ 55.834 -struct CCallS { /* data to `f_Ccall' */ 55.835 - lua_CFunction func; 55.836 - void *ud; 55.837 -}; 55.838 - 55.839 - 55.840 -static void f_Ccall (lua_State *L, void *ud) { 55.841 - struct CCallS *c = cast(struct CCallS *, ud); 55.842 - Closure *cl; 55.843 - cl = luaF_newCclosure(L, 0, getcurrenv(L)); 55.844 - cl->c.f = c->func; 55.845 - setclvalue(L, L->top, cl); /* push function */ 55.846 - api_incr_top(L); 55.847 - setpvalue(L->top, c->ud); /* push only argument */ 55.848 - api_incr_top(L); 55.849 - luaD_call(L, L->top - 2, 0); 55.850 -} 55.851 - 55.852 - 55.853 -LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) { 55.854 - struct CCallS c; 55.855 - int status; 55.856 - lua_lock(L); 55.857 - c.func = func; 55.858 - c.ud = ud; 55.859 - status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0); 55.860 - lua_unlock(L); 55.861 - return status; 55.862 -} 55.863 - 55.864 - 55.865 -LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, 55.866 - const char *chunkname) { 55.867 - ZIO z; 55.868 - int status; 55.869 - lua_lock(L); 55.870 - if (!chunkname) chunkname = "?"; 55.871 - luaZ_init(L, &z, reader, data); 55.872 - status = luaD_protectedparser(L, &z, chunkname); 55.873 - lua_unlock(L); 55.874 - return status; 55.875 -} 55.876 - 55.877 - 55.878 -LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) { 55.879 - int status; 55.880 - TValue *o; 55.881 - lua_lock(L); 55.882 - api_checknelems(L, 1); 55.883 - o = L->top - 1; 55.884 - if (isLfunction(o)) 55.885 - status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0); 55.886 - else 55.887 - status = 1; 55.888 - lua_unlock(L); 55.889 - return status; 55.890 -} 55.891 - 55.892 - 55.893 -LUA_API int lua_status (lua_State *L) { 55.894 - return L->status; 55.895 -} 55.896 - 55.897 - 55.898 -/* 55.899 -** Garbage-collection function 55.900 -*/ 55.901 - 55.902 -LUA_API int lua_gc (lua_State *L, int what, int data) { 55.903 - int res = 0; 55.904 - global_State *g; 55.905 - lua_lock(L); 55.906 - g = G(L); 55.907 - switch (what) { 55.908 - case LUA_GCSTOP: { 55.909 - g->GCthreshold = MAX_LUMEM; 55.910 - break; 55.911 - } 55.912 - case LUA_GCRESTART: { 55.913 - g->GCthreshold = g->totalbytes; 55.914 - break; 55.915 - } 55.916 - case LUA_GCCOLLECT: { 55.917 - luaC_fullgc(L); 55.918 - break; 55.919 - } 55.920 - case LUA_GCCOUNT: { 55.921 - /* GC values are expressed in Kbytes: #bytes/2^10 */ 55.922 - res = cast_int(g->totalbytes >> 10); 55.923 - break; 55.924 - } 55.925 - case LUA_GCCOUNTB: { 55.926 - res = cast_int(g->totalbytes & 0x3ff); 55.927 - break; 55.928 - } 55.929 - case LUA_GCSTEP: { 55.930 - lu_mem a = (cast(lu_mem, data) << 10); 55.931 - if (a <= g->totalbytes) 55.932 - g->GCthreshold = g->totalbytes - a; 55.933 - else 55.934 - g->GCthreshold = 0; 55.935 - while (g->GCthreshold <= g->totalbytes) { 55.936 - luaC_step(L); 55.937 - if (g->gcstate == GCSpause) { /* end of cycle? */ 55.938 - res = 1; /* signal it */ 55.939 - break; 55.940 - } 55.941 - } 55.942 - break; 55.943 - } 55.944 - case LUA_GCSETPAUSE: { 55.945 - res = g->gcpause; 55.946 - g->gcpause = data; 55.947 - break; 55.948 - } 55.949 - case LUA_GCSETSTEPMUL: { 55.950 - res = g->gcstepmul; 55.951 - g->gcstepmul = data; 55.952 - break; 55.953 - } 55.954 - default: res = -1; /* invalid option */ 55.955 - } 55.956 - lua_unlock(L); 55.957 - return res; 55.958 -} 55.959 - 55.960 - 55.961 - 55.962 -/* 55.963 -** miscellaneous functions 55.964 -*/ 55.965 - 55.966 - 55.967 -LUA_API int lua_error (lua_State *L) { 55.968 - lua_lock(L); 55.969 - api_checknelems(L, 1); 55.970 - luaG_errormsg(L); 55.971 - lua_unlock(L); 55.972 - return 0; /* to avoid warnings */ 55.973 -} 55.974 - 55.975 - 55.976 -LUA_API int lua_next (lua_State *L, int idx) { 55.977 - StkId t; 55.978 - int more; 55.979 - lua_lock(L); 55.980 - t = index2adr(L, idx); 55.981 - api_check(L, ttistable(t)); 55.982 - more = luaH_next(L, hvalue(t), L->top - 1); 55.983 - if (more) { 55.984 - api_incr_top(L); 55.985 - } 55.986 - else /* no more elements */ 55.987 - L->top -= 1; /* remove key */ 55.988 - lua_unlock(L); 55.989 - return more; 55.990 -} 55.991 - 55.992 - 55.993 -LUA_API void lua_concat (lua_State *L, int n) { 55.994 - lua_lock(L); 55.995 - api_checknelems(L, n); 55.996 - if (n >= 2) { 55.997 - luaC_checkGC(L); 55.998 - luaV_concat(L, n, cast_int(L->top - L->base) - 1); 55.999 - L->top -= (n-1); 55.1000 - } 55.1001 - else if (n == 0) { /* push empty string */ 55.1002 - setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); 55.1003 - api_incr_top(L); 55.1004 - } 55.1005 - /* else n == 1; nothing to do */ 55.1006 - lua_unlock(L); 55.1007 -} 55.1008 - 55.1009 - 55.1010 -LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { 55.1011 - lua_Alloc f; 55.1012 - lua_lock(L); 55.1013 - if (ud) *ud = G(L)->ud; 55.1014 - f = G(L)->frealloc; 55.1015 - lua_unlock(L); 55.1016 - return f; 55.1017 -} 55.1018 - 55.1019 - 55.1020 -LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { 55.1021 - lua_lock(L); 55.1022 - G(L)->ud = ud; 55.1023 - G(L)->frealloc = f; 55.1024 - lua_unlock(L); 55.1025 -} 55.1026 - 55.1027 - 55.1028 -LUA_API void *lua_newuserdata (lua_State *L, size_t size) { 55.1029 - Udata *u; 55.1030 - lua_lock(L); 55.1031 - luaC_checkGC(L); 55.1032 - u = luaS_newudata(L, size, getcurrenv(L)); 55.1033 - setuvalue(L, L->top, u); 55.1034 - api_incr_top(L); 55.1035 - lua_unlock(L); 55.1036 - return u + 1; 55.1037 -} 55.1038 - 55.1039 - 55.1040 - 55.1041 - 55.1042 -static const char *aux_upvalue (StkId fi, int n, TValue **val) { 55.1043 - Closure *f; 55.1044 - if (!ttisfunction(fi)) return NULL; 55.1045 - f = clvalue(fi); 55.1046 - if (f->c.isC) { 55.1047 - if (!(1 <= n && n <= f->c.nupvalues)) return NULL; 55.1048 - *val = &f->c.upvalue[n-1]; 55.1049 - return ""; 55.1050 - } 55.1051 - else { 55.1052 - Proto *p = f->l.p; 55.1053 - if (!(1 <= n && n <= p->sizeupvalues)) return NULL; 55.1054 - *val = f->l.upvals[n-1]->v; 55.1055 - return getstr(p->upvalues[n-1]); 55.1056 - } 55.1057 -} 55.1058 - 55.1059 - 55.1060 -LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { 55.1061 - const char *name; 55.1062 - TValue *val; 55.1063 - lua_lock(L); 55.1064 - name = aux_upvalue(index2adr(L, funcindex), n, &val); 55.1065 - if (name) { 55.1066 - setobj2s(L, L->top, val); 55.1067 - api_incr_top(L); 55.1068 - } 55.1069 - lua_unlock(L); 55.1070 - return name; 55.1071 -} 55.1072 - 55.1073 - 55.1074 -LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { 55.1075 - const char *name; 55.1076 - TValue *val; 55.1077 - StkId fi; 55.1078 - lua_lock(L); 55.1079 - fi = index2adr(L, funcindex); 55.1080 - api_checknelems(L, 1); 55.1081 - name = aux_upvalue(fi, n, &val); 55.1082 - if (name) { 55.1083 - L->top--; 55.1084 - setobj(L, val, L->top); 55.1085 - luaC_barrier(L, clvalue(fi), L->top); 55.1086 - } 55.1087 - lua_unlock(L); 55.1088 - return name; 55.1089 -} 55.1090 -
56.1 --- a/src/lua/src/lapi.h Sat Mar 03 11:04:56 2012 -0600 56.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 56.3 @@ -1,16 +0,0 @@ 56.4 -/* 56.5 -** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $ 56.6 -** Auxiliary functions from Lua API 56.7 -** See Copyright Notice in lua.h 56.8 -*/ 56.9 - 56.10 -#ifndef lapi_h 56.11 -#define lapi_h 56.12 - 56.13 - 56.14 -#include "lobject.h" 56.15 - 56.16 - 56.17 -LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o); 56.18 - 56.19 -#endif
57.1 --- a/src/lua/src/lauxlib.c Sat Mar 03 11:04:56 2012 -0600 57.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 57.3 @@ -1,652 +0,0 @@ 57.4 -/* 57.5 -** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $ 57.6 -** Auxiliary functions for building Lua libraries 57.7 -** See Copyright Notice in lua.h 57.8 -*/ 57.9 - 57.10 - 57.11 -#include <ctype.h> 57.12 -#include <errno.h> 57.13 -#include <stdarg.h> 57.14 -#include <stdio.h> 57.15 -#include <stdlib.h> 57.16 -#include <string.h> 57.17 - 57.18 - 57.19 -/* This file uses only the official API of Lua. 57.20 -** Any function declared here could be written as an application function. 57.21 -*/ 57.22 - 57.23 -#define lauxlib_c 57.24 -#define LUA_LIB 57.25 - 57.26 -#include "lua.h" 57.27 - 57.28 -#include "lauxlib.h" 57.29 - 57.30 - 57.31 -#define FREELIST_REF 0 /* free list of references */ 57.32 - 57.33 - 57.34 -/* convert a stack index to positive */ 57.35 -#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \ 57.36 - lua_gettop(L) + (i) + 1) 57.37 - 57.38 - 57.39 -/* 57.40 -** {====================================================== 57.41 -** Error-report functions 57.42 -** ======================================================= 57.43 -*/ 57.44 - 57.45 - 57.46 -LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { 57.47 - lua_Debug ar; 57.48 - if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ 57.49 - return luaL_error(L, "bad argument #%d (%s)", narg, extramsg); 57.50 - lua_getinfo(L, "n", &ar); 57.51 - if (strcmp(ar.namewhat, "method") == 0) { 57.52 - narg--; /* do not count `self' */ 57.53 - if (narg == 0) /* error is in the self argument itself? */ 57.54 - return luaL_error(L, "calling " LUA_QS " on bad self (%s)", 57.55 - ar.name, extramsg); 57.56 - } 57.57 - if (ar.name == NULL) 57.58 - ar.name = "?"; 57.59 - return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)", 57.60 - narg, ar.name, extramsg); 57.61 -} 57.62 - 57.63 - 57.64 -LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) { 57.65 - const char *msg = lua_pushfstring(L, "%s expected, got %s", 57.66 - tname, luaL_typename(L, narg)); 57.67 - return luaL_argerror(L, narg, msg); 57.68 -} 57.69 - 57.70 - 57.71 -static void tag_error (lua_State *L, int narg, int tag) { 57.72 - luaL_typerror(L, narg, lua_typename(L, tag)); 57.73 -} 57.74 - 57.75 - 57.76 -LUALIB_API void luaL_where (lua_State *L, int level) { 57.77 - lua_Debug ar; 57.78 - if (lua_getstack(L, level, &ar)) { /* check function at level */ 57.79 - lua_getinfo(L, "Sl", &ar); /* get info about it */ 57.80 - if (ar.currentline > 0) { /* is there info? */ 57.81 - lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); 57.82 - return; 57.83 - } 57.84 - } 57.85 - lua_pushliteral(L, ""); /* else, no information available... */ 57.86 -} 57.87 - 57.88 - 57.89 -LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { 57.90 - va_list argp; 57.91 - va_start(argp, fmt); 57.92 - luaL_where(L, 1); 57.93 - lua_pushvfstring(L, fmt, argp); 57.94 - va_end(argp); 57.95 - lua_concat(L, 2); 57.96 - return lua_error(L); 57.97 -} 57.98 - 57.99 -/* }====================================================== */ 57.100 - 57.101 - 57.102 -LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def, 57.103 - const char *const lst[]) { 57.104 - const char *name = (def) ? luaL_optstring(L, narg, def) : 57.105 - luaL_checkstring(L, narg); 57.106 - int i; 57.107 - for (i=0; lst[i]; i++) 57.108 - if (strcmp(lst[i], name) == 0) 57.109 - return i; 57.110 - return luaL_argerror(L, narg, 57.111 - lua_pushfstring(L, "invalid option " LUA_QS, name)); 57.112 -} 57.113 - 57.114 - 57.115 -LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { 57.116 - lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */ 57.117 - if (!lua_isnil(L, -1)) /* name already in use? */ 57.118 - return 0; /* leave previous value on top, but return 0 */ 57.119 - lua_pop(L, 1); 57.120 - lua_newtable(L); /* create metatable */ 57.121 - lua_pushvalue(L, -1); 57.122 - lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ 57.123 - return 1; 57.124 -} 57.125 - 57.126 - 57.127 -LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { 57.128 - void *p = lua_touserdata(L, ud); 57.129 - if (p != NULL) { /* value is a userdata? */ 57.130 - if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ 57.131 - lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ 57.132 - if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ 57.133 - lua_pop(L, 2); /* remove both metatables */ 57.134 - return p; 57.135 - } 57.136 - } 57.137 - } 57.138 - luaL_typerror(L, ud, tname); /* else error */ 57.139 - return NULL; /* to avoid warnings */ 57.140 -} 57.141 - 57.142 - 57.143 -LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { 57.144 - if (!lua_checkstack(L, space)) 57.145 - luaL_error(L, "stack overflow (%s)", mes); 57.146 -} 57.147 - 57.148 - 57.149 -LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) { 57.150 - if (lua_type(L, narg) != t) 57.151 - tag_error(L, narg, t); 57.152 -} 57.153 - 57.154 - 57.155 -LUALIB_API void luaL_checkany (lua_State *L, int narg) { 57.156 - if (lua_type(L, narg) == LUA_TNONE) 57.157 - luaL_argerror(L, narg, "value expected"); 57.158 -} 57.159 - 57.160 - 57.161 -LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { 57.162 - const char *s = lua_tolstring(L, narg, len); 57.163 - if (!s) tag_error(L, narg, LUA_TSTRING); 57.164 - return s; 57.165 -} 57.166 - 57.167 - 57.168 -LUALIB_API const char *luaL_optlstring (lua_State *L, int narg, 57.169 - const char *def, size_t *len) { 57.170 - if (lua_isnoneornil(L, narg)) { 57.171 - if (len) 57.172 - *len = (def ? strlen(def) : 0); 57.173 - return def; 57.174 - } 57.175 - else return luaL_checklstring(L, narg, len); 57.176 -} 57.177 - 57.178 - 57.179 -LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { 57.180 - lua_Number d = lua_tonumber(L, narg); 57.181 - if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ 57.182 - tag_error(L, narg, LUA_TNUMBER); 57.183 - return d; 57.184 -} 57.185 - 57.186 - 57.187 -LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { 57.188 - return luaL_opt(L, luaL_checknumber, narg, def); 57.189 -} 57.190 - 57.191 - 57.192 -LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { 57.193 - lua_Integer d = lua_tointeger(L, narg); 57.194 - if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ 57.195 - tag_error(L, narg, LUA_TNUMBER); 57.196 - return d; 57.197 -} 57.198 - 57.199 - 57.200 -LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, 57.201 - lua_Integer def) { 57.202 - return luaL_opt(L, luaL_checkinteger, narg, def); 57.203 -} 57.204 - 57.205 - 57.206 -LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { 57.207 - if (!lua_getmetatable(L, obj)) /* no metatable? */ 57.208 - return 0; 57.209 - lua_pushstring(L, event); 57.210 - lua_rawget(L, -2); 57.211 - if (lua_isnil(L, -1)) { 57.212 - lua_pop(L, 2); /* remove metatable and metafield */ 57.213 - return 0; 57.214 - } 57.215 - else { 57.216 - lua_remove(L, -2); /* remove only metatable */ 57.217 - return 1; 57.218 - } 57.219 -} 57.220 - 57.221 - 57.222 -LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { 57.223 - obj = abs_index(L, obj); 57.224 - if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ 57.225 - return 0; 57.226 - lua_pushvalue(L, obj); 57.227 - lua_call(L, 1, 1); 57.228 - return 1; 57.229 -} 57.230 - 57.231 - 57.232 -LUALIB_API void (luaL_register) (lua_State *L, const char *libname, 57.233 - const luaL_Reg *l) { 57.234 - luaI_openlib(L, libname, l, 0); 57.235 -} 57.236 - 57.237 - 57.238 -static int libsize (const luaL_Reg *l) { 57.239 - int size = 0; 57.240 - for (; l->name; l++) size++; 57.241 - return size; 57.242 -} 57.243 - 57.244 - 57.245 -LUALIB_API void luaI_openlib (lua_State *L, const char *libname, 57.246 - const luaL_Reg *l, int nup) { 57.247 - if (libname) { 57.248 - int size = libsize(l); 57.249 - /* check whether lib already exists */ 57.250 - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); 57.251 - lua_getfield(L, -1, libname); /* get _LOADED[libname] */ 57.252 - if (!lua_istable(L, -1)) { /* not found? */ 57.253 - lua_pop(L, 1); /* remove previous result */ 57.254 - /* try global variable (and create one if it does not exist) */ 57.255 - if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) 57.256 - luaL_error(L, "name conflict for module " LUA_QS, libname); 57.257 - lua_pushvalue(L, -1); 57.258 - lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ 57.259 - } 57.260 - lua_remove(L, -2); /* remove _LOADED table */ 57.261 - lua_insert(L, -(nup+1)); /* move library table to below upvalues */ 57.262 - } 57.263 - for (; l->name; l++) { 57.264 - int i; 57.265 - for (i=0; i<nup; i++) /* copy upvalues to the top */ 57.266 - lua_pushvalue(L, -nup); 57.267 - lua_pushcclosure(L, l->func, nup); 57.268 - lua_setfield(L, -(nup+2), l->name); 57.269 - } 57.270 - lua_pop(L, nup); /* remove upvalues */ 57.271 -} 57.272 - 57.273 - 57.274 - 57.275 -/* 57.276 -** {====================================================== 57.277 -** getn-setn: size for arrays 57.278 -** ======================================================= 57.279 -*/ 57.280 - 57.281 -#if defined(LUA_COMPAT_GETN) 57.282 - 57.283 -static int checkint (lua_State *L, int topop) { 57.284 - int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1; 57.285 - lua_pop(L, topop); 57.286 - return n; 57.287 -} 57.288 - 57.289 - 57.290 -static void getsizes (lua_State *L) { 57.291 - lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); 57.292 - if (lua_isnil(L, -1)) { /* no `size' table? */ 57.293 - lua_pop(L, 1); /* remove nil */ 57.294 - lua_newtable(L); /* create it */ 57.295 - lua_pushvalue(L, -1); /* `size' will be its own metatable */ 57.296 - lua_setmetatable(L, -2); 57.297 - lua_pushliteral(L, "kv"); 57.298 - lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */ 57.299 - lua_pushvalue(L, -1); 57.300 - lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */ 57.301 - } 57.302 -} 57.303 - 57.304 - 57.305 -LUALIB_API void luaL_setn (lua_State *L, int t, int n) { 57.306 - t = abs_index(L, t); 57.307 - lua_pushliteral(L, "n"); 57.308 - lua_rawget(L, t); 57.309 - if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */ 57.310 - lua_pushliteral(L, "n"); /* use it */ 57.311 - lua_pushinteger(L, n); 57.312 - lua_rawset(L, t); 57.313 - } 57.314 - else { /* use `sizes' */ 57.315 - getsizes(L); 57.316 - lua_pushvalue(L, t); 57.317 - lua_pushinteger(L, n); 57.318 - lua_rawset(L, -3); /* sizes[t] = n */ 57.319 - lua_pop(L, 1); /* remove `sizes' */ 57.320 - } 57.321 -} 57.322 - 57.323 - 57.324 -LUALIB_API int luaL_getn (lua_State *L, int t) { 57.325 - int n; 57.326 - t = abs_index(L, t); 57.327 - lua_pushliteral(L, "n"); /* try t.n */ 57.328 - lua_rawget(L, t); 57.329 - if ((n = checkint(L, 1)) >= 0) return n; 57.330 - getsizes(L); /* else try sizes[t] */ 57.331 - lua_pushvalue(L, t); 57.332 - lua_rawget(L, -2); 57.333 - if ((n = checkint(L, 2)) >= 0) return n; 57.334 - return (int)lua_objlen(L, t); 57.335 -} 57.336 - 57.337 -#endif 57.338 - 57.339 -/* }====================================================== */ 57.340 - 57.341 - 57.342 - 57.343 -LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, 57.344 - const char *r) { 57.345 - const char *wild; 57.346 - size_t l = strlen(p); 57.347 - luaL_Buffer b; 57.348 - luaL_buffinit(L, &b); 57.349 - while ((wild = strstr(s, p)) != NULL) { 57.350 - luaL_addlstring(&b, s, wild - s); /* push prefix */ 57.351 - luaL_addstring(&b, r); /* push replacement in place of pattern */ 57.352 - s = wild + l; /* continue after `p' */ 57.353 - } 57.354 - luaL_addstring(&b, s); /* push last suffix */ 57.355 - luaL_pushresult(&b); 57.356 - return lua_tostring(L, -1); 57.357 -} 57.358 - 57.359 - 57.360 -LUALIB_API const char *luaL_findtable (lua_State *L, int idx, 57.361 - const char *fname, int szhint) { 57.362 - const char *e; 57.363 - lua_pushvalue(L, idx); 57.364 - do { 57.365 - e = strchr(fname, '.'); 57.366 - if (e == NULL) e = fname + strlen(fname); 57.367 - lua_pushlstring(L, fname, e - fname); 57.368 - lua_rawget(L, -2); 57.369 - if (lua_isnil(L, -1)) { /* no such field? */ 57.370 - lua_pop(L, 1); /* remove this nil */ 57.371 - lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ 57.372 - lua_pushlstring(L, fname, e - fname); 57.373 - lua_pushvalue(L, -2); 57.374 - lua_settable(L, -4); /* set new table into field */ 57.375 - } 57.376 - else if (!lua_istable(L, -1)) { /* field has a non-table value? */ 57.377 - lua_pop(L, 2); /* remove table and value */ 57.378 - return fname; /* return problematic part of the name */ 57.379 - } 57.380 - lua_remove(L, -2); /* remove previous table */ 57.381 - fname = e + 1; 57.382 - } while (*e == '.'); 57.383 - return NULL; 57.384 -} 57.385 - 57.386 - 57.387 - 57.388 -/* 57.389 -** {====================================================== 57.390 -** Generic Buffer manipulation 57.391 -** ======================================================= 57.392 -*/ 57.393 - 57.394 - 57.395 -#define bufflen(B) ((B)->p - (B)->buffer) 57.396 -#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B))) 57.397 - 57.398 -#define LIMIT (LUA_MINSTACK/2) 57.399 - 57.400 - 57.401 -static int emptybuffer (luaL_Buffer *B) { 57.402 - size_t l = bufflen(B); 57.403 - if (l == 0) return 0; /* put nothing on stack */ 57.404 - else { 57.405 - lua_pushlstring(B->L, B->buffer, l); 57.406 - B->p = B->buffer; 57.407 - B->lvl++; 57.408 - return 1; 57.409 - } 57.410 -} 57.411 - 57.412 - 57.413 -static void adjuststack (luaL_Buffer *B) { 57.414 - if (B->lvl > 1) { 57.415 - lua_State *L = B->L; 57.416 - int toget = 1; /* number of levels to concat */ 57.417 - size_t toplen = lua_strlen(L, -1); 57.418 - do { 57.419 - size_t l = lua_strlen(L, -(toget+1)); 57.420 - if (B->lvl - toget + 1 >= LIMIT || toplen > l) { 57.421 - toplen += l; 57.422 - toget++; 57.423 - } 57.424 - else break; 57.425 - } while (toget < B->lvl); 57.426 - lua_concat(L, toget); 57.427 - B->lvl = B->lvl - toget + 1; 57.428 - } 57.429 -} 57.430 - 57.431 - 57.432 -LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) { 57.433 - if (emptybuffer(B)) 57.434 - adjuststack(B); 57.435 - return B->buffer; 57.436 -} 57.437 - 57.438 - 57.439 -LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { 57.440 - while (l--) 57.441 - luaL_addchar(B, *s++); 57.442 -} 57.443 - 57.444 - 57.445 -LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { 57.446 - luaL_addlstring(B, s, strlen(s)); 57.447 -} 57.448 - 57.449 - 57.450 -LUALIB_API void luaL_pushresult (luaL_Buffer *B) { 57.451 - emptybuffer(B); 57.452 - lua_concat(B->L, B->lvl); 57.453 - B->lvl = 1; 57.454 -} 57.455 - 57.456 - 57.457 -LUALIB_API void luaL_addvalue (luaL_Buffer *B) { 57.458 - lua_State *L = B->L; 57.459 - size_t vl; 57.460 - const char *s = lua_tolstring(L, -1, &vl); 57.461 - if (vl <= bufffree(B)) { /* fit into buffer? */ 57.462 - memcpy(B->p, s, vl); /* put it there */ 57.463 - B->p += vl; 57.464 - lua_pop(L, 1); /* remove from stack */ 57.465 - } 57.466 - else { 57.467 - if (emptybuffer(B)) 57.468 - lua_insert(L, -2); /* put buffer before new value */ 57.469 - B->lvl++; /* add new value into B stack */ 57.470 - adjuststack(B); 57.471 - } 57.472 -} 57.473 - 57.474 - 57.475 -LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { 57.476 - B->L = L; 57.477 - B->p = B->buffer; 57.478 - B->lvl = 0; 57.479 -} 57.480 - 57.481 -/* }====================================================== */ 57.482 - 57.483 - 57.484 -LUALIB_API int luaL_ref (lua_State *L, int t) { 57.485 - int ref; 57.486 - t = abs_index(L, t); 57.487 - if (lua_isnil(L, -1)) { 57.488 - lua_pop(L, 1); /* remove from stack */ 57.489 - return LUA_REFNIL; /* `nil' has a unique fixed reference */ 57.490 - } 57.491 - lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ 57.492 - ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ 57.493 - lua_pop(L, 1); /* remove it from stack */ 57.494 - if (ref != 0) { /* any free element? */ 57.495 - lua_rawgeti(L, t, ref); /* remove it from list */ 57.496 - lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */ 57.497 - } 57.498 - else { /* no free elements */ 57.499 - ref = (int)lua_objlen(L, t); 57.500 - ref++; /* create new reference */ 57.501 - } 57.502 - lua_rawseti(L, t, ref); 57.503 - return ref; 57.504 -} 57.505 - 57.506 - 57.507 -LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { 57.508 - if (ref >= 0) { 57.509 - t = abs_index(L, t); 57.510 - lua_rawgeti(L, t, FREELIST_REF); 57.511 - lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ 57.512 - lua_pushinteger(L, ref); 57.513 - lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ 57.514 - } 57.515 -} 57.516 - 57.517 - 57.518 - 57.519 -/* 57.520 -** {====================================================== 57.521 -** Load functions 57.522 -** ======================================================= 57.523 -*/ 57.524 - 57.525 -typedef struct LoadF { 57.526 - int extraline; 57.527 - FILE *f; 57.528 - char buff[LUAL_BUFFERSIZE]; 57.529 -} LoadF; 57.530 - 57.531 - 57.532 -static const char *getF (lua_State *L, void *ud, size_t *size) { 57.533 - LoadF *lf = (LoadF *)ud; 57.534 - (void)L; 57.535 - if (lf->extraline) { 57.536 - lf->extraline = 0; 57.537 - *size = 1; 57.538 - return "\n"; 57.539 - } 57.540 - if (feof(lf->f)) return NULL; 57.541 - *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); 57.542 - return (*size > 0) ? lf->buff : NULL; 57.543 -} 57.544 - 57.545 - 57.546 -static int errfile (lua_State *L, const char *what, int fnameindex) { 57.547 - const char *serr = strerror(errno); 57.548 - const char *filename = lua_tostring(L, fnameindex) + 1; 57.549 - lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); 57.550 - lua_remove(L, fnameindex); 57.551 - return LUA_ERRFILE; 57.552 -} 57.553 - 57.554 - 57.555 -LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { 57.556 - LoadF lf; 57.557 - int status, readstatus; 57.558 - int c; 57.559 - int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ 57.560 - lf.extraline = 0; 57.561 - if (filename == NULL) { 57.562 - lua_pushliteral(L, "=stdin"); 57.563 - lf.f = stdin; 57.564 - } 57.565 - else { 57.566 - lua_pushfstring(L, "@%s", filename); 57.567 - lf.f = fopen(filename, "r"); 57.568 - if (lf.f == NULL) return errfile(L, "open", fnameindex); 57.569 - } 57.570 - c = getc(lf.f); 57.571 - if (c == '#') { /* Unix exec. file? */ 57.572 - lf.extraline = 1; 57.573 - while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ 57.574 - if (c == '\n') c = getc(lf.f); 57.575 - } 57.576 - if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ 57.577 - lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ 57.578 - if (lf.f == NULL) return errfile(L, "reopen", fnameindex); 57.579 - /* skip eventual `#!...' */ 57.580 - while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; 57.581 - lf.extraline = 0; 57.582 - } 57.583 - ungetc(c, lf.f); 57.584 - status = lua_load(L, getF, &lf, lua_tostring(L, -1)); 57.585 - readstatus = ferror(lf.f); 57.586 - if (filename) fclose(lf.f); /* close file (even in case of errors) */ 57.587 - if (readstatus) { 57.588 - lua_settop(L, fnameindex); /* ignore results from `lua_load' */ 57.589 - return errfile(L, "read", fnameindex); 57.590 - } 57.591 - lua_remove(L, fnameindex); 57.592 - return status; 57.593 -} 57.594 - 57.595 - 57.596 -typedef struct LoadS { 57.597 - const char *s; 57.598 - size_t size; 57.599 -} LoadS; 57.600 - 57.601 - 57.602 -static const char *getS (lua_State *L, void *ud, size_t *size) { 57.603 - LoadS *ls = (LoadS *)ud; 57.604 - (void)L; 57.605 - if (ls->size == 0) return NULL; 57.606 - *size = ls->size; 57.607 - ls->size = 0; 57.608 - return ls->s; 57.609 -} 57.610 - 57.611 - 57.612 -LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size, 57.613 - const char *name) { 57.614 - LoadS ls; 57.615 - ls.s = buff; 57.616 - ls.size = size; 57.617 - return lua_load(L, getS, &ls, name); 57.618 -} 57.619 - 57.620 - 57.621 -LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) { 57.622 - return luaL_loadbuffer(L, s, strlen(s), s); 57.623 -} 57.624 - 57.625 - 57.626 - 57.627 -/* }====================================================== */ 57.628 - 57.629 - 57.630 -static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { 57.631 - (void)ud; 57.632 - (void)osize; 57.633 - if (nsize == 0) { 57.634 - free(ptr); 57.635 - return NULL; 57.636 - } 57.637 - else 57.638 - return realloc(ptr, nsize); 57.639 -} 57.640 - 57.641 - 57.642 -static int panic (lua_State *L) { 57.643 - (void)L; /* to avoid warnings */ 57.644 - fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", 57.645 - lua_tostring(L, -1)); 57.646 - return 0; 57.647 -} 57.648 - 57.649 - 57.650 -LUALIB_API lua_State *luaL_newstate (void) { 57.651 - lua_State *L = lua_newstate(l_alloc, NULL); 57.652 - if (L) lua_atpanic(L, &panic); 57.653 - return L; 57.654 -} 57.655 -
58.1 --- a/src/lua/src/lauxlib.h Sat Mar 03 11:04:56 2012 -0600 58.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 58.3 @@ -1,174 +0,0 @@ 58.4 -/* 58.5 -** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ 58.6 -** Auxiliary functions for building Lua libraries 58.7 -** See Copyright Notice in lua.h 58.8 -*/ 58.9 - 58.10 - 58.11 -#ifndef lauxlib_h 58.12 -#define lauxlib_h 58.13 - 58.14 - 58.15 -#include <stddef.h> 58.16 -#include <stdio.h> 58.17 - 58.18 -#include "lua.h" 58.19 - 58.20 - 58.21 -#if defined(LUA_COMPAT_GETN) 58.22 -LUALIB_API int (luaL_getn) (lua_State *L, int t); 58.23 -LUALIB_API void (luaL_setn) (lua_State *L, int t, int n); 58.24 -#else 58.25 -#define luaL_getn(L,i) ((int)lua_objlen(L, i)) 58.26 -#define luaL_setn(L,i,j) ((void)0) /* no op! */ 58.27 -#endif 58.28 - 58.29 -#if defined(LUA_COMPAT_OPENLIB) 58.30 -#define luaI_openlib luaL_openlib 58.31 -#endif 58.32 - 58.33 - 58.34 -/* extra error code for `luaL_load' */ 58.35 -#define LUA_ERRFILE (LUA_ERRERR+1) 58.36 - 58.37 - 58.38 -typedef struct luaL_Reg { 58.39 - const char *name; 58.40 - lua_CFunction func; 58.41 -} luaL_Reg; 58.42 - 58.43 - 58.44 - 58.45 -LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname, 58.46 - const luaL_Reg *l, int nup); 58.47 -LUALIB_API void (luaL_register) (lua_State *L, const char *libname, 58.48 - const luaL_Reg *l); 58.49 -LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 58.50 -LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 58.51 -LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); 58.52 -LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); 58.53 -LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, 58.54 - size_t *l); 58.55 -LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, 58.56 - const char *def, size_t *l); 58.57 -LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); 58.58 -LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); 58.59 - 58.60 -LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); 58.61 -LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, 58.62 - lua_Integer def); 58.63 - 58.64 -LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 58.65 -LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); 58.66 -LUALIB_API void (luaL_checkany) (lua_State *L, int narg); 58.67 - 58.68 -LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 58.69 -LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 58.70 - 58.71 -LUALIB_API void (luaL_where) (lua_State *L, int lvl); 58.72 -LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 58.73 - 58.74 -LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, 58.75 - const char *const lst[]); 58.76 - 58.77 -LUALIB_API int (luaL_ref) (lua_State *L, int t); 58.78 -LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 58.79 - 58.80 -LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); 58.81 -LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, 58.82 - const char *name); 58.83 -LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 58.84 - 58.85 -LUALIB_API lua_State *(luaL_newstate) (void); 58.86 - 58.87 - 58.88 -LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 58.89 - const char *r); 58.90 - 58.91 -LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, 58.92 - const char *fname, int szhint); 58.93 - 58.94 - 58.95 - 58.96 - 58.97 -/* 58.98 -** =============================================================== 58.99 -** some useful macros 58.100 -** =============================================================== 58.101 -*/ 58.102 - 58.103 -#define luaL_argcheck(L, cond,numarg,extramsg) \ 58.104 - ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) 58.105 -#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 58.106 -#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 58.107 -#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 58.108 -#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 58.109 -#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 58.110 -#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 58.111 - 58.112 -#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 58.113 - 58.114 -#define luaL_dofile(L, fn) \ 58.115 - (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 58.116 - 58.117 -#define luaL_dostring(L, s) \ 58.118 - (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 58.119 - 58.120 -#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 58.121 - 58.122 -#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 58.123 - 58.124 -/* 58.125 -** {====================================================== 58.126 -** Generic Buffer manipulation 58.127 -** ======================================================= 58.128 -*/ 58.129 - 58.130 - 58.131 - 58.132 -typedef struct luaL_Buffer { 58.133 - char *p; /* current position in buffer */ 58.134 - int lvl; /* number of strings in the stack (level) */ 58.135 - lua_State *L; 58.136 - char buffer[LUAL_BUFFERSIZE]; 58.137 -} luaL_Buffer; 58.138 - 58.139 -#define luaL_addchar(B,c) \ 58.140 - ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ 58.141 - (*(B)->p++ = (char)(c))) 58.142 - 58.143 -/* compatibility only */ 58.144 -#define luaL_putchar(B,c) luaL_addchar(B,c) 58.145 - 58.146 -#define luaL_addsize(B,n) ((B)->p += (n)) 58.147 - 58.148 -LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 58.149 -LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); 58.150 -LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 58.151 -LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 58.152 -LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 58.153 -LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 58.154 - 58.155 - 58.156 -/* }====================================================== */ 58.157 - 58.158 - 58.159 -/* compatibility with ref system */ 58.160 - 58.161 -/* pre-defined references */ 58.162 -#define LUA_NOREF (-2) 58.163 -#define LUA_REFNIL (-1) 58.164 - 58.165 -#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ 58.166 - (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) 58.167 - 58.168 -#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) 58.169 - 58.170 -#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) 58.171 - 58.172 - 58.173 -#define luaL_reg luaL_Reg 58.174 - 58.175 -#endif 58.176 - 58.177 -
59.1 --- a/src/lua/src/lbaselib.c Sat Mar 03 11:04:56 2012 -0600 59.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 59.3 @@ -1,653 +0,0 @@ 59.4 -/* 59.5 -** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $ 59.6 -** Basic library 59.7 -** See Copyright Notice in lua.h 59.8 -*/ 59.9 - 59.10 - 59.11 - 59.12 -#include <ctype.h> 59.13 -#include <stdio.h> 59.14 -#include <stdlib.h> 59.15 -#include <string.h> 59.16 - 59.17 -#define lbaselib_c 59.18 -#define LUA_LIB 59.19 - 59.20 -#include "lua.h" 59.21 - 59.22 -#include "lauxlib.h" 59.23 -#include "lualib.h" 59.24 - 59.25 - 59.26 - 59.27 - 59.28 -/* 59.29 -** If your system does not support `stdout', you can just remove this function. 59.30 -** If you need, you can define your own `print' function, following this 59.31 -** model but changing `fputs' to put the strings at a proper place 59.32 -** (a console window or a log file, for instance). 59.33 -*/ 59.34 -static int luaB_print (lua_State *L) { 59.35 - int n = lua_gettop(L); /* number of arguments */ 59.36 - int i; 59.37 - lua_getglobal(L, "tostring"); 59.38 - for (i=1; i<=n; i++) { 59.39 - const char *s; 59.40 - lua_pushvalue(L, -1); /* function to be called */ 59.41 - lua_pushvalue(L, i); /* value to print */ 59.42 - lua_call(L, 1, 1); 59.43 - s = lua_tostring(L, -1); /* get result */ 59.44 - if (s == NULL) 59.45 - return luaL_error(L, LUA_QL("tostring") " must return a string to " 59.46 - LUA_QL("print")); 59.47 - if (i>1) fputs("\t", stdout); 59.48 - fputs(s, stdout); 59.49 - lua_pop(L, 1); /* pop result */ 59.50 - } 59.51 - fputs("\n", stdout); 59.52 - return 0; 59.53 -} 59.54 - 59.55 - 59.56 -static int luaB_tonumber (lua_State *L) { 59.57 - int base = luaL_optint(L, 2, 10); 59.58 - if (base == 10) { /* standard conversion */ 59.59 - luaL_checkany(L, 1); 59.60 - if (lua_isnumber(L, 1)) { 59.61 - lua_pushnumber(L, lua_tonumber(L, 1)); 59.62 - return 1; 59.63 - } 59.64 - } 59.65 - else { 59.66 - const char *s1 = luaL_checkstring(L, 1); 59.67 - char *s2; 59.68 - unsigned long n; 59.69 - luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); 59.70 - n = strtoul(s1, &s2, base); 59.71 - if (s1 != s2) { /* at least one valid digit? */ 59.72 - while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ 59.73 - if (*s2 == '\0') { /* no invalid trailing characters? */ 59.74 - lua_pushnumber(L, (lua_Number)n); 59.75 - return 1; 59.76 - } 59.77 - } 59.78 - } 59.79 - lua_pushnil(L); /* else not a number */ 59.80 - return 1; 59.81 -} 59.82 - 59.83 - 59.84 -static int luaB_error (lua_State *L) { 59.85 - int level = luaL_optint(L, 2, 1); 59.86 - lua_settop(L, 1); 59.87 - if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ 59.88 - luaL_where(L, level); 59.89 - lua_pushvalue(L, 1); 59.90 - lua_concat(L, 2); 59.91 - } 59.92 - return lua_error(L); 59.93 -} 59.94 - 59.95 - 59.96 -static int luaB_getmetatable (lua_State *L) { 59.97 - luaL_checkany(L, 1); 59.98 - if (!lua_getmetatable(L, 1)) { 59.99 - lua_pushnil(L); 59.100 - return 1; /* no metatable */ 59.101 - } 59.102 - luaL_getmetafield(L, 1, "__metatable"); 59.103 - return 1; /* returns either __metatable field (if present) or metatable */ 59.104 -} 59.105 - 59.106 - 59.107 -static int luaB_setmetatable (lua_State *L) { 59.108 - int t = lua_type(L, 2); 59.109 - luaL_checktype(L, 1, LUA_TTABLE); 59.110 - luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 59.111 - "nil or table expected"); 59.112 - if (luaL_getmetafield(L, 1, "__metatable")) 59.113 - luaL_error(L, "cannot change a protected metatable"); 59.114 - lua_settop(L, 2); 59.115 - lua_setmetatable(L, 1); 59.116 - return 1; 59.117 -} 59.118 - 59.119 - 59.120 -static void getfunc (lua_State *L, int opt) { 59.121 - if (lua_isfunction(L, 1)) lua_pushvalue(L, 1); 59.122 - else { 59.123 - lua_Debug ar; 59.124 - int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1); 59.125 - luaL_argcheck(L, level >= 0, 1, "level must be non-negative"); 59.126 - if (lua_getstack(L, level, &ar) == 0) 59.127 - luaL_argerror(L, 1, "invalid level"); 59.128 - lua_getinfo(L, "f", &ar); 59.129 - if (lua_isnil(L, -1)) 59.130 - luaL_error(L, "no function environment for tail call at level %d", 59.131 - level); 59.132 - } 59.133 -} 59.134 - 59.135 - 59.136 -static int luaB_getfenv (lua_State *L) { 59.137 - getfunc(L, 1); 59.138 - if (lua_iscfunction(L, -1)) /* is a C function? */ 59.139 - lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */ 59.140 - else 59.141 - lua_getfenv(L, -1); 59.142 - return 1; 59.143 -} 59.144 - 59.145 - 59.146 -static int luaB_setfenv (lua_State *L) { 59.147 - luaL_checktype(L, 2, LUA_TTABLE); 59.148 - getfunc(L, 0); 59.149 - lua_pushvalue(L, 2); 59.150 - if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) { 59.151 - /* change environment of current thread */ 59.152 - lua_pushthread(L); 59.153 - lua_insert(L, -2); 59.154 - lua_setfenv(L, -2); 59.155 - return 0; 59.156 - } 59.157 - else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0) 59.158 - luaL_error(L, 59.159 - LUA_QL("setfenv") " cannot change environment of given object"); 59.160 - return 1; 59.161 -} 59.162 - 59.163 - 59.164 -static int luaB_rawequal (lua_State *L) { 59.165 - luaL_checkany(L, 1); 59.166 - luaL_checkany(L, 2); 59.167 - lua_pushboolean(L, lua_rawequal(L, 1, 2)); 59.168 - return 1; 59.169 -} 59.170 - 59.171 - 59.172 -static int luaB_rawget (lua_State *L) { 59.173 - luaL_checktype(L, 1, LUA_TTABLE); 59.174 - luaL_checkany(L, 2); 59.175 - lua_settop(L, 2); 59.176 - lua_rawget(L, 1); 59.177 - return 1; 59.178 -} 59.179 - 59.180 -static int luaB_rawset (lua_State *L) { 59.181 - luaL_checktype(L, 1, LUA_TTABLE); 59.182 - luaL_checkany(L, 2); 59.183 - luaL_checkany(L, 3); 59.184 - lua_settop(L, 3); 59.185 - lua_rawset(L, 1); 59.186 - return 1; 59.187 -} 59.188 - 59.189 - 59.190 -static int luaB_gcinfo (lua_State *L) { 59.191 - lua_pushinteger(L, lua_getgccount(L)); 59.192 - return 1; 59.193 -} 59.194 - 59.195 - 59.196 -static int luaB_collectgarbage (lua_State *L) { 59.197 - static const char *const opts[] = {"stop", "restart", "collect", 59.198 - "count", "step", "setpause", "setstepmul", NULL}; 59.199 - static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, 59.200 - LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL}; 59.201 - int o = luaL_checkoption(L, 1, "collect", opts); 59.202 - int ex = luaL_optint(L, 2, 0); 59.203 - int res = lua_gc(L, optsnum[o], ex); 59.204 - switch (optsnum[o]) { 59.205 - case LUA_GCCOUNT: { 59.206 - int b = lua_gc(L, LUA_GCCOUNTB, 0); 59.207 - lua_pushnumber(L, res + ((lua_Number)b/1024)); 59.208 - return 1; 59.209 - } 59.210 - case LUA_GCSTEP: { 59.211 - lua_pushboolean(L, res); 59.212 - return 1; 59.213 - } 59.214 - default: { 59.215 - lua_pushnumber(L, res); 59.216 - return 1; 59.217 - } 59.218 - } 59.219 -} 59.220 - 59.221 - 59.222 -static int luaB_type (lua_State *L) { 59.223 - luaL_checkany(L, 1); 59.224 - lua_pushstring(L, luaL_typename(L, 1)); 59.225 - return 1; 59.226 -} 59.227 - 59.228 - 59.229 -static int luaB_next (lua_State *L) { 59.230 - luaL_checktype(L, 1, LUA_TTABLE); 59.231 - lua_settop(L, 2); /* create a 2nd argument if there isn't one */ 59.232 - if (lua_next(L, 1)) 59.233 - return 2; 59.234 - else { 59.235 - lua_pushnil(L); 59.236 - return 1; 59.237 - } 59.238 -} 59.239 - 59.240 - 59.241 -static int luaB_pairs (lua_State *L) { 59.242 - luaL_checktype(L, 1, LUA_TTABLE); 59.243 - lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ 59.244 - lua_pushvalue(L, 1); /* state, */ 59.245 - lua_pushnil(L); /* and initial value */ 59.246 - return 3; 59.247 -} 59.248 - 59.249 - 59.250 -static int ipairsaux (lua_State *L) { 59.251 - int i = luaL_checkint(L, 2); 59.252 - luaL_checktype(L, 1, LUA_TTABLE); 59.253 - i++; /* next value */ 59.254 - lua_pushinteger(L, i); 59.255 - lua_rawgeti(L, 1, i); 59.256 - return (lua_isnil(L, -1)) ? 0 : 2; 59.257 -} 59.258 - 59.259 - 59.260 -static int luaB_ipairs (lua_State *L) { 59.261 - luaL_checktype(L, 1, LUA_TTABLE); 59.262 - lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ 59.263 - lua_pushvalue(L, 1); /* state, */ 59.264 - lua_pushinteger(L, 0); /* and initial value */ 59.265 - return 3; 59.266 -} 59.267 - 59.268 - 59.269 -static int load_aux (lua_State *L, int status) { 59.270 - if (status == 0) /* OK? */ 59.271 - return 1; 59.272 - else { 59.273 - lua_pushnil(L); 59.274 - lua_insert(L, -2); /* put before error message */ 59.275 - return 2; /* return nil plus error message */ 59.276 - } 59.277 -} 59.278 - 59.279 - 59.280 -static int luaB_loadstring (lua_State *L) { 59.281 - size_t l; 59.282 - const char *s = luaL_checklstring(L, 1, &l); 59.283 - const char *chunkname = luaL_optstring(L, 2, s); 59.284 - return load_aux(L, luaL_loadbuffer(L, s, l, chunkname)); 59.285 -} 59.286 - 59.287 - 59.288 -static int luaB_loadfile (lua_State *L) { 59.289 - const char *fname = luaL_optstring(L, 1, NULL); 59.290 - return load_aux(L, luaL_loadfile(L, fname)); 59.291 -} 59.292 - 59.293 - 59.294 -/* 59.295 -** Reader for generic `load' function: `lua_load' uses the 59.296 -** stack for internal stuff, so the reader cannot change the 59.297 -** stack top. Instead, it keeps its resulting string in a 59.298 -** reserved slot inside the stack. 59.299 -*/ 59.300 -static const char *generic_reader (lua_State *L, void *ud, size_t *size) { 59.301 - (void)ud; /* to avoid warnings */ 59.302 - luaL_checkstack(L, 2, "too many nested functions"); 59.303 - lua_pushvalue(L, 1); /* get function */ 59.304 - lua_call(L, 0, 1); /* call it */ 59.305 - if (lua_isnil(L, -1)) { 59.306 - *size = 0; 59.307 - return NULL; 59.308 - } 59.309 - else if (lua_isstring(L, -1)) { 59.310 - lua_replace(L, 3); /* save string in a reserved stack slot */ 59.311 - return lua_tolstring(L, 3, size); 59.312 - } 59.313 - else luaL_error(L, "reader function must return a string"); 59.314 - return NULL; /* to avoid warnings */ 59.315 -} 59.316 - 59.317 - 59.318 -static int luaB_load (lua_State *L) { 59.319 - int status; 59.320 - const char *cname = luaL_optstring(L, 2, "=(load)"); 59.321 - luaL_checktype(L, 1, LUA_TFUNCTION); 59.322 - lua_settop(L, 3); /* function, eventual name, plus one reserved slot */ 59.323 - status = lua_load(L, generic_reader, NULL, cname); 59.324 - return load_aux(L, status); 59.325 -} 59.326 - 59.327 - 59.328 -static int luaB_dofile (lua_State *L) { 59.329 - const char *fname = luaL_optstring(L, 1, NULL); 59.330 - int n = lua_gettop(L); 59.331 - if (luaL_loadfile(L, fname) != 0) lua_error(L); 59.332 - lua_call(L, 0, LUA_MULTRET); 59.333 - return lua_gettop(L) - n; 59.334 -} 59.335 - 59.336 - 59.337 -static int luaB_assert (lua_State *L) { 59.338 - luaL_checkany(L, 1); 59.339 - if (!lua_toboolean(L, 1)) 59.340 - return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); 59.341 - return lua_gettop(L); 59.342 -} 59.343 - 59.344 - 59.345 -static int luaB_unpack (lua_State *L) { 59.346 - int i, e, n; 59.347 - luaL_checktype(L, 1, LUA_TTABLE); 59.348 - i = luaL_optint(L, 2, 1); 59.349 - e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); 59.350 - if (i > e) return 0; /* empty range */ 59.351 - n = e - i + 1; /* number of elements */ 59.352 - if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ 59.353 - return luaL_error(L, "too many results to unpack"); 59.354 - lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ 59.355 - while (i++ < e) /* push arg[i + 1...e] */ 59.356 - lua_rawgeti(L, 1, i); 59.357 - return n; 59.358 -} 59.359 - 59.360 - 59.361 -static int luaB_select (lua_State *L) { 59.362 - int n = lua_gettop(L); 59.363 - if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { 59.364 - lua_pushinteger(L, n-1); 59.365 - return 1; 59.366 - } 59.367 - else { 59.368 - int i = luaL_checkint(L, 1); 59.369 - if (i < 0) i = n + i; 59.370 - else if (i > n) i = n; 59.371 - luaL_argcheck(L, 1 <= i, 1, "index out of range"); 59.372 - return n - i; 59.373 - } 59.374 -} 59.375 - 59.376 - 59.377 -static int luaB_pcall (lua_State *L) { 59.378 - int status; 59.379 - luaL_checkany(L, 1); 59.380 - status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0); 59.381 - lua_pushboolean(L, (status == 0)); 59.382 - lua_insert(L, 1); 59.383 - return lua_gettop(L); /* return status + all results */ 59.384 -} 59.385 - 59.386 - 59.387 -static int luaB_xpcall (lua_State *L) { 59.388 - int status; 59.389 - luaL_checkany(L, 2); 59.390 - lua_settop(L, 2); 59.391 - lua_insert(L, 1); /* put error function under function to be called */ 59.392 - status = lua_pcall(L, 0, LUA_MULTRET, 1); 59.393 - lua_pushboolean(L, (status == 0)); 59.394 - lua_replace(L, 1); 59.395 - return lua_gettop(L); /* return status + all results */ 59.396 -} 59.397 - 59.398 - 59.399 -static int luaB_tostring (lua_State *L) { 59.400 - luaL_checkany(L, 1); 59.401 - if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */ 59.402 - return 1; /* use its value */ 59.403 - switch (lua_type(L, 1)) { 59.404 - case LUA_TNUMBER: 59.405 - lua_pushstring(L, lua_tostring(L, 1)); 59.406 - break; 59.407 - case LUA_TSTRING: 59.408 - lua_pushvalue(L, 1); 59.409 - break; 59.410 - case LUA_TBOOLEAN: 59.411 - lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); 59.412 - break; 59.413 - case LUA_TNIL: 59.414 - lua_pushliteral(L, "nil"); 59.415 - break; 59.416 - default: 59.417 - lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1)); 59.418 - break; 59.419 - } 59.420 - return 1; 59.421 -} 59.422 - 59.423 - 59.424 -static int luaB_newproxy (lua_State *L) { 59.425 - lua_settop(L, 1); 59.426 - lua_newuserdata(L, 0); /* create proxy */ 59.427 - if (lua_toboolean(L, 1) == 0) 59.428 - return 1; /* no metatable */ 59.429 - else if (lua_isboolean(L, 1)) { 59.430 - lua_newtable(L); /* create a new metatable `m' ... */ 59.431 - lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */ 59.432 - lua_pushboolean(L, 1); 59.433 - lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */ 59.434 - } 59.435 - else { 59.436 - int validproxy = 0; /* to check if weaktable[metatable(u)] == true */ 59.437 - if (lua_getmetatable(L, 1)) { 59.438 - lua_rawget(L, lua_upvalueindex(1)); 59.439 - validproxy = lua_toboolean(L, -1); 59.440 - lua_pop(L, 1); /* remove value */ 59.441 - } 59.442 - luaL_argcheck(L, validproxy, 1, "boolean or proxy expected"); 59.443 - lua_getmetatable(L, 1); /* metatable is valid; get it */ 59.444 - } 59.445 - lua_setmetatable(L, 2); 59.446 - return 1; 59.447 -} 59.448 - 59.449 - 59.450 -static const luaL_Reg base_funcs[] = { 59.451 - {"assert", luaB_assert}, 59.452 - {"collectgarbage", luaB_collectgarbage}, 59.453 - {"dofile", luaB_dofile}, 59.454 - {"error", luaB_error}, 59.455 - {"gcinfo", luaB_gcinfo}, 59.456 - {"getfenv", luaB_getfenv}, 59.457 - {"getmetatable", luaB_getmetatable}, 59.458 - {"loadfile", luaB_loadfile}, 59.459 - {"load", luaB_load}, 59.460 - {"loadstring", luaB_loadstring}, 59.461 - {"next", luaB_next}, 59.462 - {"pcall", luaB_pcall}, 59.463 - {"print", luaB_print}, 59.464 - {"rawequal", luaB_rawequal}, 59.465 - {"rawget", luaB_rawget}, 59.466 - {"rawset", luaB_rawset}, 59.467 - {"select", luaB_select}, 59.468 - {"setfenv", luaB_setfenv}, 59.469 - {"setmetatable", luaB_setmetatable}, 59.470 - {"tonumber", luaB_tonumber}, 59.471 - {"tostring", luaB_tostring}, 59.472 - {"type", luaB_type}, 59.473 - {"unpack", luaB_unpack}, 59.474 - {"xpcall", luaB_xpcall}, 59.475 - {NULL, NULL} 59.476 -}; 59.477 - 59.478 - 59.479 -/* 59.480 -** {====================================================== 59.481 -** Coroutine library 59.482 -** ======================================================= 59.483 -*/ 59.484 - 59.485 -#define CO_RUN 0 /* running */ 59.486 -#define CO_SUS 1 /* suspended */ 59.487 -#define CO_NOR 2 /* 'normal' (it resumed another coroutine) */ 59.488 -#define CO_DEAD 3 59.489 - 59.490 -static const char *const statnames[] = 59.491 - {"running", "suspended", "normal", "dead"}; 59.492 - 59.493 -static int costatus (lua_State *L, lua_State *co) { 59.494 - if (L == co) return CO_RUN; 59.495 - switch (lua_status(co)) { 59.496 - case LUA_YIELD: 59.497 - return CO_SUS; 59.498 - case 0: { 59.499 - lua_Debug ar; 59.500 - if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 59.501 - return CO_NOR; /* it is running */ 59.502 - else if (lua_gettop(co) == 0) 59.503 - return CO_DEAD; 59.504 - else 59.505 - return CO_SUS; /* initial state */ 59.506 - } 59.507 - default: /* some error occured */ 59.508 - return CO_DEAD; 59.509 - } 59.510 -} 59.511 - 59.512 - 59.513 -static int luaB_costatus (lua_State *L) { 59.514 - lua_State *co = lua_tothread(L, 1); 59.515 - luaL_argcheck(L, co, 1, "coroutine expected"); 59.516 - lua_pushstring(L, statnames[costatus(L, co)]); 59.517 - return 1; 59.518 -} 59.519 - 59.520 - 59.521 -static int auxresume (lua_State *L, lua_State *co, int narg) { 59.522 - int status = costatus(L, co); 59.523 - if (!lua_checkstack(co, narg)) 59.524 - luaL_error(L, "too many arguments to resume"); 59.525 - if (status != CO_SUS) { 59.526 - lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]); 59.527 - return -1; /* error flag */ 59.528 - } 59.529 - lua_xmove(L, co, narg); 59.530 - lua_setlevel(L, co); 59.531 - status = lua_resume(co, narg); 59.532 - if (status == 0 || status == LUA_YIELD) { 59.533 - int nres = lua_gettop(co); 59.534 - if (!lua_checkstack(L, nres + 1)) 59.535 - luaL_error(L, "too many results to resume"); 59.536 - lua_xmove(co, L, nres); /* move yielded values */ 59.537 - return nres; 59.538 - } 59.539 - else { 59.540 - lua_xmove(co, L, 1); /* move error message */ 59.541 - return -1; /* error flag */ 59.542 - } 59.543 -} 59.544 - 59.545 - 59.546 -static int luaB_coresume (lua_State *L) { 59.547 - lua_State *co = lua_tothread(L, 1); 59.548 - int r; 59.549 - luaL_argcheck(L, co, 1, "coroutine expected"); 59.550 - r = auxresume(L, co, lua_gettop(L) - 1); 59.551 - if (r < 0) { 59.552 - lua_pushboolean(L, 0); 59.553 - lua_insert(L, -2); 59.554 - return 2; /* return false + error message */ 59.555 - } 59.556 - else { 59.557 - lua_pushboolean(L, 1); 59.558 - lua_insert(L, -(r + 1)); 59.559 - return r + 1; /* return true + `resume' returns */ 59.560 - } 59.561 -} 59.562 - 59.563 - 59.564 -static int luaB_auxwrap (lua_State *L) { 59.565 - lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 59.566 - int r = auxresume(L, co, lua_gettop(L)); 59.567 - if (r < 0) { 59.568 - if (lua_isstring(L, -1)) { /* error object is a string? */ 59.569 - luaL_where(L, 1); /* add extra info */ 59.570 - lua_insert(L, -2); 59.571 - lua_concat(L, 2); 59.572 - } 59.573 - lua_error(L); /* propagate error */ 59.574 - } 59.575 - return r; 59.576 -} 59.577 - 59.578 - 59.579 -static int luaB_cocreate (lua_State *L) { 59.580 - lua_State *NL = lua_newthread(L); 59.581 - luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, 59.582 - "Lua function expected"); 59.583 - lua_pushvalue(L, 1); /* move function to top */ 59.584 - lua_xmove(L, NL, 1); /* move function from L to NL */ 59.585 - return 1; 59.586 -} 59.587 - 59.588 - 59.589 -static int luaB_cowrap (lua_State *L) { 59.590 - luaB_cocreate(L); 59.591 - lua_pushcclosure(L, luaB_auxwrap, 1); 59.592 - return 1; 59.593 -} 59.594 - 59.595 - 59.596 -static int luaB_yield (lua_State *L) { 59.597 - return lua_yield(L, lua_gettop(L)); 59.598 -} 59.599 - 59.600 - 59.601 -static int luaB_corunning (lua_State *L) { 59.602 - if (lua_pushthread(L)) 59.603 - lua_pushnil(L); /* main thread is not a coroutine */ 59.604 - return 1; 59.605 -} 59.606 - 59.607 - 59.608 -static const luaL_Reg co_funcs[] = { 59.609 - {"create", luaB_cocreate}, 59.610 - {"resume", luaB_coresume}, 59.611 - {"running", luaB_corunning}, 59.612 - {"status", luaB_costatus}, 59.613 - {"wrap", luaB_cowrap}, 59.614 - {"yield", luaB_yield}, 59.615 - {NULL, NULL} 59.616 -}; 59.617 - 59.618 -/* }====================================================== */ 59.619 - 59.620 - 59.621 -static void auxopen (lua_State *L, const char *name, 59.622 - lua_CFunction f, lua_CFunction u) { 59.623 - lua_pushcfunction(L, u); 59.624 - lua_pushcclosure(L, f, 1); 59.625 - lua_setfield(L, -2, name); 59.626 -} 59.627 - 59.628 - 59.629 -static void base_open (lua_State *L) { 59.630 - /* set global _G */ 59.631 - lua_pushvalue(L, LUA_GLOBALSINDEX); 59.632 - lua_setglobal(L, "_G"); 59.633 - /* open lib into global table */ 59.634 - luaL_register(L, "_G", base_funcs); 59.635 - lua_pushliteral(L, LUA_VERSION); 59.636 - lua_setglobal(L, "_VERSION"); /* set global _VERSION */ 59.637 - /* `ipairs' and `pairs' need auxliliary functions as upvalues */ 59.638 - auxopen(L, "ipairs", luaB_ipairs, ipairsaux); 59.639 - auxopen(L, "pairs", luaB_pairs, luaB_next); 59.640 - /* `newproxy' needs a weaktable as upvalue */ 59.641 - lua_createtable(L, 0, 1); /* new table `w' */ 59.642 - lua_pushvalue(L, -1); /* `w' will be its own metatable */ 59.643 - lua_setmetatable(L, -2); 59.644 - lua_pushliteral(L, "kv"); 59.645 - lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ 59.646 - lua_pushcclosure(L, luaB_newproxy, 1); 59.647 - lua_setglobal(L, "newproxy"); /* set global `newproxy' */ 59.648 -} 59.649 - 59.650 - 59.651 -LUALIB_API int luaopen_base (lua_State *L) { 59.652 - base_open(L); 59.653 - luaL_register(L, LUA_COLIBNAME, co_funcs); 59.654 - return 2; 59.655 -} 59.656 -
60.1 --- a/src/lua/src/lcode.c Sat Mar 03 11:04:56 2012 -0600 60.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 60.3 @@ -1,839 +0,0 @@ 60.4 -/* 60.5 -** $Id: lcode.c,v 2.25.1.3 2007/12/28 15:32:23 roberto Exp $ 60.6 -** Code generator for Lua 60.7 -** See Copyright Notice in lua.h 60.8 -*/ 60.9 - 60.10 - 60.11 -#include <stdlib.h> 60.12 - 60.13 -#define lcode_c 60.14 -#define LUA_CORE 60.15 - 60.16 -#include "lua.h" 60.17 - 60.18 -#include "lcode.h" 60.19 -#include "ldebug.h" 60.20 -#include "ldo.h" 60.21 -#include "lgc.h" 60.22 -#include "llex.h" 60.23 -#include "lmem.h" 60.24 -#include "lobject.h" 60.25 -#include "lopcodes.h" 60.26 -#include "lparser.h" 60.27 -#include "ltable.h" 60.28 - 60.29 - 60.30 -#define hasjumps(e) ((e)->t != (e)->f) 60.31 - 60.32 - 60.33 -static int isnumeral(expdesc *e) { 60.34 - return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP); 60.35 -} 60.36 - 60.37 - 60.38 -void luaK_nil (FuncState *fs, int from, int n) { 60.39 - Instruction *previous; 60.40 - if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ 60.41 - if (fs->pc == 0) { /* function start? */ 60.42 - if (from >= fs->nactvar) 60.43 - return; /* positions are already clean */ 60.44 - } 60.45 - else { 60.46 - previous = &fs->f->code[fs->pc-1]; 60.47 - if (GET_OPCODE(*previous) == OP_LOADNIL) { 60.48 - int pfrom = GETARG_A(*previous); 60.49 - int pto = GETARG_B(*previous); 60.50 - if (pfrom <= from && from <= pto+1) { /* can connect both? */ 60.51 - if (from+n-1 > pto) 60.52 - SETARG_B(*previous, from+n-1); 60.53 - return; 60.54 - } 60.55 - } 60.56 - } 60.57 - } 60.58 - luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */ 60.59 -} 60.60 - 60.61 - 60.62 -int luaK_jump (FuncState *fs) { 60.63 - int jpc = fs->jpc; /* save list of jumps to here */ 60.64 - int j; 60.65 - fs->jpc = NO_JUMP; 60.66 - j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); 60.67 - luaK_concat(fs, &j, jpc); /* keep them on hold */ 60.68 - return j; 60.69 -} 60.70 - 60.71 - 60.72 -void luaK_ret (FuncState *fs, int first, int nret) { 60.73 - luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); 60.74 -} 60.75 - 60.76 - 60.77 -static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { 60.78 - luaK_codeABC(fs, op, A, B, C); 60.79 - return luaK_jump(fs); 60.80 -} 60.81 - 60.82 - 60.83 -static void fixjump (FuncState *fs, int pc, int dest) { 60.84 - Instruction *jmp = &fs->f->code[pc]; 60.85 - int offset = dest-(pc+1); 60.86 - lua_assert(dest != NO_JUMP); 60.87 - if (abs(offset) > MAXARG_sBx) 60.88 - luaX_syntaxerror(fs->ls, "control structure too long"); 60.89 - SETARG_sBx(*jmp, offset); 60.90 -} 60.91 - 60.92 - 60.93 -/* 60.94 -** returns current `pc' and marks it as a jump target (to avoid wrong 60.95 -** optimizations with consecutive instructions not in the same basic block). 60.96 -*/ 60.97 -int luaK_getlabel (FuncState *fs) { 60.98 - fs->lasttarget = fs->pc; 60.99 - return fs->pc; 60.100 -} 60.101 - 60.102 - 60.103 -static int getjump (FuncState *fs, int pc) { 60.104 - int offset = GETARG_sBx(fs->f->code[pc]); 60.105 - if (offset == NO_JUMP) /* point to itself represents end of list */ 60.106 - return NO_JUMP; /* end of list */ 60.107 - else 60.108 - return (pc+1)+offset; /* turn offset into absolute position */ 60.109 -} 60.110 - 60.111 - 60.112 -static Instruction *getjumpcontrol (FuncState *fs, int pc) { 60.113 - Instruction *pi = &fs->f->code[pc]; 60.114 - if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) 60.115 - return pi-1; 60.116 - else 60.117 - return pi; 60.118 -} 60.119 - 60.120 - 60.121 -/* 60.122 -** check whether list has any jump that do not produce a value 60.123 -** (or produce an inverted value) 60.124 -*/ 60.125 -static int need_value (FuncState *fs, int list) { 60.126 - for (; list != NO_JUMP; list = getjump(fs, list)) { 60.127 - Instruction i = *getjumpcontrol(fs, list); 60.128 - if (GET_OPCODE(i) != OP_TESTSET) return 1; 60.129 - } 60.130 - return 0; /* not found */ 60.131 -} 60.132 - 60.133 - 60.134 -static int patchtestreg (FuncState *fs, int node, int reg) { 60.135 - Instruction *i = getjumpcontrol(fs, node); 60.136 - if (GET_OPCODE(*i) != OP_TESTSET) 60.137 - return 0; /* cannot patch other instructions */ 60.138 - if (reg != NO_REG && reg != GETARG_B(*i)) 60.139 - SETARG_A(*i, reg); 60.140 - else /* no register to put value or register already has the value */ 60.141 - *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); 60.142 - 60.143 - return 1; 60.144 -} 60.145 - 60.146 - 60.147 -static void removevalues (FuncState *fs, int list) { 60.148 - for (; list != NO_JUMP; list = getjump(fs, list)) 60.149 - patchtestreg(fs, list, NO_REG); 60.150 -} 60.151 - 60.152 - 60.153 -static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, 60.154 - int dtarget) { 60.155 - while (list != NO_JUMP) { 60.156 - int next = getjump(fs, list); 60.157 - if (patchtestreg(fs, list, reg)) 60.158 - fixjump(fs, list, vtarget); 60.159 - else 60.160 - fixjump(fs, list, dtarget); /* jump to default target */ 60.161 - list = next; 60.162 - } 60.163 -} 60.164 - 60.165 - 60.166 -static void dischargejpc (FuncState *fs) { 60.167 - patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); 60.168 - fs->jpc = NO_JUMP; 60.169 -} 60.170 - 60.171 - 60.172 -void luaK_patchlist (FuncState *fs, int list, int target) { 60.173 - if (target == fs->pc) 60.174 - luaK_patchtohere(fs, list); 60.175 - else { 60.176 - lua_assert(target < fs->pc); 60.177 - patchlistaux(fs, list, target, NO_REG, target); 60.178 - } 60.179 -} 60.180 - 60.181 - 60.182 -void luaK_patchtohere (FuncState *fs, int list) { 60.183 - luaK_getlabel(fs); 60.184 - luaK_concat(fs, &fs->jpc, list); 60.185 -} 60.186 - 60.187 - 60.188 -void luaK_concat (FuncState *fs, int *l1, int l2) { 60.189 - if (l2 == NO_JUMP) return; 60.190 - else if (*l1 == NO_JUMP) 60.191 - *l1 = l2; 60.192 - else { 60.193 - int list = *l1; 60.194 - int next; 60.195 - while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ 60.196 - list = next; 60.197 - fixjump(fs, list, l2); 60.198 - } 60.199 -} 60.200 - 60.201 - 60.202 -void luaK_checkstack (FuncState *fs, int n) { 60.203 - int newstack = fs->freereg + n; 60.204 - if (newstack > fs->f->maxstacksize) { 60.205 - if (newstack >= MAXSTACK) 60.206 - luaX_syntaxerror(fs->ls, "function or expression too complex"); 60.207 - fs->f->maxstacksize = cast_byte(newstack); 60.208 - } 60.209 -} 60.210 - 60.211 - 60.212 -void luaK_reserveregs (FuncState *fs, int n) { 60.213 - luaK_checkstack(fs, n); 60.214 - fs->freereg += n; 60.215 -} 60.216 - 60.217 - 60.218 -static void freereg (FuncState *fs, int reg) { 60.219 - if (!ISK(reg) && reg >= fs->nactvar) { 60.220 - fs->freereg--; 60.221 - lua_assert(reg == fs->freereg); 60.222 - } 60.223 -} 60.224 - 60.225 - 60.226 -static void freeexp (FuncState *fs, expdesc *e) { 60.227 - if (e->k == VNONRELOC) 60.228 - freereg(fs, e->u.s.info); 60.229 -} 60.230 - 60.231 - 60.232 -static int addk (FuncState *fs, TValue *k, TValue *v) { 60.233 - lua_State *L = fs->L; 60.234 - TValue *idx = luaH_set(L, fs->h, k); 60.235 - Proto *f = fs->f; 60.236 - int oldsize = f->sizek; 60.237 - if (ttisnumber(idx)) { 60.238 - lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v)); 60.239 - return cast_int(nvalue(idx)); 60.240 - } 60.241 - else { /* constant not found; create a new entry */ 60.242 - setnvalue(idx, cast_num(fs->nk)); 60.243 - luaM_growvector(L, f->k, fs->nk, f->sizek, TValue, 60.244 - MAXARG_Bx, "constant table overflow"); 60.245 - while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); 60.246 - setobj(L, &f->k[fs->nk], v); 60.247 - luaC_barrier(L, f, v); 60.248 - return fs->nk++; 60.249 - } 60.250 -} 60.251 - 60.252 - 60.253 -int luaK_stringK (FuncState *fs, TString *s) { 60.254 - TValue o; 60.255 - setsvalue(fs->L, &o, s); 60.256 - return addk(fs, &o, &o); 60.257 -} 60.258 - 60.259 - 60.260 -int luaK_numberK (FuncState *fs, lua_Number r) { 60.261 - TValue o; 60.262 - setnvalue(&o, r); 60.263 - return addk(fs, &o, &o); 60.264 -} 60.265 - 60.266 - 60.267 -static int boolK (FuncState *fs, int b) { 60.268 - TValue o; 60.269 - setbvalue(&o, b); 60.270 - return addk(fs, &o, &o); 60.271 -} 60.272 - 60.273 - 60.274 -static int nilK (FuncState *fs) { 60.275 - TValue k, v; 60.276 - setnilvalue(&v); 60.277 - /* cannot use nil as key; instead use table itself to represent nil */ 60.278 - sethvalue(fs->L, &k, fs->h); 60.279 - return addk(fs, &k, &v); 60.280 -} 60.281 - 60.282 - 60.283 -void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { 60.284 - if (e->k == VCALL) { /* expression is an open function call? */ 60.285 - SETARG_C(getcode(fs, e), nresults+1); 60.286 - } 60.287 - else if (e->k == VVARARG) { 60.288 - SETARG_B(getcode(fs, e), nresults+1); 60.289 - SETARG_A(getcode(fs, e), fs->freereg); 60.290 - luaK_reserveregs(fs, 1); 60.291 - } 60.292 -} 60.293 - 60.294 - 60.295 -void luaK_setoneret (FuncState *fs, expdesc *e) { 60.296 - if (e->k == VCALL) { /* expression is an open function call? */ 60.297 - e->k = VNONRELOC; 60.298 - e->u.s.info = GETARG_A(getcode(fs, e)); 60.299 - } 60.300 - else if (e->k == VVARARG) { 60.301 - SETARG_B(getcode(fs, e), 2); 60.302 - e->k = VRELOCABLE; /* can relocate its simple result */ 60.303 - } 60.304 -} 60.305 - 60.306 - 60.307 -void luaK_dischargevars (FuncState *fs, expdesc *e) { 60.308 - switch (e->k) { 60.309 - case VLOCAL: { 60.310 - e->k = VNONRELOC; 60.311 - break; 60.312 - } 60.313 - case VUPVAL: { 60.314 - e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0); 60.315 - e->k = VRELOCABLE; 60.316 - break; 60.317 - } 60.318 - case VGLOBAL: { 60.319 - e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info); 60.320 - e->k = VRELOCABLE; 60.321 - break; 60.322 - } 60.323 - case VINDEXED: { 60.324 - freereg(fs, e->u.s.aux); 60.325 - freereg(fs, e->u.s.info); 60.326 - e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux); 60.327 - e->k = VRELOCABLE; 60.328 - break; 60.329 - } 60.330 - case VVARARG: 60.331 - case VCALL: { 60.332 - luaK_setoneret(fs, e); 60.333 - break; 60.334 - } 60.335 - default: break; /* there is one value available (somewhere) */ 60.336 - } 60.337 -} 60.338 - 60.339 - 60.340 -static int code_label (FuncState *fs, int A, int b, int jump) { 60.341 - luaK_getlabel(fs); /* those instructions may be jump targets */ 60.342 - return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); 60.343 -} 60.344 - 60.345 - 60.346 -static void discharge2reg (FuncState *fs, expdesc *e, int reg) { 60.347 - luaK_dischargevars(fs, e); 60.348 - switch (e->k) { 60.349 - case VNIL: { 60.350 - luaK_nil(fs, reg, 1); 60.351 - break; 60.352 - } 60.353 - case VFALSE: case VTRUE: { 60.354 - luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); 60.355 - break; 60.356 - } 60.357 - case VK: { 60.358 - luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info); 60.359 - break; 60.360 - } 60.361 - case VKNUM: { 60.362 - luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval)); 60.363 - break; 60.364 - } 60.365 - case VRELOCABLE: { 60.366 - Instruction *pc = &getcode(fs, e); 60.367 - SETARG_A(*pc, reg); 60.368 - break; 60.369 - } 60.370 - case VNONRELOC: { 60.371 - if (reg != e->u.s.info) 60.372 - luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0); 60.373 - break; 60.374 - } 60.375 - default: { 60.376 - lua_assert(e->k == VVOID || e->k == VJMP); 60.377 - return; /* nothing to do... */ 60.378 - } 60.379 - } 60.380 - e->u.s.info = reg; 60.381 - e->k = VNONRELOC; 60.382 -} 60.383 - 60.384 - 60.385 -static void discharge2anyreg (FuncState *fs, expdesc *e) { 60.386 - if (e->k != VNONRELOC) { 60.387 - luaK_reserveregs(fs, 1); 60.388 - discharge2reg(fs, e, fs->freereg-1); 60.389 - } 60.390 -} 60.391 - 60.392 - 60.393 -static void exp2reg (FuncState *fs, expdesc *e, int reg) { 60.394 - discharge2reg(fs, e, reg); 60.395 - if (e->k == VJMP) 60.396 - luaK_concat(fs, &e->t, e->u.s.info); /* put this jump in `t' list */ 60.397 - if (hasjumps(e)) { 60.398 - int final; /* position after whole expression */ 60.399 - int p_f = NO_JUMP; /* position of an eventual LOAD false */ 60.400 - int p_t = NO_JUMP; /* position of an eventual LOAD true */ 60.401 - if (need_value(fs, e->t) || need_value(fs, e->f)) { 60.402 - int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); 60.403 - p_f = code_label(fs, reg, 0, 1); 60.404 - p_t = code_label(fs, reg, 1, 0); 60.405 - luaK_patchtohere(fs, fj); 60.406 - } 60.407 - final = luaK_getlabel(fs); 60.408 - patchlistaux(fs, e->f, final, reg, p_f); 60.409 - patchlistaux(fs, e->t, final, reg, p_t); 60.410 - } 60.411 - e->f = e->t = NO_JUMP; 60.412 - e->u.s.info = reg; 60.413 - e->k = VNONRELOC; 60.414 -} 60.415 - 60.416 - 60.417 -void luaK_exp2nextreg (FuncState *fs, expdesc *e) { 60.418 - luaK_dischargevars(fs, e); 60.419 - freeexp(fs, e); 60.420 - luaK_reserveregs(fs, 1); 60.421 - exp2reg(fs, e, fs->freereg - 1); 60.422 -} 60.423 - 60.424 - 60.425 -int luaK_exp2anyreg (FuncState *fs, expdesc *e) { 60.426 - luaK_dischargevars(fs, e); 60.427 - if (e->k == VNONRELOC) { 60.428 - if (!hasjumps(e)) return e->u.s.info; /* exp is already in a register */ 60.429 - if (e->u.s.info >= fs->nactvar) { /* reg. is not a local? */ 60.430 - exp2reg(fs, e, e->u.s.info); /* put value on it */ 60.431 - return e->u.s.info; 60.432 - } 60.433 - } 60.434 - luaK_exp2nextreg(fs, e); /* default */ 60.435 - return e->u.s.info; 60.436 -} 60.437 - 60.438 - 60.439 -void luaK_exp2val (FuncState *fs, expdesc *e) { 60.440 - if (hasjumps(e)) 60.441 - luaK_exp2anyreg(fs, e); 60.442 - else 60.443 - luaK_dischargevars(fs, e); 60.444 -} 60.445 - 60.446 - 60.447 -int luaK_exp2RK (FuncState *fs, expdesc *e) { 60.448 - luaK_exp2val(fs, e); 60.449 - switch (e->k) { 60.450 - case VKNUM: 60.451 - case VTRUE: 60.452 - case VFALSE: 60.453 - case VNIL: { 60.454 - if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */ 60.455 - e->u.s.info = (e->k == VNIL) ? nilK(fs) : 60.456 - (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) : 60.457 - boolK(fs, (e->k == VTRUE)); 60.458 - e->k = VK; 60.459 - return RKASK(e->u.s.info); 60.460 - } 60.461 - else break; 60.462 - } 60.463 - case VK: { 60.464 - if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */ 60.465 - return RKASK(e->u.s.info); 60.466 - else break; 60.467 - } 60.468 - default: break; 60.469 - } 60.470 - /* not a constant in the right range: put it in a register */ 60.471 - return luaK_exp2anyreg(fs, e); 60.472 -} 60.473 - 60.474 - 60.475 -void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { 60.476 - switch (var->k) { 60.477 - case VLOCAL: { 60.478 - freeexp(fs, ex); 60.479 - exp2reg(fs, ex, var->u.s.info); 60.480 - return; 60.481 - } 60.482 - case VUPVAL: { 60.483 - int e = luaK_exp2anyreg(fs, ex); 60.484 - luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0); 60.485 - break; 60.486 - } 60.487 - case VGLOBAL: { 60.488 - int e = luaK_exp2anyreg(fs, ex); 60.489 - luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info); 60.490 - break; 60.491 - } 60.492 - case VINDEXED: { 60.493 - int e = luaK_exp2RK(fs, ex); 60.494 - luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e); 60.495 - break; 60.496 - } 60.497 - default: { 60.498 - lua_assert(0); /* invalid var kind to store */ 60.499 - break; 60.500 - } 60.501 - } 60.502 - freeexp(fs, ex); 60.503 -} 60.504 - 60.505 - 60.506 -void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { 60.507 - int func; 60.508 - luaK_exp2anyreg(fs, e); 60.509 - freeexp(fs, e); 60.510 - func = fs->freereg; 60.511 - luaK_reserveregs(fs, 2); 60.512 - luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key)); 60.513 - freeexp(fs, key); 60.514 - e->u.s.info = func; 60.515 - e->k = VNONRELOC; 60.516 -} 60.517 - 60.518 - 60.519 -static void invertjump (FuncState *fs, expdesc *e) { 60.520 - Instruction *pc = getjumpcontrol(fs, e->u.s.info); 60.521 - lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && 60.522 - GET_OPCODE(*pc) != OP_TEST); 60.523 - SETARG_A(*pc, !(GETARG_A(*pc))); 60.524 -} 60.525 - 60.526 - 60.527 -static int jumponcond (FuncState *fs, expdesc *e, int cond) { 60.528 - if (e->k == VRELOCABLE) { 60.529 - Instruction ie = getcode(fs, e); 60.530 - if (GET_OPCODE(ie) == OP_NOT) { 60.531 - fs->pc--; /* remove previous OP_NOT */ 60.532 - return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); 60.533 - } 60.534 - /* else go through */ 60.535 - } 60.536 - discharge2anyreg(fs, e); 60.537 - freeexp(fs, e); 60.538 - return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond); 60.539 -} 60.540 - 60.541 - 60.542 -void luaK_goiftrue (FuncState *fs, expdesc *e) { 60.543 - int pc; /* pc of last jump */ 60.544 - luaK_dischargevars(fs, e); 60.545 - switch (e->k) { 60.546 - case VK: case VKNUM: case VTRUE: { 60.547 - pc = NO_JUMP; /* always true; do nothing */ 60.548 - break; 60.549 - } 60.550 - case VFALSE: { 60.551 - pc = luaK_jump(fs); /* always jump */ 60.552 - break; 60.553 - } 60.554 - case VJMP: { 60.555 - invertjump(fs, e); 60.556 - pc = e->u.s.info; 60.557 - break; 60.558 - } 60.559 - default: { 60.560 - pc = jumponcond(fs, e, 0); 60.561 - break; 60.562 - } 60.563 - } 60.564 - luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */ 60.565 - luaK_patchtohere(fs, e->t); 60.566 - e->t = NO_JUMP; 60.567 -} 60.568 - 60.569 - 60.570 -static void luaK_goiffalse (FuncState *fs, expdesc *e) { 60.571 - int pc; /* pc of last jump */ 60.572 - luaK_dischargevars(fs, e); 60.573 - switch (e->k) { 60.574 - case VNIL: case VFALSE: { 60.575 - pc = NO_JUMP; /* always false; do nothing */ 60.576 - break; 60.577 - } 60.578 - case VTRUE: { 60.579 - pc = luaK_jump(fs); /* always jump */ 60.580 - break; 60.581 - } 60.582 - case VJMP: { 60.583 - pc = e->u.s.info; 60.584 - break; 60.585 - } 60.586 - default: { 60.587 - pc = jumponcond(fs, e, 1); 60.588 - break; 60.589 - } 60.590 - } 60.591 - luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */ 60.592 - luaK_patchtohere(fs, e->f); 60.593 - e->f = NO_JUMP; 60.594 -} 60.595 - 60.596 - 60.597 -static void codenot (FuncState *fs, expdesc *e) { 60.598 - luaK_dischargevars(fs, e); 60.599 - switch (e->k) { 60.600 - case VNIL: case VFALSE: { 60.601 - e->k = VTRUE; 60.602 - break; 60.603 - } 60.604 - case VK: case VKNUM: case VTRUE: { 60.605 - e->k = VFALSE; 60.606 - break; 60.607 - } 60.608 - case VJMP: { 60.609 - invertjump(fs, e); 60.610 - break; 60.611 - } 60.612 - case VRELOCABLE: 60.613 - case VNONRELOC: { 60.614 - discharge2anyreg(fs, e); 60.615 - freeexp(fs, e); 60.616 - e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0); 60.617 - e->k = VRELOCABLE; 60.618 - break; 60.619 - } 60.620 - default: { 60.621 - lua_assert(0); /* cannot happen */ 60.622 - break; 60.623 - } 60.624 - } 60.625 - /* interchange true and false lists */ 60.626 - { int temp = e->f; e->f = e->t; e->t = temp; } 60.627 - removevalues(fs, e->f); 60.628 - removevalues(fs, e->t); 60.629 -} 60.630 - 60.631 - 60.632 -void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { 60.633 - t->u.s.aux = luaK_exp2RK(fs, k); 60.634 - t->k = VINDEXED; 60.635 -} 60.636 - 60.637 - 60.638 -static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { 60.639 - lua_Number v1, v2, r; 60.640 - if (!isnumeral(e1) || !isnumeral(e2)) return 0; 60.641 - v1 = e1->u.nval; 60.642 - v2 = e2->u.nval; 60.643 - switch (op) { 60.644 - case OP_ADD: r = luai_numadd(v1, v2); break; 60.645 - case OP_SUB: r = luai_numsub(v1, v2); break; 60.646 - case OP_MUL: r = luai_nummul(v1, v2); break; 60.647 - case OP_DIV: 60.648 - if (v2 == 0) return 0; /* do not attempt to divide by 0 */ 60.649 - r = luai_numdiv(v1, v2); break; 60.650 - case OP_MOD: 60.651 - if (v2 == 0) return 0; /* do not attempt to divide by 0 */ 60.652 - r = luai_nummod(v1, v2); break; 60.653 - case OP_POW: r = luai_numpow(v1, v2); break; 60.654 - case OP_UNM: r = luai_numunm(v1); break; 60.655 - case OP_LEN: return 0; /* no constant folding for 'len' */ 60.656 - default: lua_assert(0); r = 0; break; 60.657 - } 60.658 - if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */ 60.659 - e1->u.nval = r; 60.660 - return 1; 60.661 -} 60.662 - 60.663 - 60.664 -static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { 60.665 - if (constfolding(op, e1, e2)) 60.666 - return; 60.667 - else { 60.668 - int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; 60.669 - int o1 = luaK_exp2RK(fs, e1); 60.670 - if (o1 > o2) { 60.671 - freeexp(fs, e1); 60.672 - freeexp(fs, e2); 60.673 - } 60.674 - else { 60.675 - freeexp(fs, e2); 60.676 - freeexp(fs, e1); 60.677 - } 60.678 - e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2); 60.679 - e1->k = VRELOCABLE; 60.680 - } 60.681 -} 60.682 - 60.683 - 60.684 -static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, 60.685 - expdesc *e2) { 60.686 - int o1 = luaK_exp2RK(fs, e1); 60.687 - int o2 = luaK_exp2RK(fs, e2); 60.688 - freeexp(fs, e2); 60.689 - freeexp(fs, e1); 60.690 - if (cond == 0 && op != OP_EQ) { 60.691 - int temp; /* exchange args to replace by `<' or `<=' */ 60.692 - temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */ 60.693 - cond = 1; 60.694 - } 60.695 - e1->u.s.info = condjump(fs, op, cond, o1, o2); 60.696 - e1->k = VJMP; 60.697 -} 60.698 - 60.699 - 60.700 -void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { 60.701 - expdesc e2; 60.702 - e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; 60.703 - switch (op) { 60.704 - case OPR_MINUS: { 60.705 - if (!isnumeral(e)) 60.706 - luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */ 60.707 - codearith(fs, OP_UNM, e, &e2); 60.708 - break; 60.709 - } 60.710 - case OPR_NOT: codenot(fs, e); break; 60.711 - case OPR_LEN: { 60.712 - luaK_exp2anyreg(fs, e); /* cannot operate on constants */ 60.713 - codearith(fs, OP_LEN, e, &e2); 60.714 - break; 60.715 - } 60.716 - default: lua_assert(0); 60.717 - } 60.718 -} 60.719 - 60.720 - 60.721 -void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { 60.722 - switch (op) { 60.723 - case OPR_AND: { 60.724 - luaK_goiftrue(fs, v); 60.725 - break; 60.726 - } 60.727 - case OPR_OR: { 60.728 - luaK_goiffalse(fs, v); 60.729 - break; 60.730 - } 60.731 - case OPR_CONCAT: { 60.732 - luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */ 60.733 - break; 60.734 - } 60.735 - case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: 60.736 - case OPR_MOD: case OPR_POW: { 60.737 - if (!isnumeral(v)) luaK_exp2RK(fs, v); 60.738 - break; 60.739 - } 60.740 - default: { 60.741 - luaK_exp2RK(fs, v); 60.742 - break; 60.743 - } 60.744 - } 60.745 -} 60.746 - 60.747 - 60.748 -void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) { 60.749 - switch (op) { 60.750 - case OPR_AND: { 60.751 - lua_assert(e1->t == NO_JUMP); /* list must be closed */ 60.752 - luaK_dischargevars(fs, e2); 60.753 - luaK_concat(fs, &e2->f, e1->f); 60.754 - *e1 = *e2; 60.755 - break; 60.756 - } 60.757 - case OPR_OR: { 60.758 - lua_assert(e1->f == NO_JUMP); /* list must be closed */ 60.759 - luaK_dischargevars(fs, e2); 60.760 - luaK_concat(fs, &e2->t, e1->t); 60.761 - *e1 = *e2; 60.762 - break; 60.763 - } 60.764 - case OPR_CONCAT: { 60.765 - luaK_exp2val(fs, e2); 60.766 - if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) { 60.767 - lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1); 60.768 - freeexp(fs, e1); 60.769 - SETARG_B(getcode(fs, e2), e1->u.s.info); 60.770 - e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info; 60.771 - } 60.772 - else { 60.773 - luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ 60.774 - codearith(fs, OP_CONCAT, e1, e2); 60.775 - } 60.776 - break; 60.777 - } 60.778 - case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break; 60.779 - case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break; 60.780 - case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break; 60.781 - case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break; 60.782 - case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break; 60.783 - case OPR_POW: codearith(fs, OP_POW, e1, e2); break; 60.784 - case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break; 60.785 - case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break; 60.786 - case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break; 60.787 - case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break; 60.788 - case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break; 60.789 - case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break; 60.790 - default: lua_assert(0); 60.791 - } 60.792 -} 60.793 - 60.794 - 60.795 -void luaK_fixline (FuncState *fs, int line) { 60.796 - fs->f->lineinfo[fs->pc - 1] = line; 60.797 -} 60.798 - 60.799 - 60.800 -static int luaK_code (FuncState *fs, Instruction i, int line) { 60.801 - Proto *f = fs->f; 60.802 - dischargejpc(fs); /* `pc' will change */ 60.803 - /* put new instruction in code array */ 60.804 - luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction, 60.805 - MAX_INT, "code size overflow"); 60.806 - f->code[fs->pc] = i; 60.807 - /* save corresponding line information */ 60.808 - luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int, 60.809 - MAX_INT, "code size overflow"); 60.810 - f->lineinfo[fs->pc] = line; 60.811 - return fs->pc++; 60.812 -} 60.813 - 60.814 - 60.815 -int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { 60.816 - lua_assert(getOpMode(o) == iABC); 60.817 - lua_assert(getBMode(o) != OpArgN || b == 0); 60.818 - lua_assert(getCMode(o) != OpArgN || c == 0); 60.819 - return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline); 60.820 -} 60.821 - 60.822 - 60.823 -int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { 60.824 - lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); 60.825 - lua_assert(getCMode(o) == OpArgN); 60.826 - return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline); 60.827 -} 60.828 - 60.829 - 60.830 -void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { 60.831 - int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; 60.832 - int b = (tostore == LUA_MULTRET) ? 0 : tostore; 60.833 - lua_assert(tostore != 0); 60.834 - if (c <= MAXARG_C) 60.835 - luaK_codeABC(fs, OP_SETLIST, base, b, c); 60.836 - else { 60.837 - luaK_codeABC(fs, OP_SETLIST, base, b, 0); 60.838 - luaK_code(fs, cast(Instruction, c), fs->ls->lastline); 60.839 - } 60.840 - fs->freereg = base + 1; /* free registers with list values */ 60.841 -} 60.842 -
61.1 --- a/src/lua/src/lcode.h Sat Mar 03 11:04:56 2012 -0600 61.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 61.3 @@ -1,76 +0,0 @@ 61.4 -/* 61.5 -** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $ 61.6 -** Code generator for Lua 61.7 -** See Copyright Notice in lua.h 61.8 -*/ 61.9 - 61.10 -#ifndef lcode_h 61.11 -#define lcode_h 61.12 - 61.13 -#include "llex.h" 61.14 -#include "lobject.h" 61.15 -#include "lopcodes.h" 61.16 -#include "lparser.h" 61.17 - 61.18 - 61.19 -/* 61.20 -** Marks the end of a patch list. It is an invalid value both as an absolute 61.21 -** address, and as a list link (would link an element to itself). 61.22 -*/ 61.23 -#define NO_JUMP (-1) 61.24 - 61.25 - 61.26 -/* 61.27 -** grep "ORDER OPR" if you change these enums 61.28 -*/ 61.29 -typedef enum BinOpr { 61.30 - OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 61.31 - OPR_CONCAT, 61.32 - OPR_NE, OPR_EQ, 61.33 - OPR_LT, OPR_LE, OPR_GT, OPR_GE, 61.34 - OPR_AND, OPR_OR, 61.35 - OPR_NOBINOPR 61.36 -} BinOpr; 61.37 - 61.38 - 61.39 -typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 61.40 - 61.41 - 61.42 -#define getcode(fs,e) ((fs)->f->code[(e)->u.s.info]) 61.43 - 61.44 -#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 61.45 - 61.46 -#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 61.47 - 61.48 -LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 61.49 -LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 61.50 -LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 61.51 -LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 61.52 -LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 61.53 -LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 61.54 -LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 61.55 -LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 61.56 -LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 61.57 -LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 61.58 -LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 61.59 -LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 61.60 -LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 61.61 -LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 61.62 -LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 61.63 -LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 61.64 -LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 61.65 -LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 61.66 -LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 61.67 -LUAI_FUNC int luaK_jump (FuncState *fs); 61.68 -LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 61.69 -LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 61.70 -LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 61.71 -LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 61.72 -LUAI_FUNC int luaK_getlabel (FuncState *fs); 61.73 -LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v); 61.74 -LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 61.75 -LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); 61.76 -LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 61.77 - 61.78 - 61.79 -#endif
62.1 --- a/src/lua/src/ldblib.c Sat Mar 03 11:04:56 2012 -0600 62.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 62.3 @@ -1,397 +0,0 @@ 62.4 -/* 62.5 -** $Id: ldblib.c,v 1.104.1.3 2008/01/21 13:11:21 roberto Exp $ 62.6 -** Interface from Lua to its debug API 62.7 -** See Copyright Notice in lua.h 62.8 -*/ 62.9 - 62.10 - 62.11 -#include <stdio.h> 62.12 -#include <stdlib.h> 62.13 -#include <string.h> 62.14 - 62.15 -#define ldblib_c 62.16 -#define LUA_LIB 62.17 - 62.18 -#include "lua.h" 62.19 - 62.20 -#include "lauxlib.h" 62.21 -#include "lualib.h" 62.22 - 62.23 - 62.24 - 62.25 -static int db_getregistry (lua_State *L) { 62.26 - lua_pushvalue(L, LUA_REGISTRYINDEX); 62.27 - return 1; 62.28 -} 62.29 - 62.30 - 62.31 -static int db_getmetatable (lua_State *L) { 62.32 - luaL_checkany(L, 1); 62.33 - if (!lua_getmetatable(L, 1)) { 62.34 - lua_pushnil(L); /* no metatable */ 62.35 - } 62.36 - return 1; 62.37 -} 62.38 - 62.39 - 62.40 -static int db_setmetatable (lua_State *L) { 62.41 - int t = lua_type(L, 2); 62.42 - luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 62.43 - "nil or table expected"); 62.44 - lua_settop(L, 2); 62.45 - lua_pushboolean(L, lua_setmetatable(L, 1)); 62.46 - return 1; 62.47 -} 62.48 - 62.49 - 62.50 -static int db_getfenv (lua_State *L) { 62.51 - lua_getfenv(L, 1); 62.52 - return 1; 62.53 -} 62.54 - 62.55 - 62.56 -static int db_setfenv (lua_State *L) { 62.57 - luaL_checktype(L, 2, LUA_TTABLE); 62.58 - lua_settop(L, 2); 62.59 - if (lua_setfenv(L, 1) == 0) 62.60 - luaL_error(L, LUA_QL("setfenv") 62.61 - " cannot change environment of given object"); 62.62 - return 1; 62.63 -} 62.64 - 62.65 - 62.66 -static void settabss (lua_State *L, const char *i, const char *v) { 62.67 - lua_pushstring(L, v); 62.68 - lua_setfield(L, -2, i); 62.69 -} 62.70 - 62.71 - 62.72 -static void settabsi (lua_State *L, const char *i, int v) { 62.73 - lua_pushinteger(L, v); 62.74 - lua_setfield(L, -2, i); 62.75 -} 62.76 - 62.77 - 62.78 -static lua_State *getthread (lua_State *L, int *arg) { 62.79 - if (lua_isthread(L, 1)) { 62.80 - *arg = 1; 62.81 - return lua_tothread(L, 1); 62.82 - } 62.83 - else { 62.84 - *arg = 0; 62.85 - return L; 62.86 - } 62.87 -} 62.88 - 62.89 - 62.90 -static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { 62.91 - if (L == L1) { 62.92 - lua_pushvalue(L, -2); 62.93 - lua_remove(L, -3); 62.94 - } 62.95 - else 62.96 - lua_xmove(L1, L, 1); 62.97 - lua_setfield(L, -2, fname); 62.98 -} 62.99 - 62.100 - 62.101 -static int db_getinfo (lua_State *L) { 62.102 - lua_Debug ar; 62.103 - int arg; 62.104 - lua_State *L1 = getthread(L, &arg); 62.105 - const char *options = luaL_optstring(L, arg+2, "flnSu"); 62.106 - if (lua_isnumber(L, arg+1)) { 62.107 - if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { 62.108 - lua_pushnil(L); /* level out of range */ 62.109 - return 1; 62.110 - } 62.111 - } 62.112 - else if (lua_isfunction(L, arg+1)) { 62.113 - lua_pushfstring(L, ">%s", options); 62.114 - options = lua_tostring(L, -1); 62.115 - lua_pushvalue(L, arg+1); 62.116 - lua_xmove(L, L1, 1); 62.117 - } 62.118 - else 62.119 - return luaL_argerror(L, arg+1, "function or level expected"); 62.120 - if (!lua_getinfo(L1, options, &ar)) 62.121 - return luaL_argerror(L, arg+2, "invalid option"); 62.122 - lua_createtable(L, 0, 2); 62.123 - if (strchr(options, 'S')) { 62.124 - settabss(L, "source", ar.source); 62.125 - settabss(L, "short_src", ar.short_src); 62.126 - settabsi(L, "linedefined", ar.linedefined); 62.127 - settabsi(L, "lastlinedefined", ar.lastlinedefined); 62.128 - settabss(L, "what", ar.what); 62.129 - } 62.130 - if (strchr(options, 'l')) 62.131 - settabsi(L, "currentline", ar.currentline); 62.132 - if (strchr(options, 'u')) 62.133 - settabsi(L, "nups", ar.nups); 62.134 - if (strchr(options, 'n')) { 62.135 - settabss(L, "name", ar.name); 62.136 - settabss(L, "namewhat", ar.namewhat); 62.137 - } 62.138 - if (strchr(options, 'L')) 62.139 - treatstackoption(L, L1, "activelines"); 62.140 - if (strchr(options, 'f')) 62.141 - treatstackoption(L, L1, "func"); 62.142 - return 1; /* return table */ 62.143 -} 62.144 - 62.145 - 62.146 -static int db_getlocal (lua_State *L) { 62.147 - int arg; 62.148 - lua_State *L1 = getthread(L, &arg); 62.149 - lua_Debug ar; 62.150 - const char *name; 62.151 - if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ 62.152 - return luaL_argerror(L, arg+1, "level out of range"); 62.153 - name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2)); 62.154 - if (name) { 62.155 - lua_xmove(L1, L, 1); 62.156 - lua_pushstring(L, name); 62.157 - lua_pushvalue(L, -2); 62.158 - return 2; 62.159 - } 62.160 - else { 62.161 - lua_pushnil(L); 62.162 - return 1; 62.163 - } 62.164 -} 62.165 - 62.166 - 62.167 -static int db_setlocal (lua_State *L) { 62.168 - int arg; 62.169 - lua_State *L1 = getthread(L, &arg); 62.170 - lua_Debug ar; 62.171 - if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ 62.172 - return luaL_argerror(L, arg+1, "level out of range"); 62.173 - luaL_checkany(L, arg+3); 62.174 - lua_settop(L, arg+3); 62.175 - lua_xmove(L, L1, 1); 62.176 - lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2))); 62.177 - return 1; 62.178 -} 62.179 - 62.180 - 62.181 -static int auxupvalue (lua_State *L, int get) { 62.182 - const char *name; 62.183 - int n = luaL_checkint(L, 2); 62.184 - luaL_checktype(L, 1, LUA_TFUNCTION); 62.185 - if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */ 62.186 - name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); 62.187 - if (name == NULL) return 0; 62.188 - lua_pushstring(L, name); 62.189 - lua_insert(L, -(get+1)); 62.190 - return get + 1; 62.191 -} 62.192 - 62.193 - 62.194 -static int db_getupvalue (lua_State *L) { 62.195 - return auxupvalue(L, 1); 62.196 -} 62.197 - 62.198 - 62.199 -static int db_setupvalue (lua_State *L) { 62.200 - luaL_checkany(L, 3); 62.201 - return auxupvalue(L, 0); 62.202 -} 62.203 - 62.204 - 62.205 - 62.206 -static const char KEY_HOOK = 'h'; 62.207 - 62.208 - 62.209 -static void hookf (lua_State *L, lua_Debug *ar) { 62.210 - static const char *const hooknames[] = 62.211 - {"call", "return", "line", "count", "tail return"}; 62.212 - lua_pushlightuserdata(L, (void *)&KEY_HOOK); 62.213 - lua_rawget(L, LUA_REGISTRYINDEX); 62.214 - lua_pushlightuserdata(L, L); 62.215 - lua_rawget(L, -2); 62.216 - if (lua_isfunction(L, -1)) { 62.217 - lua_pushstring(L, hooknames[(int)ar->event]); 62.218 - if (ar->currentline >= 0) 62.219 - lua_pushinteger(L, ar->currentline); 62.220 - else lua_pushnil(L); 62.221 - lua_assert(lua_getinfo(L, "lS", ar)); 62.222 - lua_call(L, 2, 0); 62.223 - } 62.224 -} 62.225 - 62.226 - 62.227 -static int makemask (const char *smask, int count) { 62.228 - int mask = 0; 62.229 - if (strchr(smask, 'c')) mask |= LUA_MASKCALL; 62.230 - if (strchr(smask, 'r')) mask |= LUA_MASKRET; 62.231 - if (strchr(smask, 'l')) mask |= LUA_MASKLINE; 62.232 - if (count > 0) mask |= LUA_MASKCOUNT; 62.233 - return mask; 62.234 -} 62.235 - 62.236 - 62.237 -static char *unmakemask (int mask, char *smask) { 62.238 - int i = 0; 62.239 - if (mask & LUA_MASKCALL) smask[i++] = 'c'; 62.240 - if (mask & LUA_MASKRET) smask[i++] = 'r'; 62.241 - if (mask & LUA_MASKLINE) smask[i++] = 'l'; 62.242 - smask[i] = '\0'; 62.243 - return smask; 62.244 -} 62.245 - 62.246 - 62.247 -static void gethooktable (lua_State *L) { 62.248 - lua_pushlightuserdata(L, (void *)&KEY_HOOK); 62.249 - lua_rawget(L, LUA_REGISTRYINDEX); 62.250 - if (!lua_istable(L, -1)) { 62.251 - lua_pop(L, 1); 62.252 - lua_createtable(L, 0, 1); 62.253 - lua_pushlightuserdata(L, (void *)&KEY_HOOK); 62.254 - lua_pushvalue(L, -2); 62.255 - lua_rawset(L, LUA_REGISTRYINDEX); 62.256 - } 62.257 -} 62.258 - 62.259 - 62.260 -static int db_sethook (lua_State *L) { 62.261 - int arg, mask, count; 62.262 - lua_Hook func; 62.263 - lua_State *L1 = getthread(L, &arg); 62.264 - if (lua_isnoneornil(L, arg+1)) { 62.265 - lua_settop(L, arg+1); 62.266 - func = NULL; mask = 0; count = 0; /* turn off hooks */ 62.267 - } 62.268 - else { 62.269 - const char *smask = luaL_checkstring(L, arg+2); 62.270 - luaL_checktype(L, arg+1, LUA_TFUNCTION); 62.271 - count = luaL_optint(L, arg+3, 0); 62.272 - func = hookf; mask = makemask(smask, count); 62.273 - } 62.274 - gethooktable(L); 62.275 - lua_pushlightuserdata(L, L1); 62.276 - lua_pushvalue(L, arg+1); 62.277 - lua_rawset(L, -3); /* set new hook */ 62.278 - lua_pop(L, 1); /* remove hook table */ 62.279 - lua_sethook(L1, func, mask, count); /* set hooks */ 62.280 - return 0; 62.281 -} 62.282 - 62.283 - 62.284 -static int db_gethook (lua_State *L) { 62.285 - int arg; 62.286 - lua_State *L1 = getthread(L, &arg); 62.287 - char buff[5]; 62.288 - int mask = lua_gethookmask(L1); 62.289 - lua_Hook hook = lua_gethook(L1); 62.290 - if (hook != NULL && hook != hookf) /* external hook? */ 62.291 - lua_pushliteral(L, "external hook"); 62.292 - else { 62.293 - gethooktable(L); 62.294 - lua_pushlightuserdata(L, L1); 62.295 - lua_rawget(L, -2); /* get hook */ 62.296 - lua_remove(L, -2); /* remove hook table */ 62.297 - } 62.298 - lua_pushstring(L, unmakemask(mask, buff)); 62.299 - lua_pushinteger(L, lua_gethookcount(L1)); 62.300 - return 3; 62.301 -} 62.302 - 62.303 - 62.304 -static int db_debug (lua_State *L) { 62.305 - for (;;) { 62.306 - char buffer[250]; 62.307 - fputs("lua_debug> ", stderr); 62.308 - if (fgets(buffer, sizeof(buffer), stdin) == 0 || 62.309 - strcmp(buffer, "cont\n") == 0) 62.310 - return 0; 62.311 - if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || 62.312 - lua_pcall(L, 0, 0, 0)) { 62.313 - fputs(lua_tostring(L, -1), stderr); 62.314 - fputs("\n", stderr); 62.315 - } 62.316 - lua_settop(L, 0); /* remove eventual returns */ 62.317 - } 62.318 -} 62.319 - 62.320 - 62.321 -#define LEVELS1 12 /* size of the first part of the stack */ 62.322 -#define LEVELS2 10 /* size of the second part of the stack */ 62.323 - 62.324 -static int db_errorfb (lua_State *L) { 62.325 - int level; 62.326 - int firstpart = 1; /* still before eventual `...' */ 62.327 - int arg; 62.328 - lua_State *L1 = getthread(L, &arg); 62.329 - lua_Debug ar; 62.330 - if (lua_isnumber(L, arg+2)) { 62.331 - level = (int)lua_tointeger(L, arg+2); 62.332 - lua_pop(L, 1); 62.333 - } 62.334 - else 62.335 - level = (L == L1) ? 1 : 0; /* level 0 may be this own function */ 62.336 - if (lua_gettop(L) == arg) 62.337 - lua_pushliteral(L, ""); 62.338 - else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */ 62.339 - else lua_pushliteral(L, "\n"); 62.340 - lua_pushliteral(L, "stack traceback:"); 62.341 - while (lua_getstack(L1, level++, &ar)) { 62.342 - if (level > LEVELS1 && firstpart) { 62.343 - /* no more than `LEVELS2' more levels? */ 62.344 - if (!lua_getstack(L1, level+LEVELS2, &ar)) 62.345 - level--; /* keep going */ 62.346 - else { 62.347 - lua_pushliteral(L, "\n\t..."); /* too many levels */ 62.348 - while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */ 62.349 - level++; 62.350 - } 62.351 - firstpart = 0; 62.352 - continue; 62.353 - } 62.354 - lua_pushliteral(L, "\n\t"); 62.355 - lua_getinfo(L1, "Snl", &ar); 62.356 - lua_pushfstring(L, "%s:", ar.short_src); 62.357 - if (ar.currentline > 0) 62.358 - lua_pushfstring(L, "%d:", ar.currentline); 62.359 - if (*ar.namewhat != '\0') /* is there a name? */ 62.360 - lua_pushfstring(L, " in function " LUA_QS, ar.name); 62.361 - else { 62.362 - if (*ar.what == 'm') /* main? */ 62.363 - lua_pushfstring(L, " in main chunk"); 62.364 - else if (*ar.what == 'C' || *ar.what == 't') 62.365 - lua_pushliteral(L, " ?"); /* C function or tail call */ 62.366 - else 62.367 - lua_pushfstring(L, " in function <%s:%d>", 62.368 - ar.short_src, ar.linedefined); 62.369 - } 62.370 - lua_concat(L, lua_gettop(L) - arg); 62.371 - } 62.372 - lua_concat(L, lua_gettop(L) - arg); 62.373 - return 1; 62.374 -} 62.375 - 62.376 - 62.377 -static const luaL_Reg dblib[] = { 62.378 - {"debug", db_debug}, 62.379 - {"getfenv", db_getfenv}, 62.380 - {"gethook", db_gethook}, 62.381 - {"getinfo", db_getinfo}, 62.382 - {"getlocal", db_getlocal}, 62.383 - {"getregistry", db_getregistry}, 62.384 - {"getmetatable", db_getmetatable}, 62.385 - {"getupvalue", db_getupvalue}, 62.386 - {"setfenv", db_setfenv}, 62.387 - {"sethook", db_sethook}, 62.388 - {"setlocal", db_setlocal}, 62.389 - {"setmetatable", db_setmetatable}, 62.390 - {"setupvalue", db_setupvalue}, 62.391 - {"traceback", db_errorfb}, 62.392 - {NULL, NULL} 62.393 -}; 62.394 - 62.395 - 62.396 -LUALIB_API int luaopen_debug (lua_State *L) { 62.397 - luaL_register(L, LUA_DBLIBNAME, dblib); 62.398 - return 1; 62.399 -} 62.400 -
63.1 --- a/src/lua/src/ldebug.c Sat Mar 03 11:04:56 2012 -0600 63.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 63.3 @@ -1,638 +0,0 @@ 63.4 -/* 63.5 -** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $ 63.6 -** Debug Interface 63.7 -** See Copyright Notice in lua.h 63.8 -*/ 63.9 - 63.10 - 63.11 -#include <stdarg.h> 63.12 -#include <stddef.h> 63.13 -#include <string.h> 63.14 - 63.15 - 63.16 -#define ldebug_c 63.17 -#define LUA_CORE 63.18 - 63.19 -#include "lua.h" 63.20 - 63.21 -#include "lapi.h" 63.22 -#include "lcode.h" 63.23 -#include "ldebug.h" 63.24 -#include "ldo.h" 63.25 -#include "lfunc.h" 63.26 -#include "lobject.h" 63.27 -#include "lopcodes.h" 63.28 -#include "lstate.h" 63.29 -#include "lstring.h" 63.30 -#include "ltable.h" 63.31 -#include "ltm.h" 63.32 -#include "lvm.h" 63.33 - 63.34 - 63.35 - 63.36 -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); 63.37 - 63.38 - 63.39 -static int currentpc (lua_State *L, CallInfo *ci) { 63.40 - if (!isLua(ci)) return -1; /* function is not a Lua function? */ 63.41 - if (ci == L->ci) 63.42 - ci->savedpc = L->savedpc; 63.43 - return pcRel(ci->savedpc, ci_func(ci)->l.p); 63.44 -} 63.45 - 63.46 - 63.47 -static int currentline (lua_State *L, CallInfo *ci) { 63.48 - int pc = currentpc(L, ci); 63.49 - if (pc < 0) 63.50 - return -1; /* only active lua functions have current-line information */ 63.51 - else 63.52 - return getline(ci_func(ci)->l.p, pc); 63.53 -} 63.54 - 63.55 - 63.56 -/* 63.57 -** this function can be called asynchronous (e.g. during a signal) 63.58 -*/ 63.59 -LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { 63.60 - if (func == NULL || mask == 0) { /* turn off hooks? */ 63.61 - mask = 0; 63.62 - func = NULL; 63.63 - } 63.64 - L->hook = func; 63.65 - L->basehookcount = count; 63.66 - resethookcount(L); 63.67 - L->hookmask = cast_byte(mask); 63.68 - return 1; 63.69 -} 63.70 - 63.71 - 63.72 -LUA_API lua_Hook lua_gethook (lua_State *L) { 63.73 - return L->hook; 63.74 -} 63.75 - 63.76 - 63.77 -LUA_API int lua_gethookmask (lua_State *L) { 63.78 - return L->hookmask; 63.79 -} 63.80 - 63.81 - 63.82 -LUA_API int lua_gethookcount (lua_State *L) { 63.83 - return L->basehookcount; 63.84 -} 63.85 - 63.86 - 63.87 -LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { 63.88 - int status; 63.89 - CallInfo *ci; 63.90 - lua_lock(L); 63.91 - for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) { 63.92 - level--; 63.93 - if (f_isLua(ci)) /* Lua function? */ 63.94 - level -= ci->tailcalls; /* skip lost tail calls */ 63.95 - } 63.96 - if (level == 0 && ci > L->base_ci) { /* level found? */ 63.97 - status = 1; 63.98 - ar->i_ci = cast_int(ci - L->base_ci); 63.99 - } 63.100 - else if (level < 0) { /* level is of a lost tail call? */ 63.101 - status = 1; 63.102 - ar->i_ci = 0; 63.103 - } 63.104 - else status = 0; /* no such level */ 63.105 - lua_unlock(L); 63.106 - return status; 63.107 -} 63.108 - 63.109 - 63.110 -static Proto *getluaproto (CallInfo *ci) { 63.111 - return (isLua(ci) ? ci_func(ci)->l.p : NULL); 63.112 -} 63.113 - 63.114 - 63.115 -static const char *findlocal (lua_State *L, CallInfo *ci, int n) { 63.116 - const char *name; 63.117 - Proto *fp = getluaproto(ci); 63.118 - if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL) 63.119 - return name; /* is a local variable in a Lua function */ 63.120 - else { 63.121 - StkId limit = (ci == L->ci) ? L->top : (ci+1)->func; 63.122 - if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */ 63.123 - return "(*temporary)"; 63.124 - else 63.125 - return NULL; 63.126 - } 63.127 -} 63.128 - 63.129 - 63.130 -LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { 63.131 - CallInfo *ci = L->base_ci + ar->i_ci; 63.132 - const char *name = findlocal(L, ci, n); 63.133 - lua_lock(L); 63.134 - if (name) 63.135 - luaA_pushobject(L, ci->base + (n - 1)); 63.136 - lua_unlock(L); 63.137 - return name; 63.138 -} 63.139 - 63.140 - 63.141 -LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { 63.142 - CallInfo *ci = L->base_ci + ar->i_ci; 63.143 - const char *name = findlocal(L, ci, n); 63.144 - lua_lock(L); 63.145 - if (name) 63.146 - setobjs2s(L, ci->base + (n - 1), L->top - 1); 63.147 - L->top--; /* pop value */ 63.148 - lua_unlock(L); 63.149 - return name; 63.150 -} 63.151 - 63.152 - 63.153 -static void funcinfo (lua_Debug *ar, Closure *cl) { 63.154 - if (cl->c.isC) { 63.155 - ar->source = "=[C]"; 63.156 - ar->linedefined = -1; 63.157 - ar->lastlinedefined = -1; 63.158 - ar->what = "C"; 63.159 - } 63.160 - else { 63.161 - ar->source = getstr(cl->l.p->source); 63.162 - ar->linedefined = cl->l.p->linedefined; 63.163 - ar->lastlinedefined = cl->l.p->lastlinedefined; 63.164 - ar->what = (ar->linedefined == 0) ? "main" : "Lua"; 63.165 - } 63.166 - luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); 63.167 -} 63.168 - 63.169 - 63.170 -static void info_tailcall (lua_Debug *ar) { 63.171 - ar->name = ar->namewhat = ""; 63.172 - ar->what = "tail"; 63.173 - ar->lastlinedefined = ar->linedefined = ar->currentline = -1; 63.174 - ar->source = "=(tail call)"; 63.175 - luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); 63.176 - ar->nups = 0; 63.177 -} 63.178 - 63.179 - 63.180 -static void collectvalidlines (lua_State *L, Closure *f) { 63.181 - if (f == NULL || f->c.isC) { 63.182 - setnilvalue(L->top); 63.183 - } 63.184 - else { 63.185 - Table *t = luaH_new(L, 0, 0); 63.186 - int *lineinfo = f->l.p->lineinfo; 63.187 - int i; 63.188 - for (i=0; i<f->l.p->sizelineinfo; i++) 63.189 - setbvalue(luaH_setnum(L, t, lineinfo[i]), 1); 63.190 - sethvalue(L, L->top, t); 63.191 - } 63.192 - incr_top(L); 63.193 -} 63.194 - 63.195 - 63.196 -static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, 63.197 - Closure *f, CallInfo *ci) { 63.198 - int status = 1; 63.199 - if (f == NULL) { 63.200 - info_tailcall(ar); 63.201 - return status; 63.202 - } 63.203 - for (; *what; what++) { 63.204 - switch (*what) { 63.205 - case 'S': { 63.206 - funcinfo(ar, f); 63.207 - break; 63.208 - } 63.209 - case 'l': { 63.210 - ar->currentline = (ci) ? currentline(L, ci) : -1; 63.211 - break; 63.212 - } 63.213 - case 'u': { 63.214 - ar->nups = f->c.nupvalues; 63.215 - break; 63.216 - } 63.217 - case 'n': { 63.218 - ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL; 63.219 - if (ar->namewhat == NULL) { 63.220 - ar->namewhat = ""; /* not found */ 63.221 - ar->name = NULL; 63.222 - } 63.223 - break; 63.224 - } 63.225 - case 'L': 63.226 - case 'f': /* handled by lua_getinfo */ 63.227 - break; 63.228 - default: status = 0; /* invalid option */ 63.229 - } 63.230 - } 63.231 - return status; 63.232 -} 63.233 - 63.234 - 63.235 -LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { 63.236 - int status; 63.237 - Closure *f = NULL; 63.238 - CallInfo *ci = NULL; 63.239 - lua_lock(L); 63.240 - if (*what == '>') { 63.241 - StkId func = L->top - 1; 63.242 - luai_apicheck(L, ttisfunction(func)); 63.243 - what++; /* skip the '>' */ 63.244 - f = clvalue(func); 63.245 - L->top--; /* pop function */ 63.246 - } 63.247 - else if (ar->i_ci != 0) { /* no tail call? */ 63.248 - ci = L->base_ci + ar->i_ci; 63.249 - lua_assert(ttisfunction(ci->func)); 63.250 - f = clvalue(ci->func); 63.251 - } 63.252 - status = auxgetinfo(L, what, ar, f, ci); 63.253 - if (strchr(what, 'f')) { 63.254 - if (f == NULL) setnilvalue(L->top); 63.255 - else setclvalue(L, L->top, f); 63.256 - incr_top(L); 63.257 - } 63.258 - if (strchr(what, 'L')) 63.259 - collectvalidlines(L, f); 63.260 - lua_unlock(L); 63.261 - return status; 63.262 -} 63.263 - 63.264 - 63.265 -/* 63.266 -** {====================================================== 63.267 -** Symbolic Execution and code checker 63.268 -** ======================================================= 63.269 -*/ 63.270 - 63.271 -#define check(x) if (!(x)) return 0; 63.272 - 63.273 -#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode) 63.274 - 63.275 -#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize) 63.276 - 63.277 - 63.278 - 63.279 -static int precheck (const Proto *pt) { 63.280 - check(pt->maxstacksize <= MAXSTACK); 63.281 - check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize); 63.282 - check(!(pt->is_vararg & VARARG_NEEDSARG) || 63.283 - (pt->is_vararg & VARARG_HASARG)); 63.284 - check(pt->sizeupvalues <= pt->nups); 63.285 - check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0); 63.286 - check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN); 63.287 - return 1; 63.288 -} 63.289 - 63.290 - 63.291 -#define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1]) 63.292 - 63.293 -int luaG_checkopenop (Instruction i) { 63.294 - switch (GET_OPCODE(i)) { 63.295 - case OP_CALL: 63.296 - case OP_TAILCALL: 63.297 - case OP_RETURN: 63.298 - case OP_SETLIST: { 63.299 - check(GETARG_B(i) == 0); 63.300 - return 1; 63.301 - } 63.302 - default: return 0; /* invalid instruction after an open call */ 63.303 - } 63.304 -} 63.305 - 63.306 - 63.307 -static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) { 63.308 - switch (mode) { 63.309 - case OpArgN: check(r == 0); break; 63.310 - case OpArgU: break; 63.311 - case OpArgR: checkreg(pt, r); break; 63.312 - case OpArgK: 63.313 - check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize); 63.314 - break; 63.315 - } 63.316 - return 1; 63.317 -} 63.318 - 63.319 - 63.320 -static Instruction symbexec (const Proto *pt, int lastpc, int reg) { 63.321 - int pc; 63.322 - int last; /* stores position of last instruction that changed `reg' */ 63.323 - last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */ 63.324 - check(precheck(pt)); 63.325 - for (pc = 0; pc < lastpc; pc++) { 63.326 - Instruction i = pt->code[pc]; 63.327 - OpCode op = GET_OPCODE(i); 63.328 - int a = GETARG_A(i); 63.329 - int b = 0; 63.330 - int c = 0; 63.331 - check(op < NUM_OPCODES); 63.332 - checkreg(pt, a); 63.333 - switch (getOpMode(op)) { 63.334 - case iABC: { 63.335 - b = GETARG_B(i); 63.336 - c = GETARG_C(i); 63.337 - check(checkArgMode(pt, b, getBMode(op))); 63.338 - check(checkArgMode(pt, c, getCMode(op))); 63.339 - break; 63.340 - } 63.341 - case iABx: { 63.342 - b = GETARG_Bx(i); 63.343 - if (getBMode(op) == OpArgK) check(b < pt->sizek); 63.344 - break; 63.345 - } 63.346 - case iAsBx: { 63.347 - b = GETARG_sBx(i); 63.348 - if (getBMode(op) == OpArgR) { 63.349 - int dest = pc+1+b; 63.350 - check(0 <= dest && dest < pt->sizecode); 63.351 - if (dest > 0) { 63.352 - int j; 63.353 - /* check that it does not jump to a setlist count; this 63.354 - is tricky, because the count from a previous setlist may 63.355 - have the same value of an invalid setlist; so, we must 63.356 - go all the way back to the first of them (if any) */ 63.357 - for (j = 0; j < dest; j++) { 63.358 - Instruction d = pt->code[dest-1-j]; 63.359 - if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break; 63.360 - } 63.361 - /* if 'j' is even, previous value is not a setlist (even if 63.362 - it looks like one) */ 63.363 - check((j&1) == 0); 63.364 - } 63.365 - } 63.366 - break; 63.367 - } 63.368 - } 63.369 - if (testAMode(op)) { 63.370 - if (a == reg) last = pc; /* change register `a' */ 63.371 - } 63.372 - if (testTMode(op)) { 63.373 - check(pc+2 < pt->sizecode); /* check skip */ 63.374 - check(GET_OPCODE(pt->code[pc+1]) == OP_JMP); 63.375 - } 63.376 - switch (op) { 63.377 - case OP_LOADBOOL: { 63.378 - if (c == 1) { /* does it jump? */ 63.379 - check(pc+2 < pt->sizecode); /* check its jump */ 63.380 - check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST || 63.381 - GETARG_C(pt->code[pc+1]) != 0); 63.382 - } 63.383 - break; 63.384 - } 63.385 - case OP_LOADNIL: { 63.386 - if (a <= reg && reg <= b) 63.387 - last = pc; /* set registers from `a' to `b' */ 63.388 - break; 63.389 - } 63.390 - case OP_GETUPVAL: 63.391 - case OP_SETUPVAL: { 63.392 - check(b < pt->nups); 63.393 - break; 63.394 - } 63.395 - case OP_GETGLOBAL: 63.396 - case OP_SETGLOBAL: { 63.397 - check(ttisstring(&pt->k[b])); 63.398 - break; 63.399 - } 63.400 - case OP_SELF: { 63.401 - checkreg(pt, a+1); 63.402 - if (reg == a+1) last = pc; 63.403 - break; 63.404 - } 63.405 - case OP_CONCAT: { 63.406 - check(b < c); /* at least two operands */ 63.407 - break; 63.408 - } 63.409 - case OP_TFORLOOP: { 63.410 - check(c >= 1); /* at least one result (control variable) */ 63.411 - checkreg(pt, a+2+c); /* space for results */ 63.412 - if (reg >= a+2) last = pc; /* affect all regs above its base */ 63.413 - break; 63.414 - } 63.415 - case OP_FORLOOP: 63.416 - case OP_FORPREP: 63.417 - checkreg(pt, a+3); 63.418 - /* go through */ 63.419 - case OP_JMP: { 63.420 - int dest = pc+1+b; 63.421 - /* not full check and jump is forward and do not skip `lastpc'? */ 63.422 - if (reg != NO_REG && pc < dest && dest <= lastpc) 63.423 - pc += b; /* do the jump */ 63.424 - break; 63.425 - } 63.426 - case OP_CALL: 63.427 - case OP_TAILCALL: { 63.428 - if (b != 0) { 63.429 - checkreg(pt, a+b-1); 63.430 - } 63.431 - c--; /* c = num. returns */ 63.432 - if (c == LUA_MULTRET) { 63.433 - check(checkopenop(pt, pc)); 63.434 - } 63.435 - else if (c != 0) 63.436 - checkreg(pt, a+c-1); 63.437 - if (reg >= a) last = pc; /* affect all registers above base */ 63.438 - break; 63.439 - } 63.440 - case OP_RETURN: { 63.441 - b--; /* b = num. returns */ 63.442 - if (b > 0) checkreg(pt, a+b-1); 63.443 - break; 63.444 - } 63.445 - case OP_SETLIST: { 63.446 - if (b > 0) checkreg(pt, a + b); 63.447 - if (c == 0) { 63.448 - pc++; 63.449 - check(pc < pt->sizecode - 1); 63.450 - } 63.451 - break; 63.452 - } 63.453 - case OP_CLOSURE: { 63.454 - int nup, j; 63.455 - check(b < pt->sizep); 63.456 - nup = pt->p[b]->nups; 63.457 - check(pc + nup < pt->sizecode); 63.458 - for (j = 1; j <= nup; j++) { 63.459 - OpCode op1 = GET_OPCODE(pt->code[pc + j]); 63.460 - check(op1 == OP_GETUPVAL || op1 == OP_MOVE); 63.461 - } 63.462 - if (reg != NO_REG) /* tracing? */ 63.463 - pc += nup; /* do not 'execute' these pseudo-instructions */ 63.464 - break; 63.465 - } 63.466 - case OP_VARARG: { 63.467 - check((pt->is_vararg & VARARG_ISVARARG) && 63.468 - !(pt->is_vararg & VARARG_NEEDSARG)); 63.469 - b--; 63.470 - if (b == LUA_MULTRET) check(checkopenop(pt, pc)); 63.471 - checkreg(pt, a+b-1); 63.472 - break; 63.473 - } 63.474 - default: break; 63.475 - } 63.476 - } 63.477 - return pt->code[last]; 63.478 -} 63.479 - 63.480 -#undef check 63.481 -#undef checkjump 63.482 -#undef checkreg 63.483 - 63.484 -/* }====================================================== */ 63.485 - 63.486 - 63.487 -int luaG_checkcode (const Proto *pt) { 63.488 - return (symbexec(pt, pt->sizecode, NO_REG) != 0); 63.489 -} 63.490 - 63.491 - 63.492 -static const char *kname (Proto *p, int c) { 63.493 - if (ISK(c) && ttisstring(&p->k[INDEXK(c)])) 63.494 - return svalue(&p->k[INDEXK(c)]); 63.495 - else 63.496 - return "?"; 63.497 -} 63.498 - 63.499 - 63.500 -static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos, 63.501 - const char **name) { 63.502 - if (isLua(ci)) { /* a Lua function? */ 63.503 - Proto *p = ci_func(ci)->l.p; 63.504 - int pc = currentpc(L, ci); 63.505 - Instruction i; 63.506 - *name = luaF_getlocalname(p, stackpos+1, pc); 63.507 - if (*name) /* is a local? */ 63.508 - return "local"; 63.509 - i = symbexec(p, pc, stackpos); /* try symbolic execution */ 63.510 - lua_assert(pc != -1); 63.511 - switch (GET_OPCODE(i)) { 63.512 - case OP_GETGLOBAL: { 63.513 - int g = GETARG_Bx(i); /* global index */ 63.514 - lua_assert(ttisstring(&p->k[g])); 63.515 - *name = svalue(&p->k[g]); 63.516 - return "global"; 63.517 - } 63.518 - case OP_MOVE: { 63.519 - int a = GETARG_A(i); 63.520 - int b = GETARG_B(i); /* move from `b' to `a' */ 63.521 - if (b < a) 63.522 - return getobjname(L, ci, b, name); /* get name for `b' */ 63.523 - break; 63.524 - } 63.525 - case OP_GETTABLE: { 63.526 - int k = GETARG_C(i); /* key index */ 63.527 - *name = kname(p, k); 63.528 - return "field"; 63.529 - } 63.530 - case OP_GETUPVAL: { 63.531 - int u = GETARG_B(i); /* upvalue index */ 63.532 - *name = p->upvalues ? getstr(p->upvalues[u]) : "?"; 63.533 - return "upvalue"; 63.534 - } 63.535 - case OP_SELF: { 63.536 - int k = GETARG_C(i); /* key index */ 63.537 - *name = kname(p, k); 63.538 - return "method"; 63.539 - } 63.540 - default: break; 63.541 - } 63.542 - } 63.543 - return NULL; /* no useful name found */ 63.544 -} 63.545 - 63.546 - 63.547 -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { 63.548 - Instruction i; 63.549 - if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1)) 63.550 - return NULL; /* calling function is not Lua (or is unknown) */ 63.551 - ci--; /* calling function */ 63.552 - i = ci_func(ci)->l.p->code[currentpc(L, ci)]; 63.553 - if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL || 63.554 - GET_OPCODE(i) == OP_TFORLOOP) 63.555 - return getobjname(L, ci, GETARG_A(i), name); 63.556 - else 63.557 - return NULL; /* no useful name can be found */ 63.558 -} 63.559 - 63.560 - 63.561 -/* only ANSI way to check whether a pointer points to an array */ 63.562 -static int isinstack (CallInfo *ci, const TValue *o) { 63.563 - StkId p; 63.564 - for (p = ci->base; p < ci->top; p++) 63.565 - if (o == p) return 1; 63.566 - return 0; 63.567 -} 63.568 - 63.569 - 63.570 -void luaG_typeerror (lua_State *L, const TValue *o, const char *op) { 63.571 - const char *name = NULL; 63.572 - const char *t = luaT_typenames[ttype(o)]; 63.573 - const char *kind = (isinstack(L->ci, o)) ? 63.574 - getobjname(L, L->ci, cast_int(o - L->base), &name) : 63.575 - NULL; 63.576 - if (kind) 63.577 - luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", 63.578 - op, kind, name, t); 63.579 - else 63.580 - luaG_runerror(L, "attempt to %s a %s value", op, t); 63.581 -} 63.582 - 63.583 - 63.584 -void luaG_concaterror (lua_State *L, StkId p1, StkId p2) { 63.585 - if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; 63.586 - lua_assert(!ttisstring(p1) && !ttisnumber(p1)); 63.587 - luaG_typeerror(L, p1, "concatenate"); 63.588 -} 63.589 - 63.590 - 63.591 -void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { 63.592 - TValue temp; 63.593 - if (luaV_tonumber(p1, &temp) == NULL) 63.594 - p2 = p1; /* first operand is wrong */ 63.595 - luaG_typeerror(L, p2, "perform arithmetic on"); 63.596 -} 63.597 - 63.598 - 63.599 -int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { 63.600 - const char *t1 = luaT_typenames[ttype(p1)]; 63.601 - const char *t2 = luaT_typenames[ttype(p2)]; 63.602 - if (t1[2] == t2[2]) 63.603 - luaG_runerror(L, "attempt to compare two %s values", t1); 63.604 - else 63.605 - luaG_runerror(L, "attempt to compare %s with %s", t1, t2); 63.606 - return 0; 63.607 -} 63.608 - 63.609 - 63.610 -static void addinfo (lua_State *L, const char *msg) { 63.611 - CallInfo *ci = L->ci; 63.612 - if (isLua(ci)) { /* is Lua code? */ 63.613 - char buff[LUA_IDSIZE]; /* add file:line information */ 63.614 - int line = currentline(L, ci); 63.615 - luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE); 63.616 - luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); 63.617 - } 63.618 -} 63.619 - 63.620 - 63.621 -void luaG_errormsg (lua_State *L) { 63.622 - if (L->errfunc != 0) { /* is there an error handling function? */ 63.623 - StkId errfunc = restorestack(L, L->errfunc); 63.624 - if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); 63.625 - setobjs2s(L, L->top, L->top - 1); /* move argument */ 63.626 - setobjs2s(L, L->top - 1, errfunc); /* push function */ 63.627 - incr_top(L); 63.628 - luaD_call(L, L->top - 2, 1); /* call it */ 63.629 - } 63.630 - luaD_throw(L, LUA_ERRRUN); 63.631 -} 63.632 - 63.633 - 63.634 -void luaG_runerror (lua_State *L, const char *fmt, ...) { 63.635 - va_list argp; 63.636 - va_start(argp, fmt); 63.637 - addinfo(L, luaO_pushvfstring(L, fmt, argp)); 63.638 - va_end(argp); 63.639 - luaG_errormsg(L); 63.640 -} 63.641 -
64.1 --- a/src/lua/src/ldebug.h Sat Mar 03 11:04:56 2012 -0600 64.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 64.3 @@ -1,33 +0,0 @@ 64.4 -/* 64.5 -** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $ 64.6 -** Auxiliary functions from Debug Interface module 64.7 -** See Copyright Notice in lua.h 64.8 -*/ 64.9 - 64.10 -#ifndef ldebug_h 64.11 -#define ldebug_h 64.12 - 64.13 - 64.14 -#include "lstate.h" 64.15 - 64.16 - 64.17 -#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 64.18 - 64.19 -#define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 64.20 - 64.21 -#define resethookcount(L) (L->hookcount = L->basehookcount) 64.22 - 64.23 - 64.24 -LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o, 64.25 - const char *opname); 64.26 -LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2); 64.27 -LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1, 64.28 - const TValue *p2); 64.29 -LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1, 64.30 - const TValue *p2); 64.31 -LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...); 64.32 -LUAI_FUNC void luaG_errormsg (lua_State *L); 64.33 -LUAI_FUNC int luaG_checkcode (const Proto *pt); 64.34 -LUAI_FUNC int luaG_checkopenop (Instruction i); 64.35 - 64.36 -#endif
65.1 --- a/src/lua/src/ldo.c Sat Mar 03 11:04:56 2012 -0600 65.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 65.3 @@ -1,518 +0,0 @@ 65.4 -/* 65.5 -** $Id: ldo.c,v 2.38.1.3 2008/01/18 22:31:22 roberto Exp $ 65.6 -** Stack and Call structure of Lua 65.7 -** See Copyright Notice in lua.h 65.8 -*/ 65.9 - 65.10 - 65.11 -#include <setjmp.h> 65.12 -#include <stdlib.h> 65.13 -#include <string.h> 65.14 - 65.15 -#define ldo_c 65.16 -#define LUA_CORE 65.17 - 65.18 -#include "lua.h" 65.19 - 65.20 -#include "ldebug.h" 65.21 -#include "ldo.h" 65.22 -#include "lfunc.h" 65.23 -#include "lgc.h" 65.24 -#include "lmem.h" 65.25 -#include "lobject.h" 65.26 -#include "lopcodes.h" 65.27 -#include "lparser.h" 65.28 -#include "lstate.h" 65.29 -#include "lstring.h" 65.30 -#include "ltable.h" 65.31 -#include "ltm.h" 65.32 -#include "lundump.h" 65.33 -#include "lvm.h" 65.34 -#include "lzio.h" 65.35 - 65.36 - 65.37 - 65.38 - 65.39 -/* 65.40 -** {====================================================== 65.41 -** Error-recovery functions 65.42 -** ======================================================= 65.43 -*/ 65.44 - 65.45 - 65.46 -/* chain list of long jump buffers */ 65.47 -struct lua_longjmp { 65.48 - struct lua_longjmp *previous; 65.49 - luai_jmpbuf b; 65.50 - volatile int status; /* error code */ 65.51 -}; 65.52 - 65.53 - 65.54 -void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { 65.55 - switch (errcode) { 65.56 - case LUA_ERRMEM: { 65.57 - setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG)); 65.58 - break; 65.59 - } 65.60 - case LUA_ERRERR: { 65.61 - setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); 65.62 - break; 65.63 - } 65.64 - case LUA_ERRSYNTAX: 65.65 - case LUA_ERRRUN: { 65.66 - setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ 65.67 - break; 65.68 - } 65.69 - } 65.70 - L->top = oldtop + 1; 65.71 -} 65.72 - 65.73 - 65.74 -static void restore_stack_limit (lua_State *L) { 65.75 - lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); 65.76 - if (L->size_ci > LUAI_MAXCALLS) { /* there was an overflow? */ 65.77 - int inuse = cast_int(L->ci - L->base_ci); 65.78 - if (inuse + 1 < LUAI_MAXCALLS) /* can `undo' overflow? */ 65.79 - luaD_reallocCI(L, LUAI_MAXCALLS); 65.80 - } 65.81 -} 65.82 - 65.83 - 65.84 -static void resetstack (lua_State *L, int status) { 65.85 - L->ci = L->base_ci; 65.86 - L->base = L->ci->base; 65.87 - luaF_close(L, L->base); /* close eventual pending closures */ 65.88 - luaD_seterrorobj(L, status, L->base); 65.89 - L->nCcalls = L->baseCcalls; 65.90 - L->allowhook = 1; 65.91 - restore_stack_limit(L); 65.92 - L->errfunc = 0; 65.93 - L->errorJmp = NULL; 65.94 -} 65.95 - 65.96 - 65.97 -void luaD_throw (lua_State *L, int errcode) { 65.98 - if (L->errorJmp) { 65.99 - L->errorJmp->status = errcode; 65.100 - LUAI_THROW(L, L->errorJmp); 65.101 - } 65.102 - else { 65.103 - L->status = cast_byte(errcode); 65.104 - if (G(L)->panic) { 65.105 - resetstack(L, errcode); 65.106 - lua_unlock(L); 65.107 - G(L)->panic(L); 65.108 - } 65.109 - exit(EXIT_FAILURE); 65.110 - } 65.111 -} 65.112 - 65.113 - 65.114 -int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { 65.115 - struct lua_longjmp lj; 65.116 - lj.status = 0; 65.117 - lj.previous = L->errorJmp; /* chain new error handler */ 65.118 - L->errorJmp = &lj; 65.119 - LUAI_TRY(L, &lj, 65.120 - (*f)(L, ud); 65.121 - ); 65.122 - L->errorJmp = lj.previous; /* restore old error handler */ 65.123 - return lj.status; 65.124 -} 65.125 - 65.126 -/* }====================================================== */ 65.127 - 65.128 - 65.129 -static void correctstack (lua_State *L, TValue *oldstack) { 65.130 - CallInfo *ci; 65.131 - GCObject *up; 65.132 - L->top = (L->top - oldstack) + L->stack; 65.133 - for (up = L->openupval; up != NULL; up = up->gch.next) 65.134 - gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; 65.135 - for (ci = L->base_ci; ci <= L->ci; ci++) { 65.136 - ci->top = (ci->top - oldstack) + L->stack; 65.137 - ci->base = (ci->base - oldstack) + L->stack; 65.138 - ci->func = (ci->func - oldstack) + L->stack; 65.139 - } 65.140 - L->base = (L->base - oldstack) + L->stack; 65.141 -} 65.142 - 65.143 - 65.144 -void luaD_reallocstack (lua_State *L, int newsize) { 65.145 - TValue *oldstack = L->stack; 65.146 - int realsize = newsize + 1 + EXTRA_STACK; 65.147 - lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); 65.148 - luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue); 65.149 - L->stacksize = realsize; 65.150 - L->stack_last = L->stack+newsize; 65.151 - correctstack(L, oldstack); 65.152 -} 65.153 - 65.154 - 65.155 -void luaD_reallocCI (lua_State *L, int newsize) { 65.156 - CallInfo *oldci = L->base_ci; 65.157 - luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo); 65.158 - L->size_ci = newsize; 65.159 - L->ci = (L->ci - oldci) + L->base_ci; 65.160 - L->end_ci = L->base_ci + L->size_ci - 1; 65.161 -} 65.162 - 65.163 - 65.164 -void luaD_growstack (lua_State *L, int n) { 65.165 - if (n <= L->stacksize) /* double size is enough? */ 65.166 - luaD_reallocstack(L, 2*L->stacksize); 65.167 - else 65.168 - luaD_reallocstack(L, L->stacksize + n); 65.169 -} 65.170 - 65.171 - 65.172 -static CallInfo *growCI (lua_State *L) { 65.173 - if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */ 65.174 - luaD_throw(L, LUA_ERRERR); 65.175 - else { 65.176 - luaD_reallocCI(L, 2*L->size_ci); 65.177 - if (L->size_ci > LUAI_MAXCALLS) 65.178 - luaG_runerror(L, "stack overflow"); 65.179 - } 65.180 - return ++L->ci; 65.181 -} 65.182 - 65.183 - 65.184 -void luaD_callhook (lua_State *L, int event, int line) { 65.185 - lua_Hook hook = L->hook; 65.186 - if (hook && L->allowhook) { 65.187 - ptrdiff_t top = savestack(L, L->top); 65.188 - ptrdiff_t ci_top = savestack(L, L->ci->top); 65.189 - lua_Debug ar; 65.190 - ar.event = event; 65.191 - ar.currentline = line; 65.192 - if (event == LUA_HOOKTAILRET) 65.193 - ar.i_ci = 0; /* tail call; no debug information about it */ 65.194 - else 65.195 - ar.i_ci = cast_int(L->ci - L->base_ci); 65.196 - luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 65.197 - L->ci->top = L->top + LUA_MINSTACK; 65.198 - lua_assert(L->ci->top <= L->stack_last); 65.199 - L->allowhook = 0; /* cannot call hooks inside a hook */ 65.200 - lua_unlock(L); 65.201 - (*hook)(L, &ar); 65.202 - lua_lock(L); 65.203 - lua_assert(!L->allowhook); 65.204 - L->allowhook = 1; 65.205 - L->ci->top = restorestack(L, ci_top); 65.206 - L->top = restorestack(L, top); 65.207 - } 65.208 -} 65.209 - 65.210 - 65.211 -static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { 65.212 - int i; 65.213 - int nfixargs = p->numparams; 65.214 - Table *htab = NULL; 65.215 - StkId base, fixed; 65.216 - for (; actual < nfixargs; ++actual) 65.217 - setnilvalue(L->top++); 65.218 -#if defined(LUA_COMPAT_VARARG) 65.219 - if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */ 65.220 - int nvar = actual - nfixargs; /* number of extra arguments */ 65.221 - lua_assert(p->is_vararg & VARARG_HASARG); 65.222 - luaC_checkGC(L); 65.223 - htab = luaH_new(L, nvar, 1); /* create `arg' table */ 65.224 - for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */ 65.225 - setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i); 65.226 - /* store counter in field `n' */ 65.227 - setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar)); 65.228 - } 65.229 -#endif 65.230 - /* move fixed parameters to final position */ 65.231 - fixed = L->top - actual; /* first fixed argument */ 65.232 - base = L->top; /* final position of first argument */ 65.233 - for (i=0; i<nfixargs; i++) { 65.234 - setobjs2s(L, L->top++, fixed+i); 65.235 - setnilvalue(fixed+i); 65.236 - } 65.237 - /* add `arg' parameter */ 65.238 - if (htab) { 65.239 - sethvalue(L, L->top++, htab); 65.240 - lua_assert(iswhite(obj2gco(htab))); 65.241 - } 65.242 - return base; 65.243 -} 65.244 - 65.245 - 65.246 -static StkId tryfuncTM (lua_State *L, StkId func) { 65.247 - const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); 65.248 - StkId p; 65.249 - ptrdiff_t funcr = savestack(L, func); 65.250 - if (!ttisfunction(tm)) 65.251 - luaG_typeerror(L, func, "call"); 65.252 - /* Open a hole inside the stack at `func' */ 65.253 - for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); 65.254 - incr_top(L); 65.255 - func = restorestack(L, funcr); /* previous call may change stack */ 65.256 - setobj2s(L, func, tm); /* tag method is the new function to be called */ 65.257 - return func; 65.258 -} 65.259 - 65.260 - 65.261 - 65.262 -#define inc_ci(L) \ 65.263 - ((L->ci == L->end_ci) ? growCI(L) : \ 65.264 - (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci)) 65.265 - 65.266 - 65.267 -int luaD_precall (lua_State *L, StkId func, int nresults) { 65.268 - LClosure *cl; 65.269 - ptrdiff_t funcr; 65.270 - if (!ttisfunction(func)) /* `func' is not a function? */ 65.271 - func = tryfuncTM(L, func); /* check the `function' tag method */ 65.272 - funcr = savestack(L, func); 65.273 - cl = &clvalue(func)->l; 65.274 - L->ci->savedpc = L->savedpc; 65.275 - if (!cl->isC) { /* Lua function? prepare its call */ 65.276 - CallInfo *ci; 65.277 - StkId st, base; 65.278 - Proto *p = cl->p; 65.279 - luaD_checkstack(L, p->maxstacksize); 65.280 - func = restorestack(L, funcr); 65.281 - if (!p->is_vararg) { /* no varargs? */ 65.282 - base = func + 1; 65.283 - if (L->top > base + p->numparams) 65.284 - L->top = base + p->numparams; 65.285 - } 65.286 - else { /* vararg function */ 65.287 - int nargs = cast_int(L->top - func) - 1; 65.288 - base = adjust_varargs(L, p, nargs); 65.289 - func = restorestack(L, funcr); /* previous call may change the stack */ 65.290 - } 65.291 - ci = inc_ci(L); /* now `enter' new function */ 65.292 - ci->func = func; 65.293 - L->base = ci->base = base; 65.294 - ci->top = L->base + p->maxstacksize; 65.295 - lua_assert(ci->top <= L->stack_last); 65.296 - L->savedpc = p->code; /* starting point */ 65.297 - ci->tailcalls = 0; 65.298 - ci->nresults = nresults; 65.299 - for (st = L->top; st < ci->top; st++) 65.300 - setnilvalue(st); 65.301 - L->top = ci->top; 65.302 - if (L->hookmask & LUA_MASKCALL) { 65.303 - L->savedpc++; /* hooks assume 'pc' is already incremented */ 65.304 - luaD_callhook(L, LUA_HOOKCALL, -1); 65.305 - L->savedpc--; /* correct 'pc' */ 65.306 - } 65.307 - return PCRLUA; 65.308 - } 65.309 - else { /* if is a C function, call it */ 65.310 - CallInfo *ci; 65.311 - int n; 65.312 - luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 65.313 - ci = inc_ci(L); /* now `enter' new function */ 65.314 - ci->func = restorestack(L, funcr); 65.315 - L->base = ci->base = ci->func + 1; 65.316 - ci->top = L->top + LUA_MINSTACK; 65.317 - lua_assert(ci->top <= L->stack_last); 65.318 - ci->nresults = nresults; 65.319 - if (L->hookmask & LUA_MASKCALL) 65.320 - luaD_callhook(L, LUA_HOOKCALL, -1); 65.321 - lua_unlock(L); 65.322 - n = (*curr_func(L)->c.f)(L); /* do the actual call */ 65.323 - lua_lock(L); 65.324 - if (n < 0) /* yielding? */ 65.325 - return PCRYIELD; 65.326 - else { 65.327 - luaD_poscall(L, L->top - n); 65.328 - return PCRC; 65.329 - } 65.330 - } 65.331 -} 65.332 - 65.333 - 65.334 -static StkId callrethooks (lua_State *L, StkId firstResult) { 65.335 - ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */ 65.336 - luaD_callhook(L, LUA_HOOKRET, -1); 65.337 - if (f_isLua(L->ci)) { /* Lua function? */ 65.338 - while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */ 65.339 - luaD_callhook(L, LUA_HOOKTAILRET, -1); 65.340 - } 65.341 - return restorestack(L, fr); 65.342 -} 65.343 - 65.344 - 65.345 -int luaD_poscall (lua_State *L, StkId firstResult) { 65.346 - StkId res; 65.347 - int wanted, i; 65.348 - CallInfo *ci; 65.349 - if (L->hookmask & LUA_MASKRET) 65.350 - firstResult = callrethooks(L, firstResult); 65.351 - ci = L->ci--; 65.352 - res = ci->func; /* res == final position of 1st result */ 65.353 - wanted = ci->nresults; 65.354 - L->base = (ci - 1)->base; /* restore base */ 65.355 - L->savedpc = (ci - 1)->savedpc; /* restore savedpc */ 65.356 - /* move results to correct place */ 65.357 - for (i = wanted; i != 0 && firstResult < L->top; i--) 65.358 - setobjs2s(L, res++, firstResult++); 65.359 - while (i-- > 0) 65.360 - setnilvalue(res++); 65.361 - L->top = res; 65.362 - return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ 65.363 -} 65.364 - 65.365 - 65.366 -/* 65.367 -** Call a function (C or Lua). The function to be called is at *func. 65.368 -** The arguments are on the stack, right after the function. 65.369 -** When returns, all the results are on the stack, starting at the original 65.370 -** function position. 65.371 -*/ 65.372 -void luaD_call (lua_State *L, StkId func, int nResults) { 65.373 - if (++L->nCcalls >= LUAI_MAXCCALLS) { 65.374 - if (L->nCcalls == LUAI_MAXCCALLS) 65.375 - luaG_runerror(L, "C stack overflow"); 65.376 - else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) 65.377 - luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ 65.378 - } 65.379 - if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */ 65.380 - luaV_execute(L, 1); /* call it */ 65.381 - L->nCcalls--; 65.382 - luaC_checkGC(L); 65.383 -} 65.384 - 65.385 - 65.386 -static void resume (lua_State *L, void *ud) { 65.387 - StkId firstArg = cast(StkId, ud); 65.388 - CallInfo *ci = L->ci; 65.389 - if (L->status == 0) { /* start coroutine? */ 65.390 - lua_assert(ci == L->base_ci && firstArg > L->base); 65.391 - if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA) 65.392 - return; 65.393 - } 65.394 - else { /* resuming from previous yield */ 65.395 - lua_assert(L->status == LUA_YIELD); 65.396 - L->status = 0; 65.397 - if (!f_isLua(ci)) { /* `common' yield? */ 65.398 - /* finish interrupted execution of `OP_CALL' */ 65.399 - lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL || 65.400 - GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL); 65.401 - if (luaD_poscall(L, firstArg)) /* complete it... */ 65.402 - L->top = L->ci->top; /* and correct top if not multiple results */ 65.403 - } 65.404 - else /* yielded inside a hook: just continue its execution */ 65.405 - L->base = L->ci->base; 65.406 - } 65.407 - luaV_execute(L, cast_int(L->ci - L->base_ci)); 65.408 -} 65.409 - 65.410 - 65.411 -static int resume_error (lua_State *L, const char *msg) { 65.412 - L->top = L->ci->base; 65.413 - setsvalue2s(L, L->top, luaS_new(L, msg)); 65.414 - incr_top(L); 65.415 - lua_unlock(L); 65.416 - return LUA_ERRRUN; 65.417 -} 65.418 - 65.419 - 65.420 -LUA_API int lua_resume (lua_State *L, int nargs) { 65.421 - int status; 65.422 - lua_lock(L); 65.423 - if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci)) 65.424 - return resume_error(L, "cannot resume non-suspended coroutine"); 65.425 - if (L->nCcalls >= LUAI_MAXCCALLS) 65.426 - return resume_error(L, "C stack overflow"); 65.427 - luai_userstateresume(L, nargs); 65.428 - lua_assert(L->errfunc == 0); 65.429 - L->baseCcalls = ++L->nCcalls; 65.430 - status = luaD_rawrunprotected(L, resume, L->top - nargs); 65.431 - if (status != 0) { /* error? */ 65.432 - L->status = cast_byte(status); /* mark thread as `dead' */ 65.433 - luaD_seterrorobj(L, status, L->top); 65.434 - L->ci->top = L->top; 65.435 - } 65.436 - else { 65.437 - lua_assert(L->nCcalls == L->baseCcalls); 65.438 - status = L->status; 65.439 - } 65.440 - --L->nCcalls; 65.441 - lua_unlock(L); 65.442 - return status; 65.443 -} 65.444 - 65.445 - 65.446 -LUA_API int lua_yield (lua_State *L, int nresults) { 65.447 - luai_userstateyield(L, nresults); 65.448 - lua_lock(L); 65.449 - if (L->nCcalls > L->baseCcalls) 65.450 - luaG_runerror(L, "attempt to yield across metamethod/C-call boundary"); 65.451 - L->base = L->top - nresults; /* protect stack slots below */ 65.452 - L->status = LUA_YIELD; 65.453 - lua_unlock(L); 65.454 - return -1; 65.455 -} 65.456 - 65.457 - 65.458 -int luaD_pcall (lua_State *L, Pfunc func, void *u, 65.459 - ptrdiff_t old_top, ptrdiff_t ef) { 65.460 - int status; 65.461 - unsigned short oldnCcalls = L->nCcalls; 65.462 - ptrdiff_t old_ci = saveci(L, L->ci); 65.463 - lu_byte old_allowhooks = L->allowhook; 65.464 - ptrdiff_t old_errfunc = L->errfunc; 65.465 - L->errfunc = ef; 65.466 - status = luaD_rawrunprotected(L, func, u); 65.467 - if (status != 0) { /* an error occurred? */ 65.468 - StkId oldtop = restorestack(L, old_top); 65.469 - luaF_close(L, oldtop); /* close eventual pending closures */ 65.470 - luaD_seterrorobj(L, status, oldtop); 65.471 - L->nCcalls = oldnCcalls; 65.472 - L->ci = restoreci(L, old_ci); 65.473 - L->base = L->ci->base; 65.474 - L->savedpc = L->ci->savedpc; 65.475 - L->allowhook = old_allowhooks; 65.476 - restore_stack_limit(L); 65.477 - } 65.478 - L->errfunc = old_errfunc; 65.479 - return status; 65.480 -} 65.481 - 65.482 - 65.483 - 65.484 -/* 65.485 -** Execute a protected parser. 65.486 -*/ 65.487 -struct SParser { /* data to `f_parser' */ 65.488 - ZIO *z; 65.489 - Mbuffer buff; /* buffer to be used by the scanner */ 65.490 - const char *name; 65.491 -}; 65.492 - 65.493 -static void f_parser (lua_State *L, void *ud) { 65.494 - int i; 65.495 - Proto *tf; 65.496 - Closure *cl; 65.497 - struct SParser *p = cast(struct SParser *, ud); 65.498 - int c = luaZ_lookahead(p->z); 65.499 - luaC_checkGC(L); 65.500 - tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z, 65.501 - &p->buff, p->name); 65.502 - cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L))); 65.503 - cl->l.p = tf; 65.504 - for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */ 65.505 - cl->l.upvals[i] = luaF_newupval(L); 65.506 - setclvalue(L, L->top, cl); 65.507 - incr_top(L); 65.508 -} 65.509 - 65.510 - 65.511 -int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) { 65.512 - struct SParser p; 65.513 - int status; 65.514 - p.z = z; p.name = name; 65.515 - luaZ_initbuffer(L, &p.buff); 65.516 - status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); 65.517 - luaZ_freebuffer(L, &p.buff); 65.518 - return status; 65.519 -} 65.520 - 65.521 -
66.1 --- a/src/lua/src/ldo.h Sat Mar 03 11:04:56 2012 -0600 66.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 66.3 @@ -1,57 +0,0 @@ 66.4 -/* 66.5 -** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $ 66.6 -** Stack and Call structure of Lua 66.7 -** See Copyright Notice in lua.h 66.8 -*/ 66.9 - 66.10 -#ifndef ldo_h 66.11 -#define ldo_h 66.12 - 66.13 - 66.14 -#include "lobject.h" 66.15 -#include "lstate.h" 66.16 -#include "lzio.h" 66.17 - 66.18 - 66.19 -#define luaD_checkstack(L,n) \ 66.20 - if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \ 66.21 - luaD_growstack(L, n); \ 66.22 - else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); 66.23 - 66.24 - 66.25 -#define incr_top(L) {luaD_checkstack(L,1); L->top++;} 66.26 - 66.27 -#define savestack(L,p) ((char *)(p) - (char *)L->stack) 66.28 -#define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 66.29 - 66.30 -#define saveci(L,p) ((char *)(p) - (char *)L->base_ci) 66.31 -#define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) 66.32 - 66.33 - 66.34 -/* results from luaD_precall */ 66.35 -#define PCRLUA 0 /* initiated a call to a Lua function */ 66.36 -#define PCRC 1 /* did a call to a C function */ 66.37 -#define PCRYIELD 2 /* C funtion yielded */ 66.38 - 66.39 - 66.40 -/* type of protected functions, to be ran by `runprotected' */ 66.41 -typedef void (*Pfunc) (lua_State *L, void *ud); 66.42 - 66.43 -LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name); 66.44 -LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line); 66.45 -LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 66.46 -LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 66.47 -LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 66.48 - ptrdiff_t oldtop, ptrdiff_t ef); 66.49 -LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 66.50 -LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize); 66.51 -LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 66.52 -LUAI_FUNC void luaD_growstack (lua_State *L, int n); 66.53 - 66.54 -LUAI_FUNC void luaD_throw (lua_State *L, int errcode); 66.55 -LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 66.56 - 66.57 -LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); 66.58 - 66.59 -#endif 66.60 -
67.1 --- a/src/lua/src/ldump.c Sat Mar 03 11:04:56 2012 -0600 67.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 67.3 @@ -1,164 +0,0 @@ 67.4 -/* 67.5 -** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 67.6 -** save precompiled Lua chunks 67.7 -** See Copyright Notice in lua.h 67.8 -*/ 67.9 - 67.10 -#include <stddef.h> 67.11 - 67.12 -#define ldump_c 67.13 -#define LUA_CORE 67.14 - 67.15 -#include "lua.h" 67.16 - 67.17 -#include "lobject.h" 67.18 -#include "lstate.h" 67.19 -#include "lundump.h" 67.20 - 67.21 -typedef struct { 67.22 - lua_State* L; 67.23 - lua_Writer writer; 67.24 - void* data; 67.25 - int strip; 67.26 - int status; 67.27 -} DumpState; 67.28 - 67.29 -#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 67.30 -#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 67.31 - 67.32 -static void DumpBlock(const void* b, size_t size, DumpState* D) 67.33 -{ 67.34 - if (D->status==0) 67.35 - { 67.36 - lua_unlock(D->L); 67.37 - D->status=(*D->writer)(D->L,b,size,D->data); 67.38 - lua_lock(D->L); 67.39 - } 67.40 -} 67.41 - 67.42 -static void DumpChar(int y, DumpState* D) 67.43 -{ 67.44 - char x=(char)y; 67.45 - DumpVar(x,D); 67.46 -} 67.47 - 67.48 -static void DumpInt(int x, DumpState* D) 67.49 -{ 67.50 - DumpVar(x,D); 67.51 -} 67.52 - 67.53 -static void DumpNumber(lua_Number x, DumpState* D) 67.54 -{ 67.55 - DumpVar(x,D); 67.56 -} 67.57 - 67.58 -static void DumpVector(const void* b, int n, size_t size, DumpState* D) 67.59 -{ 67.60 - DumpInt(n,D); 67.61 - DumpMem(b,n,size,D); 67.62 -} 67.63 - 67.64 -static void DumpString(const TString* s, DumpState* D) 67.65 -{ 67.66 - if (s==NULL || getstr(s)==NULL) 67.67 - { 67.68 - size_t size=0; 67.69 - DumpVar(size,D); 67.70 - } 67.71 - else 67.72 - { 67.73 - size_t size=s->tsv.len+1; /* include trailing '\0' */ 67.74 - DumpVar(size,D); 67.75 - DumpBlock(getstr(s),size,D); 67.76 - } 67.77 -} 67.78 - 67.79 -#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 67.80 - 67.81 -static void DumpFunction(const Proto* f, const TString* p, DumpState* D); 67.82 - 67.83 -static void DumpConstants(const Proto* f, DumpState* D) 67.84 -{ 67.85 - int i,n=f->sizek; 67.86 - DumpInt(n,D); 67.87 - for (i=0; i<n; i++) 67.88 - { 67.89 - const TValue* o=&f->k[i]; 67.90 - DumpChar(ttype(o),D); 67.91 - switch (ttype(o)) 67.92 - { 67.93 - case LUA_TNIL: 67.94 - break; 67.95 - case LUA_TBOOLEAN: 67.96 - DumpChar(bvalue(o),D); 67.97 - break; 67.98 - case LUA_TNUMBER: 67.99 - DumpNumber(nvalue(o),D); 67.100 - break; 67.101 - case LUA_TSTRING: 67.102 - DumpString(rawtsvalue(o),D); 67.103 - break; 67.104 - default: 67.105 - lua_assert(0); /* cannot happen */ 67.106 - break; 67.107 - } 67.108 - } 67.109 - n=f->sizep; 67.110 - DumpInt(n,D); 67.111 - for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D); 67.112 -} 67.113 - 67.114 -static void DumpDebug(const Proto* f, DumpState* D) 67.115 -{ 67.116 - int i,n; 67.117 - n= (D->strip) ? 0 : f->sizelineinfo; 67.118 - DumpVector(f->lineinfo,n,sizeof(int),D); 67.119 - n= (D->strip) ? 0 : f->sizelocvars; 67.120 - DumpInt(n,D); 67.121 - for (i=0; i<n; i++) 67.122 - { 67.123 - DumpString(f->locvars[i].varname,D); 67.124 - DumpInt(f->locvars[i].startpc,D); 67.125 - DumpInt(f->locvars[i].endpc,D); 67.126 - } 67.127 - n= (D->strip) ? 0 : f->sizeupvalues; 67.128 - DumpInt(n,D); 67.129 - for (i=0; i<n; i++) DumpString(f->upvalues[i],D); 67.130 -} 67.131 - 67.132 -static void DumpFunction(const Proto* f, const TString* p, DumpState* D) 67.133 -{ 67.134 - DumpString((f->source==p || D->strip) ? NULL : f->source,D); 67.135 - DumpInt(f->linedefined,D); 67.136 - DumpInt(f->lastlinedefined,D); 67.137 - DumpChar(f->nups,D); 67.138 - DumpChar(f->numparams,D); 67.139 - DumpChar(f->is_vararg,D); 67.140 - DumpChar(f->maxstacksize,D); 67.141 - DumpCode(f,D); 67.142 - DumpConstants(f,D); 67.143 - DumpDebug(f,D); 67.144 -} 67.145 - 67.146 -static void DumpHeader(DumpState* D) 67.147 -{ 67.148 - char h[LUAC_HEADERSIZE]; 67.149 - luaU_header(h); 67.150 - DumpBlock(h,LUAC_HEADERSIZE,D); 67.151 -} 67.152 - 67.153 -/* 67.154 -** dump Lua function as precompiled chunk 67.155 -*/ 67.156 -int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 67.157 -{ 67.158 - DumpState D; 67.159 - D.L=L; 67.160 - D.writer=w; 67.161 - D.data=data; 67.162 - D.strip=strip; 67.163 - D.status=0; 67.164 - DumpHeader(&D); 67.165 - DumpFunction(f,NULL,&D); 67.166 - return D.status; 67.167 -}
68.1 --- a/src/lua/src/lfunc.c Sat Mar 03 11:04:56 2012 -0600 68.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 68.3 @@ -1,174 +0,0 @@ 68.4 -/* 68.5 -** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $ 68.6 -** Auxiliary functions to manipulate prototypes and closures 68.7 -** See Copyright Notice in lua.h 68.8 -*/ 68.9 - 68.10 - 68.11 -#include <stddef.h> 68.12 - 68.13 -#define lfunc_c 68.14 -#define LUA_CORE 68.15 - 68.16 -#include "lua.h" 68.17 - 68.18 -#include "lfunc.h" 68.19 -#include "lgc.h" 68.20 -#include "lmem.h" 68.21 -#include "lobject.h" 68.22 -#include "lstate.h" 68.23 - 68.24 - 68.25 - 68.26 -Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { 68.27 - Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); 68.28 - luaC_link(L, obj2gco(c), LUA_TFUNCTION); 68.29 - c->c.isC = 1; 68.30 - c->c.env = e; 68.31 - c->c.nupvalues = cast_byte(nelems); 68.32 - return c; 68.33 -} 68.34 - 68.35 - 68.36 -Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { 68.37 - Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); 68.38 - luaC_link(L, obj2gco(c), LUA_TFUNCTION); 68.39 - c->l.isC = 0; 68.40 - c->l.env = e; 68.41 - c->l.nupvalues = cast_byte(nelems); 68.42 - while (nelems--) c->l.upvals[nelems] = NULL; 68.43 - return c; 68.44 -} 68.45 - 68.46 - 68.47 -UpVal *luaF_newupval (lua_State *L) { 68.48 - UpVal *uv = luaM_new(L, UpVal); 68.49 - luaC_link(L, obj2gco(uv), LUA_TUPVAL); 68.50 - uv->v = &uv->u.value; 68.51 - setnilvalue(uv->v); 68.52 - return uv; 68.53 -} 68.54 - 68.55 - 68.56 -UpVal *luaF_findupval (lua_State *L, StkId level) { 68.57 - global_State *g = G(L); 68.58 - GCObject **pp = &L->openupval; 68.59 - UpVal *p; 68.60 - UpVal *uv; 68.61 - while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) { 68.62 - lua_assert(p->v != &p->u.value); 68.63 - if (p->v == level) { /* found a corresponding upvalue? */ 68.64 - if (isdead(g, obj2gco(p))) /* is it dead? */ 68.65 - changewhite(obj2gco(p)); /* ressurect it */ 68.66 - return p; 68.67 - } 68.68 - pp = &p->next; 68.69 - } 68.70 - uv = luaM_new(L, UpVal); /* not found: create a new one */ 68.71 - uv->tt = LUA_TUPVAL; 68.72 - uv->marked = luaC_white(g); 68.73 - uv->v = level; /* current value lives in the stack */ 68.74 - uv->next = *pp; /* chain it in the proper position */ 68.75 - *pp = obj2gco(uv); 68.76 - uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 68.77 - uv->u.l.next = g->uvhead.u.l.next; 68.78 - uv->u.l.next->u.l.prev = uv; 68.79 - g->uvhead.u.l.next = uv; 68.80 - lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 68.81 - return uv; 68.82 -} 68.83 - 68.84 - 68.85 -static void unlinkupval (UpVal *uv) { 68.86 - lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 68.87 - uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 68.88 - uv->u.l.prev->u.l.next = uv->u.l.next; 68.89 -} 68.90 - 68.91 - 68.92 -void luaF_freeupval (lua_State *L, UpVal *uv) { 68.93 - if (uv->v != &uv->u.value) /* is it open? */ 68.94 - unlinkupval(uv); /* remove from open list */ 68.95 - luaM_free(L, uv); /* free upvalue */ 68.96 -} 68.97 - 68.98 - 68.99 -void luaF_close (lua_State *L, StkId level) { 68.100 - UpVal *uv; 68.101 - global_State *g = G(L); 68.102 - while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) { 68.103 - GCObject *o = obj2gco(uv); 68.104 - lua_assert(!isblack(o) && uv->v != &uv->u.value); 68.105 - L->openupval = uv->next; /* remove from `open' list */ 68.106 - if (isdead(g, o)) 68.107 - luaF_freeupval(L, uv); /* free upvalue */ 68.108 - else { 68.109 - unlinkupval(uv); 68.110 - setobj(L, &uv->u.value, uv->v); 68.111 - uv->v = &uv->u.value; /* now current value lives here */ 68.112 - luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ 68.113 - } 68.114 - } 68.115 -} 68.116 - 68.117 - 68.118 -Proto *luaF_newproto (lua_State *L) { 68.119 - Proto *f = luaM_new(L, Proto); 68.120 - luaC_link(L, obj2gco(f), LUA_TPROTO); 68.121 - f->k = NULL; 68.122 - f->sizek = 0; 68.123 - f->p = NULL; 68.124 - f->sizep = 0; 68.125 - f->code = NULL; 68.126 - f->sizecode = 0; 68.127 - f->sizelineinfo = 0; 68.128 - f->sizeupvalues = 0; 68.129 - f->nups = 0; 68.130 - f->upvalues = NULL; 68.131 - f->numparams = 0; 68.132 - f->is_vararg = 0; 68.133 - f->maxstacksize = 0; 68.134 - f->lineinfo = NULL; 68.135 - f->sizelocvars = 0; 68.136 - f->locvars = NULL; 68.137 - f->linedefined = 0; 68.138 - f->lastlinedefined = 0; 68.139 - f->source = NULL; 68.140 - return f; 68.141 -} 68.142 - 68.143 - 68.144 -void luaF_freeproto (lua_State *L, Proto *f) { 68.145 - luaM_freearray(L, f->code, f->sizecode, Instruction); 68.146 - luaM_freearray(L, f->p, f->sizep, Proto *); 68.147 - luaM_freearray(L, f->k, f->sizek, TValue); 68.148 - luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); 68.149 - luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); 68.150 - luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); 68.151 - luaM_free(L, f); 68.152 -} 68.153 - 68.154 - 68.155 -void luaF_freeclosure (lua_State *L, Closure *c) { 68.156 - int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : 68.157 - sizeLclosure(c->l.nupvalues); 68.158 - luaM_freemem(L, c, size); 68.159 -} 68.160 - 68.161 - 68.162 -/* 68.163 -** Look for n-th local variable at line `line' in function `func'. 68.164 -** Returns NULL if not found. 68.165 -*/ 68.166 -const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 68.167 - int i; 68.168 - for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) { 68.169 - if (pc < f->locvars[i].endpc) { /* is variable active? */ 68.170 - local_number--; 68.171 - if (local_number == 0) 68.172 - return getstr(f->locvars[i].varname); 68.173 - } 68.174 - } 68.175 - return NULL; /* not found */ 68.176 -} 68.177 -
69.1 --- a/src/lua/src/lfunc.h Sat Mar 03 11:04:56 2012 -0600 69.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 69.3 @@ -1,34 +0,0 @@ 69.4 -/* 69.5 -** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $ 69.6 -** Auxiliary functions to manipulate prototypes and closures 69.7 -** See Copyright Notice in lua.h 69.8 -*/ 69.9 - 69.10 -#ifndef lfunc_h 69.11 -#define lfunc_h 69.12 - 69.13 - 69.14 -#include "lobject.h" 69.15 - 69.16 - 69.17 -#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 69.18 - cast(int, sizeof(TValue)*((n)-1))) 69.19 - 69.20 -#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 69.21 - cast(int, sizeof(TValue *)*((n)-1))) 69.22 - 69.23 - 69.24 -LUAI_FUNC Proto *luaF_newproto (lua_State *L); 69.25 -LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); 69.26 -LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); 69.27 -LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 69.28 -LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 69.29 -LUAI_FUNC void luaF_close (lua_State *L, StkId level); 69.30 -LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 69.31 -LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c); 69.32 -LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 69.33 -LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 69.34 - int pc); 69.35 - 69.36 - 69.37 -#endif
70.1 --- a/src/lua/src/lgc.c Sat Mar 03 11:04:56 2012 -0600 70.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 70.3 @@ -1,711 +0,0 @@ 70.4 -/* 70.5 -** $Id: lgc.c,v 2.38.1.1 2007/12/27 13:02:25 roberto Exp $ 70.6 -** Garbage Collector 70.7 -** See Copyright Notice in lua.h 70.8 -*/ 70.9 - 70.10 -#include <string.h> 70.11 - 70.12 -#define lgc_c 70.13 -#define LUA_CORE 70.14 - 70.15 -#include "lua.h" 70.16 - 70.17 -#include "ldebug.h" 70.18 -#include "ldo.h" 70.19 -#include "lfunc.h" 70.20 -#include "lgc.h" 70.21 -#include "lmem.h" 70.22 -#include "lobject.h" 70.23 -#include "lstate.h" 70.24 -#include "lstring.h" 70.25 -#include "ltable.h" 70.26 -#include "ltm.h" 70.27 - 70.28 - 70.29 -#define GCSTEPSIZE 1024u 70.30 -#define GCSWEEPMAX 40 70.31 -#define GCSWEEPCOST 10 70.32 -#define GCFINALIZECOST 100 70.33 - 70.34 - 70.35 -#define maskmarks cast_byte(~(bitmask(BLACKBIT)|WHITEBITS)) 70.36 - 70.37 -#define makewhite(g,x) \ 70.38 - ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g))) 70.39 - 70.40 -#define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) 70.41 -#define black2gray(x) resetbit((x)->gch.marked, BLACKBIT) 70.42 - 70.43 -#define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT) 70.44 - 70.45 - 70.46 -#define isfinalized(u) testbit((u)->marked, FINALIZEDBIT) 70.47 -#define markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT) 70.48 - 70.49 - 70.50 -#define KEYWEAK bitmask(KEYWEAKBIT) 70.51 -#define VALUEWEAK bitmask(VALUEWEAKBIT) 70.52 - 70.53 - 70.54 - 70.55 -#define markvalue(g,o) { checkconsistency(o); \ 70.56 - if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); } 70.57 - 70.58 -#define markobject(g,t) { if (iswhite(obj2gco(t))) \ 70.59 - reallymarkobject(g, obj2gco(t)); } 70.60 - 70.61 - 70.62 -#define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpause) 70.63 - 70.64 - 70.65 -static void removeentry (Node *n) { 70.66 - lua_assert(ttisnil(gval(n))); 70.67 - if (iscollectable(gkey(n))) 70.68 - setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */ 70.69 -} 70.70 - 70.71 - 70.72 -static void reallymarkobject (global_State *g, GCObject *o) { 70.73 - lua_assert(iswhite(o) && !isdead(g, o)); 70.74 - white2gray(o); 70.75 - switch (o->gch.tt) { 70.76 - case LUA_TSTRING: { 70.77 - return; 70.78 - } 70.79 - case LUA_TUSERDATA: { 70.80 - Table *mt = gco2u(o)->metatable; 70.81 - gray2black(o); /* udata are never gray */ 70.82 - if (mt) markobject(g, mt); 70.83 - markobject(g, gco2u(o)->env); 70.84 - return; 70.85 - } 70.86 - case LUA_TUPVAL: { 70.87 - UpVal *uv = gco2uv(o); 70.88 - markvalue(g, uv->v); 70.89 - if (uv->v == &uv->u.value) /* closed? */ 70.90 - gray2black(o); /* open upvalues are never black */ 70.91 - return; 70.92 - } 70.93 - case LUA_TFUNCTION: { 70.94 - gco2cl(o)->c.gclist = g->gray; 70.95 - g->gray = o; 70.96 - break; 70.97 - } 70.98 - case LUA_TTABLE: { 70.99 - gco2h(o)->gclist = g->gray; 70.100 - g->gray = o; 70.101 - break; 70.102 - } 70.103 - case LUA_TTHREAD: { 70.104 - gco2th(o)->gclist = g->gray; 70.105 - g->gray = o; 70.106 - break; 70.107 - } 70.108 - case LUA_TPROTO: { 70.109 - gco2p(o)->gclist = g->gray; 70.110 - g->gray = o; 70.111 - break; 70.112 - } 70.113 - default: lua_assert(0); 70.114 - } 70.115 -} 70.116 - 70.117 - 70.118 -static void marktmu (global_State *g) { 70.119 - GCObject *u = g->tmudata; 70.120 - if (u) { 70.121 - do { 70.122 - u = u->gch.next; 70.123 - makewhite(g, u); /* may be marked, if left from previous GC */ 70.124 - reallymarkobject(g, u); 70.125 - } while (u != g->tmudata); 70.126 - } 70.127 -} 70.128 - 70.129 - 70.130 -/* move `dead' udata that need finalization to list `tmudata' */ 70.131 -size_t luaC_separateudata (lua_State *L, int all) { 70.132 - global_State *g = G(L); 70.133 - size_t deadmem = 0; 70.134 - GCObject **p = &g->mainthread->next; 70.135 - GCObject *curr; 70.136 - while ((curr = *p) != NULL) { 70.137 - if (!(iswhite(curr) || all) || isfinalized(gco2u(curr))) 70.138 - p = &curr->gch.next; /* don't bother with them */ 70.139 - else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) { 70.140 - markfinalized(gco2u(curr)); /* don't need finalization */ 70.141 - p = &curr->gch.next; 70.142 - } 70.143 - else { /* must call its gc method */ 70.144 - deadmem += sizeudata(gco2u(curr)); 70.145 - markfinalized(gco2u(curr)); 70.146 - *p = curr->gch.next; 70.147 - /* link `curr' at the end of `tmudata' list */ 70.148 - if (g->tmudata == NULL) /* list is empty? */ 70.149 - g->tmudata = curr->gch.next = curr; /* creates a circular list */ 70.150 - else { 70.151 - curr->gch.next = g->tmudata->gch.next; 70.152 - g->tmudata->gch.next = curr; 70.153 - g->tmudata = curr; 70.154 - } 70.155 - } 70.156 - } 70.157 - return deadmem; 70.158 -} 70.159 - 70.160 - 70.161 -static int traversetable (global_State *g, Table *h) { 70.162 - int i; 70.163 - int weakkey = 0; 70.164 - int weakvalue = 0; 70.165 - const TValue *mode; 70.166 - if (h->metatable) 70.167 - markobject(g, h->metatable); 70.168 - mode = gfasttm(g, h->metatable, TM_MODE); 70.169 - if (mode && ttisstring(mode)) { /* is there a weak mode? */ 70.170 - weakkey = (strchr(svalue(mode), 'k') != NULL); 70.171 - weakvalue = (strchr(svalue(mode), 'v') != NULL); 70.172 - if (weakkey || weakvalue) { /* is really weak? */ 70.173 - h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */ 70.174 - h->marked |= cast_byte((weakkey << KEYWEAKBIT) | 70.175 - (weakvalue << VALUEWEAKBIT)); 70.176 - h->gclist = g->weak; /* must be cleared after GC, ... */ 70.177 - g->weak = obj2gco(h); /* ... so put in the appropriate list */ 70.178 - } 70.179 - } 70.180 - if (weakkey && weakvalue) return 1; 70.181 - if (!weakvalue) { 70.182 - i = h->sizearray; 70.183 - while (i--) 70.184 - markvalue(g, &h->array[i]); 70.185 - } 70.186 - i = sizenode(h); 70.187 - while (i--) { 70.188 - Node *n = gnode(h, i); 70.189 - lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n))); 70.190 - if (ttisnil(gval(n))) 70.191 - removeentry(n); /* remove empty entries */ 70.192 - else { 70.193 - lua_assert(!ttisnil(gkey(n))); 70.194 - if (!weakkey) markvalue(g, gkey(n)); 70.195 - if (!weakvalue) markvalue(g, gval(n)); 70.196 - } 70.197 - } 70.198 - return weakkey || weakvalue; 70.199 -} 70.200 - 70.201 - 70.202 -/* 70.203 -** All marks are conditional because a GC may happen while the 70.204 -** prototype is still being created 70.205 -*/ 70.206 -static void traverseproto (global_State *g, Proto *f) { 70.207 - int i; 70.208 - if (f->source) stringmark(f->source); 70.209 - for (i=0; i<f->sizek; i++) /* mark literals */ 70.210 - markvalue(g, &f->k[i]); 70.211 - for (i=0; i<f->sizeupvalues; i++) { /* mark upvalue names */ 70.212 - if (f->upvalues[i]) 70.213 - stringmark(f->upvalues[i]); 70.214 - } 70.215 - for (i=0; i<f->sizep; i++) { /* mark nested protos */ 70.216 - if (f->p[i]) 70.217 - markobject(g, f->p[i]); 70.218 - } 70.219 - for (i=0; i<f->sizelocvars; i++) { /* mark local-variable names */ 70.220 - if (f->locvars[i].varname) 70.221 - stringmark(f->locvars[i].varname); 70.222 - } 70.223 -} 70.224 - 70.225 - 70.226 - 70.227 -static void traverseclosure (global_State *g, Closure *cl) { 70.228 - markobject(g, cl->c.env); 70.229 - if (cl->c.isC) { 70.230 - int i; 70.231 - for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */ 70.232 - markvalue(g, &cl->c.upvalue[i]); 70.233 - } 70.234 - else { 70.235 - int i; 70.236 - lua_assert(cl->l.nupvalues == cl->l.p->nups); 70.237 - markobject(g, cl->l.p); 70.238 - for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */ 70.239 - markobject(g, cl->l.upvals[i]); 70.240 - } 70.241 -} 70.242 - 70.243 - 70.244 -static void checkstacksizes (lua_State *L, StkId max) { 70.245 - int ci_used = cast_int(L->ci - L->base_ci); /* number of `ci' in use */ 70.246 - int s_used = cast_int(max - L->stack); /* part of stack in use */ 70.247 - if (L->size_ci > LUAI_MAXCALLS) /* handling overflow? */ 70.248 - return; /* do not touch the stacks */ 70.249 - if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci) 70.250 - luaD_reallocCI(L, L->size_ci/2); /* still big enough... */ 70.251 - condhardstacktests(luaD_reallocCI(L, ci_used + 1)); 70.252 - if (4*s_used < L->stacksize && 70.253 - 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize) 70.254 - luaD_reallocstack(L, L->stacksize/2); /* still big enough... */ 70.255 - condhardstacktests(luaD_reallocstack(L, s_used)); 70.256 -} 70.257 - 70.258 - 70.259 -static void traversestack (global_State *g, lua_State *l) { 70.260 - StkId o, lim; 70.261 - CallInfo *ci; 70.262 - markvalue(g, gt(l)); 70.263 - lim = l->top; 70.264 - for (ci = l->base_ci; ci <= l->ci; ci++) { 70.265 - lua_assert(ci->top <= l->stack_last); 70.266 - if (lim < ci->top) lim = ci->top; 70.267 - } 70.268 - for (o = l->stack; o < l->top; o++) 70.269 - markvalue(g, o); 70.270 - for (; o <= lim; o++) 70.271 - setnilvalue(o); 70.272 - checkstacksizes(l, lim); 70.273 -} 70.274 - 70.275 - 70.276 -/* 70.277 -** traverse one gray object, turning it to black. 70.278 -** Returns `quantity' traversed. 70.279 -*/ 70.280 -static l_mem propagatemark (global_State *g) { 70.281 - GCObject *o = g->gray; 70.282 - lua_assert(isgray(o)); 70.283 - gray2black(o); 70.284 - switch (o->gch.tt) { 70.285 - case LUA_TTABLE: { 70.286 - Table *h = gco2h(o); 70.287 - g->gray = h->gclist; 70.288 - if (traversetable(g, h)) /* table is weak? */ 70.289 - black2gray(o); /* keep it gray */ 70.290 - return sizeof(Table) + sizeof(TValue) * h->sizearray + 70.291 - sizeof(Node) * sizenode(h); 70.292 - } 70.293 - case LUA_TFUNCTION: { 70.294 - Closure *cl = gco2cl(o); 70.295 - g->gray = cl->c.gclist; 70.296 - traverseclosure(g, cl); 70.297 - return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) : 70.298 - sizeLclosure(cl->l.nupvalues); 70.299 - } 70.300 - case LUA_TTHREAD: { 70.301 - lua_State *th = gco2th(o); 70.302 - g->gray = th->gclist; 70.303 - th->gclist = g->grayagain; 70.304 - g->grayagain = o; 70.305 - black2gray(o); 70.306 - traversestack(g, th); 70.307 - return sizeof(lua_State) + sizeof(TValue) * th->stacksize + 70.308 - sizeof(CallInfo) * th->size_ci; 70.309 - } 70.310 - case LUA_TPROTO: { 70.311 - Proto *p = gco2p(o); 70.312 - g->gray = p->gclist; 70.313 - traverseproto(g, p); 70.314 - return sizeof(Proto) + sizeof(Instruction) * p->sizecode + 70.315 - sizeof(Proto *) * p->sizep + 70.316 - sizeof(TValue) * p->sizek + 70.317 - sizeof(int) * p->sizelineinfo + 70.318 - sizeof(LocVar) * p->sizelocvars + 70.319 - sizeof(TString *) * p->sizeupvalues; 70.320 - } 70.321 - default: lua_assert(0); return 0; 70.322 - } 70.323 -} 70.324 - 70.325 - 70.326 -static size_t propagateall (global_State *g) { 70.327 - size_t m = 0; 70.328 - while (g->gray) m += propagatemark(g); 70.329 - return m; 70.330 -} 70.331 - 70.332 - 70.333 -/* 70.334 -** The next function tells whether a key or value can be cleared from 70.335 -** a weak table. Non-collectable objects are never removed from weak 70.336 -** tables. Strings behave as `values', so are never removed too. for 70.337 -** other objects: if really collected, cannot keep them; for userdata 70.338 -** being finalized, keep them in keys, but not in values 70.339 -*/ 70.340 -static int iscleared (const TValue *o, int iskey) { 70.341 - if (!iscollectable(o)) return 0; 70.342 - if (ttisstring(o)) { 70.343 - stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */ 70.344 - return 0; 70.345 - } 70.346 - return iswhite(gcvalue(o)) || 70.347 - (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o)))); 70.348 -} 70.349 - 70.350 - 70.351 -/* 70.352 -** clear collected entries from weaktables 70.353 -*/ 70.354 -static void cleartable (GCObject *l) { 70.355 - while (l) { 70.356 - Table *h = gco2h(l); 70.357 - int i = h->sizearray; 70.358 - lua_assert(testbit(h->marked, VALUEWEAKBIT) || 70.359 - testbit(h->marked, KEYWEAKBIT)); 70.360 - if (testbit(h->marked, VALUEWEAKBIT)) { 70.361 - while (i--) { 70.362 - TValue *o = &h->array[i]; 70.363 - if (iscleared(o, 0)) /* value was collected? */ 70.364 - setnilvalue(o); /* remove value */ 70.365 - } 70.366 - } 70.367 - i = sizenode(h); 70.368 - while (i--) { 70.369 - Node *n = gnode(h, i); 70.370 - if (!ttisnil(gval(n)) && /* non-empty entry? */ 70.371 - (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) { 70.372 - setnilvalue(gval(n)); /* remove value ... */ 70.373 - removeentry(n); /* remove entry from table */ 70.374 - } 70.375 - } 70.376 - l = h->gclist; 70.377 - } 70.378 -} 70.379 - 70.380 - 70.381 -static void freeobj (lua_State *L, GCObject *o) { 70.382 - switch (o->gch.tt) { 70.383 - case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; 70.384 - case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break; 70.385 - case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break; 70.386 - case LUA_TTABLE: luaH_free(L, gco2h(o)); break; 70.387 - case LUA_TTHREAD: { 70.388 - lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread); 70.389 - luaE_freethread(L, gco2th(o)); 70.390 - break; 70.391 - } 70.392 - case LUA_TSTRING: { 70.393 - G(L)->strt.nuse--; 70.394 - luaM_freemem(L, o, sizestring(gco2ts(o))); 70.395 - break; 70.396 - } 70.397 - case LUA_TUSERDATA: { 70.398 - luaM_freemem(L, o, sizeudata(gco2u(o))); 70.399 - break; 70.400 - } 70.401 - default: lua_assert(0); 70.402 - } 70.403 -} 70.404 - 70.405 - 70.406 - 70.407 -#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM) 70.408 - 70.409 - 70.410 -static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { 70.411 - GCObject *curr; 70.412 - global_State *g = G(L); 70.413 - int deadmask = otherwhite(g); 70.414 - while ((curr = *p) != NULL && count-- > 0) { 70.415 - if (curr->gch.tt == LUA_TTHREAD) /* sweep open upvalues of each thread */ 70.416 - sweepwholelist(L, &gco2th(curr)->openupval); 70.417 - if ((curr->gch.marked ^ WHITEBITS) & deadmask) { /* not dead? */ 70.418 - lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT)); 70.419 - makewhite(g, curr); /* make it white (for next cycle) */ 70.420 - p = &curr->gch.next; 70.421 - } 70.422 - else { /* must erase `curr' */ 70.423 - lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT)); 70.424 - *p = curr->gch.next; 70.425 - if (curr == g->rootgc) /* is the first element of the list? */ 70.426 - g->rootgc = curr->gch.next; /* adjust first */ 70.427 - freeobj(L, curr); 70.428 - } 70.429 - } 70.430 - return p; 70.431 -} 70.432 - 70.433 - 70.434 -static void checkSizes (lua_State *L) { 70.435 - global_State *g = G(L); 70.436 - /* check size of string hash */ 70.437 - if (g->strt.nuse < cast(lu_int32, g->strt.size/4) && 70.438 - g->strt.size > MINSTRTABSIZE*2) 70.439 - luaS_resize(L, g->strt.size/2); /* table is too big */ 70.440 - /* check size of buffer */ 70.441 - if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */ 70.442 - size_t newsize = luaZ_sizebuffer(&g->buff) / 2; 70.443 - luaZ_resizebuffer(L, &g->buff, newsize); 70.444 - } 70.445 -} 70.446 - 70.447 - 70.448 -static void GCTM (lua_State *L) { 70.449 - global_State *g = G(L); 70.450 - GCObject *o = g->tmudata->gch.next; /* get first element */ 70.451 - Udata *udata = rawgco2u(o); 70.452 - const TValue *tm; 70.453 - /* remove udata from `tmudata' */ 70.454 - if (o == g->tmudata) /* last element? */ 70.455 - g->tmudata = NULL; 70.456 - else 70.457 - g->tmudata->gch.next = udata->uv.next; 70.458 - udata->uv.next = g->mainthread->next; /* return it to `root' list */ 70.459 - g->mainthread->next = o; 70.460 - makewhite(g, o); 70.461 - tm = fasttm(L, udata->uv.metatable, TM_GC); 70.462 - if (tm != NULL) { 70.463 - lu_byte oldah = L->allowhook; 70.464 - lu_mem oldt = g->GCthreshold; 70.465 - L->allowhook = 0; /* stop debug hooks during GC tag method */ 70.466 - g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */ 70.467 - setobj2s(L, L->top, tm); 70.468 - setuvalue(L, L->top+1, udata); 70.469 - L->top += 2; 70.470 - luaD_call(L, L->top - 2, 0); 70.471 - L->allowhook = oldah; /* restore hooks */ 70.472 - g->GCthreshold = oldt; /* restore threshold */ 70.473 - } 70.474 -} 70.475 - 70.476 - 70.477 -/* 70.478 -** Call all GC tag methods 70.479 -*/ 70.480 -void luaC_callGCTM (lua_State *L) { 70.481 - while (G(L)->tmudata) 70.482 - GCTM(L); 70.483 -} 70.484 - 70.485 - 70.486 -void luaC_freeall (lua_State *L) { 70.487 - global_State *g = G(L); 70.488 - int i; 70.489 - g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* mask to collect all elements */ 70.490 - sweepwholelist(L, &g->rootgc); 70.491 - for (i = 0; i < g->strt.size; i++) /* free all string lists */ 70.492 - sweepwholelist(L, &g->strt.hash[i]); 70.493 -} 70.494 - 70.495 - 70.496 -static void markmt (global_State *g) { 70.497 - int i; 70.498 - for (i=0; i<NUM_TAGS; i++) 70.499 - if (g->mt[i]) markobject(g, g->mt[i]); 70.500 -} 70.501 - 70.502 - 70.503 -/* mark root set */ 70.504 -static void markroot (lua_State *L) { 70.505 - global_State *g = G(L); 70.506 - g->gray = NULL; 70.507 - g->grayagain = NULL; 70.508 - g->weak = NULL; 70.509 - markobject(g, g->mainthread); 70.510 - /* make global table be traversed before main stack */ 70.511 - markvalue(g, gt(g->mainthread)); 70.512 - markvalue(g, registry(L)); 70.513 - markmt(g); 70.514 - g->gcstate = GCSpropagate; 70.515 -} 70.516 - 70.517 - 70.518 -static void remarkupvals (global_State *g) { 70.519 - UpVal *uv; 70.520 - for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) { 70.521 - lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 70.522 - if (isgray(obj2gco(uv))) 70.523 - markvalue(g, uv->v); 70.524 - } 70.525 -} 70.526 - 70.527 - 70.528 -static void atomic (lua_State *L) { 70.529 - global_State *g = G(L); 70.530 - size_t udsize; /* total size of userdata to be finalized */ 70.531 - /* remark occasional upvalues of (maybe) dead threads */ 70.532 - remarkupvals(g); 70.533 - /* traverse objects cautch by write barrier and by 'remarkupvals' */ 70.534 - propagateall(g); 70.535 - /* remark weak tables */ 70.536 - g->gray = g->weak; 70.537 - g->weak = NULL; 70.538 - lua_assert(!iswhite(obj2gco(g->mainthread))); 70.539 - markobject(g, L); /* mark running thread */ 70.540 - markmt(g); /* mark basic metatables (again) */ 70.541 - propagateall(g); 70.542 - /* remark gray again */ 70.543 - g->gray = g->grayagain; 70.544 - g->grayagain = NULL; 70.545 - propagateall(g); 70.546 - udsize = luaC_separateudata(L, 0); /* separate userdata to be finalized */ 70.547 - marktmu(g); /* mark `preserved' userdata */ 70.548 - udsize += propagateall(g); /* remark, to propagate `preserveness' */ 70.549 - cleartable(g->weak); /* remove collected objects from weak tables */ 70.550 - /* flip current white */ 70.551 - g->currentwhite = cast_byte(otherwhite(g)); 70.552 - g->sweepstrgc = 0; 70.553 - g->sweepgc = &g->rootgc; 70.554 - g->gcstate = GCSsweepstring; 70.555 - g->estimate = g->totalbytes - udsize; /* first estimate */ 70.556 -} 70.557 - 70.558 - 70.559 -static l_mem singlestep (lua_State *L) { 70.560 - global_State *g = G(L); 70.561 - /*lua_checkmemory(L);*/ 70.562 - switch (g->gcstate) { 70.563 - case GCSpause: { 70.564 - markroot(L); /* start a new collection */ 70.565 - return 0; 70.566 - } 70.567 - case GCSpropagate: { 70.568 - if (g->gray) 70.569 - return propagatemark(g); 70.570 - else { /* no more `gray' objects */ 70.571 - atomic(L); /* finish mark phase */ 70.572 - return 0; 70.573 - } 70.574 - } 70.575 - case GCSsweepstring: { 70.576 - lu_mem old = g->totalbytes; 70.577 - sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]); 70.578 - if (g->sweepstrgc >= g->strt.size) /* nothing more to sweep? */ 70.579 - g->gcstate = GCSsweep; /* end sweep-string phase */ 70.580 - lua_assert(old >= g->totalbytes); 70.581 - g->estimate -= old - g->totalbytes; 70.582 - return GCSWEEPCOST; 70.583 - } 70.584 - case GCSsweep: { 70.585 - lu_mem old = g->totalbytes; 70.586 - g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); 70.587 - if (*g->sweepgc == NULL) { /* nothing more to sweep? */ 70.588 - checkSizes(L); 70.589 - g->gcstate = GCSfinalize; /* end sweep phase */ 70.590 - } 70.591 - lua_assert(old >= g->totalbytes); 70.592 - g->estimate -= old - g->totalbytes; 70.593 - return GCSWEEPMAX*GCSWEEPCOST; 70.594 - } 70.595 - case GCSfinalize: { 70.596 - if (g->tmudata) { 70.597 - GCTM(L); 70.598 - if (g->estimate > GCFINALIZECOST) 70.599 - g->estimate -= GCFINALIZECOST; 70.600 - return GCFINALIZECOST; 70.601 - } 70.602 - else { 70.603 - g->gcstate = GCSpause; /* end collection */ 70.604 - g->gcdept = 0; 70.605 - return 0; 70.606 - } 70.607 - } 70.608 - default: lua_assert(0); return 0; 70.609 - } 70.610 -} 70.611 - 70.612 - 70.613 -void luaC_step (lua_State *L) { 70.614 - global_State *g = G(L); 70.615 - l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul; 70.616 - if (lim == 0) 70.617 - lim = (MAX_LUMEM-1)/2; /* no limit */ 70.618 - g->gcdept += g->totalbytes - g->GCthreshold; 70.619 - do { 70.620 - lim -= singlestep(L); 70.621 - if (g->gcstate == GCSpause) 70.622 - break; 70.623 - } while (lim > 0); 70.624 - if (g->gcstate != GCSpause) { 70.625 - if (g->gcdept < GCSTEPSIZE) 70.626 - g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/g->gcstepmul;*/ 70.627 - else { 70.628 - g->gcdept -= GCSTEPSIZE; 70.629 - g->GCthreshold = g->totalbytes; 70.630 - } 70.631 - } 70.632 - else { 70.633 - lua_assert(g->totalbytes >= g->estimate); 70.634 - setthreshold(g); 70.635 - } 70.636 -} 70.637 - 70.638 - 70.639 -void luaC_fullgc (lua_State *L) { 70.640 - global_State *g = G(L); 70.641 - if (g->gcstate <= GCSpropagate) { 70.642 - /* reset sweep marks to sweep all elements (returning them to white) */ 70.643 - g->sweepstrgc = 0; 70.644 - g->sweepgc = &g->rootgc; 70.645 - /* reset other collector lists */ 70.646 - g->gray = NULL; 70.647 - g->grayagain = NULL; 70.648 - g->weak = NULL; 70.649 - g->gcstate = GCSsweepstring; 70.650 - } 70.651 - lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate); 70.652 - /* finish any pending sweep phase */ 70.653 - while (g->gcstate != GCSfinalize) { 70.654 - lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep); 70.655 - singlestep(L); 70.656 - } 70.657 - markroot(L); 70.658 - while (g->gcstate != GCSpause) { 70.659 - singlestep(L); 70.660 - } 70.661 - setthreshold(g); 70.662 -} 70.663 - 70.664 - 70.665 -void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) { 70.666 - global_State *g = G(L); 70.667 - lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); 70.668 - lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); 70.669 - lua_assert(ttype(&o->gch) != LUA_TTABLE); 70.670 - /* must keep invariant? */ 70.671 - if (g->gcstate == GCSpropagate) 70.672 - reallymarkobject(g, v); /* restore invariant */ 70.673 - else /* don't mind */ 70.674 - makewhite(g, o); /* mark as white just to avoid other barriers */ 70.675 -} 70.676 - 70.677 - 70.678 -void luaC_barrierback (lua_State *L, Table *t) { 70.679 - global_State *g = G(L); 70.680 - GCObject *o = obj2gco(t); 70.681 - lua_assert(isblack(o) && !isdead(g, o)); 70.682 - lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); 70.683 - black2gray(o); /* make table gray (again) */ 70.684 - t->gclist = g->grayagain; 70.685 - g->grayagain = o; 70.686 -} 70.687 - 70.688 - 70.689 -void luaC_link (lua_State *L, GCObject *o, lu_byte tt) { 70.690 - global_State *g = G(L); 70.691 - o->gch.next = g->rootgc; 70.692 - g->rootgc = o; 70.693 - o->gch.marked = luaC_white(g); 70.694 - o->gch.tt = tt; 70.695 -} 70.696 - 70.697 - 70.698 -void luaC_linkupval (lua_State *L, UpVal *uv) { 70.699 - global_State *g = G(L); 70.700 - GCObject *o = obj2gco(uv); 70.701 - o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */ 70.702 - g->rootgc = o; 70.703 - if (isgray(o)) { 70.704 - if (g->gcstate == GCSpropagate) { 70.705 - gray2black(o); /* closed upvalues need barrier */ 70.706 - luaC_barrier(L, uv, uv->v); 70.707 - } 70.708 - else { /* sweep phase: sweep it (turning it into white) */ 70.709 - makewhite(g, o); 70.710 - lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); 70.711 - } 70.712 - } 70.713 -} 70.714 -
71.1 --- a/src/lua/src/lgc.h Sat Mar 03 11:04:56 2012 -0600 71.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 71.3 @@ -1,110 +0,0 @@ 71.4 -/* 71.5 -** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $ 71.6 -** Garbage Collector 71.7 -** See Copyright Notice in lua.h 71.8 -*/ 71.9 - 71.10 -#ifndef lgc_h 71.11 -#define lgc_h 71.12 - 71.13 - 71.14 -#include "lobject.h" 71.15 - 71.16 - 71.17 -/* 71.18 -** Possible states of the Garbage Collector 71.19 -*/ 71.20 -#define GCSpause 0 71.21 -#define GCSpropagate 1 71.22 -#define GCSsweepstring 2 71.23 -#define GCSsweep 3 71.24 -#define GCSfinalize 4 71.25 - 71.26 - 71.27 -/* 71.28 -** some userful bit tricks 71.29 -*/ 71.30 -#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 71.31 -#define setbits(x,m) ((x) |= (m)) 71.32 -#define testbits(x,m) ((x) & (m)) 71.33 -#define bitmask(b) (1<<(b)) 71.34 -#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 71.35 -#define l_setbit(x,b) setbits(x, bitmask(b)) 71.36 -#define resetbit(x,b) resetbits(x, bitmask(b)) 71.37 -#define testbit(x,b) testbits(x, bitmask(b)) 71.38 -#define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) 71.39 -#define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2))) 71.40 -#define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2))) 71.41 - 71.42 - 71.43 - 71.44 -/* 71.45 -** Layout for bit use in `marked' field: 71.46 -** bit 0 - object is white (type 0) 71.47 -** bit 1 - object is white (type 1) 71.48 -** bit 2 - object is black 71.49 -** bit 3 - for userdata: has been finalized 71.50 -** bit 3 - for tables: has weak keys 71.51 -** bit 4 - for tables: has weak values 71.52 -** bit 5 - object is fixed (should not be collected) 71.53 -** bit 6 - object is "super" fixed (only the main thread) 71.54 -*/ 71.55 - 71.56 - 71.57 -#define WHITE0BIT 0 71.58 -#define WHITE1BIT 1 71.59 -#define BLACKBIT 2 71.60 -#define FINALIZEDBIT 3 71.61 -#define KEYWEAKBIT 3 71.62 -#define VALUEWEAKBIT 4 71.63 -#define FIXEDBIT 5 71.64 -#define SFIXEDBIT 6 71.65 -#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 71.66 - 71.67 - 71.68 -#define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) 71.69 -#define isblack(x) testbit((x)->gch.marked, BLACKBIT) 71.70 -#define isgray(x) (!isblack(x) && !iswhite(x)) 71.71 - 71.72 -#define otherwhite(g) (g->currentwhite ^ WHITEBITS) 71.73 -#define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) 71.74 - 71.75 -#define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 71.76 -#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 71.77 - 71.78 -#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 71.79 - 71.80 -#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 71.81 - 71.82 - 71.83 -#define luaC_checkGC(L) { \ 71.84 - condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ 71.85 - if (G(L)->totalbytes >= G(L)->GCthreshold) \ 71.86 - luaC_step(L); } 71.87 - 71.88 - 71.89 -#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 71.90 - luaC_barrierf(L,obj2gco(p),gcvalue(v)); } 71.91 - 71.92 -#define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ 71.93 - luaC_barrierback(L,t); } 71.94 - 71.95 -#define luaC_objbarrier(L,p,o) \ 71.96 - { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 71.97 - luaC_barrierf(L,obj2gco(p),obj2gco(o)); } 71.98 - 71.99 -#define luaC_objbarriert(L,t,o) \ 71.100 - { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } 71.101 - 71.102 -LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all); 71.103 -LUAI_FUNC void luaC_callGCTM (lua_State *L); 71.104 -LUAI_FUNC void luaC_freeall (lua_State *L); 71.105 -LUAI_FUNC void luaC_step (lua_State *L); 71.106 -LUAI_FUNC void luaC_fullgc (lua_State *L); 71.107 -LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); 71.108 -LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); 71.109 -LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); 71.110 -LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); 71.111 - 71.112 - 71.113 -#endif
72.1 --- a/src/lua/src/linit.c Sat Mar 03 11:04:56 2012 -0600 72.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 72.3 @@ -1,38 +0,0 @@ 72.4 -/* 72.5 -** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $ 72.6 -** Initialization of libraries for lua.c 72.7 -** See Copyright Notice in lua.h 72.8 -*/ 72.9 - 72.10 - 72.11 -#define linit_c 72.12 -#define LUA_LIB 72.13 - 72.14 -#include "lua.h" 72.15 - 72.16 -#include "lualib.h" 72.17 -#include "lauxlib.h" 72.18 - 72.19 - 72.20 -static const luaL_Reg lualibs[] = { 72.21 - {"", luaopen_base}, 72.22 - {LUA_LOADLIBNAME, luaopen_package}, 72.23 - {LUA_TABLIBNAME, luaopen_table}, 72.24 - {LUA_IOLIBNAME, luaopen_io}, 72.25 - {LUA_OSLIBNAME, luaopen_os}, 72.26 - {LUA_STRLIBNAME, luaopen_string}, 72.27 - {LUA_MATHLIBNAME, luaopen_math}, 72.28 - {LUA_DBLIBNAME, luaopen_debug}, 72.29 - {NULL, NULL} 72.30 -}; 72.31 - 72.32 - 72.33 -LUALIB_API void luaL_openlibs (lua_State *L) { 72.34 - const luaL_Reg *lib = lualibs; 72.35 - for (; lib->func; lib++) { 72.36 - lua_pushcfunction(L, lib->func); 72.37 - lua_pushstring(L, lib->name); 72.38 - lua_call(L, 1, 0); 72.39 - } 72.40 -} 72.41 -
73.1 --- a/src/lua/src/liolib.c Sat Mar 03 11:04:56 2012 -0600 73.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 73.3 @@ -1,553 +0,0 @@ 73.4 -/* 73.5 -** $Id: liolib.c,v 2.73.1.3 2008/01/18 17:47:43 roberto Exp $ 73.6 -** Standard I/O (and system) library 73.7 -** See Copyright Notice in lua.h 73.8 -*/ 73.9 - 73.10 - 73.11 -#include <errno.h> 73.12 -#include <stdio.h> 73.13 -#include <stdlib.h> 73.14 -#include <string.h> 73.15 - 73.16 -#define liolib_c 73.17 -#define LUA_LIB 73.18 - 73.19 -#include "lua.h" 73.20 - 73.21 -#include "lauxlib.h" 73.22 -#include "lualib.h" 73.23 - 73.24 - 73.25 - 73.26 -#define IO_INPUT 1 73.27 -#define IO_OUTPUT 2 73.28 - 73.29 - 73.30 -static const char *const fnames[] = {"input", "output"}; 73.31 - 73.32 - 73.33 -static int pushresult (lua_State *L, int i, const char *filename) { 73.34 - int en = errno; /* calls to Lua API may change this value */ 73.35 - if (i) { 73.36 - lua_pushboolean(L, 1); 73.37 - return 1; 73.38 - } 73.39 - else { 73.40 - lua_pushnil(L); 73.41 - if (filename) 73.42 - lua_pushfstring(L, "%s: %s", filename, strerror(en)); 73.43 - else 73.44 - lua_pushfstring(L, "%s", strerror(en)); 73.45 - lua_pushinteger(L, en); 73.46 - return 3; 73.47 - } 73.48 -} 73.49 - 73.50 - 73.51 -static void fileerror (lua_State *L, int arg, const char *filename) { 73.52 - lua_pushfstring(L, "%s: %s", filename, strerror(errno)); 73.53 - luaL_argerror(L, arg, lua_tostring(L, -1)); 73.54 -} 73.55 - 73.56 - 73.57 -#define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE)) 73.58 - 73.59 - 73.60 -static int io_type (lua_State *L) { 73.61 - void *ud; 73.62 - luaL_checkany(L, 1); 73.63 - ud = lua_touserdata(L, 1); 73.64 - lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); 73.65 - if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1)) 73.66 - lua_pushnil(L); /* not a file */ 73.67 - else if (*((FILE **)ud) == NULL) 73.68 - lua_pushliteral(L, "closed file"); 73.69 - else 73.70 - lua_pushliteral(L, "file"); 73.71 - return 1; 73.72 -} 73.73 - 73.74 - 73.75 -static FILE *tofile (lua_State *L) { 73.76 - FILE **f = tofilep(L); 73.77 - if (*f == NULL) 73.78 - luaL_error(L, "attempt to use a closed file"); 73.79 - return *f; 73.80 -} 73.81 - 73.82 - 73.83 - 73.84 -/* 73.85 -** When creating file handles, always creates a `closed' file handle 73.86 -** before opening the actual file; so, if there is a memory error, the 73.87 -** file is not left opened. 73.88 -*/ 73.89 -static FILE **newfile (lua_State *L) { 73.90 - FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *)); 73.91 - *pf = NULL; /* file handle is currently `closed' */ 73.92 - luaL_getmetatable(L, LUA_FILEHANDLE); 73.93 - lua_setmetatable(L, -2); 73.94 - return pf; 73.95 -} 73.96 - 73.97 - 73.98 -/* 73.99 -** function to (not) close the standard files stdin, stdout, and stderr 73.100 -*/ 73.101 -static int io_noclose (lua_State *L) { 73.102 - lua_pushnil(L); 73.103 - lua_pushliteral(L, "cannot close standard file"); 73.104 - return 2; 73.105 -} 73.106 - 73.107 - 73.108 -/* 73.109 -** function to close 'popen' files 73.110 -*/ 73.111 -static int io_pclose (lua_State *L) { 73.112 - FILE **p = tofilep(L); 73.113 - int ok = lua_pclose(L, *p); 73.114 - *p = NULL; 73.115 - return pushresult(L, ok, NULL); 73.116 -} 73.117 - 73.118 - 73.119 -/* 73.120 -** function to close regular files 73.121 -*/ 73.122 -static int io_fclose (lua_State *L) { 73.123 - FILE **p = tofilep(L); 73.124 - int ok = (fclose(*p) == 0); 73.125 - *p = NULL; 73.126 - return pushresult(L, ok, NULL); 73.127 -} 73.128 - 73.129 - 73.130 -static int aux_close (lua_State *L) { 73.131 - lua_getfenv(L, 1); 73.132 - lua_getfield(L, -1, "__close"); 73.133 - return (lua_tocfunction(L, -1))(L); 73.134 -} 73.135 - 73.136 - 73.137 -static int io_close (lua_State *L) { 73.138 - if (lua_isnone(L, 1)) 73.139 - lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT); 73.140 - tofile(L); /* make sure argument is a file */ 73.141 - return aux_close(L); 73.142 -} 73.143 - 73.144 - 73.145 -static int io_gc (lua_State *L) { 73.146 - FILE *f = *tofilep(L); 73.147 - /* ignore closed files */ 73.148 - if (f != NULL) 73.149 - aux_close(L); 73.150 - return 0; 73.151 -} 73.152 - 73.153 - 73.154 -static int io_tostring (lua_State *L) { 73.155 - FILE *f = *tofilep(L); 73.156 - if (f == NULL) 73.157 - lua_pushliteral(L, "file (closed)"); 73.158 - else 73.159 - lua_pushfstring(L, "file (%p)", f); 73.160 - return 1; 73.161 -} 73.162 - 73.163 - 73.164 -static int io_open (lua_State *L) { 73.165 - const char *filename = luaL_checkstring(L, 1); 73.166 - const char *mode = luaL_optstring(L, 2, "r"); 73.167 - FILE **pf = newfile(L); 73.168 - *pf = fopen(filename, mode); 73.169 - return (*pf == NULL) ? pushresult(L, 0, filename) : 1; 73.170 -} 73.171 - 73.172 - 73.173 -/* 73.174 -** this function has a separated environment, which defines the 73.175 -** correct __close for 'popen' files 73.176 -*/ 73.177 -static int io_popen (lua_State *L) { 73.178 - const char *filename = luaL_checkstring(L, 1); 73.179 - const char *mode = luaL_optstring(L, 2, "r"); 73.180 - FILE **pf = newfile(L); 73.181 - *pf = lua_popen(L, filename, mode); 73.182 - return (*pf == NULL) ? pushresult(L, 0, filename) : 1; 73.183 -} 73.184 - 73.185 - 73.186 -static int io_tmpfile (lua_State *L) { 73.187 - FILE **pf = newfile(L); 73.188 - *pf = tmpfile(); 73.189 - return (*pf == NULL) ? pushresult(L, 0, NULL) : 1; 73.190 -} 73.191 - 73.192 - 73.193 -static FILE *getiofile (lua_State *L, int findex) { 73.194 - FILE *f; 73.195 - lua_rawgeti(L, LUA_ENVIRONINDEX, findex); 73.196 - f = *(FILE **)lua_touserdata(L, -1); 73.197 - if (f == NULL) 73.198 - luaL_error(L, "standard %s file is closed", fnames[findex - 1]); 73.199 - return f; 73.200 -} 73.201 - 73.202 - 73.203 -static int g_iofile (lua_State *L, int f, const char *mode) { 73.204 - if (!lua_isnoneornil(L, 1)) { 73.205 - const char *filename = lua_tostring(L, 1); 73.206 - if (filename) { 73.207 - FILE **pf = newfile(L); 73.208 - *pf = fopen(filename, mode); 73.209 - if (*pf == NULL) 73.210 - fileerror(L, 1, filename); 73.211 - } 73.212 - else { 73.213 - tofile(L); /* check that it's a valid file handle */ 73.214 - lua_pushvalue(L, 1); 73.215 - } 73.216 - lua_rawseti(L, LUA_ENVIRONINDEX, f); 73.217 - } 73.218 - /* return current value */ 73.219 - lua_rawgeti(L, LUA_ENVIRONINDEX, f); 73.220 - return 1; 73.221 -} 73.222 - 73.223 - 73.224 -static int io_input (lua_State *L) { 73.225 - return g_iofile(L, IO_INPUT, "r"); 73.226 -} 73.227 - 73.228 - 73.229 -static int io_output (lua_State *L) { 73.230 - return g_iofile(L, IO_OUTPUT, "w"); 73.231 -} 73.232 - 73.233 - 73.234 -static int io_readline (lua_State *L); 73.235 - 73.236 - 73.237 -static void aux_lines (lua_State *L, int idx, int toclose) { 73.238 - lua_pushvalue(L, idx); 73.239 - lua_pushboolean(L, toclose); /* close/not close file when finished */ 73.240 - lua_pushcclosure(L, io_readline, 2); 73.241 -} 73.242 - 73.243 - 73.244 -static int f_lines (lua_State *L) { 73.245 - tofile(L); /* check that it's a valid file handle */ 73.246 - aux_lines(L, 1, 0); 73.247 - return 1; 73.248 -} 73.249 - 73.250 - 73.251 -static int io_lines (lua_State *L) { 73.252 - if (lua_isnoneornil(L, 1)) { /* no arguments? */ 73.253 - /* will iterate over default input */ 73.254 - lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT); 73.255 - return f_lines(L); 73.256 - } 73.257 - else { 73.258 - const char *filename = luaL_checkstring(L, 1); 73.259 - FILE **pf = newfile(L); 73.260 - *pf = fopen(filename, "r"); 73.261 - if (*pf == NULL) 73.262 - fileerror(L, 1, filename); 73.263 - aux_lines(L, lua_gettop(L), 1); 73.264 - return 1; 73.265 - } 73.266 -} 73.267 - 73.268 - 73.269 -/* 73.270 -** {====================================================== 73.271 -** READ 73.272 -** ======================================================= 73.273 -*/ 73.274 - 73.275 - 73.276 -static int read_number (lua_State *L, FILE *f) { 73.277 - lua_Number d; 73.278 - if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { 73.279 - lua_pushnumber(L, d); 73.280 - return 1; 73.281 - } 73.282 - else return 0; /* read fails */ 73.283 -} 73.284 - 73.285 - 73.286 -static int test_eof (lua_State *L, FILE *f) { 73.287 - int c = getc(f); 73.288 - ungetc(c, f); 73.289 - lua_pushlstring(L, NULL, 0); 73.290 - return (c != EOF); 73.291 -} 73.292 - 73.293 - 73.294 -static int read_line (lua_State *L, FILE *f) { 73.295 - luaL_Buffer b; 73.296 - luaL_buffinit(L, &b); 73.297 - for (;;) { 73.298 - size_t l; 73.299 - char *p = luaL_prepbuffer(&b); 73.300 - if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */ 73.301 - luaL_pushresult(&b); /* close buffer */ 73.302 - return (lua_objlen(L, -1) > 0); /* check whether read something */ 73.303 - } 73.304 - l = strlen(p); 73.305 - if (l == 0 || p[l-1] != '\n') 73.306 - luaL_addsize(&b, l); 73.307 - else { 73.308 - luaL_addsize(&b, l - 1); /* do not include `eol' */ 73.309 - luaL_pushresult(&b); /* close buffer */ 73.310 - return 1; /* read at least an `eol' */ 73.311 - } 73.312 - } 73.313 -} 73.314 - 73.315 - 73.316 -static int read_chars (lua_State *L, FILE *f, size_t n) { 73.317 - size_t rlen; /* how much to read */ 73.318 - size_t nr; /* number of chars actually read */ 73.319 - luaL_Buffer b; 73.320 - luaL_buffinit(L, &b); 73.321 - rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ 73.322 - do { 73.323 - char *p = luaL_prepbuffer(&b); 73.324 - if (rlen > n) rlen = n; /* cannot read more than asked */ 73.325 - nr = fread(p, sizeof(char), rlen, f); 73.326 - luaL_addsize(&b, nr); 73.327 - n -= nr; /* still have to read `n' chars */ 73.328 - } while (n > 0 && nr == rlen); /* until end of count or eof */ 73.329 - luaL_pushresult(&b); /* close buffer */ 73.330 - return (n == 0 || lua_objlen(L, -1) > 0); 73.331 -} 73.332 - 73.333 - 73.334 -static int g_read (lua_State *L, FILE *f, int first) { 73.335 - int nargs = lua_gettop(L) - 1; 73.336 - int success; 73.337 - int n; 73.338 - clearerr(f); 73.339 - if (nargs == 0) { /* no arguments? */ 73.340 - success = read_line(L, f); 73.341 - n = first+1; /* to return 1 result */ 73.342 - } 73.343 - else { /* ensure stack space for all results and for auxlib's buffer */ 73.344 - luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); 73.345 - success = 1; 73.346 - for (n = first; nargs-- && success; n++) { 73.347 - if (lua_type(L, n) == LUA_TNUMBER) { 73.348 - size_t l = (size_t)lua_tointeger(L, n); 73.349 - success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); 73.350 - } 73.351 - else { 73.352 - const char *p = lua_tostring(L, n); 73.353 - luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); 73.354 - switch (p[1]) { 73.355 - case 'n': /* number */ 73.356 - success = read_number(L, f); 73.357 - break; 73.358 - case 'l': /* line */ 73.359 - success = read_line(L, f); 73.360 - break; 73.361 - case 'a': /* file */ 73.362 - read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */ 73.363 - success = 1; /* always success */ 73.364 - break; 73.365 - default: 73.366 - return luaL_argerror(L, n, "invalid format"); 73.367 - } 73.368 - } 73.369 - } 73.370 - } 73.371 - if (ferror(f)) 73.372 - return pushresult(L, 0, NULL); 73.373 - if (!success) { 73.374 - lua_pop(L, 1); /* remove last result */ 73.375 - lua_pushnil(L); /* push nil instead */ 73.376 - } 73.377 - return n - first; 73.378 -} 73.379 - 73.380 - 73.381 -static int io_read (lua_State *L) { 73.382 - return g_read(L, getiofile(L, IO_INPUT), 1); 73.383 -} 73.384 - 73.385 - 73.386 -static int f_read (lua_State *L) { 73.387 - return g_read(L, tofile(L), 2); 73.388 -} 73.389 - 73.390 - 73.391 -static int io_readline (lua_State *L) { 73.392 - FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1)); 73.393 - int sucess; 73.394 - if (f == NULL) /* file is already closed? */ 73.395 - luaL_error(L, "file is already closed"); 73.396 - sucess = read_line(L, f); 73.397 - if (ferror(f)) 73.398 - return luaL_error(L, "%s", strerror(errno)); 73.399 - if (sucess) return 1; 73.400 - else { /* EOF */ 73.401 - if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */ 73.402 - lua_settop(L, 0); 73.403 - lua_pushvalue(L, lua_upvalueindex(1)); 73.404 - aux_close(L); /* close it */ 73.405 - } 73.406 - return 0; 73.407 - } 73.408 -} 73.409 - 73.410 -/* }====================================================== */ 73.411 - 73.412 - 73.413 -static int g_write (lua_State *L, FILE *f, int arg) { 73.414 - int nargs = lua_gettop(L) - 1; 73.415 - int status = 1; 73.416 - for (; nargs--; arg++) { 73.417 - if (lua_type(L, arg) == LUA_TNUMBER) { 73.418 - /* optimization: could be done exactly as for strings */ 73.419 - status = status && 73.420 - fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; 73.421 - } 73.422 - else { 73.423 - size_t l; 73.424 - const char *s = luaL_checklstring(L, arg, &l); 73.425 - status = status && (fwrite(s, sizeof(char), l, f) == l); 73.426 - } 73.427 - } 73.428 - return pushresult(L, status, NULL); 73.429 -} 73.430 - 73.431 - 73.432 -static int io_write (lua_State *L) { 73.433 - return g_write(L, getiofile(L, IO_OUTPUT), 1); 73.434 -} 73.435 - 73.436 - 73.437 -static int f_write (lua_State *L) { 73.438 - return g_write(L, tofile(L), 2); 73.439 -} 73.440 - 73.441 - 73.442 -static int f_seek (lua_State *L) { 73.443 - static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; 73.444 - static const char *const modenames[] = {"set", "cur", "end", NULL}; 73.445 - FILE *f = tofile(L); 73.446 - int op = luaL_checkoption(L, 2, "cur", modenames); 73.447 - long offset = luaL_optlong(L, 3, 0); 73.448 - op = fseek(f, offset, mode[op]); 73.449 - if (op) 73.450 - return pushresult(L, 0, NULL); /* error */ 73.451 - else { 73.452 - lua_pushinteger(L, ftell(f)); 73.453 - return 1; 73.454 - } 73.455 -} 73.456 - 73.457 - 73.458 -static int f_setvbuf (lua_State *L) { 73.459 - static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; 73.460 - static const char *const modenames[] = {"no", "full", "line", NULL}; 73.461 - FILE *f = tofile(L); 73.462 - int op = luaL_checkoption(L, 2, NULL, modenames); 73.463 - lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); 73.464 - int res = setvbuf(f, NULL, mode[op], sz); 73.465 - return pushresult(L, res == 0, NULL); 73.466 -} 73.467 - 73.468 - 73.469 - 73.470 -static int io_flush (lua_State *L) { 73.471 - return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); 73.472 -} 73.473 - 73.474 - 73.475 -static int f_flush (lua_State *L) { 73.476 - return pushresult(L, fflush(tofile(L)) == 0, NULL); 73.477 -} 73.478 - 73.479 - 73.480 -static const luaL_Reg iolib[] = { 73.481 - {"close", io_close}, 73.482 - {"flush", io_flush}, 73.483 - {"input", io_input}, 73.484 - {"lines", io_lines}, 73.485 - {"open", io_open}, 73.486 - {"output", io_output}, 73.487 - {"popen", io_popen}, 73.488 - {"read", io_read}, 73.489 - {"tmpfile", io_tmpfile}, 73.490 - {"type", io_type}, 73.491 - {"write", io_write}, 73.492 - {NULL, NULL} 73.493 -}; 73.494 - 73.495 - 73.496 -static const luaL_Reg flib[] = { 73.497 - {"close", io_close}, 73.498 - {"flush", f_flush}, 73.499 - {"lines", f_lines}, 73.500 - {"read", f_read}, 73.501 - {"seek", f_seek}, 73.502 - {"setvbuf", f_setvbuf}, 73.503 - {"write", f_write}, 73.504 - {"__gc", io_gc}, 73.505 - {"__tostring", io_tostring}, 73.506 - {NULL, NULL} 73.507 -}; 73.508 - 73.509 - 73.510 -static void createmeta (lua_State *L) { 73.511 - luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ 73.512 - lua_pushvalue(L, -1); /* push metatable */ 73.513 - lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ 73.514 - luaL_register(L, NULL, flib); /* file methods */ 73.515 -} 73.516 - 73.517 - 73.518 -static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) { 73.519 - *newfile(L) = f; 73.520 - if (k > 0) { 73.521 - lua_pushvalue(L, -1); 73.522 - lua_rawseti(L, LUA_ENVIRONINDEX, k); 73.523 - } 73.524 - lua_pushvalue(L, -2); /* copy environment */ 73.525 - lua_setfenv(L, -2); /* set it */ 73.526 - lua_setfield(L, -3, fname); 73.527 -} 73.528 - 73.529 - 73.530 -static void newfenv (lua_State *L, lua_CFunction cls) { 73.531 - lua_createtable(L, 0, 1); 73.532 - lua_pushcfunction(L, cls); 73.533 - lua_setfield(L, -2, "__close"); 73.534 -} 73.535 - 73.536 - 73.537 -LUALIB_API int luaopen_io (lua_State *L) { 73.538 - createmeta(L); 73.539 - /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */ 73.540 - newfenv(L, io_fclose); 73.541 - lua_replace(L, LUA_ENVIRONINDEX); 73.542 - /* open library */ 73.543 - luaL_register(L, LUA_IOLIBNAME, iolib); 73.544 - /* create (and set) default files */ 73.545 - newfenv(L, io_noclose); /* close function for default files */ 73.546 - createstdfile(L, stdin, IO_INPUT, "stdin"); 73.547 - createstdfile(L, stdout, IO_OUTPUT, "stdout"); 73.548 - createstdfile(L, stderr, 0, "stderr"); 73.549 - lua_pop(L, 1); /* pop environment for default files */ 73.550 - lua_getfield(L, -1, "popen"); 73.551 - newfenv(L, io_pclose); /* create environment for 'popen' */ 73.552 - lua_setfenv(L, -2); /* set fenv for 'popen' */ 73.553 - lua_pop(L, 1); /* pop 'popen' */ 73.554 - return 1; 73.555 -} 73.556 -
74.1 --- a/src/lua/src/llex.c Sat Mar 03 11:04:56 2012 -0600 74.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 74.3 @@ -1,461 +0,0 @@ 74.4 -/* 74.5 -** $Id: llex.c,v 2.20.1.1 2007/12/27 13:02:25 roberto Exp $ 74.6 -** Lexical Analyzer 74.7 -** See Copyright Notice in lua.h 74.8 -*/ 74.9 - 74.10 - 74.11 -#include <ctype.h> 74.12 -#include <locale.h> 74.13 -#include <string.h> 74.14 - 74.15 -#define llex_c 74.16 -#define LUA_CORE 74.17 - 74.18 -#include "lua.h" 74.19 - 74.20 -#include "ldo.h" 74.21 -#include "llex.h" 74.22 -#include "lobject.h" 74.23 -#include "lparser.h" 74.24 -#include "lstate.h" 74.25 -#include "lstring.h" 74.26 -#include "ltable.h" 74.27 -#include "lzio.h" 74.28 - 74.29 - 74.30 - 74.31 -#define next(ls) (ls->current = zgetc(ls->z)) 74.32 - 74.33 - 74.34 - 74.35 - 74.36 -#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') 74.37 - 74.38 - 74.39 -/* ORDER RESERVED */ 74.40 -const char *const luaX_tokens [] = { 74.41 - "and", "break", "do", "else", "elseif", 74.42 - "end", "false", "for", "function", "if", 74.43 - "in", "local", "nil", "not", "or", "repeat", 74.44 - "return", "then", "true", "until", "while", 74.45 - "..", "...", "==", ">=", "<=", "~=", 74.46 - "<number>", "<name>", "<string>", "<eof>", 74.47 - NULL 74.48 -}; 74.49 - 74.50 - 74.51 -#define save_and_next(ls) (save(ls, ls->current), next(ls)) 74.52 - 74.53 - 74.54 -static void save (LexState *ls, int c) { 74.55 - Mbuffer *b = ls->buff; 74.56 - if (b->n + 1 > b->buffsize) { 74.57 - size_t newsize; 74.58 - if (b->buffsize >= MAX_SIZET/2) 74.59 - luaX_lexerror(ls, "lexical element too long", 0); 74.60 - newsize = b->buffsize * 2; 74.61 - luaZ_resizebuffer(ls->L, b, newsize); 74.62 - } 74.63 - b->buffer[b->n++] = cast(char, c); 74.64 -} 74.65 - 74.66 - 74.67 -void luaX_init (lua_State *L) { 74.68 - int i; 74.69 - for (i=0; i<NUM_RESERVED; i++) { 74.70 - TString *ts = luaS_new(L, luaX_tokens[i]); 74.71 - luaS_fix(ts); /* reserved words are never collected */ 74.72 - lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN); 74.73 - ts->tsv.reserved = cast_byte(i+1); /* reserved word */ 74.74 - } 74.75 -} 74.76 - 74.77 - 74.78 -#define MAXSRC 80 74.79 - 74.80 - 74.81 -const char *luaX_token2str (LexState *ls, int token) { 74.82 - if (token < FIRST_RESERVED) { 74.83 - lua_assert(token == cast(unsigned char, token)); 74.84 - return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) : 74.85 - luaO_pushfstring(ls->L, "%c", token); 74.86 - } 74.87 - else 74.88 - return luaX_tokens[token-FIRST_RESERVED]; 74.89 -} 74.90 - 74.91 - 74.92 -static const char *txtToken (LexState *ls, int token) { 74.93 - switch (token) { 74.94 - case TK_NAME: 74.95 - case TK_STRING: 74.96 - case TK_NUMBER: 74.97 - save(ls, '\0'); 74.98 - return luaZ_buffer(ls->buff); 74.99 - default: 74.100 - return luaX_token2str(ls, token); 74.101 - } 74.102 -} 74.103 - 74.104 - 74.105 -void luaX_lexerror (LexState *ls, const char *msg, int token) { 74.106 - char buff[MAXSRC]; 74.107 - luaO_chunkid(buff, getstr(ls->source), MAXSRC); 74.108 - msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg); 74.109 - if (token) 74.110 - luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token)); 74.111 - luaD_throw(ls->L, LUA_ERRSYNTAX); 74.112 -} 74.113 - 74.114 - 74.115 -void luaX_syntaxerror (LexState *ls, const char *msg) { 74.116 - luaX_lexerror(ls, msg, ls->t.token); 74.117 -} 74.118 - 74.119 - 74.120 -TString *luaX_newstring (LexState *ls, const char *str, size_t l) { 74.121 - lua_State *L = ls->L; 74.122 - TString *ts = luaS_newlstr(L, str, l); 74.123 - TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */ 74.124 - if (ttisnil(o)) 74.125 - setbvalue(o, 1); /* make sure `str' will not be collected */ 74.126 - return ts; 74.127 -} 74.128 - 74.129 - 74.130 -static void inclinenumber (LexState *ls) { 74.131 - int old = ls->current; 74.132 - lua_assert(currIsNewline(ls)); 74.133 - next(ls); /* skip `\n' or `\r' */ 74.134 - if (currIsNewline(ls) && ls->current != old) 74.135 - next(ls); /* skip `\n\r' or `\r\n' */ 74.136 - if (++ls->linenumber >= MAX_INT) 74.137 - luaX_syntaxerror(ls, "chunk has too many lines"); 74.138 -} 74.139 - 74.140 - 74.141 -void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) { 74.142 - ls->decpoint = '.'; 74.143 - ls->L = L; 74.144 - ls->lookahead.token = TK_EOS; /* no look-ahead token */ 74.145 - ls->z = z; 74.146 - ls->fs = NULL; 74.147 - ls->linenumber = 1; 74.148 - ls->lastline = 1; 74.149 - ls->source = source; 74.150 - luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ 74.151 - next(ls); /* read first char */ 74.152 -} 74.153 - 74.154 - 74.155 - 74.156 -/* 74.157 -** ======================================================= 74.158 -** LEXICAL ANALYZER 74.159 -** ======================================================= 74.160 -*/ 74.161 - 74.162 - 74.163 - 74.164 -static int check_next (LexState *ls, const char *set) { 74.165 - if (!strchr(set, ls->current)) 74.166 - return 0; 74.167 - save_and_next(ls); 74.168 - return 1; 74.169 -} 74.170 - 74.171 - 74.172 -static void buffreplace (LexState *ls, char from, char to) { 74.173 - size_t n = luaZ_bufflen(ls->buff); 74.174 - char *p = luaZ_buffer(ls->buff); 74.175 - while (n--) 74.176 - if (p[n] == from) p[n] = to; 74.177 -} 74.178 - 74.179 - 74.180 -static void trydecpoint (LexState *ls, SemInfo *seminfo) { 74.181 - /* format error: try to update decimal point separator */ 74.182 - struct lconv *cv = localeconv(); 74.183 - char old = ls->decpoint; 74.184 - ls->decpoint = (cv ? cv->decimal_point[0] : '.'); 74.185 - buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */ 74.186 - if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) { 74.187 - /* format error with correct decimal point: no more options */ 74.188 - buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ 74.189 - luaX_lexerror(ls, "malformed number", TK_NUMBER); 74.190 - } 74.191 -} 74.192 - 74.193 - 74.194 -/* LUA_NUMBER */ 74.195 -static void read_numeral (LexState *ls, SemInfo *seminfo) { 74.196 - lua_assert(isdigit(ls->current)); 74.197 - do { 74.198 - save_and_next(ls); 74.199 - } while (isdigit(ls->current) || ls->current == '.'); 74.200 - if (check_next(ls, "Ee")) /* `E'? */ 74.201 - check_next(ls, "+-"); /* optional exponent sign */ 74.202 - while (isalnum(ls->current) || ls->current == '_') 74.203 - save_and_next(ls); 74.204 - save(ls, '\0'); 74.205 - buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ 74.206 - if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */ 74.207 - trydecpoint(ls, seminfo); /* try to update decimal point separator */ 74.208 -} 74.209 - 74.210 - 74.211 -static int skip_sep (LexState *ls) { 74.212 - int count = 0; 74.213 - int s = ls->current; 74.214 - lua_assert(s == '[' || s == ']'); 74.215 - save_and_next(ls); 74.216 - while (ls->current == '=') { 74.217 - save_and_next(ls); 74.218 - count++; 74.219 - } 74.220 - return (ls->current == s) ? count : (-count) - 1; 74.221 -} 74.222 - 74.223 - 74.224 -static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { 74.225 - int cont = 0; 74.226 - (void)(cont); /* avoid warnings when `cont' is not used */ 74.227 - save_and_next(ls); /* skip 2nd `[' */ 74.228 - if (currIsNewline(ls)) /* string starts with a newline? */ 74.229 - inclinenumber(ls); /* skip it */ 74.230 - for (;;) { 74.231 - switch (ls->current) { 74.232 - case EOZ: 74.233 - luaX_lexerror(ls, (seminfo) ? "unfinished long string" : 74.234 - "unfinished long comment", TK_EOS); 74.235 - break; /* to avoid warnings */ 74.236 -#if defined(LUA_COMPAT_LSTR) 74.237 - case '[': { 74.238 - if (skip_sep(ls) == sep) { 74.239 - save_and_next(ls); /* skip 2nd `[' */ 74.240 - cont++; 74.241 -#if LUA_COMPAT_LSTR == 1 74.242 - if (sep == 0) 74.243 - luaX_lexerror(ls, "nesting of [[...]] is deprecated", '['); 74.244 -#endif 74.245 - } 74.246 - break; 74.247 - } 74.248 -#endif 74.249 - case ']': { 74.250 - if (skip_sep(ls) == sep) { 74.251 - save_and_next(ls); /* skip 2nd `]' */ 74.252 -#if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2 74.253 - cont--; 74.254 - if (sep == 0 && cont >= 0) break; 74.255 -#endif 74.256 - goto endloop; 74.257 - } 74.258 - break; 74.259 - } 74.260 - case '\n': 74.261 - case '\r': { 74.262 - save(ls, '\n'); 74.263 - inclinenumber(ls); 74.264 - if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ 74.265 - break; 74.266 - } 74.267 - default: { 74.268 - if (seminfo) save_and_next(ls); 74.269 - else next(ls); 74.270 - } 74.271 - } 74.272 - } endloop: 74.273 - if (seminfo) 74.274 - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep), 74.275 - luaZ_bufflen(ls->buff) - 2*(2 + sep)); 74.276 -} 74.277 - 74.278 - 74.279 -static void read_string (LexState *ls, int del, SemInfo *seminfo) { 74.280 - save_and_next(ls); 74.281 - while (ls->current != del) { 74.282 - switch (ls->current) { 74.283 - case EOZ: 74.284 - luaX_lexerror(ls, "unfinished string", TK_EOS); 74.285 - continue; /* to avoid warnings */ 74.286 - case '\n': 74.287 - case '\r': 74.288 - luaX_lexerror(ls, "unfinished string", TK_STRING); 74.289 - continue; /* to avoid warnings */ 74.290 - case '\\': { 74.291 - int c; 74.292 - next(ls); /* do not save the `\' */ 74.293 - switch (ls->current) { 74.294 - case 'a': c = '\a'; break; 74.295 - case 'b': c = '\b'; break; 74.296 - case 'f': c = '\f'; break; 74.297 - case 'n': c = '\n'; break; 74.298 - case 'r': c = '\r'; break; 74.299 - case 't': c = '\t'; break; 74.300 - case 'v': c = '\v'; break; 74.301 - case '\n': /* go through */ 74.302 - case '\r': save(ls, '\n'); inclinenumber(ls); continue; 74.303 - case EOZ: continue; /* will raise an error next loop */ 74.304 - default: { 74.305 - if (!isdigit(ls->current)) 74.306 - save_and_next(ls); /* handles \\, \", \', and \? */ 74.307 - else { /* \xxx */ 74.308 - int i = 0; 74.309 - c = 0; 74.310 - do { 74.311 - c = 10*c + (ls->current-'0'); 74.312 - next(ls); 74.313 - } while (++i<3 && isdigit(ls->current)); 74.314 - if (c > UCHAR_MAX) 74.315 - luaX_lexerror(ls, "escape sequence too large", TK_STRING); 74.316 - save(ls, c); 74.317 - } 74.318 - continue; 74.319 - } 74.320 - } 74.321 - save(ls, c); 74.322 - next(ls); 74.323 - continue; 74.324 - } 74.325 - default: 74.326 - save_and_next(ls); 74.327 - } 74.328 - } 74.329 - save_and_next(ls); /* skip delimiter */ 74.330 - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, 74.331 - luaZ_bufflen(ls->buff) - 2); 74.332 -} 74.333 - 74.334 - 74.335 -static int llex (LexState *ls, SemInfo *seminfo) { 74.336 - luaZ_resetbuffer(ls->buff); 74.337 - for (;;) { 74.338 - switch (ls->current) { 74.339 - case '\n': 74.340 - case '\r': { 74.341 - inclinenumber(ls); 74.342 - continue; 74.343 - } 74.344 - case '-': { 74.345 - next(ls); 74.346 - if (ls->current != '-') return '-'; 74.347 - /* else is a comment */ 74.348 - next(ls); 74.349 - if (ls->current == '[') { 74.350 - int sep = skip_sep(ls); 74.351 - luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */ 74.352 - if (sep >= 0) { 74.353 - read_long_string(ls, NULL, sep); /* long comment */ 74.354 - luaZ_resetbuffer(ls->buff); 74.355 - continue; 74.356 - } 74.357 - } 74.358 - /* else short comment */ 74.359 - while (!currIsNewline(ls) && ls->current != EOZ) 74.360 - next(ls); 74.361 - continue; 74.362 - } 74.363 - case '[': { 74.364 - int sep = skip_sep(ls); 74.365 - if (sep >= 0) { 74.366 - read_long_string(ls, seminfo, sep); 74.367 - return TK_STRING; 74.368 - } 74.369 - else if (sep == -1) return '['; 74.370 - else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING); 74.371 - } 74.372 - case '=': { 74.373 - next(ls); 74.374 - if (ls->current != '=') return '='; 74.375 - else { next(ls); return TK_EQ; } 74.376 - } 74.377 - case '<': { 74.378 - next(ls); 74.379 - if (ls->current != '=') return '<'; 74.380 - else { next(ls); return TK_LE; } 74.381 - } 74.382 - case '>': { 74.383 - next(ls); 74.384 - if (ls->current != '=') return '>'; 74.385 - else { next(ls); return TK_GE; } 74.386 - } 74.387 - case '~': { 74.388 - next(ls); 74.389 - if (ls->current != '=') return '~'; 74.390 - else { next(ls); return TK_NE; } 74.391 - } 74.392 - case '"': 74.393 - case '\'': { 74.394 - read_string(ls, ls->current, seminfo); 74.395 - return TK_STRING; 74.396 - } 74.397 - case '.': { 74.398 - save_and_next(ls); 74.399 - if (check_next(ls, ".")) { 74.400 - if (check_next(ls, ".")) 74.401 - return TK_DOTS; /* ... */ 74.402 - else return TK_CONCAT; /* .. */ 74.403 - } 74.404 - else if (!isdigit(ls->current)) return '.'; 74.405 - else { 74.406 - read_numeral(ls, seminfo); 74.407 - return TK_NUMBER; 74.408 - } 74.409 - } 74.410 - case EOZ: { 74.411 - return TK_EOS; 74.412 - } 74.413 - default: { 74.414 - if (isspace(ls->current)) { 74.415 - lua_assert(!currIsNewline(ls)); 74.416 - next(ls); 74.417 - continue; 74.418 - } 74.419 - else if (isdigit(ls->current)) { 74.420 - read_numeral(ls, seminfo); 74.421 - return TK_NUMBER; 74.422 - } 74.423 - else if (isalpha(ls->current) || ls->current == '_') { 74.424 - /* identifier or reserved word */ 74.425 - TString *ts; 74.426 - do { 74.427 - save_and_next(ls); 74.428 - } while (isalnum(ls->current) || ls->current == '_'); 74.429 - ts = luaX_newstring(ls, luaZ_buffer(ls->buff), 74.430 - luaZ_bufflen(ls->buff)); 74.431 - if (ts->tsv.reserved > 0) /* reserved word? */ 74.432 - return ts->tsv.reserved - 1 + FIRST_RESERVED; 74.433 - else { 74.434 - seminfo->ts = ts; 74.435 - return TK_NAME; 74.436 - } 74.437 - } 74.438 - else { 74.439 - int c = ls->current; 74.440 - next(ls); 74.441 - return c; /* single-char tokens (+ - / ...) */ 74.442 - } 74.443 - } 74.444 - } 74.445 - } 74.446 -} 74.447 - 74.448 - 74.449 -void luaX_next (LexState *ls) { 74.450 - ls->lastline = ls->linenumber; 74.451 - if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ 74.452 - ls->t = ls->lookahead; /* use this one */ 74.453 - ls->lookahead.token = TK_EOS; /* and discharge it */ 74.454 - } 74.455 - else 74.456 - ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */ 74.457 -} 74.458 - 74.459 - 74.460 -void luaX_lookahead (LexState *ls) { 74.461 - lua_assert(ls->lookahead.token == TK_EOS); 74.462 - ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); 74.463 -} 74.464 -
75.1 --- a/src/lua/src/llex.h Sat Mar 03 11:04:56 2012 -0600 75.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 75.3 @@ -1,81 +0,0 @@ 75.4 -/* 75.5 -** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $ 75.6 -** Lexical Analyzer 75.7 -** See Copyright Notice in lua.h 75.8 -*/ 75.9 - 75.10 -#ifndef llex_h 75.11 -#define llex_h 75.12 - 75.13 -#include "lobject.h" 75.14 -#include "lzio.h" 75.15 - 75.16 - 75.17 -#define FIRST_RESERVED 257 75.18 - 75.19 -/* maximum length of a reserved word */ 75.20 -#define TOKEN_LEN (sizeof("function")/sizeof(char)) 75.21 - 75.22 - 75.23 -/* 75.24 -* WARNING: if you change the order of this enumeration, 75.25 -* grep "ORDER RESERVED" 75.26 -*/ 75.27 -enum RESERVED { 75.28 - /* terminal symbols denoted by reserved words */ 75.29 - TK_AND = FIRST_RESERVED, TK_BREAK, 75.30 - TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 75.31 - TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 75.32 - TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 75.33 - /* other terminal symbols */ 75.34 - TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER, 75.35 - TK_NAME, TK_STRING, TK_EOS 75.36 -}; 75.37 - 75.38 -/* number of reserved words */ 75.39 -#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 75.40 - 75.41 - 75.42 -/* array with token `names' */ 75.43 -LUAI_DATA const char *const luaX_tokens []; 75.44 - 75.45 - 75.46 -typedef union { 75.47 - lua_Number r; 75.48 - TString *ts; 75.49 -} SemInfo; /* semantics information */ 75.50 - 75.51 - 75.52 -typedef struct Token { 75.53 - int token; 75.54 - SemInfo seminfo; 75.55 -} Token; 75.56 - 75.57 - 75.58 -typedef struct LexState { 75.59 - int current; /* current character (charint) */ 75.60 - int linenumber; /* input line counter */ 75.61 - int lastline; /* line of last token `consumed' */ 75.62 - Token t; /* current token */ 75.63 - Token lookahead; /* look ahead token */ 75.64 - struct FuncState *fs; /* `FuncState' is private to the parser */ 75.65 - struct lua_State *L; 75.66 - ZIO *z; /* input stream */ 75.67 - Mbuffer *buff; /* buffer for tokens */ 75.68 - TString *source; /* current source name */ 75.69 - char decpoint; /* locale decimal point */ 75.70 -} LexState; 75.71 - 75.72 - 75.73 -LUAI_FUNC void luaX_init (lua_State *L); 75.74 -LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 75.75 - TString *source); 75.76 -LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 75.77 -LUAI_FUNC void luaX_next (LexState *ls); 75.78 -LUAI_FUNC void luaX_lookahead (LexState *ls); 75.79 -LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token); 75.80 -LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s); 75.81 -LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 75.82 - 75.83 - 75.84 -#endif
76.1 --- a/src/lua/src/llimits.h Sat Mar 03 11:04:56 2012 -0600 76.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 76.3 @@ -1,128 +0,0 @@ 76.4 -/* 76.5 -** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $ 76.6 -** Limits, basic types, and some other `installation-dependent' definitions 76.7 -** See Copyright Notice in lua.h 76.8 -*/ 76.9 - 76.10 -#ifndef llimits_h 76.11 -#define llimits_h 76.12 - 76.13 - 76.14 -#include <limits.h> 76.15 -#include <stddef.h> 76.16 - 76.17 - 76.18 -#include "lua.h" 76.19 - 76.20 - 76.21 -typedef LUAI_UINT32 lu_int32; 76.22 - 76.23 -typedef LUAI_UMEM lu_mem; 76.24 - 76.25 -typedef LUAI_MEM l_mem; 76.26 - 76.27 - 76.28 - 76.29 -/* chars used as small naturals (so that `char' is reserved for characters) */ 76.30 -typedef unsigned char lu_byte; 76.31 - 76.32 - 76.33 -#define MAX_SIZET ((size_t)(~(size_t)0)-2) 76.34 - 76.35 -#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) 76.36 - 76.37 - 76.38 -#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 76.39 - 76.40 -/* 76.41 -** conversion of pointer to integer 76.42 -** this is for hashing only; there is no problem if the integer 76.43 -** cannot hold the whole pointer value 76.44 -*/ 76.45 -#define IntPoint(p) ((unsigned int)(lu_mem)(p)) 76.46 - 76.47 - 76.48 - 76.49 -/* type to ensure maximum alignment */ 76.50 -typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 76.51 - 76.52 - 76.53 -/* result of a `usual argument conversion' over lua_Number */ 76.54 -typedef LUAI_UACNUMBER l_uacNumber; 76.55 - 76.56 - 76.57 -/* internal assertions for in-house debugging */ 76.58 -#ifdef lua_assert 76.59 - 76.60 -#define check_exp(c,e) (lua_assert(c), (e)) 76.61 -#define api_check(l,e) lua_assert(e) 76.62 - 76.63 -#else 76.64 - 76.65 -#define lua_assert(c) ((void)0) 76.66 -#define check_exp(c,e) (e) 76.67 -#define api_check luai_apicheck 76.68 - 76.69 -#endif 76.70 - 76.71 - 76.72 -#ifndef UNUSED 76.73 -#define UNUSED(x) ((void)(x)) /* to avoid warnings */ 76.74 -#endif 76.75 - 76.76 - 76.77 -#ifndef cast 76.78 -#define cast(t, exp) ((t)(exp)) 76.79 -#endif 76.80 - 76.81 -#define cast_byte(i) cast(lu_byte, (i)) 76.82 -#define cast_num(i) cast(lua_Number, (i)) 76.83 -#define cast_int(i) cast(int, (i)) 76.84 - 76.85 - 76.86 - 76.87 -/* 76.88 -** type for virtual-machine instructions 76.89 -** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 76.90 -*/ 76.91 -typedef lu_int32 Instruction; 76.92 - 76.93 - 76.94 - 76.95 -/* maximum stack for a Lua function */ 76.96 -#define MAXSTACK 250 76.97 - 76.98 - 76.99 - 76.100 -/* minimum size for the string table (must be power of 2) */ 76.101 -#ifndef MINSTRTABSIZE 76.102 -#define MINSTRTABSIZE 32 76.103 -#endif 76.104 - 76.105 - 76.106 -/* minimum size for string buffer */ 76.107 -#ifndef LUA_MINBUFFER 76.108 -#define LUA_MINBUFFER 32 76.109 -#endif 76.110 - 76.111 - 76.112 -#ifndef lua_lock 76.113 -#define lua_lock(L) ((void) 0) 76.114 -#define lua_unlock(L) ((void) 0) 76.115 -#endif 76.116 - 76.117 -#ifndef luai_threadyield 76.118 -#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 76.119 -#endif 76.120 - 76.121 - 76.122 -/* 76.123 -** macro to control inclusion of some hard tests on stack reallocation 76.124 -*/ 76.125 -#ifndef HARDSTACKTESTS 76.126 -#define condhardstacktests(x) ((void)0) 76.127 -#else 76.128 -#define condhardstacktests(x) x 76.129 -#endif 76.130 - 76.131 -#endif
77.1 --- a/src/lua/src/lmathlib.c Sat Mar 03 11:04:56 2012 -0600 77.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 77.3 @@ -1,263 +0,0 @@ 77.4 -/* 77.5 -** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $ 77.6 -** Standard mathematical library 77.7 -** See Copyright Notice in lua.h 77.8 -*/ 77.9 - 77.10 - 77.11 -#include <stdlib.h> 77.12 -#include <math.h> 77.13 - 77.14 -#define lmathlib_c 77.15 -#define LUA_LIB 77.16 - 77.17 -#include "lua.h" 77.18 - 77.19 -#include "lauxlib.h" 77.20 -#include "lualib.h" 77.21 - 77.22 - 77.23 -#undef PI 77.24 -#define PI (3.14159265358979323846) 77.25 -#define RADIANS_PER_DEGREE (PI/180.0) 77.26 - 77.27 - 77.28 - 77.29 -static int math_abs (lua_State *L) { 77.30 - lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); 77.31 - return 1; 77.32 -} 77.33 - 77.34 -static int math_sin (lua_State *L) { 77.35 - lua_pushnumber(L, sin(luaL_checknumber(L, 1))); 77.36 - return 1; 77.37 -} 77.38 - 77.39 -static int math_sinh (lua_State *L) { 77.40 - lua_pushnumber(L, sinh(luaL_checknumber(L, 1))); 77.41 - return 1; 77.42 -} 77.43 - 77.44 -static int math_cos (lua_State *L) { 77.45 - lua_pushnumber(L, cos(luaL_checknumber(L, 1))); 77.46 - return 1; 77.47 -} 77.48 - 77.49 -static int math_cosh (lua_State *L) { 77.50 - lua_pushnumber(L, cosh(luaL_checknumber(L, 1))); 77.51 - return 1; 77.52 -} 77.53 - 77.54 -static int math_tan (lua_State *L) { 77.55 - lua_pushnumber(L, tan(luaL_checknumber(L, 1))); 77.56 - return 1; 77.57 -} 77.58 - 77.59 -static int math_tanh (lua_State *L) { 77.60 - lua_pushnumber(L, tanh(luaL_checknumber(L, 1))); 77.61 - return 1; 77.62 -} 77.63 - 77.64 -static int math_asin (lua_State *L) { 77.65 - lua_pushnumber(L, asin(luaL_checknumber(L, 1))); 77.66 - return 1; 77.67 -} 77.68 - 77.69 -static int math_acos (lua_State *L) { 77.70 - lua_pushnumber(L, acos(luaL_checknumber(L, 1))); 77.71 - return 1; 77.72 -} 77.73 - 77.74 -static int math_atan (lua_State *L) { 77.75 - lua_pushnumber(L, atan(luaL_checknumber(L, 1))); 77.76 - return 1; 77.77 -} 77.78 - 77.79 -static int math_atan2 (lua_State *L) { 77.80 - lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 77.81 - return 1; 77.82 -} 77.83 - 77.84 -static int math_ceil (lua_State *L) { 77.85 - lua_pushnumber(L, ceil(luaL_checknumber(L, 1))); 77.86 - return 1; 77.87 -} 77.88 - 77.89 -static int math_floor (lua_State *L) { 77.90 - lua_pushnumber(L, floor(luaL_checknumber(L, 1))); 77.91 - return 1; 77.92 -} 77.93 - 77.94 -static int math_fmod (lua_State *L) { 77.95 - lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 77.96 - return 1; 77.97 -} 77.98 - 77.99 -static int math_modf (lua_State *L) { 77.100 - double ip; 77.101 - double fp = modf(luaL_checknumber(L, 1), &ip); 77.102 - lua_pushnumber(L, ip); 77.103 - lua_pushnumber(L, fp); 77.104 - return 2; 77.105 -} 77.106 - 77.107 -static int math_sqrt (lua_State *L) { 77.108 - lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); 77.109 - return 1; 77.110 -} 77.111 - 77.112 -static int math_pow (lua_State *L) { 77.113 - lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 77.114 - return 1; 77.115 -} 77.116 - 77.117 -static int math_log (lua_State *L) { 77.118 - lua_pushnumber(L, log(luaL_checknumber(L, 1))); 77.119 - return 1; 77.120 -} 77.121 - 77.122 -static int math_log10 (lua_State *L) { 77.123 - lua_pushnumber(L, log10(luaL_checknumber(L, 1))); 77.124 - return 1; 77.125 -} 77.126 - 77.127 -static int math_exp (lua_State *L) { 77.128 - lua_pushnumber(L, exp(luaL_checknumber(L, 1))); 77.129 - return 1; 77.130 -} 77.131 - 77.132 -static int math_deg (lua_State *L) { 77.133 - lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); 77.134 - return 1; 77.135 -} 77.136 - 77.137 -static int math_rad (lua_State *L) { 77.138 - lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); 77.139 - return 1; 77.140 -} 77.141 - 77.142 -static int math_frexp (lua_State *L) { 77.143 - int e; 77.144 - lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e)); 77.145 - lua_pushinteger(L, e); 77.146 - return 2; 77.147 -} 77.148 - 77.149 -static int math_ldexp (lua_State *L) { 77.150 - lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2))); 77.151 - return 1; 77.152 -} 77.153 - 77.154 - 77.155 - 77.156 -static int math_min (lua_State *L) { 77.157 - int n = lua_gettop(L); /* number of arguments */ 77.158 - lua_Number dmin = luaL_checknumber(L, 1); 77.159 - int i; 77.160 - for (i=2; i<=n; i++) { 77.161 - lua_Number d = luaL_checknumber(L, i); 77.162 - if (d < dmin) 77.163 - dmin = d; 77.164 - } 77.165 - lua_pushnumber(L, dmin); 77.166 - return 1; 77.167 -} 77.168 - 77.169 - 77.170 -static int math_max (lua_State *L) { 77.171 - int n = lua_gettop(L); /* number of arguments */ 77.172 - lua_Number dmax = luaL_checknumber(L, 1); 77.173 - int i; 77.174 - for (i=2; i<=n; i++) { 77.175 - lua_Number d = luaL_checknumber(L, i); 77.176 - if (d > dmax) 77.177 - dmax = d; 77.178 - } 77.179 - lua_pushnumber(L, dmax); 77.180 - return 1; 77.181 -} 77.182 - 77.183 - 77.184 -static int math_random (lua_State *L) { 77.185 - /* the `%' avoids the (rare) case of r==1, and is needed also because on 77.186 - some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ 77.187 - lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; 77.188 - switch (lua_gettop(L)) { /* check number of arguments */ 77.189 - case 0: { /* no arguments */ 77.190 - lua_pushnumber(L, r); /* Number between 0 and 1 */ 77.191 - break; 77.192 - } 77.193 - case 1: { /* only upper limit */ 77.194 - int u = luaL_checkint(L, 1); 77.195 - luaL_argcheck(L, 1<=u, 1, "interval is empty"); 77.196 - lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */ 77.197 - break; 77.198 - } 77.199 - case 2: { /* lower and upper limits */ 77.200 - int l = luaL_checkint(L, 1); 77.201 - int u = luaL_checkint(L, 2); 77.202 - luaL_argcheck(L, l<=u, 2, "interval is empty"); 77.203 - lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */ 77.204 - break; 77.205 - } 77.206 - default: return luaL_error(L, "wrong number of arguments"); 77.207 - } 77.208 - return 1; 77.209 -} 77.210 - 77.211 - 77.212 -static int math_randomseed (lua_State *L) { 77.213 - srand(luaL_checkint(L, 1)); 77.214 - return 0; 77.215 -} 77.216 - 77.217 - 77.218 -static const luaL_Reg mathlib[] = { 77.219 - {"abs", math_abs}, 77.220 - {"acos", math_acos}, 77.221 - {"asin", math_asin}, 77.222 - {"atan2", math_atan2}, 77.223 - {"atan", math_atan}, 77.224 - {"ceil", math_ceil}, 77.225 - {"cosh", math_cosh}, 77.226 - {"cos", math_cos}, 77.227 - {"deg", math_deg}, 77.228 - {"exp", math_exp}, 77.229 - {"floor", math_floor}, 77.230 - {"fmod", math_fmod}, 77.231 - {"frexp", math_frexp}, 77.232 - {"ldexp", math_ldexp}, 77.233 - {"log10", math_log10}, 77.234 - {"log", math_log}, 77.235 - {"max", math_max}, 77.236 - {"min", math_min}, 77.237 - {"modf", math_modf}, 77.238 - {"pow", math_pow}, 77.239 - {"rad", math_rad}, 77.240 - {"random", math_random}, 77.241 - {"randomseed", math_randomseed}, 77.242 - {"sinh", math_sinh}, 77.243 - {"sin", math_sin}, 77.244 - {"sqrt", math_sqrt}, 77.245 - {"tanh", math_tanh}, 77.246 - {"tan", math_tan}, 77.247 - {NULL, NULL} 77.248 -}; 77.249 - 77.250 - 77.251 -/* 77.252 -** Open math library 77.253 -*/ 77.254 -LUALIB_API int luaopen_math (lua_State *L) { 77.255 - luaL_register(L, LUA_MATHLIBNAME, mathlib); 77.256 - lua_pushnumber(L, PI); 77.257 - lua_setfield(L, -2, "pi"); 77.258 - lua_pushnumber(L, HUGE_VAL); 77.259 - lua_setfield(L, -2, "huge"); 77.260 -#if defined(LUA_COMPAT_MOD) 77.261 - lua_getfield(L, -1, "fmod"); 77.262 - lua_setfield(L, -2, "mod"); 77.263 -#endif 77.264 - return 1; 77.265 -} 77.266 -
78.1 --- a/src/lua/src/lmem.c Sat Mar 03 11:04:56 2012 -0600 78.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 78.3 @@ -1,86 +0,0 @@ 78.4 -/* 78.5 -** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $ 78.6 -** Interface to Memory Manager 78.7 -** See Copyright Notice in lua.h 78.8 -*/ 78.9 - 78.10 - 78.11 -#include <stddef.h> 78.12 - 78.13 -#define lmem_c 78.14 -#define LUA_CORE 78.15 - 78.16 -#include "lua.h" 78.17 - 78.18 -#include "ldebug.h" 78.19 -#include "ldo.h" 78.20 -#include "lmem.h" 78.21 -#include "lobject.h" 78.22 -#include "lstate.h" 78.23 - 78.24 - 78.25 - 78.26 -/* 78.27 -** About the realloc function: 78.28 -** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 78.29 -** (`osize' is the old size, `nsize' is the new size) 78.30 -** 78.31 -** Lua ensures that (ptr == NULL) iff (osize == 0). 78.32 -** 78.33 -** * frealloc(ud, NULL, 0, x) creates a new block of size `x' 78.34 -** 78.35 -** * frealloc(ud, p, x, 0) frees the block `p' 78.36 -** (in this specific case, frealloc must return NULL). 78.37 -** particularly, frealloc(ud, NULL, 0, 0) does nothing 78.38 -** (which is equivalent to free(NULL) in ANSI C) 78.39 -** 78.40 -** frealloc returns NULL if it cannot create or reallocate the area 78.41 -** (any reallocation to an equal or smaller size cannot fail!) 78.42 -*/ 78.43 - 78.44 - 78.45 - 78.46 -#define MINSIZEARRAY 4 78.47 - 78.48 - 78.49 -void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 78.50 - int limit, const char *errormsg) { 78.51 - void *newblock; 78.52 - int newsize; 78.53 - if (*size >= limit/2) { /* cannot double it? */ 78.54 - if (*size >= limit) /* cannot grow even a little? */ 78.55 - luaG_runerror(L, errormsg); 78.56 - newsize = limit; /* still have at least one free place */ 78.57 - } 78.58 - else { 78.59 - newsize = (*size)*2; 78.60 - if (newsize < MINSIZEARRAY) 78.61 - newsize = MINSIZEARRAY; /* minimum size */ 78.62 - } 78.63 - newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 78.64 - *size = newsize; /* update only when everything else is OK */ 78.65 - return newblock; 78.66 -} 78.67 - 78.68 - 78.69 -void *luaM_toobig (lua_State *L) { 78.70 - luaG_runerror(L, "memory allocation error: block too big"); 78.71 - return NULL; /* to avoid warnings */ 78.72 -} 78.73 - 78.74 - 78.75 - 78.76 -/* 78.77 -** generic allocation routine. 78.78 -*/ 78.79 -void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 78.80 - global_State *g = G(L); 78.81 - lua_assert((osize == 0) == (block == NULL)); 78.82 - block = (*g->frealloc)(g->ud, block, osize, nsize); 78.83 - if (block == NULL && nsize > 0) 78.84 - luaD_throw(L, LUA_ERRMEM); 78.85 - lua_assert((nsize == 0) == (block == NULL)); 78.86 - g->totalbytes = (g->totalbytes - osize) + nsize; 78.87 - return block; 78.88 -} 78.89 -
79.1 --- a/src/lua/src/lmem.h Sat Mar 03 11:04:56 2012 -0600 79.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 79.3 @@ -1,49 +0,0 @@ 79.4 -/* 79.5 -** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 79.6 -** Interface to Memory Manager 79.7 -** See Copyright Notice in lua.h 79.8 -*/ 79.9 - 79.10 -#ifndef lmem_h 79.11 -#define lmem_h 79.12 - 79.13 - 79.14 -#include <stddef.h> 79.15 - 79.16 -#include "llimits.h" 79.17 -#include "lua.h" 79.18 - 79.19 -#define MEMERRMSG "not enough memory" 79.20 - 79.21 - 79.22 -#define luaM_reallocv(L,b,on,n,e) \ 79.23 - ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 79.24 - luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ 79.25 - luaM_toobig(L)) 79.26 - 79.27 -#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 79.28 -#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 79.29 -#define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) 79.30 - 79.31 -#define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) 79.32 -#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 79.33 -#define luaM_newvector(L,n,t) \ 79.34 - cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 79.35 - 79.36 -#define luaM_growvector(L,v,nelems,size,t,limit,e) \ 79.37 - if ((nelems)+1 > (size)) \ 79.38 - ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 79.39 - 79.40 -#define luaM_reallocvector(L, v,oldn,n,t) \ 79.41 - ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 79.42 - 79.43 - 79.44 -LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 79.45 - size_t size); 79.46 -LUAI_FUNC void *luaM_toobig (lua_State *L); 79.47 -LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 79.48 - size_t size_elem, int limit, 79.49 - const char *errormsg); 79.50 - 79.51 -#endif 79.52 -
80.1 --- a/src/lua/src/loadlib.c Sat Mar 03 11:04:56 2012 -0600 80.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 80.3 @@ -1,666 +0,0 @@ 80.4 -/* 80.5 -** $Id: loadlib.c,v 1.52.1.3 2008/08/06 13:29:28 roberto Exp $ 80.6 -** Dynamic library loader for Lua 80.7 -** See Copyright Notice in lua.h 80.8 -** 80.9 -** This module contains an implementation of loadlib for Unix systems 80.10 -** that have dlfcn, an implementation for Darwin (Mac OS X), an 80.11 -** implementation for Windows, and a stub for other systems. 80.12 -*/ 80.13 - 80.14 - 80.15 -#include <stdlib.h> 80.16 -#include <string.h> 80.17 - 80.18 - 80.19 -#define loadlib_c 80.20 -#define LUA_LIB 80.21 - 80.22 -#include "lua.h" 80.23 - 80.24 -#include "lauxlib.h" 80.25 -#include "lualib.h" 80.26 - 80.27 - 80.28 -/* prefix for open functions in C libraries */ 80.29 -#define LUA_POF "luaopen_" 80.30 - 80.31 -/* separator for open functions in C libraries */ 80.32 -#define LUA_OFSEP "_" 80.33 - 80.34 - 80.35 -#define LIBPREFIX "LOADLIB: " 80.36 - 80.37 -#define POF LUA_POF 80.38 -#define LIB_FAIL "open" 80.39 - 80.40 - 80.41 -/* error codes for ll_loadfunc */ 80.42 -#define ERRLIB 1 80.43 -#define ERRFUNC 2 80.44 - 80.45 -#define setprogdir(L) ((void)0) 80.46 - 80.47 - 80.48 -static void ll_unloadlib (void *lib); 80.49 -static void *ll_load (lua_State *L, const char *path); 80.50 -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); 80.51 - 80.52 - 80.53 - 80.54 -#if defined(LUA_DL_DLOPEN) 80.55 -/* 80.56 -** {======================================================================== 80.57 -** This is an implementation of loadlib based on the dlfcn interface. 80.58 -** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, 80.59 -** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least 80.60 -** as an emulation layer on top of native functions. 80.61 -** ========================================================================= 80.62 -*/ 80.63 - 80.64 -#include <dlfcn.h> 80.65 - 80.66 -static void ll_unloadlib (void *lib) { 80.67 - dlclose(lib); 80.68 -} 80.69 - 80.70 - 80.71 -static void *ll_load (lua_State *L, const char *path) { 80.72 - void *lib = dlopen(path, RTLD_NOW); 80.73 - if (lib == NULL) lua_pushstring(L, dlerror()); 80.74 - return lib; 80.75 -} 80.76 - 80.77 - 80.78 -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { 80.79 - lua_CFunction f = (lua_CFunction)dlsym(lib, sym); 80.80 - if (f == NULL) lua_pushstring(L, dlerror()); 80.81 - return f; 80.82 -} 80.83 - 80.84 -/* }====================================================== */ 80.85 - 80.86 - 80.87 - 80.88 -#elif defined(LUA_DL_DLL) 80.89 -/* 80.90 -** {====================================================================== 80.91 -** This is an implementation of loadlib for Windows using native functions. 80.92 -** ======================================================================= 80.93 -*/ 80.94 - 80.95 -#include <windows.h> 80.96 - 80.97 - 80.98 -#undef setprogdir 80.99 - 80.100 -static void setprogdir (lua_State *L) { 80.101 - char buff[MAX_PATH + 1]; 80.102 - char *lb; 80.103 - DWORD nsize = sizeof(buff)/sizeof(char); 80.104 - DWORD n = GetModuleFileNameA(NULL, buff, nsize); 80.105 - if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) 80.106 - luaL_error(L, "unable to get ModuleFileName"); 80.107 - else { 80.108 - *lb = '\0'; 80.109 - luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); 80.110 - lua_remove(L, -2); /* remove original string */ 80.111 - } 80.112 -} 80.113 - 80.114 - 80.115 -static void pusherror (lua_State *L) { 80.116 - int error = GetLastError(); 80.117 - char buffer[128]; 80.118 - if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, 80.119 - NULL, error, 0, buffer, sizeof(buffer), NULL)) 80.120 - lua_pushstring(L, buffer); 80.121 - else 80.122 - lua_pushfstring(L, "system error %d\n", error); 80.123 -} 80.124 - 80.125 -static void ll_unloadlib (void *lib) { 80.126 - FreeLibrary((HINSTANCE)lib); 80.127 -} 80.128 - 80.129 - 80.130 -static void *ll_load (lua_State *L, const char *path) { 80.131 - HINSTANCE lib = LoadLibraryA(path); 80.132 - if (lib == NULL) pusherror(L); 80.133 - return lib; 80.134 -} 80.135 - 80.136 - 80.137 -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { 80.138 - lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym); 80.139 - if (f == NULL) pusherror(L); 80.140 - return f; 80.141 -} 80.142 - 80.143 -/* }====================================================== */ 80.144 - 80.145 - 80.146 - 80.147 -#elif defined(LUA_DL_DYLD) 80.148 -/* 80.149 -** {====================================================================== 80.150 -** Native Mac OS X / Darwin Implementation 80.151 -** ======================================================================= 80.152 -*/ 80.153 - 80.154 -#include <mach-o/dyld.h> 80.155 - 80.156 - 80.157 -/* Mac appends a `_' before C function names */ 80.158 -#undef POF 80.159 -#define POF "_" LUA_POF 80.160 - 80.161 - 80.162 -static void pusherror (lua_State *L) { 80.163 - const char *err_str; 80.164 - const char *err_file; 80.165 - NSLinkEditErrors err; 80.166 - int err_num; 80.167 - NSLinkEditError(&err, &err_num, &err_file, &err_str); 80.168 - lua_pushstring(L, err_str); 80.169 -} 80.170 - 80.171 - 80.172 -static const char *errorfromcode (NSObjectFileImageReturnCode ret) { 80.173 - switch (ret) { 80.174 - case NSObjectFileImageInappropriateFile: 80.175 - return "file is not a bundle"; 80.176 - case NSObjectFileImageArch: 80.177 - return "library is for wrong CPU type"; 80.178 - case NSObjectFileImageFormat: 80.179 - return "bad format"; 80.180 - case NSObjectFileImageAccess: 80.181 - return "cannot access file"; 80.182 - case NSObjectFileImageFailure: 80.183 - default: 80.184 - return "unable to load library"; 80.185 - } 80.186 -} 80.187 - 80.188 - 80.189 -static void ll_unloadlib (void *lib) { 80.190 - NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); 80.191 -} 80.192 - 80.193 - 80.194 -static void *ll_load (lua_State *L, const char *path) { 80.195 - NSObjectFileImage img; 80.196 - NSObjectFileImageReturnCode ret; 80.197 - /* this would be a rare case, but prevents crashing if it happens */ 80.198 - if(!_dyld_present()) { 80.199 - lua_pushliteral(L, "dyld not present"); 80.200 - return NULL; 80.201 - } 80.202 - ret = NSCreateObjectFileImageFromFile(path, &img); 80.203 - if (ret == NSObjectFileImageSuccess) { 80.204 - NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE | 80.205 - NSLINKMODULE_OPTION_RETURN_ON_ERROR); 80.206 - NSDestroyObjectFileImage(img); 80.207 - if (mod == NULL) pusherror(L); 80.208 - return mod; 80.209 - } 80.210 - lua_pushstring(L, errorfromcode(ret)); 80.211 - return NULL; 80.212 -} 80.213 - 80.214 - 80.215 -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { 80.216 - NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym); 80.217 - if (nss == NULL) { 80.218 - lua_pushfstring(L, "symbol " LUA_QS " not found", sym); 80.219 - return NULL; 80.220 - } 80.221 - return (lua_CFunction)NSAddressOfSymbol(nss); 80.222 -} 80.223 - 80.224 -/* }====================================================== */ 80.225 - 80.226 - 80.227 - 80.228 -#else 80.229 -/* 80.230 -** {====================================================== 80.231 -** Fallback for other systems 80.232 -** ======================================================= 80.233 -*/ 80.234 - 80.235 -#undef LIB_FAIL 80.236 -#define LIB_FAIL "absent" 80.237 - 80.238 - 80.239 -#define DLMSG "dynamic libraries not enabled; check your Lua installation" 80.240 - 80.241 - 80.242 -static void ll_unloadlib (void *lib) { 80.243 - (void)lib; /* to avoid warnings */ 80.244 -} 80.245 - 80.246 - 80.247 -static void *ll_load (lua_State *L, const char *path) { 80.248 - (void)path; /* to avoid warnings */ 80.249 - lua_pushliteral(L, DLMSG); 80.250 - return NULL; 80.251 -} 80.252 - 80.253 - 80.254 -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { 80.255 - (void)lib; (void)sym; /* to avoid warnings */ 80.256 - lua_pushliteral(L, DLMSG); 80.257 - return NULL; 80.258 -} 80.259 - 80.260 -/* }====================================================== */ 80.261 -#endif 80.262 - 80.263 - 80.264 - 80.265 -static void **ll_register (lua_State *L, const char *path) { 80.266 - void **plib; 80.267 - lua_pushfstring(L, "%s%s", LIBPREFIX, path); 80.268 - lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ 80.269 - if (!lua_isnil(L, -1)) /* is there an entry? */ 80.270 - plib = (void **)lua_touserdata(L, -1); 80.271 - else { /* no entry yet; create one */ 80.272 - lua_pop(L, 1); 80.273 - plib = (void **)lua_newuserdata(L, sizeof(const void *)); 80.274 - *plib = NULL; 80.275 - luaL_getmetatable(L, "_LOADLIB"); 80.276 - lua_setmetatable(L, -2); 80.277 - lua_pushfstring(L, "%s%s", LIBPREFIX, path); 80.278 - lua_pushvalue(L, -2); 80.279 - lua_settable(L, LUA_REGISTRYINDEX); 80.280 - } 80.281 - return plib; 80.282 -} 80.283 - 80.284 - 80.285 -/* 80.286 -** __gc tag method: calls library's `ll_unloadlib' function with the lib 80.287 -** handle 80.288 -*/ 80.289 -static int gctm (lua_State *L) { 80.290 - void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); 80.291 - if (*lib) ll_unloadlib(*lib); 80.292 - *lib = NULL; /* mark library as closed */ 80.293 - return 0; 80.294 -} 80.295 - 80.296 - 80.297 -static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { 80.298 - void **reg = ll_register(L, path); 80.299 - if (*reg == NULL) *reg = ll_load(L, path); 80.300 - if (*reg == NULL) 80.301 - return ERRLIB; /* unable to load library */ 80.302 - else { 80.303 - lua_CFunction f = ll_sym(L, *reg, sym); 80.304 - if (f == NULL) 80.305 - return ERRFUNC; /* unable to find function */ 80.306 - lua_pushcfunction(L, f); 80.307 - return 0; /* return function */ 80.308 - } 80.309 -} 80.310 - 80.311 - 80.312 -static int ll_loadlib (lua_State *L) { 80.313 - const char *path = luaL_checkstring(L, 1); 80.314 - const char *init = luaL_checkstring(L, 2); 80.315 - int stat = ll_loadfunc(L, path, init); 80.316 - if (stat == 0) /* no errors? */ 80.317 - return 1; /* return the loaded function */ 80.318 - else { /* error; error message is on stack top */ 80.319 - lua_pushnil(L); 80.320 - lua_insert(L, -2); 80.321 - lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); 80.322 - return 3; /* return nil, error message, and where */ 80.323 - } 80.324 -} 80.325 - 80.326 - 80.327 - 80.328 -/* 80.329 -** {====================================================== 80.330 -** 'require' function 80.331 -** ======================================================= 80.332 -*/ 80.333 - 80.334 - 80.335 -static int readable (const char *filename) { 80.336 - FILE *f = fopen(filename, "r"); /* try to open file */ 80.337 - if (f == NULL) return 0; /* open failed */ 80.338 - fclose(f); 80.339 - return 1; 80.340 -} 80.341 - 80.342 - 80.343 -static const char *pushnexttemplate (lua_State *L, const char *path) { 80.344 - const char *l; 80.345 - while (*path == *LUA_PATHSEP) path++; /* skip separators */ 80.346 - if (*path == '\0') return NULL; /* no more templates */ 80.347 - l = strchr(path, *LUA_PATHSEP); /* find next separator */ 80.348 - if (l == NULL) l = path + strlen(path); 80.349 - lua_pushlstring(L, path, l - path); /* template */ 80.350 - return l; 80.351 -} 80.352 - 80.353 - 80.354 -static const char *findfile (lua_State *L, const char *name, 80.355 - const char *pname) { 80.356 - const char *path; 80.357 - name = luaL_gsub(L, name, ".", LUA_DIRSEP); 80.358 - lua_getfield(L, LUA_ENVIRONINDEX, pname); 80.359 - path = lua_tostring(L, -1); 80.360 - if (path == NULL) 80.361 - luaL_error(L, LUA_QL("package.%s") " must be a string", pname); 80.362 - lua_pushliteral(L, ""); /* error accumulator */ 80.363 - while ((path = pushnexttemplate(L, path)) != NULL) { 80.364 - const char *filename; 80.365 - filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); 80.366 - lua_remove(L, -2); /* remove path template */ 80.367 - if (readable(filename)) /* does file exist and is readable? */ 80.368 - return filename; /* return that file name */ 80.369 - lua_pushfstring(L, "\n\tno file " LUA_QS, filename); 80.370 - lua_remove(L, -2); /* remove file name */ 80.371 - lua_concat(L, 2); /* add entry to possible error message */ 80.372 - } 80.373 - return NULL; /* not found */ 80.374 -} 80.375 - 80.376 - 80.377 -static void loaderror (lua_State *L, const char *filename) { 80.378 - luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", 80.379 - lua_tostring(L, 1), filename, lua_tostring(L, -1)); 80.380 -} 80.381 - 80.382 - 80.383 -static int loader_Lua (lua_State *L) { 80.384 - const char *filename; 80.385 - const char *name = luaL_checkstring(L, 1); 80.386 - filename = findfile(L, name, "path"); 80.387 - if (filename == NULL) return 1; /* library not found in this path */ 80.388 - if (luaL_loadfile(L, filename) != 0) 80.389 - loaderror(L, filename); 80.390 - return 1; /* library loaded successfully */ 80.391 -} 80.392 - 80.393 - 80.394 -static const char *mkfuncname (lua_State *L, const char *modname) { 80.395 - const char *funcname; 80.396 - const char *mark = strchr(modname, *LUA_IGMARK); 80.397 - if (mark) modname = mark + 1; 80.398 - funcname = luaL_gsub(L, modname, ".", LUA_OFSEP); 80.399 - funcname = lua_pushfstring(L, POF"%s", funcname); 80.400 - lua_remove(L, -2); /* remove 'gsub' result */ 80.401 - return funcname; 80.402 -} 80.403 - 80.404 - 80.405 -static int loader_C (lua_State *L) { 80.406 - const char *funcname; 80.407 - const char *name = luaL_checkstring(L, 1); 80.408 - const char *filename = findfile(L, name, "cpath"); 80.409 - if (filename == NULL) return 1; /* library not found in this path */ 80.410 - funcname = mkfuncname(L, name); 80.411 - if (ll_loadfunc(L, filename, funcname) != 0) 80.412 - loaderror(L, filename); 80.413 - return 1; /* library loaded successfully */ 80.414 -} 80.415 - 80.416 - 80.417 -static int loader_Croot (lua_State *L) { 80.418 - const char *funcname; 80.419 - const char *filename; 80.420 - const char *name = luaL_checkstring(L, 1); 80.421 - const char *p = strchr(name, '.'); 80.422 - int stat; 80.423 - if (p == NULL) return 0; /* is root */ 80.424 - lua_pushlstring(L, name, p - name); 80.425 - filename = findfile(L, lua_tostring(L, -1), "cpath"); 80.426 - if (filename == NULL) return 1; /* root not found */ 80.427 - funcname = mkfuncname(L, name); 80.428 - if ((stat = ll_loadfunc(L, filename, funcname)) != 0) { 80.429 - if (stat != ERRFUNC) loaderror(L, filename); /* real error */ 80.430 - lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, 80.431 - name, filename); 80.432 - return 1; /* function not found */ 80.433 - } 80.434 - return 1; 80.435 -} 80.436 - 80.437 - 80.438 -static int loader_preload (lua_State *L) { 80.439 - const char *name = luaL_checkstring(L, 1); 80.440 - lua_getfield(L, LUA_ENVIRONINDEX, "preload"); 80.441 - if (!lua_istable(L, -1)) 80.442 - luaL_error(L, LUA_QL("package.preload") " must be a table"); 80.443 - lua_getfield(L, -1, name); 80.444 - if (lua_isnil(L, -1)) /* not found? */ 80.445 - lua_pushfstring(L, "\n\tno field package.preload['%s']", name); 80.446 - return 1; 80.447 -} 80.448 - 80.449 - 80.450 -static const int sentinel_ = 0; 80.451 -#define sentinel ((void *)&sentinel_) 80.452 - 80.453 - 80.454 -static int ll_require (lua_State *L) { 80.455 - const char *name = luaL_checkstring(L, 1); 80.456 - int i; 80.457 - lua_settop(L, 1); /* _LOADED table will be at index 2 */ 80.458 - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); 80.459 - lua_getfield(L, 2, name); 80.460 - if (lua_toboolean(L, -1)) { /* is it there? */ 80.461 - if (lua_touserdata(L, -1) == sentinel) /* check loops */ 80.462 - luaL_error(L, "loop or previous error loading module " LUA_QS, name); 80.463 - return 1; /* package is already loaded */ 80.464 - } 80.465 - /* else must load it; iterate over available loaders */ 80.466 - lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); 80.467 - if (!lua_istable(L, -1)) 80.468 - luaL_error(L, LUA_QL("package.loaders") " must be a table"); 80.469 - lua_pushliteral(L, ""); /* error message accumulator */ 80.470 - for (i=1; ; i++) { 80.471 - lua_rawgeti(L, -2, i); /* get a loader */ 80.472 - if (lua_isnil(L, -1)) 80.473 - luaL_error(L, "module " LUA_QS " not found:%s", 80.474 - name, lua_tostring(L, -2)); 80.475 - lua_pushstring(L, name); 80.476 - lua_call(L, 1, 1); /* call it */ 80.477 - if (lua_isfunction(L, -1)) /* did it find module? */ 80.478 - break; /* module loaded successfully */ 80.479 - else if (lua_isstring(L, -1)) /* loader returned error message? */ 80.480 - lua_concat(L, 2); /* accumulate it */ 80.481 - else 80.482 - lua_pop(L, 1); 80.483 - } 80.484 - lua_pushlightuserdata(L, sentinel); 80.485 - lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ 80.486 - lua_pushstring(L, name); /* pass name as argument to module */ 80.487 - lua_call(L, 1, 1); /* run loaded module */ 80.488 - if (!lua_isnil(L, -1)) /* non-nil return? */ 80.489 - lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ 80.490 - lua_getfield(L, 2, name); 80.491 - if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ 80.492 - lua_pushboolean(L, 1); /* use true as result */ 80.493 - lua_pushvalue(L, -1); /* extra copy to be returned */ 80.494 - lua_setfield(L, 2, name); /* _LOADED[name] = true */ 80.495 - } 80.496 - return 1; 80.497 -} 80.498 - 80.499 -/* }====================================================== */ 80.500 - 80.501 - 80.502 - 80.503 -/* 80.504 -** {====================================================== 80.505 -** 'module' function 80.506 -** ======================================================= 80.507 -*/ 80.508 - 80.509 - 80.510 -static void setfenv (lua_State *L) { 80.511 - lua_Debug ar; 80.512 - if (lua_getstack(L, 1, &ar) == 0 || 80.513 - lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ 80.514 - lua_iscfunction(L, -1)) 80.515 - luaL_error(L, LUA_QL("module") " not called from a Lua function"); 80.516 - lua_pushvalue(L, -2); 80.517 - lua_setfenv(L, -2); 80.518 - lua_pop(L, 1); 80.519 -} 80.520 - 80.521 - 80.522 -static void dooptions (lua_State *L, int n) { 80.523 - int i; 80.524 - for (i = 2; i <= n; i++) { 80.525 - lua_pushvalue(L, i); /* get option (a function) */ 80.526 - lua_pushvalue(L, -2); /* module */ 80.527 - lua_call(L, 1, 0); 80.528 - } 80.529 -} 80.530 - 80.531 - 80.532 -static void modinit (lua_State *L, const char *modname) { 80.533 - const char *dot; 80.534 - lua_pushvalue(L, -1); 80.535 - lua_setfield(L, -2, "_M"); /* module._M = module */ 80.536 - lua_pushstring(L, modname); 80.537 - lua_setfield(L, -2, "_NAME"); 80.538 - dot = strrchr(modname, '.'); /* look for last dot in module name */ 80.539 - if (dot == NULL) dot = modname; 80.540 - else dot++; 80.541 - /* set _PACKAGE as package name (full module name minus last part) */ 80.542 - lua_pushlstring(L, modname, dot - modname); 80.543 - lua_setfield(L, -2, "_PACKAGE"); 80.544 -} 80.545 - 80.546 - 80.547 -static int ll_module (lua_State *L) { 80.548 - const char *modname = luaL_checkstring(L, 1); 80.549 - int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ 80.550 - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); 80.551 - lua_getfield(L, loaded, modname); /* get _LOADED[modname] */ 80.552 - if (!lua_istable(L, -1)) { /* not found? */ 80.553 - lua_pop(L, 1); /* remove previous result */ 80.554 - /* try global variable (and create one if it does not exist) */ 80.555 - if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL) 80.556 - return luaL_error(L, "name conflict for module " LUA_QS, modname); 80.557 - lua_pushvalue(L, -1); 80.558 - lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ 80.559 - } 80.560 - /* check whether table already has a _NAME field */ 80.561 - lua_getfield(L, -1, "_NAME"); 80.562 - if (!lua_isnil(L, -1)) /* is table an initialized module? */ 80.563 - lua_pop(L, 1); 80.564 - else { /* no; initialize it */ 80.565 - lua_pop(L, 1); 80.566 - modinit(L, modname); 80.567 - } 80.568 - lua_pushvalue(L, -1); 80.569 - setfenv(L); 80.570 - dooptions(L, loaded - 1); 80.571 - return 0; 80.572 -} 80.573 - 80.574 - 80.575 -static int ll_seeall (lua_State *L) { 80.576 - luaL_checktype(L, 1, LUA_TTABLE); 80.577 - if (!lua_getmetatable(L, 1)) { 80.578 - lua_createtable(L, 0, 1); /* create new metatable */ 80.579 - lua_pushvalue(L, -1); 80.580 - lua_setmetatable(L, 1); 80.581 - } 80.582 - lua_pushvalue(L, LUA_GLOBALSINDEX); 80.583 - lua_setfield(L, -2, "__index"); /* mt.__index = _G */ 80.584 - return 0; 80.585 -} 80.586 - 80.587 - 80.588 -/* }====================================================== */ 80.589 - 80.590 - 80.591 - 80.592 -/* auxiliary mark (for internal use) */ 80.593 -#define AUXMARK "\1" 80.594 - 80.595 -static void setpath (lua_State *L, const char *fieldname, const char *envname, 80.596 - const char *def) { 80.597 - const char *path = getenv(envname); 80.598 - if (path == NULL) /* no environment variable? */ 80.599 - lua_pushstring(L, def); /* use default */ 80.600 - else { 80.601 - /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ 80.602 - path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP, 80.603 - LUA_PATHSEP AUXMARK LUA_PATHSEP); 80.604 - luaL_gsub(L, path, AUXMARK, def); 80.605 - lua_remove(L, -2); 80.606 - } 80.607 - setprogdir(L); 80.608 - lua_setfield(L, -2, fieldname); 80.609 -} 80.610 - 80.611 - 80.612 -static const luaL_Reg pk_funcs[] = { 80.613 - {"loadlib", ll_loadlib}, 80.614 - {"seeall", ll_seeall}, 80.615 - {NULL, NULL} 80.616 -}; 80.617 - 80.618 - 80.619 -static const luaL_Reg ll_funcs[] = { 80.620 - {"module", ll_module}, 80.621 - {"require", ll_require}, 80.622 - {NULL, NULL} 80.623 -}; 80.624 - 80.625 - 80.626 -static const lua_CFunction loaders[] = 80.627 - {loader_preload, loader_Lua, loader_C, loader_Croot, NULL}; 80.628 - 80.629 - 80.630 -LUALIB_API int luaopen_package (lua_State *L) { 80.631 - int i; 80.632 - /* create new type _LOADLIB */ 80.633 - luaL_newmetatable(L, "_LOADLIB"); 80.634 - lua_pushcfunction(L, gctm); 80.635 - lua_setfield(L, -2, "__gc"); 80.636 - /* create `package' table */ 80.637 - luaL_register(L, LUA_LOADLIBNAME, pk_funcs); 80.638 -#if defined(LUA_COMPAT_LOADLIB) 80.639 - lua_getfield(L, -1, "loadlib"); 80.640 - lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); 80.641 -#endif 80.642 - lua_pushvalue(L, -1); 80.643 - lua_replace(L, LUA_ENVIRONINDEX); 80.644 - /* create `loaders' table */ 80.645 - lua_createtable(L, 0, sizeof(loaders)/sizeof(loaders[0]) - 1); 80.646 - /* fill it with pre-defined loaders */ 80.647 - for (i=0; loaders[i] != NULL; i++) { 80.648 - lua_pushcfunction(L, loaders[i]); 80.649 - lua_rawseti(L, -2, i+1); 80.650 - } 80.651 - lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ 80.652 - setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */ 80.653 - setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */ 80.654 - /* store config information */ 80.655 - lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" 80.656 - LUA_EXECDIR "\n" LUA_IGMARK); 80.657 - lua_setfield(L, -2, "config"); 80.658 - /* set field `loaded' */ 80.659 - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); 80.660 - lua_setfield(L, -2, "loaded"); 80.661 - /* set field `preload' */ 80.662 - lua_newtable(L); 80.663 - lua_setfield(L, -2, "preload"); 80.664 - lua_pushvalue(L, LUA_GLOBALSINDEX); 80.665 - luaL_register(L, NULL, ll_funcs); /* open lib into global table */ 80.666 - lua_pop(L, 1); 80.667 - return 1; /* return 'package' table */ 80.668 -} 80.669 -
81.1 --- a/src/lua/src/lobject.c Sat Mar 03 11:04:56 2012 -0600 81.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 81.3 @@ -1,214 +0,0 @@ 81.4 -/* 81.5 -** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $ 81.6 -** Some generic functions over Lua objects 81.7 -** See Copyright Notice in lua.h 81.8 -*/ 81.9 - 81.10 -#include <ctype.h> 81.11 -#include <stdarg.h> 81.12 -#include <stdio.h> 81.13 -#include <stdlib.h> 81.14 -#include <string.h> 81.15 - 81.16 -#define lobject_c 81.17 -#define LUA_CORE 81.18 - 81.19 -#include "lua.h" 81.20 - 81.21 -#include "ldo.h" 81.22 -#include "lmem.h" 81.23 -#include "lobject.h" 81.24 -#include "lstate.h" 81.25 -#include "lstring.h" 81.26 -#include "lvm.h" 81.27 - 81.28 - 81.29 - 81.30 -const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL}; 81.31 - 81.32 - 81.33 -/* 81.34 -** converts an integer to a "floating point byte", represented as 81.35 -** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if 81.36 -** eeeee != 0 and (xxx) otherwise. 81.37 -*/ 81.38 -int luaO_int2fb (unsigned int x) { 81.39 - int e = 0; /* expoent */ 81.40 - while (x >= 16) { 81.41 - x = (x+1) >> 1; 81.42 - e++; 81.43 - } 81.44 - if (x < 8) return x; 81.45 - else return ((e+1) << 3) | (cast_int(x) - 8); 81.46 -} 81.47 - 81.48 - 81.49 -/* converts back */ 81.50 -int luaO_fb2int (int x) { 81.51 - int e = (x >> 3) & 31; 81.52 - if (e == 0) return x; 81.53 - else return ((x & 7)+8) << (e - 1); 81.54 -} 81.55 - 81.56 - 81.57 -int luaO_log2 (unsigned int x) { 81.58 - static const lu_byte log_2[256] = { 81.59 - 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 81.60 - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 81.61 - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 81.62 - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 81.63 - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 81.64 - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 81.65 - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 81.66 - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 81.67 - }; 81.68 - int l = -1; 81.69 - while (x >= 256) { l += 8; x >>= 8; } 81.70 - return l + log_2[x]; 81.71 - 81.72 -} 81.73 - 81.74 - 81.75 -int luaO_rawequalObj (const TValue *t1, const TValue *t2) { 81.76 - if (ttype(t1) != ttype(t2)) return 0; 81.77 - else switch (ttype(t1)) { 81.78 - case LUA_TNIL: 81.79 - return 1; 81.80 - case LUA_TNUMBER: 81.81 - return luai_numeq(nvalue(t1), nvalue(t2)); 81.82 - case LUA_TBOOLEAN: 81.83 - return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ 81.84 - case LUA_TLIGHTUSERDATA: 81.85 - return pvalue(t1) == pvalue(t2); 81.86 - default: 81.87 - lua_assert(iscollectable(t1)); 81.88 - return gcvalue(t1) == gcvalue(t2); 81.89 - } 81.90 -} 81.91 - 81.92 - 81.93 -int luaO_str2d (const char *s, lua_Number *result) { 81.94 - char *endptr; 81.95 - *result = lua_str2number(s, &endptr); 81.96 - if (endptr == s) return 0; /* conversion failed */ 81.97 - if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */ 81.98 - *result = cast_num(strtoul(s, &endptr, 16)); 81.99 - if (*endptr == '\0') return 1; /* most common case */ 81.100 - while (isspace(cast(unsigned char, *endptr))) endptr++; 81.101 - if (*endptr != '\0') return 0; /* invalid trailing characters? */ 81.102 - return 1; 81.103 -} 81.104 - 81.105 - 81.106 - 81.107 -static void pushstr (lua_State *L, const char *str) { 81.108 - setsvalue2s(L, L->top, luaS_new(L, str)); 81.109 - incr_top(L); 81.110 -} 81.111 - 81.112 - 81.113 -/* this function handles only `%d', `%c', %f, %p, and `%s' formats */ 81.114 -const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { 81.115 - int n = 1; 81.116 - pushstr(L, ""); 81.117 - for (;;) { 81.118 - const char *e = strchr(fmt, '%'); 81.119 - if (e == NULL) break; 81.120 - setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); 81.121 - incr_top(L); 81.122 - switch (*(e+1)) { 81.123 - case 's': { 81.124 - const char *s = va_arg(argp, char *); 81.125 - if (s == NULL) s = "(null)"; 81.126 - pushstr(L, s); 81.127 - break; 81.128 - } 81.129 - case 'c': { 81.130 - char buff[2]; 81.131 - buff[0] = cast(char, va_arg(argp, int)); 81.132 - buff[1] = '\0'; 81.133 - pushstr(L, buff); 81.134 - break; 81.135 - } 81.136 - case 'd': { 81.137 - setnvalue(L->top, cast_num(va_arg(argp, int))); 81.138 - incr_top(L); 81.139 - break; 81.140 - } 81.141 - case 'f': { 81.142 - setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); 81.143 - incr_top(L); 81.144 - break; 81.145 - } 81.146 - case 'p': { 81.147 - char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ 81.148 - sprintf(buff, "%p", va_arg(argp, void *)); 81.149 - pushstr(L, buff); 81.150 - break; 81.151 - } 81.152 - case '%': { 81.153 - pushstr(L, "%"); 81.154 - break; 81.155 - } 81.156 - default: { 81.157 - char buff[3]; 81.158 - buff[0] = '%'; 81.159 - buff[1] = *(e+1); 81.160 - buff[2] = '\0'; 81.161 - pushstr(L, buff); 81.162 - break; 81.163 - } 81.164 - } 81.165 - n += 2; 81.166 - fmt = e+2; 81.167 - } 81.168 - pushstr(L, fmt); 81.169 - luaV_concat(L, n+1, cast_int(L->top - L->base) - 1); 81.170 - L->top -= n; 81.171 - return svalue(L->top - 1); 81.172 -} 81.173 - 81.174 - 81.175 -const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { 81.176 - const char *msg; 81.177 - va_list argp; 81.178 - va_start(argp, fmt); 81.179 - msg = luaO_pushvfstring(L, fmt, argp); 81.180 - va_end(argp); 81.181 - return msg; 81.182 -} 81.183 - 81.184 - 81.185 -void luaO_chunkid (char *out, const char *source, size_t bufflen) { 81.186 - if (*source == '=') { 81.187 - strncpy(out, source+1, bufflen); /* remove first char */ 81.188 - out[bufflen-1] = '\0'; /* ensures null termination */ 81.189 - } 81.190 - else { /* out = "source", or "...source" */ 81.191 - if (*source == '@') { 81.192 - size_t l; 81.193 - source++; /* skip the `@' */ 81.194 - bufflen -= sizeof(" '...' "); 81.195 - l = strlen(source); 81.196 - strcpy(out, ""); 81.197 - if (l > bufflen) { 81.198 - source += (l-bufflen); /* get last part of file name */ 81.199 - strcat(out, "..."); 81.200 - } 81.201 - strcat(out, source); 81.202 - } 81.203 - else { /* out = [string "string"] */ 81.204 - size_t len = strcspn(source, "\n\r"); /* stop at first newline */ 81.205 - bufflen -= sizeof(" [string \"...\"] "); 81.206 - if (len > bufflen) len = bufflen; 81.207 - strcpy(out, "[string \""); 81.208 - if (source[len] != '\0') { /* must truncate? */ 81.209 - strncat(out, source, len); 81.210 - strcat(out, "..."); 81.211 - } 81.212 - else 81.213 - strcat(out, source); 81.214 - strcat(out, "\"]"); 81.215 - } 81.216 - } 81.217 -}
82.1 --- a/src/lua/src/lobject.h Sat Mar 03 11:04:56 2012 -0600 82.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 82.3 @@ -1,381 +0,0 @@ 82.4 -/* 82.5 -** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $ 82.6 -** Type definitions for Lua objects 82.7 -** See Copyright Notice in lua.h 82.8 -*/ 82.9 - 82.10 - 82.11 -#ifndef lobject_h 82.12 -#define lobject_h 82.13 - 82.14 - 82.15 -#include <stdarg.h> 82.16 - 82.17 - 82.18 -#include "llimits.h" 82.19 -#include "lua.h" 82.20 - 82.21 - 82.22 -/* tags for values visible from Lua */ 82.23 -#define LAST_TAG LUA_TTHREAD 82.24 - 82.25 -#define NUM_TAGS (LAST_TAG+1) 82.26 - 82.27 - 82.28 -/* 82.29 -** Extra tags for non-values 82.30 -*/ 82.31 -#define LUA_TPROTO (LAST_TAG+1) 82.32 -#define LUA_TUPVAL (LAST_TAG+2) 82.33 -#define LUA_TDEADKEY (LAST_TAG+3) 82.34 - 82.35 - 82.36 -/* 82.37 -** Union of all collectable objects 82.38 -*/ 82.39 -typedef union GCObject GCObject; 82.40 - 82.41 - 82.42 -/* 82.43 -** Common Header for all collectable objects (in macro form, to be 82.44 -** included in other objects) 82.45 -*/ 82.46 -#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked 82.47 - 82.48 - 82.49 -/* 82.50 -** Common header in struct form 82.51 -*/ 82.52 -typedef struct GCheader { 82.53 - CommonHeader; 82.54 -} GCheader; 82.55 - 82.56 - 82.57 - 82.58 - 82.59 -/* 82.60 -** Union of all Lua values 82.61 -*/ 82.62 -typedef union { 82.63 - GCObject *gc; 82.64 - void *p; 82.65 - lua_Number n; 82.66 - int b; 82.67 -} Value; 82.68 - 82.69 - 82.70 -/* 82.71 -** Tagged Values 82.72 -*/ 82.73 - 82.74 -#define TValuefields Value value; int tt 82.75 - 82.76 -typedef struct lua_TValue { 82.77 - TValuefields; 82.78 -} TValue; 82.79 - 82.80 - 82.81 -/* Macros to test type */ 82.82 -#define ttisnil(o) (ttype(o) == LUA_TNIL) 82.83 -#define ttisnumber(o) (ttype(o) == LUA_TNUMBER) 82.84 -#define ttisstring(o) (ttype(o) == LUA_TSTRING) 82.85 -#define ttistable(o) (ttype(o) == LUA_TTABLE) 82.86 -#define ttisfunction(o) (ttype(o) == LUA_TFUNCTION) 82.87 -#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN) 82.88 -#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA) 82.89 -#define ttisthread(o) (ttype(o) == LUA_TTHREAD) 82.90 -#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) 82.91 - 82.92 -/* Macros to access values */ 82.93 -#define ttype(o) ((o)->tt) 82.94 -#define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc) 82.95 -#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) 82.96 -#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) 82.97 -#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts) 82.98 -#define tsvalue(o) (&rawtsvalue(o)->tsv) 82.99 -#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u) 82.100 -#define uvalue(o) (&rawuvalue(o)->uv) 82.101 -#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl) 82.102 -#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h) 82.103 -#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) 82.104 -#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th) 82.105 - 82.106 -#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) 82.107 - 82.108 -/* 82.109 -** for internal debug only 82.110 -*/ 82.111 -#define checkconsistency(obj) \ 82.112 - lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt)) 82.113 - 82.114 -#define checkliveness(g,obj) \ 82.115 - lua_assert(!iscollectable(obj) || \ 82.116 - ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc))) 82.117 - 82.118 - 82.119 -/* Macros to set values */ 82.120 -#define setnilvalue(obj) ((obj)->tt=LUA_TNIL) 82.121 - 82.122 -#define setnvalue(obj,x) \ 82.123 - { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; } 82.124 - 82.125 -#define setpvalue(obj,x) \ 82.126 - { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; } 82.127 - 82.128 -#define setbvalue(obj,x) \ 82.129 - { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; } 82.130 - 82.131 -#define setsvalue(L,obj,x) \ 82.132 - { TValue *i_o=(obj); \ 82.133 - i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \ 82.134 - checkliveness(G(L),i_o); } 82.135 - 82.136 -#define setuvalue(L,obj,x) \ 82.137 - { TValue *i_o=(obj); \ 82.138 - i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \ 82.139 - checkliveness(G(L),i_o); } 82.140 - 82.141 -#define setthvalue(L,obj,x) \ 82.142 - { TValue *i_o=(obj); \ 82.143 - i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \ 82.144 - checkliveness(G(L),i_o); } 82.145 - 82.146 -#define setclvalue(L,obj,x) \ 82.147 - { TValue *i_o=(obj); \ 82.148 - i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \ 82.149 - checkliveness(G(L),i_o); } 82.150 - 82.151 -#define sethvalue(L,obj,x) \ 82.152 - { TValue *i_o=(obj); \ 82.153 - i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \ 82.154 - checkliveness(G(L),i_o); } 82.155 - 82.156 -#define setptvalue(L,obj,x) \ 82.157 - { TValue *i_o=(obj); \ 82.158 - i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \ 82.159 - checkliveness(G(L),i_o); } 82.160 - 82.161 - 82.162 - 82.163 - 82.164 -#define setobj(L,obj1,obj2) \ 82.165 - { const TValue *o2=(obj2); TValue *o1=(obj1); \ 82.166 - o1->value = o2->value; o1->tt=o2->tt; \ 82.167 - checkliveness(G(L),o1); } 82.168 - 82.169 - 82.170 -/* 82.171 -** different types of sets, according to destination 82.172 -*/ 82.173 - 82.174 -/* from stack to (same) stack */ 82.175 -#define setobjs2s setobj 82.176 -/* to stack (not from same stack) */ 82.177 -#define setobj2s setobj 82.178 -#define setsvalue2s setsvalue 82.179 -#define sethvalue2s sethvalue 82.180 -#define setptvalue2s setptvalue 82.181 -/* from table to same table */ 82.182 -#define setobjt2t setobj 82.183 -/* to table */ 82.184 -#define setobj2t setobj 82.185 -/* to new object */ 82.186 -#define setobj2n setobj 82.187 -#define setsvalue2n setsvalue 82.188 - 82.189 -#define setttype(obj, tt) (ttype(obj) = (tt)) 82.190 - 82.191 - 82.192 -#define iscollectable(o) (ttype(o) >= LUA_TSTRING) 82.193 - 82.194 - 82.195 - 82.196 -typedef TValue *StkId; /* index to stack elements */ 82.197 - 82.198 - 82.199 -/* 82.200 -** String headers for string table 82.201 -*/ 82.202 -typedef union TString { 82.203 - L_Umaxalign dummy; /* ensures maximum alignment for strings */ 82.204 - struct { 82.205 - CommonHeader; 82.206 - lu_byte reserved; 82.207 - unsigned int hash; 82.208 - size_t len; 82.209 - } tsv; 82.210 -} TString; 82.211 - 82.212 - 82.213 -#define getstr(ts) cast(const char *, (ts) + 1) 82.214 -#define svalue(o) getstr(rawtsvalue(o)) 82.215 - 82.216 - 82.217 - 82.218 -typedef union Udata { 82.219 - L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ 82.220 - struct { 82.221 - CommonHeader; 82.222 - struct Table *metatable; 82.223 - struct Table *env; 82.224 - size_t len; 82.225 - } uv; 82.226 -} Udata; 82.227 - 82.228 - 82.229 - 82.230 - 82.231 -/* 82.232 -** Function Prototypes 82.233 -*/ 82.234 -typedef struct Proto { 82.235 - CommonHeader; 82.236 - TValue *k; /* constants used by the function */ 82.237 - Instruction *code; 82.238 - struct Proto **p; /* functions defined inside the function */ 82.239 - int *lineinfo; /* map from opcodes to source lines */ 82.240 - struct LocVar *locvars; /* information about local variables */ 82.241 - TString **upvalues; /* upvalue names */ 82.242 - TString *source; 82.243 - int sizeupvalues; 82.244 - int sizek; /* size of `k' */ 82.245 - int sizecode; 82.246 - int sizelineinfo; 82.247 - int sizep; /* size of `p' */ 82.248 - int sizelocvars; 82.249 - int linedefined; 82.250 - int lastlinedefined; 82.251 - GCObject *gclist; 82.252 - lu_byte nups; /* number of upvalues */ 82.253 - lu_byte numparams; 82.254 - lu_byte is_vararg; 82.255 - lu_byte maxstacksize; 82.256 -} Proto; 82.257 - 82.258 - 82.259 -/* masks for new-style vararg */ 82.260 -#define VARARG_HASARG 1 82.261 -#define VARARG_ISVARARG 2 82.262 -#define VARARG_NEEDSARG 4 82.263 - 82.264 - 82.265 -typedef struct LocVar { 82.266 - TString *varname; 82.267 - int startpc; /* first point where variable is active */ 82.268 - int endpc; /* first point where variable is dead */ 82.269 -} LocVar; 82.270 - 82.271 - 82.272 - 82.273 -/* 82.274 -** Upvalues 82.275 -*/ 82.276 - 82.277 -typedef struct UpVal { 82.278 - CommonHeader; 82.279 - TValue *v; /* points to stack or to its own value */ 82.280 - union { 82.281 - TValue value; /* the value (when closed) */ 82.282 - struct { /* double linked list (when open) */ 82.283 - struct UpVal *prev; 82.284 - struct UpVal *next; 82.285 - } l; 82.286 - } u; 82.287 -} UpVal; 82.288 - 82.289 - 82.290 -/* 82.291 -** Closures 82.292 -*/ 82.293 - 82.294 -#define ClosureHeader \ 82.295 - CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \ 82.296 - struct Table *env 82.297 - 82.298 -typedef struct CClosure { 82.299 - ClosureHeader; 82.300 - lua_CFunction f; 82.301 - TValue upvalue[1]; 82.302 -} CClosure; 82.303 - 82.304 - 82.305 -typedef struct LClosure { 82.306 - ClosureHeader; 82.307 - struct Proto *p; 82.308 - UpVal *upvals[1]; 82.309 -} LClosure; 82.310 - 82.311 - 82.312 -typedef union Closure { 82.313 - CClosure c; 82.314 - LClosure l; 82.315 -} Closure; 82.316 - 82.317 - 82.318 -#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC) 82.319 -#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC) 82.320 - 82.321 - 82.322 -/* 82.323 -** Tables 82.324 -*/ 82.325 - 82.326 -typedef union TKey { 82.327 - struct { 82.328 - TValuefields; 82.329 - struct Node *next; /* for chaining */ 82.330 - } nk; 82.331 - TValue tvk; 82.332 -} TKey; 82.333 - 82.334 - 82.335 -typedef struct Node { 82.336 - TValue i_val; 82.337 - TKey i_key; 82.338 -} Node; 82.339 - 82.340 - 82.341 -typedef struct Table { 82.342 - CommonHeader; 82.343 - lu_byte flags; /* 1<<p means tagmethod(p) is not present */ 82.344 - lu_byte lsizenode; /* log2 of size of `node' array */ 82.345 - struct Table *metatable; 82.346 - TValue *array; /* array part */ 82.347 - Node *node; 82.348 - Node *lastfree; /* any free position is before this position */ 82.349 - GCObject *gclist; 82.350 - int sizearray; /* size of `array' array */ 82.351 -} Table; 82.352 - 82.353 - 82.354 - 82.355 -/* 82.356 -** `module' operation for hashing (size is always a power of 2) 82.357 -*/ 82.358 -#define lmod(s,size) \ 82.359 - (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1))))) 82.360 - 82.361 - 82.362 -#define twoto(x) (1<<(x)) 82.363 -#define sizenode(t) (twoto((t)->lsizenode)) 82.364 - 82.365 - 82.366 -#define luaO_nilobject (&luaO_nilobject_) 82.367 - 82.368 -LUAI_DATA const TValue luaO_nilobject_; 82.369 - 82.370 -#define ceillog2(x) (luaO_log2((x)-1) + 1) 82.371 - 82.372 -LUAI_FUNC int luaO_log2 (unsigned int x); 82.373 -LUAI_FUNC int luaO_int2fb (unsigned int x); 82.374 -LUAI_FUNC int luaO_fb2int (int x); 82.375 -LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2); 82.376 -LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result); 82.377 -LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, 82.378 - va_list argp); 82.379 -LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); 82.380 -LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); 82.381 - 82.382 - 82.383 -#endif 82.384 -
83.1 --- a/src/lua/src/lopcodes.c Sat Mar 03 11:04:56 2012 -0600 83.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 83.3 @@ -1,102 +0,0 @@ 83.4 -/* 83.5 -** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 83.6 -** See Copyright Notice in lua.h 83.7 -*/ 83.8 - 83.9 - 83.10 -#define lopcodes_c 83.11 -#define LUA_CORE 83.12 - 83.13 - 83.14 -#include "lopcodes.h" 83.15 - 83.16 - 83.17 -/* ORDER OP */ 83.18 - 83.19 -const char *const luaP_opnames[NUM_OPCODES+1] = { 83.20 - "MOVE", 83.21 - "LOADK", 83.22 - "LOADBOOL", 83.23 - "LOADNIL", 83.24 - "GETUPVAL", 83.25 - "GETGLOBAL", 83.26 - "GETTABLE", 83.27 - "SETGLOBAL", 83.28 - "SETUPVAL", 83.29 - "SETTABLE", 83.30 - "NEWTABLE", 83.31 - "SELF", 83.32 - "ADD", 83.33 - "SUB", 83.34 - "MUL", 83.35 - "DIV", 83.36 - "MOD", 83.37 - "POW", 83.38 - "UNM", 83.39 - "NOT", 83.40 - "LEN", 83.41 - "CONCAT", 83.42 - "JMP", 83.43 - "EQ", 83.44 - "LT", 83.45 - "LE", 83.46 - "TEST", 83.47 - "TESTSET", 83.48 - "CALL", 83.49 - "TAILCALL", 83.50 - "RETURN", 83.51 - "FORLOOP", 83.52 - "FORPREP", 83.53 - "TFORLOOP", 83.54 - "SETLIST", 83.55 - "CLOSE", 83.56 - "CLOSURE", 83.57 - "VARARG", 83.58 - NULL 83.59 -}; 83.60 - 83.61 - 83.62 -#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 83.63 - 83.64 -const lu_byte luaP_opmodes[NUM_OPCODES] = { 83.65 -/* T A B C mode opcode */ 83.66 - opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 83.67 - ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 83.68 - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 83.69 - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */ 83.70 - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 83.71 - ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */ 83.72 - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 83.73 - ,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */ 83.74 - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 83.75 - ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 83.76 - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 83.77 - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 83.78 - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 83.79 - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 83.80 - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 83.81 - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 83.82 - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 83.83 - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 83.84 - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 83.85 - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 83.86 - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 83.87 - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 83.88 - ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 83.89 - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 83.90 - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 83.91 - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 83.92 - ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */ 83.93 - ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 83.94 - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 83.95 - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 83.96 - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 83.97 - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 83.98 - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 83.99 - ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */ 83.100 - ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 83.101 - ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ 83.102 - ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 83.103 - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 83.104 -}; 83.105 -
84.1 --- a/src/lua/src/lopcodes.h Sat Mar 03 11:04:56 2012 -0600 84.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 84.3 @@ -1,268 +0,0 @@ 84.4 -/* 84.5 -** $Id: lopcodes.h,v 1.125.1.1 2007/12/27 13:02:25 roberto Exp $ 84.6 -** Opcodes for Lua virtual machine 84.7 -** See Copyright Notice in lua.h 84.8 -*/ 84.9 - 84.10 -#ifndef lopcodes_h 84.11 -#define lopcodes_h 84.12 - 84.13 -#include "llimits.h" 84.14 - 84.15 - 84.16 -/*=========================================================================== 84.17 - We assume that instructions are unsigned numbers. 84.18 - All instructions have an opcode in the first 6 bits. 84.19 - Instructions can have the following fields: 84.20 - `A' : 8 bits 84.21 - `B' : 9 bits 84.22 - `C' : 9 bits 84.23 - `Bx' : 18 bits (`B' and `C' together) 84.24 - `sBx' : signed Bx 84.25 - 84.26 - A signed argument is represented in excess K; that is, the number 84.27 - value is the unsigned value minus K. K is exactly the maximum value 84.28 - for that argument (so that -max is represented by 0, and +max is 84.29 - represented by 2*max), which is half the maximum for the corresponding 84.30 - unsigned argument. 84.31 -===========================================================================*/ 84.32 - 84.33 - 84.34 -enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */ 84.35 - 84.36 - 84.37 -/* 84.38 -** size and position of opcode arguments. 84.39 -*/ 84.40 -#define SIZE_C 9 84.41 -#define SIZE_B 9 84.42 -#define SIZE_Bx (SIZE_C + SIZE_B) 84.43 -#define SIZE_A 8 84.44 - 84.45 -#define SIZE_OP 6 84.46 - 84.47 -#define POS_OP 0 84.48 -#define POS_A (POS_OP + SIZE_OP) 84.49 -#define POS_C (POS_A + SIZE_A) 84.50 -#define POS_B (POS_C + SIZE_C) 84.51 -#define POS_Bx POS_C 84.52 - 84.53 - 84.54 -/* 84.55 -** limits for opcode arguments. 84.56 -** we use (signed) int to manipulate most arguments, 84.57 -** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) 84.58 -*/ 84.59 -#if SIZE_Bx < LUAI_BITSINT-1 84.60 -#define MAXARG_Bx ((1<<SIZE_Bx)-1) 84.61 -#define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */ 84.62 -#else 84.63 -#define MAXARG_Bx MAX_INT 84.64 -#define MAXARG_sBx MAX_INT 84.65 -#endif 84.66 - 84.67 - 84.68 -#define MAXARG_A ((1<<SIZE_A)-1) 84.69 -#define MAXARG_B ((1<<SIZE_B)-1) 84.70 -#define MAXARG_C ((1<<SIZE_C)-1) 84.71 - 84.72 - 84.73 -/* creates a mask with `n' 1 bits at position `p' */ 84.74 -#define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p) 84.75 - 84.76 -/* creates a mask with `n' 0 bits at position `p' */ 84.77 -#define MASK0(n,p) (~MASK1(n,p)) 84.78 - 84.79 -/* 84.80 -** the following macros help to manipulate instructions 84.81 -*/ 84.82 - 84.83 -#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0))) 84.84 -#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ 84.85 - ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP)))) 84.86 - 84.87 -#define GETARG_A(i) (cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0))) 84.88 -#define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ 84.89 - ((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A)))) 84.90 - 84.91 -#define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0))) 84.92 -#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ 84.93 - ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B)))) 84.94 - 84.95 -#define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0))) 84.96 -#define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \ 84.97 - ((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C)))) 84.98 - 84.99 -#define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0))) 84.100 -#define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \ 84.101 - ((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx)))) 84.102 - 84.103 -#define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx) 84.104 -#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx)) 84.105 - 84.106 - 84.107 -#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \ 84.108 - | (cast(Instruction, a)<<POS_A) \ 84.109 - | (cast(Instruction, b)<<POS_B) \ 84.110 - | (cast(Instruction, c)<<POS_C)) 84.111 - 84.112 -#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \ 84.113 - | (cast(Instruction, a)<<POS_A) \ 84.114 - | (cast(Instruction, bc)<<POS_Bx)) 84.115 - 84.116 - 84.117 -/* 84.118 -** Macros to operate RK indices 84.119 -*/ 84.120 - 84.121 -/* this bit 1 means constant (0 means register) */ 84.122 -#define BITRK (1 << (SIZE_B - 1)) 84.123 - 84.124 -/* test whether value is a constant */ 84.125 -#define ISK(x) ((x) & BITRK) 84.126 - 84.127 -/* gets the index of the constant */ 84.128 -#define INDEXK(r) ((int)(r) & ~BITRK) 84.129 - 84.130 -#define MAXINDEXRK (BITRK - 1) 84.131 - 84.132 -/* code a constant index as a RK value */ 84.133 -#define RKASK(x) ((x) | BITRK) 84.134 - 84.135 - 84.136 -/* 84.137 -** invalid register that fits in 8 bits 84.138 -*/ 84.139 -#define NO_REG MAXARG_A 84.140 - 84.141 - 84.142 -/* 84.143 -** R(x) - register 84.144 -** Kst(x) - constant (in constant table) 84.145 -** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x) 84.146 -*/ 84.147 - 84.148 - 84.149 -/* 84.150 -** grep "ORDER OP" if you change these enums 84.151 -*/ 84.152 - 84.153 -typedef enum { 84.154 -/*---------------------------------------------------------------------- 84.155 -name args description 84.156 -------------------------------------------------------------------------*/ 84.157 -OP_MOVE,/* A B R(A) := R(B) */ 84.158 -OP_LOADK,/* A Bx R(A) := Kst(Bx) */ 84.159 -OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ 84.160 -OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */ 84.161 -OP_GETUPVAL,/* A B R(A) := UpValue[B] */ 84.162 - 84.163 -OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */ 84.164 -OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */ 84.165 - 84.166 -OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */ 84.167 -OP_SETUPVAL,/* A B UpValue[B] := R(A) */ 84.168 -OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */ 84.169 - 84.170 -OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ 84.171 - 84.172 -OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ 84.173 - 84.174 -OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ 84.175 -OP_SUB,/* A B C R(A) := RK(B) - RK(C) */ 84.176 -OP_MUL,/* A B C R(A) := RK(B) * RK(C) */ 84.177 -OP_DIV,/* A B C R(A) := RK(B) / RK(C) */ 84.178 -OP_MOD,/* A B C R(A) := RK(B) % RK(C) */ 84.179 -OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */ 84.180 -OP_UNM,/* A B R(A) := -R(B) */ 84.181 -OP_NOT,/* A B R(A) := not R(B) */ 84.182 -OP_LEN,/* A B R(A) := length of R(B) */ 84.183 - 84.184 -OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ 84.185 - 84.186 -OP_JMP,/* sBx pc+=sBx */ 84.187 - 84.188 -OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ 84.189 -OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ 84.190 -OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ 84.191 - 84.192 -OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ 84.193 -OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ 84.194 - 84.195 -OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ 84.196 -OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ 84.197 -OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ 84.198 - 84.199 -OP_FORLOOP,/* A sBx R(A)+=R(A+2); 84.200 - if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/ 84.201 -OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */ 84.202 - 84.203 -OP_TFORLOOP,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); 84.204 - if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */ 84.205 -OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ 84.206 - 84.207 -OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/ 84.208 -OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ 84.209 - 84.210 -OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ 84.211 -} OpCode; 84.212 - 84.213 - 84.214 -#define NUM_OPCODES (cast(int, OP_VARARG) + 1) 84.215 - 84.216 - 84.217 - 84.218 -/*=========================================================================== 84.219 - Notes: 84.220 - (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1, 84.221 - and can be 0: OP_CALL then sets `top' to last_result+1, so 84.222 - next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'. 84.223 - 84.224 - (*) In OP_VARARG, if (B == 0) then use actual number of varargs and 84.225 - set top (like in OP_CALL with C == 0). 84.226 - 84.227 - (*) In OP_RETURN, if (B == 0) then return up to `top' 84.228 - 84.229 - (*) In OP_SETLIST, if (B == 0) then B = `top'; 84.230 - if (C == 0) then next `instruction' is real C 84.231 - 84.232 - (*) For comparisons, A specifies what condition the test should accept 84.233 - (true or false). 84.234 - 84.235 - (*) All `skips' (pc++) assume that next instruction is a jump 84.236 -===========================================================================*/ 84.237 - 84.238 - 84.239 -/* 84.240 -** masks for instruction properties. The format is: 84.241 -** bits 0-1: op mode 84.242 -** bits 2-3: C arg mode 84.243 -** bits 4-5: B arg mode 84.244 -** bit 6: instruction set register A 84.245 -** bit 7: operator is a test 84.246 -*/ 84.247 - 84.248 -enum OpArgMask { 84.249 - OpArgN, /* argument is not used */ 84.250 - OpArgU, /* argument is used */ 84.251 - OpArgR, /* argument is a register or a jump offset */ 84.252 - OpArgK /* argument is a constant or register/constant */ 84.253 -}; 84.254 - 84.255 -LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES]; 84.256 - 84.257 -#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3)) 84.258 -#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3)) 84.259 -#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) 84.260 -#define testAMode(m) (luaP_opmodes[m] & (1 << 6)) 84.261 -#define testTMode(m) (luaP_opmodes[m] & (1 << 7)) 84.262 - 84.263 - 84.264 -LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ 84.265 - 84.266 - 84.267 -/* number of list items to accumulate before a SETLIST instruction */ 84.268 -#define LFIELDS_PER_FLUSH 50 84.269 - 84.270 - 84.271 -#endif
85.1 --- a/src/lua/src/loslib.c Sat Mar 03 11:04:56 2012 -0600 85.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 85.3 @@ -1,243 +0,0 @@ 85.4 -/* 85.5 -** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $ 85.6 -** Standard Operating System library 85.7 -** See Copyright Notice in lua.h 85.8 -*/ 85.9 - 85.10 - 85.11 -#include <errno.h> 85.12 -#include <locale.h> 85.13 -#include <stdlib.h> 85.14 -#include <string.h> 85.15 -#include <time.h> 85.16 - 85.17 -#define loslib_c 85.18 -#define LUA_LIB 85.19 - 85.20 -#include "lua.h" 85.21 - 85.22 -#include "lauxlib.h" 85.23 -#include "lualib.h" 85.24 - 85.25 - 85.26 -static int os_pushresult (lua_State *L, int i, const char *filename) { 85.27 - int en = errno; /* calls to Lua API may change this value */ 85.28 - if (i) { 85.29 - lua_pushboolean(L, 1); 85.30 - return 1; 85.31 - } 85.32 - else { 85.33 - lua_pushnil(L); 85.34 - lua_pushfstring(L, "%s: %s", filename, strerror(en)); 85.35 - lua_pushinteger(L, en); 85.36 - return 3; 85.37 - } 85.38 -} 85.39 - 85.40 - 85.41 -static int os_execute (lua_State *L) { 85.42 - lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); 85.43 - return 1; 85.44 -} 85.45 - 85.46 - 85.47 -static int os_remove (lua_State *L) { 85.48 - const char *filename = luaL_checkstring(L, 1); 85.49 - return os_pushresult(L, remove(filename) == 0, filename); 85.50 -} 85.51 - 85.52 - 85.53 -static int os_rename (lua_State *L) { 85.54 - const char *fromname = luaL_checkstring(L, 1); 85.55 - const char *toname = luaL_checkstring(L, 2); 85.56 - return os_pushresult(L, rename(fromname, toname) == 0, fromname); 85.57 -} 85.58 - 85.59 - 85.60 -static int os_tmpname (lua_State *L) { 85.61 - char buff[LUA_TMPNAMBUFSIZE]; 85.62 - int err; 85.63 - lua_tmpnam(buff, err); 85.64 - if (err) 85.65 - return luaL_error(L, "unable to generate a unique filename"); 85.66 - lua_pushstring(L, buff); 85.67 - return 1; 85.68 -} 85.69 - 85.70 - 85.71 -static int os_getenv (lua_State *L) { 85.72 - lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ 85.73 - return 1; 85.74 -} 85.75 - 85.76 - 85.77 -static int os_clock (lua_State *L) { 85.78 - lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); 85.79 - return 1; 85.80 -} 85.81 - 85.82 - 85.83 -/* 85.84 -** {====================================================== 85.85 -** Time/Date operations 85.86 -** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, 85.87 -** wday=%w+1, yday=%j, isdst=? } 85.88 -** ======================================================= 85.89 -*/ 85.90 - 85.91 -static void setfield (lua_State *L, const char *key, int value) { 85.92 - lua_pushinteger(L, value); 85.93 - lua_setfield(L, -2, key); 85.94 -} 85.95 - 85.96 -static void setboolfield (lua_State *L, const char *key, int value) { 85.97 - if (value < 0) /* undefined? */ 85.98 - return; /* does not set field */ 85.99 - lua_pushboolean(L, value); 85.100 - lua_setfield(L, -2, key); 85.101 -} 85.102 - 85.103 -static int getboolfield (lua_State *L, const char *key) { 85.104 - int res; 85.105 - lua_getfield(L, -1, key); 85.106 - res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); 85.107 - lua_pop(L, 1); 85.108 - return res; 85.109 -} 85.110 - 85.111 - 85.112 -static int getfield (lua_State *L, const char *key, int d) { 85.113 - int res; 85.114 - lua_getfield(L, -1, key); 85.115 - if (lua_isnumber(L, -1)) 85.116 - res = (int)lua_tointeger(L, -1); 85.117 - else { 85.118 - if (d < 0) 85.119 - return luaL_error(L, "field " LUA_QS " missing in date table", key); 85.120 - res = d; 85.121 - } 85.122 - lua_pop(L, 1); 85.123 - return res; 85.124 -} 85.125 - 85.126 - 85.127 -static int os_date (lua_State *L) { 85.128 - const char *s = luaL_optstring(L, 1, "%c"); 85.129 - time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); 85.130 - struct tm *stm; 85.131 - if (*s == '!') { /* UTC? */ 85.132 - stm = gmtime(&t); 85.133 - s++; /* skip `!' */ 85.134 - } 85.135 - else 85.136 - stm = localtime(&t); 85.137 - if (stm == NULL) /* invalid date? */ 85.138 - lua_pushnil(L); 85.139 - else if (strcmp(s, "*t") == 0) { 85.140 - lua_createtable(L, 0, 9); /* 9 = number of fields */ 85.141 - setfield(L, "sec", stm->tm_sec); 85.142 - setfield(L, "min", stm->tm_min); 85.143 - setfield(L, "hour", stm->tm_hour); 85.144 - setfield(L, "day", stm->tm_mday); 85.145 - setfield(L, "month", stm->tm_mon+1); 85.146 - setfield(L, "year", stm->tm_year+1900); 85.147 - setfield(L, "wday", stm->tm_wday+1); 85.148 - setfield(L, "yday", stm->tm_yday+1); 85.149 - setboolfield(L, "isdst", stm->tm_isdst); 85.150 - } 85.151 - else { 85.152 - char cc[3]; 85.153 - luaL_Buffer b; 85.154 - cc[0] = '%'; cc[2] = '\0'; 85.155 - luaL_buffinit(L, &b); 85.156 - for (; *s; s++) { 85.157 - if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ 85.158 - luaL_addchar(&b, *s); 85.159 - else { 85.160 - size_t reslen; 85.161 - char buff[200]; /* should be big enough for any conversion result */ 85.162 - cc[1] = *(++s); 85.163 - reslen = strftime(buff, sizeof(buff), cc, stm); 85.164 - luaL_addlstring(&b, buff, reslen); 85.165 - } 85.166 - } 85.167 - luaL_pushresult(&b); 85.168 - } 85.169 - return 1; 85.170 -} 85.171 - 85.172 - 85.173 -static int os_time (lua_State *L) { 85.174 - time_t t; 85.175 - if (lua_isnoneornil(L, 1)) /* called without args? */ 85.176 - t = time(NULL); /* get current time */ 85.177 - else { 85.178 - struct tm ts; 85.179 - luaL_checktype(L, 1, LUA_TTABLE); 85.180 - lua_settop(L, 1); /* make sure table is at the top */ 85.181 - ts.tm_sec = getfield(L, "sec", 0); 85.182 - ts.tm_min = getfield(L, "min", 0); 85.183 - ts.tm_hour = getfield(L, "hour", 12); 85.184 - ts.tm_mday = getfield(L, "day", -1); 85.185 - ts.tm_mon = getfield(L, "month", -1) - 1; 85.186 - ts.tm_year = getfield(L, "year", -1) - 1900; 85.187 - ts.tm_isdst = getboolfield(L, "isdst"); 85.188 - t = mktime(&ts); 85.189 - } 85.190 - if (t == (time_t)(-1)) 85.191 - lua_pushnil(L); 85.192 - else 85.193 - lua_pushnumber(L, (lua_Number)t); 85.194 - return 1; 85.195 -} 85.196 - 85.197 - 85.198 -static int os_difftime (lua_State *L) { 85.199 - lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), 85.200 - (time_t)(luaL_optnumber(L, 2, 0)))); 85.201 - return 1; 85.202 -} 85.203 - 85.204 -/* }====================================================== */ 85.205 - 85.206 - 85.207 -static int os_setlocale (lua_State *L) { 85.208 - static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, 85.209 - LC_NUMERIC, LC_TIME}; 85.210 - static const char *const catnames[] = {"all", "collate", "ctype", "monetary", 85.211 - "numeric", "time", NULL}; 85.212 - const char *l = luaL_optstring(L, 1, NULL); 85.213 - int op = luaL_checkoption(L, 2, "all", catnames); 85.214 - lua_pushstring(L, setlocale(cat[op], l)); 85.215 - return 1; 85.216 -} 85.217 - 85.218 - 85.219 -static int os_exit (lua_State *L) { 85.220 - exit(luaL_optint(L, 1, EXIT_SUCCESS)); 85.221 -} 85.222 - 85.223 -static const luaL_Reg syslib[] = { 85.224 - {"clock", os_clock}, 85.225 - {"date", os_date}, 85.226 - {"difftime", os_difftime}, 85.227 - {"execute", os_execute}, 85.228 - {"exit", os_exit}, 85.229 - {"getenv", os_getenv}, 85.230 - {"remove", os_remove}, 85.231 - {"rename", os_rename}, 85.232 - {"setlocale", os_setlocale}, 85.233 - {"time", os_time}, 85.234 - {"tmpname", os_tmpname}, 85.235 - {NULL, NULL} 85.236 -}; 85.237 - 85.238 -/* }====================================================== */ 85.239 - 85.240 - 85.241 - 85.242 -LUALIB_API int luaopen_os (lua_State *L) { 85.243 - luaL_register(L, LUA_OSLIBNAME, syslib); 85.244 - return 1; 85.245 -} 85.246 -
86.1 --- a/src/lua/src/lparser.c Sat Mar 03 11:04:56 2012 -0600 86.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 86.3 @@ -1,1339 +0,0 @@ 86.4 -/* 86.5 -** $Id: lparser.c,v 2.42.1.3 2007/12/28 15:32:23 roberto Exp $ 86.6 -** Lua Parser 86.7 -** See Copyright Notice in lua.h 86.8 -*/ 86.9 - 86.10 - 86.11 -#include <string.h> 86.12 - 86.13 -#define lparser_c 86.14 -#define LUA_CORE 86.15 - 86.16 -#include "lua.h" 86.17 - 86.18 -#include "lcode.h" 86.19 -#include "ldebug.h" 86.20 -#include "ldo.h" 86.21 -#include "lfunc.h" 86.22 -#include "llex.h" 86.23 -#include "lmem.h" 86.24 -#include "lobject.h" 86.25 -#include "lopcodes.h" 86.26 -#include "lparser.h" 86.27 -#include "lstate.h" 86.28 -#include "lstring.h" 86.29 -#include "ltable.h" 86.30 - 86.31 - 86.32 - 86.33 -#define hasmultret(k) ((k) == VCALL || (k) == VVARARG) 86.34 - 86.35 -#define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]]) 86.36 - 86.37 -#define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m) 86.38 - 86.39 - 86.40 -/* 86.41 -** nodes for block list (list of active blocks) 86.42 -*/ 86.43 -typedef struct BlockCnt { 86.44 - struct BlockCnt *previous; /* chain */ 86.45 - int breaklist; /* list of jumps out of this loop */ 86.46 - lu_byte nactvar; /* # active locals outside the breakable structure */ 86.47 - lu_byte upval; /* true if some variable in the block is an upvalue */ 86.48 - lu_byte isbreakable; /* true if `block' is a loop */ 86.49 -} BlockCnt; 86.50 - 86.51 - 86.52 - 86.53 -/* 86.54 -** prototypes for recursive non-terminal functions 86.55 -*/ 86.56 -static void chunk (LexState *ls); 86.57 -static void expr (LexState *ls, expdesc *v); 86.58 - 86.59 - 86.60 -static void anchor_token (LexState *ls) { 86.61 - if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { 86.62 - TString *ts = ls->t.seminfo.ts; 86.63 - luaX_newstring(ls, getstr(ts), ts->tsv.len); 86.64 - } 86.65 -} 86.66 - 86.67 - 86.68 -static void error_expected (LexState *ls, int token) { 86.69 - luaX_syntaxerror(ls, 86.70 - luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token))); 86.71 -} 86.72 - 86.73 - 86.74 -static void errorlimit (FuncState *fs, int limit, const char *what) { 86.75 - const char *msg = (fs->f->linedefined == 0) ? 86.76 - luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) : 86.77 - luaO_pushfstring(fs->L, "function at line %d has more than %d %s", 86.78 - fs->f->linedefined, limit, what); 86.79 - luaX_lexerror(fs->ls, msg, 0); 86.80 -} 86.81 - 86.82 - 86.83 -static int testnext (LexState *ls, int c) { 86.84 - if (ls->t.token == c) { 86.85 - luaX_next(ls); 86.86 - return 1; 86.87 - } 86.88 - else return 0; 86.89 -} 86.90 - 86.91 - 86.92 -static void check (LexState *ls, int c) { 86.93 - if (ls->t.token != c) 86.94 - error_expected(ls, c); 86.95 -} 86.96 - 86.97 -static void checknext (LexState *ls, int c) { 86.98 - check(ls, c); 86.99 - luaX_next(ls); 86.100 -} 86.101 - 86.102 - 86.103 -#define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } 86.104 - 86.105 - 86.106 - 86.107 -static void check_match (LexState *ls, int what, int who, int where) { 86.108 - if (!testnext(ls, what)) { 86.109 - if (where == ls->linenumber) 86.110 - error_expected(ls, what); 86.111 - else { 86.112 - luaX_syntaxerror(ls, luaO_pushfstring(ls->L, 86.113 - LUA_QS " expected (to close " LUA_QS " at line %d)", 86.114 - luaX_token2str(ls, what), luaX_token2str(ls, who), where)); 86.115 - } 86.116 - } 86.117 -} 86.118 - 86.119 - 86.120 -static TString *str_checkname (LexState *ls) { 86.121 - TString *ts; 86.122 - check(ls, TK_NAME); 86.123 - ts = ls->t.seminfo.ts; 86.124 - luaX_next(ls); 86.125 - return ts; 86.126 -} 86.127 - 86.128 - 86.129 -static void init_exp (expdesc *e, expkind k, int i) { 86.130 - e->f = e->t = NO_JUMP; 86.131 - e->k = k; 86.132 - e->u.s.info = i; 86.133 -} 86.134 - 86.135 - 86.136 -static void codestring (LexState *ls, expdesc *e, TString *s) { 86.137 - init_exp(e, VK, luaK_stringK(ls->fs, s)); 86.138 -} 86.139 - 86.140 - 86.141 -static void checkname(LexState *ls, expdesc *e) { 86.142 - codestring(ls, e, str_checkname(ls)); 86.143 -} 86.144 - 86.145 - 86.146 -static int registerlocalvar (LexState *ls, TString *varname) { 86.147 - FuncState *fs = ls->fs; 86.148 - Proto *f = fs->f; 86.149 - int oldsize = f->sizelocvars; 86.150 - luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, 86.151 - LocVar, SHRT_MAX, "too many local variables"); 86.152 - while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; 86.153 - f->locvars[fs->nlocvars].varname = varname; 86.154 - luaC_objbarrier(ls->L, f, varname); 86.155 - return fs->nlocvars++; 86.156 -} 86.157 - 86.158 - 86.159 -#define new_localvarliteral(ls,v,n) \ 86.160 - new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n) 86.161 - 86.162 - 86.163 -static void new_localvar (LexState *ls, TString *name, int n) { 86.164 - FuncState *fs = ls->fs; 86.165 - luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables"); 86.166 - fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name)); 86.167 -} 86.168 - 86.169 - 86.170 -static void adjustlocalvars (LexState *ls, int nvars) { 86.171 - FuncState *fs = ls->fs; 86.172 - fs->nactvar = cast_byte(fs->nactvar + nvars); 86.173 - for (; nvars; nvars--) { 86.174 - getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc; 86.175 - } 86.176 -} 86.177 - 86.178 - 86.179 -static void removevars (LexState *ls, int tolevel) { 86.180 - FuncState *fs = ls->fs; 86.181 - while (fs->nactvar > tolevel) 86.182 - getlocvar(fs, --fs->nactvar).endpc = fs->pc; 86.183 -} 86.184 - 86.185 - 86.186 -static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { 86.187 - int i; 86.188 - Proto *f = fs->f; 86.189 - int oldsize = f->sizeupvalues; 86.190 - for (i=0; i<f->nups; i++) { 86.191 - if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) { 86.192 - lua_assert(f->upvalues[i] == name); 86.193 - return i; 86.194 - } 86.195 - } 86.196 - /* new one */ 86.197 - luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues"); 86.198 - luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues, 86.199 - TString *, MAX_INT, ""); 86.200 - while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL; 86.201 - f->upvalues[f->nups] = name; 86.202 - luaC_objbarrier(fs->L, f, name); 86.203 - lua_assert(v->k == VLOCAL || v->k == VUPVAL); 86.204 - fs->upvalues[f->nups].k = cast_byte(v->k); 86.205 - fs->upvalues[f->nups].info = cast_byte(v->u.s.info); 86.206 - return f->nups++; 86.207 -} 86.208 - 86.209 - 86.210 -static int searchvar (FuncState *fs, TString *n) { 86.211 - int i; 86.212 - for (i=fs->nactvar-1; i >= 0; i--) { 86.213 - if (n == getlocvar(fs, i).varname) 86.214 - return i; 86.215 - } 86.216 - return -1; /* not found */ 86.217 -} 86.218 - 86.219 - 86.220 -static void markupval (FuncState *fs, int level) { 86.221 - BlockCnt *bl = fs->bl; 86.222 - while (bl && bl->nactvar > level) bl = bl->previous; 86.223 - if (bl) bl->upval = 1; 86.224 -} 86.225 - 86.226 - 86.227 -static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { 86.228 - if (fs == NULL) { /* no more levels? */ 86.229 - init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ 86.230 - return VGLOBAL; 86.231 - } 86.232 - else { 86.233 - int v = searchvar(fs, n); /* look up at current level */ 86.234 - if (v >= 0) { 86.235 - init_exp(var, VLOCAL, v); 86.236 - if (!base) 86.237 - markupval(fs, v); /* local will be used as an upval */ 86.238 - return VLOCAL; 86.239 - } 86.240 - else { /* not found at current level; try upper one */ 86.241 - if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL) 86.242 - return VGLOBAL; 86.243 - var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */ 86.244 - var->k = VUPVAL; /* upvalue in this level */ 86.245 - return VUPVAL; 86.246 - } 86.247 - } 86.248 -} 86.249 - 86.250 - 86.251 -static void singlevar (LexState *ls, expdesc *var) { 86.252 - TString *varname = str_checkname(ls); 86.253 - FuncState *fs = ls->fs; 86.254 - if (singlevaraux(fs, varname, var, 1) == VGLOBAL) 86.255 - var->u.s.info = luaK_stringK(fs, varname); /* info points to global name */ 86.256 -} 86.257 - 86.258 - 86.259 -static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { 86.260 - FuncState *fs = ls->fs; 86.261 - int extra = nvars - nexps; 86.262 - if (hasmultret(e->k)) { 86.263 - extra++; /* includes call itself */ 86.264 - if (extra < 0) extra = 0; 86.265 - luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ 86.266 - if (extra > 1) luaK_reserveregs(fs, extra-1); 86.267 - } 86.268 - else { 86.269 - if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ 86.270 - if (extra > 0) { 86.271 - int reg = fs->freereg; 86.272 - luaK_reserveregs(fs, extra); 86.273 - luaK_nil(fs, reg, extra); 86.274 - } 86.275 - } 86.276 -} 86.277 - 86.278 - 86.279 -static void enterlevel (LexState *ls) { 86.280 - if (++ls->L->nCcalls > LUAI_MAXCCALLS) 86.281 - luaX_lexerror(ls, "chunk has too many syntax levels", 0); 86.282 -} 86.283 - 86.284 - 86.285 -#define leavelevel(ls) ((ls)->L->nCcalls--) 86.286 - 86.287 - 86.288 -static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) { 86.289 - bl->breaklist = NO_JUMP; 86.290 - bl->isbreakable = isbreakable; 86.291 - bl->nactvar = fs->nactvar; 86.292 - bl->upval = 0; 86.293 - bl->previous = fs->bl; 86.294 - fs->bl = bl; 86.295 - lua_assert(fs->freereg == fs->nactvar); 86.296 -} 86.297 - 86.298 - 86.299 -static void leaveblock (FuncState *fs) { 86.300 - BlockCnt *bl = fs->bl; 86.301 - fs->bl = bl->previous; 86.302 - removevars(fs->ls, bl->nactvar); 86.303 - if (bl->upval) 86.304 - luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); 86.305 - /* a block either controls scope or breaks (never both) */ 86.306 - lua_assert(!bl->isbreakable || !bl->upval); 86.307 - lua_assert(bl->nactvar == fs->nactvar); 86.308 - fs->freereg = fs->nactvar; /* free registers */ 86.309 - luaK_patchtohere(fs, bl->breaklist); 86.310 -} 86.311 - 86.312 - 86.313 -static void pushclosure (LexState *ls, FuncState *func, expdesc *v) { 86.314 - FuncState *fs = ls->fs; 86.315 - Proto *f = fs->f; 86.316 - int oldsize = f->sizep; 86.317 - int i; 86.318 - luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *, 86.319 - MAXARG_Bx, "constant table overflow"); 86.320 - while (oldsize < f->sizep) f->p[oldsize++] = NULL; 86.321 - f->p[fs->np++] = func->f; 86.322 - luaC_objbarrier(ls->L, f, func->f); 86.323 - init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1)); 86.324 - for (i=0; i<func->f->nups; i++) { 86.325 - OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; 86.326 - luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0); 86.327 - } 86.328 -} 86.329 - 86.330 - 86.331 -static void open_func (LexState *ls, FuncState *fs) { 86.332 - lua_State *L = ls->L; 86.333 - Proto *f = luaF_newproto(L); 86.334 - fs->f = f; 86.335 - fs->prev = ls->fs; /* linked list of funcstates */ 86.336 - fs->ls = ls; 86.337 - fs->L = L; 86.338 - ls->fs = fs; 86.339 - fs->pc = 0; 86.340 - fs->lasttarget = -1; 86.341 - fs->jpc = NO_JUMP; 86.342 - fs->freereg = 0; 86.343 - fs->nk = 0; 86.344 - fs->np = 0; 86.345 - fs->nlocvars = 0; 86.346 - fs->nactvar = 0; 86.347 - fs->bl = NULL; 86.348 - f->source = ls->source; 86.349 - f->maxstacksize = 2; /* registers 0/1 are always valid */ 86.350 - fs->h = luaH_new(L, 0, 0); 86.351 - /* anchor table of constants and prototype (to avoid being collected) */ 86.352 - sethvalue2s(L, L->top, fs->h); 86.353 - incr_top(L); 86.354 - setptvalue2s(L, L->top, f); 86.355 - incr_top(L); 86.356 -} 86.357 - 86.358 - 86.359 -static void close_func (LexState *ls) { 86.360 - lua_State *L = ls->L; 86.361 - FuncState *fs = ls->fs; 86.362 - Proto *f = fs->f; 86.363 - removevars(ls, 0); 86.364 - luaK_ret(fs, 0, 0); /* final return */ 86.365 - luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); 86.366 - f->sizecode = fs->pc; 86.367 - luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); 86.368 - f->sizelineinfo = fs->pc; 86.369 - luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); 86.370 - f->sizek = fs->nk; 86.371 - luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); 86.372 - f->sizep = fs->np; 86.373 - luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); 86.374 - f->sizelocvars = fs->nlocvars; 86.375 - luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *); 86.376 - f->sizeupvalues = f->nups; 86.377 - lua_assert(luaG_checkcode(f)); 86.378 - lua_assert(fs->bl == NULL); 86.379 - ls->fs = fs->prev; 86.380 - L->top -= 2; /* remove table and prototype from the stack */ 86.381 - /* last token read was anchored in defunct function; must reanchor it */ 86.382 - if (fs) anchor_token(ls); 86.383 -} 86.384 - 86.385 - 86.386 -Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { 86.387 - struct LexState lexstate; 86.388 - struct FuncState funcstate; 86.389 - lexstate.buff = buff; 86.390 - luaX_setinput(L, &lexstate, z, luaS_new(L, name)); 86.391 - open_func(&lexstate, &funcstate); 86.392 - funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ 86.393 - luaX_next(&lexstate); /* read first token */ 86.394 - chunk(&lexstate); 86.395 - check(&lexstate, TK_EOS); 86.396 - close_func(&lexstate); 86.397 - lua_assert(funcstate.prev == NULL); 86.398 - lua_assert(funcstate.f->nups == 0); 86.399 - lua_assert(lexstate.fs == NULL); 86.400 - return funcstate.f; 86.401 -} 86.402 - 86.403 - 86.404 - 86.405 -/*============================================================*/ 86.406 -/* GRAMMAR RULES */ 86.407 -/*============================================================*/ 86.408 - 86.409 - 86.410 -static void field (LexState *ls, expdesc *v) { 86.411 - /* field -> ['.' | ':'] NAME */ 86.412 - FuncState *fs = ls->fs; 86.413 - expdesc key; 86.414 - luaK_exp2anyreg(fs, v); 86.415 - luaX_next(ls); /* skip the dot or colon */ 86.416 - checkname(ls, &key); 86.417 - luaK_indexed(fs, v, &key); 86.418 -} 86.419 - 86.420 - 86.421 -static void yindex (LexState *ls, expdesc *v) { 86.422 - /* index -> '[' expr ']' */ 86.423 - luaX_next(ls); /* skip the '[' */ 86.424 - expr(ls, v); 86.425 - luaK_exp2val(ls->fs, v); 86.426 - checknext(ls, ']'); 86.427 -} 86.428 - 86.429 - 86.430 -/* 86.431 -** {====================================================================== 86.432 -** Rules for Constructors 86.433 -** ======================================================================= 86.434 -*/ 86.435 - 86.436 - 86.437 -struct ConsControl { 86.438 - expdesc v; /* last list item read */ 86.439 - expdesc *t; /* table descriptor */ 86.440 - int nh; /* total number of `record' elements */ 86.441 - int na; /* total number of array elements */ 86.442 - int tostore; /* number of array elements pending to be stored */ 86.443 -}; 86.444 - 86.445 - 86.446 -static void recfield (LexState *ls, struct ConsControl *cc) { 86.447 - /* recfield -> (NAME | `['exp1`]') = exp1 */ 86.448 - FuncState *fs = ls->fs; 86.449 - int reg = ls->fs->freereg; 86.450 - expdesc key, val; 86.451 - int rkkey; 86.452 - if (ls->t.token == TK_NAME) { 86.453 - luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); 86.454 - checkname(ls, &key); 86.455 - } 86.456 - else /* ls->t.token == '[' */ 86.457 - yindex(ls, &key); 86.458 - cc->nh++; 86.459 - checknext(ls, '='); 86.460 - rkkey = luaK_exp2RK(fs, &key); 86.461 - expr(ls, &val); 86.462 - luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val)); 86.463 - fs->freereg = reg; /* free registers */ 86.464 -} 86.465 - 86.466 - 86.467 -static void closelistfield (FuncState *fs, struct ConsControl *cc) { 86.468 - if (cc->v.k == VVOID) return; /* there is no list item */ 86.469 - luaK_exp2nextreg(fs, &cc->v); 86.470 - cc->v.k = VVOID; 86.471 - if (cc->tostore == LFIELDS_PER_FLUSH) { 86.472 - luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); /* flush */ 86.473 - cc->tostore = 0; /* no more items pending */ 86.474 - } 86.475 -} 86.476 - 86.477 - 86.478 -static void lastlistfield (FuncState *fs, struct ConsControl *cc) { 86.479 - if (cc->tostore == 0) return; 86.480 - if (hasmultret(cc->v.k)) { 86.481 - luaK_setmultret(fs, &cc->v); 86.482 - luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET); 86.483 - cc->na--; /* do not count last expression (unknown number of elements) */ 86.484 - } 86.485 - else { 86.486 - if (cc->v.k != VVOID) 86.487 - luaK_exp2nextreg(fs, &cc->v); 86.488 - luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); 86.489 - } 86.490 -} 86.491 - 86.492 - 86.493 -static void listfield (LexState *ls, struct ConsControl *cc) { 86.494 - expr(ls, &cc->v); 86.495 - luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); 86.496 - cc->na++; 86.497 - cc->tostore++; 86.498 -} 86.499 - 86.500 - 86.501 -static void constructor (LexState *ls, expdesc *t) { 86.502 - /* constructor -> ?? */ 86.503 - FuncState *fs = ls->fs; 86.504 - int line = ls->linenumber; 86.505 - int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); 86.506 - struct ConsControl cc; 86.507 - cc.na = cc.nh = cc.tostore = 0; 86.508 - cc.t = t; 86.509 - init_exp(t, VRELOCABLE, pc); 86.510 - init_exp(&cc.v, VVOID, 0); /* no value (yet) */ 86.511 - luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ 86.512 - checknext(ls, '{'); 86.513 - do { 86.514 - lua_assert(cc.v.k == VVOID || cc.tostore > 0); 86.515 - if (ls->t.token == '}') break; 86.516 - closelistfield(fs, &cc); 86.517 - switch(ls->t.token) { 86.518 - case TK_NAME: { /* may be listfields or recfields */ 86.519 - luaX_lookahead(ls); 86.520 - if (ls->lookahead.token != '=') /* expression? */ 86.521 - listfield(ls, &cc); 86.522 - else 86.523 - recfield(ls, &cc); 86.524 - break; 86.525 - } 86.526 - case '[': { /* constructor_item -> recfield */ 86.527 - recfield(ls, &cc); 86.528 - break; 86.529 - } 86.530 - default: { /* constructor_part -> listfield */ 86.531 - listfield(ls, &cc); 86.532 - break; 86.533 - } 86.534 - } 86.535 - } while (testnext(ls, ',') || testnext(ls, ';')); 86.536 - check_match(ls, '}', '{', line); 86.537 - lastlistfield(fs, &cc); 86.538 - SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ 86.539 - SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ 86.540 -} 86.541 - 86.542 -/* }====================================================================== */ 86.543 - 86.544 - 86.545 - 86.546 -static void parlist (LexState *ls) { 86.547 - /* parlist -> [ param { `,' param } ] */ 86.548 - FuncState *fs = ls->fs; 86.549 - Proto *f = fs->f; 86.550 - int nparams = 0; 86.551 - f->is_vararg = 0; 86.552 - if (ls->t.token != ')') { /* is `parlist' not empty? */ 86.553 - do { 86.554 - switch (ls->t.token) { 86.555 - case TK_NAME: { /* param -> NAME */ 86.556 - new_localvar(ls, str_checkname(ls), nparams++); 86.557 - break; 86.558 - } 86.559 - case TK_DOTS: { /* param -> `...' */ 86.560 - luaX_next(ls); 86.561 -#if defined(LUA_COMPAT_VARARG) 86.562 - /* use `arg' as default name */ 86.563 - new_localvarliteral(ls, "arg", nparams++); 86.564 - f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG; 86.565 -#endif 86.566 - f->is_vararg |= VARARG_ISVARARG; 86.567 - break; 86.568 - } 86.569 - default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected"); 86.570 - } 86.571 - } while (!f->is_vararg && testnext(ls, ',')); 86.572 - } 86.573 - adjustlocalvars(ls, nparams); 86.574 - f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG)); 86.575 - luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ 86.576 -} 86.577 - 86.578 - 86.579 -static void body (LexState *ls, expdesc *e, int needself, int line) { 86.580 - /* body -> `(' parlist `)' chunk END */ 86.581 - FuncState new_fs; 86.582 - open_func(ls, &new_fs); 86.583 - new_fs.f->linedefined = line; 86.584 - checknext(ls, '('); 86.585 - if (needself) { 86.586 - new_localvarliteral(ls, "self", 0); 86.587 - adjustlocalvars(ls, 1); 86.588 - } 86.589 - parlist(ls); 86.590 - checknext(ls, ')'); 86.591 - chunk(ls); 86.592 - new_fs.f->lastlinedefined = ls->linenumber; 86.593 - check_match(ls, TK_END, TK_FUNCTION, line); 86.594 - close_func(ls); 86.595 - pushclosure(ls, &new_fs, e); 86.596 -} 86.597 - 86.598 - 86.599 -static int explist1 (LexState *ls, expdesc *v) { 86.600 - /* explist1 -> expr { `,' expr } */ 86.601 - int n = 1; /* at least one expression */ 86.602 - expr(ls, v); 86.603 - while (testnext(ls, ',')) { 86.604 - luaK_exp2nextreg(ls->fs, v); 86.605 - expr(ls, v); 86.606 - n++; 86.607 - } 86.608 - return n; 86.609 -} 86.610 - 86.611 - 86.612 -static void funcargs (LexState *ls, expdesc *f) { 86.613 - FuncState *fs = ls->fs; 86.614 - expdesc args; 86.615 - int base, nparams; 86.616 - int line = ls->linenumber; 86.617 - switch (ls->t.token) { 86.618 - case '(': { /* funcargs -> `(' [ explist1 ] `)' */ 86.619 - if (line != ls->lastline) 86.620 - luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); 86.621 - luaX_next(ls); 86.622 - if (ls->t.token == ')') /* arg list is empty? */ 86.623 - args.k = VVOID; 86.624 - else { 86.625 - explist1(ls, &args); 86.626 - luaK_setmultret(fs, &args); 86.627 - } 86.628 - check_match(ls, ')', '(', line); 86.629 - break; 86.630 - } 86.631 - case '{': { /* funcargs -> constructor */ 86.632 - constructor(ls, &args); 86.633 - break; 86.634 - } 86.635 - case TK_STRING: { /* funcargs -> STRING */ 86.636 - codestring(ls, &args, ls->t.seminfo.ts); 86.637 - luaX_next(ls); /* must use `seminfo' before `next' */ 86.638 - break; 86.639 - } 86.640 - default: { 86.641 - luaX_syntaxerror(ls, "function arguments expected"); 86.642 - return; 86.643 - } 86.644 - } 86.645 - lua_assert(f->k == VNONRELOC); 86.646 - base = f->u.s.info; /* base register for call */ 86.647 - if (hasmultret(args.k)) 86.648 - nparams = LUA_MULTRET; /* open call */ 86.649 - else { 86.650 - if (args.k != VVOID) 86.651 - luaK_exp2nextreg(fs, &args); /* close last argument */ 86.652 - nparams = fs->freereg - (base+1); 86.653 - } 86.654 - init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); 86.655 - luaK_fixline(fs, line); 86.656 - fs->freereg = base+1; /* call remove function and arguments and leaves 86.657 - (unless changed) one result */ 86.658 -} 86.659 - 86.660 - 86.661 - 86.662 - 86.663 -/* 86.664 -** {====================================================================== 86.665 -** Expression parsing 86.666 -** ======================================================================= 86.667 -*/ 86.668 - 86.669 - 86.670 -static void prefixexp (LexState *ls, expdesc *v) { 86.671 - /* prefixexp -> NAME | '(' expr ')' */ 86.672 - switch (ls->t.token) { 86.673 - case '(': { 86.674 - int line = ls->linenumber; 86.675 - luaX_next(ls); 86.676 - expr(ls, v); 86.677 - check_match(ls, ')', '(', line); 86.678 - luaK_dischargevars(ls->fs, v); 86.679 - return; 86.680 - } 86.681 - case TK_NAME: { 86.682 - singlevar(ls, v); 86.683 - return; 86.684 - } 86.685 - default: { 86.686 - luaX_syntaxerror(ls, "unexpected symbol"); 86.687 - return; 86.688 - } 86.689 - } 86.690 -} 86.691 - 86.692 - 86.693 -static void primaryexp (LexState *ls, expdesc *v) { 86.694 - /* primaryexp -> 86.695 - prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */ 86.696 - FuncState *fs = ls->fs; 86.697 - prefixexp(ls, v); 86.698 - for (;;) { 86.699 - switch (ls->t.token) { 86.700 - case '.': { /* field */ 86.701 - field(ls, v); 86.702 - break; 86.703 - } 86.704 - case '[': { /* `[' exp1 `]' */ 86.705 - expdesc key; 86.706 - luaK_exp2anyreg(fs, v); 86.707 - yindex(ls, &key); 86.708 - luaK_indexed(fs, v, &key); 86.709 - break; 86.710 - } 86.711 - case ':': { /* `:' NAME funcargs */ 86.712 - expdesc key; 86.713 - luaX_next(ls); 86.714 - checkname(ls, &key); 86.715 - luaK_self(fs, v, &key); 86.716 - funcargs(ls, v); 86.717 - break; 86.718 - } 86.719 - case '(': case TK_STRING: case '{': { /* funcargs */ 86.720 - luaK_exp2nextreg(fs, v); 86.721 - funcargs(ls, v); 86.722 - break; 86.723 - } 86.724 - default: return; 86.725 - } 86.726 - } 86.727 -} 86.728 - 86.729 - 86.730 -static void simpleexp (LexState *ls, expdesc *v) { 86.731 - /* simpleexp -> NUMBER | STRING | NIL | true | false | ... | 86.732 - constructor | FUNCTION body | primaryexp */ 86.733 - switch (ls->t.token) { 86.734 - case TK_NUMBER: { 86.735 - init_exp(v, VKNUM, 0); 86.736 - v->u.nval = ls->t.seminfo.r; 86.737 - break; 86.738 - } 86.739 - case TK_STRING: { 86.740 - codestring(ls, v, ls->t.seminfo.ts); 86.741 - break; 86.742 - } 86.743 - case TK_NIL: { 86.744 - init_exp(v, VNIL, 0); 86.745 - break; 86.746 - } 86.747 - case TK_TRUE: { 86.748 - init_exp(v, VTRUE, 0); 86.749 - break; 86.750 - } 86.751 - case TK_FALSE: { 86.752 - init_exp(v, VFALSE, 0); 86.753 - break; 86.754 - } 86.755 - case TK_DOTS: { /* vararg */ 86.756 - FuncState *fs = ls->fs; 86.757 - check_condition(ls, fs->f->is_vararg, 86.758 - "cannot use " LUA_QL("...") " outside a vararg function"); 86.759 - fs->f->is_vararg &= ~VARARG_NEEDSARG; /* don't need 'arg' */ 86.760 - init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); 86.761 - break; 86.762 - } 86.763 - case '{': { /* constructor */ 86.764 - constructor(ls, v); 86.765 - return; 86.766 - } 86.767 - case TK_FUNCTION: { 86.768 - luaX_next(ls); 86.769 - body(ls, v, 0, ls->linenumber); 86.770 - return; 86.771 - } 86.772 - default: { 86.773 - primaryexp(ls, v); 86.774 - return; 86.775 - } 86.776 - } 86.777 - luaX_next(ls); 86.778 -} 86.779 - 86.780 - 86.781 -static UnOpr getunopr (int op) { 86.782 - switch (op) { 86.783 - case TK_NOT: return OPR_NOT; 86.784 - case '-': return OPR_MINUS; 86.785 - case '#': return OPR_LEN; 86.786 - default: return OPR_NOUNOPR; 86.787 - } 86.788 -} 86.789 - 86.790 - 86.791 -static BinOpr getbinopr (int op) { 86.792 - switch (op) { 86.793 - case '+': return OPR_ADD; 86.794 - case '-': return OPR_SUB; 86.795 - case '*': return OPR_MUL; 86.796 - case '/': return OPR_DIV; 86.797 - case '%': return OPR_MOD; 86.798 - case '^': return OPR_POW; 86.799 - case TK_CONCAT: return OPR_CONCAT; 86.800 - case TK_NE: return OPR_NE; 86.801 - case TK_EQ: return OPR_EQ; 86.802 - case '<': return OPR_LT; 86.803 - case TK_LE: return OPR_LE; 86.804 - case '>': return OPR_GT; 86.805 - case TK_GE: return OPR_GE; 86.806 - case TK_AND: return OPR_AND; 86.807 - case TK_OR: return OPR_OR; 86.808 - default: return OPR_NOBINOPR; 86.809 - } 86.810 -} 86.811 - 86.812 - 86.813 -static const struct { 86.814 - lu_byte left; /* left priority for each binary operator */ 86.815 - lu_byte right; /* right priority */ 86.816 -} priority[] = { /* ORDER OPR */ 86.817 - {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */ 86.818 - {10, 9}, {5, 4}, /* power and concat (right associative) */ 86.819 - {3, 3}, {3, 3}, /* equality and inequality */ 86.820 - {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */ 86.821 - {2, 2}, {1, 1} /* logical (and/or) */ 86.822 -}; 86.823 - 86.824 -#define UNARY_PRIORITY 8 /* priority for unary operators */ 86.825 - 86.826 - 86.827 -/* 86.828 -** subexpr -> (simpleexp | unop subexpr) { binop subexpr } 86.829 -** where `binop' is any binary operator with a priority higher than `limit' 86.830 -*/ 86.831 -static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { 86.832 - BinOpr op; 86.833 - UnOpr uop; 86.834 - enterlevel(ls); 86.835 - uop = getunopr(ls->t.token); 86.836 - if (uop != OPR_NOUNOPR) { 86.837 - luaX_next(ls); 86.838 - subexpr(ls, v, UNARY_PRIORITY); 86.839 - luaK_prefix(ls->fs, uop, v); 86.840 - } 86.841 - else simpleexp(ls, v); 86.842 - /* expand while operators have priorities higher than `limit' */ 86.843 - op = getbinopr(ls->t.token); 86.844 - while (op != OPR_NOBINOPR && priority[op].left > limit) { 86.845 - expdesc v2; 86.846 - BinOpr nextop; 86.847 - luaX_next(ls); 86.848 - luaK_infix(ls->fs, op, v); 86.849 - /* read sub-expression with higher priority */ 86.850 - nextop = subexpr(ls, &v2, priority[op].right); 86.851 - luaK_posfix(ls->fs, op, v, &v2); 86.852 - op = nextop; 86.853 - } 86.854 - leavelevel(ls); 86.855 - return op; /* return first untreated operator */ 86.856 -} 86.857 - 86.858 - 86.859 -static void expr (LexState *ls, expdesc *v) { 86.860 - subexpr(ls, v, 0); 86.861 -} 86.862 - 86.863 -/* }==================================================================== */ 86.864 - 86.865 - 86.866 - 86.867 -/* 86.868 -** {====================================================================== 86.869 -** Rules for Statements 86.870 -** ======================================================================= 86.871 -*/ 86.872 - 86.873 - 86.874 -static int block_follow (int token) { 86.875 - switch (token) { 86.876 - case TK_ELSE: case TK_ELSEIF: case TK_END: 86.877 - case TK_UNTIL: case TK_EOS: 86.878 - return 1; 86.879 - default: return 0; 86.880 - } 86.881 -} 86.882 - 86.883 - 86.884 -static void block (LexState *ls) { 86.885 - /* block -> chunk */ 86.886 - FuncState *fs = ls->fs; 86.887 - BlockCnt bl; 86.888 - enterblock(fs, &bl, 0); 86.889 - chunk(ls); 86.890 - lua_assert(bl.breaklist == NO_JUMP); 86.891 - leaveblock(fs); 86.892 -} 86.893 - 86.894 - 86.895 -/* 86.896 -** structure to chain all variables in the left-hand side of an 86.897 -** assignment 86.898 -*/ 86.899 -struct LHS_assign { 86.900 - struct LHS_assign *prev; 86.901 - expdesc v; /* variable (global, local, upvalue, or indexed) */ 86.902 -}; 86.903 - 86.904 - 86.905 -/* 86.906 -** check whether, in an assignment to a local variable, the local variable 86.907 -** is needed in a previous assignment (to a table). If so, save original 86.908 -** local value in a safe place and use this safe copy in the previous 86.909 -** assignment. 86.910 -*/ 86.911 -static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { 86.912 - FuncState *fs = ls->fs; 86.913 - int extra = fs->freereg; /* eventual position to save local variable */ 86.914 - int conflict = 0; 86.915 - for (; lh; lh = lh->prev) { 86.916 - if (lh->v.k == VINDEXED) { 86.917 - if (lh->v.u.s.info == v->u.s.info) { /* conflict? */ 86.918 - conflict = 1; 86.919 - lh->v.u.s.info = extra; /* previous assignment will use safe copy */ 86.920 - } 86.921 - if (lh->v.u.s.aux == v->u.s.info) { /* conflict? */ 86.922 - conflict = 1; 86.923 - lh->v.u.s.aux = extra; /* previous assignment will use safe copy */ 86.924 - } 86.925 - } 86.926 - } 86.927 - if (conflict) { 86.928 - luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0); /* make copy */ 86.929 - luaK_reserveregs(fs, 1); 86.930 - } 86.931 -} 86.932 - 86.933 - 86.934 -static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { 86.935 - expdesc e; 86.936 - check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, 86.937 - "syntax error"); 86.938 - if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */ 86.939 - struct LHS_assign nv; 86.940 - nv.prev = lh; 86.941 - primaryexp(ls, &nv.v); 86.942 - if (nv.v.k == VLOCAL) 86.943 - check_conflict(ls, lh, &nv.v); 86.944 - luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls, 86.945 - "variables in assignment"); 86.946 - assignment(ls, &nv, nvars+1); 86.947 - } 86.948 - else { /* assignment -> `=' explist1 */ 86.949 - int nexps; 86.950 - checknext(ls, '='); 86.951 - nexps = explist1(ls, &e); 86.952 - if (nexps != nvars) { 86.953 - adjust_assign(ls, nvars, nexps, &e); 86.954 - if (nexps > nvars) 86.955 - ls->fs->freereg -= nexps - nvars; /* remove extra values */ 86.956 - } 86.957 - else { 86.958 - luaK_setoneret(ls->fs, &e); /* close last expression */ 86.959 - luaK_storevar(ls->fs, &lh->v, &e); 86.960 - return; /* avoid default */ 86.961 - } 86.962 - } 86.963 - init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ 86.964 - luaK_storevar(ls->fs, &lh->v, &e); 86.965 -} 86.966 - 86.967 - 86.968 -static int cond (LexState *ls) { 86.969 - /* cond -> exp */ 86.970 - expdesc v; 86.971 - expr(ls, &v); /* read condition */ 86.972 - if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */ 86.973 - luaK_goiftrue(ls->fs, &v); 86.974 - return v.f; 86.975 -} 86.976 - 86.977 - 86.978 -static void breakstat (LexState *ls) { 86.979 - FuncState *fs = ls->fs; 86.980 - BlockCnt *bl = fs->bl; 86.981 - int upval = 0; 86.982 - while (bl && !bl->isbreakable) { 86.983 - upval |= bl->upval; 86.984 - bl = bl->previous; 86.985 - } 86.986 - if (!bl) 86.987 - luaX_syntaxerror(ls, "no loop to break"); 86.988 - if (upval) 86.989 - luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); 86.990 - luaK_concat(fs, &bl->breaklist, luaK_jump(fs)); 86.991 -} 86.992 - 86.993 - 86.994 -static void whilestat (LexState *ls, int line) { 86.995 - /* whilestat -> WHILE cond DO block END */ 86.996 - FuncState *fs = ls->fs; 86.997 - int whileinit; 86.998 - int condexit; 86.999 - BlockCnt bl; 86.1000 - luaX_next(ls); /* skip WHILE */ 86.1001 - whileinit = luaK_getlabel(fs); 86.1002 - condexit = cond(ls); 86.1003 - enterblock(fs, &bl, 1); 86.1004 - checknext(ls, TK_DO); 86.1005 - block(ls); 86.1006 - luaK_patchlist(fs, luaK_jump(fs), whileinit); 86.1007 - check_match(ls, TK_END, TK_WHILE, line); 86.1008 - leaveblock(fs); 86.1009 - luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ 86.1010 -} 86.1011 - 86.1012 - 86.1013 -static void repeatstat (LexState *ls, int line) { 86.1014 - /* repeatstat -> REPEAT block UNTIL cond */ 86.1015 - int condexit; 86.1016 - FuncState *fs = ls->fs; 86.1017 - int repeat_init = luaK_getlabel(fs); 86.1018 - BlockCnt bl1, bl2; 86.1019 - enterblock(fs, &bl1, 1); /* loop block */ 86.1020 - enterblock(fs, &bl2, 0); /* scope block */ 86.1021 - luaX_next(ls); /* skip REPEAT */ 86.1022 - chunk(ls); 86.1023 - check_match(ls, TK_UNTIL, TK_REPEAT, line); 86.1024 - condexit = cond(ls); /* read condition (inside scope block) */ 86.1025 - if (!bl2.upval) { /* no upvalues? */ 86.1026 - leaveblock(fs); /* finish scope */ 86.1027 - luaK_patchlist(ls->fs, condexit, repeat_init); /* close the loop */ 86.1028 - } 86.1029 - else { /* complete semantics when there are upvalues */ 86.1030 - breakstat(ls); /* if condition then break */ 86.1031 - luaK_patchtohere(ls->fs, condexit); /* else... */ 86.1032 - leaveblock(fs); /* finish scope... */ 86.1033 - luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init); /* and repeat */ 86.1034 - } 86.1035 - leaveblock(fs); /* finish loop */ 86.1036 -} 86.1037 - 86.1038 - 86.1039 -static int exp1 (LexState *ls) { 86.1040 - expdesc e; 86.1041 - int k; 86.1042 - expr(ls, &e); 86.1043 - k = e.k; 86.1044 - luaK_exp2nextreg(ls->fs, &e); 86.1045 - return k; 86.1046 -} 86.1047 - 86.1048 - 86.1049 -static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { 86.1050 - /* forbody -> DO block */ 86.1051 - BlockCnt bl; 86.1052 - FuncState *fs = ls->fs; 86.1053 - int prep, endfor; 86.1054 - adjustlocalvars(ls, 3); /* control variables */ 86.1055 - checknext(ls, TK_DO); 86.1056 - prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); 86.1057 - enterblock(fs, &bl, 0); /* scope for declared variables */ 86.1058 - adjustlocalvars(ls, nvars); 86.1059 - luaK_reserveregs(fs, nvars); 86.1060 - block(ls); 86.1061 - leaveblock(fs); /* end of scope for declared variables */ 86.1062 - luaK_patchtohere(fs, prep); 86.1063 - endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) : 86.1064 - luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars); 86.1065 - luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */ 86.1066 - luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1); 86.1067 -} 86.1068 - 86.1069 - 86.1070 -static void fornum (LexState *ls, TString *varname, int line) { 86.1071 - /* fornum -> NAME = exp1,exp1[,exp1] forbody */ 86.1072 - FuncState *fs = ls->fs; 86.1073 - int base = fs->freereg; 86.1074 - new_localvarliteral(ls, "(for index)", 0); 86.1075 - new_localvarliteral(ls, "(for limit)", 1); 86.1076 - new_localvarliteral(ls, "(for step)", 2); 86.1077 - new_localvar(ls, varname, 3); 86.1078 - checknext(ls, '='); 86.1079 - exp1(ls); /* initial value */ 86.1080 - checknext(ls, ','); 86.1081 - exp1(ls); /* limit */ 86.1082 - if (testnext(ls, ',')) 86.1083 - exp1(ls); /* optional step */ 86.1084 - else { /* default step = 1 */ 86.1085 - luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1)); 86.1086 - luaK_reserveregs(fs, 1); 86.1087 - } 86.1088 - forbody(ls, base, line, 1, 1); 86.1089 -} 86.1090 - 86.1091 - 86.1092 -static void forlist (LexState *ls, TString *indexname) { 86.1093 - /* forlist -> NAME {,NAME} IN explist1 forbody */ 86.1094 - FuncState *fs = ls->fs; 86.1095 - expdesc e; 86.1096 - int nvars = 0; 86.1097 - int line; 86.1098 - int base = fs->freereg; 86.1099 - /* create control variables */ 86.1100 - new_localvarliteral(ls, "(for generator)", nvars++); 86.1101 - new_localvarliteral(ls, "(for state)", nvars++); 86.1102 - new_localvarliteral(ls, "(for control)", nvars++); 86.1103 - /* create declared variables */ 86.1104 - new_localvar(ls, indexname, nvars++); 86.1105 - while (testnext(ls, ',')) 86.1106 - new_localvar(ls, str_checkname(ls), nvars++); 86.1107 - checknext(ls, TK_IN); 86.1108 - line = ls->linenumber; 86.1109 - adjust_assign(ls, 3, explist1(ls, &e), &e); 86.1110 - luaK_checkstack(fs, 3); /* extra space to call generator */ 86.1111 - forbody(ls, base, line, nvars - 3, 0); 86.1112 -} 86.1113 - 86.1114 - 86.1115 -static void forstat (LexState *ls, int line) { 86.1116 - /* forstat -> FOR (fornum | forlist) END */ 86.1117 - FuncState *fs = ls->fs; 86.1118 - TString *varname; 86.1119 - BlockCnt bl; 86.1120 - enterblock(fs, &bl, 1); /* scope for loop and control variables */ 86.1121 - luaX_next(ls); /* skip `for' */ 86.1122 - varname = str_checkname(ls); /* first variable name */ 86.1123 - switch (ls->t.token) { 86.1124 - case '=': fornum(ls, varname, line); break; 86.1125 - case ',': case TK_IN: forlist(ls, varname); break; 86.1126 - default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected"); 86.1127 - } 86.1128 - check_match(ls, TK_END, TK_FOR, line); 86.1129 - leaveblock(fs); /* loop scope (`break' jumps to this point) */ 86.1130 -} 86.1131 - 86.1132 - 86.1133 -static int test_then_block (LexState *ls) { 86.1134 - /* test_then_block -> [IF | ELSEIF] cond THEN block */ 86.1135 - int condexit; 86.1136 - luaX_next(ls); /* skip IF or ELSEIF */ 86.1137 - condexit = cond(ls); 86.1138 - checknext(ls, TK_THEN); 86.1139 - block(ls); /* `then' part */ 86.1140 - return condexit; 86.1141 -} 86.1142 - 86.1143 - 86.1144 -static void ifstat (LexState *ls, int line) { 86.1145 - /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ 86.1146 - FuncState *fs = ls->fs; 86.1147 - int flist; 86.1148 - int escapelist = NO_JUMP; 86.1149 - flist = test_then_block(ls); /* IF cond THEN block */ 86.1150 - while (ls->t.token == TK_ELSEIF) { 86.1151 - luaK_concat(fs, &escapelist, luaK_jump(fs)); 86.1152 - luaK_patchtohere(fs, flist); 86.1153 - flist = test_then_block(ls); /* ELSEIF cond THEN block */ 86.1154 - } 86.1155 - if (ls->t.token == TK_ELSE) { 86.1156 - luaK_concat(fs, &escapelist, luaK_jump(fs)); 86.1157 - luaK_patchtohere(fs, flist); 86.1158 - luaX_next(ls); /* skip ELSE (after patch, for correct line info) */ 86.1159 - block(ls); /* `else' part */ 86.1160 - } 86.1161 - else 86.1162 - luaK_concat(fs, &escapelist, flist); 86.1163 - luaK_patchtohere(fs, escapelist); 86.1164 - check_match(ls, TK_END, TK_IF, line); 86.1165 -} 86.1166 - 86.1167 - 86.1168 -static void localfunc (LexState *ls) { 86.1169 - expdesc v, b; 86.1170 - FuncState *fs = ls->fs; 86.1171 - new_localvar(ls, str_checkname(ls), 0); 86.1172 - init_exp(&v, VLOCAL, fs->freereg); 86.1173 - luaK_reserveregs(fs, 1); 86.1174 - adjustlocalvars(ls, 1); 86.1175 - body(ls, &b, 0, ls->linenumber); 86.1176 - luaK_storevar(fs, &v, &b); 86.1177 - /* debug information will only see the variable after this point! */ 86.1178 - getlocvar(fs, fs->nactvar - 1).startpc = fs->pc; 86.1179 -} 86.1180 - 86.1181 - 86.1182 -static void localstat (LexState *ls) { 86.1183 - /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */ 86.1184 - int nvars = 0; 86.1185 - int nexps; 86.1186 - expdesc e; 86.1187 - do { 86.1188 - new_localvar(ls, str_checkname(ls), nvars++); 86.1189 - } while (testnext(ls, ',')); 86.1190 - if (testnext(ls, '=')) 86.1191 - nexps = explist1(ls, &e); 86.1192 - else { 86.1193 - e.k = VVOID; 86.1194 - nexps = 0; 86.1195 - } 86.1196 - adjust_assign(ls, nvars, nexps, &e); 86.1197 - adjustlocalvars(ls, nvars); 86.1198 -} 86.1199 - 86.1200 - 86.1201 -static int funcname (LexState *ls, expdesc *v) { 86.1202 - /* funcname -> NAME {field} [`:' NAME] */ 86.1203 - int needself = 0; 86.1204 - singlevar(ls, v); 86.1205 - while (ls->t.token == '.') 86.1206 - field(ls, v); 86.1207 - if (ls->t.token == ':') { 86.1208 - needself = 1; 86.1209 - field(ls, v); 86.1210 - } 86.1211 - return needself; 86.1212 -} 86.1213 - 86.1214 - 86.1215 -static void funcstat (LexState *ls, int line) { 86.1216 - /* funcstat -> FUNCTION funcname body */ 86.1217 - int needself; 86.1218 - expdesc v, b; 86.1219 - luaX_next(ls); /* skip FUNCTION */ 86.1220 - needself = funcname(ls, &v); 86.1221 - body(ls, &b, needself, line); 86.1222 - luaK_storevar(ls->fs, &v, &b); 86.1223 - luaK_fixline(ls->fs, line); /* definition `happens' in the first line */ 86.1224 -} 86.1225 - 86.1226 - 86.1227 -static void exprstat (LexState *ls) { 86.1228 - /* stat -> func | assignment */ 86.1229 - FuncState *fs = ls->fs; 86.1230 - struct LHS_assign v; 86.1231 - primaryexp(ls, &v.v); 86.1232 - if (v.v.k == VCALL) /* stat -> func */ 86.1233 - SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ 86.1234 - else { /* stat -> assignment */ 86.1235 - v.prev = NULL; 86.1236 - assignment(ls, &v, 1); 86.1237 - } 86.1238 -} 86.1239 - 86.1240 - 86.1241 -static void retstat (LexState *ls) { 86.1242 - /* stat -> RETURN explist */ 86.1243 - FuncState *fs = ls->fs; 86.1244 - expdesc e; 86.1245 - int first, nret; /* registers with returned values */ 86.1246 - luaX_next(ls); /* skip RETURN */ 86.1247 - if (block_follow(ls->t.token) || ls->t.token == ';') 86.1248 - first = nret = 0; /* return no values */ 86.1249 - else { 86.1250 - nret = explist1(ls, &e); /* optional return values */ 86.1251 - if (hasmultret(e.k)) { 86.1252 - luaK_setmultret(fs, &e); 86.1253 - if (e.k == VCALL && nret == 1) { /* tail call? */ 86.1254 - SET_OPCODE(getcode(fs,&e), OP_TAILCALL); 86.1255 - lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar); 86.1256 - } 86.1257 - first = fs->nactvar; 86.1258 - nret = LUA_MULTRET; /* return all values */ 86.1259 - } 86.1260 - else { 86.1261 - if (nret == 1) /* only one single value? */ 86.1262 - first = luaK_exp2anyreg(fs, &e); 86.1263 - else { 86.1264 - luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */ 86.1265 - first = fs->nactvar; /* return all `active' values */ 86.1266 - lua_assert(nret == fs->freereg - first); 86.1267 - } 86.1268 - } 86.1269 - } 86.1270 - luaK_ret(fs, first, nret); 86.1271 -} 86.1272 - 86.1273 - 86.1274 -static int statement (LexState *ls) { 86.1275 - int line = ls->linenumber; /* may be needed for error messages */ 86.1276 - switch (ls->t.token) { 86.1277 - case TK_IF: { /* stat -> ifstat */ 86.1278 - ifstat(ls, line); 86.1279 - return 0; 86.1280 - } 86.1281 - case TK_WHILE: { /* stat -> whilestat */ 86.1282 - whilestat(ls, line); 86.1283 - return 0; 86.1284 - } 86.1285 - case TK_DO: { /* stat -> DO block END */ 86.1286 - luaX_next(ls); /* skip DO */ 86.1287 - block(ls); 86.1288 - check_match(ls, TK_END, TK_DO, line); 86.1289 - return 0; 86.1290 - } 86.1291 - case TK_FOR: { /* stat -> forstat */ 86.1292 - forstat(ls, line); 86.1293 - return 0; 86.1294 - } 86.1295 - case TK_REPEAT: { /* stat -> repeatstat */ 86.1296 - repeatstat(ls, line); 86.1297 - return 0; 86.1298 - } 86.1299 - case TK_FUNCTION: { 86.1300 - funcstat(ls, line); /* stat -> funcstat */ 86.1301 - return 0; 86.1302 - } 86.1303 - case TK_LOCAL: { /* stat -> localstat */ 86.1304 - luaX_next(ls); /* skip LOCAL */ 86.1305 - if (testnext(ls, TK_FUNCTION)) /* local function? */ 86.1306 - localfunc(ls); 86.1307 - else 86.1308 - localstat(ls); 86.1309 - return 0; 86.1310 - } 86.1311 - case TK_RETURN: { /* stat -> retstat */ 86.1312 - retstat(ls); 86.1313 - return 1; /* must be last statement */ 86.1314 - } 86.1315 - case TK_BREAK: { /* stat -> breakstat */ 86.1316 - luaX_next(ls); /* skip BREAK */ 86.1317 - breakstat(ls); 86.1318 - return 1; /* must be last statement */ 86.1319 - } 86.1320 - default: { 86.1321 - exprstat(ls); 86.1322 - return 0; /* to avoid warnings */ 86.1323 - } 86.1324 - } 86.1325 -} 86.1326 - 86.1327 - 86.1328 -static void chunk (LexState *ls) { 86.1329 - /* chunk -> { stat [`;'] } */ 86.1330 - int islast = 0; 86.1331 - enterlevel(ls); 86.1332 - while (!islast && !block_follow(ls->t.token)) { 86.1333 - islast = statement(ls); 86.1334 - testnext(ls, ';'); 86.1335 - lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && 86.1336 - ls->fs->freereg >= ls->fs->nactvar); 86.1337 - ls->fs->freereg = ls->fs->nactvar; /* free registers */ 86.1338 - } 86.1339 - leavelevel(ls); 86.1340 -} 86.1341 - 86.1342 -/* }====================================================================== */
87.1 --- a/src/lua/src/lparser.h Sat Mar 03 11:04:56 2012 -0600 87.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 87.3 @@ -1,82 +0,0 @@ 87.4 -/* 87.5 -** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $ 87.6 -** Lua Parser 87.7 -** See Copyright Notice in lua.h 87.8 -*/ 87.9 - 87.10 -#ifndef lparser_h 87.11 -#define lparser_h 87.12 - 87.13 -#include "llimits.h" 87.14 -#include "lobject.h" 87.15 -#include "lzio.h" 87.16 - 87.17 - 87.18 -/* 87.19 -** Expression descriptor 87.20 -*/ 87.21 - 87.22 -typedef enum { 87.23 - VVOID, /* no value */ 87.24 - VNIL, 87.25 - VTRUE, 87.26 - VFALSE, 87.27 - VK, /* info = index of constant in `k' */ 87.28 - VKNUM, /* nval = numerical value */ 87.29 - VLOCAL, /* info = local register */ 87.30 - VUPVAL, /* info = index of upvalue in `upvalues' */ 87.31 - VGLOBAL, /* info = index of table; aux = index of global name in `k' */ 87.32 - VINDEXED, /* info = table register; aux = index register (or `k') */ 87.33 - VJMP, /* info = instruction pc */ 87.34 - VRELOCABLE, /* info = instruction pc */ 87.35 - VNONRELOC, /* info = result register */ 87.36 - VCALL, /* info = instruction pc */ 87.37 - VVARARG /* info = instruction pc */ 87.38 -} expkind; 87.39 - 87.40 -typedef struct expdesc { 87.41 - expkind k; 87.42 - union { 87.43 - struct { int info, aux; } s; 87.44 - lua_Number nval; 87.45 - } u; 87.46 - int t; /* patch list of `exit when true' */ 87.47 - int f; /* patch list of `exit when false' */ 87.48 -} expdesc; 87.49 - 87.50 - 87.51 -typedef struct upvaldesc { 87.52 - lu_byte k; 87.53 - lu_byte info; 87.54 -} upvaldesc; 87.55 - 87.56 - 87.57 -struct BlockCnt; /* defined in lparser.c */ 87.58 - 87.59 - 87.60 -/* state needed to generate code for a given function */ 87.61 -typedef struct FuncState { 87.62 - Proto *f; /* current function header */ 87.63 - Table *h; /* table to find (and reuse) elements in `k' */ 87.64 - struct FuncState *prev; /* enclosing function */ 87.65 - struct LexState *ls; /* lexical state */ 87.66 - struct lua_State *L; /* copy of the Lua state */ 87.67 - struct BlockCnt *bl; /* chain of current blocks */ 87.68 - int pc; /* next position to code (equivalent to `ncode') */ 87.69 - int lasttarget; /* `pc' of last `jump target' */ 87.70 - int jpc; /* list of pending jumps to `pc' */ 87.71 - int freereg; /* first free register */ 87.72 - int nk; /* number of elements in `k' */ 87.73 - int np; /* number of elements in `p' */ 87.74 - short nlocvars; /* number of elements in `locvars' */ 87.75 - lu_byte nactvar; /* number of active local variables */ 87.76 - upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */ 87.77 - unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */ 87.78 -} FuncState; 87.79 - 87.80 - 87.81 -LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 87.82 - const char *name); 87.83 - 87.84 - 87.85 -#endif
88.1 --- a/src/lua/src/lstate.c Sat Mar 03 11:04:56 2012 -0600 88.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 88.3 @@ -1,214 +0,0 @@ 88.4 -/* 88.5 -** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $ 88.6 -** Global State 88.7 -** See Copyright Notice in lua.h 88.8 -*/ 88.9 - 88.10 - 88.11 -#include <stddef.h> 88.12 - 88.13 -#define lstate_c 88.14 -#define LUA_CORE 88.15 - 88.16 -#include "lua.h" 88.17 - 88.18 -#include "ldebug.h" 88.19 -#include "ldo.h" 88.20 -#include "lfunc.h" 88.21 -#include "lgc.h" 88.22 -#include "llex.h" 88.23 -#include "lmem.h" 88.24 -#include "lstate.h" 88.25 -#include "lstring.h" 88.26 -#include "ltable.h" 88.27 -#include "ltm.h" 88.28 - 88.29 - 88.30 -#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) 88.31 -#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) 88.32 -#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) 88.33 - 88.34 - 88.35 -/* 88.36 -** Main thread combines a thread state and the global state 88.37 -*/ 88.38 -typedef struct LG { 88.39 - lua_State l; 88.40 - global_State g; 88.41 -} LG; 88.42 - 88.43 - 88.44 - 88.45 -static void stack_init (lua_State *L1, lua_State *L) { 88.46 - /* initialize CallInfo array */ 88.47 - L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo); 88.48 - L1->ci = L1->base_ci; 88.49 - L1->size_ci = BASIC_CI_SIZE; 88.50 - L1->end_ci = L1->base_ci + L1->size_ci - 1; 88.51 - /* initialize stack array */ 88.52 - L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue); 88.53 - L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; 88.54 - L1->top = L1->stack; 88.55 - L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1; 88.56 - /* initialize first ci */ 88.57 - L1->ci->func = L1->top; 88.58 - setnilvalue(L1->top++); /* `function' entry for this `ci' */ 88.59 - L1->base = L1->ci->base = L1->top; 88.60 - L1->ci->top = L1->top + LUA_MINSTACK; 88.61 -} 88.62 - 88.63 - 88.64 -static void freestack (lua_State *L, lua_State *L1) { 88.65 - luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo); 88.66 - luaM_freearray(L, L1->stack, L1->stacksize, TValue); 88.67 -} 88.68 - 88.69 - 88.70 -/* 88.71 -** open parts that may cause memory-allocation errors 88.72 -*/ 88.73 -static void f_luaopen (lua_State *L, void *ud) { 88.74 - global_State *g = G(L); 88.75 - UNUSED(ud); 88.76 - stack_init(L, L); /* init stack */ 88.77 - sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ 88.78 - sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ 88.79 - luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 88.80 - luaT_init(L); 88.81 - luaX_init(L); 88.82 - luaS_fix(luaS_newliteral(L, MEMERRMSG)); 88.83 - g->GCthreshold = 4*g->totalbytes; 88.84 -} 88.85 - 88.86 - 88.87 -static void preinit_state (lua_State *L, global_State *g) { 88.88 - G(L) = g; 88.89 - L->stack = NULL; 88.90 - L->stacksize = 0; 88.91 - L->errorJmp = NULL; 88.92 - L->hook = NULL; 88.93 - L->hookmask = 0; 88.94 - L->basehookcount = 0; 88.95 - L->allowhook = 1; 88.96 - resethookcount(L); 88.97 - L->openupval = NULL; 88.98 - L->size_ci = 0; 88.99 - L->nCcalls = L->baseCcalls = 0; 88.100 - L->status = 0; 88.101 - L->base_ci = L->ci = NULL; 88.102 - L->savedpc = NULL; 88.103 - L->errfunc = 0; 88.104 - setnilvalue(gt(L)); 88.105 -} 88.106 - 88.107 - 88.108 -static void close_state (lua_State *L) { 88.109 - global_State *g = G(L); 88.110 - luaF_close(L, L->stack); /* close all upvalues for this thread */ 88.111 - luaC_freeall(L); /* collect all objects */ 88.112 - lua_assert(g->rootgc == obj2gco(L)); 88.113 - lua_assert(g->strt.nuse == 0); 88.114 - luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); 88.115 - luaZ_freebuffer(L, &g->buff); 88.116 - freestack(L, L); 88.117 - lua_assert(g->totalbytes == sizeof(LG)); 88.118 - (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); 88.119 -} 88.120 - 88.121 - 88.122 -lua_State *luaE_newthread (lua_State *L) { 88.123 - lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); 88.124 - luaC_link(L, obj2gco(L1), LUA_TTHREAD); 88.125 - preinit_state(L1, G(L)); 88.126 - stack_init(L1, L); /* init stack */ 88.127 - setobj2n(L, gt(L1), gt(L)); /* share table of globals */ 88.128 - L1->hookmask = L->hookmask; 88.129 - L1->basehookcount = L->basehookcount; 88.130 - L1->hook = L->hook; 88.131 - resethookcount(L1); 88.132 - lua_assert(iswhite(obj2gco(L1))); 88.133 - return L1; 88.134 -} 88.135 - 88.136 - 88.137 -void luaE_freethread (lua_State *L, lua_State *L1) { 88.138 - luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 88.139 - lua_assert(L1->openupval == NULL); 88.140 - luai_userstatefree(L1); 88.141 - freestack(L, L1); 88.142 - luaM_freemem(L, fromstate(L1), state_size(lua_State)); 88.143 -} 88.144 - 88.145 - 88.146 -LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { 88.147 - int i; 88.148 - lua_State *L; 88.149 - global_State *g; 88.150 - void *l = (*f)(ud, NULL, 0, state_size(LG)); 88.151 - if (l == NULL) return NULL; 88.152 - L = tostate(l); 88.153 - g = &((LG *)L)->g; 88.154 - L->next = NULL; 88.155 - L->tt = LUA_TTHREAD; 88.156 - g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); 88.157 - L->marked = luaC_white(g); 88.158 - set2bits(L->marked, FIXEDBIT, SFIXEDBIT); 88.159 - preinit_state(L, g); 88.160 - g->frealloc = f; 88.161 - g->ud = ud; 88.162 - g->mainthread = L; 88.163 - g->uvhead.u.l.prev = &g->uvhead; 88.164 - g->uvhead.u.l.next = &g->uvhead; 88.165 - g->GCthreshold = 0; /* mark it as unfinished state */ 88.166 - g->strt.size = 0; 88.167 - g->strt.nuse = 0; 88.168 - g->strt.hash = NULL; 88.169 - setnilvalue(registry(L)); 88.170 - luaZ_initbuffer(L, &g->buff); 88.171 - g->panic = NULL; 88.172 - g->gcstate = GCSpause; 88.173 - g->rootgc = obj2gco(L); 88.174 - g->sweepstrgc = 0; 88.175 - g->sweepgc = &g->rootgc; 88.176 - g->gray = NULL; 88.177 - g->grayagain = NULL; 88.178 - g->weak = NULL; 88.179 - g->tmudata = NULL; 88.180 - g->totalbytes = sizeof(LG); 88.181 - g->gcpause = LUAI_GCPAUSE; 88.182 - g->gcstepmul = LUAI_GCMUL; 88.183 - g->gcdept = 0; 88.184 - for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL; 88.185 - if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { 88.186 - /* memory allocation error: free partial state */ 88.187 - close_state(L); 88.188 - L = NULL; 88.189 - } 88.190 - else 88.191 - luai_userstateopen(L); 88.192 - return L; 88.193 -} 88.194 - 88.195 - 88.196 -static void callallgcTM (lua_State *L, void *ud) { 88.197 - UNUSED(ud); 88.198 - luaC_callGCTM(L); /* call GC metamethods for all udata */ 88.199 -} 88.200 - 88.201 - 88.202 -LUA_API void lua_close (lua_State *L) { 88.203 - L = G(L)->mainthread; /* only the main thread can be closed */ 88.204 - lua_lock(L); 88.205 - luaF_close(L, L->stack); /* close all upvalues for this thread */ 88.206 - luaC_separateudata(L, 1); /* separate udata that have GC metamethods */ 88.207 - L->errfunc = 0; /* no error function during GC metamethods */ 88.208 - do { /* repeat until no more errors */ 88.209 - L->ci = L->base_ci; 88.210 - L->base = L->top = L->ci->base; 88.211 - L->nCcalls = L->baseCcalls = 0; 88.212 - } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); 88.213 - lua_assert(G(L)->tmudata == NULL); 88.214 - luai_userstateclose(L); 88.215 - close_state(L); 88.216 -} 88.217 -
89.1 --- a/src/lua/src/lstate.h Sat Mar 03 11:04:56 2012 -0600 89.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 89.3 @@ -1,169 +0,0 @@ 89.4 -/* 89.5 -** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $ 89.6 -** Global State 89.7 -** See Copyright Notice in lua.h 89.8 -*/ 89.9 - 89.10 -#ifndef lstate_h 89.11 -#define lstate_h 89.12 - 89.13 -#include "lua.h" 89.14 - 89.15 -#include "lobject.h" 89.16 -#include "ltm.h" 89.17 -#include "lzio.h" 89.18 - 89.19 - 89.20 - 89.21 -struct lua_longjmp; /* defined in ldo.c */ 89.22 - 89.23 - 89.24 -/* table of globals */ 89.25 -#define gt(L) (&L->l_gt) 89.26 - 89.27 -/* registry */ 89.28 -#define registry(L) (&G(L)->l_registry) 89.29 - 89.30 - 89.31 -/* extra stack space to handle TM calls and some other extras */ 89.32 -#define EXTRA_STACK 5 89.33 - 89.34 - 89.35 -#define BASIC_CI_SIZE 8 89.36 - 89.37 -#define BASIC_STACK_SIZE (2*LUA_MINSTACK) 89.38 - 89.39 - 89.40 - 89.41 -typedef struct stringtable { 89.42 - GCObject **hash; 89.43 - lu_int32 nuse; /* number of elements */ 89.44 - int size; 89.45 -} stringtable; 89.46 - 89.47 - 89.48 -/* 89.49 -** informations about a call 89.50 -*/ 89.51 -typedef struct CallInfo { 89.52 - StkId base; /* base for this function */ 89.53 - StkId func; /* function index in the stack */ 89.54 - StkId top; /* top for this function */ 89.55 - const Instruction *savedpc; 89.56 - int nresults; /* expected number of results from this function */ 89.57 - int tailcalls; /* number of tail calls lost under this entry */ 89.58 -} CallInfo; 89.59 - 89.60 - 89.61 - 89.62 -#define curr_func(L) (clvalue(L->ci->func)) 89.63 -#define ci_func(ci) (clvalue((ci)->func)) 89.64 -#define f_isLua(ci) (!ci_func(ci)->c.isC) 89.65 -#define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci)) 89.66 - 89.67 - 89.68 -/* 89.69 -** `global state', shared by all threads of this state 89.70 -*/ 89.71 -typedef struct global_State { 89.72 - stringtable strt; /* hash table for strings */ 89.73 - lua_Alloc frealloc; /* function to reallocate memory */ 89.74 - void *ud; /* auxiliary data to `frealloc' */ 89.75 - lu_byte currentwhite; 89.76 - lu_byte gcstate; /* state of garbage collector */ 89.77 - int sweepstrgc; /* position of sweep in `strt' */ 89.78 - GCObject *rootgc; /* list of all collectable objects */ 89.79 - GCObject **sweepgc; /* position of sweep in `rootgc' */ 89.80 - GCObject *gray; /* list of gray objects */ 89.81 - GCObject *grayagain; /* list of objects to be traversed atomically */ 89.82 - GCObject *weak; /* list of weak tables (to be cleared) */ 89.83 - GCObject *tmudata; /* last element of list of userdata to be GC */ 89.84 - Mbuffer buff; /* temporary buffer for string concatentation */ 89.85 - lu_mem GCthreshold; 89.86 - lu_mem totalbytes; /* number of bytes currently allocated */ 89.87 - lu_mem estimate; /* an estimate of number of bytes actually in use */ 89.88 - lu_mem gcdept; /* how much GC is `behind schedule' */ 89.89 - int gcpause; /* size of pause between successive GCs */ 89.90 - int gcstepmul; /* GC `granularity' */ 89.91 - lua_CFunction panic; /* to be called in unprotected errors */ 89.92 - TValue l_registry; 89.93 - struct lua_State *mainthread; 89.94 - UpVal uvhead; /* head of double-linked list of all open upvalues */ 89.95 - struct Table *mt[NUM_TAGS]; /* metatables for basic types */ 89.96 - TString *tmname[TM_N]; /* array with tag-method names */ 89.97 -} global_State; 89.98 - 89.99 - 89.100 -/* 89.101 -** `per thread' state 89.102 -*/ 89.103 -struct lua_State { 89.104 - CommonHeader; 89.105 - lu_byte status; 89.106 - StkId top; /* first free slot in the stack */ 89.107 - StkId base; /* base of current function */ 89.108 - global_State *l_G; 89.109 - CallInfo *ci; /* call info for current function */ 89.110 - const Instruction *savedpc; /* `savedpc' of current function */ 89.111 - StkId stack_last; /* last free slot in the stack */ 89.112 - StkId stack; /* stack base */ 89.113 - CallInfo *end_ci; /* points after end of ci array*/ 89.114 - CallInfo *base_ci; /* array of CallInfo's */ 89.115 - int stacksize; 89.116 - int size_ci; /* size of array `base_ci' */ 89.117 - unsigned short nCcalls; /* number of nested C calls */ 89.118 - unsigned short baseCcalls; /* nested C calls when resuming coroutine */ 89.119 - lu_byte hookmask; 89.120 - lu_byte allowhook; 89.121 - int basehookcount; 89.122 - int hookcount; 89.123 - lua_Hook hook; 89.124 - TValue l_gt; /* table of globals */ 89.125 - TValue env; /* temporary place for environments */ 89.126 - GCObject *openupval; /* list of open upvalues in this stack */ 89.127 - GCObject *gclist; 89.128 - struct lua_longjmp *errorJmp; /* current error recover point */ 89.129 - ptrdiff_t errfunc; /* current error handling function (stack index) */ 89.130 -}; 89.131 - 89.132 - 89.133 -#define G(L) (L->l_G) 89.134 - 89.135 - 89.136 -/* 89.137 -** Union of all collectable objects 89.138 -*/ 89.139 -union GCObject { 89.140 - GCheader gch; 89.141 - union TString ts; 89.142 - union Udata u; 89.143 - union Closure cl; 89.144 - struct Table h; 89.145 - struct Proto p; 89.146 - struct UpVal uv; 89.147 - struct lua_State th; /* thread */ 89.148 -}; 89.149 - 89.150 - 89.151 -/* macros to convert a GCObject into a specific value */ 89.152 -#define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) 89.153 -#define gco2ts(o) (&rawgco2ts(o)->tsv) 89.154 -#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 89.155 -#define gco2u(o) (&rawgco2u(o)->uv) 89.156 -#define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) 89.157 -#define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 89.158 -#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 89.159 -#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 89.160 -#define ngcotouv(o) \ 89.161 - check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 89.162 -#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 89.163 - 89.164 -/* macro to convert any Lua object into a GCObject */ 89.165 -#define obj2gco(v) (cast(GCObject *, (v))) 89.166 - 89.167 - 89.168 -LUAI_FUNC lua_State *luaE_newthread (lua_State *L); 89.169 -LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 89.170 - 89.171 -#endif 89.172 -
90.1 --- a/src/lua/src/lstring.c Sat Mar 03 11:04:56 2012 -0600 90.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 90.3 @@ -1,111 +0,0 @@ 90.4 -/* 90.5 -** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 90.6 -** String table (keeps all strings handled by Lua) 90.7 -** See Copyright Notice in lua.h 90.8 -*/ 90.9 - 90.10 - 90.11 -#include <string.h> 90.12 - 90.13 -#define lstring_c 90.14 -#define LUA_CORE 90.15 - 90.16 -#include "lua.h" 90.17 - 90.18 -#include "lmem.h" 90.19 -#include "lobject.h" 90.20 -#include "lstate.h" 90.21 -#include "lstring.h" 90.22 - 90.23 - 90.24 - 90.25 -void luaS_resize (lua_State *L, int newsize) { 90.26 - GCObject **newhash; 90.27 - stringtable *tb; 90.28 - int i; 90.29 - if (G(L)->gcstate == GCSsweepstring) 90.30 - return; /* cannot resize during GC traverse */ 90.31 - newhash = luaM_newvector(L, newsize, GCObject *); 90.32 - tb = &G(L)->strt; 90.33 - for (i=0; i<newsize; i++) newhash[i] = NULL; 90.34 - /* rehash */ 90.35 - for (i=0; i<tb->size; i++) { 90.36 - GCObject *p = tb->hash[i]; 90.37 - while (p) { /* for each node in the list */ 90.38 - GCObject *next = p->gch.next; /* save next */ 90.39 - unsigned int h = gco2ts(p)->hash; 90.40 - int h1 = lmod(h, newsize); /* new position */ 90.41 - lua_assert(cast_int(h%newsize) == lmod(h, newsize)); 90.42 - p->gch.next = newhash[h1]; /* chain it */ 90.43 - newhash[h1] = p; 90.44 - p = next; 90.45 - } 90.46 - } 90.47 - luaM_freearray(L, tb->hash, tb->size, TString *); 90.48 - tb->size = newsize; 90.49 - tb->hash = newhash; 90.50 -} 90.51 - 90.52 - 90.53 -static TString *newlstr (lua_State *L, const char *str, size_t l, 90.54 - unsigned int h) { 90.55 - TString *ts; 90.56 - stringtable *tb; 90.57 - if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 90.58 - luaM_toobig(L); 90.59 - ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString))); 90.60 - ts->tsv.len = l; 90.61 - ts->tsv.hash = h; 90.62 - ts->tsv.marked = luaC_white(G(L)); 90.63 - ts->tsv.tt = LUA_TSTRING; 90.64 - ts->tsv.reserved = 0; 90.65 - memcpy(ts+1, str, l*sizeof(char)); 90.66 - ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 90.67 - tb = &G(L)->strt; 90.68 - h = lmod(h, tb->size); 90.69 - ts->tsv.next = tb->hash[h]; /* chain new entry */ 90.70 - tb->hash[h] = obj2gco(ts); 90.71 - tb->nuse++; 90.72 - if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 90.73 - luaS_resize(L, tb->size*2); /* too crowded */ 90.74 - return ts; 90.75 -} 90.76 - 90.77 - 90.78 -TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 90.79 - GCObject *o; 90.80 - unsigned int h = cast(unsigned int, l); /* seed */ 90.81 - size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ 90.82 - size_t l1; 90.83 - for (l1=l; l1>=step; l1-=step) /* compute hash */ 90.84 - h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1])); 90.85 - for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; 90.86 - o != NULL; 90.87 - o = o->gch.next) { 90.88 - TString *ts = rawgco2ts(o); 90.89 - if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) { 90.90 - /* string may be dead */ 90.91 - if (isdead(G(L), o)) changewhite(o); 90.92 - return ts; 90.93 - } 90.94 - } 90.95 - return newlstr(L, str, l, h); /* not found */ 90.96 -} 90.97 - 90.98 - 90.99 -Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { 90.100 - Udata *u; 90.101 - if (s > MAX_SIZET - sizeof(Udata)) 90.102 - luaM_toobig(L); 90.103 - u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata))); 90.104 - u->uv.marked = luaC_white(G(L)); /* is not finalized */ 90.105 - u->uv.tt = LUA_TUSERDATA; 90.106 - u->uv.len = s; 90.107 - u->uv.metatable = NULL; 90.108 - u->uv.env = e; 90.109 - /* chain it on udata list (after main thread) */ 90.110 - u->uv.next = G(L)->mainthread->next; 90.111 - G(L)->mainthread->next = obj2gco(u); 90.112 - return u; 90.113 -} 90.114 -
91.1 --- a/src/lua/src/lstring.h Sat Mar 03 11:04:56 2012 -0600 91.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 91.3 @@ -1,31 +0,0 @@ 91.4 -/* 91.5 -** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $ 91.6 -** String table (keep all strings handled by Lua) 91.7 -** See Copyright Notice in lua.h 91.8 -*/ 91.9 - 91.10 -#ifndef lstring_h 91.11 -#define lstring_h 91.12 - 91.13 - 91.14 -#include "lgc.h" 91.15 -#include "lobject.h" 91.16 -#include "lstate.h" 91.17 - 91.18 - 91.19 -#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 91.20 - 91.21 -#define sizeudata(u) (sizeof(union Udata)+(u)->len) 91.22 - 91.23 -#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) 91.24 -#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 91.25 - (sizeof(s)/sizeof(char))-1)) 91.26 - 91.27 -#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 91.28 - 91.29 -LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 91.30 -LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 91.31 -LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 91.32 - 91.33 - 91.34 -#endif
92.1 --- a/src/lua/src/lstrlib.c Sat Mar 03 11:04:56 2012 -0600 92.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 92.3 @@ -1,869 +0,0 @@ 92.4 -/* 92.5 -** $Id: lstrlib.c,v 1.132.1.4 2008/07/11 17:27:21 roberto Exp $ 92.6 -** Standard library for string operations and pattern-matching 92.7 -** See Copyright Notice in lua.h 92.8 -*/ 92.9 - 92.10 - 92.11 -#include <ctype.h> 92.12 -#include <stddef.h> 92.13 -#include <stdio.h> 92.14 -#include <stdlib.h> 92.15 -#include <string.h> 92.16 - 92.17 -#define lstrlib_c 92.18 -#define LUA_LIB 92.19 - 92.20 -#include "lua.h" 92.21 - 92.22 -#include "lauxlib.h" 92.23 -#include "lualib.h" 92.24 - 92.25 - 92.26 -/* macro to `unsign' a character */ 92.27 -#define uchar(c) ((unsigned char)(c)) 92.28 - 92.29 - 92.30 - 92.31 -static int str_len (lua_State *L) { 92.32 - size_t l; 92.33 - luaL_checklstring(L, 1, &l); 92.34 - lua_pushinteger(L, l); 92.35 - return 1; 92.36 -} 92.37 - 92.38 - 92.39 -static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) { 92.40 - /* relative string position: negative means back from end */ 92.41 - if (pos < 0) pos += (ptrdiff_t)len + 1; 92.42 - return (pos >= 0) ? pos : 0; 92.43 -} 92.44 - 92.45 - 92.46 -static int str_sub (lua_State *L) { 92.47 - size_t l; 92.48 - const char *s = luaL_checklstring(L, 1, &l); 92.49 - ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l); 92.50 - ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l); 92.51 - if (start < 1) start = 1; 92.52 - if (end > (ptrdiff_t)l) end = (ptrdiff_t)l; 92.53 - if (start <= end) 92.54 - lua_pushlstring(L, s+start-1, end-start+1); 92.55 - else lua_pushliteral(L, ""); 92.56 - return 1; 92.57 -} 92.58 - 92.59 - 92.60 -static int str_reverse (lua_State *L) { 92.61 - size_t l; 92.62 - luaL_Buffer b; 92.63 - const char *s = luaL_checklstring(L, 1, &l); 92.64 - luaL_buffinit(L, &b); 92.65 - while (l--) luaL_addchar(&b, s[l]); 92.66 - luaL_pushresult(&b); 92.67 - return 1; 92.68 -} 92.69 - 92.70 - 92.71 -static int str_lower (lua_State *L) { 92.72 - size_t l; 92.73 - size_t i; 92.74 - luaL_Buffer b; 92.75 - const char *s = luaL_checklstring(L, 1, &l); 92.76 - luaL_buffinit(L, &b); 92.77 - for (i=0; i<l; i++) 92.78 - luaL_addchar(&b, tolower(uchar(s[i]))); 92.79 - luaL_pushresult(&b); 92.80 - return 1; 92.81 -} 92.82 - 92.83 - 92.84 -static int str_upper (lua_State *L) { 92.85 - size_t l; 92.86 - size_t i; 92.87 - luaL_Buffer b; 92.88 - const char *s = luaL_checklstring(L, 1, &l); 92.89 - luaL_buffinit(L, &b); 92.90 - for (i=0; i<l; i++) 92.91 - luaL_addchar(&b, toupper(uchar(s[i]))); 92.92 - luaL_pushresult(&b); 92.93 - return 1; 92.94 -} 92.95 - 92.96 -static int str_rep (lua_State *L) { 92.97 - size_t l; 92.98 - luaL_Buffer b; 92.99 - const char *s = luaL_checklstring(L, 1, &l); 92.100 - int n = luaL_checkint(L, 2); 92.101 - luaL_buffinit(L, &b); 92.102 - while (n-- > 0) 92.103 - luaL_addlstring(&b, s, l); 92.104 - luaL_pushresult(&b); 92.105 - return 1; 92.106 -} 92.107 - 92.108 - 92.109 -static int str_byte (lua_State *L) { 92.110 - size_t l; 92.111 - const char *s = luaL_checklstring(L, 1, &l); 92.112 - ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l); 92.113 - ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l); 92.114 - int n, i; 92.115 - if (posi <= 0) posi = 1; 92.116 - if ((size_t)pose > l) pose = l; 92.117 - if (posi > pose) return 0; /* empty interval; return no values */ 92.118 - n = (int)(pose - posi + 1); 92.119 - if (posi + n <= pose) /* overflow? */ 92.120 - luaL_error(L, "string slice too long"); 92.121 - luaL_checkstack(L, n, "string slice too long"); 92.122 - for (i=0; i<n; i++) 92.123 - lua_pushinteger(L, uchar(s[posi+i-1])); 92.124 - return n; 92.125 -} 92.126 - 92.127 - 92.128 -static int str_char (lua_State *L) { 92.129 - int n = lua_gettop(L); /* number of arguments */ 92.130 - int i; 92.131 - luaL_Buffer b; 92.132 - luaL_buffinit(L, &b); 92.133 - for (i=1; i<=n; i++) { 92.134 - int c = luaL_checkint(L, i); 92.135 - luaL_argcheck(L, uchar(c) == c, i, "invalid value"); 92.136 - luaL_addchar(&b, uchar(c)); 92.137 - } 92.138 - luaL_pushresult(&b); 92.139 - return 1; 92.140 -} 92.141 - 92.142 - 92.143 -static int writer (lua_State *L, const void* b, size_t size, void* B) { 92.144 - (void)L; 92.145 - luaL_addlstring((luaL_Buffer*) B, (const char *)b, size); 92.146 - return 0; 92.147 -} 92.148 - 92.149 - 92.150 -static int str_dump (lua_State *L) { 92.151 - luaL_Buffer b; 92.152 - luaL_checktype(L, 1, LUA_TFUNCTION); 92.153 - lua_settop(L, 1); 92.154 - luaL_buffinit(L,&b); 92.155 - if (lua_dump(L, writer, &b) != 0) 92.156 - luaL_error(L, "unable to dump given function"); 92.157 - luaL_pushresult(&b); 92.158 - return 1; 92.159 -} 92.160 - 92.161 - 92.162 - 92.163 -/* 92.164 -** {====================================================== 92.165 -** PATTERN MATCHING 92.166 -** ======================================================= 92.167 -*/ 92.168 - 92.169 - 92.170 -#define CAP_UNFINISHED (-1) 92.171 -#define CAP_POSITION (-2) 92.172 - 92.173 -typedef struct MatchState { 92.174 - const char *src_init; /* init of source string */ 92.175 - const char *src_end; /* end (`\0') of source string */ 92.176 - lua_State *L; 92.177 - int level; /* total number of captures (finished or unfinished) */ 92.178 - struct { 92.179 - const char *init; 92.180 - ptrdiff_t len; 92.181 - } capture[LUA_MAXCAPTURES]; 92.182 -} MatchState; 92.183 - 92.184 - 92.185 -#define L_ESC '%' 92.186 -#define SPECIALS "^$*+?.([%-" 92.187 - 92.188 - 92.189 -static int check_capture (MatchState *ms, int l) { 92.190 - l -= '1'; 92.191 - if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) 92.192 - return luaL_error(ms->L, "invalid capture index"); 92.193 - return l; 92.194 -} 92.195 - 92.196 - 92.197 -static int capture_to_close (MatchState *ms) { 92.198 - int level = ms->level; 92.199 - for (level--; level>=0; level--) 92.200 - if (ms->capture[level].len == CAP_UNFINISHED) return level; 92.201 - return luaL_error(ms->L, "invalid pattern capture"); 92.202 -} 92.203 - 92.204 - 92.205 -static const char *classend (MatchState *ms, const char *p) { 92.206 - switch (*p++) { 92.207 - case L_ESC: { 92.208 - if (*p == '\0') 92.209 - luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); 92.210 - return p+1; 92.211 - } 92.212 - case '[': { 92.213 - if (*p == '^') p++; 92.214 - do { /* look for a `]' */ 92.215 - if (*p == '\0') 92.216 - luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); 92.217 - if (*(p++) == L_ESC && *p != '\0') 92.218 - p++; /* skip escapes (e.g. `%]') */ 92.219 - } while (*p != ']'); 92.220 - return p+1; 92.221 - } 92.222 - default: { 92.223 - return p; 92.224 - } 92.225 - } 92.226 -} 92.227 - 92.228 - 92.229 -static int match_class (int c, int cl) { 92.230 - int res; 92.231 - switch (tolower(cl)) { 92.232 - case 'a' : res = isalpha(c); break; 92.233 - case 'c' : res = iscntrl(c); break; 92.234 - case 'd' : res = isdigit(c); break; 92.235 - case 'l' : res = islower(c); break; 92.236 - case 'p' : res = ispunct(c); break; 92.237 - case 's' : res = isspace(c); break; 92.238 - case 'u' : res = isupper(c); break; 92.239 - case 'w' : res = isalnum(c); break; 92.240 - case 'x' : res = isxdigit(c); break; 92.241 - case 'z' : res = (c == 0); break; 92.242 - default: return (cl == c); 92.243 - } 92.244 - return (islower(cl) ? res : !res); 92.245 -} 92.246 - 92.247 - 92.248 -static int matchbracketclass (int c, const char *p, const char *ec) { 92.249 - int sig = 1; 92.250 - if (*(p+1) == '^') { 92.251 - sig = 0; 92.252 - p++; /* skip the `^' */ 92.253 - } 92.254 - while (++p < ec) { 92.255 - if (*p == L_ESC) { 92.256 - p++; 92.257 - if (match_class(c, uchar(*p))) 92.258 - return sig; 92.259 - } 92.260 - else if ((*(p+1) == '-') && (p+2 < ec)) { 92.261 - p+=2; 92.262 - if (uchar(*(p-2)) <= c && c <= uchar(*p)) 92.263 - return sig; 92.264 - } 92.265 - else if (uchar(*p) == c) return sig; 92.266 - } 92.267 - return !sig; 92.268 -} 92.269 - 92.270 - 92.271 -static int singlematch (int c, const char *p, const char *ep) { 92.272 - switch (*p) { 92.273 - case '.': return 1; /* matches any char */ 92.274 - case L_ESC: return match_class(c, uchar(*(p+1))); 92.275 - case '[': return matchbracketclass(c, p, ep-1); 92.276 - default: return (uchar(*p) == c); 92.277 - } 92.278 -} 92.279 - 92.280 - 92.281 -static const char *match (MatchState *ms, const char *s, const char *p); 92.282 - 92.283 - 92.284 -static const char *matchbalance (MatchState *ms, const char *s, 92.285 - const char *p) { 92.286 - if (*p == 0 || *(p+1) == 0) 92.287 - luaL_error(ms->L, "unbalanced pattern"); 92.288 - if (*s != *p) return NULL; 92.289 - else { 92.290 - int b = *p; 92.291 - int e = *(p+1); 92.292 - int cont = 1; 92.293 - while (++s < ms->src_end) { 92.294 - if (*s == e) { 92.295 - if (--cont == 0) return s+1; 92.296 - } 92.297 - else if (*s == b) cont++; 92.298 - } 92.299 - } 92.300 - return NULL; /* string ends out of balance */ 92.301 -} 92.302 - 92.303 - 92.304 -static const char *max_expand (MatchState *ms, const char *s, 92.305 - const char *p, const char *ep) { 92.306 - ptrdiff_t i = 0; /* counts maximum expand for item */ 92.307 - while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep)) 92.308 - i++; 92.309 - /* keeps trying to match with the maximum repetitions */ 92.310 - while (i>=0) { 92.311 - const char *res = match(ms, (s+i), ep+1); 92.312 - if (res) return res; 92.313 - i--; /* else didn't match; reduce 1 repetition to try again */ 92.314 - } 92.315 - return NULL; 92.316 -} 92.317 - 92.318 - 92.319 -static const char *min_expand (MatchState *ms, const char *s, 92.320 - const char *p, const char *ep) { 92.321 - for (;;) { 92.322 - const char *res = match(ms, s, ep+1); 92.323 - if (res != NULL) 92.324 - return res; 92.325 - else if (s<ms->src_end && singlematch(uchar(*s), p, ep)) 92.326 - s++; /* try with one more repetition */ 92.327 - else return NULL; 92.328 - } 92.329 -} 92.330 - 92.331 - 92.332 -static const char *start_capture (MatchState *ms, const char *s, 92.333 - const char *p, int what) { 92.334 - const char *res; 92.335 - int level = ms->level; 92.336 - if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); 92.337 - ms->capture[level].init = s; 92.338 - ms->capture[level].len = what; 92.339 - ms->level = level+1; 92.340 - if ((res=match(ms, s, p)) == NULL) /* match failed? */ 92.341 - ms->level--; /* undo capture */ 92.342 - return res; 92.343 -} 92.344 - 92.345 - 92.346 -static const char *end_capture (MatchState *ms, const char *s, 92.347 - const char *p) { 92.348 - int l = capture_to_close(ms); 92.349 - const char *res; 92.350 - ms->capture[l].len = s - ms->capture[l].init; /* close capture */ 92.351 - if ((res = match(ms, s, p)) == NULL) /* match failed? */ 92.352 - ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ 92.353 - return res; 92.354 -} 92.355 - 92.356 - 92.357 -static const char *match_capture (MatchState *ms, const char *s, int l) { 92.358 - size_t len; 92.359 - l = check_capture(ms, l); 92.360 - len = ms->capture[l].len; 92.361 - if ((size_t)(ms->src_end-s) >= len && 92.362 - memcmp(ms->capture[l].init, s, len) == 0) 92.363 - return s+len; 92.364 - else return NULL; 92.365 -} 92.366 - 92.367 - 92.368 -static const char *match (MatchState *ms, const char *s, const char *p) { 92.369 - init: /* using goto's to optimize tail recursion */ 92.370 - switch (*p) { 92.371 - case '(': { /* start capture */ 92.372 - if (*(p+1) == ')') /* position capture? */ 92.373 - return start_capture(ms, s, p+2, CAP_POSITION); 92.374 - else 92.375 - return start_capture(ms, s, p+1, CAP_UNFINISHED); 92.376 - } 92.377 - case ')': { /* end capture */ 92.378 - return end_capture(ms, s, p+1); 92.379 - } 92.380 - case L_ESC: { 92.381 - switch (*(p+1)) { 92.382 - case 'b': { /* balanced string? */ 92.383 - s = matchbalance(ms, s, p+2); 92.384 - if (s == NULL) return NULL; 92.385 - p+=4; goto init; /* else return match(ms, s, p+4); */ 92.386 - } 92.387 - case 'f': { /* frontier? */ 92.388 - const char *ep; char previous; 92.389 - p += 2; 92.390 - if (*p != '[') 92.391 - luaL_error(ms->L, "missing " LUA_QL("[") " after " 92.392 - LUA_QL("%%f") " in pattern"); 92.393 - ep = classend(ms, p); /* points to what is next */ 92.394 - previous = (s == ms->src_init) ? '\0' : *(s-1); 92.395 - if (matchbracketclass(uchar(previous), p, ep-1) || 92.396 - !matchbracketclass(uchar(*s), p, ep-1)) return NULL; 92.397 - p=ep; goto init; /* else return match(ms, s, ep); */ 92.398 - } 92.399 - default: { 92.400 - if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */ 92.401 - s = match_capture(ms, s, uchar(*(p+1))); 92.402 - if (s == NULL) return NULL; 92.403 - p+=2; goto init; /* else return match(ms, s, p+2) */ 92.404 - } 92.405 - goto dflt; /* case default */ 92.406 - } 92.407 - } 92.408 - } 92.409 - case '\0': { /* end of pattern */ 92.410 - return s; /* match succeeded */ 92.411 - } 92.412 - case '$': { 92.413 - if (*(p+1) == '\0') /* is the `$' the last char in pattern? */ 92.414 - return (s == ms->src_end) ? s : NULL; /* check end of string */ 92.415 - else goto dflt; 92.416 - } 92.417 - default: dflt: { /* it is a pattern item */ 92.418 - const char *ep = classend(ms, p); /* points to what is next */ 92.419 - int m = s<ms->src_end && singlematch(uchar(*s), p, ep); 92.420 - switch (*ep) { 92.421 - case '?': { /* optional */ 92.422 - const char *res; 92.423 - if (m && ((res=match(ms, s+1, ep+1)) != NULL)) 92.424 - return res; 92.425 - p=ep+1; goto init; /* else return match(ms, s, ep+1); */ 92.426 - } 92.427 - case '*': { /* 0 or more repetitions */ 92.428 - return max_expand(ms, s, p, ep); 92.429 - } 92.430 - case '+': { /* 1 or more repetitions */ 92.431 - return (m ? max_expand(ms, s+1, p, ep) : NULL); 92.432 - } 92.433 - case '-': { /* 0 or more repetitions (minimum) */ 92.434 - return min_expand(ms, s, p, ep); 92.435 - } 92.436 - default: { 92.437 - if (!m) return NULL; 92.438 - s++; p=ep; goto init; /* else return match(ms, s+1, ep); */ 92.439 - } 92.440 - } 92.441 - } 92.442 - } 92.443 -} 92.444 - 92.445 - 92.446 - 92.447 -static const char *lmemfind (const char *s1, size_t l1, 92.448 - const char *s2, size_t l2) { 92.449 - if (l2 == 0) return s1; /* empty strings are everywhere */ 92.450 - else if (l2 > l1) return NULL; /* avoids a negative `l1' */ 92.451 - else { 92.452 - const char *init; /* to search for a `*s2' inside `s1' */ 92.453 - l2--; /* 1st char will be checked by `memchr' */ 92.454 - l1 = l1-l2; /* `s2' cannot be found after that */ 92.455 - while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { 92.456 - init++; /* 1st char is already checked */ 92.457 - if (memcmp(init, s2+1, l2) == 0) 92.458 - return init-1; 92.459 - else { /* correct `l1' and `s1' to try again */ 92.460 - l1 -= init-s1; 92.461 - s1 = init; 92.462 - } 92.463 - } 92.464 - return NULL; /* not found */ 92.465 - } 92.466 -} 92.467 - 92.468 - 92.469 -static void push_onecapture (MatchState *ms, int i, const char *s, 92.470 - const char *e) { 92.471 - if (i >= ms->level) { 92.472 - if (i == 0) /* ms->level == 0, too */ 92.473 - lua_pushlstring(ms->L, s, e - s); /* add whole match */ 92.474 - else 92.475 - luaL_error(ms->L, "invalid capture index"); 92.476 - } 92.477 - else { 92.478 - ptrdiff_t l = ms->capture[i].len; 92.479 - if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); 92.480 - if (l == CAP_POSITION) 92.481 - lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); 92.482 - else 92.483 - lua_pushlstring(ms->L, ms->capture[i].init, l); 92.484 - } 92.485 -} 92.486 - 92.487 - 92.488 -static int push_captures (MatchState *ms, const char *s, const char *e) { 92.489 - int i; 92.490 - int nlevels = (ms->level == 0 && s) ? 1 : ms->level; 92.491 - luaL_checkstack(ms->L, nlevels, "too many captures"); 92.492 - for (i = 0; i < nlevels; i++) 92.493 - push_onecapture(ms, i, s, e); 92.494 - return nlevels; /* number of strings pushed */ 92.495 -} 92.496 - 92.497 - 92.498 -static int str_find_aux (lua_State *L, int find) { 92.499 - size_t l1, l2; 92.500 - const char *s = luaL_checklstring(L, 1, &l1); 92.501 - const char *p = luaL_checklstring(L, 2, &l2); 92.502 - ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; 92.503 - if (init < 0) init = 0; 92.504 - else if ((size_t)(init) > l1) init = (ptrdiff_t)l1; 92.505 - if (find && (lua_toboolean(L, 4) || /* explicit request? */ 92.506 - strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ 92.507 - /* do a plain search */ 92.508 - const char *s2 = lmemfind(s+init, l1-init, p, l2); 92.509 - if (s2) { 92.510 - lua_pushinteger(L, s2-s+1); 92.511 - lua_pushinteger(L, s2-s+l2); 92.512 - return 2; 92.513 - } 92.514 - } 92.515 - else { 92.516 - MatchState ms; 92.517 - int anchor = (*p == '^') ? (p++, 1) : 0; 92.518 - const char *s1=s+init; 92.519 - ms.L = L; 92.520 - ms.src_init = s; 92.521 - ms.src_end = s+l1; 92.522 - do { 92.523 - const char *res; 92.524 - ms.level = 0; 92.525 - if ((res=match(&ms, s1, p)) != NULL) { 92.526 - if (find) { 92.527 - lua_pushinteger(L, s1-s+1); /* start */ 92.528 - lua_pushinteger(L, res-s); /* end */ 92.529 - return push_captures(&ms, NULL, 0) + 2; 92.530 - } 92.531 - else 92.532 - return push_captures(&ms, s1, res); 92.533 - } 92.534 - } while (s1++ < ms.src_end && !anchor); 92.535 - } 92.536 - lua_pushnil(L); /* not found */ 92.537 - return 1; 92.538 -} 92.539 - 92.540 - 92.541 -static int str_find (lua_State *L) { 92.542 - return str_find_aux(L, 1); 92.543 -} 92.544 - 92.545 - 92.546 -static int str_match (lua_State *L) { 92.547 - return str_find_aux(L, 0); 92.548 -} 92.549 - 92.550 - 92.551 -static int gmatch_aux (lua_State *L) { 92.552 - MatchState ms; 92.553 - size_t ls; 92.554 - const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); 92.555 - const char *p = lua_tostring(L, lua_upvalueindex(2)); 92.556 - const char *src; 92.557 - ms.L = L; 92.558 - ms.src_init = s; 92.559 - ms.src_end = s+ls; 92.560 - for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3)); 92.561 - src <= ms.src_end; 92.562 - src++) { 92.563 - const char *e; 92.564 - ms.level = 0; 92.565 - if ((e = match(&ms, src, p)) != NULL) { 92.566 - lua_Integer newstart = e-s; 92.567 - if (e == src) newstart++; /* empty match? go at least one position */ 92.568 - lua_pushinteger(L, newstart); 92.569 - lua_replace(L, lua_upvalueindex(3)); 92.570 - return push_captures(&ms, src, e); 92.571 - } 92.572 - } 92.573 - return 0; /* not found */ 92.574 -} 92.575 - 92.576 - 92.577 -static int gmatch (lua_State *L) { 92.578 - luaL_checkstring(L, 1); 92.579 - luaL_checkstring(L, 2); 92.580 - lua_settop(L, 2); 92.581 - lua_pushinteger(L, 0); 92.582 - lua_pushcclosure(L, gmatch_aux, 3); 92.583 - return 1; 92.584 -} 92.585 - 92.586 - 92.587 -static int gfind_nodef (lua_State *L) { 92.588 - return luaL_error(L, LUA_QL("string.gfind") " was renamed to " 92.589 - LUA_QL("string.gmatch")); 92.590 -} 92.591 - 92.592 - 92.593 -static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, 92.594 - const char *e) { 92.595 - size_t l, i; 92.596 - const char *news = lua_tolstring(ms->L, 3, &l); 92.597 - for (i = 0; i < l; i++) { 92.598 - if (news[i] != L_ESC) 92.599 - luaL_addchar(b, news[i]); 92.600 - else { 92.601 - i++; /* skip ESC */ 92.602 - if (!isdigit(uchar(news[i]))) 92.603 - luaL_addchar(b, news[i]); 92.604 - else if (news[i] == '0') 92.605 - luaL_addlstring(b, s, e - s); 92.606 - else { 92.607 - push_onecapture(ms, news[i] - '1', s, e); 92.608 - luaL_addvalue(b); /* add capture to accumulated result */ 92.609 - } 92.610 - } 92.611 - } 92.612 -} 92.613 - 92.614 - 92.615 -static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, 92.616 - const char *e) { 92.617 - lua_State *L = ms->L; 92.618 - switch (lua_type(L, 3)) { 92.619 - case LUA_TNUMBER: 92.620 - case LUA_TSTRING: { 92.621 - add_s(ms, b, s, e); 92.622 - return; 92.623 - } 92.624 - case LUA_TFUNCTION: { 92.625 - int n; 92.626 - lua_pushvalue(L, 3); 92.627 - n = push_captures(ms, s, e); 92.628 - lua_call(L, n, 1); 92.629 - break; 92.630 - } 92.631 - case LUA_TTABLE: { 92.632 - push_onecapture(ms, 0, s, e); 92.633 - lua_gettable(L, 3); 92.634 - break; 92.635 - } 92.636 - } 92.637 - if (!lua_toboolean(L, -1)) { /* nil or false? */ 92.638 - lua_pop(L, 1); 92.639 - lua_pushlstring(L, s, e - s); /* keep original text */ 92.640 - } 92.641 - else if (!lua_isstring(L, -1)) 92.642 - luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); 92.643 - luaL_addvalue(b); /* add result to accumulator */ 92.644 -} 92.645 - 92.646 - 92.647 -static int str_gsub (lua_State *L) { 92.648 - size_t srcl; 92.649 - const char *src = luaL_checklstring(L, 1, &srcl); 92.650 - const char *p = luaL_checkstring(L, 2); 92.651 - int tr = lua_type(L, 3); 92.652 - int max_s = luaL_optint(L, 4, srcl+1); 92.653 - int anchor = (*p == '^') ? (p++, 1) : 0; 92.654 - int n = 0; 92.655 - MatchState ms; 92.656 - luaL_Buffer b; 92.657 - luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || 92.658 - tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, 92.659 - "string/function/table expected"); 92.660 - luaL_buffinit(L, &b); 92.661 - ms.L = L; 92.662 - ms.src_init = src; 92.663 - ms.src_end = src+srcl; 92.664 - while (n < max_s) { 92.665 - const char *e; 92.666 - ms.level = 0; 92.667 - e = match(&ms, src, p); 92.668 - if (e) { 92.669 - n++; 92.670 - add_value(&ms, &b, src, e); 92.671 - } 92.672 - if (e && e>src) /* non empty match? */ 92.673 - src = e; /* skip it */ 92.674 - else if (src < ms.src_end) 92.675 - luaL_addchar(&b, *src++); 92.676 - else break; 92.677 - if (anchor) break; 92.678 - } 92.679 - luaL_addlstring(&b, src, ms.src_end-src); 92.680 - luaL_pushresult(&b); 92.681 - lua_pushinteger(L, n); /* number of substitutions */ 92.682 - return 2; 92.683 -} 92.684 - 92.685 -/* }====================================================== */ 92.686 - 92.687 - 92.688 -/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ 92.689 -#define MAX_ITEM 512 92.690 -/* valid flags in a format specification */ 92.691 -#define FLAGS "-+ #0" 92.692 -/* 92.693 -** maximum size of each format specification (such as '%-099.99d') 92.694 -** (+10 accounts for %99.99x plus margin of error) 92.695 -*/ 92.696 -#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10) 92.697 - 92.698 - 92.699 -static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { 92.700 - size_t l; 92.701 - const char *s = luaL_checklstring(L, arg, &l); 92.702 - luaL_addchar(b, '"'); 92.703 - while (l--) { 92.704 - switch (*s) { 92.705 - case '"': case '\\': case '\n': { 92.706 - luaL_addchar(b, '\\'); 92.707 - luaL_addchar(b, *s); 92.708 - break; 92.709 - } 92.710 - case '\r': { 92.711 - luaL_addlstring(b, "\\r", 2); 92.712 - break; 92.713 - } 92.714 - case '\0': { 92.715 - luaL_addlstring(b, "\\000", 4); 92.716 - break; 92.717 - } 92.718 - default: { 92.719 - luaL_addchar(b, *s); 92.720 - break; 92.721 - } 92.722 - } 92.723 - s++; 92.724 - } 92.725 - luaL_addchar(b, '"'); 92.726 -} 92.727 - 92.728 -static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { 92.729 - const char *p = strfrmt; 92.730 - while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ 92.731 - if ((size_t)(p - strfrmt) >= sizeof(FLAGS)) 92.732 - luaL_error(L, "invalid format (repeated flags)"); 92.733 - if (isdigit(uchar(*p))) p++; /* skip width */ 92.734 - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ 92.735 - if (*p == '.') { 92.736 - p++; 92.737 - if (isdigit(uchar(*p))) p++; /* skip precision */ 92.738 - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ 92.739 - } 92.740 - if (isdigit(uchar(*p))) 92.741 - luaL_error(L, "invalid format (width or precision too long)"); 92.742 - *(form++) = '%'; 92.743 - strncpy(form, strfrmt, p - strfrmt + 1); 92.744 - form += p - strfrmt + 1; 92.745 - *form = '\0'; 92.746 - return p; 92.747 -} 92.748 - 92.749 - 92.750 -static void addintlen (char *form) { 92.751 - size_t l = strlen(form); 92.752 - char spec = form[l - 1]; 92.753 - strcpy(form + l - 1, LUA_INTFRMLEN); 92.754 - form[l + sizeof(LUA_INTFRMLEN) - 2] = spec; 92.755 - form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0'; 92.756 -} 92.757 - 92.758 - 92.759 -static int str_format (lua_State *L) { 92.760 - int arg = 1; 92.761 - size_t sfl; 92.762 - const char *strfrmt = luaL_checklstring(L, arg, &sfl); 92.763 - const char *strfrmt_end = strfrmt+sfl; 92.764 - luaL_Buffer b; 92.765 - luaL_buffinit(L, &b); 92.766 - while (strfrmt < strfrmt_end) { 92.767 - if (*strfrmt != L_ESC) 92.768 - luaL_addchar(&b, *strfrmt++); 92.769 - else if (*++strfrmt == L_ESC) 92.770 - luaL_addchar(&b, *strfrmt++); /* %% */ 92.771 - else { /* format item */ 92.772 - char form[MAX_FORMAT]; /* to store the format (`%...') */ 92.773 - char buff[MAX_ITEM]; /* to store the formatted item */ 92.774 - arg++; 92.775 - strfrmt = scanformat(L, strfrmt, form); 92.776 - switch (*strfrmt++) { 92.777 - case 'c': { 92.778 - sprintf(buff, form, (int)luaL_checknumber(L, arg)); 92.779 - break; 92.780 - } 92.781 - case 'd': case 'i': { 92.782 - addintlen(form); 92.783 - sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); 92.784 - break; 92.785 - } 92.786 - case 'o': case 'u': case 'x': case 'X': { 92.787 - addintlen(form); 92.788 - sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); 92.789 - break; 92.790 - } 92.791 - case 'e': case 'E': case 'f': 92.792 - case 'g': case 'G': { 92.793 - sprintf(buff, form, (double)luaL_checknumber(L, arg)); 92.794 - break; 92.795 - } 92.796 - case 'q': { 92.797 - addquoted(L, &b, arg); 92.798 - continue; /* skip the 'addsize' at the end */ 92.799 - } 92.800 - case 's': { 92.801 - size_t l; 92.802 - const char *s = luaL_checklstring(L, arg, &l); 92.803 - if (!strchr(form, '.') && l >= 100) { 92.804 - /* no precision and string is too long to be formatted; 92.805 - keep original string */ 92.806 - lua_pushvalue(L, arg); 92.807 - luaL_addvalue(&b); 92.808 - continue; /* skip the `addsize' at the end */ 92.809 - } 92.810 - else { 92.811 - sprintf(buff, form, s); 92.812 - break; 92.813 - } 92.814 - } 92.815 - default: { /* also treat cases `pnLlh' */ 92.816 - return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " 92.817 - LUA_QL("format"), *(strfrmt - 1)); 92.818 - } 92.819 - } 92.820 - luaL_addlstring(&b, buff, strlen(buff)); 92.821 - } 92.822 - } 92.823 - luaL_pushresult(&b); 92.824 - return 1; 92.825 -} 92.826 - 92.827 - 92.828 -static const luaL_Reg strlib[] = { 92.829 - {"byte", str_byte}, 92.830 - {"char", str_char}, 92.831 - {"dump", str_dump}, 92.832 - {"find", str_find}, 92.833 - {"format", str_format}, 92.834 - {"gfind", gfind_nodef}, 92.835 - {"gmatch", gmatch}, 92.836 - {"gsub", str_gsub}, 92.837 - {"len", str_len}, 92.838 - {"lower", str_lower}, 92.839 - {"match", str_match}, 92.840 - {"rep", str_rep}, 92.841 - {"reverse", str_reverse}, 92.842 - {"sub", str_sub}, 92.843 - {"upper", str_upper}, 92.844 - {NULL, NULL} 92.845 -}; 92.846 - 92.847 - 92.848 -static void createmetatable (lua_State *L) { 92.849 - lua_createtable(L, 0, 1); /* create metatable for strings */ 92.850 - lua_pushliteral(L, ""); /* dummy string */ 92.851 - lua_pushvalue(L, -2); 92.852 - lua_setmetatable(L, -2); /* set string metatable */ 92.853 - lua_pop(L, 1); /* pop dummy string */ 92.854 - lua_pushvalue(L, -2); /* string library... */ 92.855 - lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */ 92.856 - lua_pop(L, 1); /* pop metatable */ 92.857 -} 92.858 - 92.859 - 92.860 -/* 92.861 -** Open string library 92.862 -*/ 92.863 -LUALIB_API int luaopen_string (lua_State *L) { 92.864 - luaL_register(L, LUA_STRLIBNAME, strlib); 92.865 -#if defined(LUA_COMPAT_GFIND) 92.866 - lua_getfield(L, -1, "gmatch"); 92.867 - lua_setfield(L, -2, "gfind"); 92.868 -#endif 92.869 - createmetatable(L); 92.870 - return 1; 92.871 -} 92.872 -
93.1 --- a/src/lua/src/ltable.c Sat Mar 03 11:04:56 2012 -0600 93.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 93.3 @@ -1,588 +0,0 @@ 93.4 -/* 93.5 -** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $ 93.6 -** Lua tables (hash) 93.7 -** See Copyright Notice in lua.h 93.8 -*/ 93.9 - 93.10 - 93.11 -/* 93.12 -** Implementation of tables (aka arrays, objects, or hash tables). 93.13 -** Tables keep its elements in two parts: an array part and a hash part. 93.14 -** Non-negative integer keys are all candidates to be kept in the array 93.15 -** part. The actual size of the array is the largest `n' such that at 93.16 -** least half the slots between 0 and n are in use. 93.17 -** Hash uses a mix of chained scatter table with Brent's variation. 93.18 -** A main invariant of these tables is that, if an element is not 93.19 -** in its main position (i.e. the `original' position that its hash gives 93.20 -** to it), then the colliding element is in its own main position. 93.21 -** Hence even when the load factor reaches 100%, performance remains good. 93.22 -*/ 93.23 - 93.24 -#include <math.h> 93.25 -#include <string.h> 93.26 - 93.27 -#define ltable_c 93.28 -#define LUA_CORE 93.29 - 93.30 -#include "lua.h" 93.31 - 93.32 -#include "ldebug.h" 93.33 -#include "ldo.h" 93.34 -#include "lgc.h" 93.35 -#include "lmem.h" 93.36 -#include "lobject.h" 93.37 -#include "lstate.h" 93.38 -#include "ltable.h" 93.39 - 93.40 - 93.41 -/* 93.42 -** max size of array part is 2^MAXBITS 93.43 -*/ 93.44 -#if LUAI_BITSINT > 26 93.45 -#define MAXBITS 26 93.46 -#else 93.47 -#define MAXBITS (LUAI_BITSINT-2) 93.48 -#endif 93.49 - 93.50 -#define MAXASIZE (1 << MAXBITS) 93.51 - 93.52 - 93.53 -#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) 93.54 - 93.55 -#define hashstr(t,str) hashpow2(t, (str)->tsv.hash) 93.56 -#define hashboolean(t,p) hashpow2(t, p) 93.57 - 93.58 - 93.59 -/* 93.60 -** for some types, it is better to avoid modulus by power of 2, as 93.61 -** they tend to have many 2 factors. 93.62 -*/ 93.63 -#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) 93.64 - 93.65 - 93.66 -#define hashpointer(t,p) hashmod(t, IntPoint(p)) 93.67 - 93.68 - 93.69 -/* 93.70 -** number of ints inside a lua_Number 93.71 -*/ 93.72 -#define numints cast_int(sizeof(lua_Number)/sizeof(int)) 93.73 - 93.74 - 93.75 - 93.76 -#define dummynode (&dummynode_) 93.77 - 93.78 -static const Node dummynode_ = { 93.79 - {{NULL}, LUA_TNIL}, /* value */ 93.80 - {{{NULL}, LUA_TNIL, NULL}} /* key */ 93.81 -}; 93.82 - 93.83 - 93.84 -/* 93.85 -** hash for lua_Numbers 93.86 -*/ 93.87 -static Node *hashnum (const Table *t, lua_Number n) { 93.88 - unsigned int a[numints]; 93.89 - int i; 93.90 - if (luai_numeq(n, 0)) /* avoid problems with -0 */ 93.91 - return gnode(t, 0); 93.92 - memcpy(a, &n, sizeof(a)); 93.93 - for (i = 1; i < numints; i++) a[0] += a[i]; 93.94 - return hashmod(t, a[0]); 93.95 -} 93.96 - 93.97 - 93.98 - 93.99 -/* 93.100 -** returns the `main' position of an element in a table (that is, the index 93.101 -** of its hash value) 93.102 -*/ 93.103 -static Node *mainposition (const Table *t, const TValue *key) { 93.104 - switch (ttype(key)) { 93.105 - case LUA_TNUMBER: 93.106 - return hashnum(t, nvalue(key)); 93.107 - case LUA_TSTRING: 93.108 - return hashstr(t, rawtsvalue(key)); 93.109 - case LUA_TBOOLEAN: 93.110 - return hashboolean(t, bvalue(key)); 93.111 - case LUA_TLIGHTUSERDATA: 93.112 - return hashpointer(t, pvalue(key)); 93.113 - default: 93.114 - return hashpointer(t, gcvalue(key)); 93.115 - } 93.116 -} 93.117 - 93.118 - 93.119 -/* 93.120 -** returns the index for `key' if `key' is an appropriate key to live in 93.121 -** the array part of the table, -1 otherwise. 93.122 -*/ 93.123 -static int arrayindex (const TValue *key) { 93.124 - if (ttisnumber(key)) { 93.125 - lua_Number n = nvalue(key); 93.126 - int k; 93.127 - lua_number2int(k, n); 93.128 - if (luai_numeq(cast_num(k), n)) 93.129 - return k; 93.130 - } 93.131 - return -1; /* `key' did not match some condition */ 93.132 -} 93.133 - 93.134 - 93.135 -/* 93.136 -** returns the index of a `key' for table traversals. First goes all 93.137 -** elements in the array part, then elements in the hash part. The 93.138 -** beginning of a traversal is signalled by -1. 93.139 -*/ 93.140 -static int findindex (lua_State *L, Table *t, StkId key) { 93.141 - int i; 93.142 - if (ttisnil(key)) return -1; /* first iteration */ 93.143 - i = arrayindex(key); 93.144 - if (0 < i && i <= t->sizearray) /* is `key' inside array part? */ 93.145 - return i-1; /* yes; that's the index (corrected to C) */ 93.146 - else { 93.147 - Node *n = mainposition(t, key); 93.148 - do { /* check whether `key' is somewhere in the chain */ 93.149 - /* key may be dead already, but it is ok to use it in `next' */ 93.150 - if (luaO_rawequalObj(key2tval(n), key) || 93.151 - (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) && 93.152 - gcvalue(gkey(n)) == gcvalue(key))) { 93.153 - i = cast_int(n - gnode(t, 0)); /* key index in hash table */ 93.154 - /* hash elements are numbered after array ones */ 93.155 - return i + t->sizearray; 93.156 - } 93.157 - else n = gnext(n); 93.158 - } while (n); 93.159 - luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */ 93.160 - return 0; /* to avoid warnings */ 93.161 - } 93.162 -} 93.163 - 93.164 - 93.165 -int luaH_next (lua_State *L, Table *t, StkId key) { 93.166 - int i = findindex(L, t, key); /* find original element */ 93.167 - for (i++; i < t->sizearray; i++) { /* try first array part */ 93.168 - if (!ttisnil(&t->array[i])) { /* a non-nil value? */ 93.169 - setnvalue(key, cast_num(i+1)); 93.170 - setobj2s(L, key+1, &t->array[i]); 93.171 - return 1; 93.172 - } 93.173 - } 93.174 - for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ 93.175 - if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ 93.176 - setobj2s(L, key, key2tval(gnode(t, i))); 93.177 - setobj2s(L, key+1, gval(gnode(t, i))); 93.178 - return 1; 93.179 - } 93.180 - } 93.181 - return 0; /* no more elements */ 93.182 -} 93.183 - 93.184 - 93.185 -/* 93.186 -** {============================================================= 93.187 -** Rehash 93.188 -** ============================================================== 93.189 -*/ 93.190 - 93.191 - 93.192 -static int computesizes (int nums[], int *narray) { 93.193 - int i; 93.194 - int twotoi; /* 2^i */ 93.195 - int a = 0; /* number of elements smaller than 2^i */ 93.196 - int na = 0; /* number of elements to go to array part */ 93.197 - int n = 0; /* optimal size for array part */ 93.198 - for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { 93.199 - if (nums[i] > 0) { 93.200 - a += nums[i]; 93.201 - if (a > twotoi/2) { /* more than half elements present? */ 93.202 - n = twotoi; /* optimal size (till now) */ 93.203 - na = a; /* all elements smaller than n will go to array part */ 93.204 - } 93.205 - } 93.206 - if (a == *narray) break; /* all elements already counted */ 93.207 - } 93.208 - *narray = n; 93.209 - lua_assert(*narray/2 <= na && na <= *narray); 93.210 - return na; 93.211 -} 93.212 - 93.213 - 93.214 -static int countint (const TValue *key, int *nums) { 93.215 - int k = arrayindex(key); 93.216 - if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ 93.217 - nums[ceillog2(k)]++; /* count as such */ 93.218 - return 1; 93.219 - } 93.220 - else 93.221 - return 0; 93.222 -} 93.223 - 93.224 - 93.225 -static int numusearray (const Table *t, int *nums) { 93.226 - int lg; 93.227 - int ttlg; /* 2^lg */ 93.228 - int ause = 0; /* summation of `nums' */ 93.229 - int i = 1; /* count to traverse all array keys */ 93.230 - for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */ 93.231 - int lc = 0; /* counter */ 93.232 - int lim = ttlg; 93.233 - if (lim > t->sizearray) { 93.234 - lim = t->sizearray; /* adjust upper limit */ 93.235 - if (i > lim) 93.236 - break; /* no more elements to count */ 93.237 - } 93.238 - /* count elements in range (2^(lg-1), 2^lg] */ 93.239 - for (; i <= lim; i++) { 93.240 - if (!ttisnil(&t->array[i-1])) 93.241 - lc++; 93.242 - } 93.243 - nums[lg] += lc; 93.244 - ause += lc; 93.245 - } 93.246 - return ause; 93.247 -} 93.248 - 93.249 - 93.250 -static int numusehash (const Table *t, int *nums, int *pnasize) { 93.251 - int totaluse = 0; /* total number of elements */ 93.252 - int ause = 0; /* summation of `nums' */ 93.253 - int i = sizenode(t); 93.254 - while (i--) { 93.255 - Node *n = &t->node[i]; 93.256 - if (!ttisnil(gval(n))) { 93.257 - ause += countint(key2tval(n), nums); 93.258 - totaluse++; 93.259 - } 93.260 - } 93.261 - *pnasize += ause; 93.262 - return totaluse; 93.263 -} 93.264 - 93.265 - 93.266 -static void setarrayvector (lua_State *L, Table *t, int size) { 93.267 - int i; 93.268 - luaM_reallocvector(L, t->array, t->sizearray, size, TValue); 93.269 - for (i=t->sizearray; i<size; i++) 93.270 - setnilvalue(&t->array[i]); 93.271 - t->sizearray = size; 93.272 -} 93.273 - 93.274 - 93.275 -static void setnodevector (lua_State *L, Table *t, int size) { 93.276 - int lsize; 93.277 - if (size == 0) { /* no elements to hash part? */ 93.278 - t->node = cast(Node *, dummynode); /* use common `dummynode' */ 93.279 - lsize = 0; 93.280 - } 93.281 - else { 93.282 - int i; 93.283 - lsize = ceillog2(size); 93.284 - if (lsize > MAXBITS) 93.285 - luaG_runerror(L, "table overflow"); 93.286 - size = twoto(lsize); 93.287 - t->node = luaM_newvector(L, size, Node); 93.288 - for (i=0; i<size; i++) { 93.289 - Node *n = gnode(t, i); 93.290 - gnext(n) = NULL; 93.291 - setnilvalue(gkey(n)); 93.292 - setnilvalue(gval(n)); 93.293 - } 93.294 - } 93.295 - t->lsizenode = cast_byte(lsize); 93.296 - t->lastfree = gnode(t, size); /* all positions are free */ 93.297 -} 93.298 - 93.299 - 93.300 -static void resize (lua_State *L, Table *t, int nasize, int nhsize) { 93.301 - int i; 93.302 - int oldasize = t->sizearray; 93.303 - int oldhsize = t->lsizenode; 93.304 - Node *nold = t->node; /* save old hash ... */ 93.305 - if (nasize > oldasize) /* array part must grow? */ 93.306 - setarrayvector(L, t, nasize); 93.307 - /* create new hash part with appropriate size */ 93.308 - setnodevector(L, t, nhsize); 93.309 - if (nasize < oldasize) { /* array part must shrink? */ 93.310 - t->sizearray = nasize; 93.311 - /* re-insert elements from vanishing slice */ 93.312 - for (i=nasize; i<oldasize; i++) { 93.313 - if (!ttisnil(&t->array[i])) 93.314 - setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]); 93.315 - } 93.316 - /* shrink array */ 93.317 - luaM_reallocvector(L, t->array, oldasize, nasize, TValue); 93.318 - } 93.319 - /* re-insert elements from hash part */ 93.320 - for (i = twoto(oldhsize) - 1; i >= 0; i--) { 93.321 - Node *old = nold+i; 93.322 - if (!ttisnil(gval(old))) 93.323 - setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); 93.324 - } 93.325 - if (nold != dummynode) 93.326 - luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ 93.327 -} 93.328 - 93.329 - 93.330 -void luaH_resizearray (lua_State *L, Table *t, int nasize) { 93.331 - int nsize = (t->node == dummynode) ? 0 : sizenode(t); 93.332 - resize(L, t, nasize, nsize); 93.333 -} 93.334 - 93.335 - 93.336 -static void rehash (lua_State *L, Table *t, const TValue *ek) { 93.337 - int nasize, na; 93.338 - int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */ 93.339 - int i; 93.340 - int totaluse; 93.341 - for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ 93.342 - nasize = numusearray(t, nums); /* count keys in array part */ 93.343 - totaluse = nasize; /* all those keys are integer keys */ 93.344 - totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */ 93.345 - /* count extra key */ 93.346 - nasize += countint(ek, nums); 93.347 - totaluse++; 93.348 - /* compute new size for array part */ 93.349 - na = computesizes(nums, &nasize); 93.350 - /* resize the table to new computed sizes */ 93.351 - resize(L, t, nasize, totaluse - na); 93.352 -} 93.353 - 93.354 - 93.355 - 93.356 -/* 93.357 -** }============================================================= 93.358 -*/ 93.359 - 93.360 - 93.361 -Table *luaH_new (lua_State *L, int narray, int nhash) { 93.362 - Table *t = luaM_new(L, Table); 93.363 - luaC_link(L, obj2gco(t), LUA_TTABLE); 93.364 - t->metatable = NULL; 93.365 - t->flags = cast_byte(~0); 93.366 - /* temporary values (kept only if some malloc fails) */ 93.367 - t->array = NULL; 93.368 - t->sizearray = 0; 93.369 - t->lsizenode = 0; 93.370 - t->node = cast(Node *, dummynode); 93.371 - setarrayvector(L, t, narray); 93.372 - setnodevector(L, t, nhash); 93.373 - return t; 93.374 -} 93.375 - 93.376 - 93.377 -void luaH_free (lua_State *L, Table *t) { 93.378 - if (t->node != dummynode) 93.379 - luaM_freearray(L, t->node, sizenode(t), Node); 93.380 - luaM_freearray(L, t->array, t->sizearray, TValue); 93.381 - luaM_free(L, t); 93.382 -} 93.383 - 93.384 - 93.385 -static Node *getfreepos (Table *t) { 93.386 - while (t->lastfree-- > t->node) { 93.387 - if (ttisnil(gkey(t->lastfree))) 93.388 - return t->lastfree; 93.389 - } 93.390 - return NULL; /* could not find a free place */ 93.391 -} 93.392 - 93.393 - 93.394 - 93.395 -/* 93.396 -** inserts a new key into a hash table; first, check whether key's main 93.397 -** position is free. If not, check whether colliding node is in its main 93.398 -** position or not: if it is not, move colliding node to an empty place and 93.399 -** put new key in its main position; otherwise (colliding node is in its main 93.400 -** position), new key goes to an empty position. 93.401 -*/ 93.402 -static TValue *newkey (lua_State *L, Table *t, const TValue *key) { 93.403 - Node *mp = mainposition(t, key); 93.404 - if (!ttisnil(gval(mp)) || mp == dummynode) { 93.405 - Node *othern; 93.406 - Node *n = getfreepos(t); /* get a free place */ 93.407 - if (n == NULL) { /* cannot find a free place? */ 93.408 - rehash(L, t, key); /* grow table */ 93.409 - return luaH_set(L, t, key); /* re-insert key into grown table */ 93.410 - } 93.411 - lua_assert(n != dummynode); 93.412 - othern = mainposition(t, key2tval(mp)); 93.413 - if (othern != mp) { /* is colliding node out of its main position? */ 93.414 - /* yes; move colliding node into free position */ 93.415 - while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ 93.416 - gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ 93.417 - *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ 93.418 - gnext(mp) = NULL; /* now `mp' is free */ 93.419 - setnilvalue(gval(mp)); 93.420 - } 93.421 - else { /* colliding node is in its own main position */ 93.422 - /* new node will go into free position */ 93.423 - gnext(n) = gnext(mp); /* chain new position */ 93.424 - gnext(mp) = n; 93.425 - mp = n; 93.426 - } 93.427 - } 93.428 - gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; 93.429 - luaC_barriert(L, t, key); 93.430 - lua_assert(ttisnil(gval(mp))); 93.431 - return gval(mp); 93.432 -} 93.433 - 93.434 - 93.435 -/* 93.436 -** search function for integers 93.437 -*/ 93.438 -const TValue *luaH_getnum (Table *t, int key) { 93.439 - /* (1 <= key && key <= t->sizearray) */ 93.440 - if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) 93.441 - return &t->array[key-1]; 93.442 - else { 93.443 - lua_Number nk = cast_num(key); 93.444 - Node *n = hashnum(t, nk); 93.445 - do { /* check whether `key' is somewhere in the chain */ 93.446 - if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk)) 93.447 - return gval(n); /* that's it */ 93.448 - else n = gnext(n); 93.449 - } while (n); 93.450 - return luaO_nilobject; 93.451 - } 93.452 -} 93.453 - 93.454 - 93.455 -/* 93.456 -** search function for strings 93.457 -*/ 93.458 -const TValue *luaH_getstr (Table *t, TString *key) { 93.459 - Node *n = hashstr(t, key); 93.460 - do { /* check whether `key' is somewhere in the chain */ 93.461 - if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key) 93.462 - return gval(n); /* that's it */ 93.463 - else n = gnext(n); 93.464 - } while (n); 93.465 - return luaO_nilobject; 93.466 -} 93.467 - 93.468 - 93.469 -/* 93.470 -** main search function 93.471 -*/ 93.472 -const TValue *luaH_get (Table *t, const TValue *key) { 93.473 - switch (ttype(key)) { 93.474 - case LUA_TNIL: return luaO_nilobject; 93.475 - case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key)); 93.476 - case LUA_TNUMBER: { 93.477 - int k; 93.478 - lua_Number n = nvalue(key); 93.479 - lua_number2int(k, n); 93.480 - if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */ 93.481 - return luaH_getnum(t, k); /* use specialized version */ 93.482 - /* else go through */ 93.483 - } 93.484 - default: { 93.485 - Node *n = mainposition(t, key); 93.486 - do { /* check whether `key' is somewhere in the chain */ 93.487 - if (luaO_rawequalObj(key2tval(n), key)) 93.488 - return gval(n); /* that's it */ 93.489 - else n = gnext(n); 93.490 - } while (n); 93.491 - return luaO_nilobject; 93.492 - } 93.493 - } 93.494 -} 93.495 - 93.496 - 93.497 -TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { 93.498 - const TValue *p = luaH_get(t, key); 93.499 - t->flags = 0; 93.500 - if (p != luaO_nilobject) 93.501 - return cast(TValue *, p); 93.502 - else { 93.503 - if (ttisnil(key)) luaG_runerror(L, "table index is nil"); 93.504 - else if (ttisnumber(key) && luai_numisnan(nvalue(key))) 93.505 - luaG_runerror(L, "table index is NaN"); 93.506 - return newkey(L, t, key); 93.507 - } 93.508 -} 93.509 - 93.510 - 93.511 -TValue *luaH_setnum (lua_State *L, Table *t, int key) { 93.512 - const TValue *p = luaH_getnum(t, key); 93.513 - if (p != luaO_nilobject) 93.514 - return cast(TValue *, p); 93.515 - else { 93.516 - TValue k; 93.517 - setnvalue(&k, cast_num(key)); 93.518 - return newkey(L, t, &k); 93.519 - } 93.520 -} 93.521 - 93.522 - 93.523 -TValue *luaH_setstr (lua_State *L, Table *t, TString *key) { 93.524 - const TValue *p = luaH_getstr(t, key); 93.525 - if (p != luaO_nilobject) 93.526 - return cast(TValue *, p); 93.527 - else { 93.528 - TValue k; 93.529 - setsvalue(L, &k, key); 93.530 - return newkey(L, t, &k); 93.531 - } 93.532 -} 93.533 - 93.534 - 93.535 -static int unbound_search (Table *t, unsigned int j) { 93.536 - unsigned int i = j; /* i is zero or a present index */ 93.537 - j++; 93.538 - /* find `i' and `j' such that i is present and j is not */ 93.539 - while (!ttisnil(luaH_getnum(t, j))) { 93.540 - i = j; 93.541 - j *= 2; 93.542 - if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ 93.543 - /* table was built with bad purposes: resort to linear search */ 93.544 - i = 1; 93.545 - while (!ttisnil(luaH_getnum(t, i))) i++; 93.546 - return i - 1; 93.547 - } 93.548 - } 93.549 - /* now do a binary search between them */ 93.550 - while (j - i > 1) { 93.551 - unsigned int m = (i+j)/2; 93.552 - if (ttisnil(luaH_getnum(t, m))) j = m; 93.553 - else i = m; 93.554 - } 93.555 - return i; 93.556 -} 93.557 - 93.558 - 93.559 -/* 93.560 -** Try to find a boundary in table `t'. A `boundary' is an integer index 93.561 -** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). 93.562 -*/ 93.563 -int luaH_getn (Table *t) { 93.564 - unsigned int j = t->sizearray; 93.565 - if (j > 0 && ttisnil(&t->array[j - 1])) { 93.566 - /* there is a boundary in the array part: (binary) search for it */ 93.567 - unsigned int i = 0; 93.568 - while (j - i > 1) { 93.569 - unsigned int m = (i+j)/2; 93.570 - if (ttisnil(&t->array[m - 1])) j = m; 93.571 - else i = m; 93.572 - } 93.573 - return i; 93.574 - } 93.575 - /* else must find a boundary in hash part */ 93.576 - else if (t->node == dummynode) /* hash part is empty? */ 93.577 - return j; /* that is easy... */ 93.578 - else return unbound_search(t, j); 93.579 -} 93.580 - 93.581 - 93.582 - 93.583 -#if defined(LUA_DEBUG) 93.584 - 93.585 -Node *luaH_mainposition (const Table *t, const TValue *key) { 93.586 - return mainposition(t, key); 93.587 -} 93.588 - 93.589 -int luaH_isdummy (Node *n) { return n == dummynode; } 93.590 - 93.591 -#endif
94.1 --- a/src/lua/src/ltable.h Sat Mar 03 11:04:56 2012 -0600 94.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 94.3 @@ -1,40 +0,0 @@ 94.4 -/* 94.5 -** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $ 94.6 -** Lua tables (hash) 94.7 -** See Copyright Notice in lua.h 94.8 -*/ 94.9 - 94.10 -#ifndef ltable_h 94.11 -#define ltable_h 94.12 - 94.13 -#include "lobject.h" 94.14 - 94.15 - 94.16 -#define gnode(t,i) (&(t)->node[i]) 94.17 -#define gkey(n) (&(n)->i_key.nk) 94.18 -#define gval(n) (&(n)->i_val) 94.19 -#define gnext(n) ((n)->i_key.nk.next) 94.20 - 94.21 -#define key2tval(n) (&(n)->i_key.tvk) 94.22 - 94.23 - 94.24 -LUAI_FUNC const TValue *luaH_getnum (Table *t, int key); 94.25 -LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key); 94.26 -LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 94.27 -LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key); 94.28 -LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 94.29 -LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 94.30 -LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash); 94.31 -LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 94.32 -LUAI_FUNC void luaH_free (lua_State *L, Table *t); 94.33 -LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 94.34 -LUAI_FUNC int luaH_getn (Table *t); 94.35 - 94.36 - 94.37 -#if defined(LUA_DEBUG) 94.38 -LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 94.39 -LUAI_FUNC int luaH_isdummy (Node *n); 94.40 -#endif 94.41 - 94.42 - 94.43 -#endif
95.1 --- a/src/lua/src/ltablib.c Sat Mar 03 11:04:56 2012 -0600 95.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 95.3 @@ -1,287 +0,0 @@ 95.4 -/* 95.5 -** $Id: ltablib.c,v 1.38.1.3 2008/02/14 16:46:58 roberto Exp $ 95.6 -** Library for Table Manipulation 95.7 -** See Copyright Notice in lua.h 95.8 -*/ 95.9 - 95.10 - 95.11 -#include <stddef.h> 95.12 - 95.13 -#define ltablib_c 95.14 -#define LUA_LIB 95.15 - 95.16 -#include "lua.h" 95.17 - 95.18 -#include "lauxlib.h" 95.19 -#include "lualib.h" 95.20 - 95.21 - 95.22 -#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n)) 95.23 - 95.24 - 95.25 -static int foreachi (lua_State *L) { 95.26 - int i; 95.27 - int n = aux_getn(L, 1); 95.28 - luaL_checktype(L, 2, LUA_TFUNCTION); 95.29 - for (i=1; i <= n; i++) { 95.30 - lua_pushvalue(L, 2); /* function */ 95.31 - lua_pushinteger(L, i); /* 1st argument */ 95.32 - lua_rawgeti(L, 1, i); /* 2nd argument */ 95.33 - lua_call(L, 2, 1); 95.34 - if (!lua_isnil(L, -1)) 95.35 - return 1; 95.36 - lua_pop(L, 1); /* remove nil result */ 95.37 - } 95.38 - return 0; 95.39 -} 95.40 - 95.41 - 95.42 -static int foreach (lua_State *L) { 95.43 - luaL_checktype(L, 1, LUA_TTABLE); 95.44 - luaL_checktype(L, 2, LUA_TFUNCTION); 95.45 - lua_pushnil(L); /* first key */ 95.46 - while (lua_next(L, 1)) { 95.47 - lua_pushvalue(L, 2); /* function */ 95.48 - lua_pushvalue(L, -3); /* key */ 95.49 - lua_pushvalue(L, -3); /* value */ 95.50 - lua_call(L, 2, 1); 95.51 - if (!lua_isnil(L, -1)) 95.52 - return 1; 95.53 - lua_pop(L, 2); /* remove value and result */ 95.54 - } 95.55 - return 0; 95.56 -} 95.57 - 95.58 - 95.59 -static int maxn (lua_State *L) { 95.60 - lua_Number max = 0; 95.61 - luaL_checktype(L, 1, LUA_TTABLE); 95.62 - lua_pushnil(L); /* first key */ 95.63 - while (lua_next(L, 1)) { 95.64 - lua_pop(L, 1); /* remove value */ 95.65 - if (lua_type(L, -1) == LUA_TNUMBER) { 95.66 - lua_Number v = lua_tonumber(L, -1); 95.67 - if (v > max) max = v; 95.68 - } 95.69 - } 95.70 - lua_pushnumber(L, max); 95.71 - return 1; 95.72 -} 95.73 - 95.74 - 95.75 -static int getn (lua_State *L) { 95.76 - lua_pushinteger(L, aux_getn(L, 1)); 95.77 - return 1; 95.78 -} 95.79 - 95.80 - 95.81 -static int setn (lua_State *L) { 95.82 - luaL_checktype(L, 1, LUA_TTABLE); 95.83 -#ifndef luaL_setn 95.84 - luaL_setn(L, 1, luaL_checkint(L, 2)); 95.85 -#else 95.86 - luaL_error(L, LUA_QL("setn") " is obsolete"); 95.87 -#endif 95.88 - lua_pushvalue(L, 1); 95.89 - return 1; 95.90 -} 95.91 - 95.92 - 95.93 -static int tinsert (lua_State *L) { 95.94 - int e = aux_getn(L, 1) + 1; /* first empty element */ 95.95 - int pos; /* where to insert new element */ 95.96 - switch (lua_gettop(L)) { 95.97 - case 2: { /* called with only 2 arguments */ 95.98 - pos = e; /* insert new element at the end */ 95.99 - break; 95.100 - } 95.101 - case 3: { 95.102 - int i; 95.103 - pos = luaL_checkint(L, 2); /* 2nd argument is the position */ 95.104 - if (pos > e) e = pos; /* `grow' array if necessary */ 95.105 - for (i = e; i > pos; i--) { /* move up elements */ 95.106 - lua_rawgeti(L, 1, i-1); 95.107 - lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ 95.108 - } 95.109 - break; 95.110 - } 95.111 - default: { 95.112 - return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); 95.113 - } 95.114 - } 95.115 - luaL_setn(L, 1, e); /* new size */ 95.116 - lua_rawseti(L, 1, pos); /* t[pos] = v */ 95.117 - return 0; 95.118 -} 95.119 - 95.120 - 95.121 -static int tremove (lua_State *L) { 95.122 - int e = aux_getn(L, 1); 95.123 - int pos = luaL_optint(L, 2, e); 95.124 - if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ 95.125 - return 0; /* nothing to remove */ 95.126 - luaL_setn(L, 1, e - 1); /* t.n = n-1 */ 95.127 - lua_rawgeti(L, 1, pos); /* result = t[pos] */ 95.128 - for ( ;pos<e; pos++) { 95.129 - lua_rawgeti(L, 1, pos+1); 95.130 - lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ 95.131 - } 95.132 - lua_pushnil(L); 95.133 - lua_rawseti(L, 1, e); /* t[e] = nil */ 95.134 - return 1; 95.135 -} 95.136 - 95.137 - 95.138 -static void addfield (lua_State *L, luaL_Buffer *b, int i) { 95.139 - lua_rawgeti(L, 1, i); 95.140 - if (!lua_isstring(L, -1)) 95.141 - luaL_error(L, "invalid value (%s) at index %d in table for " 95.142 - LUA_QL("concat"), luaL_typename(L, -1), i); 95.143 - luaL_addvalue(b); 95.144 -} 95.145 - 95.146 - 95.147 -static int tconcat (lua_State *L) { 95.148 - luaL_Buffer b; 95.149 - size_t lsep; 95.150 - int i, last; 95.151 - const char *sep = luaL_optlstring(L, 2, "", &lsep); 95.152 - luaL_checktype(L, 1, LUA_TTABLE); 95.153 - i = luaL_optint(L, 3, 1); 95.154 - last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1)); 95.155 - luaL_buffinit(L, &b); 95.156 - for (; i < last; i++) { 95.157 - addfield(L, &b, i); 95.158 - luaL_addlstring(&b, sep, lsep); 95.159 - } 95.160 - if (i == last) /* add last value (if interval was not empty) */ 95.161 - addfield(L, &b, i); 95.162 - luaL_pushresult(&b); 95.163 - return 1; 95.164 -} 95.165 - 95.166 - 95.167 - 95.168 -/* 95.169 -** {====================================================== 95.170 -** Quicksort 95.171 -** (based on `Algorithms in MODULA-3', Robert Sedgewick; 95.172 -** Addison-Wesley, 1993.) 95.173 -*/ 95.174 - 95.175 - 95.176 -static void set2 (lua_State *L, int i, int j) { 95.177 - lua_rawseti(L, 1, i); 95.178 - lua_rawseti(L, 1, j); 95.179 -} 95.180 - 95.181 -static int sort_comp (lua_State *L, int a, int b) { 95.182 - if (!lua_isnil(L, 2)) { /* function? */ 95.183 - int res; 95.184 - lua_pushvalue(L, 2); 95.185 - lua_pushvalue(L, a-1); /* -1 to compensate function */ 95.186 - lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ 95.187 - lua_call(L, 2, 1); 95.188 - res = lua_toboolean(L, -1); 95.189 - lua_pop(L, 1); 95.190 - return res; 95.191 - } 95.192 - else /* a < b? */ 95.193 - return lua_lessthan(L, a, b); 95.194 -} 95.195 - 95.196 -static void auxsort (lua_State *L, int l, int u) { 95.197 - while (l < u) { /* for tail recursion */ 95.198 - int i, j; 95.199 - /* sort elements a[l], a[(l+u)/2] and a[u] */ 95.200 - lua_rawgeti(L, 1, l); 95.201 - lua_rawgeti(L, 1, u); 95.202 - if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ 95.203 - set2(L, l, u); /* swap a[l] - a[u] */ 95.204 - else 95.205 - lua_pop(L, 2); 95.206 - if (u-l == 1) break; /* only 2 elements */ 95.207 - i = (l+u)/2; 95.208 - lua_rawgeti(L, 1, i); 95.209 - lua_rawgeti(L, 1, l); 95.210 - if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */ 95.211 - set2(L, i, l); 95.212 - else { 95.213 - lua_pop(L, 1); /* remove a[l] */ 95.214 - lua_rawgeti(L, 1, u); 95.215 - if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */ 95.216 - set2(L, i, u); 95.217 - else 95.218 - lua_pop(L, 2); 95.219 - } 95.220 - if (u-l == 2) break; /* only 3 elements */ 95.221 - lua_rawgeti(L, 1, i); /* Pivot */ 95.222 - lua_pushvalue(L, -1); 95.223 - lua_rawgeti(L, 1, u-1); 95.224 - set2(L, i, u-1); 95.225 - /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */ 95.226 - i = l; j = u-1; 95.227 - for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ 95.228 - /* repeat ++i until a[i] >= P */ 95.229 - while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { 95.230 - if (i>u) luaL_error(L, "invalid order function for sorting"); 95.231 - lua_pop(L, 1); /* remove a[i] */ 95.232 - } 95.233 - /* repeat --j until a[j] <= P */ 95.234 - while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { 95.235 - if (j<l) luaL_error(L, "invalid order function for sorting"); 95.236 - lua_pop(L, 1); /* remove a[j] */ 95.237 - } 95.238 - if (j<i) { 95.239 - lua_pop(L, 3); /* pop pivot, a[i], a[j] */ 95.240 - break; 95.241 - } 95.242 - set2(L, i, j); 95.243 - } 95.244 - lua_rawgeti(L, 1, u-1); 95.245 - lua_rawgeti(L, 1, i); 95.246 - set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */ 95.247 - /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ 95.248 - /* adjust so that smaller half is in [j..i] and larger one in [l..u] */ 95.249 - if (i-l < u-i) { 95.250 - j=l; i=i-1; l=i+2; 95.251 - } 95.252 - else { 95.253 - j=i+1; i=u; u=j-2; 95.254 - } 95.255 - auxsort(L, j, i); /* call recursively the smaller one */ 95.256 - } /* repeat the routine for the larger one */ 95.257 -} 95.258 - 95.259 -static int sort (lua_State *L) { 95.260 - int n = aux_getn(L, 1); 95.261 - luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ 95.262 - if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ 95.263 - luaL_checktype(L, 2, LUA_TFUNCTION); 95.264 - lua_settop(L, 2); /* make sure there is two arguments */ 95.265 - auxsort(L, 1, n); 95.266 - return 0; 95.267 -} 95.268 - 95.269 -/* }====================================================== */ 95.270 - 95.271 - 95.272 -static const luaL_Reg tab_funcs[] = { 95.273 - {"concat", tconcat}, 95.274 - {"foreach", foreach}, 95.275 - {"foreachi", foreachi}, 95.276 - {"getn", getn}, 95.277 - {"maxn", maxn}, 95.278 - {"insert", tinsert}, 95.279 - {"remove", tremove}, 95.280 - {"setn", setn}, 95.281 - {"sort", sort}, 95.282 - {NULL, NULL} 95.283 -}; 95.284 - 95.285 - 95.286 -LUALIB_API int luaopen_table (lua_State *L) { 95.287 - luaL_register(L, LUA_TABLIBNAME, tab_funcs); 95.288 - return 1; 95.289 -} 95.290 -
96.1 --- a/src/lua/src/ltm.c Sat Mar 03 11:04:56 2012 -0600 96.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 96.3 @@ -1,75 +0,0 @@ 96.4 -/* 96.5 -** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 96.6 -** Tag methods 96.7 -** See Copyright Notice in lua.h 96.8 -*/ 96.9 - 96.10 - 96.11 -#include <string.h> 96.12 - 96.13 -#define ltm_c 96.14 -#define LUA_CORE 96.15 - 96.16 -#include "lua.h" 96.17 - 96.18 -#include "lobject.h" 96.19 -#include "lstate.h" 96.20 -#include "lstring.h" 96.21 -#include "ltable.h" 96.22 -#include "ltm.h" 96.23 - 96.24 - 96.25 - 96.26 -const char *const luaT_typenames[] = { 96.27 - "nil", "boolean", "userdata", "number", 96.28 - "string", "table", "function", "userdata", "thread", 96.29 - "proto", "upval" 96.30 -}; 96.31 - 96.32 - 96.33 -void luaT_init (lua_State *L) { 96.34 - static const char *const luaT_eventname[] = { /* ORDER TM */ 96.35 - "__index", "__newindex", 96.36 - "__gc", "__mode", "__eq", 96.37 - "__add", "__sub", "__mul", "__div", "__mod", 96.38 - "__pow", "__unm", "__len", "__lt", "__le", 96.39 - "__concat", "__call" 96.40 - }; 96.41 - int i; 96.42 - for (i=0; i<TM_N; i++) { 96.43 - G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); 96.44 - luaS_fix(G(L)->tmname[i]); /* never collect these names */ 96.45 - } 96.46 -} 96.47 - 96.48 - 96.49 -/* 96.50 -** function to be used with macro "fasttm": optimized for absence of 96.51 -** tag methods 96.52 -*/ 96.53 -const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 96.54 - const TValue *tm = luaH_getstr(events, ename); 96.55 - lua_assert(event <= TM_EQ); 96.56 - if (ttisnil(tm)) { /* no tag method? */ 96.57 - events->flags |= cast_byte(1u<<event); /* cache this fact */ 96.58 - return NULL; 96.59 - } 96.60 - else return tm; 96.61 -} 96.62 - 96.63 - 96.64 -const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { 96.65 - Table *mt; 96.66 - switch (ttype(o)) { 96.67 - case LUA_TTABLE: 96.68 - mt = hvalue(o)->metatable; 96.69 - break; 96.70 - case LUA_TUSERDATA: 96.71 - mt = uvalue(o)->metatable; 96.72 - break; 96.73 - default: 96.74 - mt = G(L)->mt[ttype(o)]; 96.75 - } 96.76 - return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 96.77 -} 96.78 -
97.1 --- a/src/lua/src/ltm.h Sat Mar 03 11:04:56 2012 -0600 97.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 97.3 @@ -1,54 +0,0 @@ 97.4 -/* 97.5 -** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $ 97.6 -** Tag methods 97.7 -** See Copyright Notice in lua.h 97.8 -*/ 97.9 - 97.10 -#ifndef ltm_h 97.11 -#define ltm_h 97.12 - 97.13 - 97.14 -#include "lobject.h" 97.15 - 97.16 - 97.17 -/* 97.18 -* WARNING: if you change the order of this enumeration, 97.19 -* grep "ORDER TM" 97.20 -*/ 97.21 -typedef enum { 97.22 - TM_INDEX, 97.23 - TM_NEWINDEX, 97.24 - TM_GC, 97.25 - TM_MODE, 97.26 - TM_EQ, /* last tag method with `fast' access */ 97.27 - TM_ADD, 97.28 - TM_SUB, 97.29 - TM_MUL, 97.30 - TM_DIV, 97.31 - TM_MOD, 97.32 - TM_POW, 97.33 - TM_UNM, 97.34 - TM_LEN, 97.35 - TM_LT, 97.36 - TM_LE, 97.37 - TM_CONCAT, 97.38 - TM_CALL, 97.39 - TM_N /* number of elements in the enum */ 97.40 -} TMS; 97.41 - 97.42 - 97.43 - 97.44 -#define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 97.45 - ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 97.46 - 97.47 -#define fasttm(l,et,e) gfasttm(G(l), et, e) 97.48 - 97.49 -LUAI_DATA const char *const luaT_typenames[]; 97.50 - 97.51 - 97.52 -LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 97.53 -LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 97.54 - TMS event); 97.55 -LUAI_FUNC void luaT_init (lua_State *L); 97.56 - 97.57 -#endif
98.1 --- a/src/lua/src/lua.h Sat Mar 03 11:04:56 2012 -0600 98.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 98.3 @@ -1,388 +0,0 @@ 98.4 -/* 98.5 -** $Id: lua.h,v 1.218.1.5 2008/08/06 13:30:12 roberto Exp $ 98.6 -** Lua - An Extensible Extension Language 98.7 -** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 98.8 -** See Copyright Notice at the end of this file 98.9 -*/ 98.10 - 98.11 - 98.12 -#ifndef lua_h 98.13 -#define lua_h 98.14 - 98.15 -#include <stdarg.h> 98.16 -#include <stddef.h> 98.17 - 98.18 - 98.19 -#include "luaconf.h" 98.20 - 98.21 - 98.22 -#define LUA_VERSION "Lua 5.1" 98.23 -#define LUA_RELEASE "Lua 5.1.4" 98.24 -#define LUA_VERSION_NUM 501 98.25 -#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio" 98.26 -#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" 98.27 - 98.28 - 98.29 -/* mark for precompiled code (`<esc>Lua') */ 98.30 -#define LUA_SIGNATURE "\033Lua" 98.31 - 98.32 -/* option for multiple returns in `lua_pcall' and `lua_call' */ 98.33 -#define LUA_MULTRET (-1) 98.34 - 98.35 - 98.36 -/* 98.37 -** pseudo-indices 98.38 -*/ 98.39 -#define LUA_REGISTRYINDEX (-10000) 98.40 -#define LUA_ENVIRONINDEX (-10001) 98.41 -#define LUA_GLOBALSINDEX (-10002) 98.42 -#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) 98.43 - 98.44 - 98.45 -/* thread status; 0 is OK */ 98.46 -#define LUA_YIELD 1 98.47 -#define LUA_ERRRUN 2 98.48 -#define LUA_ERRSYNTAX 3 98.49 -#define LUA_ERRMEM 4 98.50 -#define LUA_ERRERR 5 98.51 - 98.52 - 98.53 -typedef struct lua_State lua_State; 98.54 - 98.55 -typedef int (*lua_CFunction) (lua_State *L); 98.56 - 98.57 - 98.58 -/* 98.59 -** functions that read/write blocks when loading/dumping Lua chunks 98.60 -*/ 98.61 -typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); 98.62 - 98.63 -typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); 98.64 - 98.65 - 98.66 -/* 98.67 -** prototype for memory-allocation functions 98.68 -*/ 98.69 -typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); 98.70 - 98.71 - 98.72 -/* 98.73 -** basic types 98.74 -*/ 98.75 -#define LUA_TNONE (-1) 98.76 - 98.77 -#define LUA_TNIL 0 98.78 -#define LUA_TBOOLEAN 1 98.79 -#define LUA_TLIGHTUSERDATA 2 98.80 -#define LUA_TNUMBER 3 98.81 -#define LUA_TSTRING 4 98.82 -#define LUA_TTABLE 5 98.83 -#define LUA_TFUNCTION 6 98.84 -#define LUA_TUSERDATA 7 98.85 -#define LUA_TTHREAD 8 98.86 - 98.87 - 98.88 - 98.89 -/* minimum Lua stack available to a C function */ 98.90 -#define LUA_MINSTACK 20 98.91 - 98.92 - 98.93 -/* 98.94 -** generic extra include file 98.95 -*/ 98.96 -#if defined(LUA_USER_H) 98.97 -#include LUA_USER_H 98.98 -#endif 98.99 - 98.100 - 98.101 -/* type of numbers in Lua */ 98.102 -typedef LUA_NUMBER lua_Number; 98.103 - 98.104 - 98.105 -/* type for integer functions */ 98.106 -typedef LUA_INTEGER lua_Integer; 98.107 - 98.108 - 98.109 - 98.110 -/* 98.111 -** state manipulation 98.112 -*/ 98.113 -LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); 98.114 -LUA_API void (lua_close) (lua_State *L); 98.115 -LUA_API lua_State *(lua_newthread) (lua_State *L); 98.116 - 98.117 -LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); 98.118 - 98.119 - 98.120 -/* 98.121 -** basic stack manipulation 98.122 -*/ 98.123 -LUA_API int (lua_gettop) (lua_State *L); 98.124 -LUA_API void (lua_settop) (lua_State *L, int idx); 98.125 -LUA_API void (lua_pushvalue) (lua_State *L, int idx); 98.126 -LUA_API void (lua_remove) (lua_State *L, int idx); 98.127 -LUA_API void (lua_insert) (lua_State *L, int idx); 98.128 -LUA_API void (lua_replace) (lua_State *L, int idx); 98.129 -LUA_API int (lua_checkstack) (lua_State *L, int sz); 98.130 - 98.131 -LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); 98.132 - 98.133 - 98.134 -/* 98.135 -** access functions (stack -> C) 98.136 -*/ 98.137 - 98.138 -LUA_API int (lua_isnumber) (lua_State *L, int idx); 98.139 -LUA_API int (lua_isstring) (lua_State *L, int idx); 98.140 -LUA_API int (lua_iscfunction) (lua_State *L, int idx); 98.141 -LUA_API int (lua_isuserdata) (lua_State *L, int idx); 98.142 -LUA_API int (lua_type) (lua_State *L, int idx); 98.143 -LUA_API const char *(lua_typename) (lua_State *L, int tp); 98.144 - 98.145 -LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2); 98.146 -LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); 98.147 -LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2); 98.148 - 98.149 -LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); 98.150 -LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); 98.151 -LUA_API int (lua_toboolean) (lua_State *L, int idx); 98.152 -LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); 98.153 -LUA_API size_t (lua_objlen) (lua_State *L, int idx); 98.154 -LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); 98.155 -LUA_API void *(lua_touserdata) (lua_State *L, int idx); 98.156 -LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); 98.157 -LUA_API const void *(lua_topointer) (lua_State *L, int idx); 98.158 - 98.159 - 98.160 -/* 98.161 -** push functions (C -> stack) 98.162 -*/ 98.163 -LUA_API void (lua_pushnil) (lua_State *L); 98.164 -LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); 98.165 -LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); 98.166 -LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); 98.167 -LUA_API void (lua_pushstring) (lua_State *L, const char *s); 98.168 -LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, 98.169 - va_list argp); 98.170 -LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); 98.171 -LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); 98.172 -LUA_API void (lua_pushboolean) (lua_State *L, int b); 98.173 -LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); 98.174 -LUA_API int (lua_pushthread) (lua_State *L); 98.175 - 98.176 - 98.177 -/* 98.178 -** get functions (Lua -> stack) 98.179 -*/ 98.180 -LUA_API void (lua_gettable) (lua_State *L, int idx); 98.181 -LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); 98.182 -LUA_API void (lua_rawget) (lua_State *L, int idx); 98.183 -LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); 98.184 -LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); 98.185 -LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); 98.186 -LUA_API int (lua_getmetatable) (lua_State *L, int objindex); 98.187 -LUA_API void (lua_getfenv) (lua_State *L, int idx); 98.188 - 98.189 - 98.190 -/* 98.191 -** set functions (stack -> Lua) 98.192 -*/ 98.193 -LUA_API void (lua_settable) (lua_State *L, int idx); 98.194 -LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); 98.195 -LUA_API void (lua_rawset) (lua_State *L, int idx); 98.196 -LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); 98.197 -LUA_API int (lua_setmetatable) (lua_State *L, int objindex); 98.198 -LUA_API int (lua_setfenv) (lua_State *L, int idx); 98.199 - 98.200 - 98.201 -/* 98.202 -** `load' and `call' functions (load and run Lua code) 98.203 -*/ 98.204 -LUA_API void (lua_call) (lua_State *L, int nargs, int nresults); 98.205 -LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); 98.206 -LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); 98.207 -LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, 98.208 - const char *chunkname); 98.209 - 98.210 -LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); 98.211 - 98.212 - 98.213 -/* 98.214 -** coroutine functions 98.215 -*/ 98.216 -LUA_API int (lua_yield) (lua_State *L, int nresults); 98.217 -LUA_API int (lua_resume) (lua_State *L, int narg); 98.218 -LUA_API int (lua_status) (lua_State *L); 98.219 - 98.220 -/* 98.221 -** garbage-collection function and options 98.222 -*/ 98.223 - 98.224 -#define LUA_GCSTOP 0 98.225 -#define LUA_GCRESTART 1 98.226 -#define LUA_GCCOLLECT 2 98.227 -#define LUA_GCCOUNT 3 98.228 -#define LUA_GCCOUNTB 4 98.229 -#define LUA_GCSTEP 5 98.230 -#define LUA_GCSETPAUSE 6 98.231 -#define LUA_GCSETSTEPMUL 7 98.232 - 98.233 -LUA_API int (lua_gc) (lua_State *L, int what, int data); 98.234 - 98.235 - 98.236 -/* 98.237 -** miscellaneous functions 98.238 -*/ 98.239 - 98.240 -LUA_API int (lua_error) (lua_State *L); 98.241 - 98.242 -LUA_API int (lua_next) (lua_State *L, int idx); 98.243 - 98.244 -LUA_API void (lua_concat) (lua_State *L, int n); 98.245 - 98.246 -LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); 98.247 -LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); 98.248 - 98.249 - 98.250 - 98.251 -/* 98.252 -** =============================================================== 98.253 -** some useful macros 98.254 -** =============================================================== 98.255 -*/ 98.256 - 98.257 -#define lua_pop(L,n) lua_settop(L, -(n)-1) 98.258 - 98.259 -#define lua_newtable(L) lua_createtable(L, 0, 0) 98.260 - 98.261 -#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) 98.262 - 98.263 -#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) 98.264 - 98.265 -#define lua_strlen(L,i) lua_objlen(L, (i)) 98.266 - 98.267 -#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) 98.268 -#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) 98.269 -#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) 98.270 -#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) 98.271 -#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) 98.272 -#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) 98.273 -#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) 98.274 -#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) 98.275 - 98.276 -#define lua_pushliteral(L, s) \ 98.277 - lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) 98.278 - 98.279 -#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) 98.280 -#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) 98.281 - 98.282 -#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) 98.283 - 98.284 - 98.285 - 98.286 -/* 98.287 -** compatibility macros and functions 98.288 -*/ 98.289 - 98.290 -#define lua_open() luaL_newstate() 98.291 - 98.292 -#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) 98.293 - 98.294 -#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0) 98.295 - 98.296 -#define lua_Chunkreader lua_Reader 98.297 -#define lua_Chunkwriter lua_Writer 98.298 - 98.299 - 98.300 -/* hack */ 98.301 -LUA_API void lua_setlevel (lua_State *from, lua_State *to); 98.302 - 98.303 - 98.304 -/* 98.305 -** {====================================================================== 98.306 -** Debug API 98.307 -** ======================================================================= 98.308 -*/ 98.309 - 98.310 - 98.311 -/* 98.312 -** Event codes 98.313 -*/ 98.314 -#define LUA_HOOKCALL 0 98.315 -#define LUA_HOOKRET 1 98.316 -#define LUA_HOOKLINE 2 98.317 -#define LUA_HOOKCOUNT 3 98.318 -#define LUA_HOOKTAILRET 4 98.319 - 98.320 - 98.321 -/* 98.322 -** Event masks 98.323 -*/ 98.324 -#define LUA_MASKCALL (1 << LUA_HOOKCALL) 98.325 -#define LUA_MASKRET (1 << LUA_HOOKRET) 98.326 -#define LUA_MASKLINE (1 << LUA_HOOKLINE) 98.327 -#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) 98.328 - 98.329 -typedef struct lua_Debug lua_Debug; /* activation record */ 98.330 - 98.331 - 98.332 -/* Functions to be called by the debuger in specific events */ 98.333 -typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); 98.334 - 98.335 - 98.336 -LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar); 98.337 -LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); 98.338 -LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); 98.339 -LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); 98.340 -LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n); 98.341 -LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n); 98.342 - 98.343 -LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); 98.344 -LUA_API lua_Hook lua_gethook (lua_State *L); 98.345 -LUA_API int lua_gethookmask (lua_State *L); 98.346 -LUA_API int lua_gethookcount (lua_State *L); 98.347 - 98.348 - 98.349 -struct lua_Debug { 98.350 - int event; 98.351 - const char *name; /* (n) */ 98.352 - const char *namewhat; /* (n) `global', `local', `field', `method' */ 98.353 - const char *what; /* (S) `Lua', `C', `main', `tail' */ 98.354 - const char *source; /* (S) */ 98.355 - int currentline; /* (l) */ 98.356 - int nups; /* (u) number of upvalues */ 98.357 - int linedefined; /* (S) */ 98.358 - int lastlinedefined; /* (S) */ 98.359 - char short_src[LUA_IDSIZE]; /* (S) */ 98.360 - /* private part */ 98.361 - int i_ci; /* active function */ 98.362 -}; 98.363 - 98.364 -/* }====================================================================== */ 98.365 - 98.366 - 98.367 -/****************************************************************************** 98.368 -* Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved. 98.369 -* 98.370 -* Permission is hereby granted, free of charge, to any person obtaining 98.371 -* a copy of this software and associated documentation files (the 98.372 -* "Software"), to deal in the Software without restriction, including 98.373 -* without limitation the rights to use, copy, modify, merge, publish, 98.374 -* distribute, sublicense, and/or sell copies of the Software, and to 98.375 -* permit persons to whom the Software is furnished to do so, subject to 98.376 -* the following conditions: 98.377 -* 98.378 -* The above copyright notice and this permission notice shall be 98.379 -* included in all copies or substantial portions of the Software. 98.380 -* 98.381 -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 98.382 -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 98.383 -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 98.384 -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 98.385 -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 98.386 -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 98.387 -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 98.388 -******************************************************************************/ 98.389 - 98.390 - 98.391 -#endif
99.1 --- a/src/lua/src/luaconf.h Sat Mar 03 11:04:56 2012 -0600 99.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 99.3 @@ -1,763 +0,0 @@ 99.4 -/* 99.5 -** $Id: luaconf.h,v 1.82.1.7 2008/02/11 16:25:08 roberto Exp $ 99.6 -** Configuration file for Lua 99.7 -** See Copyright Notice in lua.h 99.8 -*/ 99.9 - 99.10 - 99.11 -#ifndef lconfig_h 99.12 -#define lconfig_h 99.13 - 99.14 -#include <limits.h> 99.15 -#include <stddef.h> 99.16 - 99.17 - 99.18 -/* 99.19 -** ================================================================== 99.20 -** Search for "@@" to find all configurable definitions. 99.21 -** =================================================================== 99.22 -*/ 99.23 - 99.24 - 99.25 -/* 99.26 -@@ LUA_ANSI controls the use of non-ansi features. 99.27 -** CHANGE it (define it) if you want Lua to avoid the use of any 99.28 -** non-ansi feature or library. 99.29 -*/ 99.30 -#if defined(__STRICT_ANSI__) 99.31 -#define LUA_ANSI 99.32 -#endif 99.33 - 99.34 - 99.35 -#if !defined(LUA_ANSI) && defined(_WIN32) 99.36 -#define LUA_WIN 99.37 -#endif 99.38 - 99.39 -#if defined(LUA_USE_LINUX) 99.40 -#define LUA_USE_POSIX 99.41 -#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ 99.42 -#define LUA_USE_READLINE /* needs some extra libraries */ 99.43 -#endif 99.44 - 99.45 -#if defined(LUA_USE_MACOSX) 99.46 -#define LUA_USE_POSIX 99.47 -#define LUA_DL_DYLD /* does not need extra library */ 99.48 -#endif 99.49 - 99.50 - 99.51 - 99.52 -/* 99.53 -@@ LUA_USE_POSIX includes all functionallity listed as X/Open System 99.54 -@* Interfaces Extension (XSI). 99.55 -** CHANGE it (define it) if your system is XSI compatible. 99.56 -*/ 99.57 -#if defined(LUA_USE_POSIX) 99.58 -#define LUA_USE_MKSTEMP 99.59 -#define LUA_USE_ISATTY 99.60 -#define LUA_USE_POPEN 99.61 -#define LUA_USE_ULONGJMP 99.62 -#endif 99.63 - 99.64 - 99.65 -/* 99.66 -@@ LUA_PATH and LUA_CPATH are the names of the environment variables that 99.67 -@* Lua check to set its paths. 99.68 -@@ LUA_INIT is the name of the environment variable that Lua 99.69 -@* checks for initialization code. 99.70 -** CHANGE them if you want different names. 99.71 -*/ 99.72 -#define LUA_PATH "LUA_PATH" 99.73 -#define LUA_CPATH "LUA_CPATH" 99.74 -#define LUA_INIT "LUA_INIT" 99.75 - 99.76 - 99.77 -/* 99.78 -@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for 99.79 -@* Lua libraries. 99.80 -@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for 99.81 -@* C libraries. 99.82 -** CHANGE them if your machine has a non-conventional directory 99.83 -** hierarchy or if you want to install your libraries in 99.84 -** non-conventional directories. 99.85 -*/ 99.86 -#if defined(_WIN32) 99.87 -/* 99.88 -** In Windows, any exclamation mark ('!') in the path is replaced by the 99.89 -** path of the directory of the executable file of the current process. 99.90 -*/ 99.91 -#define LUA_LDIR "!\\lua\\" 99.92 -#define LUA_CDIR "!\\" 99.93 -#define LUA_PATH_DEFAULT \ 99.94 - ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ 99.95 - LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua" 99.96 -#define LUA_CPATH_DEFAULT \ 99.97 - ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll" 99.98 - 99.99 -#else 99.100 -#define LUA_ROOT "/usr/local/" 99.101 -#define LUA_LDIR LUA_ROOT "share/lua/5.1/" 99.102 -#define LUA_CDIR LUA_ROOT "lib/lua/5.1/" 99.103 -#define LUA_PATH_DEFAULT \ 99.104 - "./?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ 99.105 - LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua" 99.106 -#define LUA_CPATH_DEFAULT \ 99.107 - "./?.so;" LUA_CDIR"?.so;" LUA_CDIR"loadall.so" 99.108 -#endif 99.109 - 99.110 - 99.111 -/* 99.112 -@@ LUA_DIRSEP is the directory separator (for submodules). 99.113 -** CHANGE it if your machine does not use "/" as the directory separator 99.114 -** and is not Windows. (On Windows Lua automatically uses "\".) 99.115 -*/ 99.116 -#if defined(_WIN32) 99.117 -#define LUA_DIRSEP "\\" 99.118 -#else 99.119 -#define LUA_DIRSEP "/" 99.120 -#endif 99.121 - 99.122 - 99.123 -/* 99.124 -@@ LUA_PATHSEP is the character that separates templates in a path. 99.125 -@@ LUA_PATH_MARK is the string that marks the substitution points in a 99.126 -@* template. 99.127 -@@ LUA_EXECDIR in a Windows path is replaced by the executable's 99.128 -@* directory. 99.129 -@@ LUA_IGMARK is a mark to ignore all before it when bulding the 99.130 -@* luaopen_ function name. 99.131 -** CHANGE them if for some reason your system cannot use those 99.132 -** characters. (E.g., if one of those characters is a common character 99.133 -** in file/directory names.) Probably you do not need to change them. 99.134 -*/ 99.135 -#define LUA_PATHSEP ";" 99.136 -#define LUA_PATH_MARK "?" 99.137 -#define LUA_EXECDIR "!" 99.138 -#define LUA_IGMARK "-" 99.139 - 99.140 - 99.141 -/* 99.142 -@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger. 99.143 -** CHANGE that if ptrdiff_t is not adequate on your machine. (On most 99.144 -** machines, ptrdiff_t gives a good choice between int or long.) 99.145 -*/ 99.146 -#define LUA_INTEGER ptrdiff_t 99.147 - 99.148 - 99.149 -/* 99.150 -@@ LUA_API is a mark for all core API functions. 99.151 -@@ LUALIB_API is a mark for all standard library functions. 99.152 -** CHANGE them if you need to define those functions in some special way. 99.153 -** For instance, if you want to create one Windows DLL with the core and 99.154 -** the libraries, you may want to use the following definition (define 99.155 -** LUA_BUILD_AS_DLL to get it). 99.156 -*/ 99.157 -#if defined(LUA_BUILD_AS_DLL) 99.158 - 99.159 -#if defined(LUA_CORE) || defined(LUA_LIB) 99.160 -#define LUA_API __declspec(dllexport) 99.161 -#else 99.162 -#define LUA_API __declspec(dllimport) 99.163 -#endif 99.164 - 99.165 -#else 99.166 - 99.167 -#define LUA_API extern 99.168 - 99.169 -#endif 99.170 - 99.171 -/* more often than not the libs go together with the core */ 99.172 -#define LUALIB_API LUA_API 99.173 - 99.174 - 99.175 -/* 99.176 -@@ LUAI_FUNC is a mark for all extern functions that are not to be 99.177 -@* exported to outside modules. 99.178 -@@ LUAI_DATA is a mark for all extern (const) variables that are not to 99.179 -@* be exported to outside modules. 99.180 -** CHANGE them if you need to mark them in some special way. Elf/gcc 99.181 -** (versions 3.2 and later) mark them as "hidden" to optimize access 99.182 -** when Lua is compiled as a shared library. 99.183 -*/ 99.184 -#if defined(luaall_c) 99.185 -#define LUAI_FUNC static 99.186 -#define LUAI_DATA /* empty */ 99.187 - 99.188 -#elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ 99.189 - defined(__ELF__) 99.190 -#define LUAI_FUNC __attribute__((visibility("hidden"))) extern 99.191 -#define LUAI_DATA LUAI_FUNC 99.192 - 99.193 -#else 99.194 -#define LUAI_FUNC extern 99.195 -#define LUAI_DATA extern 99.196 -#endif 99.197 - 99.198 - 99.199 - 99.200 -/* 99.201 -@@ LUA_QL describes how error messages quote program elements. 99.202 -** CHANGE it if you want a different appearance. 99.203 -*/ 99.204 -#define LUA_QL(x) "'" x "'" 99.205 -#define LUA_QS LUA_QL("%s") 99.206 - 99.207 - 99.208 -/* 99.209 -@@ LUA_IDSIZE gives the maximum size for the description of the source 99.210 -@* of a function in debug information. 99.211 -** CHANGE it if you want a different size. 99.212 -*/ 99.213 -#define LUA_IDSIZE 60 99.214 - 99.215 - 99.216 -/* 99.217 -** {================================================================== 99.218 -** Stand-alone configuration 99.219 -** =================================================================== 99.220 -*/ 99.221 - 99.222 -#if defined(lua_c) || defined(luaall_c) 99.223 - 99.224 -/* 99.225 -@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that 99.226 -@* is, whether we're running lua interactively). 99.227 -** CHANGE it if you have a better definition for non-POSIX/non-Windows 99.228 -** systems. 99.229 -*/ 99.230 -#if defined(LUA_USE_ISATTY) 99.231 -#include <unistd.h> 99.232 -#define lua_stdin_is_tty() isatty(0) 99.233 -#elif defined(LUA_WIN) 99.234 -#include <io.h> 99.235 -#include <stdio.h> 99.236 -#define lua_stdin_is_tty() _isatty(_fileno(stdin)) 99.237 -#else 99.238 -#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ 99.239 -#endif 99.240 - 99.241 - 99.242 -/* 99.243 -@@ LUA_PROMPT is the default prompt used by stand-alone Lua. 99.244 -@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua. 99.245 -** CHANGE them if you want different prompts. (You can also change the 99.246 -** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.) 99.247 -*/ 99.248 -#define LUA_PROMPT "> " 99.249 -#define LUA_PROMPT2 ">> " 99.250 - 99.251 - 99.252 -/* 99.253 -@@ LUA_PROGNAME is the default name for the stand-alone Lua program. 99.254 -** CHANGE it if your stand-alone interpreter has a different name and 99.255 -** your system is not able to detect that name automatically. 99.256 -*/ 99.257 -#define LUA_PROGNAME "lua" 99.258 - 99.259 - 99.260 -/* 99.261 -@@ LUA_MAXINPUT is the maximum length for an input line in the 99.262 -@* stand-alone interpreter. 99.263 -** CHANGE it if you need longer lines. 99.264 -*/ 99.265 -#define LUA_MAXINPUT 512 99.266 - 99.267 - 99.268 -/* 99.269 -@@ lua_readline defines how to show a prompt and then read a line from 99.270 -@* the standard input. 99.271 -@@ lua_saveline defines how to "save" a read line in a "history". 99.272 -@@ lua_freeline defines how to free a line read by lua_readline. 99.273 -** CHANGE them if you want to improve this functionality (e.g., by using 99.274 -** GNU readline and history facilities). 99.275 -*/ 99.276 -#if defined(LUA_USE_READLINE) 99.277 -#include <stdio.h> 99.278 -#include <readline/readline.h> 99.279 -#include <readline/history.h> 99.280 -#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) 99.281 -#define lua_saveline(L,idx) \ 99.282 - if (lua_strlen(L,idx) > 0) /* non-empty line? */ \ 99.283 - add_history(lua_tostring(L, idx)); /* add it to history */ 99.284 -#define lua_freeline(L,b) ((void)L, free(b)) 99.285 -#else 99.286 -#define lua_readline(L,b,p) \ 99.287 - ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ 99.288 - fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ 99.289 -#define lua_saveline(L,idx) { (void)L; (void)idx; } 99.290 -#define lua_freeline(L,b) { (void)L; (void)b; } 99.291 -#endif 99.292 - 99.293 -#endif 99.294 - 99.295 -/* }================================================================== */ 99.296 - 99.297 - 99.298 -/* 99.299 -@@ LUAI_GCPAUSE defines the default pause between garbage-collector cycles 99.300 -@* as a percentage. 99.301 -** CHANGE it if you want the GC to run faster or slower (higher values 99.302 -** mean larger pauses which mean slower collection.) You can also change 99.303 -** this value dynamically. 99.304 -*/ 99.305 -#define LUAI_GCPAUSE 200 /* 200% (wait memory to double before next GC) */ 99.306 - 99.307 - 99.308 -/* 99.309 -@@ LUAI_GCMUL defines the default speed of garbage collection relative to 99.310 -@* memory allocation as a percentage. 99.311 -** CHANGE it if you want to change the granularity of the garbage 99.312 -** collection. (Higher values mean coarser collections. 0 represents 99.313 -** infinity, where each step performs a full collection.) You can also 99.314 -** change this value dynamically. 99.315 -*/ 99.316 -#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ 99.317 - 99.318 - 99.319 - 99.320 -/* 99.321 -@@ LUA_COMPAT_GETN controls compatibility with old getn behavior. 99.322 -** CHANGE it (define it) if you want exact compatibility with the 99.323 -** behavior of setn/getn in Lua 5.0. 99.324 -*/ 99.325 -#undef LUA_COMPAT_GETN 99.326 - 99.327 -/* 99.328 -@@ LUA_COMPAT_LOADLIB controls compatibility about global loadlib. 99.329 -** CHANGE it to undefined as soon as you do not need a global 'loadlib' 99.330 -** function (the function is still available as 'package.loadlib'). 99.331 -*/ 99.332 -#undef LUA_COMPAT_LOADLIB 99.333 - 99.334 -/* 99.335 -@@ LUA_COMPAT_VARARG controls compatibility with old vararg feature. 99.336 -** CHANGE it to undefined as soon as your programs use only '...' to 99.337 -** access vararg parameters (instead of the old 'arg' table). 99.338 -*/ 99.339 -#define LUA_COMPAT_VARARG 99.340 - 99.341 -/* 99.342 -@@ LUA_COMPAT_MOD controls compatibility with old math.mod function. 99.343 -** CHANGE it to undefined as soon as your programs use 'math.fmod' or 99.344 -** the new '%' operator instead of 'math.mod'. 99.345 -*/ 99.346 -#define LUA_COMPAT_MOD 99.347 - 99.348 -/* 99.349 -@@ LUA_COMPAT_LSTR controls compatibility with old long string nesting 99.350 -@* facility. 99.351 -** CHANGE it to 2 if you want the old behaviour, or undefine it to turn 99.352 -** off the advisory error when nesting [[...]]. 99.353 -*/ 99.354 -#define LUA_COMPAT_LSTR 1 99.355 - 99.356 -/* 99.357 -@@ LUA_COMPAT_GFIND controls compatibility with old 'string.gfind' name. 99.358 -** CHANGE it to undefined as soon as you rename 'string.gfind' to 99.359 -** 'string.gmatch'. 99.360 -*/ 99.361 -#define LUA_COMPAT_GFIND 99.362 - 99.363 -/* 99.364 -@@ LUA_COMPAT_OPENLIB controls compatibility with old 'luaL_openlib' 99.365 -@* behavior. 99.366 -** CHANGE it to undefined as soon as you replace to 'luaL_register' 99.367 -** your uses of 'luaL_openlib' 99.368 -*/ 99.369 -#define LUA_COMPAT_OPENLIB 99.370 - 99.371 - 99.372 - 99.373 -/* 99.374 -@@ luai_apicheck is the assert macro used by the Lua-C API. 99.375 -** CHANGE luai_apicheck if you want Lua to perform some checks in the 99.376 -** parameters it gets from API calls. This may slow down the interpreter 99.377 -** a bit, but may be quite useful when debugging C code that interfaces 99.378 -** with Lua. A useful redefinition is to use assert.h. 99.379 -*/ 99.380 -#if defined(LUA_USE_APICHECK) 99.381 -#include <assert.h> 99.382 -#define luai_apicheck(L,o) { (void)L; assert(o); } 99.383 -#else 99.384 -#define luai_apicheck(L,o) { (void)L; } 99.385 -#endif 99.386 - 99.387 - 99.388 -/* 99.389 -@@ LUAI_BITSINT defines the number of bits in an int. 99.390 -** CHANGE here if Lua cannot automatically detect the number of bits of 99.391 -** your machine. Probably you do not need to change this. 99.392 -*/ 99.393 -/* avoid overflows in comparison */ 99.394 -#if INT_MAX-20 < 32760 99.395 -#define LUAI_BITSINT 16 99.396 -#elif INT_MAX > 2147483640L 99.397 -/* int has at least 32 bits */ 99.398 -#define LUAI_BITSINT 32 99.399 -#else 99.400 -#error "you must define LUA_BITSINT with number of bits in an integer" 99.401 -#endif 99.402 - 99.403 - 99.404 -/* 99.405 -@@ LUAI_UINT32 is an unsigned integer with at least 32 bits. 99.406 -@@ LUAI_INT32 is an signed integer with at least 32 bits. 99.407 -@@ LUAI_UMEM is an unsigned integer big enough to count the total 99.408 -@* memory used by Lua. 99.409 -@@ LUAI_MEM is a signed integer big enough to count the total memory 99.410 -@* used by Lua. 99.411 -** CHANGE here if for some weird reason the default definitions are not 99.412 -** good enough for your machine. (The definitions in the 'else' 99.413 -** part always works, but may waste space on machines with 64-bit 99.414 -** longs.) Probably you do not need to change this. 99.415 -*/ 99.416 -#if LUAI_BITSINT >= 32 99.417 -#define LUAI_UINT32 unsigned int 99.418 -#define LUAI_INT32 int 99.419 -#define LUAI_MAXINT32 INT_MAX 99.420 -#define LUAI_UMEM size_t 99.421 -#define LUAI_MEM ptrdiff_t 99.422 -#else 99.423 -/* 16-bit ints */ 99.424 -#define LUAI_UINT32 unsigned long 99.425 -#define LUAI_INT32 long 99.426 -#define LUAI_MAXINT32 LONG_MAX 99.427 -#define LUAI_UMEM unsigned long 99.428 -#define LUAI_MEM long 99.429 -#endif 99.430 - 99.431 - 99.432 -/* 99.433 -@@ LUAI_MAXCALLS limits the number of nested calls. 99.434 -** CHANGE it if you need really deep recursive calls. This limit is 99.435 -** arbitrary; its only purpose is to stop infinite recursion before 99.436 -** exhausting memory. 99.437 -*/ 99.438 -#define LUAI_MAXCALLS 20000 99.439 - 99.440 - 99.441 -/* 99.442 -@@ LUAI_MAXCSTACK limits the number of Lua stack slots that a C function 99.443 -@* can use. 99.444 -** CHANGE it if you need lots of (Lua) stack space for your C 99.445 -** functions. This limit is arbitrary; its only purpose is to stop C 99.446 -** functions to consume unlimited stack space. (must be smaller than 99.447 -** -LUA_REGISTRYINDEX) 99.448 -*/ 99.449 -#define LUAI_MAXCSTACK 8000 99.450 - 99.451 - 99.452 - 99.453 -/* 99.454 -** {================================================================== 99.455 -** CHANGE (to smaller values) the following definitions if your system 99.456 -** has a small C stack. (Or you may want to change them to larger 99.457 -** values if your system has a large C stack and these limits are 99.458 -** too rigid for you.) Some of these constants control the size of 99.459 -** stack-allocated arrays used by the compiler or the interpreter, while 99.460 -** others limit the maximum number of recursive calls that the compiler 99.461 -** or the interpreter can perform. Values too large may cause a C stack 99.462 -** overflow for some forms of deep constructs. 99.463 -** =================================================================== 99.464 -*/ 99.465 - 99.466 - 99.467 -/* 99.468 -@@ LUAI_MAXCCALLS is the maximum depth for nested C calls (short) and 99.469 -@* syntactical nested non-terminals in a program. 99.470 -*/ 99.471 -#define LUAI_MAXCCALLS 200 99.472 - 99.473 - 99.474 -/* 99.475 -@@ LUAI_MAXVARS is the maximum number of local variables per function 99.476 -@* (must be smaller than 250). 99.477 -*/ 99.478 -#define LUAI_MAXVARS 200 99.479 - 99.480 - 99.481 -/* 99.482 -@@ LUAI_MAXUPVALUES is the maximum number of upvalues per function 99.483 -@* (must be smaller than 250). 99.484 -*/ 99.485 -#define LUAI_MAXUPVALUES 60 99.486 - 99.487 - 99.488 -/* 99.489 -@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. 99.490 -*/ 99.491 -#define LUAL_BUFFERSIZE BUFSIZ 99.492 - 99.493 -/* }================================================================== */ 99.494 - 99.495 - 99.496 - 99.497 - 99.498 -/* 99.499 -** {================================================================== 99.500 -@@ LUA_NUMBER is the type of numbers in Lua. 99.501 -** CHANGE the following definitions only if you want to build Lua 99.502 -** with a number type different from double. You may also need to 99.503 -** change lua_number2int & lua_number2integer. 99.504 -** =================================================================== 99.505 -*/ 99.506 - 99.507 -#define LUA_NUMBER_DOUBLE 99.508 -#define LUA_NUMBER double 99.509 - 99.510 -/* 99.511 -@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' 99.512 -@* over a number. 99.513 -*/ 99.514 -#define LUAI_UACNUMBER double 99.515 - 99.516 - 99.517 -/* 99.518 -@@ LUA_NUMBER_SCAN is the format for reading numbers. 99.519 -@@ LUA_NUMBER_FMT is the format for writing numbers. 99.520 -@@ lua_number2str converts a number to a string. 99.521 -@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. 99.522 -@@ lua_str2number converts a string to a number. 99.523 -*/ 99.524 -#define LUA_NUMBER_SCAN "%lf" 99.525 -#define LUA_NUMBER_FMT "%.14g" 99.526 -#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) 99.527 -#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ 99.528 -#define lua_str2number(s,p) strtod((s), (p)) 99.529 - 99.530 - 99.531 -/* 99.532 -@@ The luai_num* macros define the primitive operations over numbers. 99.533 -*/ 99.534 -#if defined(LUA_CORE) 99.535 -#include <math.h> 99.536 -#define luai_numadd(a,b) ((a)+(b)) 99.537 -#define luai_numsub(a,b) ((a)-(b)) 99.538 -#define luai_nummul(a,b) ((a)*(b)) 99.539 -#define luai_numdiv(a,b) ((a)/(b)) 99.540 -#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b)) 99.541 -#define luai_numpow(a,b) (pow(a,b)) 99.542 -#define luai_numunm(a) (-(a)) 99.543 -#define luai_numeq(a,b) ((a)==(b)) 99.544 -#define luai_numlt(a,b) ((a)<(b)) 99.545 -#define luai_numle(a,b) ((a)<=(b)) 99.546 -#define luai_numisnan(a) (!luai_numeq((a), (a))) 99.547 -#endif 99.548 - 99.549 - 99.550 -/* 99.551 -@@ lua_number2int is a macro to convert lua_Number to int. 99.552 -@@ lua_number2integer is a macro to convert lua_Number to lua_Integer. 99.553 -** CHANGE them if you know a faster way to convert a lua_Number to 99.554 -** int (with any rounding method and without throwing errors) in your 99.555 -** system. In Pentium machines, a naive typecast from double to int 99.556 -** in C is extremely slow, so any alternative is worth trying. 99.557 -*/ 99.558 - 99.559 -/* On a Pentium, resort to a trick */ 99.560 -#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \ 99.561 - (defined(__i386) || defined (_M_IX86) || defined(__i386__)) 99.562 - 99.563 -/* On a Microsoft compiler, use assembler */ 99.564 -#if 0//defined(_MSC_VER) // nitsuja: the "using assembly" method doesn't correctly handle integers between 0xFF000000 and and 0xFFFFFFFF, while the other branch does handle it correctly and it's not slower. 99.565 - 99.566 -#define lua_number2int(i,d) __asm fld d __asm fistp i 99.567 -#define lua_number2integer(i,n) lua_number2int(i, n) 99.568 - 99.569 -/* the next trick should work on any Pentium, but sometimes clashes 99.570 - with a DirectX idiosyncrasy */ 99.571 -#else 99.572 - 99.573 -union luai_Cast { double l_d; long l_l; }; 99.574 -#define lua_number2int(i,d) \ 99.575 - { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; } 99.576 -#define lua_number2integer(i,n) lua_number2int(i, n) 99.577 - 99.578 -#endif 99.579 - 99.580 - 99.581 -/* this option always works, but may be slow */ 99.582 -#else 99.583 -#define lua_number2int(i,d) ((i)=(int)(d)) 99.584 -#define lua_number2integer(i,d) ((i)=(lua_Integer)(d)) 99.585 - 99.586 -#endif 99.587 - 99.588 -/* }================================================================== */ 99.589 - 99.590 - 99.591 -/* 99.592 -@@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment. 99.593 -** CHANGE it if your system requires alignments larger than double. (For 99.594 -** instance, if your system supports long doubles and they must be 99.595 -** aligned in 16-byte boundaries, then you should add long double in the 99.596 -** union.) Probably you do not need to change this. 99.597 -*/ 99.598 -#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } 99.599 - 99.600 - 99.601 -/* 99.602 -@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling. 99.603 -** CHANGE them if you prefer to use longjmp/setjmp even with C++ 99.604 -** or if want/don't to use _longjmp/_setjmp instead of regular 99.605 -** longjmp/setjmp. By default, Lua handles errors with exceptions when 99.606 -** compiling as C++ code, with _longjmp/_setjmp when asked to use them, 99.607 -** and with longjmp/setjmp otherwise. 99.608 -*/ 99.609 -#if defined(__cplusplus) 99.610 -/* C++ exceptions */ 99.611 -#define LUAI_THROW(L,c) throw(c) 99.612 -#define LUAI_TRY(L,c,a) try { a } catch(...) \ 99.613 - { if ((c)->status == 0) (c)->status = -1; } 99.614 -#define luai_jmpbuf int /* dummy variable */ 99.615 - 99.616 -#elif defined(LUA_USE_ULONGJMP) 99.617 -/* in Unix, try _longjmp/_setjmp (more efficient) */ 99.618 -#define LUAI_THROW(L,c) _longjmp((c)->b, 1) 99.619 -#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } 99.620 -#define luai_jmpbuf jmp_buf 99.621 - 99.622 -#else 99.623 -/* default handling with long jumps */ 99.624 -#define LUAI_THROW(L,c) longjmp((c)->b, 1) 99.625 -#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } 99.626 -#define luai_jmpbuf jmp_buf 99.627 - 99.628 -#endif 99.629 - 99.630 - 99.631 -/* 99.632 -@@ LUA_MAXCAPTURES is the maximum number of captures that a pattern 99.633 -@* can do during pattern-matching. 99.634 -** CHANGE it if you need more captures. This limit is arbitrary. 99.635 -*/ 99.636 -#define LUA_MAXCAPTURES 32 99.637 - 99.638 - 99.639 -/* 99.640 -@@ lua_tmpnam is the function that the OS library uses to create a 99.641 -@* temporary name. 99.642 -@@ LUA_TMPNAMBUFSIZE is the maximum size of a name created by lua_tmpnam. 99.643 -** CHANGE them if you have an alternative to tmpnam (which is considered 99.644 -** insecure) or if you want the original tmpnam anyway. By default, Lua 99.645 -** uses tmpnam except when POSIX is available, where it uses mkstemp. 99.646 -*/ 99.647 -#if defined(loslib_c) || defined(luaall_c) 99.648 - 99.649 -#if defined(LUA_USE_MKSTEMP) 99.650 -#include <unistd.h> 99.651 -#define LUA_TMPNAMBUFSIZE 32 99.652 -#define lua_tmpnam(b,e) { \ 99.653 - strcpy(b, "/tmp/lua_XXXXXX"); \ 99.654 - e = mkstemp(b); \ 99.655 - if (e != -1) close(e); \ 99.656 - e = (e == -1); } 99.657 - 99.658 -#else 99.659 -#define LUA_TMPNAMBUFSIZE L_tmpnam 99.660 -#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } 99.661 -#endif 99.662 - 99.663 -#endif 99.664 - 99.665 - 99.666 -/* 99.667 -@@ lua_popen spawns a new process connected to the current one through 99.668 -@* the file streams. 99.669 -** CHANGE it if you have a way to implement it in your system. 99.670 -*/ 99.671 -#if defined(LUA_USE_POPEN) 99.672 - 99.673 -#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) 99.674 -#define lua_pclose(L,file) ((void)L, (pclose(file) != -1)) 99.675 - 99.676 -#elif defined(LUA_WIN) 99.677 - 99.678 -#define lua_popen(L,c,m) ((void)L, _popen(c,m)) 99.679 -#define lua_pclose(L,file) ((void)L, (_pclose(file) != -1)) 99.680 - 99.681 -#else 99.682 - 99.683 -#define lua_popen(L,c,m) ((void)((void)c, m), \ 99.684 - luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0) 99.685 -#define lua_pclose(L,file) ((void)((void)L, file), 0) 99.686 - 99.687 -#endif 99.688 - 99.689 -/* 99.690 -@@ LUA_DL_* define which dynamic-library system Lua should use. 99.691 -** CHANGE here if Lua has problems choosing the appropriate 99.692 -** dynamic-library system for your platform (either Windows' DLL, Mac's 99.693 -** dyld, or Unix's dlopen). If your system is some kind of Unix, there 99.694 -** is a good chance that it has dlopen, so LUA_DL_DLOPEN will work for 99.695 -** it. To use dlopen you also need to adapt the src/Makefile (probably 99.696 -** adding -ldl to the linker options), so Lua does not select it 99.697 -** automatically. (When you change the makefile to add -ldl, you must 99.698 -** also add -DLUA_USE_DLOPEN.) 99.699 -** If you do not want any kind of dynamic library, undefine all these 99.700 -** options. 99.701 -** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD. 99.702 -*/ 99.703 -#if defined(LUA_USE_DLOPEN) 99.704 -#define LUA_DL_DLOPEN 99.705 -#endif 99.706 - 99.707 -#if defined(LUA_WIN) 99.708 -#define LUA_DL_DLL 99.709 -#endif 99.710 - 99.711 - 99.712 -/* 99.713 -@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State 99.714 -@* (the data goes just *before* the lua_State pointer). 99.715 -** CHANGE (define) this if you really need that. This value must be 99.716 -** a multiple of the maximum alignment required for your machine. 99.717 -*/ 99.718 -#define LUAI_EXTRASPACE 0 99.719 - 99.720 - 99.721 -/* 99.722 -@@ luai_userstate* allow user-specific actions on threads. 99.723 -** CHANGE them if you defined LUAI_EXTRASPACE and need to do something 99.724 -** extra when a thread is created/deleted/resumed/yielded. 99.725 -*/ 99.726 -#define luai_userstateopen(L) ((void)L) 99.727 -#define luai_userstateclose(L) ((void)L) 99.728 -#define luai_userstatethread(L,L1) ((void)L) 99.729 -#define luai_userstatefree(L) ((void)L) 99.730 -#define luai_userstateresume(L,n) ((void)L) 99.731 -#define luai_userstateyield(L,n) ((void)L) 99.732 - 99.733 - 99.734 -/* 99.735 -@@ LUA_INTFRMLEN is the length modifier for integer conversions 99.736 -@* in 'string.format'. 99.737 -@@ LUA_INTFRM_T is the integer type correspoding to the previous length 99.738 -@* modifier. 99.739 -** CHANGE them if your system supports long long or does not support long. 99.740 -*/ 99.741 - 99.742 -#if defined(LUA_USELONGLONG) 99.743 - 99.744 -#define LUA_INTFRMLEN "ll" 99.745 -#define LUA_INTFRM_T long long 99.746 - 99.747 -#else 99.748 - 99.749 -#define LUA_INTFRMLEN "l" 99.750 -#define LUA_INTFRM_T long 99.751 - 99.752 -#endif 99.753 - 99.754 - 99.755 - 99.756 -/* =================================================================== */ 99.757 - 99.758 -/* 99.759 -** Local configuration. You can use this space to add your redefinitions 99.760 -** without modifying the main part of the file. 99.761 -*/ 99.762 - 99.763 - 99.764 - 99.765 -#endif 99.766 -
100.1 --- a/src/lua/src/lualib.h Sat Mar 03 11:04:56 2012 -0600 100.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 100.3 @@ -1,53 +0,0 @@ 100.4 -/* 100.5 -** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $ 100.6 -** Lua standard libraries 100.7 -** See Copyright Notice in lua.h 100.8 -*/ 100.9 - 100.10 - 100.11 -#ifndef lualib_h 100.12 -#define lualib_h 100.13 - 100.14 -#include "lua.h" 100.15 - 100.16 - 100.17 -/* Key to file-handle type */ 100.18 -#define LUA_FILEHANDLE "FILE*" 100.19 - 100.20 - 100.21 -#define LUA_COLIBNAME "coroutine" 100.22 -LUALIB_API int (luaopen_base) (lua_State *L); 100.23 - 100.24 -#define LUA_TABLIBNAME "table" 100.25 -LUALIB_API int (luaopen_table) (lua_State *L); 100.26 - 100.27 -#define LUA_IOLIBNAME "io" 100.28 -LUALIB_API int (luaopen_io) (lua_State *L); 100.29 - 100.30 -#define LUA_OSLIBNAME "os" 100.31 -LUALIB_API int (luaopen_os) (lua_State *L); 100.32 - 100.33 -#define LUA_STRLIBNAME "string" 100.34 -LUALIB_API int (luaopen_string) (lua_State *L); 100.35 - 100.36 -#define LUA_MATHLIBNAME "math" 100.37 -LUALIB_API int (luaopen_math) (lua_State *L); 100.38 - 100.39 -#define LUA_DBLIBNAME "debug" 100.40 -LUALIB_API int (luaopen_debug) (lua_State *L); 100.41 - 100.42 -#define LUA_LOADLIBNAME "package" 100.43 -LUALIB_API int (luaopen_package) (lua_State *L); 100.44 - 100.45 - 100.46 -/* open all previous libraries */ 100.47 -LUALIB_API void (luaL_openlibs) (lua_State *L); 100.48 - 100.49 - 100.50 - 100.51 -#ifndef lua_assert 100.52 -#define lua_assert(x) ((void)0) 100.53 -#endif 100.54 - 100.55 - 100.56 -#endif
101.1 --- a/src/lua/src/lundump.c Sat Mar 03 11:04:56 2012 -0600 101.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 101.3 @@ -1,227 +0,0 @@ 101.4 -/* 101.5 -** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $ 101.6 -** load precompiled Lua chunks 101.7 -** See Copyright Notice in lua.h 101.8 -*/ 101.9 - 101.10 -#include <string.h> 101.11 - 101.12 -#define lundump_c 101.13 -#define LUA_CORE 101.14 - 101.15 -#include "lua.h" 101.16 - 101.17 -#include "ldebug.h" 101.18 -#include "ldo.h" 101.19 -#include "lfunc.h" 101.20 -#include "lmem.h" 101.21 -#include "lobject.h" 101.22 -#include "lstring.h" 101.23 -#include "lundump.h" 101.24 -#include "lzio.h" 101.25 - 101.26 -typedef struct { 101.27 - lua_State* L; 101.28 - ZIO* Z; 101.29 - Mbuffer* b; 101.30 - const char* name; 101.31 -} LoadState; 101.32 - 101.33 -#ifdef LUAC_TRUST_BINARIES 101.34 -#define IF(c,s) 101.35 -#define error(S,s) 101.36 -#else 101.37 -#define IF(c,s) if (c) error(S,s) 101.38 - 101.39 -static void error(LoadState* S, const char* why) 101.40 -{ 101.41 - luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); 101.42 - luaD_throw(S->L,LUA_ERRSYNTAX); 101.43 -} 101.44 -#endif 101.45 - 101.46 -#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 101.47 -#define LoadByte(S) (lu_byte)LoadChar(S) 101.48 -#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 101.49 -#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 101.50 - 101.51 -static void LoadBlock(LoadState* S, void* b, size_t size) 101.52 -{ 101.53 - size_t r=luaZ_read(S->Z,b,size); 101.54 - IF (r!=0, "unexpected end"); 101.55 -} 101.56 - 101.57 -static int LoadChar(LoadState* S) 101.58 -{ 101.59 - char x; 101.60 - LoadVar(S,x); 101.61 - return x; 101.62 -} 101.63 - 101.64 -static int LoadInt(LoadState* S) 101.65 -{ 101.66 - int x; 101.67 - LoadVar(S,x); 101.68 - IF (x<0, "bad integer"); 101.69 - return x; 101.70 -} 101.71 - 101.72 -static lua_Number LoadNumber(LoadState* S) 101.73 -{ 101.74 - lua_Number x; 101.75 - LoadVar(S,x); 101.76 - return x; 101.77 -} 101.78 - 101.79 -static TString* LoadString(LoadState* S) 101.80 -{ 101.81 - size_t size; 101.82 - LoadVar(S,size); 101.83 - if (size==0) 101.84 - return NULL; 101.85 - else 101.86 - { 101.87 - char* s=luaZ_openspace(S->L,S->b,size); 101.88 - LoadBlock(S,s,size); 101.89 - return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 101.90 - } 101.91 -} 101.92 - 101.93 -static void LoadCode(LoadState* S, Proto* f) 101.94 -{ 101.95 - int n=LoadInt(S); 101.96 - f->code=luaM_newvector(S->L,n,Instruction); 101.97 - f->sizecode=n; 101.98 - LoadVector(S,f->code,n,sizeof(Instruction)); 101.99 -} 101.100 - 101.101 -static Proto* LoadFunction(LoadState* S, TString* p); 101.102 - 101.103 -static void LoadConstants(LoadState* S, Proto* f) 101.104 -{ 101.105 - int i,n; 101.106 - n=LoadInt(S); 101.107 - f->k=luaM_newvector(S->L,n,TValue); 101.108 - f->sizek=n; 101.109 - for (i=0; i<n; i++) setnilvalue(&f->k[i]); 101.110 - for (i=0; i<n; i++) 101.111 - { 101.112 - TValue* o=&f->k[i]; 101.113 - int t=LoadChar(S); 101.114 - switch (t) 101.115 - { 101.116 - case LUA_TNIL: 101.117 - setnilvalue(o); 101.118 - break; 101.119 - case LUA_TBOOLEAN: 101.120 - setbvalue(o,LoadChar(S)!=0); 101.121 - break; 101.122 - case LUA_TNUMBER: 101.123 - setnvalue(o,LoadNumber(S)); 101.124 - break; 101.125 - case LUA_TSTRING: 101.126 - setsvalue2n(S->L,o,LoadString(S)); 101.127 - break; 101.128 - default: 101.129 - error(S,"bad constant"); 101.130 - break; 101.131 - } 101.132 - } 101.133 - n=LoadInt(S); 101.134 - f->p=luaM_newvector(S->L,n,Proto*); 101.135 - f->sizep=n; 101.136 - for (i=0; i<n; i++) f->p[i]=NULL; 101.137 - for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source); 101.138 -} 101.139 - 101.140 -static void LoadDebug(LoadState* S, Proto* f) 101.141 -{ 101.142 - int i,n; 101.143 - n=LoadInt(S); 101.144 - f->lineinfo=luaM_newvector(S->L,n,int); 101.145 - f->sizelineinfo=n; 101.146 - LoadVector(S,f->lineinfo,n,sizeof(int)); 101.147 - n=LoadInt(S); 101.148 - f->locvars=luaM_newvector(S->L,n,LocVar); 101.149 - f->sizelocvars=n; 101.150 - for (i=0; i<n; i++) f->locvars[i].varname=NULL; 101.151 - for (i=0; i<n; i++) 101.152 - { 101.153 - f->locvars[i].varname=LoadString(S); 101.154 - f->locvars[i].startpc=LoadInt(S); 101.155 - f->locvars[i].endpc=LoadInt(S); 101.156 - } 101.157 - n=LoadInt(S); 101.158 - f->upvalues=luaM_newvector(S->L,n,TString*); 101.159 - f->sizeupvalues=n; 101.160 - for (i=0; i<n; i++) f->upvalues[i]=NULL; 101.161 - for (i=0; i<n; i++) f->upvalues[i]=LoadString(S); 101.162 -} 101.163 - 101.164 -static Proto* LoadFunction(LoadState* S, TString* p) 101.165 -{ 101.166 - Proto* f; 101.167 - if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); 101.168 - f=luaF_newproto(S->L); 101.169 - setptvalue2s(S->L,S->L->top,f); incr_top(S->L); 101.170 - f->source=LoadString(S); if (f->source==NULL) f->source=p; 101.171 - f->linedefined=LoadInt(S); 101.172 - f->lastlinedefined=LoadInt(S); 101.173 - f->nups=LoadByte(S); 101.174 - f->numparams=LoadByte(S); 101.175 - f->is_vararg=LoadByte(S); 101.176 - f->maxstacksize=LoadByte(S); 101.177 - LoadCode(S,f); 101.178 - LoadConstants(S,f); 101.179 - LoadDebug(S,f); 101.180 - IF (!luaG_checkcode(f), "bad code"); 101.181 - S->L->top--; 101.182 - S->L->nCcalls--; 101.183 - return f; 101.184 -} 101.185 - 101.186 -static void LoadHeader(LoadState* S) 101.187 -{ 101.188 - char h[LUAC_HEADERSIZE]; 101.189 - char s[LUAC_HEADERSIZE]; 101.190 - luaU_header(h); 101.191 - LoadBlock(S,s,LUAC_HEADERSIZE); 101.192 - IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); 101.193 -} 101.194 - 101.195 -/* 101.196 -** load precompiled chunk 101.197 -*/ 101.198 -Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 101.199 -{ 101.200 - LoadState S; 101.201 - if (*name=='@' || *name=='=') 101.202 - S.name=name+1; 101.203 - else if (*name==LUA_SIGNATURE[0]) 101.204 - S.name="binary string"; 101.205 - else 101.206 - S.name=name; 101.207 - S.L=L; 101.208 - S.Z=Z; 101.209 - S.b=buff; 101.210 - LoadHeader(&S); 101.211 - return LoadFunction(&S,luaS_newliteral(L,"=?")); 101.212 -} 101.213 - 101.214 -/* 101.215 -* make header 101.216 -*/ 101.217 -void luaU_header (char* h) 101.218 -{ 101.219 - int x=1; 101.220 - memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); 101.221 - h+=sizeof(LUA_SIGNATURE)-1; 101.222 - *h++=(char)LUAC_VERSION; 101.223 - *h++=(char)LUAC_FORMAT; 101.224 - *h++=(char)*(char*)&x; /* endianness */ 101.225 - *h++=(char)sizeof(int); 101.226 - *h++=(char)sizeof(size_t); 101.227 - *h++=(char)sizeof(Instruction); 101.228 - *h++=(char)sizeof(lua_Number); 101.229 - *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ 101.230 -}
102.1 --- a/src/lua/src/lundump.h Sat Mar 03 11:04:56 2012 -0600 102.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 102.3 @@ -1,36 +0,0 @@ 102.4 -/* 102.5 -** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 102.6 -** load precompiled Lua chunks 102.7 -** See Copyright Notice in lua.h 102.8 -*/ 102.9 - 102.10 -#ifndef lundump_h 102.11 -#define lundump_h 102.12 - 102.13 -#include "lobject.h" 102.14 -#include "lzio.h" 102.15 - 102.16 -/* load one chunk; from lundump.c */ 102.17 -LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 102.18 - 102.19 -/* make header; from lundump.c */ 102.20 -LUAI_FUNC void luaU_header (char* h); 102.21 - 102.22 -/* dump one chunk; from ldump.c */ 102.23 -LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 102.24 - 102.25 -#ifdef luac_c 102.26 -/* print one chunk; from print.c */ 102.27 -LUAI_FUNC void luaU_print (const Proto* f, int full); 102.28 -#endif 102.29 - 102.30 -/* for header of binary files -- this is Lua 5.1 */ 102.31 -#define LUAC_VERSION 0x51 102.32 - 102.33 -/* for header of binary files -- this is the official format */ 102.34 -#define LUAC_FORMAT 0 102.35 - 102.36 -/* size of header of binary files */ 102.37 -#define LUAC_HEADERSIZE 12 102.38 - 102.39 -#endif
103.1 --- a/src/lua/src/lvm.c Sat Mar 03 11:04:56 2012 -0600 103.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 103.3 @@ -1,763 +0,0 @@ 103.4 -/* 103.5 -** $Id: lvm.c,v 2.63.1.3 2007/12/28 15:32:23 roberto Exp $ 103.6 -** Lua virtual machine 103.7 -** See Copyright Notice in lua.h 103.8 -*/ 103.9 - 103.10 - 103.11 -#include <stdio.h> 103.12 -#include <stdlib.h> 103.13 -#include <string.h> 103.14 - 103.15 -#define lvm_c 103.16 -#define LUA_CORE 103.17 - 103.18 -#include "lua.h" 103.19 - 103.20 -#include "ldebug.h" 103.21 -#include "ldo.h" 103.22 -#include "lfunc.h" 103.23 -#include "lgc.h" 103.24 -#include "lobject.h" 103.25 -#include "lopcodes.h" 103.26 -#include "lstate.h" 103.27 -#include "lstring.h" 103.28 -#include "ltable.h" 103.29 -#include "ltm.h" 103.30 -#include "lvm.h" 103.31 - 103.32 - 103.33 - 103.34 -/* limit for table tag-method chains (to avoid loops) */ 103.35 -#define MAXTAGLOOP 100 103.36 - 103.37 - 103.38 -const TValue *luaV_tonumber (const TValue *obj, TValue *n) { 103.39 - lua_Number num; 103.40 - if (ttisnumber(obj)) return obj; 103.41 - if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) { 103.42 - setnvalue(n, num); 103.43 - return n; 103.44 - } 103.45 - else 103.46 - return NULL; 103.47 -} 103.48 - 103.49 - 103.50 -int luaV_tostring (lua_State *L, StkId obj) { 103.51 - if (!ttisnumber(obj)) 103.52 - return 0; 103.53 - else { 103.54 - char s[LUAI_MAXNUMBER2STR]; 103.55 - lua_Number n = nvalue(obj); 103.56 - lua_number2str(s, n); 103.57 - setsvalue2s(L, obj, luaS_new(L, s)); 103.58 - return 1; 103.59 - } 103.60 -} 103.61 - 103.62 - 103.63 -static void traceexec (lua_State *L, const Instruction *pc) { 103.64 - lu_byte mask = L->hookmask; 103.65 - const Instruction *oldpc = L->savedpc; 103.66 - L->savedpc = pc; 103.67 - if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) { 103.68 - resethookcount(L); 103.69 - luaD_callhook(L, LUA_HOOKCOUNT, -1); 103.70 - } 103.71 - if (mask & LUA_MASKLINE) { 103.72 - Proto *p = ci_func(L->ci)->l.p; 103.73 - int npc = pcRel(pc, p); 103.74 - int newline = getline(p, npc); 103.75 - /* call linehook when enter a new function, when jump back (loop), 103.76 - or when enter a new line */ 103.77 - if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p))) 103.78 - luaD_callhook(L, LUA_HOOKLINE, newline); 103.79 - } 103.80 -} 103.81 - 103.82 - 103.83 -static void callTMres (lua_State *L, StkId res, const TValue *f, 103.84 - const TValue *p1, const TValue *p2) { 103.85 - ptrdiff_t result = savestack(L, res); 103.86 - setobj2s(L, L->top, f); /* push function */ 103.87 - setobj2s(L, L->top+1, p1); /* 1st argument */ 103.88 - setobj2s(L, L->top+2, p2); /* 2nd argument */ 103.89 - luaD_checkstack(L, 3); 103.90 - L->top += 3; 103.91 - luaD_call(L, L->top - 3, 1); 103.92 - res = restorestack(L, result); 103.93 - L->top--; 103.94 - setobjs2s(L, res, L->top); 103.95 -} 103.96 - 103.97 - 103.98 - 103.99 -static void callTM (lua_State *L, const TValue *f, const TValue *p1, 103.100 - const TValue *p2, const TValue *p3) { 103.101 - setobj2s(L, L->top, f); /* push function */ 103.102 - setobj2s(L, L->top+1, p1); /* 1st argument */ 103.103 - setobj2s(L, L->top+2, p2); /* 2nd argument */ 103.104 - setobj2s(L, L->top+3, p3); /* 3th argument */ 103.105 - luaD_checkstack(L, 4); 103.106 - L->top += 4; 103.107 - luaD_call(L, L->top - 4, 0); 103.108 -} 103.109 - 103.110 - 103.111 -void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { 103.112 - int loop; 103.113 - for (loop = 0; loop < MAXTAGLOOP; loop++) { 103.114 - const TValue *tm; 103.115 - if (ttistable(t)) { /* `t' is a table? */ 103.116 - Table *h = hvalue(t); 103.117 - const TValue *res = luaH_get(h, key); /* do a primitive get */ 103.118 - if (!ttisnil(res) || /* result is no nil? */ 103.119 - (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ 103.120 - setobj2s(L, val, res); 103.121 - return; 103.122 - } 103.123 - /* else will try the tag method */ 103.124 - } 103.125 - else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) 103.126 - luaG_typeerror(L, t, "index"); 103.127 - if (ttisfunction(tm)) { 103.128 - callTMres(L, val, tm, t, key); 103.129 - return; 103.130 - } 103.131 - t = tm; /* else repeat with `tm' */ 103.132 - } 103.133 - luaG_runerror(L, "loop in gettable"); 103.134 -} 103.135 - 103.136 - 103.137 -void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { 103.138 - int loop; 103.139 - for (loop = 0; loop < MAXTAGLOOP; loop++) { 103.140 - const TValue *tm; 103.141 - if (ttistable(t)) { /* `t' is a table? */ 103.142 - Table *h = hvalue(t); 103.143 - TValue *oldval = luaH_set(L, h, key); /* do a primitive set */ 103.144 - if (!ttisnil(oldval) || /* result is no nil? */ 103.145 - (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */ 103.146 - setobj2t(L, oldval, val); 103.147 - luaC_barriert(L, h, val); 103.148 - return; 103.149 - } 103.150 - /* else will try the tag method */ 103.151 - } 103.152 - else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) 103.153 - luaG_typeerror(L, t, "index"); 103.154 - if (ttisfunction(tm)) { 103.155 - callTM(L, tm, t, key, val); 103.156 - return; 103.157 - } 103.158 - t = tm; /* else repeat with `tm' */ 103.159 - } 103.160 - luaG_runerror(L, "loop in settable"); 103.161 -} 103.162 - 103.163 - 103.164 -static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2, 103.165 - StkId res, TMS event) { 103.166 - const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ 103.167 - if (ttisnil(tm)) 103.168 - tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 103.169 - if (ttisnil(tm)) return 0; 103.170 - callTMres(L, res, tm, p1, p2); 103.171 - return 1; 103.172 -} 103.173 - 103.174 - 103.175 -static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2, 103.176 - TMS event) { 103.177 - const TValue *tm1 = fasttm(L, mt1, event); 103.178 - const TValue *tm2; 103.179 - if (tm1 == NULL) return NULL; /* no metamethod */ 103.180 - if (mt1 == mt2) return tm1; /* same metatables => same metamethods */ 103.181 - tm2 = fasttm(L, mt2, event); 103.182 - if (tm2 == NULL) return NULL; /* no metamethod */ 103.183 - if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */ 103.184 - return tm1; 103.185 - return NULL; 103.186 -} 103.187 - 103.188 - 103.189 -static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2, 103.190 - TMS event) { 103.191 - const TValue *tm1 = luaT_gettmbyobj(L, p1, event); 103.192 - const TValue *tm2; 103.193 - if (ttisnil(tm1)) return -1; /* no metamethod? */ 103.194 - tm2 = luaT_gettmbyobj(L, p2, event); 103.195 - if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */ 103.196 - return -1; 103.197 - callTMres(L, L->top, tm1, p1, p2); 103.198 - return !l_isfalse(L->top); 103.199 -} 103.200 - 103.201 - 103.202 -static int l_strcmp (const TString *ls, const TString *rs) { 103.203 - const char *l = getstr(ls); 103.204 - size_t ll = ls->tsv.len; 103.205 - const char *r = getstr(rs); 103.206 - size_t lr = rs->tsv.len; 103.207 - for (;;) { 103.208 - int temp = strcoll(l, r); 103.209 - if (temp != 0) return temp; 103.210 - else { /* strings are equal up to a `\0' */ 103.211 - size_t len = strlen(l); /* index of first `\0' in both strings */ 103.212 - if (len == lr) /* r is finished? */ 103.213 - return (len == ll) ? 0 : 1; 103.214 - else if (len == ll) /* l is finished? */ 103.215 - return -1; /* l is smaller than r (because r is not finished) */ 103.216 - /* both strings longer than `len'; go on comparing (after the `\0') */ 103.217 - len++; 103.218 - l += len; ll -= len; r += len; lr -= len; 103.219 - } 103.220 - } 103.221 -} 103.222 - 103.223 - 103.224 -int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { 103.225 - int res; 103.226 - if (ttype(l) != ttype(r)) 103.227 - return luaG_ordererror(L, l, r); 103.228 - else if (ttisnumber(l)) 103.229 - return luai_numlt(nvalue(l), nvalue(r)); 103.230 - else if (ttisstring(l)) 103.231 - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; 103.232 - else if ((res = call_orderTM(L, l, r, TM_LT)) != -1) 103.233 - return res; 103.234 - return luaG_ordererror(L, l, r); 103.235 -} 103.236 - 103.237 - 103.238 -static int lessequal (lua_State *L, const TValue *l, const TValue *r) { 103.239 - int res; 103.240 - if (ttype(l) != ttype(r)) 103.241 - return luaG_ordererror(L, l, r); 103.242 - else if (ttisnumber(l)) 103.243 - return luai_numle(nvalue(l), nvalue(r)); 103.244 - else if (ttisstring(l)) 103.245 - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; 103.246 - else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */ 103.247 - return res; 103.248 - else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */ 103.249 - return !res; 103.250 - return luaG_ordererror(L, l, r); 103.251 -} 103.252 - 103.253 - 103.254 -int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) { 103.255 - const TValue *tm; 103.256 - lua_assert(ttype(t1) == ttype(t2)); 103.257 - switch (ttype(t1)) { 103.258 - case LUA_TNIL: return 1; 103.259 - case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2)); 103.260 - case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ 103.261 - case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); 103.262 - case LUA_TUSERDATA: { 103.263 - if (uvalue(t1) == uvalue(t2)) return 1; 103.264 - tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, 103.265 - TM_EQ); 103.266 - break; /* will try TM */ 103.267 - } 103.268 - case LUA_TTABLE: { 103.269 - if (hvalue(t1) == hvalue(t2)) return 1; 103.270 - tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ); 103.271 - break; /* will try TM */ 103.272 - } 103.273 - default: return gcvalue(t1) == gcvalue(t2); 103.274 - } 103.275 - if (tm == NULL) return 0; /* no TM? */ 103.276 - callTMres(L, L->top, tm, t1, t2); /* call TM */ 103.277 - return !l_isfalse(L->top); 103.278 -} 103.279 - 103.280 - 103.281 -void luaV_concat (lua_State *L, int total, int last) { 103.282 - do { 103.283 - StkId top = L->base + last + 1; 103.284 - int n = 2; /* number of elements handled in this pass (at least 2) */ 103.285 - if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { 103.286 - if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) 103.287 - luaG_concaterror(L, top-2, top-1); 103.288 - } else if (tsvalue(top-1)->len == 0) /* second op is empty? */ 103.289 - (void)tostring(L, top - 2); /* result is first op (as string) */ 103.290 - else { 103.291 - /* at least two string values; get as many as possible */ 103.292 - size_t tl = tsvalue(top-1)->len; 103.293 - char *buffer; 103.294 - int i; 103.295 - /* collect total length */ 103.296 - for (n = 1; n < total && tostring(L, top-n-1); n++) { 103.297 - size_t l = tsvalue(top-n-1)->len; 103.298 - if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow"); 103.299 - tl += l; 103.300 - } 103.301 - buffer = luaZ_openspace(L, &G(L)->buff, tl); 103.302 - tl = 0; 103.303 - for (i=n; i>0; i--) { /* concat all strings */ 103.304 - size_t l = tsvalue(top-i)->len; 103.305 - memcpy(buffer+tl, svalue(top-i), l); 103.306 - tl += l; 103.307 - } 103.308 - setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); 103.309 - } 103.310 - total -= n-1; /* got `n' strings to create 1 new */ 103.311 - last -= n-1; 103.312 - } while (total > 1); /* repeat until only 1 result left */ 103.313 -} 103.314 - 103.315 - 103.316 -static void Arith (lua_State *L, StkId ra, const TValue *rb, 103.317 - const TValue *rc, TMS op) { 103.318 - TValue tempb, tempc; 103.319 - const TValue *b, *c; 103.320 - if ((b = luaV_tonumber(rb, &tempb)) != NULL && 103.321 - (c = luaV_tonumber(rc, &tempc)) != NULL) { 103.322 - lua_Number nb = nvalue(b), nc = nvalue(c); 103.323 - switch (op) { 103.324 - case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break; 103.325 - case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break; 103.326 - case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break; 103.327 - case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break; 103.328 - case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break; 103.329 - case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break; 103.330 - case TM_UNM: setnvalue(ra, luai_numunm(nb)); break; 103.331 - default: lua_assert(0); break; 103.332 - } 103.333 - } 103.334 - else if (!call_binTM(L, rb, rc, ra, op)) 103.335 - luaG_aritherror(L, rb, rc); 103.336 -} 103.337 - 103.338 - 103.339 - 103.340 -/* 103.341 -** some macros for common tasks in `luaV_execute' 103.342 -*/ 103.343 - 103.344 -#define runtime_check(L, c) { if (!(c)) break; } 103.345 - 103.346 -#define RA(i) (base+GETARG_A(i)) 103.347 -/* to be used after possible stack reallocation */ 103.348 -#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) 103.349 -#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) 103.350 -#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ 103.351 - ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) 103.352 -#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ 103.353 - ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) 103.354 -#define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i)) 103.355 - 103.356 - 103.357 -#define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);} 103.358 - 103.359 - 103.360 -#define Protect(x) { L->savedpc = pc; {x;}; base = L->base; } 103.361 - 103.362 - 103.363 -#define arith_op(op,tm) { \ 103.364 - TValue *rb = RKB(i); \ 103.365 - TValue *rc = RKC(i); \ 103.366 - if (ttisnumber(rb) && ttisnumber(rc)) { \ 103.367 - lua_Number nb = nvalue(rb), nc = nvalue(rc); \ 103.368 - setnvalue(ra, op(nb, nc)); \ 103.369 - } \ 103.370 - else \ 103.371 - Protect(Arith(L, ra, rb, rc, tm)); \ 103.372 - } 103.373 - 103.374 - 103.375 - 103.376 -void luaV_execute (lua_State *L, int nexeccalls) { 103.377 - LClosure *cl; 103.378 - StkId base; 103.379 - TValue *k; 103.380 - const Instruction *pc; 103.381 - reentry: /* entry point */ 103.382 - lua_assert(isLua(L->ci)); 103.383 - pc = L->savedpc; 103.384 - cl = &clvalue(L->ci->func)->l; 103.385 - base = L->base; 103.386 - k = cl->p->k; 103.387 - /* main loop of interpreter */ 103.388 - for (;;) { 103.389 - const Instruction i = *pc++; 103.390 - StkId ra; 103.391 - if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && 103.392 - (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { 103.393 - traceexec(L, pc); 103.394 - if (L->status == LUA_YIELD) { /* did hook yield? */ 103.395 - L->savedpc = pc - 1; 103.396 - return; 103.397 - } 103.398 - base = L->base; 103.399 - } 103.400 - /* warning!! several calls may realloc the stack and invalidate `ra' */ 103.401 - ra = RA(i); 103.402 - lua_assert(base == L->base && L->base == L->ci->base); 103.403 - lua_assert(base <= L->top && L->top <= L->stack + L->stacksize); 103.404 - lua_assert(L->top == L->ci->top || luaG_checkopenop(i)); 103.405 - switch (GET_OPCODE(i)) { 103.406 - case OP_MOVE: { 103.407 - setobjs2s(L, ra, RB(i)); 103.408 - continue; 103.409 - } 103.410 - case OP_LOADK: { 103.411 - setobj2s(L, ra, KBx(i)); 103.412 - continue; 103.413 - } 103.414 - case OP_LOADBOOL: { 103.415 - setbvalue(ra, GETARG_B(i)); 103.416 - if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ 103.417 - continue; 103.418 - } 103.419 - case OP_LOADNIL: { 103.420 - TValue *rb = RB(i); 103.421 - do { 103.422 - setnilvalue(rb--); 103.423 - } while (rb >= ra); 103.424 - continue; 103.425 - } 103.426 - case OP_GETUPVAL: { 103.427 - int b = GETARG_B(i); 103.428 - setobj2s(L, ra, cl->upvals[b]->v); 103.429 - continue; 103.430 - } 103.431 - case OP_GETGLOBAL: { 103.432 - TValue g; 103.433 - TValue *rb = KBx(i); 103.434 - sethvalue(L, &g, cl->env); 103.435 - lua_assert(ttisstring(rb)); 103.436 - Protect(luaV_gettable(L, &g, rb, ra)); 103.437 - continue; 103.438 - } 103.439 - case OP_GETTABLE: { 103.440 - Protect(luaV_gettable(L, RB(i), RKC(i), ra)); 103.441 - continue; 103.442 - } 103.443 - case OP_SETGLOBAL: { 103.444 - TValue g; 103.445 - sethvalue(L, &g, cl->env); 103.446 - lua_assert(ttisstring(KBx(i))); 103.447 - Protect(luaV_settable(L, &g, KBx(i), ra)); 103.448 - continue; 103.449 - } 103.450 - case OP_SETUPVAL: { 103.451 - UpVal *uv = cl->upvals[GETARG_B(i)]; 103.452 - setobj(L, uv->v, ra); 103.453 - luaC_barrier(L, uv, ra); 103.454 - continue; 103.455 - } 103.456 - case OP_SETTABLE: { 103.457 - Protect(luaV_settable(L, ra, RKB(i), RKC(i))); 103.458 - continue; 103.459 - } 103.460 - case OP_NEWTABLE: { 103.461 - int b = GETARG_B(i); 103.462 - int c = GETARG_C(i); 103.463 - sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); 103.464 - Protect(luaC_checkGC(L)); 103.465 - continue; 103.466 - } 103.467 - case OP_SELF: { 103.468 - StkId rb = RB(i); 103.469 - setobjs2s(L, ra+1, rb); 103.470 - Protect(luaV_gettable(L, rb, RKC(i), ra)); 103.471 - continue; 103.472 - } 103.473 - case OP_ADD: { 103.474 - arith_op(luai_numadd, TM_ADD); 103.475 - continue; 103.476 - } 103.477 - case OP_SUB: { 103.478 - arith_op(luai_numsub, TM_SUB); 103.479 - continue; 103.480 - } 103.481 - case OP_MUL: { 103.482 - arith_op(luai_nummul, TM_MUL); 103.483 - continue; 103.484 - } 103.485 - case OP_DIV: { 103.486 - arith_op(luai_numdiv, TM_DIV); 103.487 - continue; 103.488 - } 103.489 - case OP_MOD: { 103.490 - arith_op(luai_nummod, TM_MOD); 103.491 - continue; 103.492 - } 103.493 - case OP_POW: { 103.494 - arith_op(luai_numpow, TM_POW); 103.495 - continue; 103.496 - } 103.497 - case OP_UNM: { 103.498 - TValue *rb = RB(i); 103.499 - if (ttisnumber(rb)) { 103.500 - lua_Number nb = nvalue(rb); 103.501 - setnvalue(ra, luai_numunm(nb)); 103.502 - } 103.503 - else { 103.504 - Protect(Arith(L, ra, rb, rb, TM_UNM)); 103.505 - } 103.506 - continue; 103.507 - } 103.508 - case OP_NOT: { 103.509 - int res = l_isfalse(RB(i)); /* next assignment may change this value */ 103.510 - setbvalue(ra, res); 103.511 - continue; 103.512 - } 103.513 - case OP_LEN: { 103.514 - const TValue *rb = RB(i); 103.515 - switch (ttype(rb)) { 103.516 - case LUA_TTABLE: { 103.517 - setnvalue(ra, cast_num(luaH_getn(hvalue(rb)))); 103.518 - break; 103.519 - } 103.520 - case LUA_TSTRING: { 103.521 - setnvalue(ra, cast_num(tsvalue(rb)->len)); 103.522 - break; 103.523 - } 103.524 - default: { /* try metamethod */ 103.525 - Protect( 103.526 - if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN)) 103.527 - luaG_typeerror(L, rb, "get length of"); 103.528 - ) 103.529 - } 103.530 - } 103.531 - continue; 103.532 - } 103.533 - case OP_CONCAT: { 103.534 - int b = GETARG_B(i); 103.535 - int c = GETARG_C(i); 103.536 - Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L)); 103.537 - setobjs2s(L, RA(i), base+b); 103.538 - continue; 103.539 - } 103.540 - case OP_JMP: { 103.541 - dojump(L, pc, GETARG_sBx(i)); 103.542 - continue; 103.543 - } 103.544 - case OP_EQ: { 103.545 - TValue *rb = RKB(i); 103.546 - TValue *rc = RKC(i); 103.547 - Protect( 103.548 - if (equalobj(L, rb, rc) == GETARG_A(i)) 103.549 - dojump(L, pc, GETARG_sBx(*pc)); 103.550 - ) 103.551 - pc++; 103.552 - continue; 103.553 - } 103.554 - case OP_LT: { 103.555 - Protect( 103.556 - if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i)) 103.557 - dojump(L, pc, GETARG_sBx(*pc)); 103.558 - ) 103.559 - pc++; 103.560 - continue; 103.561 - } 103.562 - case OP_LE: { 103.563 - Protect( 103.564 - if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i)) 103.565 - dojump(L, pc, GETARG_sBx(*pc)); 103.566 - ) 103.567 - pc++; 103.568 - continue; 103.569 - } 103.570 - case OP_TEST: { 103.571 - if (l_isfalse(ra) != GETARG_C(i)) 103.572 - dojump(L, pc, GETARG_sBx(*pc)); 103.573 - pc++; 103.574 - continue; 103.575 - } 103.576 - case OP_TESTSET: { 103.577 - TValue *rb = RB(i); 103.578 - if (l_isfalse(rb) != GETARG_C(i)) { 103.579 - setobjs2s(L, ra, rb); 103.580 - dojump(L, pc, GETARG_sBx(*pc)); 103.581 - } 103.582 - pc++; 103.583 - continue; 103.584 - } 103.585 - case OP_CALL: { 103.586 - int b = GETARG_B(i); 103.587 - int nresults = GETARG_C(i) - 1; 103.588 - if (b != 0) L->top = ra+b; /* else previous instruction set top */ 103.589 - L->savedpc = pc; 103.590 - switch (luaD_precall(L, ra, nresults)) { 103.591 - case PCRLUA: { 103.592 - nexeccalls++; 103.593 - goto reentry; /* restart luaV_execute over new Lua function */ 103.594 - } 103.595 - case PCRC: { 103.596 - /* it was a C function (`precall' called it); adjust results */ 103.597 - if (nresults >= 0) L->top = L->ci->top; 103.598 - base = L->base; 103.599 - continue; 103.600 - } 103.601 - default: { 103.602 - return; /* yield */ 103.603 - } 103.604 - } 103.605 - } 103.606 - case OP_TAILCALL: { 103.607 - int b = GETARG_B(i); 103.608 - if (b != 0) L->top = ra+b; /* else previous instruction set top */ 103.609 - L->savedpc = pc; 103.610 - lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); 103.611 - switch (luaD_precall(L, ra, LUA_MULTRET)) { 103.612 - case PCRLUA: { 103.613 - /* tail call: put new frame in place of previous one */ 103.614 - CallInfo *ci = L->ci - 1; /* previous frame */ 103.615 - int aux; 103.616 - StkId func = ci->func; 103.617 - StkId pfunc = (ci+1)->func; /* previous function index */ 103.618 - if (L->openupval) luaF_close(L, ci->base); 103.619 - L->base = ci->base = ci->func + ((ci+1)->base - pfunc); 103.620 - for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */ 103.621 - setobjs2s(L, func+aux, pfunc+aux); 103.622 - ci->top = L->top = func+aux; /* correct top */ 103.623 - lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize); 103.624 - ci->savedpc = L->savedpc; 103.625 - ci->tailcalls++; /* one more call lost */ 103.626 - L->ci--; /* remove new frame */ 103.627 - goto reentry; 103.628 - } 103.629 - case PCRC: { /* it was a C function (`precall' called it) */ 103.630 - base = L->base; 103.631 - continue; 103.632 - } 103.633 - default: { 103.634 - return; /* yield */ 103.635 - } 103.636 - } 103.637 - } 103.638 - case OP_RETURN: { 103.639 - int b = GETARG_B(i); 103.640 - if (b != 0) L->top = ra+b-1; 103.641 - if (L->openupval) luaF_close(L, base); 103.642 - L->savedpc = pc; 103.643 - b = luaD_poscall(L, ra); 103.644 - if (--nexeccalls == 0) /* was previous function running `here'? */ 103.645 - return; /* no: return */ 103.646 - else { /* yes: continue its execution */ 103.647 - if (b) L->top = L->ci->top; 103.648 - lua_assert(isLua(L->ci)); 103.649 - lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL); 103.650 - goto reentry; 103.651 - } 103.652 - } 103.653 - case OP_FORLOOP: { 103.654 - lua_Number step = nvalue(ra+2); 103.655 - lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */ 103.656 - lua_Number limit = nvalue(ra+1); 103.657 - if (luai_numlt(0, step) ? luai_numle(idx, limit) 103.658 - : luai_numle(limit, idx)) { 103.659 - dojump(L, pc, GETARG_sBx(i)); /* jump back */ 103.660 - setnvalue(ra, idx); /* update internal index... */ 103.661 - setnvalue(ra+3, idx); /* ...and external index */ 103.662 - } 103.663 - continue; 103.664 - } 103.665 - case OP_FORPREP: { 103.666 - const TValue *init = ra; 103.667 - const TValue *plimit = ra+1; 103.668 - const TValue *pstep = ra+2; 103.669 - L->savedpc = pc; /* next steps may throw errors */ 103.670 - if (!tonumber(init, ra)) 103.671 - luaG_runerror(L, LUA_QL("for") " initial value must be a number"); 103.672 - else if (!tonumber(plimit, ra+1)) 103.673 - luaG_runerror(L, LUA_QL("for") " limit must be a number"); 103.674 - else if (!tonumber(pstep, ra+2)) 103.675 - luaG_runerror(L, LUA_QL("for") " step must be a number"); 103.676 - setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep))); 103.677 - dojump(L, pc, GETARG_sBx(i)); 103.678 - continue; 103.679 - } 103.680 - case OP_TFORLOOP: { 103.681 - StkId cb = ra + 3; /* call base */ 103.682 - setobjs2s(L, cb+2, ra+2); 103.683 - setobjs2s(L, cb+1, ra+1); 103.684 - setobjs2s(L, cb, ra); 103.685 - L->top = cb+3; /* func. + 2 args (state and index) */ 103.686 - Protect(luaD_call(L, cb, GETARG_C(i))); 103.687 - L->top = L->ci->top; 103.688 - cb = RA(i) + 3; /* previous call may change the stack */ 103.689 - if (!ttisnil(cb)) { /* continue loop? */ 103.690 - setobjs2s(L, cb-1, cb); /* save control variable */ 103.691 - dojump(L, pc, GETARG_sBx(*pc)); /* jump back */ 103.692 - } 103.693 - pc++; 103.694 - continue; 103.695 - } 103.696 - case OP_SETLIST: { 103.697 - int n = GETARG_B(i); 103.698 - int c = GETARG_C(i); 103.699 - int last; 103.700 - Table *h; 103.701 - if (n == 0) { 103.702 - n = cast_int(L->top - ra) - 1; 103.703 - L->top = L->ci->top; 103.704 - } 103.705 - if (c == 0) c = cast_int(*pc++); 103.706 - runtime_check(L, ttistable(ra)); 103.707 - h = hvalue(ra); 103.708 - last = ((c-1)*LFIELDS_PER_FLUSH) + n; 103.709 - if (last > h->sizearray) /* needs more space? */ 103.710 - luaH_resizearray(L, h, last); /* pre-alloc it at once */ 103.711 - for (; n > 0; n--) { 103.712 - TValue *val = ra+n; 103.713 - setobj2t(L, luaH_setnum(L, h, last--), val); 103.714 - luaC_barriert(L, h, val); 103.715 - } 103.716 - continue; 103.717 - } 103.718 - case OP_CLOSE: { 103.719 - luaF_close(L, ra); 103.720 - continue; 103.721 - } 103.722 - case OP_CLOSURE: { 103.723 - Proto *p; 103.724 - Closure *ncl; 103.725 - int nup, j; 103.726 - p = cl->p->p[GETARG_Bx(i)]; 103.727 - nup = p->nups; 103.728 - ncl = luaF_newLclosure(L, nup, cl->env); 103.729 - ncl->l.p = p; 103.730 - for (j=0; j<nup; j++, pc++) { 103.731 - if (GET_OPCODE(*pc) == OP_GETUPVAL) 103.732 - ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)]; 103.733 - else { 103.734 - lua_assert(GET_OPCODE(*pc) == OP_MOVE); 103.735 - ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc)); 103.736 - } 103.737 - } 103.738 - setclvalue(L, ra, ncl); 103.739 - Protect(luaC_checkGC(L)); 103.740 - continue; 103.741 - } 103.742 - case OP_VARARG: { 103.743 - int b = GETARG_B(i) - 1; 103.744 - int j; 103.745 - CallInfo *ci = L->ci; 103.746 - int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1; 103.747 - if (b == LUA_MULTRET) { 103.748 - Protect(luaD_checkstack(L, n)); 103.749 - ra = RA(i); /* previous call may change the stack */ 103.750 - b = n; 103.751 - L->top = ra + n; 103.752 - } 103.753 - for (j = 0; j < b; j++) { 103.754 - if (j < n) { 103.755 - setobjs2s(L, ra + j, ci->base - n + j); 103.756 - } 103.757 - else { 103.758 - setnilvalue(ra + j); 103.759 - } 103.760 - } 103.761 - continue; 103.762 - } 103.763 - } 103.764 - } 103.765 -} 103.766 -
104.1 --- a/src/lua/src/lvm.h Sat Mar 03 11:04:56 2012 -0600 104.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 104.3 @@ -1,36 +0,0 @@ 104.4 -/* 104.5 -** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $ 104.6 -** Lua virtual machine 104.7 -** See Copyright Notice in lua.h 104.8 -*/ 104.9 - 104.10 -#ifndef lvm_h 104.11 -#define lvm_h 104.12 - 104.13 - 104.14 -#include "ldo.h" 104.15 -#include "lobject.h" 104.16 -#include "ltm.h" 104.17 - 104.18 - 104.19 -#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) 104.20 - 104.21 -#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \ 104.22 - (((o) = luaV_tonumber(o,n)) != NULL)) 104.23 - 104.24 -#define equalobj(L,o1,o2) \ 104.25 - (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) 104.26 - 104.27 - 104.28 -LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 104.29 -LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); 104.30 -LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 104.31 -LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 104.32 -LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 104.33 - StkId val); 104.34 -LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 104.35 - StkId val); 104.36 -LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls); 104.37 -LUAI_FUNC void luaV_concat (lua_State *L, int total, int last); 104.38 - 104.39 -#endif
105.1 --- a/src/lua/src/lzio.c Sat Mar 03 11:04:56 2012 -0600 105.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 105.3 @@ -1,82 +0,0 @@ 105.4 -/* 105.5 -** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 105.6 -** a generic input stream interface 105.7 -** See Copyright Notice in lua.h 105.8 -*/ 105.9 - 105.10 - 105.11 -#include <string.h> 105.12 - 105.13 -#define lzio_c 105.14 -#define LUA_CORE 105.15 - 105.16 -#include "lua.h" 105.17 - 105.18 -#include "llimits.h" 105.19 -#include "lmem.h" 105.20 -#include "lstate.h" 105.21 -#include "lzio.h" 105.22 - 105.23 - 105.24 -int luaZ_fill (ZIO *z) { 105.25 - size_t size; 105.26 - lua_State *L = z->L; 105.27 - const char *buff; 105.28 - lua_unlock(L); 105.29 - buff = z->reader(L, z->data, &size); 105.30 - lua_lock(L); 105.31 - if (buff == NULL || size == 0) return EOZ; 105.32 - z->n = size - 1; 105.33 - z->p = buff; 105.34 - return char2int(*(z->p++)); 105.35 -} 105.36 - 105.37 - 105.38 -int luaZ_lookahead (ZIO *z) { 105.39 - if (z->n == 0) { 105.40 - if (luaZ_fill(z) == EOZ) 105.41 - return EOZ; 105.42 - else { 105.43 - z->n++; /* luaZ_fill removed first byte; put back it */ 105.44 - z->p--; 105.45 - } 105.46 - } 105.47 - return char2int(*z->p); 105.48 -} 105.49 - 105.50 - 105.51 -void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 105.52 - z->L = L; 105.53 - z->reader = reader; 105.54 - z->data = data; 105.55 - z->n = 0; 105.56 - z->p = NULL; 105.57 -} 105.58 - 105.59 - 105.60 -/* --------------------------------------------------------------- read --- */ 105.61 -size_t luaZ_read (ZIO *z, void *b, size_t n) { 105.62 - while (n) { 105.63 - size_t m; 105.64 - if (luaZ_lookahead(z) == EOZ) 105.65 - return n; /* return number of missing bytes */ 105.66 - m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 105.67 - memcpy(b, z->p, m); 105.68 - z->n -= m; 105.69 - z->p += m; 105.70 - b = (char *)b + m; 105.71 - n -= m; 105.72 - } 105.73 - return 0; 105.74 -} 105.75 - 105.76 -/* ------------------------------------------------------------------------ */ 105.77 -char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 105.78 - if (n > buff->buffsize) { 105.79 - if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 105.80 - luaZ_resizebuffer(L, buff, n); 105.81 - } 105.82 - return buff->buffer; 105.83 -} 105.84 - 105.85 -
106.1 --- a/src/lua/src/lzio.h Sat Mar 03 11:04:56 2012 -0600 106.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 106.3 @@ -1,67 +0,0 @@ 106.4 -/* 106.5 -** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $ 106.6 -** Buffered streams 106.7 -** See Copyright Notice in lua.h 106.8 -*/ 106.9 - 106.10 - 106.11 -#ifndef lzio_h 106.12 -#define lzio_h 106.13 - 106.14 -#include "lua.h" 106.15 - 106.16 -#include "lmem.h" 106.17 - 106.18 - 106.19 -#define EOZ (-1) /* end of stream */ 106.20 - 106.21 -typedef struct Zio ZIO; 106.22 - 106.23 -#define char2int(c) cast(int, cast(unsigned char, (c))) 106.24 - 106.25 -#define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) 106.26 - 106.27 -typedef struct Mbuffer { 106.28 - char *buffer; 106.29 - size_t n; 106.30 - size_t buffsize; 106.31 -} Mbuffer; 106.32 - 106.33 -#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 106.34 - 106.35 -#define luaZ_buffer(buff) ((buff)->buffer) 106.36 -#define luaZ_sizebuffer(buff) ((buff)->buffsize) 106.37 -#define luaZ_bufflen(buff) ((buff)->n) 106.38 - 106.39 -#define luaZ_resetbuffer(buff) ((buff)->n = 0) 106.40 - 106.41 - 106.42 -#define luaZ_resizebuffer(L, buff, size) \ 106.43 - (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 106.44 - (buff)->buffsize = size) 106.45 - 106.46 -#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 106.47 - 106.48 - 106.49 -LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 106.50 -LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 106.51 - void *data); 106.52 -LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 106.53 -LUAI_FUNC int luaZ_lookahead (ZIO *z); 106.54 - 106.55 - 106.56 - 106.57 -/* --------- Private Part ------------------ */ 106.58 - 106.59 -struct Zio { 106.60 - size_t n; /* bytes still unread */ 106.61 - const char *p; /* current position in buffer */ 106.62 - lua_Reader reader; 106.63 - void* data; /* additional data */ 106.64 - lua_State *L; /* Lua state (for reader) */ 106.65 -}; 106.66 - 106.67 - 106.68 -LUAI_FUNC int luaZ_fill (ZIO *z); 106.69 - 106.70 -#endif
107.1 --- a/src/lua/src/print.c Sat Mar 03 11:04:56 2012 -0600 107.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 107.3 @@ -1,227 +0,0 @@ 107.4 -/* 107.5 -** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $ 107.6 -** print bytecodes 107.7 -** See Copyright Notice in lua.h 107.8 -*/ 107.9 - 107.10 -#include <ctype.h> 107.11 -#include <stdio.h> 107.12 - 107.13 -#define luac_c 107.14 -#define LUA_CORE 107.15 - 107.16 -#include "ldebug.h" 107.17 -#include "lobject.h" 107.18 -#include "lopcodes.h" 107.19 -#include "lundump.h" 107.20 - 107.21 -#define PrintFunction luaU_print 107.22 - 107.23 -#define Sizeof(x) ((int)sizeof(x)) 107.24 -#define VOID(p) ((const void*)(p)) 107.25 - 107.26 -static void PrintString(const TString* ts) 107.27 -{ 107.28 - const char* s=getstr(ts); 107.29 - size_t i,n=ts->tsv.len; 107.30 - putchar('"'); 107.31 - for (i=0; i<n; i++) 107.32 - { 107.33 - int c=s[i]; 107.34 - switch (c) 107.35 - { 107.36 - case '"': printf("\\\""); break; 107.37 - case '\\': printf("\\\\"); break; 107.38 - case '\a': printf("\\a"); break; 107.39 - case '\b': printf("\\b"); break; 107.40 - case '\f': printf("\\f"); break; 107.41 - case '\n': printf("\\n"); break; 107.42 - case '\r': printf("\\r"); break; 107.43 - case '\t': printf("\\t"); break; 107.44 - case '\v': printf("\\v"); break; 107.45 - default: if (isprint((unsigned char)c)) 107.46 - putchar(c); 107.47 - else 107.48 - printf("\\%03u",(unsigned char)c); 107.49 - } 107.50 - } 107.51 - putchar('"'); 107.52 -} 107.53 - 107.54 -static void PrintConstant(const Proto* f, int i) 107.55 -{ 107.56 - const TValue* o=&f->k[i]; 107.57 - switch (ttype(o)) 107.58 - { 107.59 - case LUA_TNIL: 107.60 - printf("nil"); 107.61 - break; 107.62 - case LUA_TBOOLEAN: 107.63 - printf(bvalue(o) ? "true" : "false"); 107.64 - break; 107.65 - case LUA_TNUMBER: 107.66 - printf(LUA_NUMBER_FMT,nvalue(o)); 107.67 - break; 107.68 - case LUA_TSTRING: 107.69 - PrintString(rawtsvalue(o)); 107.70 - break; 107.71 - default: /* cannot happen */ 107.72 - printf("? type=%d",ttype(o)); 107.73 - break; 107.74 - } 107.75 -} 107.76 - 107.77 -static void PrintCode(const Proto* f) 107.78 -{ 107.79 - const Instruction* code=f->code; 107.80 - int pc,n=f->sizecode; 107.81 - for (pc=0; pc<n; pc++) 107.82 - { 107.83 - Instruction i=code[pc]; 107.84 - OpCode o=GET_OPCODE(i); 107.85 - int a=GETARG_A(i); 107.86 - int b=GETARG_B(i); 107.87 - int c=GETARG_C(i); 107.88 - int bx=GETARG_Bx(i); 107.89 - int sbx=GETARG_sBx(i); 107.90 - int line=getline(f,pc); 107.91 - printf("\t%d\t",pc+1); 107.92 - if (line>0) printf("[%d]\t",line); else printf("[-]\t"); 107.93 - printf("%-9s\t",luaP_opnames[o]); 107.94 - switch (getOpMode(o)) 107.95 - { 107.96 - case iABC: 107.97 - printf("%d",a); 107.98 - if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b); 107.99 - if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c); 107.100 - break; 107.101 - case iABx: 107.102 - if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx); 107.103 - break; 107.104 - case iAsBx: 107.105 - if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx); 107.106 - break; 107.107 - } 107.108 - switch (o) 107.109 - { 107.110 - case OP_LOADK: 107.111 - printf("\t; "); PrintConstant(f,bx); 107.112 - break; 107.113 - case OP_GETUPVAL: 107.114 - case OP_SETUPVAL: 107.115 - printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-"); 107.116 - break; 107.117 - case OP_GETGLOBAL: 107.118 - case OP_SETGLOBAL: 107.119 - printf("\t; %s",svalue(&f->k[bx])); 107.120 - break; 107.121 - case OP_GETTABLE: 107.122 - case OP_SELF: 107.123 - if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } 107.124 - break; 107.125 - case OP_SETTABLE: 107.126 - case OP_ADD: 107.127 - case OP_SUB: 107.128 - case OP_MUL: 107.129 - case OP_DIV: 107.130 - case OP_POW: 107.131 - case OP_EQ: 107.132 - case OP_LT: 107.133 - case OP_LE: 107.134 - if (ISK(b) || ISK(c)) 107.135 - { 107.136 - printf("\t; "); 107.137 - if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); 107.138 - printf(" "); 107.139 - if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); 107.140 - } 107.141 - break; 107.142 - case OP_JMP: 107.143 - case OP_FORLOOP: 107.144 - case OP_FORPREP: 107.145 - printf("\t; to %d",sbx+pc+2); 107.146 - break; 107.147 - case OP_CLOSURE: 107.148 - printf("\t; %p",VOID(f->p[bx])); 107.149 - break; 107.150 - case OP_SETLIST: 107.151 - if (c==0) printf("\t; %d",(int)code[++pc]); 107.152 - else printf("\t; %d",c); 107.153 - break; 107.154 - default: 107.155 - break; 107.156 - } 107.157 - printf("\n"); 107.158 - } 107.159 -} 107.160 - 107.161 -#define SS(x) (x==1)?"":"s" 107.162 -#define S(x) x,SS(x) 107.163 - 107.164 -static void PrintHeader(const Proto* f) 107.165 -{ 107.166 - const char* s=getstr(f->source); 107.167 - if (*s=='@' || *s=='=') 107.168 - s++; 107.169 - else if (*s==LUA_SIGNATURE[0]) 107.170 - s="(bstring)"; 107.171 - else 107.172 - s="(string)"; 107.173 - printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n", 107.174 - (f->linedefined==0)?"main":"function",s, 107.175 - f->linedefined,f->lastlinedefined, 107.176 - S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f)); 107.177 - printf("%d%s param%s, %d slot%s, %d upvalue%s, ", 107.178 - f->numparams,f->is_vararg?"+":"",SS(f->numparams), 107.179 - S(f->maxstacksize),S(f->nups)); 107.180 - printf("%d local%s, %d constant%s, %d function%s\n", 107.181 - S(f->sizelocvars),S(f->sizek),S(f->sizep)); 107.182 -} 107.183 - 107.184 -static void PrintConstants(const Proto* f) 107.185 -{ 107.186 - int i,n=f->sizek; 107.187 - printf("constants (%d) for %p:\n",n,VOID(f)); 107.188 - for (i=0; i<n; i++) 107.189 - { 107.190 - printf("\t%d\t",i+1); 107.191 - PrintConstant(f,i); 107.192 - printf("\n"); 107.193 - } 107.194 -} 107.195 - 107.196 -static void PrintLocals(const Proto* f) 107.197 -{ 107.198 - int i,n=f->sizelocvars; 107.199 - printf("locals (%d) for %p:\n",n,VOID(f)); 107.200 - for (i=0; i<n; i++) 107.201 - { 107.202 - printf("\t%d\t%s\t%d\t%d\n", 107.203 - i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); 107.204 - } 107.205 -} 107.206 - 107.207 -static void PrintUpvalues(const Proto* f) 107.208 -{ 107.209 - int i,n=f->sizeupvalues; 107.210 - printf("upvalues (%d) for %p:\n",n,VOID(f)); 107.211 - if (f->upvalues==NULL) return; 107.212 - for (i=0; i<n; i++) 107.213 - { 107.214 - printf("\t%d\t%s\n",i,getstr(f->upvalues[i])); 107.215 - } 107.216 -} 107.217 - 107.218 -void PrintFunction(const Proto* f, int full) 107.219 -{ 107.220 - int i,n=f->sizep; 107.221 - PrintHeader(f); 107.222 - PrintCode(f); 107.223 - if (full) 107.224 - { 107.225 - PrintConstants(f); 107.226 - PrintLocals(f); 107.227 - PrintUpvalues(f); 107.228 - } 107.229 - for (i=0; i<n; i++) PrintFunction(f->p[i],full); 107.230 -}