diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..ae2899e088 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +.gitattributes + +*.so +*.o +*.a + +manual/manual.html + +testes/time.txt +testes/time-debug.txt + +testes/libs/all + +temp +lua diff --git a/README.md b/README.md new file mode 100644 index 0000000000..5bc0ee77c4 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Lua + +This is the repository of Lua development code, as seen by the Lua team. It contains the full history of all commits but is mirrored irregularly. For complete information about Lua, visit [Lua.org](https://www.lua.org/). + +Please **do not** send pull requests. To report issues, post a message to the [Lua mailing list](https://www.lua.org/lua-l.html). + +Download official Lua releases from [Lua.org](https://www.lua.org/download.html). diff --git a/all b/all index 8f78ee4d5b..86f38ac1c3 100755 --- a/all +++ b/all @@ -1,8 +1,8 @@ make -s -j cd testes/libs; make -s cd .. # back to directory 'testes' -ulimit -S -s 2000 -if { ../lua all.lua; } then +ulimit -S -s 1100 +if { ../lua -W all.lua; } then echo -e "\n\n final OK!!!!\n\n" else echo -e "\n\n >>>> BUG!!!!\n\n" diff --git a/bugs b/bugs deleted file mode 100644 index a965025b66..0000000000 --- a/bugs +++ /dev/null @@ -1,4052 +0,0 @@ ---[=[ -** lua.stx / llex.c -Tue Dec 2 10:45:48 EDT 1997 ->> BUG: "lastline" was not reset on function entry, so debug information ->> started only in the 2nd line of a function. - - - -================================================================= ---- Version 3.1 alpha - -** lua.c -Thu Jan 15 14:34:58 EDT 1998 ->> must include "stdlib.h" (for "exit()"). - -** lbuiltin.c / lobject.h -Thu Jan 15 14:34:58 EDT 1998 ->> MAX_WORD may be bigger than MAX_INT -(by lhf) - -** llex.c -Mon Jan 19 18:17:18 EDT 1998 ->> wrong line number (+1) in error report when file starts with "#..." - -** lstrlib.c -Tue Jan 27 15:27:49 EDT 1998 ->> formats like "%020d" were considered too big (3 digits); moreover, ->> some sistems limit printf to at most 500 chars, so we can limit sizes ->> to 2 digits (99). - -** lapi.c -Tue Jan 27 17:12:36 EDT 1998 ->> "lua_getstring" may create a new string, so should check GC - -** lstring.c / ltable.c -Wed Jan 28 14:48:12 EDT 1998 ->> tables can become full of "empty" slots, and keep growing without limits. - -** lstrlib.c -Mon Mar 9 15:26:09 EST 1998 ->> gsub('a', '(b?)%1*' ...) loops (because the capture is empty). - -** lstrlib.c -Mon May 18 19:20:00 EST 1998 ->> arguments for "format" 'x', 'X', 'o' and 'u' must be unsigned int. - - - -================================================================= ---- Version 3.1 - -** liolib.c / lauxlib.c -Mon Sep 7 15:57:02 EST 1998 ->> function "luaL_argerror" prints wrong argument number (from a user's point -of view) when functions have upvalues. - -** lstrlib.c -Tue Nov 10 17:29:36 EDT 1998 ->> gsub/strfind do not check whether captures are properly finished. -(by roberto/tomas) - -** lbuiltin.c -Fri Dec 18 11:22:55 EDT 1998 ->> "tonumber" goes crazy with negative numbers in other bases (not 10), -because "strtol" returns long, not unsigned long. -(by Visual C++) - -** lstrlib.c -Mon Jan 4 10:41:40 EDT 1999 ->> "format" does not check size of format item (such as "%00000...00000d"). - -** lapi.c -Wed Feb 3 14:40:21 EDT 1999 ->> getlocal cannot return the local itself, since lua_isstring and -lua_isnumber can modify it. - -** lstrlib.c -Thu Feb 4 17:08:50 EDT 1999 ->> format "%s" may break limit of "sprintf" on some machines. -(by Marcelo Sales) - -** lzio.c -Thu Mar 4 11:49:37 EST 1999 ->> file stream cannot call fread after EOF. -(by lhf) - - - -================================================================= ---- Version 3.2 (beta) - -** lstrlib.c -Fri Apr 30 11:10:20 EST 1999 ->> '$' at end of pattern was matching regular '$', too. -(by anna; since 2.5) - -** lbuiltin.c -Fri May 21 17:15:11 EST 1999 ->> foreach, foreachi, foreachvar points to function in stack when stack -can be reallocated. -(by tomas; since 3.2 beta) - -** lparser.c -Wed Jun 16 10:32:46 EST 1999 ->> cannot assign to unlimited variables, because it causes overflow in -the number of returns of a function. -(since 3.1) - - - -================================================================= ---- Version 3.2 - -** lmathlib.c -Wed Aug 18 11:28:38 EST 1999 ->> random(0) and random(x,0) are wrong (0 is read as no argument!). -(by Dave Bollinger; since 3.1) - -** lparser.c -Thu Sep 2 10:07:20 EST 1999 ->> in the (old) expression << ls->fs->f->consts[checkname(ls)] >>, checkname -could realloc f->consts. -(by Supratik Champati; since 3.2 beta) - -** lobject.c / lbuiltin.c -Wed Sep 8 17:41:54 EST 1999 ->> tonumber'e1' and tonumber(' ', x), for x!=10, gave 0 instead of nil. -(since 3.1) - -** lstrlib.c -Thu Nov 11 14:36:30 EDT 1999 ->> `strfind' does not handle \0 in plain search. -(by Jon Kleiser; since 3.1) - -** lparser.c -Wed Dec 29 16:05:43 EDT 1999 ->> return gives wrong line in debug information -(by lhf; since 3.2 [at least]) - -** ldo.c -Thu Dec 30 16:39:33 EDT 1999 ->> cannot reopen stdin (for binary mode) -(by lhf & roberto; since 3.1) - -** lapi.c -Thu Mar 2 09:41:53 EST 2000 ->> lua_settable should check stack space (it could call a T.M.) -(by lhf & celes; since 3.2; it was already fixed by fixed stack) - -** lparser.c -Mon Apr 3 09:59:06 EST 2000 ->> '%' should be in expfollow -(by Edgar Toernig; since 3.1; it was already fixed) - -** lbuiltin.c -Mon Apr 3 10:05:05 EST 2000 ->> tostring() without arguments gives seg. fault. -(by Edgar Toernig; since 3.0) - - - -================================================================= ---- Version 4.0 alpha - -Tested with full test suites (as locked in Mon Apr 24 14:23:11 EST 2000) -in the following platforms: -* Linux - gcc, g++ -* AIX - gcc -* Solaris - gcc, cc -* IRIX - cc, cc-purify -* Windows - Visual C++ (.c e .cpp, warning level=4) - - -** lstrlib.c -Tue May 2 15:27:58 EST 2000 ->> `strfind' gets wrong subject length when there is an offset -(by Jon Kleiser; since 4.0a) - -** lparser.c -Fri May 12 15:11:12 EST 2000 ->> first element in a list constructor is not adjusted to one value ->> (e.g. «a = {gsub('a','a','')}») -(by Tomas; since 4.0a) - -** lparser.c -Wed May 24 14:50:16 EST 2000 ->> record-constructor starting with an upvalue name gets an error ->> (e.g. «local a; function f() x = {a=1} end») -(by Edgar Toernig; since 3.1) - -** lparser.c -Tue Aug 29 15:56:05 EST 2000 ->> error message for `for' uses `while' -(since 4.0a; already corrected) - -** lgc.c -Tue Aug 29 15:57:41 EST 2000 ->> gc tag method for nil could call line hook -(by ry; since ?) - - - -================================================================= ---- Version 4.0 Beta - -** liolib.c -Fri Sep 22 15:12:37 EST 2000 ->> `read("*w")' should return nil at EOF -(by roberto; since 4.0b) - -** lvm.c -Mon Sep 25 11:47:48 EST 2000 ->> lua_gettable does not get key from stack top -(by Philip Yi; since 4.0b) - -** lgc.c -Mon Sep 25 11:50:48 EST 2000 ->> GC may crash when checking locked C closures -(by Philip Yi; since 4.0b) - -** lapi.c -Wed Sep 27 09:50:19 EST 2000 ->> lua_tag should return LUA_NOTAG for non-valid indices -(by Paul Hankin; since 4.0b) - -** llex.h / llex.c / lparser.c -Wed Sep 27 13:39:45 EST 2000 ->> parser overwrites semantic information when looking ahead ->> (e.g. «a = {print'foo'}») -(by Edgar Toernig; since 4.0b, deriving from previous bug) - -** liolib.c -Thu Oct 26 10:50:46 EDT 2000 ->> in function `read_file', realloc() doesn't free the buffer if it can't ->> allocate new memory -(by Mauro Vezzosi; since 4.0b) - - - -================================================================= ---- Version 4.0 - -** lparser.c -Wed Nov 29 09:51:44 EDT 2000 ->> parser does not accept a `;' after a `return' -(by lhf; since 4.0b) - -** liolib.c -Fri Dec 22 15:30:42 EDT 2000 ->> when `read' fails it must return nil (and not no value) -(by cassino; since at least 3.1) - -** lstring.c/lapi.c -Thu Feb 1 11:55:45 EDT 2001 ->> lua_pushuserdata(L, NULL) is buggy -(by Edgar Toernig; since 4.0) - -** ldo.c -Fri Feb 2 14:06:40 EDT 2001 ->> «while 1 dostring[[print('hello\n')]] end» never reclaims memory -(by Andrew Paton; since 4.0b) - -** lbaselib.c -Tue Feb 6 11:57:13 EDT 2001 ->> ESC (which starts precompiled code) in C is \33, not \27 -(by Edgar Toernig and lhf; since 4.0b) - -** lparser.c -Tue Jul 10 16:59:18 EST 2001 ->> error message for `%a' gave wrong line number -(by Leonardo Constantino; since 4.0) - -** lbaselib.c -Fri Dec 21 15:21:05 EDT 2001 ->> seg. fault when rawget/rawset get extra arguments -(by Eric Mauger; since 4.0b) - -** lvm.c -Wed Jun 19 13:28:20 EST 2002 ->> line hook gets wrong `ar' -(by Daniel C. Sinclair; since 4.0.b) - -** ldo.c -Wed Jun 19 13:31:49 EST 2002 ->> `protectedparser' may run GC, and then collect `filename' ->> (in function `parse_file') -(by Alex Bilyk; since 4.0) - - - - -================================================================= ---- Version 5.0 alpha - -** lgc.c -Fri Aug 30 13:49:14 EST 2002 ->> GC metamethod stored in a weak metatable being collected together with ->> userdata may not be cleared properly -(by Roberto; since 5.0a) - -** lapi.c -Thu Nov 21 11:00:00 EST 2002 ->> ULONG_MAX>>10 may not fit into an int -(by Jeff Petkau; since 4.0) - -** lparser.c -Fri Dec 6 17:06:40 UTC 2002 ->> scope of generic for variables is not sound -(by Gavin Wraith; since 5.0a) - - - - -================================================================= ---- Version 5.0 beta -** lbaselib.c -Fri Dec 20 09:53:19 UTC 2002 ->> `resume' was checking the wrong value for stack overflow -(by Maik Zimmermann; since 5.0b) - -** ldo.c -Thu Jan 23 11:29:06 UTC 2003 ->> error during garbage collection in luaD_protectedparser is not being ->> protected -(by Benoit Germain; since 5.0a) - -** ldo.c (and others) -Fri Feb 28 14:20:33 EST 2003 ->> GC metamethod calls could mess C/Lua stack syncronization -(by Roberto; since 5.0b) - -** lzio.h/zlio.c -Thu Mar 20 11:40:12 EST 2003 ->> zio mixes a 255 as first char in a buffer with EOZ -(by lhf; since 5.0a) - - - ---]=] ------------------------------------------------------------------ --- Lua 5.0 (final) - -Bug{ -what = [[lua_closethread exists only in the manual]], -report = [[by Nguyen Binh, 28/04/2003]], -patch = [[no patch; the manual is wrong]], -} - - -Bug{ -what = [[attempt to resume a running coroutine crashes Lua]], -example = [[ -function co_func (current_co) - coroutine.resume(co) -end -co = coroutine.create(co_func) -coroutine.resume(co) -coroutine.resume(co) --> seg. fault -]], -report = [[by Alex Bilyk, 09/05/2003]], -patch = [[ -* ldo.c: -325,326c325 -< if (nargs >= L->top - L->base) -< luaG_runerror(L, "cannot resume dead coroutine"); ---- -> lua_assert(nargs < L->top - L->base); -329c328,329 -< else if (ci->state & CI_YIELD) { /* inside a yield? */ ---- -> else { /* inside a yield */ -> lua_assert(ci->state & CI_YIELD); -344,345d343 -< else -< luaG_runerror(L, "cannot resume non-suspended coroutine"); -351a350,358 -> static int resume_error (lua_State *L, const char *msg) { -> L->top = L->ci->base; -> setsvalue2s(L->top, luaS_new(L, msg)); -> incr_top(L); -> lua_unlock(L); -> return LUA_ERRRUN; -> } -> -> -355a363,368 -> if (L->ci == L->base_ci) { -> if (nargs >= L->top - L->base) -> return resume_error(L, "cannot resume dead coroutine"); -> } -> else if (!(L->ci->state & CI_YIELD)) /* not inside a yield? */ -> return resume_error(L, "cannot resume non-suspended coroutine"); -]], -} - - -Bug{ -what = [[file:close cannot be called without a file. (results in seg fault)]], -example = [[ -> io.stdin.close() -- correct call shold be io.stdin:close() -]], -report = [[by Tuomo Valkonen, 27/05/2003]], -patch = [[ -* liolib.c: -161c161 -< if (lua_isnone(L, 1)) { ---- -> if (lua_isnone(L, 1) && lua_type(L, lua_upvalueindex(1)) == LUA_TTABLE) { -]], --}} -} - - -Bug{ -what = [[C functions also may have stacks larger than current top]], -example = [[ -Must recompile lua with a change in lua.c and with lua_assert defined: -* lua.c: -381a382 -> lua_checkstack(l, 1000); -]], -report = [[Alex Bilyk, 09/06/2003]], -patch = [[ -* lgc.c: -247c247 -< if (!(ci->state & CI_C) && lim < ci->top) ---- -> if (lim < ci->top) -]], -} - - -Bug{ -what = [[`pc' address is invalidated when a coroutine is suspended]], -example = [[ -function g(x) - coroutine.yield(x) -end - -function f (i) - debug.sethook(print, "l") - for j=1,1000 do - g(i+j) - end -end - -co = coroutine.wrap(f) -co(10) -pcall(co) -pcall(co) -]], -report = [[Nick Trout, 07/07/2003]], -patch = [[ -* lvm.c: -402,403c402,403 -< L->ci->u.l.pc = &pc; -< if (L->hookmask & LUA_MASKCALL) ---- -> if (L->hookmask & LUA_MASKCALL) { -> L->ci->u.l.pc = &pc; -404a405 -> } -405a407 -> L->ci->u.l.pc = &pc; -676,678c678 -< lua_assert(ci->u.l.pc == &pc && -< ttisfunction(ci->base - 1) && -< (ci->state & CI_SAVEDPC)); ---- -> lua_assert(ttisfunction(ci->base - 1) && (ci->state & CI_SAVEDPC)); -]] -} - - -Bug{ -what = [[userdata to be collected still counts into new GC threshold, -increasing memory consumption]], -report = [[Roberto, 25/07/2003]], -example = [[ -a = newproxy(true) -getmetatable(a).__gc = function () end -for i=1,10000000 do - newproxy(a) - if math.mod(i, 10000) == 0 then print(gcinfo()) end -end -]], -patch = [[ -* lgc.h: -18c18 -< void luaC_separateudata (lua_State *L); ---- -> size_t luaC_separateudata (lua_State *L); - -* lgc.c: -113c113,114 -< void luaC_separateudata (lua_State *L) { ---- -> size_t luaC_separateudata (lua_State *L) { -> size_t deadmem = 0; -127a129 -> deadmem += sizeudata(gcotou(curr)->uv.len); -136a139 -> return deadmem; -390c393 -< static void checkSizes (lua_State *L) { ---- -> static void checkSizes (lua_State *L, size_t deadmem) { -400c403 -< G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */ ---- -> G(L)->GCthreshold = 2*G(L)->nblocks - deadmem; /* new threshold */ -454c457,458 -< static void mark (lua_State *L) { ---- -> static size_t mark (lua_State *L) { -> size_t deadmem; -467c471 -< luaC_separateudata(L); /* separate userdata to be preserved */ ---- -> deadmem = luaC_separateudata(L); /* separate userdata to be preserved */ -475a480 -> return deadmem; -480c485 -< mark(L); ---- -> size_t deadmem = mark(L); -482c487 -< checkSizes(L); ---- -> checkSizes(L, deadmem); -]] -} - -Bug{ -what=[[IBM AS400 (OS400) has sizeof(void *)==16, and a `%p' may generate -up to 60 characters in a `printf'. That causes a buffer overflow in -`tostring'.]], - -report = [[David Burgess, 25/08/2003]], - -example = [[print{}; (in an AS400 machine)]], - -patch = [[ -* liolib.c: -178c178 -< char buff[32]; ---- -> char buff[128]; - -* lbaselib.c: -327c327 -< char buff[64]; ---- -> char buff[128]; -]] -} - - -Bug{ -what = [[syntax `local function' does not increment stack size]], - -report = [[Rici Lake, 26/09/2003]], - -example = [[ --- must run this with precompiled code -local a,b,c -local function d () end -]], - -patch = [[ -* lparser.c: -1143a1144 -> FuncState *fs = ls->fs; -1145c1146,1147 -< init_exp(&v, VLOCAL, ls->fs->freereg++); ---- -> init_exp(&v, VLOCAL, fs->freereg); -> luaK_reserveregs(fs, 1); -1148c1150,1152 -< luaK_storevar(ls->fs, &v, &b); ---- -> luaK_storevar(fs, &v, &b); -> /* debug information will only see the variable after this point! */ -> getlocvar(fs, fs->nactvar - 1).startpc = fs->pc; -]], - -} - - -Bug{ - -what = [[count hook may be called without being set]], - -report = [[Andreas Stenius, 06/10/2003]], - -example = [[ -set your hooks with - - lua_sethook(L, my_hook, LUA_MASKLINE | LUA_MASKRET, 1); - -(It is weird to use a count > 0 without setting the count hook, -but it is not wrong.) -]], - -patch = [[ -* lvm.c: -69c69 -< if (mask > LUA_MASKLINE) { /* instruction-hook set? */ ---- -> if (mask & LUA_MASKCOUNT) { /* instruction-hook set? */ -]], - -} - - -Bug{ - -what = [[`dofile' eats one return value when called without arguments]], - -report = [[Frederico Abraham, 15/01/2004]], - -example = [[ -a,b = dofile() --< here you enter `return 1,2,3 ' -print(a,b) --> 2 3 (should be 1 and 2) -]], - -patch = [[ -* lbaselib.c: -313a314 -> int n = lua_gettop(L); -317c318 -< return lua_gettop(L) - 1; ---- -> return lua_gettop(L) - n; -]], - -} - - - ------------------------------------------------------------------ --- Lua 5.0.2 - -Bug{ -what = [[string concatenation may cause arithmetic overflow, leading -to a buffer overflow]], - -report = [[Rici Lake, 20/05/2004]], - -example = [[ -longs = string.rep("\0", 2^25) -function catter(i) - return assert(loadstring( - string.format("return function(a) return a%s end", - string.rep("..a", i-1))))() -end -rep129 = catter(129) -rep129(longs) -]], - -patch = [[ -* lvm.c: -@@ -321,15 +321,15 @@ - luaG_concaterror(L, top-2, top-1); - } else if (tsvalue(top-1)->tsv.len > 0) { /* if len=0, do nothing */ - /* at least two string values; get as many as possible */ -- lu_mem tl = cast(lu_mem, tsvalue(top-1)->tsv.len) + -- cast(lu_mem, tsvalue(top-2)->tsv.len); -+ size_t tl = tsvalue(top-1)->tsv.len; - char *buffer; - int i; -- while (n < total && tostring(L, top-n-1)) { /* collect total length */ -- tl += tsvalue(top-n-1)->tsv.len; -- n++; -+ /* collect total length */ -+ for (n = 1; n < total && tostring(L, top-n-1); n++) { -+ size_t l = tsvalue(top-n-1)->tsv.len; -+ if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow"); -+ tl += l; - } -- if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow"); - buffer = luaZ_openspace(L, &G(L)->buff, tl); - tl = 0; - for (i=n; i>0; i--) { /* concat all strings */ -]] -} - - -Bug{ -what = [[lua_getupvalue and setupvalue do not check for index too small]], - -report = [[Mike Pall, ?/2004]], - -example = [[debug.getupvalue(function() end, 0)]], - -patch = [[ -* lapi.c -941c941 -< if (n > f->c.nupvalues) return NULL; ---- -> if (!(1 <= n && n <= f->c.nupvalues)) return NULL; -947c947 -< if (n > p->sizeupvalues) return NULL; ---- -> if (!(1 <= n && n <= p->sizeupvalues)) return NULL; -]] -} - - -Bug{ -what = [[values holded in open upvalues of suspended threads may be -incorrectly collected]], - -report = [[Spencer Schumann, 31/12/2004]], - -example = [[ -local thread_id = 0 -local threads = {} - -function fn(thread) - thread_id = thread_id + 1 - threads[thread_id] = function() - thread = nil - end - coroutine.yield() -end - -while true do - local thread = coroutine.create(fn) - coroutine.resume(thread, thread) -end -]], - -patch = [[ -* lgc.c: -221,224c221,222 -< if (!u->marked) { -< markobject(st, &u->value); -< u->marked = 1; -< } ---- -> markobject(st, u->v); -> u->marked = 1; -]], -} - - -Bug{ -what = [[rawset/rawget do not ignore extra arguments]], - -report = [[Romulo Bahiense, 11/03/2005]], - -example = [[ -a = {} -rawset(a, 1, 2, 3) -print(a[1], a[2]) -- should be 2 and nil -]], - -patch = [[ -* lbaselib.c: -175a176 -> lua_settop(L, 2); -183a185 -> lua_settop(L, 3); -]], -} - - -Bug{ -what = [[weak tables that survive one collection are never collected]], - -report = [[Chromix, 02/01/2006]], - -example = [[ -a = {} -print(gcinfo()) -for i = 1, 10000 do - a[i] = setmetatable({}, {__mode = "v"}) -end -collectgarbage() -a = nil -collectgarbage() -print(gcinfo()) -]], - -patch = [[ -* lgc.c -@@ -366,7 +366,7 @@ - GCObject *curr; - int count = 0; /* number of collected items */ - while ((curr = *p) != NULL) { -- if (curr->gch.marked > limit) { -+ if ((curr->gch.marked & ~(KEYWEAK | VALUEWEAK)) > limit) { - unmark(curr); - p = &curr->gch.next; - } -]], - -} - - -Bug{ -what = [[Some "not not exp" may not result in boolean values]], -report = [[]], -since = [[4.0]], -example = [[ --- should print false, but prints nil -print(not not (nil and 4)) -]], -patch = [[]], -} - - -Bug{ -what = [[On some machines, closing a "piped file" (created with io.popen) -may crash Lua]], -report = [[]], -since = [[5.0]], -example = [[ --- only on some machines - f = io.popen("ls") - f:close() -]], -patch = [[]], -} - - - ------------------------------------------------------------------ --- Lua 5.1 - -Bug{ -what = [[In 16-bit machines, expressions and/or with numeric constants as the -right operand may result in weird values]], - -report = [[Andreas Stenius/Kein-Hong Man, 15/03/2006]], - -example = [[ -print(false or 0) -- on 16-bit machines -]], - -patch = [[ -* lcode.c: -@@ -731,17 +731,15 @@ - case OPR_AND: { - lua_assert(e1->t == NO_JUMP); /* list must be closed */ - luaK_dischargevars(fs, e2); -- luaK_concat(fs, &e1->f, e2->f); -- e1->k = e2->k; e1->u.s.info = e2->u.s.info; -- e1->u.s.aux = e2->u.s.aux; e1->t = e2->t; -+ luaK_concat(fs, &e2->f, e1->f); -+ *e1 = *e2; - break; - } - case OPR_OR: { - lua_assert(e1->f == NO_JUMP); /* list must be closed */ - luaK_dischargevars(fs, e2); -- luaK_concat(fs, &e1->t, e2->t); -- e1->k = e2->k; e1->u.s.info = e2->u.s.info; -- e1->u.s.aux = e2->u.s.aux; e1->f = e2->f; -+ luaK_concat(fs, &e2->t, e1->t); -+ *e1 = *e2; - break; - } -]], - -} - - -Bug{ -what = [[luaL_checkudata may produce wrong error message]], - -report = [[Greg Falcon, 21/03/2006]], - -example = [[ -getmetatable(io.stdin).__gc() - --> bad argument #1 to '__gc' (FILE* expected, got table) -]], - -patch = [[ -* lauxlib.c: -@@ -123,11 +123,17 @@ - - LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { - void *p = lua_touserdata(L, ud); -- lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ -- if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2)) -- luaL_typerror(L, ud, tname); -- lua_pop(L, 2); /* remove both metatables */ -- return p; -+ if (p != NULL) { /* value is a userdata? */ -+ if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ -+ lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ -+ if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ -+ lua_pop(L, 2); /* remove both metatables */ -+ return p; -+ } -+ } -+ } -+ luaL_typerror(L, ud, tname); /* else error */ -+ return NULL; /* to avoid warnings */ - } -]] - -} - - -Bug{ -what = [[ -In Windows, -when Lua is used in an application that also uses DirectX, -it may present an erractic behavior. -THIS IS NOT A LUA BUG! -The problem is that DirectX violates an ABI that Lua depends on.]], - -patch = [[ -The simplest solution is to use DirectX with -the D3DCREATE_FPU_PRESERVE flag. - -Otherwise, you can change the definition of lua_number2int, -in luaconf.h, to this one: -#define lua_number2int(i,d) __asm fld d __asm fistp i -]], - -} - - -Bug{ -what = [[option '%q' in string.format does not handle '\r' correctly.]], - -example = [[ -local s = "a string with \r and \n and \r\n and \n\r" -local c = string.format("return %q", s) -assert(assert(loadstring(c))() == s) -]], - -patch = [[ -* lstrlib.c: -@@ -703,6 +703,10 @@ - luaL_addchar(b, *s); - break; - } -+ case '\r': { -+ luaL_addlstring(b, "\\r", 2); -+ break; -+ } - case '\0': { - luaL_addlstring(b, "\\000", 4); - break; -]], - -} - - -Bug{ -what = [[lua_dostring/lua_dofile should return any values returned -by the chunk]], - -patch = [[ -* lauxlib.h: -@@ -108,9 +108,11 @@ - - #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) - --#define luaL_dofile(L, fn) (luaL_loadfile(L, fn) || lua_pcall(L, 0, 0, 0)) -+#define luaL_dofile(L, fn) \ -+ (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) - --#define luaL_dostring(L, s) (luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0))+#define luaL_dostring(L, s) \ -+ (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) - - #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) -]], - -} - - -Bug{ - -what = [[garbage collector does not compensate enough for finalizers]], - -patch = [[ -lgc.c: -@@ -322,4 +322,6 @@ - --static void propagateall (global_State *g) { -- while (g->gray) propagatemark(g); -+static size_t propagateall (global_State *g) { -+ size_t m = 0; -+ while (g->gray) m += propagatemark(g); -+ return m; - } -@@ -542,3 +544,3 @@ - marktmu(g); /* mark `preserved' userdata */ -- propagateall(g); /* remark, to propagate `preserveness' */ -+ udsize += propagateall(g); /* remark, to propagate `preserveness' */ - cleartable(g->weak); /* remove collected objects from weak tables */ -@@ -592,2 +594,4 @@ - GCTM(L); -+ if (g->estimate > GCFINALIZECOST) -+ g->estimate -= GCFINALIZECOST; -]] -} - - -Bug{ - -what = [[debug hooks may get wrong when mixed with coroutines]], - -report = [[by Ivko Stanilov, 03/06/2006]], - -example = [[ -co = coroutine.create(function (a,b) - coroutine.yield(a, b) - return b, "end" -end) - -debug.sethook(co, function() end, "lcr") -coroutine.resume(co, 100, 2000) -coroutine.resume(co, 100, 2000) -]], - -patch = [[ -* ldo.c: -@@ -389,6 +389,7 @@ - return; - } - else { /* resuming from previous yield */ -+ L->status = 0; - if (!f_isLua(ci)) { /* `common' yield? */ - /* finish interrupted execution of `OP_CALL' */ - lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL || -@@ -399,7 +400,6 @@ - else /* yielded inside a hook: just continue its execution */ - L->base = L->ci->base; - } -- L->status = 0; - luaV_execute(L, cast_int(L->ci - L->base_ci)); - } -]], - -} - - - ------------------------------------------------------------------ --- Lua 5.1.1 - -Bug{ -what = [[list constructors have wrong limit]], - -report = [[by Norman Ramsey, June 2006]], - -since = "5.1", - -example = [[ -a = {} -a[1] = "x={1" -for i = 2, 2^20 do - a[i] = 1 -end -a[#a + 1] = "}" -s = table.concat(a, ",") -assert(loadstring(s))() -print(#x) -]], - -patch = [[ -* lparser.c: -@@ -489,7 +489,7 @@ - - static void listfield (LexState *ls, struct ConsControl *cc) { - expr(ls, &cc->v); -- luaY_checklimit(ls->fs, cc->na, MAXARG_Bx, "items in a constructor"); -+ luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); - cc->na++; - cc->tostore++; - } -]], - -} - - -Bug{ -what = [[wrong message error in some cases involving closures]], - -report = [[Shmuel Zeigerman, on 07/2006]], - -since = "5.1", - -example = [[ -local Var -local function main() - NoSuchName (function() Var=0 end) -end -main() ---> lua5.1: temp:3: attempt to call upvalue 'Var' (a nil value) -]], - -patch = [[ -*ldebug.c: -@@ -435,14 +435,16 @@ - break; - } - case OP_CLOSURE: { -- int nup; -+ int nup, j; - check(b < pt->sizep); - nup = pt->p[b]->nups; - check(pc + nup < pt->sizecode); -- for (; nup>0; nup--) { -- OpCode op1 = GET_OPCODE(pt->code[pc+nup]); -+ for (j = 1; j <= nup; j++) { -+ OpCode op1 = GET_OPCODE(pt->code[pc + j]); - check(op1 == OP_GETUPVAL || op1 == OP_MOVE); - } -+ if (reg != NO_REG) /* tracing? */ -+ pc += nup; /* do not 'execute' these pseudo-instructions */ - break; - } - case OP_VARARG: { -]], - -} - - -Bug{ -what = [[string.format("%") may read past the string]], -report = [[Roberto, on 09/2006]], -since = [[5.0]], -example = [[print(string.format("%"))]], -patch = [[ -*lstrlib.c: -@@ -723,7 +723,7 @@ - - static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { const char *p = strfrmt; -- while (strchr(FLAGS, *p)) p++; /* skip flags */ -+ while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ - if ((size_t)(p - strfrmt) >= sizeof(FLAGS)) - luaL_error(L, "invalid format (repeated flags)"); - if (isdigit(uchar(*p))) p++; /* skip width */ -]], -} - - -Bug{ -what = [[os.date throws an error when result is the empty string]], -report = [[]], -since = [[4.0]], -example = [[print(os.date(""))]], -patch = [[ -*loslib.c: -@@ -148,7 +148,18 @@ - else { -- char b[256]; -- if (strftime(b, sizeof(b), s, stm)) -- lua_pushstring(L, b); -- else -- return luaL_error(L, LUA_QL("date") " format too long"); -+ char cc[3]; -+ luaL_Buffer b; -+ cc[0] = '%'; cc[2] = '\0'; -+ luaL_buffinit(L, &b); -+ for (; *s; s++) { -+ if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ -+ luaL_addchar(&b, *s); -+ else { -+ size_t reslen; -+ char buff[200]; /* should be big enough for any conversion result */ -+ cc[1] = *(++s); -+ reslen = strftime(buff, sizeof(buff), cc, stm); -+ luaL_addlstring(&b, buff, reslen); -+ } -+ } -+ luaL_pushresult(&b); - } -]], -} - - -Bug{ -what = [[setfenv accepts invalid 1st argument]], -report = [[Doug Rogers, on 02/2007]], -since = [[5.0]], -example = [[setfenv(nil, {}) -- should throw an error]], -patch = [[ -*lbaselib.c: -@@ -116,3 +116,3 @@ - --static void getfunc (lua_State *L) { -+static void getfunc (lua_State *L, int opt) { - if (lua_isfunction(L, 1)) lua_pushvalue(L, 1); -@@ -120,3 +120,3 @@ - lua_Debug ar; -- int level = luaL_optint(L, 1, 1); -+ int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1); - luaL_argcheck(L, level >= 0, 1, "level must be non-negative"); -@@ -133,3 +133,3 @@ - static int luaB_getfenv (lua_State *L) { -- getfunc(L); -+ getfunc(L, 1); - if (lua_iscfunction(L, -1)) /* is a C function? */ -@@ -144,3 +144,3 @@ - luaL_checktype(L, 2, LUA_TTABLE); -- getfunc(L); -+ getfunc(L, 0); - lua_pushvalue(L, 2); -]], -} - - -Bug{ -what = [[wrong code for arithmetic expressions in some specific scenarios]], -report = [[Thierry Grellier, on 01/2007]], -since = [[5.1]], -example = [[ --- use a large number of names (almost 256) -v1=1; v2=1; v3=1; v4=1; v5=1; v6=1; v7=1; v8=1; v9=1; -v10=1; v11=1; v12=1; v13=1; v14=1; v15=1; v16=1; v17=1; -v18=1; v19=1; v20=1; v21=1; v22=1; v23=1; v24=1; v25=1; -v26=1; v27=1; v28=1; v29=1; v30=1; v31=1; v32=1; v33=1; -v34=1; v35=1; v36=1; v37=1; v38=1; v39=1; v40=1; v41=1; -v42=1; v43=1; v44=1; v45=1; v46=1; v47=1; v48=1; v49=1; -v50=1; v51=1; v52=1; v53=1; v54=1; v55=1; v56=1; v57=1; -v58=1; v59=1; v60=1; v61=1; v62=1; v63=1; v64=1; v65=1; -v66=1; v67=1; v68=1; v69=1; v70=1; v71=1; v72=1; v73=1; -v74=1; v75=1; v76=1; v77=1; v78=1; v79=1; v80=1; v81=1; -v82=1; v83=1; v84=1; v85=1; v86=1; v87=1; v88=1; v89=1; -v90=1; v91=1; v92=1; v93=1; v94=1; v95=1; v96=1; v97=1; -v98=1; v99=1; v100=1; v101=1; v102=1; v103=1; v104=1; v105=1; -v106=1; v107=1; v108=1; v109=1; v110=1; v111=1; v112=1; v113=1; -v114=1; v115=1; v116=1; v117=1; v118=1; v119=1; v120=1; v121=1; -v122=1; v123=1; v124=1; v125=1; v126=1; v127=1; v128=1; v129=1; -v130=1; v131=1; v132=1; v133=1; v134=1; v135=1; v136=1; v137=1; -v138=1; v139=1; v140=1; v141=1; v142=1; v143=1; v144=1; v145=1; -v146=1; v147=1; v148=1; v149=1; v150=1; v151=1; v152=1; v153=1; -v154=1; v155=1; v156=1; v157=1; v158=1; v159=1; v160=1; v161=1; -v162=1; v163=1; v164=1; v165=1; v166=1; v167=1; v168=1; v169=1; -v170=1; v171=1; v172=1; v173=1; v174=1; v175=1; v176=1; v177=1; -v178=1; v179=1; v180=1; v181=1; v182=1; v183=1; v184=1; v185=1; -v186=1; v187=1; v188=1; v189=1; v190=1; v191=1; v192=1; v193=1; -v194=1; v195=1; v196=1; v197=1; v198=1; v199=1; v200=1; v201=1; -v202=1; v203=1; v204=1; v205=1; v206=1; v207=1; v208=1; v209=1; -v210=1; v211=1; v212=1; v213=1; v214=1; v215=1; v216=1; v217=1; -v218=1; v219=1; v220=1; v221=1; v222=1; v223=1; v224=1; v225=1; -v226=1; v227=1; v228=1; v229=1; v230=1; v231=1; v232=1; v233=1; -v234=1; v235=1; v236=1; v237=1; v238=1; v239=1; v240=1; v241=1; -v242=1; v243=1; v244=1; v245=1; v246=1; v247=1; v248=1; v249=1; -v250=1; -v251={k1 = 1}; -v252=1; -print(2 * v251.k1, v251.k1 * 2); -- 2 2, OK -v253=1; -print(2 * v251.k1, v251.k1 * 2); -- 1 2, ??? -]], -patch = [[ -*lcode.c: -@@ -657,10 +657,16 @@ - if (constfolding(op, e1, e2)) - return; - else { -- int o1 = luaK_exp2RK(fs, e1); - int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; -- freeexp(fs, e2); -- freeexp(fs, e1); -+ int o1 = luaK_exp2RK(fs, e1); -+ if (o1 > o2) { -+ freeexp(fs, e1); -+ freeexp(fs, e2); -+ } -+ else { -+ freeexp(fs, e2); -+ freeexp(fs, e1); -+ } - e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2); - e1->k = VRELOCABLE; - } -@@ -718,10 +724,15 @@ - luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */ - break; - } -- default: { -+ case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: -+ case OPR_MOD: case OPR_POW: { - if (!isnumeral(v)) luaK_exp2RK(fs, v); - break; - } -+ default: { -+ luaK_exp2RK(fs, v); -+ break; -+ } - } - } -]], -} - -Bug{ -what = [[assignment of nil to parameter may be optimized away]], -report = [[Thomas Lauer, on 03/2007]], -since = [[5.1]], -example = [[ -function f (a) - a=nil - return a -end - -print(f("test")) -]], -patch = [[ -*lcode.c: -@@ -35,16 +35,20 @@ - void luaK_nil (FuncState *fs, int from, int n) { - Instruction *previous; - if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ -- if (fs->pc == 0) /* function start? */ -- return; /* positions are already clean */ -- previous = &fs->f->code[fs->pc-1]; -- if (GET_OPCODE(*previous) == OP_LOADNIL) { -- int pfrom = GETARG_A(*previous); -- int pto = GETARG_B(*previous); -- if (pfrom <= from && from <= pto+1) { /* can connect both? */ -- if (from+n-1 > pto) -- SETARG_B(*previous, from+n-1); -- return; -+ if (fs->pc == 0) { /* function start? */ -+ if (from >= fs->nactvar) -+ return; /* positions are already clean */ -+ } -+ else { -+ previous = &fs->f->code[fs->pc-1]; -+ if (GET_OPCODE(*previous) == OP_LOADNIL) { -+ int pfrom = GETARG_A(*previous); -+ int pto = GETARG_B(*previous); -+ if (pfrom <= from && from <= pto+1) { /* can connect both? */ -+ if (from+n-1 > pto) -+ SETARG_B(*previous, from+n-1); -+ return; -+ } - } - } - } -]], -} - - -Bug{ -what = [[__concat metamethod converts numbers to strings]], -report = [[Paul Winwood, on 12/2006]], -since = [[5.0]], -example = [[ -a = {} -setmetatable(a, {__concat = function (a,b) print(type(a), type(b)) end}) -a = 4 .. a -]], -patch = [[ -*lvm.c: -@@ -281,10 +281,12 @@ - do { - StkId top = L->base + last + 1; - int n = 2; /* number of elements handled in this pass (at least 2) */ -- if (!tostring(L, top-2) || !tostring(L, top-1)) { -+ if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { - if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) - luaG_concaterror(L, top-2, top-1); -- } else if (tsvalue(top-1)->len > 0) { /* if len=0, do nothing */ -+ } else if (tsvalue(top-1)->len == 0) /* second op is empty? */ -+ (void)tostring(L, top - 2); /* result is first op (as string) */ -+ else { - /* at least two string values; get as many as possible */ - size_t tl = tsvalue(top-1)->len; - char *buffer; -]], -} - - -Bug{ -what = [[As a library, loadlib.c should not access Lua internals -(via lobject.h)]], -report = [[Jérôme Vuarand, on 03/2007]], -since = [[5.0]], -example = [[the bug has no effect on external behavior]], -patch = [[remove the '#include "lobject.h" and use -'lua_pushfstring' instead of 'luaO_pushfstring']], -} - - - ------------------------------------------------------------------ --- Lua 5.1.2 - -Bug{ -what = [[Lua may close standard files, -which then may be used by C]], -report = [[David Manura/Ross Berteig, on 04/2007]], -since = [[]], -example = [[ -io.close(io.stderr) --- in some systems, following attempts to write to 'stderr' may crash -a = a + 1 -]], -patch = [[ -]], -} - -Bug{ -what = [[code generated for "-nil", "-true", and "-false" is wrong]], -report = [[David Manura/Rici Lake, on 04/2007]], -since = [[5.1]], -example = [[print(-nil)]], -patch = [[ -lcode.c: -@@ -699,7 +699,7 @@ - e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; - switch (op) { - case OPR_MINUS: { -- if (e->k == VK) -+ if (!isnumeral(e)) - luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */ - codearith(fs, OP_UNM, e, &e2); - break; -]], -} - -Bug{ -what = [[Count hook may be called without being set.]], -report = [[Mike Pall, on 05/2007]], -since = [[?]], -example = [[]], -patch = [[ -lvm.c: -@@ -61,11 +61,9 @@ - lu_byte mask = L->hookmask; - const Instruction *oldpc = L->savedpc; - L->savedpc = pc; -- if (mask > LUA_MASKLINE) { /* instruction-hook set? */ -- if (L->hookcount == 0) { -- resethookcount(L); -- luaD_callhook(L, LUA_HOOKCOUNT, -1); -- } -+ if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) { -+ resethookcount(L); -+ luaD_callhook(L, LUA_HOOKCOUNT, -1); - } - if (mask & LUA_MASKLINE) { - Proto *p = ci_func(L->ci)->l.p; -]], -} - -Bug{ -what = [[recursive coroutines may overflow C stack]], -report = [[ , on ]], -since = [[5.0]], -example = [[ -a = function(a) coroutine.wrap(a)(a) end -a(a) -]], -patch = [[The 'nCcalls' counter should be shared by all threads. -(That is, it should be declared in the 'global_State' structure, -not in 'lua_State'.) -]], -} - -Bug{ -what = [[wrong error message in some concatenations]], -report = [[Alex Davies, on 05/2007]], -since = [[5.1.2]], -example = [[a = nil; a = (1)..a]], -patch = [[ -ldebug.c: -@@ -563,8 +563,8 @@ - - - void luaG_concaterror (lua_State *L, StkId p1, StkId p2) { -- if (ttisstring(p1)) p1 = p2; -- lua_assert(!ttisstring(p1)); -+ if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; -+ lua_assert(!ttisstring(p1) && !ttisnumber(p1)); - luaG_typeerror(L, p1, "concatenate"); - } - -]], -} - -Bug{ -what = [[Very small numbers all collide in the hash function. -(This creates only performance problems; the behavoir is correct.)]], -report = [[, on ]], -since = [[5.0]], -example = [[]], -patch = [[ -ltable.c: -87,88c87,88 -< n += 1; /* normalize number (avoid -0) */ -< lua_assert(sizeof(a) <= sizeof(n)); ---- -> if (luai_numeq(n, 0)) /* avoid problems with -0 */ -> return gnode(t, 0); -]], -} - -Bug{ -what = [[Too many variables in an assignment may cause a -C stack overflow]], -report = [[Mike Pall, on 07/2007]], -since = [[5.0]], -example = [[ -$ ulimit -s 1024 # Reduce C stack to 1MB for quicker results -$ lua -e 'local s = "a,"; for i=1,18 do s = s..s end print(loadstring("local a;"..s.."a=nil", ""))' -]], -patch = [[ -lparser.c: -@@ -938,6 +938,8 @@ - primaryexp(ls, &nv.v); - if (nv.v.k == VLOCAL) - check_conflict(ls, lh, &nv.v); -+ luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls, -+ "variable names"); - assignment(ls, &nv, nvars+1); - } - else { /* assignment -> `=' explist1 */ -]], -} - -Bug{ -what = [[An error in a module loaded through the '-l' option -shows no traceback]], -report = [[David Manura, on 08/2007]], -since = [[5.1]], -example = [[lua -ltemp (assuming temp.lua has an error)]], -patch = [[ -lua.c: -@@ -144,7 +144,7 @@ - static int dolibrary (lua_State *L, const char *name) { - lua_getglobal(L, "require"); - lua_pushstring(L, name); -- return report(L, lua_pcall(L, 1, 0, 0)); -+ return report(L, docall(L, 1, 1)); - } -]], -} - -Bug{ -what = [['gsub' may go wild when wrongly called without its third -argument and with a large subject]], -report = [[Florian Berger, on 10/2007]], -since = [[5.1]], -example = [[ -x = string.rep('a', 10000) .. string.rep('b', 10000) -print(#string.gsub(x, 'b')) -]], -patch = [[ -lstrlib.c: -@@ -631,6 +631,2 @@ - } -- default: { -- luaL_argerror(L, 3, "string/function/table expected"); -- return; -- } - } -@@ -650,2 +646,3 @@ - const char *p = luaL_checkstring(L, 2); -+ int tr = lua_type(L, 3); - int max_s = luaL_optint(L, 4, srcl+1); -@@ -655,2 +652,5 @@ - luaL_Buffer b; -+ luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || -+ tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, -+ "string/function/table expected"); - luaL_buffinit(L, &b); -]], -} - -Bug{ -what = [[table.remove removes last element of a table when given -an out-of-bound index]], -report = [[Patrick Donnelly, on 11/2007]], -since = [[5.0]], -example = [[ -a = {1,2,3} -table.remove(a, 4) -print(a[3]) --> nil (should be 3) -]], -patch = [[ -ltablib.c: -@@ -118,7 +118,8 @@ - static int tremove (lua_State *L) { - int e = aux_getn(L, 1); - int pos = luaL_optint(L, 2, e); -- if (e == 0) return 0; /* table is `empty' */ -+ if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ -+ return 0; /* nothing to remove */ - luaL_setn(L, 1, e - 1); /* t.n = n-1 */ - lua_rawgeti(L, 1, pos); /* result = t[pos] */ - for ( ;pos debug.setfenv(3, {}) -]], -patch = [[ -lapi.c: -@@ -749,7 +749,7 @@ - res = 0; - break; - } -- luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); -+ if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); - L->top--; - lua_unlock(L); - return res; -]], -} - -Bug{ -what = [[stand-alone interpreter shows incorrect error message -when the "message" is a coroutine]], -report = [[Patrick Donnelly, on 17/12/2007]], -since = [[5.1]], -example = [[> error(coroutine.create(function() end))]], -patch = [[ -lua.c: -@@ -74,6 +74,8 @@ - - - static int traceback (lua_State *L) { -+ if (!lua_isstring(L, 1)) /* 'message' not a string? */ -+ return 1; /* keep it intact */ - lua_getfield(L, LUA_GLOBALSINDEX, "debug"); - if (!lua_istable(L, -1)) { - lua_pop(L, 1); - -]], -} - -Bug{ -what = [[debug.sethook/gethook may overflow the thread's stack]], -report = [[Ivko Stanilov, on 2008/01/04]], -since = [[5.1]], -example = [[ -a = coroutine.create(function() yield() end) -coroutine.resume(a) -debug.sethook(a) -- may overflow the stack of 'a' -]], -patch = [[ -ldblib.c: -@@ -268,12 +268,11 @@ - count = luaL_optint(L, arg+3, 0); - func = hookf; mask = makemask(smask, count); - } -- gethooktable(L1); -- lua_pushlightuserdata(L1, L1); -+ gethooktable(L); -+ lua_pushlightuserdata(L, L1); - lua_pushvalue(L, arg+1); -- lua_xmove(L, L1, 1); -- lua_rawset(L1, -3); /* set new hook */ -- lua_pop(L1, 1); /* remove hook table */ -+ lua_rawset(L, -3); /* set new hook */ -+ lua_pop(L, 1); /* remove hook table */ - lua_sethook(L1, func, mask, count); /* set hooks */ - return 0; - } -@@ -288,11 +287,10 @@ - if (hook != NULL && hook != hookf) /* external hook? */ - lua_pushliteral(L, "external hook"); - else { -- gethooktable(L1); -- lua_pushlightuserdata(L1, L1); -- lua_rawget(L1, -2); /* get hook */ -- lua_remove(L1, -2); /* remove hook table */ -- lua_xmove(L1, L, 1); -+ gethooktable(L); -+ lua_pushlightuserdata(L, L1); -+ lua_rawget(L, -2); /* get hook */ -+ lua_remove(L, -2); /* remove hook table */ - } - lua_pushstring(L, unmakemask(mask, buff)); - lua_pushinteger(L, lua_gethookcount(L1)); -]] -} - - - ------------------------------------------------------------------ --- Lua 5.1.3 - -Bug{ -what = [[LUAI_MAXCSTACK must be smaller than -LUA_REGISTRYINDEX]], -report = [[Patrick Donnelly, on 2008/02/11]], -since = [[5.1.3]], -example = [[ -j = 1e4 -co = coroutine.create(function() - t = {} - for i = 1, j do t[i] = i end - return unpack(t) -end) -print(coroutine.resume(co)) -]], -patch = [[ -luaconf.h: -443c443,444 -< ** functions to consume unlimited stack space. ---- -> ** functions to consume unlimited stack space. (must be smaller than -> ** -LUA_REGISTRYINDEX) -445,446c446 -< #define LUAI_MCS_AUX ((int)(INT_MAX / (4*sizeof(LUA_NUMBER)))) -< #define LUAI_MAXCSTACK (LUAI_MCS_AUX > SHRT_MAX ? SHRT_MAX : LUAI_MCS_AUX) ---- -> #define LUAI_MAXCSTACK 8000 -]], -} - -Bug{ -what = [[coroutine.resume pushes element without ensuring stack size]], -report = [[on 2008/02/11]], -since = [[5.0]], -example = [[(this bug cannot be detected without internal assertions)]], -patch = [[ -lbaselib.c: -@@ -526,7 +526,7 @@ - status = lua_resume(co, narg); - if (status == 0 || status == LUA_YIELD) { - int nres = lua_gettop(co); -- if (!lua_checkstack(L, nres)) -+ if (!lua_checkstack(L, nres + 1)) - luaL_error(L, "too many results to resume"); - lua_xmove(co, L, nres); /* move yielded values */ - return nres; -]], -} - -Bug{ -what = [[lua_checkstack may have arithmetic overflow for large 'size']], -report = [[Patrick Donnelly, on 2008/02/12]], -since = [[5.0]], -example = [[ -print(unpack({1,2,3}, 0, 2^31-3)) -]], -patch = [[ ---- lapi.c 2008/01/03 15:20:39 2.55.1.3 -+++ lapi.c 2008/02/14 16:05:21 -@@ -93,15 +93,14 @@ - - - LUA_API int lua_checkstack (lua_State *L, int size) { -- int res; -+ int res = 1; - lua_lock(L); -- if ((L->top - L->base + size) > LUAI_MAXCSTACK) -+ if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) - res = 0; /* stack overflow */ -- else { -+ else if (size > 0) { - luaD_checkstack(L, size); - if (L->ci->top < L->top + size) - L->ci->top = L->top + size; -- res = 1; - } - lua_unlock(L); - return res; -]], -} - -Bug{ -what = [[unpack with maximum indices may crash due to arithmetic overflow]], -report = [[Patrick Donnelly, on 2008/02/12]], -since = [[5.1]], -example = [[ -print(unpack({1,2,3}, 2^31-1, 2^31-1)) -]], -patch = [[ ---- lbaselib.c 2008/02/11 16:24:24 1.191.1.5 -+++ lbaselib.c 2008/02/14 16:10:25 -@@ -344,10 +344,12 @@ - luaL_checktype(L, 1, LUA_TTABLE); - i = luaL_optint(L, 2, 1); - e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); -+ if (i > e) return 0; /* empty range */ - n = e - i + 1; /* number of elements */ -- if (n <= 0) return 0; /* empty range */ -- luaL_checkstack(L, n, "table too big to unpack"); -- for (; i<=e; i++) /* push arg[i...e] */ -+ if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ -+ return luaL_error(L, "too many results to unpack"); -+ lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ -+ while (i++ < e) /* push arg[i + 1...e] */ - lua_rawgeti(L, 1, i); - return n; - } -]], -} - -Bug{ -what = [[The validator for precompiled code has several flaws that -allow malicious binary code to crash the application]], -report = [[Peter Cawley, on 2008/03/24]], -since = [[5.0]], -example = [[ -a = string.dump(function()return;end) -a = a:gsub(string.char(30,37,122,128), string.char(34,0,0), 1) -loadstring(a)() -]], -patch = [[ ---- ldebug.c 2007/12/28 15:32:23 2.29.1.3 -+++ ldebug.c 2008/04/04 15:15:40 -@@ -275,12 +275,12 @@ - - static int precheck (const Proto *pt) { - check(pt->maxstacksize <= MAXSTACK); -- lua_assert(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize); -- lua_assert(!(pt->is_vararg & VARARG_NEEDSARG) || -+ check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize); -+ check(!(pt->is_vararg & VARARG_NEEDSARG) || - (pt->is_vararg & VARARG_HASARG)); - check(pt->sizeupvalues <= pt->nups); - check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0); -- check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN); -+ check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN); - return 1; - } - -@@ -363,7 +363,11 @@ - } - switch (op) { - case OP_LOADBOOL: { -- check(c == 0 || pc+2 < pt->sizecode); /* check its jump */ -+ if (c == 1) { /* does it jump? */ -+ check(pc+2 < pt->sizecode); /* check its jump */ -+ check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST || -+ GETARG_C(pt->code[pc+1]) != 0); -+ } - break; - } - case OP_LOADNIL: { -@@ -428,7 +432,10 @@ - } - case OP_SETLIST: { - if (b > 0) checkreg(pt, a + b); -- if (c == 0) pc++; -+ if (c == 0) { -+ pc++; -+ check(pc < pt->sizecode - 1); -+ } - break; - } - case OP_CLOSURE: { -]], -} - -Bug{ -what = [[maliciously crafted precompiled code can blow the C stack]], -report = [[Greg Falcon, on 2008/03/25]], -since = [[5.0]], -example = [[ -function crash(depth) - local init = '\27\76\117\97\81\0\1\4\4\4\8\0\7\0\0\0\61\115\116' .. - '\100\105\110\0\1\0\0\0\1\0\0\0\0\0\0\2\2\0\0\0\36' .. - '\0\0\0\30\0\128\0\0\0\0\0\1\0\0\0\0\0\0\0\1\0\0\0' .. - '\1\0\0\0\0\0\0\2' - local mid = '\1\0\0\0\30\0\128\0\0\0\0\0\0\0\0\0\1\0\0\0\1\0\0\0\0' - local fin = '\0\0\0\0\0\0\0\2\0\0\0\1\0\0\0\1\0\0\0\1\0\0\0\2\0' .. - '\0\0\97\0\1\0\0\0\1\0\0\0\0\0\0\0' - local lch = '\2\0\0\0\36\0\0\0\30\0\128\0\0\0\0\0\1\0\0\0\0\0\0' .. - '\0\1\0\0\0\1\0\0\0\0\0\0\2' - local rch = '\0\0\0\0\0\0\0\2\0\0\0\1\0\0\0\1\0\0\0\1\0\0\0\2\0' .. - '\0\0\97\0\1\0\0\0\1' - for i=1,depth do lch,rch = lch..lch,rch..rch end - loadstring(init .. lch .. mid .. rch .. fin) -end -for i=1,25 do print(i); crash(i) end -]], -patch = [[ ---- lundump.c 2008/04/04 16:00:45 2.7.1.3 -+++ lundump.c 2008/04/04 19:51:41 2.7.1.4 -@@ -161,7 +161,9 @@ - - static Proto* LoadFunction(LoadState* S, TString* p) - { -- Proto* f=luaF_newproto(S->L); -+ Proto* f; -+ if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); -+ f=luaF_newproto(S->L); - setptvalue2s(S->L,S->L->top,f); incr_top(S->L); - f->source=LoadString(S); if (f->source==NULL) f->source=p; - f->linedefined=LoadInt(S); -@@ -175,6 +177,7 @@ - LoadDebug(S,f); - IF (!luaG_checkcode(f), "bad code"); - S->L->top--; -+ S->L->nCcalls--; - return f; - } -]], -} - -Bug{ -what = [[code validator may reject (maliciously crafted) correct code]], -report = [[Greg Falcon, on 2008/03/26]], -since = [[5.0]], -example = [[ -z={} -for i=1,27290 do z[i]='1,' end -z = 'if 1+1==2 then local a={' .. table.concat(z) .. '} end' -func = loadstring(z) -print(loadstring(string.dump(func))) -]], -patch = [[ ---- ldebug.c 2008/04/04 15:30:05 2.29.1.4 -+++ ldebug.c 2008/04/04 15:47:10 -@@ -346,9 +346,18 @@ - int dest = pc+1+b; - check(0 <= dest && dest < pt->sizecode); - if (dest > 0) { -- /* cannot jump to a setlist count */ -- Instruction d = pt->code[dest-1]; -- check(!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)); -+ int j; -+ /* check that it does not jump to a setlist count; this -+ is tricky, because the count from a previous setlist may -+ have the same value of an invalid setlist; so, we must -+ go all the way back to the first of them (if any) */ -+ for (j = 0; j < dest; j++) { -+ Instruction d = pt->code[dest-1-j]; -+ if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break; -+ } -+ /* if 'j' is even, previous value is not a setlist (even if -+ it looks like one) */ -+ check((j&1) == 0); - } - } - break; -]], -} - -Bug{ -what = [[maliciously crafted precompiled code can inject invalid boolean -values into Lua code]], -report = [[Greg Falcon, on 2008/03/27]], -since = [[5.0]], -example = [[ -maybe = string.dump(function() return ({[true]=true})[true] end) -maybe = maybe:gsub('\1\1','\1\2') -maybe = loadstring(maybe)() -assert(type(maybe) == "boolean" and maybe ~= true and maybe ~= false) -]], -patch = [[ ---- lundump.c 2008/01/18 16:39:11 2.7.1.2 -+++ lundump.c 2008/04/04 15:50:39 -@@ -115,7 +115,7 @@ - setnilvalue(o); - break; - case LUA_TBOOLEAN: -- setbvalue(o,LoadChar(S)); -+ setbvalue(o,LoadChar(S)!=0); - break; - case LUA_TNUMBER: - setnvalue(o,LoadNumber(S)); -]], -} - - -Bug{ -what = [['string.byte' gets confused with some out-of-range negative indices]], -report = [[Mike Pall, on 2008/06/03]], -since = [[5.1]], -example = [[ -print(string.byte("abc", -5)) --> 97 98 99 (should print nothing) -]], -patch = [[ ---- lstrlib.c 2007/12/28 15:32:23 1.132.1.3 -+++ lstrlib.c 2008/07/05 11:53:42 -@@ -35,7 +35,8 @@ - - static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) { - /* relative string position: negative means back from end */ -- return (pos>=0) ? pos : (ptrdiff_t)len+pos+1; -+ if (pos < 0) pos += (ptrdiff_t)len + 1; -+ return (pos >= 0) ? pos : 0; - } - - -]], -} - - -Bug{ -what = [[user-requested GC step may loop forever]], -report = [[Makoto Hamanaka, on 2008/07/01]], -since = [[5.1]], -example = [[ -collectgarbage("setpause", 100) -- small value -collectgarbage("setstepmul", 2000) -- large value -collectgarbage("step",0) -]], -patch = [[ ---- lapi.c 2008/02/14 16:46:39 2.55.1.4 -+++ lapi.c 2008/07/04 18:34:48 -@@ -929,10 +929,13 @@ - g->GCthreshold = g->totalbytes - a; - else - g->GCthreshold = 0; -- while (g->GCthreshold <= g->totalbytes) -+ while (g->GCthreshold <= g->totalbytes) { - luaC_step(L); -- if (g->gcstate == GCSpause) /* end of cycle? */ -- res = 1; /* signal it */ -+ if (g->gcstate == GCSpause) { /* end of cycle? */ -+ res = 1; /* signal it */ -+ break; -+ } -+ } - break; - } - case LUA_GCSETPAUSE: { -]], -} - - -Bug{ -what = [['module' may change the environment of a C function]], -report = [[Peter Cawley, on 2008/07/16]], -since = [[5.1]], -example = [[ -pcall(module, "xuxu") -assert(debug.getfenv(pcall) == xuxu) -]], -patch = [[ ---- loadlib.c 2007/12/28 14:58:43 1.52.1.2 -+++ loadlib.c 2008/08/05 19:39:00 -@@ -506,8 +506,11 @@ - - static void setfenv (lua_State *L) { - lua_Debug ar; -- lua_getstack(L, 1, &ar); -- lua_getinfo(L, "f", &ar); -+ if (lua_getstack(L, 1, &ar) == 0 || -+ lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ -+ lua_iscfunction(L, -1)) -+ luaL_error(L, "function " LUA_QL("module") -+ " not called from a Lua function"); - lua_pushvalue(L, -2); - lua_setfenv(L, -2); - lua_pop(L, 1); -]], -} - - -Bug{ -what = [[internal macro 'svalue' is wrong]], -report = [[Martijn van Buul, on 2008/08/04]], -since = [[5.1]], -example = [[ -/* in luaconf.h */ -#define LUAI_USER_ALIGNMENT_T union { char b[32]; } -]], -patch = [[ ---- lobject.h 2007/12/27 13:02:25 2.20.1.1 -+++ lobject.h 2008/08/05 19:40:48 -@@ -210,3 +210,3 @@ - #define getstr(ts) cast(const char *, (ts) + 1) --#define svalue(o) getstr(tsvalue(o)) -+#define svalue(o) getstr(rawtsvalue(o)) - -]], -} - - ------------------------------------------------------------------ --- Lua 5.1.4 - -Bug{ -what = [[malicious zero-length string in binary code may segfault Lua]], -report = [[Peter Cawley, on 2008/09/01]], -since = [[5.1]], -example = [[ -loadstring(('').dump(function()X''end):gsub('\2%z%z%zX','\0\0\0'))() -]], -patch = [[ -]], -} - - -Bug{ -what = [[wrong code generation for some particular boolean expressions]], -report = [[Brian Kelley, on 2009/04/15]], -since = [[5.0]], -example = [[ -print(((1 or false) and true) or false) --> 1 --- should be 'true' -]], -patch = [[ ---- lcode.c 2007/12/28 15:32:23 2.25.1.3 -+++ lcode.c 2009/06/15 14:07:34 -@@ -544,15 +544,18 @@ - pc = NO_JUMP; /* always true; do nothing */ - break; - } -- case VFALSE: { -- pc = luaK_jump(fs); /* always jump */ -- break; -- } - case VJMP: { - invertjump(fs, e); - pc = e->u.s.info; - break; - } -+ case VFALSE: { -+ if (!hasjumps(e)) { -+ pc = luaK_jump(fs); /* always jump */ -+ break; -+ } -+ /* else go through */ -+ } - default: { - pc = jumponcond(fs, e, 0); - break; -@@ -572,14 +575,17 @@ - pc = NO_JUMP; /* always false; do nothing */ - break; - } -- case VTRUE: { -- pc = luaK_jump(fs); /* always jump */ -- break; -- } - case VJMP: { - pc = e->u.s.info; - break; - } -+ case VTRUE: { -+ if (!hasjumps(e)) { -+ pc = luaK_jump(fs); /* always jump */ -+ break; -+ } -+ /* else go through */ -+ } - default: { - pc = jumponcond(fs, e, 1); - break; -]], -} - -Bug{ -what = [['luaV_settable' may invalidate a reference to a table and try -to reuse it]], -report = [[Mark Feldman, on 2009/06/27]], -since = [[5.0]], -example = [[ -grandparent = {} -grandparent.__newindex = function(s,_,_) print(s) end - -parent = {} -parent.__newindex = parent -setmetatable(parent, grandparent) - -child = setmetatable({}, parent) -child.foo = 10 --> (crash on some machines) -]], -patch = [[ ---- lvm.c 2007/12/28 15:32:23 2.63.1.3 -+++ lvm.c 2009/07/01 20:36:59 -@@ -133,6 +133,7 @@ - - void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { - int loop; -+ TValue temp; - for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; - if (ttistable(t)) { /* `t' is a table? */ -@@ -152,7 +153,9 @@ - callTM(L, tm, t, key, val); - return; - } -- t = tm; /* else repeat with `tm' */ -+ /* else repeat with `tm' */ -+ setobj(L, &temp, tm); /* avoid pointing inside table (may rehash) */ -+ t = &temp; - } - luaG_runerror(L, "loop in settable"); - } -]], -} - -Bug{ -what = [[smart use of varargs may create functions that return too -many arguments and overflow the stack of C functions]], -report = [[Patrick Donnelly, on 2008/12/10]], -since = [[]], -example = [[ -local function lunpack(i, ...) - if i == 0 then return ... - else - return lunpack(i-1, 1, ...) - end -end - -Now, if C calls lunpack(n) with a huge n, it may end with -too many values in its stack and confuse its stack indices. -]], -patch = [[ -]], -} - -Bug{ -what = [['debug.getfenv' does not check whether it has an argument]], -report = [[Patrick Donnelly, 2009/07/30]], -since = [[5.1]], -example = [[debug.getfenv() -- should raise an error]], -patch = [[ ---- ldblib.c 2008/01/21 13:11:21 1.104.1.3 -+++ ldblib.c 2009/08/04 18:43:12 -@@ -45,6 +45,7 @@ - - - static int db_getfenv (lua_State *L) { -+ luaL_checkany(L, 1); - lua_getfenv(L, 1); - return 1; - } -]], -} - -Bug{ -what = [[GC may get stuck during a parser and avoids proper resizing of -the string table, -making its lists grow too much and degrading performance]], -report = [[Sean Conner, 2009/11/10]], -since = [[5.1]], -example = [[See http://lua-users.org/lists/lua-l/2009-11/msg00463.html]], -patch = [[ ---- llex.c 2007/12/27 13:02:25 2.20.1.1 -+++ llex.c 2009/11/23 14:49:40 -@@ -118,8 +118,10 @@ - lua_State *L = ls->L; - TString *ts = luaS_newlstr(L, str, l); - TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */ -- if (ttisnil(o)) -+ if (ttisnil(o)) { - setbvalue(o, 1); /* make sure `str' will not be collected */ -+ luaC_checkGC(L); -+ } - return ts; - } - -]] -} - -Bug{ -what = [['string.format' may get buffer as an argument when there are -missing arguments and format string is too long]], -report = [[Roberto I., 2010/04/12]], -since = [[5.0]], -example = [[ -x = string.rep("x", 10000) .. "%d" -print(string.format(x)) -- gives wrong error message -]], -patch = [[ ---- lstrlib.c 2008/07/11 17:27:21 1.132.1.4 -+++ lstrlib.c 2010/05/14 15:12:53 -@@ -754,6 +754,7 @@ - - - static int str_format (lua_State *L) { -+ int top = lua_gettop(L); - int arg = 1; - size_t sfl; - const char *strfrmt = luaL_checklstring(L, arg, &sfl); -@@ -768,7 +769,8 @@ - else { /* format item */ - char form[MAX_FORMAT]; /* to store the format (`%...') */ - char buff[MAX_ITEM]; /* to store the formatted item */ -- arg++; -+ if (++arg > top) -+ luaL_argerror(L, arg, "no value"); - strfrmt = scanformat(L, strfrmt, form); - switch (*strfrmt++) { - case 'c': { -]] -} - -Bug{ -what = [['io.read(op, "*n")' may return garbage if second read fails]], -report = [[Roberto I., 2010/04/12]], -since = [[5.0]], -example = [[ -print(io.read("*n", "*n")) --<< enter "10 hi" ---> file (0x884420) nil -]], -patch = [[ ---- liolib.c 2008/01/18 17:47:43 2.73.1.3 -+++ liolib.c 2010/05/14 15:29:29 -@@ -276,7 +276,10 @@ - lua_pushnumber(L, d); - return 1; - } -- else return 0; /* read fails */ -+ else { -+ lua_pushnil(L); /* "result" to be removed */ -+ return 0; /* read fails */ -+ } - } - - -]] -} - -Bug{ -what = [[wrong code generation for some particular boolean expressions]], -report = [[Thierry Van Elsuwe, 2011/01/20]], -since = [[5.0]], -example = [[ -print((('hi' or true) and true) or true) ---> hi (should be true) -print(((nil and nil) or false) and true) ---> nil (should be false) -]], -patch = [[ ---- lcode.c 2009/06/15 14:12:25 2.25.1.4 -+++ lcode.c 2011/01/31 14:44:25 -@@ -549,13 +549,6 @@ - pc = e->u.s.info; - break; - } -- case VFALSE: { -- if (!hasjumps(e)) { -- pc = luaK_jump(fs); /* always jump */ -- break; -- } -- /* else go through */ -- } - default: { - pc = jumponcond(fs, e, 0); - break; -@@ -579,13 +572,6 @@ - pc = e->u.s.info; - break; - } -- case VTRUE: { -- if (!hasjumps(e)) { -- pc = luaK_jump(fs); /* always jump */ -- break; -- } -- /* else go through */ -- } - default: { - pc = jumponcond(fs, e, 1); - break; -]] -} - -Bug{ -what = [[__newindex metamethod may not work if metatable is its own -metatable]], -report = [[Cuero Bugot, 2011/08/09]], -since = [[5.1]], -example = [[ -meta={} -setmetatable(meta, meta) -meta.__newindex = function(t, key, value) print("set") end -o = setmetatable({}, meta) -o.x = 10 -- should print 'set' -]], -patch = [[ ---- lvm.c 2009/07/01 21:10:33 2.63.1.4 -+++ lvm.c 2011/08/17 20:36:28 -@@ -142,6 +142,7 @@ - if (!ttisnil(oldval) || /* result is no nil? */ - (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */ - setobj2t(L, oldval, val); -+ h->flags = 0; - luaC_barriert(L, h, val); - return; - } -]] -} - -Bug{ -what = [[parser may collect a prototype while building it]], -report = [[Ingo van Lil, 2011/10/13]], -since = [[5.1.4 (caused by patch 5.1.4-6)]], -example = nil, -patch = [[ ---- lparser.c 2007/12/28 15:32:23 2.42.1.3 -+++ lparser.c 2011/10/17 13:10:43 -@@ -374,9 +374,9 @@ - lua_assert(luaG_checkcode(f)); - lua_assert(fs->bl == NULL); - ls->fs = fs->prev; -- L->top -= 2; /* remove table and prototype from the stack */ - /* last token read was anchored in defunct function; must reanchor it */ - if (fs) anchor_token(ls); -+ L->top -= 2; /* remove table and prototype from the stack */ - } - - -]] -} - - -Bug{ -what = [[When loading a file, -Lua may call the reader function again after it returned end of input -]], -report = [[Chris Howie, 2013/06/05]], -since = [[5.1]], -fix = [[5.2]], -example = [[ -load(function () print("called"); return nil end) ---> called ---> called (should be called only once!) -]], -patch = [[ ---- lzio.h 2007/12/27 13:02:25 1.21.1.1 -+++ lzio.h 2013/07/04 13:55:59 -@@ -59,6 +59,7 @@ - lua_Reader reader; - void* data; /* additional data */ - lua_State *L; /* Lua state (for reader) */ -+ int eoz; /* true if reader has no more data */ - }; - - ---- lzio.c 2007/12/27 13:02:25 1.31.1.1 -+++ lzio.c 2013/07/04 13:53:06 -@@ -22,10 +22,14 @@ - size_t size; - lua_State *L = z->L; - const char *buff; -+ if (z->eoz) return EOZ; - lua_unlock(L); - buff = z->reader(L, z->data, &size); - lua_lock(L); -- if (buff == NULL || size == 0) return EOZ; -+ if (buff == NULL || size == 0) { -+ z->eoz = 1; /* avoid calling reader function next time */ -+ return EOZ; -+ } - z->n = size - 1; - z->p = buff; - return char2int(*(z->p++)); -@@ -51,6 +55,7 @@ - z->data = data; - z->n = 0; - z->p = NULL; -+ z->eoz = 0; - } -]] -} - - ------------------------------------------------------------------ --- Lua 5.2.0 - -Bug{ -what = [[memory hoarding when creating Lua hooks for coroutines]], -report = [[Arseny Vakhrushev, 2012/01/16]], -since = [[5.1]], -fix = [[5.2.1]], -example = [[ -collectgarbage(); print(collectgarbage'count' * 1024) - -for i = 1, 100 do - local co = coroutine.create(function () end) - local x = {} - for j=1,1000 do x[j] = j end - debug.sethook(co, function () return x end, 'l') -end - -collectgarbage(); print(collectgarbage'count' * 1024) --- value should back to near the original level -]], -patch = [[ --- For 5.2 - ---- ldblib.c 2011/10/24 14:54:05 1.131 -+++ ldblib.c 2012/01/18 02:36:59 -@@ -253,14 +253,15 @@ - } - - --#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY); -+#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY) - - - static void hookf (lua_State *L, lua_Debug *ar) { - static const char *const hooknames[] = - {"call", "return", "line", "count", "tail call"}; - gethooktable(L); -- lua_rawgetp(L, -1, L); -+ lua_pushthread(L); -+ lua_rawget(L, -2); - if (lua_isfunction(L, -1)) { - lua_pushstring(L, hooknames[(int)ar->event]); - if (ar->currentline >= 0) -@@ -306,10 +307,15 @@ - count = luaL_optint(L, arg+3, 0); - func = hookf; mask = makemask(smask, count); - } -- gethooktable(L); -+ if (gethooktable(L) == 0) { /* creating hook table? */ -+ lua_pushstring(L, "k"); -+ lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ -+ lua_pushvalue(L, -1); -+ lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ -+ } -+ lua_pushthread(L1); lua_xmove(L1, L, 1); - lua_pushvalue(L, arg+1); -- lua_rawsetp(L, -2, L1); /* set new hook */ -- lua_pop(L, 1); /* remove hook table */ -+ lua_rawset(L, -3); /* set new hook */ - lua_sethook(L1, func, mask, count); /* set hooks */ - return 0; - } -@@ -325,7 +331,8 @@ - lua_pushliteral(L, "external hook"); - else { - gethooktable(L); -- lua_rawgetp(L, -1, L1); /* get hook */ -+ lua_pushthread(L1); lua_xmove(L1, L, 1); -+ lua_rawget(L, -2); /* get hook */ - lua_remove(L, -2); /* remove hook table */ - } - lua_pushstring(L, unmakemask(mask, buff)); -]] -} - -Bug{ -what = [[Lexical gets confused with some combination of arithmetic -operators and hexadecimal numbers]], -report = [[Alexandra Barros, 2012/01/17]], -since = [[5.2.0]], -fix = [[5.2.1]], -example = [[print(0xE+1)]], -patch = [[ ---- llex.c 2011/11/30 12:43:51 2.59 -+++ llex.c 2012/01/20 18:22:50 -@@ -223,12 +223,19 @@ - - /* LUA_NUMBER */ - static void read_numeral (LexState *ls, SemInfo *seminfo) { -+ const char *expo = "Ee"; -+ int first = ls->current; - lua_assert(lisdigit(ls->current)); -- do { -- save_and_next(ls); -- if (check_next(ls, "EePp")) /* exponent part? */ -+ save_and_next(ls); -+ if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */ -+ expo = "Pp"; -+ for (;;) { -+ if (check_next(ls, expo)) /* exponent part? */ - check_next(ls, "+-"); /* optional exponent sign */ -- } while (lislalnum(ls->current) || ls->current == '.'); -+ if (lisxdigit(ls->current) || ls->current == '.') -+ save_and_next(ls); -+ else break; -+ } - save(ls, '\0'); - buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ - if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ -]] -} - -Bug{ -what = [[Finalizers may call functions from a dynamic library after -the library has been unloaded]], -report = [[Josh Haberman, 2012/04/08]], -since = [[5.1]], -fix = [[5.2.1]], -example = [[ -local u = setmetatable({}, {__gc = function () foo() end}) -local m = require 'mod' -- 'mod' may be any dynamic library written in C -foo = m.foo -- 'foo' may be any function from 'mod' --- end program; it crashes -]], -patch = [[ -loadlib.c: -95c95 -< #define LIBPREFIX "LOADLIB: " ---- -> #define CLIBS "_CLIBS" -251,266c251,256 -< -< static void **ll_register (lua_State *L, const char *path) { -< void **plib; -< lua_pushfstring(L, "%s%s", LIBPREFIX, path); -< lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ -< if (!lua_isnil(L, -1)) /* is there an entry? */ -< plib = (void **)lua_touserdata(L, -1); -< else { /* no entry yet; create one */ -< lua_pop(L, 1); /* remove result from gettable */ -< plib = (void **)lua_newuserdata(L, sizeof(const void *)); -< *plib = NULL; -< luaL_setmetatable(L, "_LOADLIB"); -< lua_pushfstring(L, "%s%s", LIBPREFIX, path); -< lua_pushvalue(L, -2); -< lua_settable(L, LUA_REGISTRYINDEX); -< } ---- -> static void *ll_checkclib (lua_State *L, const char *path) { -> void *plib; -> lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); -> lua_getfield(L, -1, path); -> plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ -> lua_pop(L, 2); /* pop CLIBS table and 'plib' */ -270a261,270 -> static void ll_addtoclib (lua_State *L, const char *path, void *plib) { -> lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); -> lua_pushlightuserdata(L, plib); -> lua_pushvalue(L, -1); -> lua_setfield(L, -3, path); /* CLIBS[path] = plib */ -> lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */ -> lua_pop(L, 1); /* pop CLIBS table */ -> } -> -> -272,273c272,273 -< ** __gc tag method: calls library's `ll_unloadlib' function with the lib -< ** handle ---- -> ** __gc tag method for CLIBS table: calls 'll_unloadlib' for all lib -> ** handles in list CLIBS -276,278c276,281 -< void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); -< if (*lib) ll_unloadlib(*lib); -< *lib = NULL; /* mark library as closed */ ---- -> int n = luaL_len(L, 1); -> for (; n >= 1; n--) { /* for each handle, in reverse order */ -> lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ -> ll_unloadlib(lua_touserdata(L, -1)); -> lua_pop(L, 1); /* pop handle */ -> } -284,286c287,292 -< void **reg = ll_register(L, path); -< if (*reg == NULL) *reg = ll_load(L, path, *sym == '*'); -< if (*reg == NULL) return ERRLIB; /* unable to load library */ ---- -> void *reg = ll_checkclib(L, path); /* check loaded C libraries */ -> if (reg == NULL) { /* must load library? */ -> reg = ll_load(L, path, *sym == '*'); -> if (reg == NULL) return ERRLIB; /* unable to load library */ -> ll_addtoclib(L, path, reg); -> } -292c298 -< lua_CFunction f = ll_sym(L, *reg, sym); ---- -> lua_CFunction f = ll_sym(L, reg, sym); -675,676c681,683 -< /* create new type _LOADLIB */ -< luaL_newmetatable(L, "_LOADLIB"); ---- -> /* create table CLIBS to keep track of loaded C libraries */ -> luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); -> lua_createtable(L, 0, 1); /* metatable for CLIBS */ -678a686 -> lua_setmetatable(L, -2); -]] -} - -Bug{ -what = [[wrong handling of 'nCcalls' in coroutines]], -report = [[Alexander Gavrilov, 2012/04/18]], -since = [[5.2.0]], -fix = [[5.2.1]], -example = [[ -coroutine.wrap(function() - print(pcall(pcall,pcall,pcall,pcall,pcall,error,3)) -end)() -]], -patch = [[ ---- ldo.c 2011/11/29 15:55:08 2.102 -+++ ldo.c 2012/04/26 20:38:32 -@@ -402,8 +402,6 @@ - int n; - lua_assert(ci->u.c.k != NULL); /* must have a continuation */ - lua_assert(L->nny == 0); -- /* finish 'luaD_call' */ -- L->nCcalls--; - /* finish 'lua_callk' */ - adjustresults(L, ci->nresults); - /* call continuation function */ -@@ -513,7 +511,6 @@ - api_checknelems(L, n); - firstArg = L->top - n; /* yield results come from continuation */ - } -- L->nCcalls--; /* finish 'luaD_call' */ - luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ - } - unroll(L, NULL); -]] -} - -Bug{ -what = [[Internal Lua values may escape through the debug API]], -report = [[Dan Tull, 2012/04/20]], -since = [[5.1]], -fix = [[5.2.1]], -example = [[ --- for Lua 5.1 -local firsttime = true -local function foo () - if firsttime then - firsttime = false - return "a = 1" - else - for i = 1, 10 do - print(debug.getlocal(2, i)) - end - end -end - -print(load(foo)) -- prints some lines and then seg. fault. -]], -patch = [[ -]] -} - -Bug{ -what = [[Problems when yielding from debug hooks]], -report = [[Erik Cassel, 2012/06/05]], -since = [[5.2.0]], -fix = [[5.2.1]], -example = [[ -Set, in C, a line hook that simply yields, -and then call any Lua function. -You get an infinite loop of yields. -]], -patch = [[ -]] -} - - ------------------------------------------------------------------ --- Lua 5.2.1 - -Bug{ -what = [[Some patterns can overflow the C stack, due to recursion]], -report = [[Tim Starling, 2012/07/08]], -since = [[2.5]], -fix = [[5.2.2]], -example = [[print(string.find(string.rep("a", 2^20), string.rep(".?", 2^20)))]], -patch = [[ -]] -} - - -Bug{ -what = [['pcall' may not restore previous error function when -inside coroutines]], -report = [[Alexander Gavrilov, 2012/06/12]], -since = [[5.2.0]], -fix = [[5.2.2]], -example = [[ -function errfunc(x) - return 'errfunc' -end - -function test(do_yield) - print(do_yield and "yielding" or "not yielding") - pcall(function() -- this pcall sets errfunc back to none - if do_yield then - coroutine.yield() -- stops errfunc from being restored - end - end) - error('fail!') -end - -coro = coroutine.wrap(function() - print(xpcall(test, errfunc, false)) - print(xpcall(test, errfunc, true)) - print(xpcall(test, errfunc, false)) -end) - -coro() ---> not yielding ---> false errfunc ---> yielding -coro() ---> false temp:12: fail! <<<< should be 'errfunc' too ---> not yielding ---> false errfunc -]], -patch = [[ ---- ldo.c 2012/08/28 18:30:45 2.107 -+++ ldo.c 2012/09/23 15:49:55 -@@ -403,7 +403,11 @@ - int n; - lua_assert(ci->u.c.k != NULL); /* must have a continuation */ - lua_assert(L->nny == 0); -- /* finish 'lua_callk' */ -+ if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ -+ ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */ -+ L->errfunc = ci->u.c.old_errfunc; -+ } -+ /* finish 'lua_callk'/'lua_pcall' */ - adjustresults(L, ci->nresults); - /* call continuation function */ - if (!(ci->callstatus & CIST_STAT)) /* no call status? */ -]] -} - -Bug{ -what = [[Check for garbage collector in function calls does not cover -all paths]], -report = [[Roberto, 2012/08/15]], -since = [[5.2.1]], -fix = [[5.2.2]], -example = [[ -See -http://lua-users.org/lists/lua-l/2012-08/msg00149.html -]], -patch = [[ -@@ -311,6 +311,7 @@ - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); - ci->callstatus = 0; -+ luaC_checkGC(L); /* stack grow uses memory */ - if (L->hookmask & LUA_MASKCALL) - luaD_hook(L, LUA_HOOKCALL, -1); - lua_unlock(L); -@@ -338,6 +339,7 @@ - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = CIST_LUA; - L->top = ci->top; -+ luaC_checkGC(L); /* stack grow uses memory */ - if (L->hookmask & LUA_MASKCALL) - callhook(L, ci); - return 0; -@@ -393,7 +395,6 @@ - luaV_execute(L); /* call it */ - if (!allowyield) L->nny--; - L->nCcalls--; -- luaC_checkGC(L); - } -]] -} - -Bug{ -what = [[load/loadfile returns wrong result when given an environment -for a binary chunk with no upvalues]], -report = [[Vladimir Strakh, 2012/11/28]], -since = [[5.2.0]], -fix = [[5.2.2]], -example = [[ -f = load(string.dump(function () return 1 end), nil, "b", {}) -print(type(f)) --> table (whould be a function) -]], -patch = [[ ---- lbaselib.c 2012/04/27 14:13:19 1.274 -+++ lbaselib.c 2012/12/03 20:08:15 -@@ -244,5 +244,11 @@ - --static int load_aux (lua_State *L, int status) { -- if (status == LUA_OK) -+static int load_aux (lua_State *L, int status, int envidx) { -+ if (status == LUA_OK) { -+ if (envidx != 0) { /* 'env' parameter? */ -+ lua_pushvalue(L, envidx); /* environment for loaded function */ -+ if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ -+ lua_pop(L, 1); /* remove 'env' if not used by previous call */ -+ } - return 1; -+ } - else { -@@ -258,9 +264,5 @@ - const char *mode = luaL_optstring(L, 2, NULL); -- int env = !lua_isnone(L, 3); /* 'env' parameter? */ -+ int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ - int status = luaL_loadfilex(L, fname, mode); -- if (status == LUA_OK && env) { /* 'env' parameter? */ -- lua_pushvalue(L, 3); -- lua_setupvalue(L, -2, 1); /* set it as 1st upvalue of loaded chunk */ -- } -- return load_aux(L, status); -+ return load_aux(L, status, env); - } -@@ -309,5 +311,5 @@ - size_t l; -- int top = lua_gettop(L); - const char *s = lua_tolstring(L, 1, &l); - const char *mode = luaL_optstring(L, 3, "bt"); -+ int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ - if (s != NULL) { /* loading a string? */ -@@ -322,7 +324,3 @@ - } -- if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */ -- lua_pushvalue(L, 4); /* environment for loaded function */ -- lua_setupvalue(L, -2, 1); /* set it as 1st upvalue */ -- } -- return load_aux(L, status); -+ return load_aux(L, status, env); - } -]] -} - -Bug{ -what = [[Lua does not check memory use when creating error messages]], -report = [[John Dunn, 2012/09/24]], -since = [[5.2.0]], -fix = nil, -example = [[ -local code = "function test()\n bob.joe.larry = 23\n end" - -load(code)() - --- memory will grow steadly -for i = 1, math.huge do - pcall(test) - if i % 100000 == 0 then - io.write(collectgarbage'count'*1024, "\n") - end -end -]], -patch = [[ -]] -} - - - - - ------------------------------------------------------------------ --- Lua 5.2.2 - - -Bug{ -what = [[stack overflow in vararg functions with many fixed -parameters called with few arguments]], -report = [[云风, 2013/04/17]], -since = [[5.1]], -fix = [[5.2.3]], -example = [[ -function f(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, - p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, - p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, - p31, p32, p33, p34, p35, p36, p37, p38, p39, p40, - p41, p42, p43, p44, p45, p46, p48, p49, p50, ...) - local a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14 -end - -f() -- seg. fault (on some machines) -]], -patch = [[ ---- ldo.c 2012/10/01 14:05:04 2.108 -+++ ldo.c 2013/04/19 20:56:06 -@@ -324,7 +324,7 @@ - case LUA_TLCL: { /* Lua function: prepare its call */ - StkId base; - Proto *p = clLvalue(func)->p; -- luaD_checkstack(L, p->maxstacksize); -+ luaD_checkstack(L, p->maxstacksize + p->numparams); - func = restorestack(L, funcr); - n = cast_int(L->top - func) - 1; /* number of real arguments */ - for (; n < p->numparams; n++) -]], -} - -Bug{ -what = [[garbage collector can trigger too many times in recursive loops]], -report = [[Roberto, 2013/04/25]], -since = [[5.2.2]], -fix = [[5.2.3]], -example = [[ -function f() f() end -f() -- it takes too long before a "stack overflow" error -]], -patch = [[ ---- lgc.c 2013/04/12 18:48:47 2.140.1.1 -+++ lgc.c 2013/04/25 21:30:20 -@@ -495,2 +495,3 @@ - static lu_mem traversestack (global_State *g, lua_State *th) { -+ int n = 0; - StkId o = th->stack; -@@ -505,3 +506,9 @@ - } -- return sizeof(lua_State) + sizeof(TValue) * th->stacksize; -+ else { /* count call infos to compute size */ -+ CallInfo *ci; -+ for (ci = &th->base_ci; ci != th->ci; ci = ci->next) -+ n++; -+ } -+ return sizeof(lua_State) + sizeof(TValue) * th->stacksize + -+ sizeof(CallInfo) * n; - } -]] -} - --- [[]] -Bug{ -what = [[Wrong assert when reporting concatenation errors -(manifests only when Lua is compiled in debug mode)]], -report = [[Roberto, 2013/05/05]], -since = [[?]], -fix = [[5.2.3]], -example = [[ --- only with Lua compiled in debug mode -print({} .. 2) -]], -patch = [[ ---- ldebug.c 2013/04/12 18:48:47 2.90.1.1 -+++ ldebug.c 2013/05/05 14:38:30 -@@ -519,5 +519,5 @@ - l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) { - if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; -- lua_assert(!ttisstring(p1) && !ttisnumber(p2)); -+ lua_assert(!ttisstring(p1) && !ttisnumber(p1)); - luaG_typeerror(L, p1, "concatenate"); - } -]] -} - -Bug{ -what = [[Wrong error message in some short-cut expressions]], -report = [[Egor Skriptunoff, 2013/05/10]], -since = [[5.0]], -fix = [[5.2.3]], -example = [[ -> a,b,c = true,true,true -> (a and b or c)('', '') -stdin:1: attempt to call a boolean value (global 'c') - - (It should be global 'b' instead of 'c'.) -]], -patch = [[ ---- ldebug.c 2013/05/06 17:20:22 2.90.1.2 -+++ ldebug.c 2013/05/14 19:52:48 -@@ -327,12 +327,20 @@ - } - - -+static int filterpc (int pc, int jmptarget) { -+ if (pc < jmptarget) /* is code conditional (inside a jump)? */ -+ return -1; /* cannot know who sets that register */ -+ else return pc; /* current position sets that register */ -+} -+ -+ - /* - ** try to find last instruction before 'lastpc' that modified register 'reg' - */ - static int findsetreg (Proto *p, int lastpc, int reg) { - int pc; - int setreg = -1; /* keep last instruction that changed 'reg' */ -+ int jmptarget = 0; /* any code before this address is conditional */ - for (pc = 0; pc < lastpc; pc++) { - Instruction i = p->code[pc]; - OpCode op = GET_OPCODE(i); -@@ -341,33 +349,38 @@ - case OP_LOADNIL: { - int b = GETARG_B(i); - if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */ -- setreg = pc; -+ setreg = filterpc(pc, jmptarget); - break; - } - case OP_TFORCALL: { -- if (reg >= a + 2) setreg = pc; /* affect all regs above its base */ -+ if (reg >= a + 2) /* affect all regs above its base */ -+ setreg = filterpc(pc, jmptarget); - break; - } - case OP_CALL: - case OP_TAILCALL: { -- if (reg >= a) setreg = pc; /* affect all registers above base */ -+ if (reg >= a) /* affect all registers above base */ -+ setreg = filterpc(pc, jmptarget); - break; - } - case OP_JMP: { - int b = GETARG_sBx(i); - int dest = pc + 1 + b; - /* jump is forward and do not skip `lastpc'? */ -- if (pc < dest && dest <= lastpc) -- pc += b; /* do the jump */ -+ if (pc < dest && dest <= lastpc) { -+ if (dest > jmptarget) -+ jmptarget = dest; /* update 'jmptarget' */ -+ } - break; - } - case OP_TEST: { -- if (reg == a) setreg = pc; /* jumped code can change 'a' */ -+ if (reg == a) /* jumped code can change 'a' */ -+ setreg = filterpc(pc, jmptarget); - break; - } - default: - if (testAMode(op) && reg == a) /* any instruction that set A */ -- setreg = pc; -+ setreg = filterpc(pc, jmptarget); - break; - } - } -]] -} - -Bug{ -what = [[luac listings choke on long strings]], -report = [[Ashwin Hirschi, 2013/07/03]], -since = [[5.1.2]], -fix = [[5.2.3]], -example = [[ --- When you call 'luac -l' over this chunk, it chokes the output -s="Lorem ipsum dolor sit amet, consectetur, " -]], -patch = [[ ---- luac.c 2011-11-29 15:46:33 -0200 1.69 -+++ luac.c 2013-07-03 21:26:01 -0300 -@@ -251,7 +251,7 @@ - static void PrintConstant(const Proto* f, int i) - { - const TValue* o=&f->k[i]; -- switch (ttype(o)) -+ switch (ttypenv(o)) - { - case LUA_TNIL: - printf("nil"); -]] -} - -Bug{ -what = [[GC can collect a long string still in use during parser]], -report = [[Roberto, 2013/08/30]], -since = [[5.2]], -fix = [[5.2.3]], -example = [[This bug is very difficult to happen (and to reproduce), -because it depends on the GC running in a very specific way when -parsing a source code with long (larger than 40 characters) identifiers.]], -patch = [[ ---- ltable.h 2013/04/12 18:48:47 2.16.1.1 -+++ ltable.h 2013/08/30 15:34:24 -@@ -18,4 +18,8 @@ - #define invalidateTMcache(t) ((t)->flags = 0) - -+/* returns the key, given the value of a table entry */ -+#define keyfromval(v) \ -+ (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) -+ - - LUAI_FUNC const TValue *luaH_getint (Table *t, int key); - ---- llex.c 2013/04/12 18:48:47 2.63.1.1 -+++ llex.c 2013/08/30 15:34:59 -@@ -134,4 +134,7 @@ - luaC_checkGC(L); - } -+ else { /* string already present */ -+ ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */ -+ } - L->top--; /* remove string from stack */ - return ts; -]] -} - - -Bug{ -what = [[Call to macro 'luai_userstateclose' should be done only -after the calls to __gc methods.]], -report = [[Jean-Luc Jumpertz, 2013/09/02]], -since = [[ ]], -fix = nil, -example = [[No example]], -patch = [[ ---- lstate.c 2013/04/12 18:48:47 2.99.1.1 -+++ lstate.c 2013/11/08 17:39:57 -@@ -194,2 +194,4 @@ - g->gcrunning = 1; /* allow gc */ -+ g->version = lua_version(NULL); -+ luai_userstateopen(L); - } -@@ -224,2 +226,4 @@ - luaC_freeallobjects(L); /* collect all objects */ -+ if (g->version) /* closing a fully built state? */ -+ luai_userstateclose(L); - luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); -@@ -289,3 +293,3 @@ - g->panic = NULL; -- g->version = lua_version(NULL); -+ g->version = NULL; - g->gcstate = GCSpause; -@@ -308,4 +312,2 @@ - } -- else -- luai_userstateopen(L); - return L; -@@ -317,3 +319,2 @@ - lua_lock(L); -- luai_userstateclose(L); - close_state(L); -]] -} - - -Bug{ -what = [[Resuming the running coroutine makes it unyieldable]], -report = [[Florian Nücke, 2013/10/28]], -since = [[5.2]], -fix = [[5.2.3]], -example = [[ --- should print 'true' -print(coroutine.resume(coroutine.create(function() - coroutine.resume(coroutine.running()) - coroutine.yield() -end))) -]], -patch = [[ ---- ldo.c 2013/04/19 21:03:23 2.108.1.2 -+++ ldo.c 2013/11/08 18:20:57 -@@ -536,2 +536,3 @@ - int status; -+ int oldnny = L->nny; /* save 'nny' */ - lua_lock(L); -@@ -557,3 +558,3 @@ - } -- L->nny = 1; /* do not allow yields */ -+ L->nny = oldnny; /* restore 'nny' */ - L->nCcalls--; -]] -} - - - ------------------------------------------------------------------ --- Lua 5.2.3 - -Bug{ -what = [[compiler can optimize away overflow check in 'table.unpack']], -report = [[Paige DePol, 2014/03/30]], -since = [[5.1 (at least)]], -fix = nil, -example = [[ -> unpack({}, 0, 2^31 - 1) -(segfaults on some platforms with some compiler options) -]], -patch = [[ ---- ltablib.c 2013/04/12 18:48:47 1.65.1.1 -+++ ltablib.c 2014/05/07 16:32:55 1.65.1.2 -@@ -134,13 +135,14 @@ - - - static int unpack (lua_State *L) { -- int i, e, n; -+ int i, e; -+ unsigned int n; - luaL_checktype(L, 1, LUA_TTABLE); - i = luaL_optint(L, 2, 1); - e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); - if (i > e) return 0; /* empty range */ -- n = e - i + 1; /* number of elements */ -- if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ -+ n = (unsigned int)e - (unsigned int)i; /* number of elements minus 1 */ -+ if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n)) - return luaL_error(L, "too many results to unpack"); - lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ - while (i++ < e) /* push arg[i + 1...e] */ -]] -} - -Bug{ -what = [[Ephemeron table can wrongly collect entry with strong key]], -report = [[Jörg Richter, 2014/08/22]], -since = [[5.2]], -fix = nil, -example = [[ -(This bug is very hard to reproduce, -because it depends on a specific interleaving of -events between the incremental collector and the program.) -]], -patch = [[ ---- lgc.c 2013/04/26 18:22:05 2.140.1.2 -+++ lgc.c 2014/09/01 13:24:33 -@@ -403,7 +403,7 @@ - reallymarkobject(g, gcvalue(gval(n))); /* mark it now */ - } - } -- if (prop) -+ if (g->gcstate != GCSatomic || prop) - linktable(h, &g->ephemeron); /* have to propagate again */ - else if (hasclears) /* does table have white keys? */ - linktable(h, &g->allweak); /* may have to clean white keys */ -]] -} - -Bug{ -what = [[Chunk with too many lines can seg. fault]], -report = [[Roberto, 2014/11/14]], -since = [[5.1 (at least)]], -fix = nil, -example = [[ --- the cause of the bug is the use of an unitialized variable, so --- it cannot be reproduced reliably -local s = string.rep("\n", 2^24) -print(load(function () return s end)) -]], -patch = [[ ---- llex.c 2013/08/30 15:49:41 2.63.1.2 -+++ llex.c 2015/02/09 17:05:31 -@@ -153,5 +153,5 @@ - next(ls); /* skip `\n\r' or `\r\n' */ - if (++ls->linenumber >= MAX_INT) -- luaX_syntaxerror(ls, "chunk has too many lines"); -+ lexerror(ls, "chunk has too many lines", 0); - } - -]] -} - - ------------------------------------------------------------------ --- Lua 5.3.0 - -Bug{ -what = [['string.format("%f")' can cause a buffer overflow -(only when 'lua_Number' is long double!)]], -report = [[Roberto, 2015/01/13]], -since = [[5.3]], -fix = nil, -example = [[string.format("%.99f", 1e4000) -- when floats are long double]], -patch = [[ ---- lstrlib.c 2014/12/11 14:03:07 1.221 -+++ lstrlib.c 2015/02/23 19:01:42 -@@ -800,3 +800,4 @@ - /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ --#define MAX_ITEM 512 -+#define MAX_ITEM \ -+ (sizeof(lua_Number) <= 4 ? 150 : sizeof(lua_Number) <= 8 ? 450 : 5050) - -]] -} - -Bug{ -what = [['debug.getlocal' on a coroutine suspended in a hook -can crash the interpreter]], -report = [[云风, 2015/02/11]], -since = [[5.2]], -fix = nil, -example = [[see http://lua-users.org/lists/lua-l/2015-02/msg00146.html]], -patch = [[ ---- ldebug.c 2015/01/02 12:52:22 2.110 -+++ ldebug.c 2015/02/13 16:03:23 -@@ -49,4 +49,14 @@ - - -+static void swapextra (lua_State *L) { -+ if (L->status == LUA_YIELD) { -+ CallInfo *ci = L->ci; /* get function that yielded */ -+ StkId temp = ci->func; /* exchange its 'func' and 'extra' values */ -+ ci->func = restorestack(L, ci->extra); -+ ci->extra = savestack(L, temp); -+ } -+} -+ -+ - /* - ** this function can be called asynchronous (e.g. during a signal) -@@ -145,4 +155,5 @@ - const char *name; - lua_lock(L); -+ swapextra(L); - if (ar == NULL) { /* information about non-active function? */ - if (!isLfunction(L->top - 1)) /* not a Lua function? */ -@@ -159,4 +170,5 @@ - } - } -+ swapextra(L); - lua_unlock(L); - return name; -@@ -166,10 +178,13 @@ - LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { - StkId pos = 0; /* to avoid warnings */ -- const char *name = findlocal(L, ar->i_ci, n, &pos); -+ const char *name; - lua_lock(L); -+ swapextra(L); -+ name = findlocal(L, ar->i_ci, n, &pos); - if (name) { - setobjs2s(L, pos, L->top - 1); - L->top--; /* pop value */ - } -+ swapextra(L); - lua_unlock(L); - return name; -@@ -271,4 +286,5 @@ - StkId func; - lua_lock(L); -+ swapextra(L); - if (*what == '>') { - ci = NULL; -@@ -289,4 +305,5 @@ - api_incr_top(L); - } -+ swapextra(L); - if (strchr(what, 'L')) - collectvalidlines(L, cl); -]] -} - - -Bug{ -what = [[suspended '__le' metamethod can give wrong result]], -report = [[Eric Zhong, 2015/04/07]], -since = [[5.2]], -fix = nil, - -example = [[ -mt = {__le = function (a,b) coroutine.yield("yield"); return a.x <= b.x end} -t1 = setmetatable({x=1}, mt) -t2 = {x=2} -co = coroutine.wrap(function (a,b) return t2 <= t1 end) -co() -print(co()) --> true (should be false) -]], - -patch = [[ ---- lstate.h 2014/10/30 18:53:28 2.119 -+++ lstate.h 2015/04/13 15:58:40 -@@ -94,6 +94,7 @@ - #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ - #define CIST_TAIL (1<<5) /* call was tail called */ - #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ -+#define CIST_LEQ (1<<7) /* using __lt for __le */ - - #define isLua(ci) ((ci)->callstatus & CIST_LUA) - ---- lvm.c 2014/12/27 20:30:38 2.232 -+++ lvm.c 2015/04/13 15:51:30 -@@ -292,9 +292,14 @@ - return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; - else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try 'le' */ - return res; -- else if ((res = luaT_callorderTM(L, r, l, TM_LT)) < 0) /* else try 'lt' */ -- luaG_ordererror(L, l, r); -- return !res; -+ else { /* try 'lt': */ -+ L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ -+ res = luaT_callorderTM(L, r, l, TM_LT); -+ L->ci->callstatus ^= CIST_LEQ; /* clear mark */ -+ if (res < 0) -+ luaG_ordererror(L, l, r); -+ return !res; /* result is negated */ -+ } - } - - -@@ -553,11 +558,11 @@ - case OP_LE: case OP_LT: case OP_EQ: { - int res = !l_isfalse(L->top - 1); - L->top--; -- /* metamethod should not be called when operand is K */ -- lua_assert(!ISK(GETARG_B(inst))); -- if (op == OP_LE && /* "<=" using "<" instead? */ -- ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE))) -- res = !res; /* invert result */ -+ if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ -+ lua_assert(op == OP_LE); -+ ci->callstatus ^= CIST_LEQ; /* clear mark */ -+ res = !res; /* negate result */ -+ } - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); - if (res != GETARG_A(inst)) /* condition failed? */ - ci->u.l.savedpc++; /* skip jump instruction */ -]] -} - - -Bug{ -what = [[return hook may not see correct values for - active local variables when function returns]], -report = [[Philipp Janda/Peng Yi, 2015/05/19]], -since = [[5.0]], -fix = nil, -example = [[ -see messasge http://lua-users.org/lists/lua-l/2015-05/msg00376.html]], -patch = [[ -]] -} - - - ------------------------------------------------------------------ --- Lua 5.3.1 - -Bug{ -what = [['io.lines' does not check maximum number of options]], -report = [[Patrick Donnell, 2015/07/10]], -since = [[5.3.0]], -fix = nil, -example = [[ --- can segfault in some machines -t ={}; for i = 1, 253 do t[i] = 1 end -io.lines("someexistingfile", table.unpack(t))() -]], -patch = [[ ---- liolib.c 2015/07/07 17:03:34 2.146 -+++ liolib.c 2015/07/15 14:40:28 2.147 -@@ -318,8 +318,15 @@ - static int io_readline (lua_State *L); - - -+/* -+** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit -+** in the limit for upvalues of a closure) -+*/ -+#define MAXARGLINE 250 -+ - static void aux_lines (lua_State *L, int toclose) { - int n = lua_gettop(L) - 1; /* number of arguments to read */ -+ luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); - lua_pushinteger(L, n); /* number of arguments to read */ - lua_pushboolean(L, toclose); /* close/not close file when finished */ - lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */ -]] -} - - ------------------------------------------------------------------ --- Lua 5.3.2 - -Bug{ -what = [[Metatable may access its own dealocated field when -it has a self reference in __newindex]], -report = [[actboy168@gmail.com, 2016/01/01]], -since = [[5.3.2]], -fix = nil, -example = [[ -local mt = {} -mt.__newindex = mt -local t = setmetatable({}, mt) -t[1] = 1 -- will segfault on some machines -]], -patch = [[ ---- lvm.c 2015/11/23 11:30:45 2.265 -+++ lvm.c 2016/01/01 14:34:12 -@@ -190,18 +190,19 @@ - for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; - if (oldval != NULL) { -- lua_assert(ttistable(t) && ttisnil(oldval)); -+ Table *h = hvalue(t); /* save 't' table */ -+ lua_assert(ttisnil(oldval)); - /* must check the metamethod */ -- if ((tm = fasttm(L, hvalue(t)->metatable, TM_NEWINDEX)) == NULL && -+ if ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL && - /* no metamethod; is there a previous entry in the table? */ - (oldval != luaO_nilobject || - /* no previous entry; must create one. (The next test is - always true; we only need the assignment.) */ -- (oldval = luaH_newkey(L, hvalue(t), key), 1))) { -+ (oldval = luaH_newkey(L, h, key), 1))) { - /* no metamethod and (now) there is an entry with given key */ - setobj2t(L, cast(TValue *, oldval), val); -- invalidateTMcache(hvalue(t)); -- luaC_barrierback(L, hvalue(t), val); -+ invalidateTMcache(h); -+ luaC_barrierback(L, h, val); - return; - } - /* else will try the metamethod */ -]] -} - - -Bug{ -what = [[label between local definitions can mix-up their initializations]], -report = [[Karel Tuma, 2016/03/01]], -since = [[5.2]], -fix = nil, -example = [[ -do - local k = 0 - local x - ::foo:: - local y -- should be reset to nil after goto, but it is not - assert(not y) - y = true - k = k + 1 - if k < 2 then goto foo end -end -]], -patch = [[ ---- lparser.c 2015/11/02 16:09:30 2.149 -+++ lparser.c 2016/03/03 12:03:37 -@@ -1226,7 +1226,7 @@ - checkrepeated(fs, ll, label); /* check for repeated labels */ - checknext(ls, TK_DBCOLON); /* skip double colon */ - /* create new entry for this label */ -- l = newlabelentry(ls, ll, label, line, fs->pc); -+ l = newlabelentry(ls, ll, label, line, luaK_getlabel(fs)); - skipnoopstat(ls); /* skip other no-op statements */ - if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */ - /* assume that locals are already out of scope */ -]] -} - - -Bug{ -what = [['gmatch' iterator fails when called from a coroutine different -from the one that created it]], -report = [[Nagaev Boris, 2016/03/18]], -since = [[5.3.2]], -fix = nil, -example = [[ -local f = string.gmatch("1 2 3 4 5", "%d+") -print(f()) --> 1 -co = coroutine.wrap(f) -print(co()) --> ??? (should be 2) -]], -patch = [[ ---- lstrlib.c 2015/11/25 16:28:17 1.239 -+++ lstrlib.c 2016/04/11 15:29:41 -@@ -688,6 +688,7 @@ - static int gmatch_aux (lua_State *L) { - GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3)); - const char *src; -+ gm->ms.L = L; - for (src = gm->src; src <= gm->ms.src_end; src++) { - const char *e; - reprepstate(&gm->ms); -]] -} - - ------------------------------------------------------------------ --- Lua 5.3.3 - - -Bug{ -what = [[expression list with four or more expressions in -a 'for' loop can crash the interpreter]], -report = [[Marco Schöpl, 2016/06/17]], -since = [[5.2]], -fix = nil, -example = [[ --- the next loop will probably crash the interpreter -repeat until load "for _ in _,_,_,_ do local function _() end" -]], -patch = [[ ---- lparser.c 2016/05/13 19:10:16 2.153 -+++ lparser.c 2016/06/17 19:52:48 -@@ -323,6 +323,8 @@ - luaK_nil(fs, reg, extra); - } - } -+ if (nexps > nvars) -+ ls->fs->freereg -= nexps - nvars; /* remove extra values */ - } - - -@@ -1160,11 +1162,8 @@ - int nexps; - checknext(ls, '='); - nexps = explist(ls, &e); -- if (nexps != nvars) { -+ if (nexps != nvars) - adjust_assign(ls, nvars, nexps, &e); -- if (nexps > nvars) -- ls->fs->freereg -= nexps - nvars; /* remove extra values */ -- } - else { - luaK_setoneret(ls->fs, &e); /* close last expression */ - luaK_storevar(ls->fs, &lh->v, &e); -]] -} - - -Bug{ -what = [[Checking a format for 'os.date' may read pass the format string]], -report = [[Nagaev Boris, 2016/07/10]], -since = [[5.3.3]], -fix = nil, -example = [[ -This bug does not seem to happen with regular compilers. -It needs an "interceptor" 'memcmp' function that continues -reading memory after a difference is found.]], -patch = [[ -2c2 -< ** $Id: bugs,v 1.160 2018/05/24 20:25:14 roberto Exp roberto $ ---- -> ** $Id: bugs,v 1.160 2018/05/24 20:25:14 roberto Exp roberto $ -263c263,264 -< for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { ---- -> int convlen = (int)strlen(conv); -> for (option = LUA_STRFTIMEOPTIONS; *option != '\0' && oplen <= convlen; option += oplen) { -]] -} - - -Bug{ -what = [[Lua can generate wrong code in functions with too many constants]], -report = [[Marco Schöpl, 2016/07/17]], -since = [[5.3.3]], -fix = nil, -example = [[See http://lua-users.org/lists/lua-l/2016-07/msg00303.html]], -patch = [[ ---- lcode.c 2016/06/20 19:12:46 2.110 -+++ lcode.c 2016/07/18 15:43:41 -@@ -1018,8 +1018,8 @@ - */ - static void codebinexpval (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int line) { -- int rk1 = luaK_exp2RK(fs, e1); /* both operands are "RK" */ -- int rk2 = luaK_exp2RK(fs, e2); -+ int rk2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ -+ int rk1 = luaK_exp2RK(fs, e1); - freeexps(fs, e1, e2); - e1->u.info = luaK_codeABC(fs, op, 0, rk1, rk2); /* generate opcode */ - e1->k = VRELOCABLE; /* all those operations are relocatable */ -]] -} - - -Bug{ -what = [[When a coroutine tries to resume a non-suspended coroutine, -it can do some mess (and break C assertions) before detecting the error]], -report = [[Marco Schöpl, 2016/07/20]], -since = [[ ]], -fix = nil, -example = [[ --- with C assertions on -A = coroutine.running() -B = coroutine.create(function() coroutine.resume(A) end) -coroutine.resume(B) - --- or -A = coroutine.wrap(function() pcall(A, _) end) -A() -]], -patch = [[ -]] -} - - ------------------------------------------------------------------ --- Lua 5.3.4 - - -Bug{ -what = [[Wrong code for a goto followed by a label inside an 'if']], -report = [[云风, 2017/04/13]], -since = [[5.2]], -fix = nil, -example = [[ --- should print 32323232..., but prints only '3' -if true then - goto LBL - ::loop:: - print(2) - ::LBL:: - print(3) - goto loop -end -]], -patch = [[ ---- lparser.c 2017/04/19 17:20:42 2.155.1.1 -+++ lparser.c 2017/04/29 18:11:40 2.155.1.2 -@@ -1392,7 +1392,7 @@ - luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ - enterblock(fs, &bl, 0); /* must enter block before 'goto' */ - gotostat(ls, v.t); /* handle goto/break */ -- skipnoopstat(ls); /* skip other no-op statements */ -+ while (testnext(ls, ';')) {} /* skip semicolons */ - if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ - leaveblock(fs); - return; /* and that is it */ -]] -} - - -Bug{ -what = [[Lua crashes when building sequences with more than 2^30 elements.]], -report = [[Viacheslav Usov, 2017/05/11]], -since = [[ ]], -fix = nil, -example = [[ --- crashes if machine has enough memory -local t = {} -for i = 1, 0x7fffffff do - t[i] = i -end -]], -patch = [[ ---- ltable.c 2017/04/19 17:20:42 2.118.1.1 -+++ ltable.c 2018/05/24 18:34:38 -@@ -223,7 +223,9 @@ - unsigned int na = 0; /* number of elements to go to array part */ - unsigned int optimal = 0; /* optimal size for array part */ - /* loop while keys can fill more than half of total size */ -- for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) { -+ for (i = 0, twotoi = 1; -+ twotoi > 0 && *pna > twotoi / 2; -+ i++, twotoi *= 2) { - if (nums[i] > 0) { - a += nums[i]; - if (a > twotoi/2) { /* more than half elements present? */ -]] -} - - -Bug{ -what = [[Table length computation overflows for sequences larger than -2^31 elements.]], -report = [[Viacheslav Usov, 2017/05/12]], -since = [[ ]], -fix = nil, -example = [[ --- on a machine with enough memory -local t = {} -for i = 1, 2147483681 do - t[i] = i -end -print(#t) -]], -patch = [[ ---- ltable.h 2017/04/19 17:20:42 2.23.1.1 -+++ ltable.h 2018/05/24 19:31:50 -@@ -56,3 +56,3 @@ - LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); --LUAI_FUNC int luaH_getn (Table *t); -+LUAI_FUNC lua_Unsigned luaH_getn (Table *t); - ---- ltable.c 2018/05/24 19:22:37 2.118.1.2 -+++ ltable.c 2018/05/24 19:25:05 -@@ -614,4 +614,4 @@ - --static int unbound_search (Table *t, unsigned int j) { -- unsigned int i = j; /* i is zero or a present index */ -+static lua_Unsigned unbound_search (Table *t, lua_Unsigned j) { -+ lua_Unsigned i = j; /* i is zero or a present index */ - j++; -@@ -620,3 +620,3 @@ - i = j; -- if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */ -+ if (j > l_castS2U(LUA_MAXINTEGER) / 2) { /* overflow? */ - /* table was built with bad purposes: resort to linear search */ -@@ -630,3 +630,3 @@ - while (j - i > 1) { -- unsigned int m = (i+j)/2; -+ lua_Unsigned m = (i+j)/2; - if (ttisnil(luaH_getint(t, m))) j = m; -@@ -642,3 +642,3 @@ - */ --int luaH_getn (Table *t) { -+lua_Unsigned luaH_getn (Table *t) { - unsigned int j = t->sizearray; -]] -} - - -Bug{ -what = [[Lua does not check GC when creating error messages]], -report = [[Viacheslav Usov, 2017/07/06]], -since = [[5.3.2]], -fix = nil, -example = [[ -function test() - bob.joe.larry = 23 -end - --- memory will grow steadly -for i = 1, math.huge do - pcall(test) - if i % 100000 == 0 then - io.write(collectgarbage'count'*1024, "\n") - end -end -]], -patch = [[ ---- ldebug.c 2017/04/19 17:20:42 2.121.1.1 -+++ ldebug.c 2017/07/10 17:08:39 -@@ -653,6 +653,7 @@ - CallInfo *ci = L->ci; - const char *msg; - va_list argp; -+ luaC_checkGC(L); /* error message uses memory */ - va_start(argp, fmt); - msg = luaO_pushvfstring(L, fmt, argp); /* format message */ - va_end(argp); -]] -} - - -Bug{ -what = [[dead keys with nil values can stay in weak tables]], -report = [[云风 Cloud Wu, 2017/08/15]], -since = [[5.2]], -fix = nil, -example = [[ --- The following chunk, under a memory checker like valgrind, --- produces a memory access violation. - -local a = setmetatable({}, {__mode = 'kv'}) - -a['ABCDEFGHIJKLMNOPQRSTUVWXYZ' .. 'abcdefghijklmnopqrstuvwxyz'] = {} -a[next(a)] = nil -collectgarbage() -print(a['BCDEFGHIJKLMNOPQRSTUVWXYZ' .. 'abcdefghijklmnopqrstuvwxyz']) -]], -patch = [[ ---- lgc.c 2016/12/22 13:08:50 2.215 -+++ lgc.c 2017/08/31 16:08:23 -@@ -643,8 +643,9 @@ - for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) { - setnilvalue(gval(n)); /* remove value ... */ -- removeentry(n); /* and remove entry from table */ - } -+ if (ttisnil(gval(n))) /* is entry empty? */ -+ removeentry(n); /* remove entry from table */ - } - } - } -]] -} - - -Bug{ -what = [['lua_pushcclosure' should not call the garbage collector when -'n' is zero.]], -report = [[Andrew Gierth, 2017/12/05]], -since = [[5.3.3]], -fix = nil, -example = [[ ]], -patch = [[ ---- lapi.c 2017/04/19 17:13:00 2.259.1.1 -+++ lapi.c 2017/12/06 18:14:45 -@@ -533,6 +533,7 @@ - lua_lock(L); - if (n == 0) { - setfvalue(L->top, fn); -+ api_incr_top(L); - } - else { - CClosure *cl; -@@ -546,9 +547,9 @@ - /* does not need barrier because closure is white */ - } - setclCvalue(L, L->top, cl); -+ api_incr_top(L); -+ luaC_checkGC(L); - } -- api_incr_top(L); -- luaC_checkGC(L); - lua_unlock(L); - } -]] -} - - -Bug{ -what = [[memory-allocation error when resizing a table can leave it -in an inconsistent state.]], -report = [[Roberto, 2017/12/08]], -since = [[5.0]], -fix = nil, -example = [[ -local a = {x = 1, y = 1, z = 1} -a[1] = 10 -- goes to the hash part (which has 4 slots) -print(a[1]) --> 10 - --- assume that the 2nd memory allocation from now fails -pcall(rawset, a, 2, 20) -- forces a rehash - --- a[1] now exists both in the array part (because the array part --- grew) and in the hash part (because the allocation of the hash --- part failed, keeping it as it was). --- This makes the following traversal goes forever... -for k,v in pairs(a) do print(k,v) end -]], -patch = [[ ---- ltable.c 2018/05/24 19:39:05 2.118.1.3 -+++ ltable.c 2018/06/04 16:00:25 -@@ -332,17 +332,34 @@ - } - - -+typedef struct { -+ Table *t; -+ unsigned int nhsize; -+} AuxsetnodeT; -+ -+ -+static void auxsetnode (lua_State *L, void *ud) { -+ AuxsetnodeT *asn = cast(AuxsetnodeT *, ud); -+ setnodevector(L, asn->t, asn->nhsize); -+} -+ -+ - void luaH_resize (lua_State *L, Table *t, unsigned int nasize, - unsigned int nhsize) { - unsigned int i; - int j; -+ AuxsetnodeT asn; - unsigned int oldasize = t->sizearray; - int oldhsize = allocsizenode(t); - Node *nold = t->node; /* save old hash ... */ - if (nasize > oldasize) /* array part must grow? */ - setarrayvector(L, t, nasize); - /* create new hash part with appropriate size */ -- setnodevector(L, t, nhsize); -+ asn.t = t; asn.nhsize = nhsize; -+ if (luaD_rawrunprotected(L, auxsetnode, &asn) != LUA_OK) { /* mem. error? */ -+ setarrayvector(L, t, oldasize); /* array back to its original size */ -+ luaD_throw(L, LUA_ERRMEM); /* rethrow memory error */ -+ } - if (nasize < oldasize) { /* array part must shrink? */ - t->sizearray = nasize; - /* re-insert elements from vanishing slice */ -]] -} - - - ---[=[ -Bug{ -what = [[Long brackets with a huge number of '=' overflow some -internal buffer arithmetic]], -report = [[Marco, 2018/12/12]], -since = [[5.1]], -fix = nil, -example = [[ -local eqs = string.rep("=", 0x3ffffffe) -local code = "return [" .. eqs .. "[a]" .. eqs .. "]" -print(#assert(load(code))()) -]], -patch = [[ -]] -} -]=] - - - - ---[=[ -Bug{ -what = [[ ]], -report = [[ ]], -since = [[ ]], -fix = nil, -example = [[ ]], -patch = [[ -]] -} -]=] - - diff --git a/lapi.c b/lapi.c index 06396ad3bc..27fa524797 100644 --- a/lapi.c +++ b/lapi.c @@ -39,11 +39,9 @@ const char lua_ident[] = /* -** Test for a valid index. -** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed. -** However, it covers the most common cases in a faster way. +** Test for a valid index (one that is not the 'nilvalue'). */ -#define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue) +#define isvalid(L, o) ((o) != &G(L)->nilvalue) /* test for pseudo index */ @@ -53,64 +51,73 @@ const char lua_ident[] = #define isupvalue(i) ((i) < LUA_REGISTRYINDEX) +/* +** Convert an acceptable index to a pointer to its respective value. +** Non-valid indices return the special nil value 'G(L)->nilvalue'. +*/ static TValue *index2value (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { - StkId o = ci->func + idx; - api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index"); - if (o >= L->top) return &G(L)->nilvalue; + StkId o = ci->func.p + idx; + api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index"); + if (o >= L->top.p) return &G(L)->nilvalue; else return s2v(o); } else if (!ispseudo(idx)) { /* negative index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); - return s2v(L->top + idx); + api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), + "invalid index"); + return s2v(L->top.p + idx); } else if (idx == LUA_REGISTRYINDEX) return &G(L)->l_registry; else { /* upvalues */ idx = LUA_REGISTRYINDEX - idx; api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); - if (ttislcf(s2v(ci->func))) /* light C function? */ - return &G(L)->nilvalue; /* it has no upvalues */ - else { - CClosure *func = clCvalue(s2v(ci->func)); - return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; + if (ttisCclosure(s2v(ci->func.p))) { /* C closure? */ + CClosure *func = clCvalue(s2v(ci->func.p)); + return (idx <= func->nupvalues) ? &func->upvalue[idx-1] + : &G(L)->nilvalue; + } + else { /* light C function or Lua function (through a hook)?) */ + api_check(L, ttislcf(s2v(ci->func.p)), "caller not a C function"); + return &G(L)->nilvalue; /* no upvalues */ } } } + +/* +** Convert a valid actual index (not a pseudo-index) to its address. +*/ static StkId index2stack (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { - StkId o = ci->func + idx; - api_check(L, o < L->top, "unacceptable index"); + StkId o = ci->func.p + idx; + api_check(L, o < L->top.p, "invalid index"); return o; } else { /* non-positive index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); + api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), + "invalid index"); api_check(L, !ispseudo(idx), "invalid index"); - return L->top + idx; + return L->top.p + idx; } } LUA_API int lua_checkstack (lua_State *L, int n) { int res; - CallInfo *ci = L->ci; + CallInfo *ci; lua_lock(L); + ci = L->ci; api_check(L, n >= 0, "negative 'n'"); - if (L->stack_last - L->top > n) /* stack large enough? */ + if (L->stack_last.p - L->top.p > n) /* stack large enough? */ res = 1; /* yes; check is OK */ - else { /* no; need to grow stack */ - int inuse = cast_int(L->top - L->stack) + EXTRA_STACK; - if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ - res = 0; /* no */ - else /* try to grow stack */ - res = luaD_growstack(L, n, 0); - } - if (res && ci->top < L->top + n) - ci->top = L->top + n; /* adjust frame top */ + else /* need to grow stack */ + res = luaD_growstack(L, n, 0); + if (res && ci->top.p < L->top.p + n) + ci->top.p = L->top.p + n; /* adjust frame top */ lua_unlock(L); return res; } @@ -120,13 +127,13 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { int i; if (from == to) return; lua_lock(to); - api_checknelems(from, n); + api_checkpop(from, n); api_check(from, G(from) == G(to), "moving among independent states"); - api_check(from, to->ci->top - to->top >= n, "stack overflow"); - from->top -= n; + api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow"); + from->top.p -= n; for (i = 0; i < n; i++) { - setobjs2s(to, to->top, from->top + i); - to->top++; /* stack already checked by previous 'api_check' */ + setobjs2s(to, to->top.p, from->top.p + i); + to->top.p++; /* stack already checked by previous 'api_check' */ } lua_unlock(to); } @@ -160,30 +167,50 @@ LUA_API lua_Number lua_version (lua_State *L) { LUA_API int lua_absindex (lua_State *L, int idx) { return (idx > 0 || ispseudo(idx)) ? idx - : cast_int(L->top - L->ci->func) + idx; + : cast_int(L->top.p - L->ci->func.p) + idx; } LUA_API int lua_gettop (lua_State *L) { - return cast_int(L->top - (L->ci->func + 1)); + return cast_int(L->top.p - (L->ci->func.p + 1)); } LUA_API void lua_settop (lua_State *L, int idx) { - StkId func = L->ci->func; + CallInfo *ci; + StkId func, newtop; + ptrdiff_t diff; /* difference for new top */ lua_lock(L); + ci = L->ci; + func = ci->func.p; if (idx >= 0) { - StkId newtop = (func + 1) + idx; - api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); - while (L->top < newtop) - setnilvalue(s2v(L->top++)); - L->top = newtop; + api_check(L, idx <= ci->top.p - (func + 1), "new top too large"); + diff = ((func + 1) + idx) - L->top.p; + for (; diff > 0; diff--) + setnilvalue(s2v(L->top.p++)); /* clear new slots */ } else { - api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); - L->top += idx+1; /* 'subtract' index (index is negative) */ + api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top"); + diff = idx + 1; /* will "subtract" index (as it is negative) */ + } + newtop = L->top.p + diff; + if (diff < 0 && L->tbclist.p >= newtop) { + lua_assert(ci->callstatus & CIST_TBC); + newtop = luaF_close(L, newtop, CLOSEKTOP, 0); } - luaF_close(L, L->top, LUA_OK); + L->top.p = newtop; /* correct top only after closing any upvalue */ + lua_unlock(L); +} + + +LUA_API void lua_closeslot (lua_State *L, int idx) { + StkId level; + lua_lock(L); + level = index2stack(L, idx); + api_check(L, (L->ci->callstatus & CIST_TBC) && (L->tbclist.p == level), + "no variable to close at given level"); + level = luaF_close(L, level, CLOSEKTOP, 0); + setnilvalue(s2v(level)); lua_unlock(L); } @@ -211,8 +238,9 @@ static void reverse (lua_State *L, StkId from, StkId to) { LUA_API void lua_rotate (lua_State *L, int idx, int n) { StkId p, t, m; lua_lock(L); - t = L->top - 1; /* end of stack segment being rotated */ + t = L->top.p - 1; /* end of stack segment being rotated */ p = index2stack(L, idx); /* start of segment */ + api_check(L, L->tbclist.p < p, "moving a to-be-closed slot"); api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ reverse(L, p, m); /* reverse the prefix with length 'n' */ @@ -227,10 +255,10 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { lua_lock(L); fr = index2value(L, fromidx); to = index2value(L, toidx); - api_check(l, isvalid(L, to), "invalid index"); + api_check(L, isvalid(L, to), "invalid index"); setobj(L, to, fr); if (isupvalue(toidx)) /* function upvalue? */ - luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); + luaC_barrier(L, clCvalue(s2v(L->ci->func.p)), fr); /* LUA_REGISTRYINDEX does not need gc barrier (collector revisits it before finishing collection) */ lua_unlock(L); @@ -239,7 +267,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { LUA_API void lua_pushvalue (lua_State *L, int idx) { lua_lock(L); - setobj2s(L, L->top, index2value(L, idx)); + setobj2s(L, L->top.p, index2value(L, idx)); api_incr_top(L); lua_unlock(L); } @@ -259,7 +287,7 @@ LUA_API int lua_type (lua_State *L, int idx) { LUA_API const char *lua_typename (lua_State *L, int t) { UNUSED(L); - api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); + api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type"); return ttypename(t); } @@ -305,15 +333,15 @@ LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { LUA_API void lua_arith (lua_State *L, int op) { lua_lock(L); if (op != LUA_OPUNM && op != LUA_OPBNOT) - api_checknelems(L, 2); /* all other operations expect two operands */ + api_checkpop(L, 2); /* all other operations expect two operands */ else { /* for unary operations, add fake 2nd operand */ - api_checknelems(L, 1); - setobjs2s(L, L->top, L->top - 1); + api_checkpop(L, 1); + setobjs2s(L, L->top.p, L->top.p - 1); api_incr_top(L); } /* first operand at top - 2, second at top - 1; result go to top - 2 */ - luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2); - L->top--; /* remove second operand */ + luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2); + L->top.p--; /* pop second operand */ lua_unlock(L); } @@ -338,8 +366,20 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { } +LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff) { + const TValue *o = index2value(L, idx); + if (ttisnumber(o)) { + unsigned len = luaO_tostringbuff(o, buff); + buff[len++] = '\0'; /* add final zero */ + return len; + } + else + return 0; +} + + LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { - size_t sz = luaO_str2num(s, s2v(L->top)); + size_t sz = luaO_str2num(s, s2v(L->top.p)); if (sz != 0) api_incr_top(L); return sz; @@ -347,23 +387,21 @@ LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { - lua_Number n; + lua_Number n = 0; const TValue *o = index2value(L, idx); int isnum = tonumber(o, &n); - if (!isnum) - n = 0; /* call to 'tonumber' may change 'n' even if it fails */ - if (pisnum) *pisnum = isnum; + if (pisnum) + *pisnum = isnum; return n; } LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { - lua_Integer res; + lua_Integer res = 0; const TValue *o = index2value(L, idx); int isnum = tointeger(o, &res); - if (!isnum) - res = 0; /* call to 'tointeger' may change 'n' even if it fails */ - if (pisnum) *pisnum = isnum; + if (pisnum) + *pisnum = isnum; return res; } @@ -375,31 +413,40 @@ LUA_API int lua_toboolean (lua_State *L, int idx) { LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { - TValue *o = index2value(L, idx); + TValue *o; + lua_lock(L); + o = index2value(L, idx); if (!ttisstring(o)) { if (!cvt2str(o)) { /* not convertible? */ if (len != NULL) *len = 0; + lua_unlock(L); return NULL; } - lua_lock(L); /* 'luaO_tostring' may create a new string */ luaO_tostring(L, o); luaC_checkGC(L); o = index2value(L, idx); /* previous call may reallocate the stack */ - lua_unlock(L); } + lua_unlock(L); if (len != NULL) - *len = vslen(o); - return svalue(o); + return getlstr(tsvalue(o), *len); + else + return getstr(tsvalue(o)); } LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { const TValue *o = index2value(L, idx); switch (ttypetag(o)) { - case LUA_TSHRSTR: return tsvalue(o)->shrlen; - case LUA_TLNGSTR: return tsvalue(o)->u.lnglen; - case LUA_TUSERDATA: return uvalue(o)->len; - case LUA_TTABLE: return luaH_getn(hvalue(o)); + case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->shrlen); + case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->u.lnglen); + case LUA_VUSERDATA: return cast(lua_Unsigned, uvalue(o)->len); + case LUA_VTABLE: { + lua_Unsigned res; + lua_lock(L); + res = luaH_getn(L, hvalue(o)); + lua_unlock(L); + return res; + } default: return 0; } } @@ -414,7 +461,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { } -static void *touserdata (const TValue *o) { +l_sinline void *touserdata (const TValue *o) { switch (ttype(o)) { case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); @@ -437,7 +484,7 @@ LUA_API lua_State *lua_tothread (lua_State *L, int idx) { /* ** Returns a pointer to the internal representation of an object. -** Note that ANSI C does not allow the conversion of a pointer to +** Note that ISO C does not allow the conversion of a pointer to ** function to a 'void*', so the conversion here goes through ** a 'size_t'. (As the returned pointer is only informative, this ** conversion should not be a problem.) @@ -445,8 +492,8 @@ LUA_API lua_State *lua_tothread (lua_State *L, int idx) { LUA_API const void *lua_topointer (lua_State *L, int idx) { const TValue *o = index2value(L, idx); switch (ttypetag(o)) { - case LUA_TLCF: return cast_voidp(cast_sizet(fvalue(o))); - case LUA_TUSERDATA: case LUA_TLIGHTUSERDATA: + case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o))); + case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA: return touserdata(o); default: { if (iscollectable(o)) @@ -466,7 +513,7 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) { LUA_API void lua_pushnil (lua_State *L) { lua_lock(L); - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); api_incr_top(L); lua_unlock(L); } @@ -474,7 +521,7 @@ LUA_API void lua_pushnil (lua_State *L) { LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { lua_lock(L); - setfltvalue(s2v(L->top), n); + setfltvalue(s2v(L->top.p), n); api_incr_top(L); lua_unlock(L); } @@ -482,7 +529,7 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { lua_lock(L); - setivalue(s2v(L->top), n); + setivalue(s2v(L->top.p), n); api_incr_top(L); lua_unlock(L); } @@ -497,7 +544,22 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { TString *ts; lua_lock(L); ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); - setsvalue2s(L, L->top, ts); + setsvalue2s(L, L->top.p, ts); + api_incr_top(L); + luaC_checkGC(L); + lua_unlock(L); + return getstr(ts); +} + + +LUA_API const char *lua_pushexternalstring (lua_State *L, + const char *s, size_t len, lua_Alloc falloc, void *ud) { + TString *ts; + lua_lock(L); + api_check(L, len <= MAX_SIZE, "string too large"); + api_check(L, s[len] == '\0', "string not ending with zero"); + ts = luaS_newextlstr (L, s, len, falloc, ud); + setsvalue2s(L, L->top.p, ts); api_incr_top(L); luaC_checkGC(L); lua_unlock(L); @@ -508,11 +570,11 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { LUA_API const char *lua_pushstring (lua_State *L, const char *s) { lua_lock(L); if (s == NULL) - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); else { TString *ts; ts = luaS_new(L, s); - setsvalue2s(L, L->top, ts); + setsvalue2s(L, L->top.p, ts); s = getstr(ts); /* internal copy's address */ } api_incr_top(L); @@ -537,9 +599,7 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { const char *ret; va_list argp; lua_lock(L); - va_start(argp, fmt); - ret = luaO_pushvfstring(L, fmt, argp); - va_end(argp); + pushvfstring(L, argp, fmt, ret); luaC_checkGC(L); lua_unlock(L); return ret; @@ -549,21 +609,23 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { lua_lock(L); if (n == 0) { - setfvalue(s2v(L->top), fn); + setfvalue(s2v(L->top.p), fn); api_incr_top(L); } else { + int i; CClosure *cl; - api_checknelems(L, n); + api_checkpop(L, n); api_check(L, n <= MAXUPVAL, "upvalue index too large"); cl = luaF_newCclosure(L, n); cl->f = fn; - L->top -= n; - while (n--) { - setobj2n(L, &cl->upvalue[n], s2v(L->top + n)); + for (i = 0; i < n; i++) { + setobj2n(L, &cl->upvalue[i], s2v(L->top.p - n + i)); /* does not need barrier because closure is white */ + lua_assert(iswhite(cl)); } - setclCvalue(L, s2v(L->top), cl); + L->top.p -= n; + setclCvalue(L, s2v(L->top.p), cl); api_incr_top(L); luaC_checkGC(L); } @@ -573,7 +635,10 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { LUA_API void lua_pushboolean (lua_State *L, int b) { lua_lock(L); - setbvalue(s2v(L->top), (b != 0)); /* ensure that true is 1 */ + if (b) + setbtvalue(s2v(L->top.p)); + else + setbfvalue(s2v(L->top.p)); api_incr_top(L); lua_unlock(L); } @@ -581,7 +646,7 @@ LUA_API void lua_pushboolean (lua_State *L, int b) { LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { lua_lock(L); - setpvalue(s2v(L->top), p); + setpvalue(s2v(L->top.p), p); api_incr_top(L); lua_unlock(L); } @@ -589,10 +654,10 @@ LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { LUA_API int lua_pushthread (lua_State *L) { lua_lock(L); - setthvalue(L, s2v(L->top), L); + setthvalue(L, s2v(L->top.p), L); api_incr_top(L); lua_unlock(L); - return (G(L)->mainthread == L); + return (mainthread(G(L)) == L); } @@ -603,41 +668,53 @@ LUA_API int lua_pushthread (lua_State *L) { static int auxgetstr (lua_State *L, const TValue *t, const char *k) { - const TValue *slot; + lu_byte tag; TString *str = luaS_new(L, k); - if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - setobj2s(L, L->top, slot); + luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag); + if (!tagisempty(tag)) api_incr_top(L); - } else { - setsvalue2s(L, L->top, str); + setsvalue2s(L, L->top.p, str); api_incr_top(L); - luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); + tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag); } lua_unlock(L); - return ttype(s2v(L->top - 1)); + return novariant(tag); +} + + +/* +** The following function assumes that the registry cannot be a weak +** table; so, an emergency collection while using the global table +** cannot collect it. +*/ +static void getGlobalTable (lua_State *L, TValue *gt) { + Table *registry = hvalue(&G(L)->l_registry); + lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt); + (void)tag; /* avoid not-used warnings when checks are off */ + api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist"); } LUA_API int lua_getglobal (lua_State *L, const char *name) { - Table *reg = hvalue(&G(L)->l_registry); + TValue gt; lua_lock(L); - return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); + getGlobalTable(L, >); + return auxgetstr(L, >, name); } LUA_API int lua_gettable (lua_State *L, int idx) { - const TValue *slot; + lu_byte tag; TValue *t; lua_lock(L); + api_checkpop(L, 1); t = index2value(L, idx); - if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) { - setobj2s(L, L->top - 1, slot); - } - else - luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); + luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag); + if (tagisempty(tag)) + tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag); lua_unlock(L); - return ttype(s2v(L->top - 1)); + return novariant(tag); } @@ -649,35 +726,31 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { TValue *t; - const TValue *slot; + lu_byte tag; lua_lock(L); t = index2value(L, idx); - if (luaV_fastgeti(L, t, n, slot)) { - setobj2s(L, L->top, slot); - } - else { - TValue aux; - setivalue(&aux, n); - luaV_finishget(L, t, &aux, L->top, slot); + luaV_fastgeti(t, n, s2v(L->top.p), tag); + if (tagisempty(tag)) { + TValue key; + setivalue(&key, n); + tag = luaV_finishget(L, t, &key, L->top.p, tag); } api_incr_top(L); lua_unlock(L); - return ttype(s2v(L->top - 1)); + return novariant(tag); } -static int finishrawget (lua_State *L, const TValue *val) { - if (isempty(val)) /* avoid copying empty items to the stack */ - setnilvalue(s2v(L->top)); - else - setobj2s(L, L->top, val); +static int finishrawget (lua_State *L, lu_byte tag) { + if (tagisempty(tag)) /* avoid copying empty items to the stack */ + setnilvalue(s2v(L->top.p)); api_incr_top(L); lua_unlock(L); - return ttype(s2v(L->top - 1)); + return novariant(tag); } -static Table *gettable (lua_State *L, int idx) { +l_sinline Table *gettable (lua_State *L, int idx) { TValue *t = index2value(L, idx); api_check(L, ttistable(t), "table expected"); return hvalue(t); @@ -686,21 +759,23 @@ static Table *gettable (lua_State *L, int idx) { LUA_API int lua_rawget (lua_State *L, int idx) { Table *t; - const TValue *val; + lu_byte tag; lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); t = gettable(L, idx); - val = luaH_get(t, s2v(L->top - 1)); - L->top--; /* remove key */ - return finishrawget(L, val); + tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1)); + L->top.p--; /* pop key */ + return finishrawget(L, tag); } LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { Table *t; + lu_byte tag; lua_lock(L); t = gettable(L, idx); - return finishrawget(L, luaH_getint(t, n)); + luaH_fastgeti(t, n, s2v(L->top.p), tag); + return finishrawget(L, tag); } @@ -710,7 +785,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { lua_lock(L); t = gettable(L, idx); setpvalue(&k, cast_voidp(p)); - return finishrawget(L, luaH_get(t, &k)); + return finishrawget(L, luaH_get(t, &k, s2v(L->top.p))); } @@ -718,10 +793,10 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { Table *t; lua_lock(L); t = luaH_new(L); - sethvalue2s(L, L->top, t); + sethvalue2s(L, L->top.p, t); api_incr_top(L); if (narray > 0 || nrec > 0) - luaH_resize(L, t, narray, nrec); + luaH_resize(L, t, cast_uint(narray), cast_uint(nrec)); luaC_checkGC(L); lua_unlock(L); } @@ -745,7 +820,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { break; } if (mt != NULL) { - sethvalue2s(L, L->top, mt); + sethvalue2s(L, L->top.p, mt); api_incr_top(L); res = 1; } @@ -761,12 +836,12 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); if (n <= 0 || n > uvalue(o)->nuvalue) { - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); t = LUA_TNONE; } else { - setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv); - t = ttype(s2v(L->top)); + setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv); + t = ttype(s2v(L->top.p)); } api_incr_top(L); lua_unlock(L); @@ -782,42 +857,44 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { ** t[k] = value at the top of the stack (where 'k' is a string) */ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { - const TValue *slot; + int hres; TString *str = luaS_new(L, k); - api_checknelems(L, 1); - if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - luaV_finishfastset(L, t, slot, s2v(L->top - 1)); - L->top--; /* pop value */ + api_checkpop(L, 1); + luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr); + if (hres == HOK) { + luaV_finishfastset(L, t, s2v(L->top.p - 1)); + L->top.p--; /* pop value */ } else { - setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ + setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */ api_incr_top(L); - luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot); - L->top -= 2; /* pop value and key */ + luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres); + L->top.p -= 2; /* pop value and key */ } lua_unlock(L); /* lock done by caller */ } LUA_API void lua_setglobal (lua_State *L, const char *name) { - Table *reg = hvalue(&G(L)->l_registry); + TValue gt; lua_lock(L); /* unlock done in 'auxsetstr' */ - auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); + getGlobalTable(L, >); + auxsetstr(L, >, name); } LUA_API void lua_settable (lua_State *L, int idx) { TValue *t; - const TValue *slot; + int hres; lua_lock(L); - api_checknelems(L, 2); + api_checkpop(L, 2); t = index2value(L, idx); - if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) { - luaV_finishfastset(L, t, slot, s2v(L->top - 1)); - } + luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset); + if (hres == HOK) + luaV_finishfastset(L, t, s2v(L->top.p - 1)); else - luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot); - L->top -= 2; /* pop index and value */ + luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres); + L->top.p -= 2; /* pop index and value */ lua_unlock(L); } @@ -830,61 +907,56 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { TValue *t; - const TValue *slot; + int hres; lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); t = index2value(L, idx); - if (luaV_fastgeti(L, t, n, slot)) { - luaV_finishfastset(L, t, slot, s2v(L->top - 1)); - } + luaV_fastseti(t, n, s2v(L->top.p - 1), hres); + if (hres == HOK) + luaV_finishfastset(L, t, s2v(L->top.p - 1)); else { - TValue aux; - setivalue(&aux, n); - luaV_finishset(L, t, &aux, s2v(L->top - 1), slot); + TValue temp; + setivalue(&temp, n); + luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres); } - L->top--; /* pop value */ + L->top.p--; /* pop value */ lua_unlock(L); } -LUA_API void lua_rawset (lua_State *L, int idx) { +static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { Table *t; - TValue *slot; lua_lock(L); - api_checknelems(L, 2); + api_checkpop(L, n); t = gettable(L, idx); - slot = luaH_set(L, t, s2v(L->top - 2)); - setobj2t(L, slot, s2v(L->top - 1)); + luaH_set(L, t, key, s2v(L->top.p - 1)); invalidateTMcache(t); - luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); - L->top -= 2; + luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); + L->top.p -= n; lua_unlock(L); } -LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { - Table *t; - lua_lock(L); - api_checknelems(L, 1); - t = gettable(L, idx); - luaH_setint(L, t, n, s2v(L->top - 1)); - luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); - L->top--; - lua_unlock(L); +LUA_API void lua_rawset (lua_State *L, int idx) { + aux_rawset(L, idx, s2v(L->top.p - 2), 2); } LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { + TValue k; + setpvalue(&k, cast_voidp(p)); + aux_rawset(L, idx, &k, 1); +} + + +LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { Table *t; - TValue k, *slot; lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); t = gettable(L, idx); - setpvalue(&k, cast_voidp(p)); - slot = luaH_set(L, t, &k); - setobj2t(L, slot, s2v(L->top - 1)); - luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); - L->top--; + luaH_setint(L, t, n, s2v(L->top.p - 1)); + luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); + L->top.p--; lua_unlock(L); } @@ -893,13 +965,13 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { TValue *obj; Table *mt; lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); obj = index2value(L, objindex); - if (ttisnil(s2v(L->top - 1))) + if (ttisnil(s2v(L->top.p - 1))) mt = NULL; else { - api_check(L, ttistable(s2v(L->top - 1)), "table expected"); - mt = hvalue(s2v(L->top - 1)); + api_check(L, ttistable(s2v(L->top.p - 1)), "table expected"); + mt = hvalue(s2v(L->top.p - 1)); } switch (ttype(obj)) { case LUA_TTABLE: { @@ -923,7 +995,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { break; } } - L->top--; + L->top.p--; lua_unlock(L); return 1; } @@ -933,17 +1005,17 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { TValue *o; int res; lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); - if (!(0 < n && n <= uvalue(o)->nuvalue)) - res = 0; + if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue))) + res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */ else { - setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1)); - luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); + setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1)); + luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1)); res = 1; } - L->top--; + L->top.p--; lua_unlock(L); return res; } @@ -955,8 +1027,11 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { #define checkresults(L,na,nr) \ - api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ - "results from function overflow current stack size") + (api_check(L, (nr) == LUA_MULTRET \ + || (L->ci->top.p - L->top.p >= (nr) - (na)), \ + "results from function overflow current stack size"), \ + api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS, \ + "invalid number of results")) LUA_API void lua_callk (lua_State *L, int nargs, int nresults, @@ -965,10 +1040,10 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, lua_lock(L); api_check(L, k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); - api_checknelems(L, nargs+1); + api_checkpop(L, nargs + 1); api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); - func = L->top - (nargs+1); + func = L->top.p - (nargs+1); if (k != NULL && yieldable(L)) { /* need to prepare continuation? */ L->ci->u.c.k = k; /* save continuation */ L->ci->u.c.ctx = ctx; /* save context */ @@ -1001,12 +1076,12 @@ static void f_call (lua_State *L, void *ud) { LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k) { struct CallS c; - int status; + TStatus status; ptrdiff_t func; lua_lock(L); api_check(L, k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); - api_checknelems(L, nargs+1); + api_checkpop(L, nargs + 1); api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); if (errfunc == 0) @@ -1016,7 +1091,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, api_check(L, ttisfunction(s2v(o)), "error handler must be a function"); func = savestack(L, o); } - c.func = L->top - (nargs+1); /* function to be called */ + c.func = L->top.p - (nargs+1); /* function to be called */ if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */ c.nresults = nresults; /* do a 'conventional' protected call */ status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); @@ -1029,7 +1104,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, ci->u2.funcidx = cast_int(savestack(L, c.func)); ci->u.c.old_errfunc = L->errfunc; L->errfunc = func; - setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ + setoah(ci, L->allowhook); /* save value of 'allowhook' */ ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ luaD_call(L, c.func, nresults); /* do the call */ ci->callstatus &= ~CIST_YPCALL; @@ -1038,51 +1113,54 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, } adjustresults(L, nresults); lua_unlock(L); - return status; + return APIstatus(status); } LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *chunkname, const char *mode) { ZIO z; - int status; + TStatus status; lua_lock(L); if (!chunkname) chunkname = "?"; luaZ_init(L, &z, reader, data); status = luaD_protectedparser(L, &z, chunkname, mode); if (status == LUA_OK) { /* no errors? */ - LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */ + LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */ if (f->nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ - Table *reg = hvalue(&G(L)->l_registry); - const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); + TValue gt; + getGlobalTable(L, >); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ - setobj(L, f->upvals[0]->v, gt); - luaC_barrier(L, f->upvals[0], gt); + setobj(L, f->upvals[0]->v.p, >); + luaC_barrier(L, f->upvals[0], >); } } lua_unlock(L); - return status; + return APIstatus(status); } +/* +** Dump a Lua function, calling 'writer' to write its parts. Ensure +** the stack returns with its original size. +*/ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { int status; - TValue *o; + ptrdiff_t otop = savestack(L, L->top.p); /* original top */ + TValue *f = s2v(L->top.p - 1); /* function to be dumped */ lua_lock(L); - api_checknelems(L, 1); - o = s2v(L->top - 1); - if (isLfunction(o)) - status = luaU_dump(L, getproto(o), writer, data, strip); - else - status = 1; + api_checkpop(L, 1); + api_check(L, isLfunction(f), "Lua function expected"); + status = luaU_dump(L, clLvalue(f)->p, writer, data, strip); + L->top.p = restorestack(L, otop); /* restore top */ lua_unlock(L); return status; } LUA_API int lua_status (lua_State *L) { - return L->status; + return APIstatus(L->status); } @@ -1093,16 +1171,18 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { va_list argp; int res = 0; global_State *g = G(L); + if (g->gcstp & (GCSTPGC | GCSTPCLS)) /* internal stop? */ + return -1; /* all options are invalid when stopped */ lua_lock(L); va_start(argp, what); switch (what) { case LUA_GCSTOP: { - g->gcrunning = 0; + g->gcstp = GCSTPUSR; /* stopped by the user */ break; } case LUA_GCRESTART: { luaE_setdebt(g, 0); - g->gcrunning = 1; + g->gcstp = 0; /* (other bits must be zero here) */ break; } case LUA_GCCOLLECT: { @@ -1119,65 +1199,42 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { break; } case LUA_GCSTEP: { - int data = va_arg(argp, int); - l_mem debt = 1; /* =1 to signal that it did an actual step */ - lu_byte oldrunning = g->gcrunning; - g->gcrunning = 1; /* allow GC to run */ - if (data == 0) { - luaE_setdebt(g, 0); /* do a basic step */ - luaC_step(L); - } - else { /* add 'data' to total debt */ - debt = cast(l_mem, data) * 1024 + g->GCdebt; - luaE_setdebt(g, debt); - luaC_checkGC(L); - } - g->gcrunning = oldrunning; /* restore previous state */ - if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ + lu_byte oldstp = g->gcstp; + l_mem n = cast(l_mem, va_arg(argp, size_t)); + int work = 0; /* true if GC did some work */ + g->gcstp = 0; /* allow GC to run (other bits must be zero here) */ + if (n <= 0) + n = g->GCdebt; /* force to run one basic step */ + luaE_setdebt(g, g->GCdebt - n); + luaC_condGC(L, (void)0, work = 1); + if (work && g->gcstate == GCSpause) /* end of cycle? */ res = 1; /* signal it */ - break; - } - case LUA_GCSETPAUSE: { - int data = va_arg(argp, int); - res = getgcparam(g->gcpause); - setgcparam(g->gcpause, data); - break; - } - case LUA_GCSETSTEPMUL: { - int data = va_arg(argp, int); - res = getgcparam(g->gcstepmul); - setgcparam(g->gcstepmul, data); + g->gcstp = oldstp; /* restore previous state */ break; } case LUA_GCISRUNNING: { - res = g->gcrunning; + res = gcrunning(g); break; } case LUA_GCGEN: { - int minormul = va_arg(argp, int); - int majormul = va_arg(argp, int); - res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; - if (minormul != 0) - g->genminormul = minormul; - if (majormul != 0) - setgcparam(g->genmajormul, majormul); - luaC_changemode(L, KGC_GEN); + res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; + luaC_changemode(L, KGC_GENMINOR); break; } case LUA_GCINC: { - int pause = va_arg(argp, int); - int stepmul = va_arg(argp, int); - int stepsize = va_arg(argp, int); - res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; - if (pause != 0) - setgcparam(g->gcpause, pause); - if (stepmul != 0) - setgcparam(g->gcstepmul, stepmul); - if (stepsize != 0) - g->gcstepsize = stepsize; + res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; luaC_changemode(L, KGC_INC); break; } + case LUA_GCPARAM: { + int param = va_arg(argp, int); + int value = va_arg(argp, int); + api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter"); + res = cast_int(luaO_applyparam(g->gcparams[param], 100)); + if (value >= 0) + g->gcparams[param] = luaO_codeparam(cast_uint(value)); + break; + } default: res = -1; /* invalid option */ } va_end(argp); @@ -1193,9 +1250,15 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { LUA_API int lua_error (lua_State *L) { + TValue *errobj; lua_lock(L); - api_checknelems(L, 1); - luaG_errormsg(L); + errobj = s2v(L->top.p - 1); + api_checkpop(L, 1); + /* error object is the memory error message? */ + if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg)) + luaM_error(L); /* raise a memory error */ + else + luaG_errormsg(L); /* raise a regular error */ /* code unreachable; will unlock when control actually leaves the kernel */ return 0; /* to avoid warnings */ } @@ -1205,31 +1268,25 @@ LUA_API int lua_next (lua_State *L, int idx) { Table *t; int more; lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); t = gettable(L, idx); - more = luaH_next(L, t, L->top - 1); - if (more) { + more = luaH_next(L, t, L->top.p - 1); + if (more) api_incr_top(L); - } else /* no more elements */ - L->top -= 1; /* remove key */ + L->top.p--; /* pop key */ lua_unlock(L); return more; } LUA_API void lua_toclose (lua_State *L, int idx) { - int nresults; StkId o; lua_lock(L); o = index2stack(L, idx); - nresults = L->ci->nresults; - api_check(L, L->openupval == NULL || uplevel(L->openupval) <= o, - "marked index below or equal new one"); + api_check(L, L->tbclist.p < o, "given index below or equal a marked one"); luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ - if (!hastocloseCfunc(nresults)) /* function not marked yet? */ - L->ci->nresults = codeNresults(nresults); /* mark it */ - lua_assert(hastocloseCfunc(L->ci->nresults)); + L->ci->callstatus |= CIST_TBC; /* mark that function has TBC slots */ lua_unlock(L); } @@ -1237,15 +1294,14 @@ LUA_API void lua_toclose (lua_State *L, int idx) { LUA_API void lua_concat (lua_State *L, int n) { lua_lock(L); api_checknelems(L, n); - if (n >= 2) { + if (n > 0) { luaV_concat(L, n); + luaC_checkGC(L); } - else if (n == 0) { /* push empty string */ - setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); + else { /* nothing to concatenate */ + setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */ api_incr_top(L); } - /* else n == 1; nothing to do */ - luaC_checkGC(L); lua_unlock(L); } @@ -1254,7 +1310,7 @@ LUA_API void lua_len (lua_State *L, int idx) { TValue *t; lua_lock(L); t = index2value(L, idx); - luaV_objlen(L, L->top, t); + luaV_objlen(L, L->top.p, t); api_incr_top(L); lua_unlock(L); } @@ -1297,9 +1353,9 @@ void lua_warning (lua_State *L, const char *msg, int tocont) { LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { Udata *u; lua_lock(L); - api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value"); - u = luaS_newudata(L, size, nuvalue); - setuvalue(L, s2v(L->top), u); + api_check(L, 0 <= nuvalue && nuvalue < SHRT_MAX, "invalid value"); + u = luaS_newudata(L, size, cast(unsigned short, nuvalue)); + setuvalue(L, s2v(L->top.p), u); api_incr_top(L); luaC_checkGC(L); lua_unlock(L); @@ -1311,19 +1367,21 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { static const char *aux_upvalue (TValue *fi, int n, TValue **val, GCObject **owner) { switch (ttypetag(fi)) { - case LUA_TCCL: { /* C closure */ + case LUA_VCCL: { /* C closure */ CClosure *f = clCvalue(fi); - if (!(1 <= n && n <= f->nupvalues)) return NULL; + if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues))) + return NULL; /* 'n' not in [1, f->nupvalues] */ *val = &f->upvalue[n-1]; if (owner) *owner = obj2gco(f); return ""; } - case LUA_TLCL: { /* Lua closure */ + case LUA_VLCL: { /* Lua closure */ LClosure *f = clLvalue(fi); TString *name; Proto *p = f->p; - if (!(1 <= n && n <= p->sizeupvalues)) return NULL; - *val = f->upvals[n-1]->v; + if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues))) + return NULL; /* 'n' not in [1, p->sizeupvalues] */ + *val = f->upvals[n-1]->v.p; if (owner) *owner = obj2gco(f->upvals[n - 1]); name = p->upvalues[n-1].name; return (name == NULL) ? "(no name)" : getstr(name); @@ -1339,7 +1397,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { lua_lock(L); name = aux_upvalue(index2value(L, funcindex), n, &val, NULL); if (name) { - setobj2s(L, L->top, val); + setobj2s(L, L->top.p, val); api_incr_top(L); } lua_unlock(L); @@ -1357,8 +1415,8 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { api_checknelems(L, 1); name = aux_upvalue(fi, n, &val, &owner); if (name) { - L->top--; - setobj(L, val, s2v(L->top)); + L->top.p--; + setobj(L, val, s2v(L->top.p)); luaC_barrier(L, owner, val); } lua_unlock(L); @@ -1367,29 +1425,35 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { + static const UpVal *const nullup = NULL; LClosure *f; TValue *fi = index2value(L, fidx); api_check(L, ttisLclosure(fi), "Lua function expected"); f = clLvalue(fi); - api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); if (pf) *pf = f; - return &f->upvals[n - 1]; /* get its upvalue pointer */ + if (1 <= n && n <= f->p->sizeupvalues) + return &f->upvals[n - 1]; /* get its upvalue pointer */ + else + return (UpVal**)&nullup; } LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { TValue *fi = index2value(L, fidx); switch (ttypetag(fi)) { - case LUA_TLCL: { /* lua closure */ + case LUA_VLCL: { /* lua closure */ return *getupvalref(L, fidx, n, NULL); } - case LUA_TCCL: { /* C closure */ + case LUA_VCCL: { /* C closure */ CClosure *f = clCvalue(fi); - api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); - return &f->upvalue[n - 1]; - } + if (1 <= n && n <= f->nupvalues) + return &f->upvalue[n - 1]; + /* else */ + } /* FALLTHROUGH */ + case LUA_VLCF: + return NULL; /* light C functions have no upvalues */ default: { - api_check(L, 0, "closure expected"); + api_check(L, 0, "function expected"); return NULL; } } @@ -1401,6 +1465,7 @@ LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, LClosure *f1; UpVal **up1 = getupvalref(L, fidx1, n1, &f1); UpVal **up2 = getupvalref(L, fidx2, n2, NULL); + api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index"); *up1 = *up2; luaC_objbarrier(L, f1, *up1); } diff --git a/lapi.h b/lapi.h index 5a4206f1ae..9b54534428 100644 --- a/lapi.h +++ b/lapi.h @@ -11,27 +11,55 @@ #include "llimits.h" #include "lstate.h" -#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ - "stack overflow");} -#define adjustresults(L,nres) \ - { if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } +#if defined(LUA_USE_APICHECK) +#include +#define api_check(l,e,msg) assert(e) +#else /* for testing */ +#define api_check(l,e,msg) ((void)(l), lua_assert((e) && msg)) +#endif + + + +/* Increments 'L->top.p', checking for stack overflows */ +#define api_incr_top(L) \ + (L->top.p++, api_check(L, L->top.p <= L->ci->top.p, "stack overflow")) + + +/* +** macros that are executed whenever program enters the Lua core +** ('lua_lock') and leaves the core ('lua_unlock') +*/ +#if !defined(lua_lock) +#define lua_lock(L) ((void) 0) +#define lua_unlock(L) ((void) 0) +#endif -#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ - "not enough elements in the stack") /* -** To reduce the overhead of returning from C functions, the presence of -** to-be-closed variables in these functions is coded in the CallInfo's -** field 'nresults', in a way that functions with no to-be-closed variables -** with zero, one, or "all" wanted results have no overhead. Functions -** with other number of wanted results, as well as functions with -** variables to be closed, have an extra check. +** If a call returns too many multiple returns, the callee may not have +** stack space to accommodate all results. In this case, this macro +** increases its stack space ('L->ci->top.p'). */ +#define adjustresults(L,nres) \ + { if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \ + L->ci->top.p = L->top.p; } -#define hastocloseCfunc(n) ((n) < LUA_MULTRET) -#define codeNresults(n) (-(n) - 3) +/* Ensure the stack has at least 'n' elements */ +#define api_checknelems(L,n) \ + api_check(L, (n) < (L->top.p - L->ci->func.p), \ + "not enough elements in the stack") + + +/* Ensure the stack has at least 'n' elements to be popped. (Some +** functions only update a slot after checking it for popping, but that +** is only an optimization for a pop followed by a push.) +*/ +#define api_checkpop(L,n) \ + api_check(L, (n) < L->top.p - L->ci->func.p && \ + L->tbclist.p < L->top.p - (n), \ + "not enough free elements in the stack") #endif diff --git a/lauxlib.c b/lauxlib.c index e9c02d3660..7cf90cb78a 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -25,12 +25,7 @@ #include "lua.h" #include "lauxlib.h" - - -#if !defined(MAX_SIZET) -/* maximum value for size_t */ -#define MAX_SIZET ((size_t)(~(size_t)0)) -#endif +#include "llimits.h" /* @@ -46,8 +41,8 @@ /* -** search for 'objidx' in table at index -1. -** return 1 + string at top if find a good name. +** Search for 'objidx' in table at index -1. ('objidx' must be an +** absolute index.) Return 1 + string at top if it found a good name. */ static int findfield (lua_State *L, int objidx, int level) { if (level == 0 || !lua_istable(L, -1)) @@ -60,10 +55,10 @@ static int findfield (lua_State *L, int objidx, int level) { return 1; } else if (findfield(L, objidx, level - 1)) { /* try recursively */ - lua_remove(L, -2); /* remove table (but keep name) */ - lua_pushliteral(L, "."); - lua_insert(L, -2); /* place '.' between the two names */ - lua_concat(L, 3); + /* stack: lib_name, lib_table, field_name (top) */ + lua_pushliteral(L, "."); /* place '.' between the two names */ + lua_replace(L, -3); /* (in the slot occupied by table) */ + lua_concat(L, 3); /* lib_name.field_name */ return 1; } } @@ -80,14 +75,15 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { int top = lua_gettop(L); lua_getinfo(L, "f", ar); /* push function */ lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); + luaL_checkstack(L, 6, "not enough stack"); /* slots for 'findfield' */ if (findfield(L, top + 1, 2)) { const char *name = lua_tostring(L, -1); if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */ lua_pushstring(L, name + 3); /* push name without prefix */ lua_remove(L, -2); /* remove original name */ } - lua_copy(L, -1, top + 1); /* move name to proper place */ - lua_pop(L, 2); /* remove pushed values */ + lua_copy(L, -1, top + 1); /* copy name to proper place */ + lua_settop(L, top + 1); /* remove table "loaded" and name copy */ return 1; } else { @@ -98,14 +94,14 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { static void pushfuncname (lua_State *L, lua_Debug *ar) { - if (pushglobalfuncname(L, ar)) { /* try first a global name */ - lua_pushfstring(L, "function '%s'", lua_tostring(L, -1)); - lua_remove(L, -2); /* remove name */ - } - else if (*ar->namewhat != '\0') /* is there a name from code? */ + if (*ar->namewhat != '\0') /* is there a name from code? */ lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */ else if (*ar->what == 'm') /* main? */ lua_pushliteral(L, "main chunk"); + else if (pushglobalfuncname(L, ar)) { /* try a global name */ + lua_pushfstring(L, "function '%s'", lua_tostring(L, -1)); + lua_remove(L, -2); /* remove name */ + } else if (*ar->what != 'C') /* for Lua functions, use */ lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); else /* nothing left... */ @@ -130,32 +126,37 @@ static int lastlevel (lua_State *L) { LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level) { + luaL_Buffer b; lua_Debug ar; - int top = lua_gettop(L); int last = lastlevel(L1); - int n1 = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; - if (msg) - lua_pushfstring(L, "%s\n", msg); - luaL_checkstack(L, 10, NULL); - lua_pushliteral(L, "stack traceback:"); + int limit2show = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; + luaL_buffinit(L, &b); + if (msg) { + luaL_addstring(&b, msg); + luaL_addchar(&b, '\n'); + } + luaL_addstring(&b, "stack traceback:"); while (lua_getstack(L1, level++, &ar)) { - if (n1-- == 0) { /* too many levels? */ - lua_pushliteral(L, "\n\t..."); /* add a '...' */ - level = last - LEVELS2 + 1; /* and skip to last ones */ + if (limit2show-- == 0) { /* too many levels? */ + int n = last - level - LEVELS2 + 1; /* number of levels to skip */ + lua_pushfstring(L, "\n\t...\t(skipping %d levels)", n); + luaL_addvalue(&b); /* add warning about skip */ + level += n; /* and skip to last levels */ } else { lua_getinfo(L1, "Slnt", &ar); - lua_pushfstring(L, "\n\t%s:", ar.short_src); - if (ar.currentline > 0) - lua_pushfstring(L, "%d:", ar.currentline); - lua_pushliteral(L, " in "); + if (ar.currentline <= 0) + lua_pushfstring(L, "\n\t%s: in ", ar.short_src); + else + lua_pushfstring(L, "\n\t%s:%d: in ", ar.short_src, ar.currentline); + luaL_addvalue(&b); pushfuncname(L, &ar); + luaL_addvalue(&b); if (ar.istailcall) - lua_pushliteral(L, "\n\t(...tail calls...)"); - lua_concat(L, lua_gettop(L) - top); + luaL_addstring(&b, "\n\t(...tail calls...)"); } } - lua_concat(L, lua_gettop(L) - top); + luaL_pushresult(&b); } /* }====================================================== */ @@ -169,23 +170,31 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { lua_Debug ar; + const char *argword; if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ return luaL_error(L, "bad argument #%d (%s)", arg, extramsg); - lua_getinfo(L, "n", &ar); - if (strcmp(ar.namewhat, "method") == 0) { - arg--; /* do not count 'self' */ - if (arg == 0) /* error is in the self argument itself? */ - return luaL_error(L, "calling '%s' on bad self (%s)", - ar.name, extramsg); + lua_getinfo(L, "nt", &ar); + if (arg <= ar.extraargs) /* error in an extra argument? */ + argword = "extra argument"; + else { + arg -= ar.extraargs; /* do not count extra arguments */ + if (strcmp(ar.namewhat, "method") == 0) { /* colon syntax? */ + arg--; /* do not count (extra) self argument */ + if (arg == 0) /* error in self argument? */ + return luaL_error(L, "calling '%s' on bad self (%s)", + ar.name, extramsg); + /* else go through; error in a regular argument */ + } + argword = "argument"; } if (ar.name == NULL) ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; - return luaL_error(L, "bad argument #%d to '%s' (%s)", - arg, ar.name, extramsg); + return luaL_error(L, "bad %s #%d to '%s' (%s)", + argword, arg, ar.name, extramsg); } -int luaL_typeerror (lua_State *L, int arg, const char *tname) { +LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) { const char *msg; const char *typearg; /* name for the type of the actual argument */ if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) @@ -224,7 +233,7 @@ LUALIB_API void luaL_where (lua_State *L, int level) { /* ** Again, the use of 'lua_pushvfstring' ensures this function does ** not need reserved stack space when called. (At worst, it generates -** an error with "stack overflow" instead of the given message.) +** a memory error instead of the given message.) */ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { va_list argp; @@ -244,11 +253,13 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { return 1; } else { - lua_pushnil(L); + const char *msg; + luaL_pushfail(L); + msg = (en != 0) ? strerror(en) : "(no extra info)"; if (fname) - lua_pushfstring(L, "%s: %s", fname, strerror(en)); + lua_pushfstring(L, "%s: %s", fname, msg); else - lua_pushstring(L, strerror(en)); + lua_pushstring(L, msg); lua_pushinteger(L, en); return 3; } @@ -278,18 +289,18 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { LUALIB_API int luaL_execresult (lua_State *L, int stat) { - const char *what = "exit"; /* type of termination */ - if (stat == -1) /* error? */ + if (stat != 0 && errno != 0) /* error with an 'errno'? */ return luaL_fileresult(L, 0, NULL); else { + const char *what = "exit"; /* type of termination */ l_inspectstat(stat, what); /* interpret result */ if (*what == 'e' && stat == 0) /* successful termination? */ lua_pushboolean(L, 1); else - lua_pushnil(L); + luaL_pushfail(L); lua_pushstring(L, what); lua_pushinteger(L, stat); - return 3; /* return true/nil,what,code */ + return 3; /* return true/fail,what,code */ } } @@ -373,7 +384,7 @@ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def, ** but without 'msg'.) */ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { - if (!lua_checkstack(L, space)) { + if (l_unlikely(!lua_checkstack(L, space))) { if (msg) luaL_error(L, "stack overflow (%s)", msg); else @@ -383,20 +394,20 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { - if (lua_type(L, arg) != t) + if (l_unlikely(lua_type(L, arg) != t)) tag_error(L, arg, t); } LUALIB_API void luaL_checkany (lua_State *L, int arg) { - if (lua_type(L, arg) == LUA_TNONE) + if (l_unlikely(lua_type(L, arg) == LUA_TNONE)) luaL_argerror(L, arg, "value expected"); } LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { const char *s = lua_tolstring(L, arg, len); - if (!s) tag_error(L, arg, LUA_TSTRING); + if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING); return s; } @@ -415,7 +426,7 @@ LUALIB_API const char *luaL_optlstring (lua_State *L, int arg, LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { int isnum; lua_Number d = lua_tonumberx(L, arg, &isnum); - if (!isnum) + if (l_unlikely(!isnum)) tag_error(L, arg, LUA_TNUMBER); return d; } @@ -437,7 +448,7 @@ static void interror (lua_State *L, int arg) { LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { int isnum; lua_Integer d = lua_tointegerx(L, arg, &isnum); - if (!isnum) { + if (l_unlikely(!isnum)) { interror(L, arg); } return d; @@ -465,16 +476,27 @@ typedef struct UBox { } UBox; +/* Resize the buffer used by a box. Optimize for the common case of +** resizing to the old size. (For instance, __gc will resize the box +** to 0 even after it was closed. 'pushresult' may also resize it to a +** final size that is equal to the one set when the buffer was created.) +*/ static void *resizebox (lua_State *L, int idx, size_t newsize) { - void *ud; - lua_Alloc allocf = lua_getallocf(L, &ud); UBox *box = (UBox *)lua_touserdata(L, idx); - void *temp = allocf(ud, box->box, box->bsize, newsize); - if (temp == NULL && newsize > 0) /* allocation error? */ - luaL_error(L, "not enough memory for buffer allocation"); - box->box = temp; - box->bsize = newsize; - return temp; + if (box->bsize == newsize) /* not changing size? */ + return box->box; /* keep the buffer */ + else { + void *ud; + lua_Alloc allocf = lua_getallocf(L, &ud); + void *temp = allocf(ud, box->box, box->bsize, newsize); + if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */ + lua_pushliteral(L, "not enough memory"); + lua_error(L); /* raise a memory error */ + } + box->box = temp; + box->bsize = newsize; + return temp; + } } @@ -508,26 +530,39 @@ static void newbox (lua_State *L) { #define buffonstack(B) ((B)->b != (B)->init.b) +/* +** Whenever buffer is accessed, slot 'idx' must either be a box (which +** cannot be NULL) or it is a placeholder for the buffer. +*/ +#define checkbufferlevel(B,idx) \ + lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL \ + : lua_touserdata(B->L, idx) == (void*)B) + + /* ** Compute new size for buffer 'B', enough to accommodate extra 'sz' -** bytes. +** bytes plus one for a terminating zero. */ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { - size_t newsize = B->size * 2; /* double buffer size */ - if (MAX_SIZET - sz < B->n) /* overflow in (B->n + sz)? */ - return luaL_error(B->L, "buffer too large"); - if (newsize < B->n + sz) /* double is not big enough? */ - newsize = B->n + sz; + size_t newsize = B->size; + if (l_unlikely(sz >= MAX_SIZE - B->n)) + return cast_sizet(luaL_error(B->L, "resulting string too large")); + /* else B->n + sz + 1 <= MAX_SIZE */ + if (newsize <= MAX_SIZE/3 * 2) /* no overflow? */ + newsize += (newsize >> 1); /* new size *= 1.5 */ + if (newsize < B->n + sz + 1) /* not big enough? */ + newsize = B->n + sz + 1; return newsize; } /* ** Returns a pointer to a free area with at least 'sz' bytes in buffer -** 'B'. 'boxidx' is the relative position in the stack where the -** buffer's box is or should be. +** 'B'. 'boxidx' is the relative position in the stack where is the +** buffer's box or its placeholder. */ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { + checkbufferlevel(B, boxidx); if (B->size - B->n >= sz) /* enough space? */ return B->b + B->n; else { @@ -538,10 +573,9 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { if (buffonstack(B)) /* buffer already has a box? */ newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ else { /* no box yet */ - lua_pushnil(L); /* reserve slot for final result */ + lua_remove(L, boxidx); /* remove placeholder */ newbox(L); /* create a new box */ - /* move box (and slot) to its intended position */ - lua_rotate(L, boxidx - 1, 2); + lua_insert(L, boxidx); /* move box to its intended position */ lua_toclose(L, boxidx); newbuff = (char *)resizebox(L, boxidx, newsize); memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ @@ -576,11 +610,25 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { LUALIB_API void luaL_pushresult (luaL_Buffer *B) { lua_State *L = B->L; - lua_pushlstring(L, B->b, B->n); - if (buffonstack(B)) { - lua_copy(L, -1, -3); /* move string to reserved slot */ - lua_pop(L, 2); /* pop string and box (closing the box) */ + checkbufferlevel(B, -1); + if (!buffonstack(B)) /* using static buffer? */ + lua_pushlstring(L, B->b, B->n); /* save result as regular string */ + else { /* reuse buffer already allocated */ + UBox *box = (UBox *)lua_touserdata(L, -1); + void *ud; + lua_Alloc allocf = lua_getallocf(L, &ud); /* function to free buffer */ + size_t len = B->n; /* final string length */ + char *s; + resizebox(L, -1, len + 1); /* adjust box size to content size */ + s = (char*)box->box; /* final buffer address */ + s[len] = '\0'; /* add ending zero */ + /* clear box, as Lua will take control of the buffer */ + box->bsize = 0; box->box = NULL; + lua_pushexternalstring(L, s, len, allocf, ud); + lua_closeslot(L, -2); /* close the box */ + lua_gc(L, LUA_GCSTEP, len); } + lua_remove(L, -2); /* remove box or placeholder from the stack */ } @@ -595,7 +643,7 @@ LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { ** box (if existent) is not on the top of the stack. So, instead of ** calling 'luaL_addlstring', it replicates the code using -2 as the ** last argument to 'prepbuffsize', signaling that the box is (or will -** be) bellow the string being added to the buffer. (Box creation can +** be) below the string being added to the buffer. (Box creation can ** trigger an emergency GC, so we should not remove the string from the ** stack before we have the space guaranteed.) */ @@ -615,6 +663,7 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { B->b = B->init.b; B->n = 0; B->size = LUAL_BUFFERSIZE; + lua_pushlightuserdata(L, (void*)B); /* push placeholder */ } @@ -632,10 +681,11 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { ** ======================================================= */ -/* index of free-list header */ -#define freelist 0 - - +/* +** The previously freed references form a linked list: t[1] is the index +** of a first free index, t[t[1]] is the index of the second element, +** etc. A zero signals the end of the list. +*/ LUALIB_API int luaL_ref (lua_State *L, int t) { int ref; if (lua_isnil(L, -1)) { @@ -643,12 +693,18 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { return LUA_REFNIL; /* 'nil' has a unique fixed reference */ } t = lua_absindex(L, t); - lua_rawgeti(L, t, freelist); /* get first free element */ - ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */ - lua_pop(L, 1); /* remove it from stack */ + if (lua_rawgeti(L, t, 1) == LUA_TNUMBER) /* already initialized? */ + ref = (int)lua_tointeger(L, -1); /* ref = t[1] */ + else { /* first access */ + lua_assert(!lua_toboolean(L, -1)); /* must be nil or false */ + ref = 0; /* list is empty */ + lua_pushinteger(L, 0); /* initialize as an empty list */ + lua_rawseti(L, t, 1); /* ref = t[1] = 0 */ + } + lua_pop(L, 1); /* remove element from stack */ if (ref != 0) { /* any free element? */ lua_rawgeti(L, t, ref); /* remove it from list */ - lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */ + lua_rawseti(L, t, 1); /* (t[1] = t[ref]) */ } else /* no free elements */ ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */ @@ -660,10 +716,11 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { if (ref >= 0) { t = lua_absindex(L, t); - lua_rawgeti(L, t, freelist); - lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */ + lua_rawgeti(L, t, 1); + lua_assert(lua_isinteger(L, -1)); + lua_rawseti(L, t, ref); /* t[ref] = t[1] */ lua_pushinteger(L, ref); - lua_rawseti(L, t, freelist); /* t[freelist] = ref */ + lua_rawseti(L, t, 1); /* t[1] = ref */ } } @@ -677,7 +734,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { */ typedef struct LoadF { - int n; /* number of pre-read characters */ + unsigned n; /* number of pre-read characters */ FILE *f; /* file being read */ char buff[BUFSIZ]; /* area for reading file */ } LoadF; @@ -685,7 +742,7 @@ typedef struct LoadF { static const char *getF (lua_State *L, void *ud, size_t *size) { LoadF *lf = (LoadF *)ud; - (void)L; /* not used */ + UNUSED(L); if (lf->n > 0) { /* are there pre-read characters to be read? */ *size = lf->n; /* return them (chars already in buffer) */ lf->n = 0; /* no more pre-read characters */ @@ -702,25 +759,29 @@ static const char *getF (lua_State *L, void *ud, size_t *size) { static int errfile (lua_State *L, const char *what, int fnameindex) { - const char *serr = strerror(errno); + int err = errno; const char *filename = lua_tostring(L, fnameindex) + 1; - lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); + if (err != 0) + lua_pushfstring(L, "cannot %s %s: %s", what, filename, strerror(err)); + else + lua_pushfstring(L, "cannot %s %s", what, filename); lua_remove(L, fnameindex); return LUA_ERRFILE; } -static int skipBOM (LoadF *lf) { - const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */ - int c; - lf->n = 0; - do { - c = getc(lf->f); - if (c == EOF || c != *(const unsigned char *)p++) return c; - lf->buff[lf->n++] = c; /* to be read by the parser */ - } while (*p != '\0'); - lf->n = 0; /* prefix matched; discard it */ - return getc(lf->f); /* return next character */ +/* +** Skip an optional BOM at the start of a stream. If there is an +** incomplete BOM (the first character is correct but the rest is +** not), returns the first character anyway to force an error +** (as no chunk can start with 0xEF). +*/ +static int skipBOM (FILE *f) { + int c = getc(f); /* read first character */ + if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF) /* correct BOM? */ + return getc(f); /* ignore BOM and return next char */ + else /* no (valid) BOM */ + return c; /* return first character */ } @@ -731,13 +792,13 @@ static int skipBOM (LoadF *lf) { ** first "valid" character of the file (after the optional BOM and ** a first-line comment). */ -static int skipcomment (LoadF *lf, int *cp) { - int c = *cp = skipBOM(lf); +static int skipcomment (FILE *f, int *cp) { + int c = *cp = skipBOM(f); if (c == '#') { /* first line is a comment (Unix exec. file)? */ do { /* skip first line */ - c = getc(lf->f); + c = getc(f); } while (c != EOF && c != '\n'); - *cp = getc(lf->f); /* skip end-of-line, if present */ + *cp = getc(f); /* next character after comment, if present */ return 1; /* there was a comment */ } else return 0; /* no comment */ @@ -756,20 +817,27 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, } else { lua_pushfstring(L, "@%s", filename); + errno = 0; lf.f = fopen(filename, "r"); if (lf.f == NULL) return errfile(L, "open", fnameindex); } - if (skipcomment(&lf, &c)) /* read initial portion */ - lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ - if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ - lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ - if (lf.f == NULL) return errfile(L, "reopen", fnameindex); - skipcomment(&lf, &c); /* re-read initial portion */ + lf.n = 0; + if (skipcomment(lf.f, &c)) /* read initial portion */ + lf.buff[lf.n++] = '\n'; /* add newline to correct line numbers */ + if (c == LUA_SIGNATURE[0]) { /* binary file? */ + lf.n = 0; /* remove possible newline */ + if (filename) { /* "real" file? */ + errno = 0; + lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ + if (lf.f == NULL) return errfile(L, "reopen", fnameindex); + skipcomment(lf.f, &c); /* re-read initial portion */ + } } if (c != EOF) - lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ + lf.buff[lf.n++] = cast_char(c); /* 'c' is the first character */ status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); readstatus = ferror(lf.f); + errno = 0; /* no useful error number until here */ if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus) { lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ @@ -788,7 +856,7 @@ typedef struct LoadS { static const char *getS (lua_State *L, void *ud, size_t *size) { LoadS *ls = (LoadS *)ud; - (void)L; /* not used */ + UNUSED(L); if (ls->size == 0) return NULL; *size = ls->size; ls->size = 0; @@ -844,7 +912,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { int isnum; lua_len(L, idx); l = lua_tointegerx(L, -1, &isnum); - if (!isnum) + if (l_unlikely(!isnum)) luaL_error(L, "object length is not an integer"); lua_pop(L, 1); /* remove object */ return l; @@ -852,6 +920,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { + idx = lua_absindex(L,idx); if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */ if (!lua_isstring(L, -1)) luaL_error(L, "'__tostring' must return a string"); @@ -859,10 +928,9 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { else { switch (lua_type(L, idx)) { case LUA_TNUMBER: { - if (lua_isinteger(L, idx)) - lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx)); - else - lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx)); + char buff[LUA_N2SBUFFSZ]; + lua_numbertocstring(L, idx, buff); + lua_pushstring(L, buff); break; } case LUA_TSTRING: @@ -897,10 +965,14 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { luaL_checkstack(L, nup, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ - int i; - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(L, -nup); - lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + if (l->func == NULL) /* placeholder? */ + lua_pushboolean(L, 0); + else { + int i; + for (i = 0; i < nup; i++) /* copy upvalues to the top */ + lua_pushvalue(L, -nup); + lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + } lua_setfield(L, -(nup + 2), l->name); } lua_pop(L, nup); /* remove upvalues */ @@ -951,25 +1023,31 @@ LUALIB_API void luaL_requiref (lua_State *L, const char *modname, } -LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, - const char *r) { +LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, + const char *p, const char *r) { const char *wild; size_t l = strlen(p); - luaL_Buffer b; - luaL_buffinit(L, &b); while ((wild = strstr(s, p)) != NULL) { - luaL_addlstring(&b, s, wild - s); /* push prefix */ - luaL_addstring(&b, r); /* push replacement in place of pattern */ + luaL_addlstring(b, s, ct_diff2sz(wild - s)); /* push prefix */ + luaL_addstring(b, r); /* push replacement in place of pattern */ s = wild + l; /* continue after 'p' */ } - luaL_addstring(&b, s); /* push last suffix */ + luaL_addstring(b, s); /* push last suffix */ +} + + +LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, + const char *p, const char *r) { + luaL_Buffer b; + luaL_buffinit(L, &b); + luaL_addgsub(&b, s, p, r); luaL_pushresult(&b); return lua_tostring(L, -1); } -static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { - (void)ud; (void)osize; /* not used */ +void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { + UNUSED(ud); UNUSED(osize); if (nsize == 0) { free(ptr); return NULL; @@ -979,37 +1057,135 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { } +/* +** Standard panic function just prints an error message. The test +** with 'lua_type' avoids possible memory errors in 'lua_tostring'. +*/ static int panic (lua_State *L) { + const char *msg = (lua_type(L, -1) == LUA_TSTRING) + ? lua_tostring(L, -1) + : "error object is not a string"; lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)); + msg); return 0; /* return to Lua to abort */ } /* -** Emit a warning. '*previoustocont' signals whether previous message -** was to be continued by the current one. +** Warning functions: +** warnfoff: warning system is off +** warnfon: ready to start a new message +** warnfcont: previous message is to be continued */ -static void warnf (void *ud, const char *message, int tocont) { - int *previoustocont = (int *)ud; - if (!*previoustocont) /* previous message was the last? */ - lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ +static void warnfoff (void *ud, const char *message, int tocont); +static void warnfon (void *ud, const char *message, int tocont); +static void warnfcont (void *ud, const char *message, int tocont); + + +/* +** Check whether message is a control message. If so, execute the +** control or ignore it if unknown. +*/ +static int checkcontrol (lua_State *L, const char *message, int tocont) { + if (tocont || *(message++) != '@') /* not a control message? */ + return 0; + else { + if (strcmp(message, "off") == 0) + lua_setwarnf(L, warnfoff, L); /* turn warnings off */ + else if (strcmp(message, "on") == 0) + lua_setwarnf(L, warnfon, L); /* turn warnings on */ + return 1; /* it was a control message */ + } +} + + +static void warnfoff (void *ud, const char *message, int tocont) { + checkcontrol((lua_State *)ud, message, tocont); +} + + +/* +** Writes the message and handle 'tocont', finishing the message +** if needed and setting the next warn function. +*/ +static void warnfcont (void *ud, const char *message, int tocont) { + lua_State *L = (lua_State *)ud; lua_writestringerror("%s", message); /* write message */ - if (!tocont) /* is this the last part? */ + if (tocont) /* not the last part? */ + lua_setwarnf(L, warnfcont, L); /* to be continued */ + else { /* last part */ lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ - *previoustocont = tocont; + lua_setwarnf(L, warnfon, L); /* next call is a new message */ + } +} + + +static void warnfon (void *ud, const char *message, int tocont) { + if (checkcontrol((lua_State *)ud, message, tocont)) /* control message? */ + return; /* nothing else to be done */ + lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ + warnfcont(ud, message, tocont); /* finish processing */ } -LUALIB_API lua_State *luaL_newstate (void) { - lua_State *L = lua_newstate(l_alloc, NULL); - if (L) { - int *previoustocont; /* space for warning state */ + +/* +** A function to compute an unsigned int with some level of +** randomness. Rely on Address Space Layout Randomization (if present) +** and the current time. +*/ +#if !defined(luai_makeseed) + +#include + + +/* Size for the buffer, in bytes */ +#define BUFSEEDB (sizeof(void*) + sizeof(time_t)) + +/* Size for the buffer in int's, rounded up */ +#define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int)) + +/* +** Copy the contents of variable 'v' into the buffer pointed by 'b'. +** (The '&b[0]' disguises 'b' to fix an absurd warning from clang.) +*/ +#define addbuff(b,v) (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v)) + + +static unsigned int luai_makeseed (void) { + unsigned int buff[BUFSEED]; + unsigned int res; + unsigned int i; + time_t t = time(NULL); + char *b = (char*)buff; + addbuff(b, b); /* local variable's address */ + addbuff(b, t); /* time */ + /* fill (rare but possible) remain of the buffer with zeros */ + memset(b, 0, sizeof(buff) - BUFSEEDB); + res = buff[0]; + for (i = 1; i < BUFSEED; i++) + res ^= (res >> 3) + (res << 7) + buff[i]; + return res; +} + +#endif + + +LUALIB_API unsigned int luaL_makeseed (lua_State *L) { + UNUSED(L); + return luai_makeseed(); +} + + +/* +** Use the name with parentheses so that headers can redefine it +** as a macro. +*/ +LUALIB_API lua_State *(luaL_newstate) (void) { + lua_State *L = lua_newstate(luaL_alloc, NULL, luaL_makeseed(NULL)); + if (l_likely(L)) { lua_atpanic(L, &panic); - previoustocont = (int *)lua_newuserdatauv(L, sizeof(int), 0); - luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */ - *previoustocont = 0; /* next message starts a new warning */ - lua_setwarnf(L, warnf, previoustocont); + lua_setwarnf(L, warnfon, L); } return L; } diff --git a/lauxlib.h b/lauxlib.h index e5d378aebe..7f1d3ca195 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -12,13 +12,16 @@ #include #include +#include "luaconf.h" #include "lua.h" /* global table */ -#define LUA_GNAME "_G" +#define LUA_GNAME "_G" +typedef struct luaL_Buffer luaL_Buffer; + /* extra error code for 'luaL_loadfilex' */ #define LUA_ERRFILE (LUA_ERRERR+1) @@ -78,6 +81,9 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); LUALIB_API int (luaL_execresult) (lua_State *L, int stat); +LUALIB_API void *luaL_alloc (void *ud, void *ptr, size_t osize, + size_t nsize); + /* predefined references */ #define LUA_NOREF (-2) @@ -97,10 +103,14 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); LUALIB_API lua_State *(luaL_newstate) (void); +LUALIB_API unsigned luaL_makeseed (lua_State *L); + LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); -LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, - const char *r); +LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s, + const char *p, const char *r); +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, + const char *p, const char *r); LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); @@ -126,10 +136,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) #define luaL_argcheck(L, cond,arg,extramsg) \ - ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) + ((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg)))) #define luaL_argexpected(L,cond,arg,tname) \ - ((void)((cond) || luaL_typeerror(L, (arg), (tname)))) + ((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname)))) #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) @@ -149,13 +159,30 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) +/* +** Perform arithmetic operations on lua_Integer values with wrap-around +** semantics, as the Lua core does. +*/ +#define luaL_intop(op,v1,v2) \ + ((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2))) + + +/* push the value used to represent failure/error */ +#if defined(LUA_FAILISFALSE) +#define luaL_pushfail(L) lua_pushboolean(L, 0) +#else +#define luaL_pushfail(L) lua_pushnil(L) +#endif + + + /* ** {====================================================== ** Generic Buffer manipulation ** ======================================================= */ -typedef struct luaL_Buffer { +struct luaL_Buffer { char *b; /* buffer address */ size_t size; /* buffer size */ size_t n; /* number of characters in buffer */ @@ -164,7 +191,11 @@ typedef struct luaL_Buffer { LUAI_MAXALIGN; /* ensure maximum alignment for buffer */ char b[LUAL_BUFFERSIZE]; /* initial buffer */ } init; -} luaL_Buffer; +}; + + +#define luaL_bufflen(bf) ((bf)->n) +#define luaL_buffaddr(bf) ((bf)->b) #define luaL_addchar(B,c) \ @@ -173,6 +204,8 @@ typedef struct luaL_Buffer { #define luaL_addsize(B,s) ((B)->n += (s)) +#define luaL_buffsub(B,s) ((B)->n -= (s)) + LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); @@ -210,30 +243,6 @@ typedef struct luaL_Stream { /* }====================================================== */ -/* -** {================================================================== -** "Abstraction Layer" for basic report of messages and errors -** =================================================================== -*/ - -/* print a string */ -#if !defined(lua_writestring) -#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) -#endif - -/* print a newline and flush the output */ -#if !defined(lua_writeline) -#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) -#endif - -/* print an error message */ -#if !defined(lua_writestringerror) -#define lua_writestringerror(s,p) \ - (fprintf(stderr, (s), (p)), fflush(stderr)) -#endif - -/* }================================================================== */ - /* ** {============================================================ diff --git a/lbaselib.c b/lbaselib.c index d4b619a530..891bb90f48 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -19,23 +19,18 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" static int luaB_print (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ int i; - lua_getglobal(L, "tostring"); - for (i=1; i<=n; i++) { - const char *s; + for (i = 1; i <= n; i++) { /* for each argument */ size_t l; - lua_pushvalue(L, -1); /* function to be called */ - lua_pushvalue(L, i); /* value to print */ - lua_call(L, 1, 1); - s = lua_tolstring(L, -1, &l); /* get result */ - if (s == NULL) - return luaL_error(L, "'tostring' must return a string to 'print'"); - if (i>1) lua_writestring("\t", 1); - lua_writestring(s, l); + const char *s = luaL_tolstring(L, i, &l); /* convert it to string */ + if (i > 1) /* not the first element? */ + lua_writestring("\t", 1); /* add a tab before it */ + lua_writestring(s, l); /* print it */ lua_pop(L, 1); /* pop result */ } lua_writeline(); @@ -43,30 +38,42 @@ static int luaB_print (lua_State *L) { } +/* +** Creates a warning with all given arguments. +** Check first for errors; otherwise an error may interrupt +** the composition of a warning, leaving it unfinished. +*/ static int luaB_warn (lua_State *L) { - const char *msg = luaL_checkstring(L, 1); - lua_warning(L, msg, lua_toboolean(L, 2)); + int n = lua_gettop(L); /* number of arguments */ + int i; + luaL_checkstring(L, 1); /* at least one argument */ + for (i = 2; i <= n; i++) + luaL_checkstring(L, i); /* make sure all arguments are strings */ + for (i = 1; i < n; i++) /* compose warning */ + lua_warning(L, lua_tostring(L, i), 1); + lua_warning(L, lua_tostring(L, n), 0); /* close warning */ return 0; } #define SPACECHARS " \f\n\r\t\v" -static const char *b_str2int (const char *s, int base, lua_Integer *pn) { +static const char *b_str2int (const char *s, unsigned base, lua_Integer *pn) { lua_Unsigned n = 0; int neg = 0; s += strspn(s, SPACECHARS); /* skip initial spaces */ if (*s == '-') { s++; neg = 1; } /* handle sign */ else if (*s == '+') s++; - if (!isalnum((unsigned char)*s)) /* no digit? */ + if (!isalnum(cast_uchar(*s))) /* no digit? */ return NULL; do { - int digit = (isdigit((unsigned char)*s)) ? *s - '0' - : (toupper((unsigned char)*s) - 'A') + 10; + unsigned digit = cast_uint(isdigit(cast_uchar(*s)) + ? *s - '0' + : (toupper(cast_uchar(*s)) - 'A') + 10); if (digit >= base) return NULL; /* invalid numeral */ n = n * base + digit; s++; - } while (isalnum((unsigned char)*s)); + } while (isalnum(cast_uchar(*s))); s += strspn(s, SPACECHARS); /* skip trailing spaces */ *pn = (lua_Integer)((neg) ? (0u - n) : n); return s; @@ -96,12 +103,12 @@ static int luaB_tonumber (lua_State *L) { luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */ s = lua_tolstring(L, 1, &l); luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); - if (b_str2int(s, (int)base, &n) == s + l) { + if (b_str2int(s, cast_uint(base), &n) == s + l) { lua_pushinteger(L, n); return 1; } /* else not a number */ } /* else not a number */ - lua_pushnil(L); /* not a number */ + luaL_pushfail(L); /* not a number */ return 1; } @@ -133,7 +140,7 @@ static int luaB_setmetatable (lua_State *L) { int t = lua_type(L, 2); luaL_checktype(L, 1, LUA_TTABLE); luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); - if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL) + if (l_unlikely(luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)) return luaL_error(L, "cannot change a protected metatable"); lua_settop(L, 2); lua_setmetatable(L, 1); @@ -153,7 +160,7 @@ static int luaB_rawlen (lua_State *L) { int t = lua_type(L, 1); luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, "table or string"); - lua_pushinteger(L, lua_rawlen(L, 1)); + lua_pushinteger(L, l_castU2S(lua_rawlen(L, 1))); return 1; } @@ -177,61 +184,76 @@ static int luaB_rawset (lua_State *L) { static int pushmode (lua_State *L, int oldmode) { - lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational"); + if (oldmode == -1) + luaL_pushfail(L); /* invalid call to 'lua_gc' */ + else + lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" + : "generational"); return 1; } +/* +** check whether call to 'lua_gc' was valid (not inside a finalizer) +*/ +#define checkvalres(res) { if (res == -1) break; } + static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", - "count", "step", "setpause", "setstepmul", - "isrunning", "generational", "incremental", NULL}; - static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, - LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, - LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; + "count", "step", "isrunning", "generational", "incremental", + "param", NULL}; + static const char optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, + LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC, + LUA_GCPARAM}; int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; switch (o) { case LUA_GCCOUNT: { int k = lua_gc(L, o); int b = lua_gc(L, LUA_GCCOUNTB); + checkvalres(k); lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024)); return 1; } case LUA_GCSTEP: { - int step = (int)luaL_optinteger(L, 2, 0); - int res = lua_gc(L, o, step); + lua_Integer n = luaL_optinteger(L, 2, 0); + int res = lua_gc(L, o, cast_sizet(n)); + checkvalres(res); lua_pushboolean(L, res); return 1; } - case LUA_GCSETPAUSE: - case LUA_GCSETSTEPMUL: { - int p = (int)luaL_optinteger(L, 2, 0); - int previous = lua_gc(L, o, p); - lua_pushinteger(L, previous); - return 1; - } case LUA_GCISRUNNING: { int res = lua_gc(L, o); + checkvalres(res); lua_pushboolean(L, res); return 1; } case LUA_GCGEN: { - int minormul = (int)luaL_optinteger(L, 2, 0); - int majormul = (int)luaL_optinteger(L, 3, 0); - return pushmode(L, lua_gc(L, o, minormul, majormul)); + return pushmode(L, lua_gc(L, o)); } case LUA_GCINC: { - int pause = (int)luaL_optinteger(L, 2, 0); - int stepmul = (int)luaL_optinteger(L, 3, 0); - int stepsize = (int)luaL_optinteger(L, 4, 0); - return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize)); + return pushmode(L, lua_gc(L, o)); + } + case LUA_GCPARAM: { + static const char *const params[] = { + "minormul", "majorminor", "minormajor", + "pause", "stepmul", "stepsize", NULL}; + static const char pnum[] = { + LUA_GCPMINORMUL, LUA_GCPMAJORMINOR, LUA_GCPMINORMAJOR, + LUA_GCPPAUSE, LUA_GCPSTEPMUL, LUA_GCPSTEPSIZE}; + int p = pnum[luaL_checkoption(L, 2, NULL, params)]; + lua_Integer value = luaL_optinteger(L, 3, -1); + lua_pushinteger(L, lua_gc(L, o, p, (int)value)); + return 1; } default: { int res = lua_gc(L, o); + checkvalres(res); lua_pushinteger(L, res); return 1; } } + luaL_pushfail(L); /* invalid call (inside a finalizer) */ + return 1; } @@ -255,18 +277,24 @@ static int luaB_next (lua_State *L) { } +static int pairscont (lua_State *L, int status, lua_KContext k) { + (void)L; (void)status; (void)k; /* unused */ + return 4; /* __pairs did all the work, just return its results */ +} + static int luaB_pairs (lua_State *L) { luaL_checkany(L, 1); if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */ - lua_pushcfunction(L, luaB_next); /* will return generator, */ - lua_pushvalue(L, 1); /* state, */ - lua_pushnil(L); /* and initial value */ + lua_pushcfunction(L, luaB_next); /* will return generator and */ + lua_pushvalue(L, 1); /* state */ + lua_pushnil(L); /* initial value */ + lua_pushnil(L); /* to-be-closed object */ } else { lua_pushvalue(L, 1); /* argument 'self' to metamethod */ - lua_call(L, 1, 3); /* get 3 values from metamethod */ + lua_callk(L, 1, 4, 0, pairscont); /* get 4 values from metamethod */ } - return 3; + return 4; } @@ -274,7 +302,8 @@ static int luaB_pairs (lua_State *L) { ** Traversal function for 'ipairs' */ static int ipairsaux (lua_State *L) { - lua_Integer i = luaL_checkinteger(L, 2) + 1; + lua_Integer i = luaL_checkinteger(L, 2); + i = luaL_intop(+, i, 1); lua_pushinteger(L, i); return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2; } @@ -294,7 +323,7 @@ static int luaB_ipairs (lua_State *L) { static int load_aux (lua_State *L, int status, int envidx) { - if (status == LUA_OK) { + if (l_likely(status == LUA_OK)) { if (envidx != 0) { /* 'env' parameter? */ lua_pushvalue(L, envidx); /* environment for loaded function */ if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ @@ -303,16 +332,24 @@ static int load_aux (lua_State *L, int status, int envidx) { return 1; } else { /* error (message is on top of the stack) */ - lua_pushnil(L); + luaL_pushfail(L); lua_insert(L, -2); /* put before error message */ - return 2; /* return nil plus error message */ + return 2; /* return fail plus error message */ } } +static const char *getMode (lua_State *L, int idx) { + const char *mode = luaL_optstring(L, idx, "bt"); + if (strchr(mode, 'B') != NULL) /* Lua code cannot use fixed buffers */ + luaL_argerror(L, idx, "invalid mode"); + return mode; +} + + static int luaB_loadfile (lua_State *L) { const char *fname = luaL_optstring(L, 1, NULL); - const char *mode = luaL_optstring(L, 2, NULL); + const char *mode = getMode(L, 2); int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ int status = luaL_loadfilex(L, fname, mode); return load_aux(L, status, env); @@ -350,7 +387,7 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { *size = 0; return NULL; } - else if (!lua_isstring(L, -1)) + else if (l_unlikely(!lua_isstring(L, -1))) luaL_error(L, "reader function must return a string"); lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ return lua_tolstring(L, RESERVEDSLOT, size); @@ -361,7 +398,7 @@ static int luaB_load (lua_State *L) { int status; size_t l; const char *s = lua_tolstring(L, 1, &l); - const char *mode = luaL_optstring(L, 3, "bt"); + const char *mode = getMode(L, 3); int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ if (s != NULL) { /* loading a string? */ const char *chunkname = luaL_optstring(L, 2, s); @@ -388,7 +425,7 @@ static int dofilecont (lua_State *L, int d1, lua_KContext d2) { static int luaB_dofile (lua_State *L) { const char *fname = luaL_optstring(L, 1, NULL); lua_settop(L, 1); - if (luaL_loadfile(L, fname) != LUA_OK) + if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK)) return lua_error(L); lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); return dofilecont(L, 0, 0); @@ -396,7 +433,7 @@ static int luaB_dofile (lua_State *L) { static int luaB_assert (lua_State *L) { - if (lua_toboolean(L, 1)) /* condition is true? */ + if (l_likely(lua_toboolean(L, 1))) /* condition is true? */ return lua_gettop(L); /* return all arguments */ else { /* error */ luaL_checkany(L, 1); /* there must be a condition */ @@ -432,7 +469,7 @@ static int luaB_select (lua_State *L) { ** ignored). */ static int finishpcall (lua_State *L, int status, lua_KContext extra) { - if (status != LUA_OK && status != LUA_YIELD) { /* error? */ + if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) { /* error? */ lua_pushboolean(L, 0); /* first result (false) */ lua_pushvalue(L, -2); /* error message */ return 2; /* return false, msg */ diff --git a/lcode.c b/lcode.c index 1872ede2ba..4caa8046f6 100644 --- a/lcode.c +++ b/lcode.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include #include @@ -30,10 +31,7 @@ #include "lvm.h" -/* Maximum number of registers in a Lua function (must fit in 8 bits) */ -#define MAXREGS 255 - - +/* (note that expressions VJMP also have jumps.) */ #define hasjumps(e) ((e)->t != (e)->f) @@ -42,8 +40,12 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k); /* semantic error */ -l_noret luaK_semerror (LexState *ls, const char *msg) { +l_noret luaK_semerror (LexState *ls, const char *fmt, ...) { + const char *msg; + va_list argp; + pushvfstring(ls->L, argp, fmt, msg); ls->t.token = 0; /* remove "near " from final message */ + ls->linenumber = ls->lastline; /* back to line of last used token */ luaX_syntaxerror(ls, msg); } @@ -52,7 +54,7 @@ l_noret luaK_semerror (LexState *ls, const char *msg) { ** If expression is a numeric constant, fills 'v' with its value ** and returns 1. Otherwise, returns 0. */ -static int tonumeral(const expdesc *e, TValue *v) { +static int tonumeral (const expdesc *e, TValue *v) { if (hasjumps(e)) return 0; /* not a numeral */ switch (e->k) { @@ -67,6 +69,45 @@ static int tonumeral(const expdesc *e, TValue *v) { } +/* +** Get the constant value from a constant expression +*/ +static TValue *const2val (FuncState *fs, const expdesc *e) { + lua_assert(e->k == VCONST); + return &fs->ls->dyd->actvar.arr[e->u.info].k; +} + + +/* +** If expression is a constant, fills 'v' with its value +** and returns 1. Otherwise, returns 0. +*/ +int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) { + if (hasjumps(e)) + return 0; /* not a constant */ + switch (e->k) { + case VFALSE: + setbfvalue(v); + return 1; + case VTRUE: + setbtvalue(v); + return 1; + case VNIL: + setnilvalue(v); + return 1; + case VKSTR: { + setsvalue(fs->ls->L, v, e->u.strval); + return 1; + } + case VCONST: { + setobj(fs->ls->L, v, const2val(fs, e)); + return 1; + } + default: return tonumeral(e, v); + } +} + + /* ** Return the previous instruction of the current code. If there ** may be a jump target between the current instruction and the @@ -74,7 +115,7 @@ static int tonumeral(const expdesc *e, TValue *v) { ** optimizations). */ static Instruction *previousinstruction (FuncState *fs) { - static const Instruction invalidinstruction = -1; + static const Instruction invalidinstruction = ~(Instruction)0; if (fs->pc > fs->lasttarget) return &fs->f->code[fs->pc - 1]; /* previous instruction */ else @@ -171,6 +212,7 @@ void luaK_ret (FuncState *fs, int first, int nret) { case 1: op = OP_RETURN1; break; default: op = OP_RETURN; break; } + luaY_checklimit(fs, nret + 1, MAXARG_B, "returns"); luaK_codeABC(fs, op, first, nret + 1, 0); } @@ -179,8 +221,8 @@ void luaK_ret (FuncState *fs, int first, int nret) { ** Code a "conditional jump", that is, a test or comparison opcode ** followed by a jump. Return jump position. */ -static int condjump (FuncState *fs, OpCode op, int A, int B, int k) { - luaK_codeABCk(fs, op, A, B, 0, k); +static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) { + luaK_codeABCk(fs, op, A, B, C, k); return luaK_jump(fs); } @@ -275,15 +317,6 @@ void luaK_patchtohere (FuncState *fs, int list) { } -/* -** MAXimum number of successive Instructions WiTHout ABSolute line -** information. -*/ -#if !defined(MAXIWTHABS) -#define MAXIWTHABS 120 -#endif - - /* limit for difference between lines in relative line info. */ #define LIMLINEDIFF 0x80 @@ -298,17 +331,17 @@ void luaK_patchtohere (FuncState *fs, int list) { static void savelineinfo (FuncState *fs, Proto *f, int line) { int linedif = line - fs->previousline; int pc = fs->pc - 1; /* last instruction coded */ - if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ > MAXIWTHABS) { + if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) { luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo, - f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines"); + f->sizeabslineinfo, AbsLineInfo, INT_MAX, "lines"); f->abslineinfo[fs->nabslineinfo].pc = pc; f->abslineinfo[fs->nabslineinfo++].line = line; linedif = ABSLINEINFO; /* signal that there is absolute information */ - fs->iwthabs = 0; /* restart counter */ + fs->iwthabs = 1; /* restart counter */ } luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte, - MAX_INT, "opcodes"); - f->lineinfo[pc] = linedif; + INT_MAX, "opcodes"); + f->lineinfo[pc] = cast(ls_byte, linedif); fs->previousline = line; /* last line saved */ } @@ -323,12 +356,12 @@ static void removelastlineinfo (FuncState *fs) { Proto *f = fs->f; int pc = fs->pc - 1; /* last instruction coded */ if (f->lineinfo[pc] != ABSLINEINFO) { /* relative line info? */ - fs->previousline -= f->lineinfo[pc]; /* last line saved */ - fs->iwthabs--; + fs->previousline -= f->lineinfo[pc]; /* correct last line saved */ + fs->iwthabs--; /* undo previous increment */ } else { /* absolute line information */ + lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc); fs->nabslineinfo--; /* remove it */ - lua_assert(f->abslineinfo[fs->nabslineinfo].pc = pc); fs->iwthabs = MAXIWTHABS + 1; /* force next line info to be absolute */ } } @@ -348,11 +381,11 @@ static void removelastinstruction (FuncState *fs) { ** Emit instruction 'i', checking for array sizes and saving also its ** line information. Return 'i' position. */ -static int luaK_code (FuncState *fs, Instruction i) { +int luaK_code (FuncState *fs, Instruction i) { Proto *f = fs->f; /* put new instruction in code array */ luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, - MAX_INT, "opcodes"); + INT_MAX, "opcodes"); f->code[fs->pc++] = i; savelineinfo(fs, f, fs->ls->lastline); return fs->pc - 1; /* index of new instruction */ @@ -363,32 +396,40 @@ static int luaK_code (FuncState *fs, Instruction i) { ** Format and emit an 'iABC' instruction. (Assertions check consistency ** of parameters versus opcode.) */ -int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) { +int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) { lua_assert(getOpMode(o) == iABC); - lua_assert(a <= MAXARG_A && b <= MAXARG_B && - c <= MAXARG_C && (k & ~1) == 0); - return luaK_code(fs, CREATE_ABCk(o, a, b, c, k)); + lua_assert(A <= MAXARG_A && B <= MAXARG_B && + C <= MAXARG_C && (k & ~1) == 0); + return luaK_code(fs, CREATE_ABCk(o, A, B, C, k)); +} + + +int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) { + lua_assert(getOpMode(o) == ivABC); + lua_assert(A <= MAXARG_A && B <= MAXARG_vB && + C <= MAXARG_vC && (k & ~1) == 0); + return luaK_code(fs, CREATE_vABCk(o, A, B, C, k)); } /* ** Format and emit an 'iABx' instruction. */ -int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { +int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bc) { lua_assert(getOpMode(o) == iABx); - lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx); - return luaK_code(fs, CREATE_ABx(o, a, bc)); + lua_assert(A <= MAXARG_A && Bc <= MAXARG_Bx); + return luaK_code(fs, CREATE_ABx(o, A, Bc)); } /* ** Format and emit an 'iAsBx' instruction. */ -int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { - unsigned int b = bc + OFFSET_sBx; +static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) { + int b = Bc + OFFSET_sBx; lua_assert(getOpMode(o) == iAsBx); - lua_assert(a <= MAXARG_A && b <= MAXARG_Bx); - return luaK_code(fs, CREATE_ABx(o, a, b)); + lua_assert(A <= MAXARG_A && b <= MAXARG_Bx); + return luaK_code(fs, CREATE_ABx(o, A, b)); } @@ -396,7 +437,7 @@ int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { ** Format and emit an 'isJ' instruction. */ static int codesJ (FuncState *fs, OpCode o, int sj, int k) { - unsigned int j = sj + OFFSET_sJ; + int j = sj + OFFSET_sJ; lua_assert(getOpMode(o) == isJ); lua_assert(j <= MAXARG_sJ && (k & ~1) == 0); return luaK_code(fs, CREATE_sJ(o, j, k)); @@ -406,9 +447,9 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k) { /* ** Emit an "extra argument" instruction (format 'iAx') */ -static int codeextraarg (FuncState *fs, int a) { - lua_assert(a <= MAXARG_Ax); - return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); +static int codeextraarg (FuncState *fs, int A) { + lua_assert(A <= MAXARG_Ax); + return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, A)); } @@ -435,9 +476,7 @@ static int luaK_codek (FuncState *fs, int reg, int k) { void luaK_checkstack (FuncState *fs, int n) { int newstack = fs->freereg + n; if (newstack > fs->f->maxstacksize) { - if (newstack >= MAXREGS) - luaX_syntaxerror(fs->ls, - "function or expression needs too many registers"); + luaY_checklimit(fs, newstack, MAX_FSTACK, "registers"); fs->f->maxstacksize = cast_byte(newstack); } } @@ -448,7 +487,7 @@ void luaK_checkstack (FuncState *fs, int n) { */ void luaK_reserveregs (FuncState *fs, int n) { luaK_checkstack(fs, n); - fs->freereg += n; + fs->freereg = cast_byte(fs->freereg + n); } @@ -458,7 +497,7 @@ void luaK_reserveregs (FuncState *fs, int n) { ) */ static void freereg (FuncState *fs, int reg) { - if (reg >= fs->nactvar) { + if (reg >= luaY_nvarstack(fs)) { fs->freereg--; lua_assert(reg == fs->freereg); } @@ -502,31 +541,14 @@ static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { /* ** Add constant 'v' to prototype's list of constants (field 'k'). -** Use scanner's table to cache position of constants in constant list -** and try to reuse constants. Because some values should not be used -** as keys (nil cannot be a key, integer keys can collapse with float -** keys), the caller must provide a useful 'key' for indexing the cache. */ -static int addk (FuncState *fs, TValue *key, TValue *v) { +static int addk (FuncState *fs, Proto *f, TValue *v) { lua_State *L = fs->ls->L; - Proto *f = fs->f; - TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */ - int k, oldsize; - if (ttisinteger(idx)) { /* is there an index there? */ - k = cast_int(ivalue(idx)); - /* correct value? (warning: must distinguish floats from integers!) */ - if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) && - luaV_rawequalobj(&f->k[k], v)) - return k; /* reuse index */ - } - /* constant not found; create a new entry */ - oldsize = f->sizek; - k = fs->nk; - /* numerical value does not need GC barrier; - table has no metatable, so it does not need to invalidate cache */ - setivalue(idx, k); + int oldsize = f->sizek; + int k = fs->nk; luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); - while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); + while (oldsize < f->sizek) + setnilvalue(&f->k[oldsize++]); setobj(L, &f->k[k], v); fs->nk++; luaC_barrier(L, f, v); @@ -534,46 +556,106 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { } +/* +** Use scanner's table to cache position of constants in constant list +** and try to reuse constants. Because some values should not be used +** as keys (nil cannot be a key, integer keys can collapse with float +** keys), the caller must provide a useful 'key' for indexing the cache. +*/ +static int k2proto (FuncState *fs, TValue *key, TValue *v) { + TValue val; + Proto *f = fs->f; + int tag = luaH_get(fs->kcache, key, &val); /* query scanner table */ + if (!tagisempty(tag)) { /* is there an index there? */ + int k = cast_int(ivalue(&val)); + /* collisions can happen only for float keys */ + lua_assert(ttisfloat(key) || luaV_rawequalobj(&f->k[k], v)); + return k; /* reuse index */ + } + else { /* constant not found; create a new entry */ + int k = addk(fs, f, v); + /* cache it for reuse; numerical value does not need GC barrier; + table is not a metatable, so it does not need to invalidate cache */ + setivalue(&val, k); + luaH_set(fs->ls->L, fs->kcache, key, &val); + return k; + } +} + + /* ** Add a string to list of constants and return its index. */ -int luaK_stringK (FuncState *fs, TString *s) { +static int stringK (FuncState *fs, TString *s) { TValue o; setsvalue(fs->ls->L, &o, s); - return addk(fs, &o, &o); /* use string itself as key */ + return k2proto(fs, &o, &o); /* use string itself as key */ } /* ** Add an integer to list of constants and return its index. -** Integers use userdata as keys to avoid collision with floats with -** same value; conversion to 'void*' is used only for hashing, so there -** are no "precision" problems. */ static int luaK_intK (FuncState *fs, lua_Integer n) { - TValue k, o; - setpvalue(&k, cast_voidp(cast_sizet(n))); + TValue o; setivalue(&o, n); - return addk(fs, &k, &o); + return k2proto(fs, &o, &o); /* use integer itself as key */ } /* -** Add a float to list of constants and return its index. +** Add a float to list of constants and return its index. Floats +** with integral values need a different key, to avoid collision +** with actual integers. To that end, we add to the number its smaller +** power-of-two fraction that is still significant in its scale. +** (For doubles, the fraction would be 2^-52). +** This method is not bulletproof: different numbers may generate the +** same key (e.g., very large numbers will overflow to 'inf') and for +** floats larger than 2^53 the result is still an integer. For those +** cases, just generate a new entry. At worst, this only wastes an entry +** with a duplicate. */ static int luaK_numberK (FuncState *fs, lua_Number r) { + TValue o, kv; + setfltvalue(&o, r); /* value as a TValue */ + if (r == 0) { /* handle zero as a special case */ + setpvalue(&kv, fs); /* use FuncState as index */ + return k2proto(fs, &kv, &o); /* cannot collide */ + } + else { + const int nbm = l_floatatt(MANT_DIG); + const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1); + const lua_Number k = r * (1 + q); /* key */ + lua_Integer ik; + setfltvalue(&kv, k); /* key as a TValue */ + if (!luaV_flttointeger(k, &ik, F2Ieq)) { /* not an integer value? */ + int n = k2proto(fs, &kv, &o); /* use key */ + if (luaV_rawequalobj(&fs->f->k[n], &o)) /* correct value? */ + return n; + } + /* else, either key is still an integer or there was a collision; + anyway, do not try to reuse constant; instead, create a new one */ + return addk(fs, fs->f, &o); + } +} + + +/* +** Add a false to list of constants and return its index. +*/ +static int boolF (FuncState *fs) { TValue o; - setfltvalue(&o, r); - return addk(fs, &o, &o); /* use number itself as key */ + setbfvalue(&o); + return k2proto(fs, &o, &o); /* use boolean itself as key */ } /* -** Add a boolean to list of constants and return its index. +** Add a true to list of constants and return its index. */ -static int boolK (FuncState *fs, int b) { +static int boolT (FuncState *fs) { TValue o; - setbvalue(&o, b); - return addk(fs, &o, &o); /* use boolean itself as key */ + setbtvalue(&o); + return k2proto(fs, &o, &o); /* use boolean itself as key */ } @@ -583,19 +665,19 @@ static int boolK (FuncState *fs, int b) { static int nilK (FuncState *fs) { TValue k, v; setnilvalue(&v); - /* cannot use nil as key; instead use table itself to represent nil */ - sethvalue(fs->ls->L, &k, fs->ls->h); - return addk(fs, &k, &v); + /* cannot use nil as key; instead use table itself */ + sethvalue(fs->ls->L, &k, fs->kcache); + return k2proto(fs, &k, &v); } /* -** Check whether 'i' can be stored in an 'sC' operand. -** Equivalent to (0 <= i + OFFSET_sC && i + OFFSET_sC <= MAXARG_C) -** but without risk of overflows in the addition. +** Check whether 'i' can be stored in an 'sC' operand. Equivalent to +** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of +** overflows in the hidden addition inside 'int2sC'. */ static int fitsC (lua_Integer i) { - return (-OFFSET_sC <= i && i <= MAXARG_C - OFFSET_sC); + return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C)); } @@ -609,41 +691,91 @@ static int fitsBx (lua_Integer i) { void luaK_int (FuncState *fs, int reg, lua_Integer i) { if (fitsBx(i)) - luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i)); + codeAsBx(fs, OP_LOADI, reg, cast_int(i)); else luaK_codek(fs, reg, luaK_intK(fs, i)); } -static int floatI (lua_Number f, lua_Integer *fi) { - return (luaV_flttointeger(f, fi, 0) && fitsBx(*fi)); -} - - static void luaK_float (FuncState *fs, int reg, lua_Number f) { lua_Integer fi; - if (floatI(f, &fi)) - luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); + if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi)) + codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); else luaK_codek(fs, reg, luaK_numberK(fs, f)); } +/* +** Get the value of 'var' in a register and generate an opcode to check +** whether that register is nil. 'k' is the index of the variable name +** in the list of constants. If its value cannot be encoded in Bx, a 0 +** will use '?' for the name. +*/ +void luaK_codecheckglobal (FuncState *fs, expdesc *var, int k, int line) { + luaK_exp2anyreg(fs, var); + luaK_fixline(fs, line); + k = (k >= MAXARG_Bx) ? 0 : k + 1; + luaK_codeABx(fs, OP_ERRNNIL, var->u.info, k); + luaK_fixline(fs, line); + freeexp(fs, var); +} + + +/* +** Convert a constant in 'v' into an expression description 'e' +*/ +static void const2exp (TValue *v, expdesc *e) { + switch (ttypetag(v)) { + case LUA_VNUMINT: + e->k = VKINT; e->u.ival = ivalue(v); + break; + case LUA_VNUMFLT: + e->k = VKFLT; e->u.nval = fltvalue(v); + break; + case LUA_VFALSE: + e->k = VFALSE; + break; + case LUA_VTRUE: + e->k = VTRUE; + break; + case LUA_VNIL: + e->k = VNIL; + break; + case LUA_VSHRSTR: case LUA_VLNGSTR: + e->k = VKSTR; e->u.strval = tsvalue(v); + break; + default: lua_assert(0); + } +} + + /* ** Fix an expression to return the number of results 'nresults'. -** Either 'e' is a multi-ret expression (function call or vararg) -** or 'nresults' is LUA_MULTRET (as any expression can satisfy that). +** 'e' must be a multi-ret expression (function call or vararg). */ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { Instruction *pc = &getinstruction(fs, e); + luaY_checklimit(fs, nresults + 1, MAXARG_C, "multiple results"); if (e->k == VCALL) /* expression is an open function call? */ SETARG_C(*pc, nresults + 1); - else if (e->k == VVARARG) { + else { + lua_assert(e->k == VVARARG); SETARG_C(*pc, nresults + 1); SETARG_A(*pc, fs->freereg); luaK_reserveregs(fs, 1); } - else lua_assert(nresults == LUA_MULTRET); +} + + +/* +** Convert a VKSTR to a VK +*/ +static int str2K (FuncState *fs, expdesc *e) { + lua_assert(e->k == VKSTR); + e->u.info = stringK(fs, e->u.strval); + e->k = VK; + return e->u.info; } @@ -670,14 +802,32 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { } } +/* +** Change a vararg parameter into a regular local variable +*/ +void luaK_vapar2local (FuncState *fs, expdesc *var) { + needvatab(fs->f); /* function will need a vararg table */ + /* now a vararg parameter is equivalent to a regular local variable */ + var->k = VLOCAL; +} + /* -** Ensure that expression 'e' is not a variable. +** Ensure that expression 'e' is not a variable (nor a ). ** (Expression still may have jump lists.) */ void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { + case VCONST: { + const2exp(const2val(fs, e), e); + break; + } + case VVARGVAR: { + luaK_vapar2local(fs, e); /* turn it into a local variable */ + } /* FALLTHROUGH */ case VLOCAL: { /* already in a register */ + int temp = e->u.var.ridx; + e->u.info = temp; /* (can't do a direct assignment; values overlap) */ e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } @@ -709,6 +859,12 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { e->k = VRELOC; break; } + case VVARGIND: { + freeregs(fs, e->u.ind.t, e->u.ind.idx); + e->u.info = luaK_codeABC(fs, OP_GETVARG, 0, e->u.ind.t, e->u.ind.idx); + e->k = VRELOC; + break; + } case VVARARG: case VCALL: { luaK_setoneret(fs, e); break; @@ -719,8 +875,8 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { /* -** Ensures expression value is in register 'reg' (and therefore -** 'e' will become a non-relocatable expression). +** Ensure expression value is in register 'reg', making 'e' a +** non-relocatable expression. ** (Expression still may have jump lists.) */ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { @@ -730,10 +886,17 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_nil(fs, reg, 1); break; } - case VFALSE: case VTRUE: { - luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); + case VFALSE: { + luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0); + break; + } + case VTRUE: { + luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0); break; } + case VKSTR: { + str2K(fs, e); + } /* FALLTHROUGH */ case VK: { luaK_codek(fs, reg, e->u.info); break; @@ -767,7 +930,8 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { /* -** Ensures expression value is in any register. +** Ensure expression value is in a register, making 'e' a +** non-relocatable expression. ** (Expression still may have jump lists.) */ static void discharge2anyreg (FuncState *fs, expdesc *e) { @@ -778,9 +942,9 @@ static void discharge2anyreg (FuncState *fs, expdesc *e) { } -static int code_loadbool (FuncState *fs, int A, int b, int jump) { +static int code_loadbool (FuncState *fs, int A, OpCode op) { luaK_getlabel(fs); /* those instructions may be jump targets */ - return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); + return luaK_codeABC(fs, op, A, 0, 0); } @@ -799,7 +963,7 @@ static int need_value (FuncState *fs, int list) { /* ** Ensures final expression result (which includes results from its -** jump ** lists) is in register 'reg'. +** jump lists) is in register 'reg'. ** If expression has jumps, need to patch these jumps either to ** its final position or to "load" instructions (for those tests ** that do not produce values). @@ -814,8 +978,9 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) { int p_t = NO_JUMP; /* position of an eventual LOAD true */ if (need_value(fs, e->t) || need_value(fs, e->f)) { int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); - p_f = code_loadbool(fs, reg, 0, 1); - p_t = code_loadbool(fs, reg, 1, 0); + p_f = code_loadbool(fs, reg, OP_LFALSESKIP); /* skip next inst. */ + p_t = code_loadbool(fs, reg, OP_LOADTRUE); + /* jump around these booleans if 'e' is not a test */ luaK_patchtohere(fs, fj); } final = luaK_getlabel(fs); @@ -848,22 +1013,25 @@ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { if (e->k == VNONRELOC) { /* expression already has a register? */ if (!hasjumps(e)) /* no jumps? */ return e->u.info; /* result is already in a register */ - if (e->u.info >= fs->nactvar) { /* reg. is not a local? */ + if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */ exp2reg(fs, e, e->u.info); /* put final result in it */ return e->u.info; } + /* else expression has jumps and cannot change its register + to hold the jump values, because it is a local variable. + Go through to the default case. */ } - luaK_exp2nextreg(fs, e); /* otherwise, use next available register */ + luaK_exp2nextreg(fs, e); /* default: use next available register */ return e->u.info; } /* -** Ensures final expression result is either in a register -** or in an upvalue. +** Ensures final expression result is either in a register, +** in an upvalue, or it is the vararg parameter. */ void luaK_exp2anyregup (FuncState *fs, expdesc *e) { - if (e->k != VUPVAL || hasjumps(e)) + if ((e->k != VUPVAL && e->k != VVARGVAR) || hasjumps(e)) luaK_exp2anyreg(fs, e); } @@ -873,7 +1041,7 @@ void luaK_exp2anyregup (FuncState *fs, expdesc *e) { ** or it is a constant. */ void luaK_exp2val (FuncState *fs, expdesc *e) { - if (hasjumps(e)) + if (e->k == VJMP || hasjumps(e)) luaK_exp2anyreg(fs, e); else luaK_dischargevars(fs, e); @@ -888,11 +1056,12 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) { if (!hasjumps(e)) { int info; switch (e->k) { /* move constants to 'k' */ - case VTRUE: info = boolK(fs, 1); break; - case VFALSE: info = boolK(fs, 0); break; + case VTRUE: info = boolT(fs); break; + case VFALSE: info = boolF(fs); break; case VNIL: info = nilK(fs); break; case VKINT: info = luaK_intK(fs, e->u.ival); break; case VKFLT: info = luaK_numberK(fs, e->u.nval); break; + case VKSTR: info = stringK(fs, e->u.strval); break; case VK: info = e->u.info; break; default: return 0; /* not a constant */ } @@ -913,7 +1082,7 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) { ** in the range of R/K indices). ** Returns 1 iff expression is K. */ -int luaK_exp2RK (FuncState *fs, expdesc *e) { +static int exp2RK (FuncState *fs, expdesc *e) { if (luaK_exp2K(fs, e)) return 1; else { /* not a constant in the right range: put it in a register */ @@ -923,10 +1092,10 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) { } -static void codeABRK (FuncState *fs, OpCode o, int a, int b, +static void codeABRK (FuncState *fs, OpCode o, int A, int B, expdesc *ec) { - int k = luaK_exp2RK(fs, ec); - luaK_codeABCk(fs, o, a, b, ec->u.info, k); + int k = exp2RK(fs, ec); + luaK_codeABCk(fs, o, A, B, ec->u.info, k); } @@ -937,7 +1106,7 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { switch (var->k) { case VLOCAL: { freeexp(fs, ex); - exp2reg(fs, ex, var->u.info); /* compute 'ex' into proper place */ + exp2reg(fs, ex, var->u.var.ridx); /* compute 'ex' into proper place */ return; } case VUPVAL: { @@ -957,6 +1126,10 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex); break; } + case VVARGIND: { + needvatab(fs->f); /* function will need a vararg table */ + /* now, assignment is to a regular table */ + } /* FALLTHROUGH */ case VINDEXED: { codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex); break; @@ -967,22 +1140,6 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { } -/* -** Emit SELF instruction (convert expression 'e' into 'e:key(e,'). -*/ -void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { - int ereg; - luaK_exp2anyreg(fs, e); - ereg = e->u.info; /* register where 'e' was placed */ - freeexp(fs, e); - e->u.info = fs->freereg; /* base register for op_self */ - e->k = VNONRELOC; /* self expression has a fixed register */ - luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ - codeABRK(fs, OP_SELF, e->u.info, ereg, key); - freeexp(fs, key); -} - - /* ** Negate condition 'e' (where 'e' is a comparison). */ @@ -1005,13 +1162,13 @@ static int jumponcond (FuncState *fs, expdesc *e, int cond) { Instruction ie = getinstruction(fs, e); if (GET_OPCODE(ie) == OP_NOT) { removelastinstruction(fs); /* remove previous OP_NOT */ - return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); + return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond); } /* else go through */ } discharge2anyreg(fs, e); freeexp(fs, e); - return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond); + return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond); } @@ -1027,7 +1184,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { pc = e->u.info; /* save jump position */ break; } - case VK: case VKFLT: case VKINT: case VTRUE: { + case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: { pc = NO_JUMP; /* always true; do nothing */ break; } @@ -1045,7 +1202,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { /* ** Emit code to go through if 'e' is false, jump otherwise. */ -void luaK_goiffalse (FuncState *fs, expdesc *e) { +static void luaK_goiffalse (FuncState *fs, expdesc *e) { int pc; /* pc of new jump */ luaK_dischargevars(fs, e); switch (e->k) { @@ -1072,13 +1229,12 @@ void luaK_goiffalse (FuncState *fs, expdesc *e) { ** Code 'not e', doing constant folding. */ static void codenot (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); switch (e->k) { case VNIL: case VFALSE: { e->k = VTRUE; /* true == not nil == not false */ break; } - case VK: case VKFLT: case VKINT: case VTRUE: { + case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: { e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */ break; } @@ -1104,17 +1260,17 @@ static void codenot (FuncState *fs, expdesc *e) { /* -** Check whether expression 'e' is a small literal string +** Check whether expression 'e' is a short literal string */ static int isKstr (FuncState *fs, expdesc *e) { - return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B && + return (e->k == VK && !hasjumps(e) && e->u.info <= MAXINDEXRK && ttisshrstring(&fs->f->k[e->u.info])); } /* ** Check whether expression 'e' is a literal integer. */ -int luaK_isKint (expdesc *e) { +static int isKint (expdesc *e) { return (e->k == VKINT && !hasjumps(e)); } @@ -1124,7 +1280,7 @@ int luaK_isKint (expdesc *e) { ** proper range to fit in register C */ static int isCint (expdesc *e) { - return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); + return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); } @@ -1133,21 +1289,24 @@ static int isCint (expdesc *e) { ** proper range to fit in register sC */ static int isSCint (expdesc *e) { - return luaK_isKint(e) && fitsC(e->u.ival); + return isKint(e) && fitsC(e->u.ival); } /* ** Check whether expression 'e' is a literal integer or float in -** proper range to fit in register sC +** proper range to fit in a register (sB or sC). */ -static int isSCnumber (expdesc *e, lua_Integer *i) { +static int isSCnumber (expdesc *e, int *pi, int *isfloat) { + lua_Integer i; if (e->k == VKINT) - *i = e->u.ival; - else if (!(e->k == VKFLT && floatI(e->u.nval, i))) + i = e->u.ival; + else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq)) + *isfloat = 1; + else return 0; /* not a number */ - if (!hasjumps(e) && fitsC(*i)) { - *i += OFFSET_sC; + if (!hasjumps(e) && fitsC(i)) { + *pi = int2sC(cast_int(i)); return 1; } else @@ -1155,6 +1314,40 @@ static int isSCnumber (expdesc *e, lua_Integer *i) { } +/* +** Emit SELF instruction or equivalent: the code will convert +** expression 'e' into 'e.key(e,'. +*/ +void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { + int ereg, base; + luaK_exp2anyreg(fs, e); + ereg = e->u.info; /* register where 'e' (the receiver) was placed */ + freeexp(fs, e); + base = e->u.info = fs->freereg; /* base register for op_self */ + e->k = VNONRELOC; /* self expression has a fixed register */ + luaK_reserveregs(fs, 2); /* method and 'self' produced by op_self */ + lua_assert(key->k == VKSTR); + /* is method name a short string in a valid K index? */ + if (strisshr(key->u.strval) && luaK_exp2K(fs, key)) { + /* can use 'self' opcode */ + luaK_codeABCk(fs, OP_SELF, base, ereg, key->u.info, 0); + } + else { /* cannot use 'self' opcode; use move+gettable */ + luaK_exp2anyreg(fs, key); /* put method name in a register */ + luaK_codeABC(fs, OP_MOVE, base + 1, ereg, 0); /* copy self to base+1 */ + luaK_codeABC(fs, OP_GETTABLE, base, ereg, key->u.info); /* get method */ + } + freeexp(fs, key); +} + + +/* auxiliary function to define indexing expressions */ +static void fillidxk (expdesc *t, int idx, expkind k) { + t->u.ind.idx = cast_byte(idx); + t->k = k; +} + + /* ** Create expression 't[k]'. 't' must have its final result already in a ** register or upvalue. Upvalues can only be indexed by literal strings. @@ -1162,26 +1355,39 @@ static int isSCnumber (expdesc *e, lua_Integer *i) { ** values in registers. */ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { - lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL)); - if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non string? */ + int keystr = -1; + if (k->k == VKSTR) + keystr = str2K(fs, k); + lua_assert(!hasjumps(t) && + (t->k == VLOCAL || t->k == VVARGVAR || + t->k == VNONRELOC || t->k == VUPVAL)); + if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ luaK_exp2anyreg(fs, t); /* put it in a register */ - t->u.ind.t = t->u.info; /* register or upvalue index */ if (t->k == VUPVAL) { - t->u.ind.idx = k->u.info; /* literal string */ - t->k = VINDEXUP; - } - else if (isKstr(fs, k)) { - t->u.ind.idx = k->u.info; /* literal string */ - t->k = VINDEXSTR; + lu_byte temp = cast_byte(t->u.info); /* upvalue index */ + t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */ + lua_assert(isKstr(fs, k)); + fillidxk(t, k->u.info, VINDEXUP); /* literal short string */ } - else if (isCint(k)) { - t->u.ind.idx = cast_int(k->u.ival); /* integer constant in proper range */ - t->k = VINDEXI; + else if (t->k == VVARGVAR) { /* indexing the vararg parameter? */ + int kreg = luaK_exp2anyreg(fs, k); /* put key in some register */ + lu_byte vreg = cast_byte(t->u.var.ridx); /* register with vararg param. */ + lua_assert(vreg == fs->f->numparams); + t->u.ind.t = vreg; /* (avoid a direct assignment; values may overlap) */ + fillidxk(t, kreg, VVARGIND); /* 't' represents 'vararg[k]' */ } else { - t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */ - t->k = VINDEXED; + /* register index of the table */ + t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info); + if (isKstr(fs, k)) + fillidxk(t, k->u.info, VINDEXSTR); /* literal short string */ + else if (isCint(k)) /* int. constant in proper range? */ + fillidxk(t, cast_int(k->u.ival), VINDEXI); + else + fillidxk(t, luaK_exp2anyreg(fs, k), VINDEXED); /* register */ } + t->u.ind.keystr = keystr; /* string index in 'k' */ + t->u.ind.ro = 0; /* by default, not read-only */ } @@ -1195,7 +1401,8 @@ static int validop (int op, TValue *v1, TValue *v2) { case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ lua_Integer i; - return (tointegerns(v1, &i) && tointegerns(v2, &i)); + return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) && + luaV_tointegerns(v2, &i, LUA_FLOORN2I)); } case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ return (nvalue(v2) != 0); @@ -1209,7 +1416,7 @@ static int validop (int op, TValue *v1, TValue *v2) { ** (In this case, 'e1' has the final result.) */ static int constfolding (FuncState *fs, int op, expdesc *e1, - const expdesc *e2) { + const expdesc *e2) { TValue v1, v2, res; if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) return 0; /* non-numeric operands or not safe to fold */ @@ -1229,6 +1436,35 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, } +/* +** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP) +*/ +l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) { + lua_assert(baser <= opr && + ((baser == OPR_ADD && opr <= OPR_SHR) || + (baser == OPR_LT && opr <= OPR_LE))); + return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base)); +} + + +/* +** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP) +*/ +l_sinline OpCode unopr2op (UnOpr opr) { + return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) + + cast_int(OP_UNM)); +} + + +/* +** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM) +*/ +l_sinline TMS binopr2TM (BinOpr opr) { + lua_assert(OPR_ADD <= opr && opr <= OPR_SHR); + return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD)); +} + + /* ** Emit code for unary expressions that "produce values" ** (everything but 'not'). @@ -1248,18 +1484,18 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { ** (everything but logical operators 'and'/'or' and comparison ** operators). ** Expression to produce final result will be encoded in 'e1'. -** Because 'luaK_exp2anyreg' can free registers, its calls must be -** in "stack order" (that is, first on 'e2', which may have more -** recent registers to be released). */ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, - OpCode op, int v2, int k, int line) { + OpCode op, int v2, int flip, int line, + OpCode mmop, TMS event) { int v1 = luaK_exp2anyreg(fs, e1); - int pc = luaK_codeABCk(fs, op, 0, v1, v2, k); + int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0); freeexps(fs, e1, e2); e1->u.info = pc; e1->k = VRELOC; /* all those operations are relocatable */ luaK_fixline(fs, line); + luaK_codeABCk(fs, mmop, v1, v2, cast_int(event), flip); /* metamethod */ + luaK_fixline(fs, line); } @@ -1267,20 +1503,61 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, ** Emit code for binary expressions that "produce values" over ** two registers. */ -static void codebinexpval (FuncState *fs, OpCode op, +static void codebinexpval (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { - int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ - finishbinexpval(fs, e1, e2, op, v2, 0, line); + OpCode op = binopr2op(opr, OPR_ADD, OP_ADD); + int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */ + /* 'e1' must be already in a register or it is a constant */ + lua_assert((VNIL <= e1->k && e1->k <= VKSTR) || + e1->k == VNONRELOC || e1->k == VRELOC); + lua_assert(OP_ADD <= op && op <= OP_SHR); + finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr)); } /* -** Code binary operators ('+', '-', ...) with immediate operands. +** Code binary operators with immediate operands. */ static void codebini (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int k, int line) { - int v2 = cast_int(e2->u.ival) + OFFSET_sC; /* immediate operand */ - finishbinexpval(fs, e1, e2, op, v2, k, line); + expdesc *e1, expdesc *e2, int flip, int line, + TMS event) { + int v2 = int2sC(cast_int(e2->u.ival)); /* immediate operand */ + lua_assert(e2->k == VKINT); + finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event); +} + + +/* +** Code binary operators with K operand. +*/ +static void codebinK (FuncState *fs, BinOpr opr, + expdesc *e1, expdesc *e2, int flip, int line) { + TMS event = binopr2TM(opr); + int v2 = e2->u.info; /* K index */ + OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK); + finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); +} + + +/* Try to code a binary operator negating its second operand. +** For the metamethod, 2nd operand must keep its original value. +*/ +static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2, + OpCode op, int line, TMS event) { + if (!isKint(e2)) + return 0; /* not an integer constant */ + else { + lua_Integer i2 = e2->u.ival; + if (!(fitsC(i2) && fitsC(-i2))) + return 0; /* not in the proper range */ + else { /* operating a small integer constant */ + int v2 = cast_int(i2); + finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event); + /* correct metamethod argument */ + SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2)); + return 1; /* successfully coded */ + } + } } @@ -1289,25 +1566,27 @@ static void swapexps (expdesc *e1, expdesc *e2) { } +/* +** Code binary operators with no constant operand. +*/ +static void codebinNoK (FuncState *fs, BinOpr opr, + expdesc *e1, expdesc *e2, int flip, int line) { + if (flip) + swapexps(e1, e2); /* back to original order */ + codebinexpval(fs, opr, e1, e2, line); /* use standard operators */ +} + + /* ** Code arithmetic operators ('+', '-', ...). If second operand is a -** constant in the proper range, use variant opcodes with immediate -** operands or K operands. +** constant in the proper range, use variant opcodes with K operands. */ -static void codearith (FuncState *fs, OpCode op, +static void codearith (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int flip, int line) { - if (isSCint(e2)) /* immediate operand? */ - codebini(fs, cast(OpCode, op - OP_ADD + OP_ADDI), e1, e2, flip, line); - else if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ - int v2 = e2->u.info; /* K index */ - op = cast(OpCode, op - OP_ADD + OP_ADDK); - finishbinexpval(fs, e1, e2, op, v2, flip, line); - } - else { /* 'e2' is neither an immediate nor a K operand */ - if (flip) - swapexps(e1, e2); /* back to original order */ - codebinexpval(fs, op, e1, e2, line); /* use standard operators */ - } + if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) /* K operand? */ + codebinK(fs, opr, e1, e2, flip, line); + else /* 'e2' is neither an immediate nor a K operand */ + codebinNoK(fs, opr, e1, e2, flip, line); } @@ -1316,88 +1595,66 @@ static void codearith (FuncState *fs, OpCode op, ** numeric constant, change order of operands to try to use an ** immediate or K operator. */ -static void codecommutative (FuncState *fs, OpCode op, +static void codecommutative (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2, int line) { int flip = 0; if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */ swapexps(e1, e2); /* change order */ flip = 1; } - codearith(fs, op, e1, e2, flip, line); + if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */ + codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD); + else + codearith(fs, op, e1, e2, flip, line); } /* -** Code bitwise operations; they are all associative, so the function +** Code bitwise operations; they are all commutative, so the function ** tries to put an integer constant as the 2nd operand (a K operand). */ static void codebitwise (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { - int inv = 0; - int v2; - OpCode op; - if (e1->k == VKINT && luaK_exp2RK(fs, e1)) { + int flip = 0; + if (e1->k == VKINT) { swapexps(e1, e2); /* 'e2' will be the constant operand */ - inv = 1; - } - else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */ - op = cast(OpCode, opr - OPR_BAND + OP_BAND); - codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ - return; - } - v2 = e2->u.info; /* index in K array */ - op = cast(OpCode, opr - OPR_BAND + OP_BANDK); - lua_assert(ttisinteger(&fs->f->k[v2])); - finishbinexpval(fs, e1, e2, op, v2, inv, line); -} - - -/* -** Code shift operators. If second operand is constant, use immediate -** operand (negating it if shift is in the other direction). -*/ -static void codeshift (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int line) { - if (isSCint(e2)) { - int changedir = 0; - if (op == OP_SHL) { - changedir = 1; - e2->u.ival = -(e2->u.ival); - } - codebini(fs, OP_SHRI, e1, e2, changedir, line); + flip = 1; } - else - codebinexpval(fs, op, e1, e2, line); + if (e2->k == VKINT && luaK_exp2K(fs, e2)) /* K operand? */ + codebinK(fs, opr, e1, e2, flip, line); + else /* no constants */ + codebinNoK(fs, opr, e1, e2, flip, line); } /* -** Emit code for order comparisons. -** When the first operand A is an integral value in the proper range, -** change (A < B) to (B > A) and (A <= B) to (B >= A) so that -** it can use an immediate operand. +** Emit code for order comparisons. When using an immediate operand, +** 'isfloat' tells whether the original value was a float. */ -static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { +static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { int r1, r2; - lua_Integer im; - if (isSCnumber(e2, &im)) { + int im; + int isfloat = 0; + OpCode op; + if (isSCnumber(e2, &im, &isfloat)) { /* use immediate operand */ r1 = luaK_exp2anyreg(fs, e1); - r2 = cast_int(im); - op = cast(OpCode, (op - OP_LT) + OP_LTI); + r2 = im; + op = binopr2op(opr, OPR_LT, OP_LTI); } - else if (isSCnumber(e1, &im)) { + else if (isSCnumber(e1, &im, &isfloat)) { /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ r1 = luaK_exp2anyreg(fs, e2); - r2 = cast_int(im); - op = (op == OP_LT) ? OP_GTI : OP_GEI; + r2 = im; + op = binopr2op(opr, OPR_LT, OP_GTI); } else { /* regular case, compare two registers */ r1 = luaK_exp2anyreg(fs, e1); r2 = luaK_exp2anyreg(fs, e2); + op = binopr2op(opr, OPR_LT, OP_LT); } freeexps(fs, e1, e2); - e1->u.info = condjump(fs, op, r1, r2, 1); + e1->u.info = condjump(fs, op, r1, r2, isfloat, 1); e1->k = VJMP; } @@ -1408,18 +1665,19 @@ static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { */ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { int r1, r2; - lua_Integer im; + int im; + int isfloat = 0; /* not needed here, but kept for symmetry */ OpCode op; if (e1->k != VNONRELOC) { lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT); swapexps(e1, e2); } - r1 = luaK_exp2anyreg(fs, e1); /* 1nd expression must be in register */ - if (isSCnumber(e2, &im)) { + r1 = luaK_exp2anyreg(fs, e1); /* 1st expression must be in register */ + if (isSCnumber(e2, &im, &isfloat)) { op = OP_EQI; - r2 = cast_int(im); /* immediate operand */ + r2 = im; /* immediate operand */ } - else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */ + else if (exp2RK(fs, e2)) { /* 2nd expression is constant? */ op = OP_EQK; r2 = e2->u.info; /* constant index */ } @@ -1428,7 +1686,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { r2 = luaK_exp2anyreg(fs, e2); } freeexps(fs, e1, e2); - e1->u.info = condjump(fs, op, r1, r2, (opr == OPR_EQ)); + e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ)); e1->k = VJMP; } @@ -1436,15 +1694,16 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { /* ** Apply prefix operation 'op' to expression 'e'. */ -void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { +void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) { static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; - switch (op) { + luaK_dischargevars(fs, e); + switch (opr) { case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ - if (constfolding(fs, op + LUA_OPUNM, e, &ef)) + if (constfolding(fs, cast_int(opr + LUA_OPUNM), e, &ef)) break; - /* FALLTHROUGH */ + /* else */ /* FALLTHROUGH */ case OPR_LEN: - codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line); + codeunexpval(fs, unopr2op(opr), e, line); break; case OPR_NOT: codenot(fs, e); break; default: lua_assert(0); @@ -1457,6 +1716,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { ** 2nd operand. */ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { + luaK_dischargevars(fs, v); switch (op) { case OPR_AND: { luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */ @@ -1477,19 +1737,20 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { case OPR_SHL: case OPR_SHR: { if (!tonumeral(v, NULL)) luaK_exp2anyreg(fs, v); - /* else keep numeral, which may be folded with 2nd operand */ + /* else keep numeral, which may be folded or used as an immediate + operand */ break; } case OPR_EQ: case OPR_NE: { if (!tonumeral(v, NULL)) - luaK_exp2RK(fs, v); + exp2RK(fs, v); /* else keep numeral, which may be an immediate operand */ break; } case OPR_LT: case OPR_LE: case OPR_GT: case OPR_GE: { - lua_Integer dummy; - if (!isSCnumber(v, &dummy)) + int dummy, dummy2; + if (!isSCnumber(v, &dummy, &dummy2)) luaK_exp2anyreg(fs, v); /* else keep numeral, which may be an immediate operand */ break; @@ -1525,17 +1786,18 @@ static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) { */ void luaK_posfix (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { + luaK_dischargevars(fs, e2); + if (foldbinop(opr) && constfolding(fs, cast_int(opr + LUA_OPADD), e1, e2)) + return; /* done by folding */ switch (opr) { case OPR_AND: { - lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */ - luaK_dischargevars(fs, e2); + lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */ luaK_concat(fs, &e2->f, e1->f); *e1 = *e2; break; } case OPR_OR: { - lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */ - luaK_dischargevars(fs, e2); + lua_assert(e1->f == NO_JUMP); /* list closed by 'luaK_infix' */ luaK_concat(fs, &e2->t, e1->t); *e1 = *e2; break; @@ -1546,51 +1808,52 @@ void luaK_posfix (FuncState *fs, BinOpr opr, break; } case OPR_ADD: case OPR_MUL: { - if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) - codecommutative(fs, cast(OpCode, opr + OP_ADD), e1, e2, line); + codecommutative(fs, opr, e1, e2, line); break; } - case OPR_SUB: case OPR_DIV: - case OPR_IDIV: case OPR_MOD: case OPR_POW: { - if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) - codearith(fs, cast(OpCode, opr + OP_ADD), e1, e2, 0, line); + case OPR_SUB: { + if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB)) + break; /* coded as (r1 + -I) */ + /* ELSE */ + } /* FALLTHROUGH */ + case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: { + codearith(fs, opr, e1, e2, 0, line); break; } case OPR_BAND: case OPR_BOR: case OPR_BXOR: { - if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) - codebitwise(fs, opr, e1, e2, line); + codebitwise(fs, opr, e1, e2, line); break; } case OPR_SHL: { - if (!constfolding(fs, LUA_OPSHL, e1, e2)) { - if (isSCint(e1)) { - swapexps(e1, e2); - codebini(fs, OP_SHLI, e1, e2, 1, line); - } - else - codeshift(fs, OP_SHL, e1, e2, line); + if (isSCint(e1)) { + swapexps(e1, e2); + codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); /* I << r2 */ } + else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) { + /* coded as (r1 >> -I) */; + } + else /* regular case (two registers) */ + codebinexpval(fs, opr, e1, e2, line); break; } case OPR_SHR: { - if (!constfolding(fs, LUA_OPSHR, e1, e2)) - codeshift(fs, OP_SHR, e1, e2, line); + if (isSCint(e2)) + codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */ + else /* regular case (two registers) */ + codebinexpval(fs, opr, e1, e2, line); break; } case OPR_EQ: case OPR_NE: { codeeq(fs, opr, e1, e2); break; } - case OPR_LT: case OPR_LE: { - OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); - codeorder(fs, op, e1, e2); - break; - } case OPR_GT: case OPR_GE: { /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */ - OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); swapexps(e1, e2); - codeorder(fs, op, e1, e2); + opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT); + } /* FALLTHROUGH */ + case OPR_LT: case OPR_LE: { + codeorder(fs, opr, e1, e2); break; } default: lua_assert(0); @@ -1608,6 +1871,17 @@ void luaK_fixline (FuncState *fs, int line) { } +void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) { + Instruction *inst = &fs->f->code[pc]; + int extra = asize / (MAXARG_vC + 1); /* higher bits of array size */ + int rc = asize % (MAXARG_vC + 1); /* lower bits of array size */ + int k = (extra > 0); /* true iff needs extra argument */ + hsize = (hsize != 0) ? luaO_ceillog2(cast_uint(hsize)) + 1 : 0; + *inst = CREATE_vABCk(OP_NEWTABLE, ra, hsize, rc, k); + *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra); +} + + /* ** Emit a SETLIST instruction. ** 'base' is register that keeps table; @@ -1616,18 +1890,18 @@ void luaK_fixline (FuncState *fs, int line) { ** table (or LUA_MULTRET to add up to stack top). */ void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { - int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; - int b = (tostore == LUA_MULTRET) ? 0 : tostore; - lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH); - if (c <= MAXARG_C) - luaK_codeABC(fs, OP_SETLIST, base, b, c); - else if (c <= MAXARG_Ax) { - luaK_codeABC(fs, OP_SETLIST, base, b, 0); - codeextraarg(fs, c); + lua_assert(tostore != 0); + if (tostore == LUA_MULTRET) + tostore = 0; + if (nelems <= MAXARG_vC) + luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 0); + else { + int extra = nelems / (MAXARG_vC + 1); + nelems %= (MAXARG_vC + 1); + luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 1); + codeextraarg(fs, extra); } - else - luaX_syntaxerror(fs->ls, "constructor too long"); - fs->freereg = base + 1; /* free registers with list values */ + fs->freereg = cast_byte(base + 1); /* free registers with list values */ } @@ -1640,8 +1914,8 @@ static int finaltarget (Instruction *code, int i) { Instruction pc = code[i]; if (GET_OPCODE(pc) != OP_JMP) break; - else - i += GETARG_sJ(pc) + 1; + else + i += GETARG_sJ(pc) + 1; } return i; } @@ -1651,29 +1925,44 @@ static int finaltarget (Instruction *code, int i) { ** Do a final pass over the code of a function, doing small peephole ** optimizations and adjustments. */ +#include "lopnames.h" void luaK_finish (FuncState *fs) { int i; Proto *p = fs->f; + if (p->flag & PF_VATAB) /* will it use a vararg table? */ + p->flag &= cast_byte(~PF_VAHID); /* then it will not use hidden args. */ for (i = 0; i < fs->pc; i++) { Instruction *pc = &p->code[i]; - lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc)); + /* avoid "not used" warnings when assert is off (for 'onelua.c') */ + (void)luaP_isOT; (void)luaP_isIT; + lua_assert(i == 0 || luaP_isOT(*(pc - 1)) == luaP_isIT(*pc)); switch (GET_OPCODE(*pc)) { case OP_RETURN0: case OP_RETURN1: { - if (!(fs->needclose || p->is_vararg)) + if (!(fs->needclose || (p->flag & PF_VAHID))) break; /* no extra work */ /* else use OP_RETURN to do the extra work */ SET_OPCODE(*pc, OP_RETURN); } /* FALLTHROUGH */ case OP_RETURN: case OP_TAILCALL: { - if (fs->needclose || p->is_vararg) { - SETARG_C(*pc, p->is_vararg ? p->numparams + 1 : 0); - SETARG_k(*pc, 1); /* signal that there is extra work */ - } + if (fs->needclose) + SETARG_k(*pc, 1); /* signal that it needs to close */ + if (p->flag & PF_VAHID) /* does it use hidden arguments? */ + SETARG_C(*pc, p->numparams + 1); /* signal that */ + break; + } + case OP_GETVARG: { + if (p->flag & PF_VATAB) /* function has a vararg table? */ + SET_OPCODE(*pc, OP_GETTABLE); /* must get vararg there */ + break; + } + case OP_VARARG: { + if (p->flag & PF_VATAB) /* function has a vararg table? */ + SETARG_k(*pc, 1); /* must get vararg there */ break; } - case OP_JMP: { + case OP_JMP: { /* to optimize jumps to jumps */ int target = finaltarget(p->code, i); - fixjump(fs, i, target); + fixjump(fs, i, target); /* jump directly to final target */ break; } default: break; diff --git a/lcode.h b/lcode.h index 0758f88dee..09e5c802b0 100644 --- a/lcode.h +++ b/lcode.h @@ -24,19 +24,27 @@ ** grep "ORDER OPR" if you change these enums (ORDER OP) */ typedef enum BinOpr { + /* arithmetic operators */ OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, - OPR_DIV, - OPR_IDIV, + OPR_DIV, OPR_IDIV, + /* bitwise operators */ OPR_BAND, OPR_BOR, OPR_BXOR, OPR_SHL, OPR_SHR, + /* string operator */ OPR_CONCAT, + /* comparison operators */ OPR_EQ, OPR_LT, OPR_LE, OPR_NE, OPR_GT, OPR_GE, + /* logical operators */ OPR_AND, OPR_OR, OPR_NOBINOPR } BinOpr; +/* true if operation is foldable (that is, it is arithmetic or bitwise) */ +#define foldbinop(op) ((op) <= OPR_SHR) + + #define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0) @@ -51,27 +59,29 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) -LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); -LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); -LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, - int B, int C, int k); -LUAI_FUNC int luaK_isKint (expdesc *e); +LUAI_FUNC int luaK_code (FuncState *fs, Instruction i); +LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bx); +LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, + int k); +LUAI_FUNC int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, + int k); +LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); +LUAI_FUNC void luaK_codecheckglobal (FuncState *fs, expdesc *var, int k, + int line); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); -LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n); +LUAI_FUNC void luaK_vapar2local (FuncState *fs, expdesc *var); LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); @@ -85,9 +95,11 @@ LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2, int line); +LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc, + int ra, int asize, int hsize); LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); LUAI_FUNC void luaK_finish (FuncState *fs); -LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); +LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *fmt, ...); #endif diff --git a/lcorolib.c b/lcorolib.c index cdb5fedc93..eb30bf4da5 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -16,6 +16,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" static lua_State *getco (lua_State *L) { @@ -25,20 +26,20 @@ static lua_State *getco (lua_State *L) { } +/* +** Resumes a coroutine. Returns the number of results for non-error +** cases or -1 for errors. +*/ static int auxresume (lua_State *L, lua_State *co, int narg) { int status, nres; - if (!lua_checkstack(co, narg)) { + if (l_unlikely(!lua_checkstack(co, narg))) { lua_pushliteral(L, "too many arguments to resume"); return -1; /* error flag */ } - if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { - lua_pushliteral(L, "cannot resume dead coroutine"); - return -1; /* error flag */ - } lua_xmove(L, co, narg); status = lua_resume(co, L, narg, &nres); - if (status == LUA_OK || status == LUA_YIELD) { - if (!lua_checkstack(L, nres + 1)) { + if (l_likely(status == LUA_OK || status == LUA_YIELD)) { + if (l_unlikely(!lua_checkstack(L, nres + 1))) { lua_pop(co, nres); /* remove results anyway */ lua_pushliteral(L, "too many results to resume"); return -1; /* error flag */ @@ -57,7 +58,7 @@ static int luaB_coresume (lua_State *L) { lua_State *co = getco(L); int r; r = auxresume(L, co, lua_gettop(L) - 1); - if (r < 0) { + if (l_unlikely(r < 0)) { lua_pushboolean(L, 0); lua_insert(L, -2); return 2; /* return false + error message */ @@ -73,9 +74,16 @@ static int luaB_coresume (lua_State *L) { static int luaB_auxwrap (lua_State *L) { lua_State *co = lua_tothread(L, lua_upvalueindex(1)); int r = auxresume(L, co, lua_gettop(L)); - if (r < 0) { - if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ - luaL_where(L, 1); /* add extra info */ + if (l_unlikely(r < 0)) { /* error? */ + int stat = lua_status(co); + if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ + stat = lua_closethread(co, L); /* close its tbc variables */ + lua_assert(stat != LUA_OK); + lua_xmove(co, L, 1); /* move error message to the caller */ + } + if (stat != LUA_ERRMEM && /* not a memory error and ... */ + lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */ + luaL_where(L, 1); /* add extra info, if available */ lua_insert(L, -2); lua_concat(L, 2); } @@ -113,7 +121,8 @@ static int luaB_yield (lua_State *L) { #define COS_NORM 3 -static const char *statname[] = {"running", "dead", "suspended", "normal"}; +static const char *const statname[] = + {"running", "dead", "suspended", "normal"}; static int auxstatus (lua_State *L, lua_State *co) { @@ -145,8 +154,14 @@ static int luaB_costatus (lua_State *L) { } +static lua_State *getoptco (lua_State *L) { + return (lua_isnone(L, 1) ? L : getco(L)); +} + + static int luaB_yieldable (lua_State *L) { - lua_pushboolean(L, lua_isyieldable(L)); + lua_State *co = getoptco(L); + lua_pushboolean(L, lua_isyieldable(co)); return 1; } @@ -158,24 +173,33 @@ static int luaB_corunning (lua_State *L) { } -static int luaB_kill (lua_State *L) { - lua_State *co = getco(L); +static int luaB_close (lua_State *L) { + lua_State *co = getoptco(L); int status = auxstatus(L, co); switch (status) { case COS_DEAD: case COS_YIELD: { - status = lua_resetthread(co); + status = lua_closethread(co, L); if (status == LUA_OK) { lua_pushboolean(L, 1); return 1; } else { lua_pushboolean(L, 0); - lua_xmove(co, L, 1); /* copy error message */ + lua_xmove(co, L, 1); /* move error message */ return 2; } } - default: /* normal or running coroutine */ - return luaL_error(L, "cannot kill a %s coroutine", statname[status]); + case COS_NORM: + return luaL_error(L, "cannot close a %s coroutine", statname[status]); + case COS_RUN: + lua_geti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); /* get main */ + if (lua_tothread(L, -1) == co) + return luaL_error(L, "cannot close main thread"); + lua_closethread(co, L); /* close itself */ + /* previous call does not return *//* FALLTHROUGH */ + default: + lua_assert(0); + return 0; } } @@ -188,7 +212,7 @@ static const luaL_Reg co_funcs[] = { {"wrap", luaB_cowrap}, {"yield", luaB_yield}, {"isyieldable", luaB_yieldable}, - {"kill", luaB_kill}, + {"close", luaB_close}, {NULL, NULL} }; diff --git a/lctype.c b/lctype.c index 4eaad167ed..b1a43e44b0 100644 --- a/lctype.c +++ b/lctype.c @@ -16,6 +16,15 @@ #include + +#if defined (LUA_UCID) /* accept UniCode IDentifiers? */ +/* consider all non-ASCII codepoints to be alphabetic */ +#define NONA 0x01 +#else +#define NONA 0x00 /* default */ +#endif + + LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 0x00, /* EOZ */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ @@ -34,22 +43,22 @@ LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 8. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 9. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* a. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* b. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, /* c. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* d. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* e. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, 0x00, 0x00, 0x00, /* f. */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; #endif /* } */ diff --git a/lctype.h b/lctype.h index cbff4d7e19..864e190188 100644 --- a/lctype.h +++ b/lctype.h @@ -13,7 +13,7 @@ /* ** WARNING: the functions defined here do not necessarily correspond ** to the similar functions in the standard C ctype.h. They are -** optimized for the specific needs of Lua +** optimized for the specific needs of Lua. */ #if !defined(LUA_USE_CTYPE) @@ -61,13 +61,19 @@ #define lisprint(c) testprop(c, MASK(PRINTBIT)) #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) + /* -** this 'ltolower' only works for alphabetic characters +** In ASCII, this 'ltolower' is correct for alphabetic characters and +** for '.'. That is enough for Lua needs. ('check_exp' ensures that +** the character either is an upper-case letter or is unchanged by +** the transformation, which holds for lower-case letters and '.'.) */ -#define ltolower(c) ((c) | ('A' ^ 'a')) +#define ltolower(c) \ + check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \ + (c) | ('A' ^ 'a')) -/* two more entries for 0 and -1 (EOZ) */ +/* one entry for each character and for -1 (EOZ) */ LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];) diff --git a/ldblib.c b/ldblib.c index ada35250c3..c7b74812e8 100644 --- a/ldblib.c +++ b/ldblib.c @@ -18,13 +18,14 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* -** The hook table at registry[&HOOKKEY] maps threads to their current -** hook function. (We only need the unique address of 'HOOKKEY'.) +** The hook table at registry[HOOKKEY] maps threads to their current +** hook function. */ -static const int HOOKKEY = 0; +static const char *const HOOKKEY = "_HOOKKEY"; /* @@ -33,7 +34,7 @@ static const int HOOKKEY = 0; ** checked. */ static void checkstack (lua_State *L, lua_State *L1, int n) { - if (L != L1 && !lua_checkstack(L1, n)) + if (l_unlikely(L != L1 && !lua_checkstack(L1, n))) luaL_error(L, "stack overflow"); } @@ -65,7 +66,7 @@ static int db_setmetatable (lua_State *L) { static int db_getuservalue (lua_State *L) { int n = (int)luaL_optinteger(L, 2, 1); if (lua_type(L, 1) != LUA_TUSERDATA) - lua_pushnil(L); + luaL_pushfail(L); else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) { lua_pushboolean(L, 1); return 2; @@ -80,7 +81,7 @@ static int db_setuservalue (lua_State *L) { luaL_checkany(L, 2); lua_settop(L, 2); if (!lua_setiuservalue(L, 1, n)) - lua_pushnil(L); + luaL_pushfail(L); return 1; } @@ -152,6 +153,7 @@ static int db_getinfo (lua_State *L) { lua_State *L1 = getthread(L, &arg); const char *options = luaL_optstring(L, arg+2, "flnSrtu"); checkstack(L, L1, 3); + luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'"); if (lua_isfunction(L, arg + 1)) { /* info about a function? */ options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ @@ -159,7 +161,7 @@ static int db_getinfo (lua_State *L) { } else { /* stack level */ if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) { - lua_pushnil(L); /* level out of range */ + luaL_pushfail(L); /* level out of range */ return 1; } } @@ -167,7 +169,8 @@ static int db_getinfo (lua_State *L) { return luaL_argerror(L, arg+2, "invalid option"); lua_newtable(L); /* table to collect results */ if (strchr(options, 'S')) { - settabss(L, "source", ar.source); + lua_pushlstring(L, ar.source, ar.srclen); + lua_setfield(L, -2, "source"); settabss(L, "short_src", ar.short_src); settabsi(L, "linedefined", ar.linedefined); settabsi(L, "lastlinedefined", ar.lastlinedefined); @@ -188,8 +191,10 @@ static int db_getinfo (lua_State *L) { settabsi(L, "ftransfer", ar.ftransfer); settabsi(L, "ntransfer", ar.ntransfer); } - if (strchr(options, 't')) + if (strchr(options, 't')) { settabsb(L, "istailcall", ar.istailcall); + settabsi(L, "extraargs", ar.extraargs); + } if (strchr(options, 'L')) treatstackoption(L, L1, "activelines"); if (strchr(options, 'f')) @@ -201,8 +206,6 @@ static int db_getinfo (lua_State *L) { static int db_getlocal (lua_State *L) { int arg; lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - const char *name; int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */ if (lua_isfunction(L, arg + 1)) { /* function argument? */ lua_pushvalue(L, arg + 1); /* push function */ @@ -210,8 +213,10 @@ static int db_getlocal (lua_State *L) { return 1; /* return only name (there is no value) */ } else { /* stack-level argument */ + lua_Debug ar; + const char *name; int level = (int)luaL_checkinteger(L, arg + 1); - if (!lua_getstack(L1, level, &ar)) /* out of range? */ + if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */ return luaL_argerror(L, arg+1, "level out of range"); checkstack(L, L1, 1); name = lua_getlocal(L1, &ar, nvar); @@ -222,7 +227,7 @@ static int db_getlocal (lua_State *L) { return 2; } else { - lua_pushnil(L); /* no name (nor value) */ + luaL_pushfail(L); /* no name (nor value) */ return 1; } } @@ -236,7 +241,7 @@ static int db_setlocal (lua_State *L) { lua_Debug ar; int level = (int)luaL_checkinteger(L, arg + 1); int nvar = (int)luaL_checkinteger(L, arg + 2); - if (!lua_getstack(L1, level, &ar)) /* out of range? */ + if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */ return luaL_argerror(L, arg+1, "level out of range"); luaL_checkany(L, arg+3); lua_settop(L, arg+3); @@ -280,25 +285,33 @@ static int db_setupvalue (lua_State *L) { ** Check whether a given upvalue from a given closure exists and ** returns its index */ -static int checkupval (lua_State *L, int argf, int argnup) { +static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) { + void *id; int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */ luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */ - luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup, - "invalid upvalue index"); - return nup; + id = lua_upvalueid(L, argf, nup); + if (pnup) { + luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index"); + *pnup = nup; + } + return id; } static int db_upvalueid (lua_State *L) { - int n = checkupval(L, 1, 2); - lua_pushlightuserdata(L, lua_upvalueid(L, 1, n)); + void *id = checkupval(L, 1, 2, NULL); + if (id != NULL) + lua_pushlightuserdata(L, id); + else + luaL_pushfail(L); return 1; } static int db_upvaluejoin (lua_State *L) { - int n1 = checkupval(L, 1, 2); - int n2 = checkupval(L, 3, 4); + int n1, n2; + checkupval(L, 1, 2, &n1); + checkupval(L, 3, 4, &n2); luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected"); luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected"); lua_upvaluejoin(L, 1, n1, 3, n2); @@ -313,7 +326,7 @@ static int db_upvaluejoin (lua_State *L) { static void hookf (lua_State *L, lua_Debug *ar) { static const char *const hooknames[] = {"call", "return", "line", "count", "tail call"}; - lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); + lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY); lua_pushthread(L); if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ @@ -366,14 +379,12 @@ static int db_sethook (lua_State *L) { count = (int)luaL_optinteger(L, arg + 3, 0); func = hookf; mask = makemask(smask, count); } - if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) { - lua_createtable(L, 0, 2); /* create a hook table */ - lua_pushvalue(L, -1); - lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */ - lua_pushstring(L, "k"); + if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) { + /* table just created; initialize it */ + lua_pushliteral(L, "k"); lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ lua_pushvalue(L, -1); - lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ + lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */ } checkstack(L, L1, 1); lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ @@ -390,12 +401,14 @@ static int db_gethook (lua_State *L) { char buff[5]; int mask = lua_gethookmask(L1); lua_Hook hook = lua_gethook(L1); - if (hook == NULL) /* no hook? */ - lua_pushnil(L); + if (hook == NULL) { /* no hook? */ + luaL_pushfail(L); + return 1; + } else if (hook != hookf) /* external hook? */ lua_pushliteral(L, "external hook"); else { /* hook table must exist */ - lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); + lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY); checkstack(L, L1, 1); lua_pushthread(L1); lua_xmove(L1, L, 1); lua_rawget(L, -2); /* 1st result = hooktable[L1] */ @@ -411,12 +424,12 @@ static int db_debug (lua_State *L) { for (;;) { char buffer[250]; lua_writestringerror("%s", "lua_debug> "); - if (fgets(buffer, sizeof(buffer), stdin) == 0 || + if (fgets(buffer, sizeof(buffer), stdin) == NULL || strcmp(buffer, "cont\n") == 0) return 0; if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || lua_pcall(L, 0, 0, 0)) - lua_writestringerror("%s\n", lua_tostring(L, -1)); + lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL)); lua_settop(L, 0); /* remove eventual returns */ } } diff --git a/ldebug.c b/ldebug.c index bd471e0c08..8df5f5f28b 100644 --- a/ldebug.c +++ b/ldebug.c @@ -31,15 +31,13 @@ -#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) +#define LuaClosure(f) ((f) != NULL && (f)->c.tt == LUA_VLCL) +static const char strlocal[] = "local"; +static const char strupval[] = "upvalue"; -/* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue(s2v((ci)->func))) - - -static const char *funcnamefromcode (lua_State *L, CallInfo *ci, - const char **name); +static const char *funcnamefromcall (lua_State *L, CallInfo *ci, + const char **name); static int currentpc (CallInfo *ci) { @@ -50,10 +48,16 @@ static int currentpc (CallInfo *ci) { /* ** Get a "base line" to find the line corresponding to an instruction. -** For that, search the array of absolute line info for the largest saved -** instruction smaller or equal to the wanted instruction. A special -** case is when there is no absolute info or the instruction is before -** the first absolute one. +** Base lines are regularly placed at MAXIWTHABS intervals, so usually +** an integer division gets the right place. When the source file has +** large sequences of empty/comment lines, it may need extra entries, +** so the original estimate needs a correction. +** If the original estimate is -1, the initial 'if' ensures that the +** 'while' will run at least once. +** The assertion that the estimate is a lower bound for the correct base +** is valid as long as the debug info has been generated with the same +** value for MAXIWTHABS or smaller. (Previous releases use a little +** smaller value.) */ static int getbaseline (const Proto *f, int pc, int *basepc) { if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) { @@ -61,20 +65,12 @@ static int getbaseline (const Proto *f, int pc, int *basepc) { return f->linedefined; } else { - unsigned int i; - if (pc >= f->abslineinfo[f->sizeabslineinfo - 1].pc) - i = f->sizeabslineinfo - 1; /* instruction is after last saved one */ - else { /* binary search */ - unsigned int j = f->sizeabslineinfo - 1; /* pc < anchorlines[j] */ - i = 0; /* abslineinfo[i] <= pc */ - while (i < j - 1) { - unsigned int m = (j + i) / 2; - if (pc >= f->abslineinfo[m].pc) - i = m; - else - j = m; - } - } + int i = pc / MAXIWTHABS - 1; /* get an estimate */ + /* estimate must be a lower bound of the correct base */ + lua_assert(i < 0 || + (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc)); + while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc) + i++; /* low estimate; adjust it */ *basepc = f->abslineinfo[i].pc; return f->abslineinfo[i].line; } @@ -101,19 +97,21 @@ int luaG_getfuncline (const Proto *f, int pc) { } -static int currentline (CallInfo *ci) { +static int getcurrentline (CallInfo *ci) { return luaG_getfuncline(ci_func(ci)->p, currentpc(ci)); } /* -** This function can be called asynchronously (e.g. during a signal), -** under "reasonable" assumptions. A new 'ci' is completely linked -** in the list before it becomes part of the "active" list, and -** we assume that pointers are atomic (see comment in next function). -** (If we traverse one more item, there is no problem. If we traverse -** one less item, the worst that can happen is that the signal will -** not interrupt the script.) +** Set 'trap' for all active Lua frames. +** This function can be called during a signal, under "reasonable" +** assumptions. A new 'ci' is completely linked in the list before it +** becomes part of the "active" list, and we assume that pointers are +** atomic; see comment in next function. +** (A compiler doing interprocedural optimizations could, theoretically, +** reorder memory writes in such a way that the list could be +** temporarily broken while inserting a new element. We simply assume it +** has no good reasons to do that.) */ static void settraps (CallInfo *ci) { for (; ci != NULL; ci = ci->previous) @@ -123,22 +121,20 @@ static void settraps (CallInfo *ci) { /* -** This function can be called asynchronously (e.g. during a signal), -** under "reasonable" assumptions. -** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by -** 'resethookcount') are for debug only, and it is no problem if they -** get arbitrary values (causes at most one wrong hook call). 'hookmask' -** is an atomic value. We assume that pointers are atomic too (e.g., gcc -** ensures that for all platforms where it runs). Moreover, 'hook' is -** always checked before being called (see 'luaD_hook'). +** This function can be called during a signal, under "reasonable" +** assumptions. +** Fields 'basehookcount' and 'hookcount' (set by 'resethookcount') +** are for debug only, and it is no problem if they get arbitrary +** values (causes at most one wrong hook call). 'hookmask' is an atomic +** value. We assume that pointers are atomic too (e.g., gcc ensures that +** for all platforms where it runs). Moreover, 'hook' is always checked +** before being called (see 'luaD_hook'). */ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { if (func == NULL || mask == 0) { /* turn off hooks? */ mask = 0; func = NULL; } - if (isLua(L->ci)) - L->oldpc = L->ci->u.l.savedpc; L->hook = func; L->basehookcount = count; resethookcount(L); @@ -188,10 +184,10 @@ static const char *upvalname (const Proto *p, int uv) { static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - if (clLvalue(s2v(ci->func))->p->is_vararg) { + if (clLvalue(s2v(ci->func.p))->p->flag & PF_VAHID) { int nextra = ci->u.l.nextraargs; - if (n <= nextra) { - *pos = ci->func - nextra + (n - 1); + if (n >= -nextra) { /* 'n' is negative */ + *pos = ci->func.p - nextra - (n + 1); return "(vararg)"; /* generic name for any vararg */ } } @@ -200,16 +196,16 @@ static const char *findvararg (CallInfo *ci, int n, StkId *pos) { const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { - StkId base = ci->func + 1; + StkId base = ci->func.p + 1; const char *name = NULL; if (isLua(ci)) { if (n < 0) /* access to vararg values? */ - return findvararg(ci, -n, pos); + return findvararg(ci, n, pos); else name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); } if (name == NULL) { /* no 'standard' name? */ - StkId limit = (ci == L->ci) ? L->top : ci->next->func; + StkId limit = (ci == L->ci) ? L->top.p : ci->next->func.p; if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */ /* generic name for any valid slot */ name = isLua(ci) ? "(temporary)" : "(C temporary)"; @@ -227,16 +223,16 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { const char *name; lua_lock(L); if (ar == NULL) { /* information about non-active function? */ - if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */ + if (!isLfunction(s2v(L->top.p - 1))) /* not a Lua function? */ name = NULL; else /* consider live variables at function start (parameters) */ - name = luaF_getlocalname(clLvalue(s2v(L->top - 1))->p, n, 0); + name = luaF_getlocalname(clLvalue(s2v(L->top.p - 1))->p, n, 0); } else { /* active function; get information through 'ar' */ StkId pos = NULL; /* to avoid warnings */ name = luaG_findlocal(L, ar->i_ci, n, &pos); if (name) { - setobjs2s(L, L->top, pos); + setobjs2s(L, L->top.p, pos); api_incr_top(L); } } @@ -251,8 +247,9 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { lua_lock(L); name = luaG_findlocal(L, ar->i_ci, n, &pos); if (name) { - setobjs2s(L, pos, L->top - 1); - L->top--; /* pop value */ + api_checkpop(L, 1); + setobjs2s(L, pos, L->top.p - 1); + L->top.p--; /* pop value */ } lua_unlock(L); return name; @@ -260,20 +257,27 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { static void funcinfo (lua_Debug *ar, Closure *cl) { - if (noLuaClosure(cl)) { + if (!LuaClosure(cl)) { ar->source = "=[C]"; + ar->srclen = LL("=[C]"); ar->linedefined = -1; ar->lastlinedefined = -1; ar->what = "C"; } else { const Proto *p = cl->l.p; - ar->source = p->source ? getstr(p->source) : "=?"; + if (p->source) { + ar->source = getlstr(p->source, ar->srclen); + } + else { + ar->source = "=?"; + ar->srclen = LL("=?"); + } ar->linedefined = p->linedefined; ar->lastlinedefined = p->lastlinedefined; ar->what = (ar->linedefined == 0) ? "main" : "Lua"; } - luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); + luaO_chunkid(ar->short_src, ar->source, ar->srclen); } @@ -286,37 +290,40 @@ static int nextline (const Proto *p, int currentline, int pc) { static void collectvalidlines (lua_State *L, Closure *f) { - if (noLuaClosure(f)) { - setnilvalue(s2v(L->top)); + if (!LuaClosure(f)) { + setnilvalue(s2v(L->top.p)); api_incr_top(L); } else { - int i; - TValue v; const Proto *p = f->l.p; int currentline = p->linedefined; Table *t = luaH_new(L); /* new table to store active lines */ - sethvalue2s(L, L->top, t); /* push it on stack */ + sethvalue2s(L, L->top.p, t); /* push it on stack */ api_incr_top(L); - setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ - for (i = 0; i < p->sizelineinfo; i++) { /* for all lines with code */ - currentline = nextline(p, currentline, i); - luaH_setint(L, t, currentline, &v); /* table[line] = true */ + if (p->lineinfo != NULL) { /* proto with debug information? */ + int i; + TValue v; + setbtvalue(&v); /* boolean 'true' to be the value of all indices */ + if (!(isvararg(p))) /* regular function? */ + i = 0; /* consider all instructions */ + else { /* vararg function */ + lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP); + currentline = nextline(p, currentline, 0); + i = 1; /* skip first instruction (OP_VARARGPREP) */ + } + for (; i < p->sizelineinfo; i++) { /* for each instruction */ + currentline = nextline(p, currentline, i); /* get its line */ + luaH_setint(L, t, currentline, &v); /* table[line] = true */ + } } } } static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { - if (ci == NULL) /* no 'ci'? */ - return NULL; /* no info */ - else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */ - *name = "__gc"; - return "metamethod"; /* report it as such */ - } - /* calling function is a known Lua function? */ - else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) - return funcnamefromcode(L, ci->previous, name); + /* calling function is a known function? */ + if (ci != NULL && !(ci->callstatus & CIST_TAIL)) + return funcnamefromcall(L, ci->previous, name); else return NULL; /* no way to find a name */ } @@ -331,23 +338,31 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 'l': { - ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1; + ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1; break; } case 'u': { ar->nups = (f == NULL) ? 0 : f->c.nupvalues; - if (noLuaClosure(f)) { + if (!LuaClosure(f)) { ar->isvararg = 1; ar->nparams = 0; } else { - ar->isvararg = f->l.p->is_vararg; + ar->isvararg = (isvararg(f->l.p)) ? 1 : 0; ar->nparams = f->l.p->numparams; } break; } case 't': { - ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0; + if (ci != NULL) { + ar->istailcall = !!(ci->callstatus & CIST_TAIL); + ar->extraargs = + cast_uchar((ci->callstatus & MAX_CCMT) >> CIST_CCMT); + } + else { + ar->istailcall = 0; + ar->extraargs = 0; + } break; } case 'n': { @@ -359,12 +374,13 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 'r': { - if (ci == NULL || !(ci->callstatus & CIST_TRAN)) + if (ci == NULL || !(ci->callstatus & CIST_HOOKED)) ar->ftransfer = ar->ntransfer = 0; else { - ar->ftransfer = ci->u2.transferinfo.ftransfer; - ar->ntransfer = ci->u2.transferinfo.ntransfer; + ar->ftransfer = L->transferinfo.ftransfer; + ar->ntransfer = L->transferinfo.ntransfer; } + break; } case 'L': case 'f': /* handled by lua_getinfo */ @@ -384,20 +400,20 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { lua_lock(L); if (*what == '>') { ci = NULL; - func = s2v(L->top - 1); + func = s2v(L->top.p - 1); api_check(L, ttisfunction(func), "function expected"); what++; /* skip the '>' */ - L->top--; /* pop function */ + L->top.p--; /* pop function */ } else { ci = ar->i_ci; - func = s2v(ci->func); + func = s2v(ci->func.p); lua_assert(ttisfunction(func)); } cl = ttisclosure(func) ? clvalue(func) : NULL; status = auxgetinfo(L, what, ar, cl, ci); if (strchr(what, 'f')) { - setobj2s(L, L->top, func); + setobj2s(L, L->top.p, func); api_incr_top(L); } if (strchr(what, 'L')) @@ -413,40 +429,6 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { ** ======================================================= */ -static const char *getobjname (const Proto *p, int lastpc, int reg, - const char **name); - - -/* -** Find a "name" for the constant 'c'. -*/ -static void kname (const Proto *p, int c, const char **name) { - TValue *kvalue = &p->k[c]; - *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?"; -} - - -/* -** Find a "name" for the register 'c'. -*/ -static void rname (const Proto *p, int pc, int c, const char **name) { - const char *what = getobjname(p, pc, c, name); /* search for 'c' */ - if (!(what && *what == 'c')) /* did not find a constant name? */ - *name = "?"; -} - - -/* -** Find a "name" for a 'C' value in an RK instruction. -*/ -static void rkname (const Proto *p, int pc, Instruction i, const char **name) { - int c = GETARG_C(i); /* key index */ - if (GETARG_k(i)) /* is 'c' a constant? */ - kname(p, c, name); - else /* 'c' is a register */ - rname(p, pc, c, name); -} - static int filterpc (int pc, int jmptarget) { if (pc < jmptarget) /* is code conditional (inside a jump)? */ @@ -456,12 +438,14 @@ static int filterpc (int pc, int jmptarget) { /* -** try to find last instruction before 'lastpc' that modified register 'reg' +** Try to find last instruction before 'lastpc' that modified register 'reg'. */ static int findsetreg (const Proto *p, int lastpc, int reg) { int pc; int setreg = -1; /* keep last instruction that changed 'reg' */ int jmptarget = 0; /* any code before this address is conditional */ + if (testMMMode(GET_OPCODE(p->code[lastpc]))) + lastpc--; /* previous instruction was not actually executed */ for (pc = 0; pc < lastpc; pc++) { Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); @@ -503,28 +487,29 @@ static int findsetreg (const Proto *p, int lastpc, int reg) { /* -** Check whether table being indexed by instruction 'i' is the -** environment '_ENV' +** Find a "name" for the constant 'c'. */ -static const char *gxf (const Proto *p, int pc, Instruction i, int isup) { - int t = GETARG_B(i); /* table index */ - const char *name; /* name of indexed variable */ - if (isup) /* is an upvalue? */ - name = upvalname(p, t); - else - getobjname(p, pc, t, &name); - return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field"; +static const char *kname (const Proto *p, int index, const char **name) { + TValue *kvalue = &p->k[index]; + if (ttisstring(kvalue)) { + *name = getstr(tsvalue(kvalue)); + return "constant"; + } + else { + *name = "?"; + return NULL; + } } - const char *getobjname (const Proto *p, int lastpc, int reg, - const char **name) { - int pc; - *name = luaF_getlocalname(p, reg + 1, lastpc); +static const char *basicgetobjname (const Proto *p, int *ppc, int reg, + const char **name) { + int pc = *ppc; + *name = luaF_getlocalname(p, reg + 1, pc); if (*name) /* is a local? */ - return "local"; + return strlocal; /* else try symbolic execution */ - pc = findsetreg(p, lastpc, reg); + *ppc = pc = findsetreg(p, pc, reg); if (pc != -1) { /* could find instruction? */ Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); @@ -532,18 +517,73 @@ static const char *gxf (const Proto *p, int pc, Instruction i, int isup) { case OP_MOVE: { int b = GETARG_B(i); /* move from 'b' to 'a' */ if (b < GETARG_A(i)) - return getobjname(p, pc, b, name); /* get name for 'b' */ + return basicgetobjname(p, ppc, b, name); /* get name for 'b' */ break; } + case OP_GETUPVAL: { + *name = upvalname(p, GETARG_B(i)); + return strupval; + } + case OP_LOADK: return kname(p, GETARG_Bx(i), name); + case OP_LOADKX: return kname(p, GETARG_Ax(p->code[pc + 1]), name); + default: break; + } + } + return NULL; /* could not find reasonable name */ +} + + +/* +** Find a "name" for the register 'c'. +*/ +static void rname (const Proto *p, int pc, int c, const char **name) { + const char *what = basicgetobjname(p, &pc, c, name); /* search for 'c' */ + if (!(what && *what == 'c')) /* did not find a constant name? */ + *name = "?"; +} + + +/* +** Check whether table being indexed by instruction 'i' is the +** environment '_ENV' +*/ +static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) { + int t = GETARG_B(i); /* table index */ + const char *name; /* name of indexed variable */ + if (isup) /* is 't' an upvalue? */ + name = upvalname(p, t); + else { /* 't' is a register */ + const char *what = basicgetobjname(p, &pc, t, &name); + /* 'name' must be the name of a local variable (at the current + level or an upvalue) */ + if (what != strlocal && what != strupval) + name = NULL; /* cannot be the variable _ENV */ + } + return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field"; +} + + +/* +** Extend 'basicgetobjname' to handle table accesses +*/ +static const char *getobjname (const Proto *p, int lastpc, int reg, + const char **name) { + const char *kind = basicgetobjname(p, &lastpc, reg, name); + if (kind != NULL) + return kind; + else if (lastpc != -1) { /* could find instruction? */ + Instruction i = p->code[lastpc]; + OpCode op = GET_OPCODE(i); + switch (op) { case OP_GETTABUP: { int k = GETARG_C(i); /* key index */ kname(p, k, name); - return gxf(p, pc, i, 1); + return isEnv(p, lastpc, i, 1); } case OP_GETTABLE: { int k = GETARG_C(i); /* key index */ - rname(p, pc, k, name); - return gxf(p, pc, i, 0); + rname(p, lastpc, k, name); + return isEnv(p, lastpc, i, 0); } case OP_GETI: { *name = "integer index"; @@ -552,24 +592,11 @@ static const char *gxf (const Proto *p, int pc, Instruction i, int isup) { case OP_GETFIELD: { int k = GETARG_C(i); /* key index */ kname(p, k, name); - return gxf(p, pc, i, 0); - } - case OP_GETUPVAL: { - *name = upvalname(p, GETARG_B(i)); - return "upvalue"; - } - case OP_LOADK: - case OP_LOADKX: { - int b = (op == OP_LOADK) ? GETARG_Bx(i) - : GETARG_Ax(p->code[pc + 1]); - if (ttisstring(&p->k[b])) { - *name = svalue(&p->k[b]); - return "constant"; - } - break; + return isEnv(p, lastpc, i, 0); } case OP_SELF: { - rkname(p, pc, i, name); + int k = GETARG_C(i); /* key index */ + kname(p, k, name); return "method"; } default: break; /* go through to return NULL */ @@ -585,16 +612,10 @@ static const char *gxf (const Proto *p, int pc, Instruction i, int isup) { ** Returns what the name is (e.g., "for iterator", "method", ** "metamethod") and sets '*name' to point to the name. */ -static const char *funcnamefromcode (lua_State *L, CallInfo *ci, - const char **name) { +static const char *funcnamefromcode (lua_State *L, const Proto *p, + int pc, const char **name) { TMS tm = (TMS)0; /* (initial value avoids warnings) */ - const Proto *p = ci_func(ci)->p; /* calling function */ - int pc = currentpc(ci); /* calling instruction index */ Instruction i = p->code[pc]; /* calling instruction */ - if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ - *name = "?"; - return "hook"; - } switch (GET_OPCODE(i)) { case OP_CALL: case OP_TAILCALL: @@ -611,24 +632,8 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD: tm = TM_NEWINDEX; break; - case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_MODI: - case OP_POWI: case OP_DIVI: case OP_IDIVI: { - int offset = GET_OPCODE(i) - OP_ADDI; /* ORDER OP */ - tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ - break; - } - case OP_ADDK: case OP_SUBK: case OP_MULK: case OP_MODK: - case OP_POWK: case OP_DIVK: case OP_IDIVK: - case OP_BANDK: case OP_BORK: case OP_BXORK: { - int offset = GET_OPCODE(i) - OP_ADDK; /* ORDER OP */ - tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ - break; - } - case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD: - case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND: - case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: { - int offset = GET_OPCODE(i) - OP_ADD; /* ORDER OP */ - tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ + case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { + tm = cast(TMS, GETARG_C(i)); break; } case OP_UNM: tm = TM_UNM; break; @@ -636,32 +641,55 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, case OP_LEN: tm = TM_LEN; break; case OP_CONCAT: tm = TM_CONCAT; break; case OP_EQ: tm = TM_EQ; break; - case OP_LT: case OP_LE: case OP_LTI: case OP_LEI: - *name = "order"; /* '<=' can call '__lt', etc. */ - return "metamethod"; - case OP_SHRI: case OP_SHLI: - *name = "shift"; - return "metamethod"; + /* no cases for OP_EQI and OP_EQK, as they don't call metamethods */ + case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break; + case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break; + case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break; default: return NULL; /* cannot find a reasonable name */ } - *name = getstr(G(L)->tmname[tm]) + 2; + *name = getshrstr(G(L)->tmname[tm]) + 2; return "metamethod"; } + +/* +** Try to find a name for a function based on how it was called. +*/ +static const char *funcnamefromcall (lua_State *L, CallInfo *ci, + const char **name) { + if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ + *name = "?"; + return "hook"; + } + else if (ci->callstatus & CIST_FIN) { /* was it called as a finalizer? */ + *name = "__gc"; + return "metamethod"; /* report it as such */ + } + else if (isLua(ci)) + return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name); + else + return NULL; +} + /* }====================================================== */ /* -** The subtraction of two potentially unrelated pointers is -** not ISO C, but it should not crash a program; the subsequent -** checks are ISO C and ensure a correct result. +** Check whether pointer 'o' points to some value in the stack frame of +** the current function and, if so, returns its index. Because 'o' may +** not point to a value in this stack, we cannot compare it with the +** region boundaries (undefined behavior in ISO C). */ -static int isinstack (CallInfo *ci, const TValue *o) { - StkId base = ci->func + 1; - ptrdiff_t i = cast(StkId, o) - base; - return (0 <= i && i < (ci->top - base) && s2v(base + i) == o); +static int instack (CallInfo *ci, const TValue *o) { + int pos; + StkId base = ci->func.p + 1; + for (pos = 0; base + pos < ci->top.p; pos++) { + if (o == s2v(base + pos)) + return pos; + } + return -1; /* not found */ } @@ -675,32 +703,73 @@ static const char *getupvalname (CallInfo *ci, const TValue *o, LClosure *c = ci_func(ci); int i; for (i = 0; i < c->nupvalues; i++) { - if (c->upvals[i]->v == o) { + if (c->upvals[i]->v.p == o) { *name = upvalname(c->p, i); - return "upvalue"; + return strupval; } } return NULL; } +static const char *formatvarinfo (lua_State *L, const char *kind, + const char *name) { + if (kind == NULL) + return ""; /* no information */ + else + return luaO_pushfstring(L, " (%s '%s')", kind, name); +} + +/* +** Build a string with a "description" for the value 'o', such as +** "variable 'x'" or "upvalue 'y'". +*/ static const char *varinfo (lua_State *L, const TValue *o) { - const char *name = NULL; /* to avoid warnings */ CallInfo *ci = L->ci; + const char *name = NULL; /* to avoid warnings */ const char *kind = NULL; if (isLua(ci)) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ - if (!kind && isinstack(ci, o)) /* no? try a register */ - kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(cast(StkId, o) - (ci->func + 1)), &name); + if (!kind) { /* not an upvalue? */ + int reg = instack(ci, o); /* try a register */ + if (reg >= 0) /* is 'o' a register? */ + kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name); + } } - return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; + return formatvarinfo(L, kind, name); } -l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { +/* +** Raise a type error +*/ +static l_noret typeerror (lua_State *L, const TValue *o, const char *op, + const char *extra) { const char *t = luaT_objtypename(L, o); - luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o)); + luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra); +} + + +/* +** Raise a type error with "standard" information about the faulty +** object 'o' (using 'varinfo'). +*/ +l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { + typeerror(L, o, op, varinfo(L, o)); +} + + +/* +** Raise an error for calling a non-callable object. Try to find a name +** for the object based on how it was called ('funcnamefromcall'); if it +** cannot get a name there, try 'varinfo'. +*/ +l_noret luaG_callerror (lua_State *L, const TValue *o) { + CallInfo *ci = L->ci; + const char *name = NULL; /* to avoid warnings */ + const char *kind = funcnamefromcall(L, ci, &name); + const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o); + typeerror(L, o, "call", extra); } @@ -729,7 +798,7 @@ l_noret luaG_opinterror (lua_State *L, const TValue *p1, */ l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { lua_Integer temp; - if (!tointegerns(p1, &temp)) + if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I)) p2 = p1; luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); } @@ -745,16 +814,26 @@ l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { } +l_noret luaG_errnnil (lua_State *L, LClosure *cl, int k) { + const char *globalname = "?"; /* default name if k == 0 */ + if (k > 0) + kname(cl->p, k - 1, &globalname); + luaG_runerror(L, "global '%s' already defined", globalname); +} + + /* add src:line information to 'msg' */ const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, int line) { - char buff[LUA_IDSIZE]; - if (src) - luaO_chunkid(buff, getstr(src), LUA_IDSIZE); - else { /* no source available; use "?" instead */ - buff[0] = '?'; buff[1] = '\0'; + if (src == NULL) /* no debug information? */ + return luaO_pushfstring(L, "?:?: %s", msg); + else { + char buff[LUA_IDSIZE]; + size_t idlen; + const char *id = getlstr(src, idlen); + luaO_chunkid(buff, id, idlen); + return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); } - return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); } @@ -762,10 +841,14 @@ l_noret luaG_errormsg (lua_State *L) { if (L->errfunc != 0) { /* is there an error handling function? */ StkId errfunc = restorestack(L, L->errfunc); lua_assert(ttisfunction(s2v(errfunc))); - setobjs2s(L, L->top, L->top - 1); /* move argument */ - setobjs2s(L, L->top - 1, errfunc); /* push function */ - L->top++; /* assume EXTRA_STACK */ - luaD_callnoyield(L, L->top - 2, 1); /* call it */ + setobjs2s(L, L->top.p, L->top.p - 1); /* move argument */ + setobjs2s(L, L->top.p - 1, errfunc); /* push function */ + L->top.p++; /* assume EXTRA_STACK */ + luaD_callnoyield(L, L->top.p - 2, 1); /* call it */ + } + if (ttisnil(s2v(L->top.p - 1))) { /* error object is nil? */ + /* change it to a proper message */ + setsvalue2s(L, L->top.p - 1, luaS_newliteral(L, "")); } luaD_throw(L, LUA_ERRRUN); } @@ -776,31 +859,84 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { const char *msg; va_list argp; luaC_checkGC(L); /* error message uses memory */ - va_start(argp, fmt); - msg = luaO_pushvfstring(L, fmt, argp); /* format message */ - va_end(argp); - if (isLua(ci)) /* if Lua function, add source:line information */ - luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci)); + pushvfstring(L, argp, fmt, msg); + if (isLua(ci)) { /* Lua function? */ + /* add source:line information */ + luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); + setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */ + L->top.p--; + } luaG_errormsg(L); } /* ** Check whether new instruction 'newpc' is in a different line from -** previous instruction 'oldpc'. +** previous instruction 'oldpc'. More often than not, 'newpc' is only +** one or a few instructions after 'oldpc' (it must be after, see +** caller), so try to avoid calling 'luaG_getfuncline'. If they are +** too far apart, there is a good chance of a ABSLINEINFO in the way, +** so it goes directly to 'luaG_getfuncline'. */ static int changedline (const Proto *p, int oldpc, int newpc) { - while (oldpc++ < newpc) { - if (p->lineinfo[oldpc] != 0) - return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc)); + if (p->lineinfo == NULL) /* no debug information? */ + return 0; + if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */ + int delta = 0; /* line difference */ + int pc = oldpc; + for (;;) { + int lineinfo = p->lineinfo[++pc]; + if (lineinfo == ABSLINEINFO) + break; /* cannot compute delta; fall through */ + delta += lineinfo; + if (pc == newpc) + return (delta != 0); /* delta computed successfully */ + } } - return 0; /* no line changes in the way */ + /* either instructions are too far apart or there is an absolute line + info in the way; compute line difference explicitly */ + return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc)); } +/* +** Traces Lua calls. If code is running the first instruction of a function, +** and function is not vararg, and it is not coming from an yield, +** calls 'luaD_hookcall'. (Vararg functions will call 'luaD_hookcall' +** after adjusting its variable arguments; otherwise, they could call +** a line/count hook before the call hook. Functions coming from +** an yield already called 'luaD_hookcall' before yielding.) +*/ +int luaG_tracecall (lua_State *L) { + CallInfo *ci = L->ci; + Proto *p = ci_func(ci)->p; + ci->u.l.trap = 1; /* ensure hooks will be checked */ + if (ci->u.l.savedpc == p->code) { /* first instruction (not resuming)? */ + if (isvararg(p)) + return 0; /* hooks will start at VARARGPREP instruction */ + else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yielded? */ + luaD_hookcall(L, ci); /* check 'call' hook */ + } + return 1; /* keep 'trap' on */ +} + + +/* +** Traces the execution of a Lua function. Called before the execution +** of each opcode, when debug is on. 'L->oldpc' stores the last +** instruction traced, to detect line changes. When entering a new +** function, 'npci' will be zero and will test as a new line whatever +** the value of 'oldpc'. Some exceptional conditions may return to +** a function without setting 'oldpc'. In that case, 'oldpc' may be +** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc' +** at most causes an extra call to a line hook.) +** This function is not "Protected" when called, so it should correct +** 'L->top.p' before calling anything that can run the GC. +*/ int luaG_traceexec (lua_State *L, const Instruction *pc) { CallInfo *ci = L->ci; - lu_byte mask = L->hookmask; + lu_byte mask = cast_byte(L->hookmask); + const Proto *p = ci_func(ci)->p; int counthook; if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ ci->u.l.trap = 0; /* don't need to stop again */ @@ -808,34 +944,33 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { } pc++; /* reference is always next instruction */ ci->u.l.savedpc = pc; /* save 'pc' */ - counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); + counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0); if (counthook) resethookcount(L); /* reset count */ else if (!(mask & LUA_MASKLINE)) return 1; /* no line hook and count != 0; nothing to be done now */ - if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ + if (ci->callstatus & CIST_HOOKYIELD) { /* hook yielded last time? */ ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ return 1; /* do not call hook again (VM yielded, so it did not move) */ } - if (!isIT(*(ci->u.l.savedpc - 1))) - L->top = ci->top; /* prepare top */ + if (!luaP_isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */ + L->top.p = ci->top.p; /* correct top */ if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ if (mask & LUA_MASKLINE) { - const Proto *p = ci_func(ci)->p; + /* 'L->oldpc' may be invalid; use zero in this case */ + int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0; int npci = pcRel(pc, p); - if (npci == 0 || /* call linehook when enter a new function, */ - pc <= L->oldpc || /* when jump back (loop), or when */ - changedline(p, pcRel(L->oldpc, p), npci)) { /* enter new line */ + if (npci <= oldpc || /* call hook when jump back (loop), */ + changedline(p, oldpc, npci)) { /* or when enter new line */ int newline = luaG_getfuncline(p, npci); luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ } - L->oldpc = pc; /* 'pc' of last call to line hook */ + L->oldpc = npci; /* 'pc' of last call to line hook */ } if (L->status == LUA_YIELD) { /* did hook yield? */ if (counthook) L->hookcount = 1; /* undo decrement to zero */ - ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ luaD_throw(L, LUA_YIELD); } diff --git a/ldebug.h b/ldebug.h index 1fe0efab08..20d07818b4 100644 --- a/ldebug.h +++ b/ldebug.h @@ -13,6 +13,11 @@ #define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1) + +/* Active Lua function (given call info) */ +#define ci_func(ci) (clLvalue(s2v((ci)->func.p))) + + #define resethookcount(L) (L->hookcount = L->basehookcount) /* @@ -21,11 +26,22 @@ */ #define ABSLINEINFO (-0x80) + +/* +** MAXimum number of successive Instructions WiTHout ABSolute line +** information. (A power of two allows fast divisions.) +*/ +#if !defined(MAXIWTHABS) +#define MAXIWTHABS 128 +#endif + + LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc); LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos); LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *opname); +LUAI_FUNC l_noret luaG_callerror (lua_State *L, const TValue *o); LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what); LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, @@ -37,11 +53,13 @@ LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2); LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2); +LUAI_FUNC l_noret luaG_errnnil (lua_State *L, LClosure *cl, int k); LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, int line); LUAI_FUNC l_noret luaG_errormsg (lua_State *L); LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc); +LUAI_FUNC int luaG_tracecall (lua_State *L); #endif diff --git a/ldo.c b/ldo.c index 077109c4ab..6d0184ecd5 100644 --- a/ldo.c +++ b/ldo.c @@ -38,16 +38,37 @@ #define errorstatus(s) ((s) > LUA_YIELD) +/* +** these macros allow user-specific actions when a thread is +** resumed/yielded. +*/ +#if !defined(luai_userstateresume) +#define luai_userstateresume(L,n) ((void)L) +#endif + +#if !defined(luai_userstateyield) +#define luai_userstateyield(L,n) ((void)L) +#endif + + /* ** {====================================================== ** Error-recovery functions ** ======================================================= */ +/* chained list of long jump buffers */ +typedef struct lua_longjmp { + struct lua_longjmp *previous; + jmp_buf b; + volatile TStatus status; /* error code */ +} lua_longjmp; + + /* ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By ** default, Lua handles errors with exceptions when compiling as -** C++ code, with _longjmp/_setjmp when asked to use them, and with +** C++ code, with _longjmp/_setjmp when available (POSIX), and with ** longjmp/setjmp otherwise. */ #if !defined(LUAI_THROW) /* { */ @@ -56,79 +77,67 @@ /* C++ exceptions */ #define LUAI_THROW(L,c) throw(c) -#define LUAI_TRY(L,c,a) \ - try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; } -#define luai_jmpbuf int /* dummy variable */ + +static void LUAI_TRY (lua_State *L, lua_longjmp *c, Pfunc f, void *ud) { + try { + f(L, ud); /* call function protected */ + } + catch (lua_longjmp *c1) { /* Lua error */ + if (c1 != c) /* not the correct level? */ + throw; /* rethrow to upper level */ + } + catch (...) { /* non-Lua exception */ + c->status = -1; /* create some error code */ + } +} + #elif defined(LUA_USE_POSIX) /* }{ */ -/* in POSIX, try _longjmp/_setjmp (more efficient) */ +/* in POSIX, use _longjmp/_setjmp (more efficient) */ #define LUAI_THROW(L,c) _longjmp((c)->b, 1) -#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } -#define luai_jmpbuf jmp_buf +#define LUAI_TRY(L,c,f,ud) if (_setjmp((c)->b) == 0) ((f)(L, ud)) #else /* }{ */ /* ISO C handling with long jumps */ #define LUAI_THROW(L,c) longjmp((c)->b, 1) -#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } -#define luai_jmpbuf jmp_buf +#define LUAI_TRY(L,c,f,ud) if (setjmp((c)->b) == 0) ((f)(L, ud)) #endif /* } */ #endif /* } */ - -/* chain list of long jump buffers */ -struct lua_longjmp { - struct lua_longjmp *previous; - luai_jmpbuf b; - volatile int status; /* error code */ -}; - - -void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { - switch (errcode) { - case LUA_ERRMEM: { /* memory error? */ - setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ - break; - } - case LUA_ERRERR: { - setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); - break; - } - case CLOSEPROTECT: { - setnilvalue(s2v(oldtop)); /* no error message */ - break; - } - default: { - setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ - break; - } +void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) { + if (errcode == LUA_ERRMEM) { /* memory error? */ + setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ + } + else { + lua_assert(errorstatus(errcode)); /* must be a real error */ + lua_assert(!ttisnil(s2v(L->top.p - 1))); /* with a non-nil object */ + setobjs2s(L, oldtop, L->top.p - 1); /* move it to 'oldtop' */ } - L->top = oldtop + 1; + L->top.p = oldtop + 1; /* top goes back to old top plus error object */ } -l_noret luaD_throw (lua_State *L, int errcode) { +l_noret luaD_throw (lua_State *L, TStatus errcode) { if (L->errorJmp) { /* thread has an error handler? */ L->errorJmp->status = errcode; /* set status */ LUAI_THROW(L, L->errorJmp); /* jump to it */ } else { /* thread has no error handler */ global_State *g = G(L); - errcode = luaF_close(L, L->stack, errcode); /* close all upvalues */ - L->status = cast_byte(errcode); /* mark it as dead */ - if (g->mainthread->errorJmp) { /* main thread has a handler? */ - setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ - luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ + lua_State *mainth = mainthread(g); + errcode = luaE_resetthread(L, errcode); /* close all upvalues */ + L->status = errcode; + if (mainth->errorJmp) { /* main thread has a handler? */ + setobjs2s(L, mainth->top.p++, L->top.p - 1); /* copy error obj. */ + luaD_throw(mainth, errcode); /* re-throw in main thread */ } else { /* no handler at all; abort */ if (g->panic) { /* panic function? */ - luaD_seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */ - if (L->ci->top < L->top) - L->ci->top = L->top; /* pushing msg. can break this invariant */ lua_unlock(L); g->panic(L); /* call panic function (last chance to jump out) */ } @@ -138,18 +147,25 @@ l_noret luaD_throw (lua_State *L, int errcode) { } -int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { - l_uint32 oldnCcalls = L->nCcalls - L->nci; - struct lua_longjmp lj; - lua_assert(L->nCcalls >= L->nci); +l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode) { + if (L->errorJmp) { + /* unroll error entries up to the first level */ + while (L->errorJmp->previous != NULL) + L->errorJmp = L->errorJmp->previous; + } + luaD_throw(L, errcode); +} + + +TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { + l_uint32 oldnCcalls = L->nCcalls; + lua_longjmp lj; lj.status = LUA_OK; lj.previous = L->errorJmp; /* chain new error handler */ L->errorJmp = &lj; - LUAI_TRY(L, &lj, - (*f)(L, ud); - ); + LUAI_TRY(L, &lj, f, ud); /* call 'f' catching errors */ L->errorJmp = lj.previous; /* restore old error handler */ - L->nCcalls = oldnCcalls + L->nci; + L->nCcalls = oldnCcalls; return lj.status; } @@ -161,107 +177,255 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { ** Stack reallocation ** =================================================================== */ -static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { + +/* some stack space for error handling */ +#define STACKERRSPACE 200 + + +/* +** LUAI_MAXSTACK limits the size of the Lua stack. +** It must fit into INT_MAX/2. +*/ + +#if !defined(LUAI_MAXSTACK) +#if 1000000 < (INT_MAX / 2) +#define LUAI_MAXSTACK 1000000 +#else +#define LUAI_MAXSTACK (INT_MAX / 2u) +#endif +#endif + + +/* maximum stack size that respects size_t */ +#define MAXSTACK_BYSIZET ((MAX_SIZET / sizeof(StackValue)) - STACKERRSPACE) + +/* +** Minimum between LUAI_MAXSTACK and MAXSTACK_BYSIZET +** (Maximum size for the stack must respect size_t.) +*/ +#define MAXSTACK cast_int(LUAI_MAXSTACK < MAXSTACK_BYSIZET \ + ? LUAI_MAXSTACK : MAXSTACK_BYSIZET) + + +/* stack size with extra space for error handling */ +#define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE) + + +/* raise a stack error while running the message handler */ +l_noret luaD_errerr (lua_State *L) { + TString *msg = luaS_newliteral(L, "error in error handling"); + setsvalue2s(L, L->top.p, msg); + L->top.p++; /* assume EXTRA_STACK */ + luaD_throw(L, LUA_ERRERR); +} + + +/* +** Check whether stack has enough space to run a simple function (such +** as a finalizer): At least BASIC_STACK_SIZE in the Lua stack and +** 2 slots in the C stack. +*/ +int luaD_checkminstack (lua_State *L) { + return ((stacksize(L) < MAXSTACK - BASIC_STACK_SIZE) && + (getCcalls(L) < LUAI_MAXCCALLS - 2)); +} + + +/* +** In ISO C, any pointer use after the pointer has been deallocated is +** undefined behavior. So, before a stack reallocation, all pointers +** should be changed to offsets, and after the reallocation they should +** be changed back to pointers. As during the reallocation the pointers +** are invalid, the reallocation cannot run emergency collections. +** Alternatively, we can use the old address after the deallocation. +** That is not strict ISO C, but seems to work fine everywhere. +** The following macro chooses how strict is the code. +*/ +#if !defined(LUAI_STRICT_ADDRESS) +#define LUAI_STRICT_ADDRESS 1 +#endif + +#if LUAI_STRICT_ADDRESS +/* +** Change all pointers to the stack into offsets. +*/ +static void relstack (lua_State *L) { CallInfo *ci; UpVal *up; - if (oldstack == newstack) - return; /* stack address did not change */ - L->top = (L->top - oldstack) + newstack; + L->top.offset = savestack(L, L->top.p); + L->tbclist.offset = savestack(L, L->tbclist.p); + for (up = L->openupval; up != NULL; up = up->u.open.next) + up->v.offset = savestack(L, uplevel(up)); + for (ci = L->ci; ci != NULL; ci = ci->previous) { + ci->top.offset = savestack(L, ci->top.p); + ci->func.offset = savestack(L, ci->func.p); + } +} + + +/* +** Change back all offsets into pointers. +*/ +static void correctstack (lua_State *L, StkId oldstack) { + CallInfo *ci; + UpVal *up; + UNUSED(oldstack); + L->top.p = restorestack(L, L->top.offset); + L->tbclist.p = restorestack(L, L->tbclist.offset); for (up = L->openupval; up != NULL; up = up->u.open.next) - up->v = s2v((uplevel(up) - oldstack) + newstack); + up->v.p = s2v(restorestack(L, up->v.offset)); for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->top = (ci->top - oldstack) + newstack; - ci->func = (ci->func - oldstack) + newstack; + ci->top.p = restorestack(L, ci->top.offset); + ci->func.p = restorestack(L, ci->func.offset); if (isLua(ci)) ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ } } +#else +/* +** Assume that it is fine to use an address after its deallocation, +** as long as we do not dereference it. +*/ -/* some space for error handling */ -#define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) +static void relstack (lua_State *L) { UNUSED(L); } /* do nothing */ +/* +** Correct pointers into 'oldstack' to point into 'L->stack'. +*/ +static void correctstack (lua_State *L, StkId oldstack) { + CallInfo *ci; + UpVal *up; + StkId newstack = L->stack.p; + if (oldstack == newstack) + return; + L->top.p = L->top.p - oldstack + newstack; + L->tbclist.p = L->tbclist.p - oldstack + newstack; + for (up = L->openupval; up != NULL; up = up->u.open.next) + up->v.p = s2v(uplevel(up) - oldstack + newstack); + for (ci = L->ci; ci != NULL; ci = ci->previous) { + ci->top.p = ci->top.p - oldstack + newstack; + ci->func.p = ci->func.p - oldstack + newstack; + if (isLua(ci)) + ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ + } +} +#endif + + +/* +** Reallocate the stack to a new size, correcting all pointers into it. +** In case of allocation error, raise an error or return false according +** to 'raiseerror'. +*/ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { - int lim = L->stacksize; - StkId newstack = luaM_reallocvector(L, L->stack, lim, newsize, StackValue); - lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); - lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); - if (unlikely(newstack == NULL)) { /* reallocation failed? */ + int oldsize = stacksize(L); + int i; + StkId newstack; + StkId oldstack = L->stack.p; + lu_byte oldgcstop = G(L)->gcstopem; + lua_assert(newsize <= MAXSTACK || newsize == ERRORSTACKSIZE); + relstack(L); /* change pointers to offsets */ + G(L)->gcstopem = 1; /* stop emergency collection */ + newstack = luaM_reallocvector(L, oldstack, oldsize + EXTRA_STACK, + newsize + EXTRA_STACK, StackValue); + G(L)->gcstopem = oldgcstop; /* restore emergency collection */ + if (l_unlikely(newstack == NULL)) { /* reallocation failed? */ + correctstack(L, oldstack); /* change offsets back to pointers */ if (raiseerror) luaM_error(L); else return 0; /* do not raise an error */ } - for (; lim < newsize; lim++) - setnilvalue(s2v(newstack + lim)); /* erase new segment */ - correctstack(L, L->stack, newstack); - L->stack = newstack; - L->stacksize = newsize; - L->stack_last = L->stack + newsize - EXTRA_STACK; + L->stack.p = newstack; + correctstack(L, oldstack); /* change offsets back to pointers */ + L->stack_last.p = L->stack.p + newsize; + for (i = oldsize + EXTRA_STACK; i < newsize + EXTRA_STACK; i++) + setnilvalue(s2v(newstack + i)); /* erase new segment */ return 1; } /* -** Try to grow the stack by at least 'n' elements. when 'raiseerror' +** Try to grow the stack by at least 'n' elements. When 'raiseerror' ** is true, raises any error; otherwise, return 0 in case of errors. */ int luaD_growstack (lua_State *L, int n, int raiseerror) { - int size = L->stacksize; - int newsize = 2 * size; /* tentative new size */ - if (unlikely(size > LUAI_MAXSTACK)) { /* need more space after extra size? */ + int size = stacksize(L); + if (l_unlikely(size > MAXSTACK)) { + /* if stack is larger than maximum, thread is already using the + extra space reserved for errors, that is, thread is handling + a stack error; cannot grow further than that. */ + lua_assert(stacksize(L) == ERRORSTACKSIZE); if (raiseerror) - luaD_throw(L, LUA_ERRERR); /* error inside message handler */ - else return 0; + luaD_errerr(L); /* stack error inside message handler */ + return 0; /* if not 'raiseerror', just signal it */ } - else { - int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; - if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ - newsize = LUAI_MAXSTACK; + else if (n < MAXSTACK) { /* avoids arithmetic overflows */ + int newsize = size + (size >> 1); /* tentative new size (size * 1.5) */ + int needed = cast_int(L->top.p - L->stack.p) + n; + if (newsize > MAXSTACK) /* cannot cross the limit */ + newsize = MAXSTACK; if (newsize < needed) /* but must respect what was asked for */ newsize = needed; - if (unlikely(newsize > LUAI_MAXSTACK)) { /* stack overflow? */ - /* add extra size to be able to handle the error message */ - luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); - if (raiseerror) - luaG_runerror(L, "stack overflow"); - else return 0; - } - } /* else no errors */ - return luaD_reallocstack(L, newsize, raiseerror); + if (l_likely(newsize <= MAXSTACK)) + return luaD_reallocstack(L, newsize, raiseerror); + } + /* else stack overflow */ + /* add extra size to be able to handle the error message */ + luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); + if (raiseerror) + luaG_runerror(L, "stack overflow"); + return 0; } +/* +** Compute how much of the stack is being used, by computing the +** maximum top of all call frames in the stack and the current top. +*/ static int stackinuse (lua_State *L) { CallInfo *ci; - StkId lim = L->top; + int res; + StkId lim = L->top.p; for (ci = L->ci; ci != NULL; ci = ci->previous) { - if (lim < ci->top) lim = ci->top; + if (lim < ci->top.p) lim = ci->top.p; } - lua_assert(lim <= L->stack_last); - return cast_int(lim - L->stack) + 1; /* part of stack in use */ + lua_assert(lim <= L->stack_last.p + EXTRA_STACK); + res = cast_int(lim - L->stack.p) + 1; /* part of stack in use */ + if (res < LUA_MINSTACK) + res = LUA_MINSTACK; /* ensure a minimum size */ + return res; } +/* +** If stack size is more than 3 times the current use, reduce that size +** to twice the current use. (So, the final stack size is at most 2/3 the +** previous size, and half of its entries are empty.) +** As a particular case, if stack was handling a stack overflow and now +** it is not, 'max' (limited by MAXSTACK) will be smaller than +** stacksize (equal to ERRORSTACKSIZE in this case), and so the stack +** will be reduced to a "regular" size. +*/ void luaD_shrinkstack (lua_State *L) { int inuse = stackinuse(L); - int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; - if (goodsize > LUAI_MAXSTACK) - goodsize = LUAI_MAXSTACK; /* respect stack limit */ + int max = (inuse > MAXSTACK / 3) ? MAXSTACK : inuse * 3; /* if thread is currently not handling a stack overflow and its - good size is smaller than current size, shrink its stack */ - if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && - goodsize < L->stacksize) - luaD_reallocstack(L, goodsize, 0); /* ok if that fails */ + size is larger than maximum "reasonable" size, shrink it */ + if (inuse <= MAXSTACK && stacksize(L) > max) { + int nsize = (inuse > MAXSTACK / 2) ? MAXSTACK : inuse * 2; + luaD_reallocstack(L, nsize, 0); /* ok if that fails */ + } else /* don't change stack */ - condmovestack(L,{},{}); /* (change only for debugging) */ + condmovestack(L,(void)0,(void)0); /* (change only for debugging) */ luaE_shrinkCI(L); /* shrink CI list */ } void luaD_inctop (lua_State *L) { + L->top.p++; luaD_checkstack(L, 1); - L->top++; } /* }================================================================== */ @@ -276,32 +440,30 @@ void luaD_hook (lua_State *L, int event, int line, int ftransfer, int ntransfer) { lua_Hook hook = L->hook; if (hook && L->allowhook) { /* make sure there is a hook */ - int mask = CIST_HOOKED; CallInfo *ci = L->ci; - ptrdiff_t top = savestack(L, L->top); - ptrdiff_t ci_top = savestack(L, ci->top); + ptrdiff_t top = savestack(L, L->top.p); /* preserve original 'top' */ + ptrdiff_t ci_top = savestack(L, ci->top.p); /* idem for 'ci->top' */ lua_Debug ar; ar.event = event; ar.currentline = line; ar.i_ci = ci; - if (ntransfer != 0) { - mask |= CIST_TRAN; /* 'ci' has transfer information */ - ci->u2.transferinfo.ftransfer = ftransfer; - ci->u2.transferinfo.ntransfer = ntransfer; - } + L->transferinfo.ftransfer = ftransfer; + L->transferinfo.ntransfer = ntransfer; + if (isLua(ci) && L->top.p < ci->top.p) + L->top.p = ci->top.p; /* protect entire activation register */ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - if (L->top + LUA_MINSTACK > ci->top) - ci->top = L->top + LUA_MINSTACK; + if (ci->top.p < L->top.p + LUA_MINSTACK) + ci->top.p = L->top.p + LUA_MINSTACK; L->allowhook = 0; /* cannot call hooks inside a hook */ - ci->callstatus |= mask; + ci->callstatus |= CIST_HOOKED; lua_unlock(L); (*hook)(L, &ar); lua_lock(L); lua_assert(!L->allowhook); L->allowhook = 1; - ci->top = restorestack(L, ci_top); - L->top = restorestack(L, top); - ci->callstatus &= ~mask; + ci->top.p = restorestack(L, ci_top); + L->top.p = restorestack(L, top); + ci->callstatus &= ~CIST_HOOKED; } } @@ -312,244 +474,386 @@ void luaD_hook (lua_State *L, int event, int line, ** active. */ void luaD_hookcall (lua_State *L, CallInfo *ci) { - int hook = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; - Proto *p; - if (!(L->hookmask & LUA_MASKCALL)) /* some other hook? */ - return; /* don't call hook */ - p = clLvalue(s2v(ci->func))->p; - L->top = ci->top; /* prepare top */ - ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - luaD_hook(L, hook, -1, 1, p->numparams); - ci->u.l.savedpc--; /* correct 'pc' */ -} - - -static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { - ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ - int delta = 0; - if (isLuacode(ci)) { - Proto *p = clLvalue(s2v(ci->func))->p; - if (p->is_vararg) - delta = ci->u.l.nextraargs + p->numparams + 1; - if (L->top < ci->top) - L->top = ci->top; /* correct top to run hook */ + L->oldpc = 0; /* set 'oldpc' for new function */ + if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */ + int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL + : LUA_HOOKCALL; + Proto *p = ci_func(ci)->p; + ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ + luaD_hook(L, event, -1, 1, p->numparams); + ci->u.l.savedpc--; /* correct 'pc' */ } +} + + +/* +** Executes a return hook for Lua and C functions and sets/corrects +** 'oldpc'. (Note that this correction is needed by the line hook, so it +** is done even when return hooks are off.) +*/ +static void rethook (lua_State *L, CallInfo *ci, int nres) { if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ + StkId firstres = L->top.p - nres; /* index of first result */ + int delta = 0; /* correction for vararg functions */ int ftransfer; - ci->func += delta; /* if vararg, back to virtual 'func' */ - ftransfer = cast(unsigned short, firstres - ci->func); + if (isLua(ci)) { + Proto *p = ci_func(ci)->p; + if (p->flag & PF_VAHID) + delta = ci->u.l.nextraargs + p->numparams + 1; + } + ci->func.p += delta; /* if vararg, back to virtual 'func' */ + ftransfer = cast_int(firstres - ci->func.p); luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ - ci->func -= delta; + ci->func.p -= delta; } - if (isLua(ci->previous)) - L->oldpc = ci->previous->u.l.savedpc; /* update 'oldpc' */ - return restorestack(L, oldtop); + if (isLua(ci = ci->previous)) + L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* set 'oldpc' */ } /* -** Check whether __call metafield of 'func' is a function. If so, put -** it in stack below original 'func' so that 'luaD_call' can call -** it. Raise an error if __call metafield is not a function. +** Check whether 'func' has a '__call' metafield. If so, put it in the +** stack, below original 'func', so that 'luaD_precall' can call it. +** Raise an error if there is no '__call' metafield. +** Bits CIST_CCMT in status count how many _call metamethods were +** invoked and how many corresponding extra arguments were pushed. +** (This count will be saved in the 'callstatus' of the call). +** Raise an error if this counter overflows. */ -void luaD_tryfuncTM (lua_State *L, StkId func) { - const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); +static unsigned tryfuncTM (lua_State *L, StkId func, unsigned status) { + const TValue *tm; StkId p; - if (unlikely(!ttisfunction(tm))) - luaG_typeerror(L, s2v(func), "call"); - for (p = L->top; p > func; p--) + tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); + if (l_unlikely(ttisnil(tm))) /* no metamethod? */ + luaG_callerror(L, s2v(func)); + for (p = L->top.p; p > func; p--) /* open space for metamethod */ setobjs2s(L, p, p-1); - L->top++; /* assume EXTRA_STACK */ + L->top.p++; /* stack space pre-allocated by the caller */ setobj2s(L, func, tm); /* metamethod is the new function to be called */ + if ((status & MAX_CCMT) == MAX_CCMT) /* is counter full? */ + luaG_runerror(L, "'__call' chain too long"); + return status + (1u << CIST_CCMT); /* increment counter */ +} + + +/* Generic case for 'moveresult' */ +l_sinline void genmoveresults (lua_State *L, StkId res, int nres, + int wanted) { + StkId firstresult = L->top.p - nres; /* index of first result */ + int i; + if (nres > wanted) /* extra results? */ + nres = wanted; /* don't need them */ + for (i = 0; i < nres; i++) /* move all results to correct place */ + setobjs2s(L, res + i, firstresult + i); + for (; i < wanted; i++) /* complete wanted number of results */ + setnilvalue(s2v(res + i)); + L->top.p = res + wanted; /* top points after the last result */ } /* -** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'. -** Handle most typical cases (zero results for commands, one result for -** expressions, multiple results for tail calls/single parameters) -** separated. +** Given 'nres' results at 'firstResult', move 'fwanted-1' of them +** to 'res'. Handle most typical cases (zero results for commands, +** one result for expressions, multiple results for tail calls/single +** parameters) separated. The flag CIST_TBC in 'fwanted', if set, +** forces the switch to go to the default case. */ -static void moveresults (lua_State *L, StkId res, int nres, int wanted) { - StkId firstresult; - int i; - switch (wanted) { /* handle typical cases separately */ - case 0: /* no values needed */ - L->top = res; +l_sinline void moveresults (lua_State *L, StkId res, int nres, + l_uint32 fwanted) { + switch (fwanted) { /* handle typical cases separately */ + case 0 + 1: /* no values needed */ + L->top.p = res; return; - case 1: /* one value needed */ + case 1 + 1: /* one value needed */ if (nres == 0) /* no results? */ setnilvalue(s2v(res)); /* adjust with nil */ - else - setobjs2s(L, res, L->top - nres); /* move it to proper place */ - L->top = res + 1; + else /* at least one result */ + setobjs2s(L, res, L->top.p - nres); /* move it to proper place */ + L->top.p = res + 1; return; - case LUA_MULTRET: - wanted = nres; /* we want all results */ + case LUA_MULTRET + 1: + genmoveresults(L, res, nres, nres); /* we want all results */ break; - default: /* multiple results (or to-be-closed variables) */ - if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ - ptrdiff_t savedres = savestack(L, res); - luaF_close(L, res, LUA_OK); /* may change the stack */ - res = restorestack(L, savedres); - wanted = codeNresults(wanted); /* correct value */ + default: { /* two/more results and/or to-be-closed variables */ + int wanted = get_nresults(fwanted); + if (fwanted & CIST_TBC) { /* to-be-closed variables? */ + L->ci->u2.nres = nres; + L->ci->callstatus |= CIST_CLSRET; /* in case of yields */ + res = luaF_close(L, res, CLOSEKTOP, 1); + L->ci->callstatus &= ~CIST_CLSRET; + if (L->hookmask) { /* if needed, call hook after '__close's */ + ptrdiff_t savedres = savestack(L, res); + rethook(L, L->ci, nres); + res = restorestack(L, savedres); /* hook can move stack */ + } if (wanted == LUA_MULTRET) - wanted = nres; + wanted = nres; /* we want all results */ } + genmoveresults(L, res, nres, wanted); break; + } } - firstresult = L->top - nres; /* index of first result */ - /* move all results to correct place */ - for (i = 0; i < nres && i < wanted; i++) - setobjs2s(L, res + i, firstresult + i); - for (; i < wanted; i++) /* complete wanted number of results */ - setnilvalue(s2v(res + i)); - L->top = res + wanted; /* top points after the last result */ } /* -** Finishes a function call: calls hook if necessary, removes CallInfo, -** moves current number of results to proper place. +** Finishes a function call: calls hook if necessary, moves current +** number of results to proper place, and returns to previous call +** info. If function has to close variables, hook must be called after +** that. */ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { - if (L->hookmask) - L->top = rethook(L, ci, L->top - nres, nres); - L->ci = ci->previous; /* back to caller */ + l_uint32 fwanted = ci->callstatus & (CIST_TBC | CIST_NRESULTS); + if (l_unlikely(L->hookmask) && !(fwanted & CIST_TBC)) + rethook(L, ci, nres); /* move results to proper place */ - moveresults(L, ci->func, nres, ci->nresults); + moveresults(L, ci->func.p, nres, fwanted); + /* function cannot be in any of these cases when returning */ + lua_assert(!(ci->callstatus & + (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_CLSRET))); + L->ci = ci->previous; /* back to caller (after closing variables) */ } -#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) +#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) + + +/* +** Allocate and initialize CallInfo structure. At this point, the +** only valid fields in the call status are number of results, +** CIST_C (if it's a C function), and number of extra arguments. +** (All these bit-fields fit in 16-bit values.) +*/ +l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, unsigned status, + StkId top) { + CallInfo *ci = L->ci = next_ci(L); /* new frame */ + ci->func.p = func; + lua_assert((status & ~(CIST_NRESULTS | CIST_C | MAX_CCMT)) == 0); + ci->callstatus = status; + ci->top.p = top; + return ci; +} + + +/* +** precall for C functions +*/ +l_sinline int precallC (lua_State *L, StkId func, unsigned status, + lua_CFunction f) { + int n; /* number of returns */ + CallInfo *ci; + checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ + L->ci = ci = prepCallInfo(L, func, status | CIST_C, + L->top.p + LUA_MINSTACK); + lua_assert(ci->top.p <= L->stack_last.p); + if (l_unlikely(L->hookmask & LUA_MASKCALL)) { + int narg = cast_int(L->top.p - func) - 1; + luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); + } + lua_unlock(L); + n = (*f)(L); /* do the actual call */ + lua_lock(L); + api_checknelems(L, n); + luaD_poscall(L, ci, n); + return n; +} /* ** Prepare a function for a tail call, building its call info on top ** of the current call info. 'narg1' is the number of arguments plus 1 -** (so that it includes the function itself). +** (so that it includes the function itself). Return the number of +** results, if it was a C function, or -1 for a Lua function. */ -void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { - Proto *p = clLvalue(s2v(func))->p; - int fsize = p->maxstacksize; /* frame size */ - int nfixparams = p->numparams; - int i; - for (i = 0; i < narg1; i++) /* move down function and arguments */ - setobjs2s(L, ci->func + i, func + i); - checkstackGC(L, fsize); - func = ci->func; /* moved-down function */ - for (; narg1 <= nfixparams; narg1++) - setnilvalue(s2v(func + narg1)); /* complete missing arguments */ - ci->top = func + 1 + fsize; /* top for new function */ - lua_assert(ci->top <= L->stack_last); - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus |= CIST_TAIL; - L->top = func + narg1; /* set top */ -} - - -/* -** Call a function (C or Lua). The function to be called is at *func. -** The arguments are on the stack, right after the function. -** When returns, all the results are on the stack, starting at the original -** function position. -*/ -void luaD_call (lua_State *L, StkId func, int nresults) { - lua_CFunction f; - TValue *funcv = s2v(func); - switch (ttypetag(funcv)) { - case LUA_TCCL: /* C closure */ - f = clCvalue(funcv)->f; - goto Cfunc; - case LUA_TLCF: /* light C function */ - f = fvalue(funcv); - Cfunc: { - int n; /* number of returns */ - CallInfo *ci; - checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - ci = next_ci(L); - ci->nresults = nresults; - ci->callstatus = CIST_C; - ci->top = L->top + LUA_MINSTACK; - ci->func = func; - lua_assert(ci->top <= L->stack_last); - if (L->hookmask & LUA_MASKCALL) { - int narg = cast_int(L->top - func) - 1; - luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); - } - lua_unlock(L); - n = (*f)(L); /* do the actual call */ - lua_lock(L); - api_checknelems(L, n); - luaD_poscall(L, ci, n); - break; +int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, + int narg1, int delta) { + unsigned status = LUA_MULTRET + 1; + retry: + switch (ttypetag(s2v(func))) { + case LUA_VCCL: /* C closure */ + return precallC(L, func, status, clCvalue(s2v(func))->f); + case LUA_VLCF: /* light C function */ + return precallC(L, func, status, fvalue(s2v(func))); + case LUA_VLCL: { /* Lua function */ + Proto *p = clLvalue(s2v(func))->p; + int fsize = p->maxstacksize; /* frame size */ + int nfixparams = p->numparams; + int i; + checkstackp(L, fsize - delta, func); + ci->func.p -= delta; /* restore 'func' (if vararg) */ + for (i = 0; i < narg1; i++) /* move down function and arguments */ + setobjs2s(L, ci->func.p + i, func + i); + func = ci->func.p; /* moved-down function */ + for (; narg1 <= nfixparams; narg1++) + setnilvalue(s2v(func + narg1)); /* complete missing arguments */ + ci->top.p = func + 1 + fsize; /* top for new function */ + lua_assert(ci->top.p <= L->stack_last.p); + ci->u.l.savedpc = p->code; /* starting point */ + ci->callstatus |= CIST_TAIL; + L->top.p = func + narg1; /* set top */ + return -1; } - case LUA_TLCL: { /* Lua function */ + default: { /* not a function */ + checkstackp(L, 1, func); /* space for metamethod */ + status = tryfuncTM(L, func, status); /* try '__call' metamethod */ + narg1++; + goto retry; /* try again */ + } + } +} + + +/* +** Prepares the call to a function (C or Lua). For C functions, also do +** the call. The function to be called is at '*func'. The arguments +** are on the stack, right after the function. Returns the CallInfo +** to be executed, if it was a Lua function. Otherwise (a C function) +** returns NULL, with all the results on the stack, starting at the +** original function position. +*/ +CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { + unsigned status = cast_uint(nresults + 1); + lua_assert(status <= MAXRESULTS + 1); + retry: + switch (ttypetag(s2v(func))) { + case LUA_VCCL: /* C closure */ + precallC(L, func, status, clCvalue(s2v(func))->f); + return NULL; + case LUA_VLCF: /* light C function */ + precallC(L, func, status, fvalue(s2v(func))); + return NULL; + case LUA_VLCL: { /* Lua function */ CallInfo *ci; - Proto *p = clLvalue(funcv)->p; - int narg = cast_int(L->top - func) - 1; /* number of real arguments */ + Proto *p = clLvalue(s2v(func))->p; + int narg = cast_int(L->top.p - func) - 1; /* number of real arguments */ int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ checkstackp(L, fsize, func); - ci = next_ci(L); - ci->nresults = nresults; + L->ci = ci = prepCallInfo(L, func, status, func + 1 + fsize); ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = 0; - ci->top = func + 1 + fsize; - ci->func = func; for (; narg < nfixparams; narg++) - setnilvalue(s2v(L->top++)); /* complete missing arguments */ - lua_assert(ci->top <= L->stack_last); - luaV_execute(L, ci); /* run the function */ - break; + setnilvalue(s2v(L->top.p++)); /* complete missing arguments */ + lua_assert(ci->top.p <= L->stack_last.p); + return ci; } default: { /* not a function */ - luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ - luaD_call(L, func, nresults); /* now it must be a function */ - break; + checkstackp(L, 1, func); /* space for metamethod */ + status = tryfuncTM(L, func, status); /* try '__call' metamethod */ + goto retry; /* try again with metamethod */ } } } +/* +** Call a function (C or Lua) through C. 'inc' can be 1 (increment +** number of recursive invocations in the C stack) or nyci (the same +** plus increment number of non-yieldable calls). +** This function can be called with some use of EXTRA_STACK, so it should +** check the stack before doing anything else. 'luaD_precall' already +** does that. +*/ +l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) { + CallInfo *ci; + L->nCcalls += inc; + if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) { + checkstackp(L, 0, func); /* free any use of EXTRA_STACK */ + luaE_checkcstack(L); + } + if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ + ci->callstatus |= CIST_FRESH; /* mark that it is a "fresh" execute */ + luaV_execute(L, ci); /* call it */ + } + L->nCcalls -= inc; +} + + +/* +** External interface for 'ccall' +*/ +void luaD_call (lua_State *L, StkId func, int nResults) { + ccall(L, func, nResults, 1); +} + + /* ** Similar to 'luaD_call', but does not allow yields during the call. -** If there is a stack overflow, freeing all CI structures will -** force the subsequent call to invoke 'luaE_extendCI', which then -** will raise any errors. */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { - incXCcalls(L); - if (getCcalls(L) >= LUAI_MAXCCALLS) /* possible stack overflow? */ - luaE_freeCI(L); - luaD_call(L, func, nResults); - decXCcalls(L); + ccall(L, func, nResults, nyci); } /* -** Completes the execution of an interrupted C function, calling its -** continuation function. +** Finish the job of 'lua_pcallk' after it was interrupted by an yield. +** (The caller, 'finishCcall', does the final call to 'adjustresults'.) +** The main job is to complete the 'luaD_pcall' called by 'lua_pcallk'. +** If a '__close' method yields here, eventually control will be back +** to 'finishCcall' (when that '__close' method finally returns) and +** 'finishpcallk' will run again and close any still pending '__close' +** methods. Similarly, if a '__close' method errs, 'precover' calls +** 'unroll' which calls ''finishCcall' and we are back here again, to +** close any pending '__close' methods. +** Note that, up to the call to 'luaF_close', the corresponding +** 'CallInfo' is not modified, so that this repeated run works like the +** first one (except that it has at least one less '__close' to do). In +** particular, field CIST_RECST preserves the error status across these +** multiple runs, changing only if there is a new error. */ -static void finishCcall (lua_State *L, int status) { - CallInfo *ci = L->ci; - int n; - /* must have a continuation and must be able to call it */ - lua_assert(ci->u.c.k != NULL && yieldable(L)); - /* error status can only happen in a protected call */ - lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD); - if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ - ci->callstatus &= ~CIST_YPCALL; /* continuation is also inside it */ - L->errfunc = ci->u.c.old_errfunc; /* with the same error function */ - } - /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already - handled */ - adjustresults(L, ci->nresults); - lua_unlock(L); - n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ - lua_lock(L); - api_checknelems(L, n); +static TStatus finishpcallk (lua_State *L, CallInfo *ci) { + TStatus status = getcistrecst(ci); /* get original status */ + if (l_likely(status == LUA_OK)) /* no error? */ + status = LUA_YIELD; /* was interrupted by an yield */ + else { /* error */ + StkId func = restorestack(L, ci->u2.funcidx); + L->allowhook = getoah(ci); /* restore 'allowhook' */ + func = luaF_close(L, func, status, 1); /* can yield or raise an error */ + luaD_seterrorobj(L, status, func); + luaD_shrinkstack(L); /* restore stack size in case of overflow */ + setcistrecst(ci, LUA_OK); /* clear original status */ + } + ci->callstatus &= ~CIST_YPCALL; + L->errfunc = ci->u.c.old_errfunc; + /* if it is here, there were errors or yields; unlike 'lua_pcallk', + do not change status */ + return status; +} + + +/* +** Completes the execution of a C function interrupted by an yield. +** The interruption must have happened while the function was either +** closing its tbc variables in 'moveresults' or executing +** 'lua_callk'/'lua_pcallk'. In the first case, it just redoes +** 'luaD_poscall'. In the second case, the call to 'finishpcallk' +** finishes the interrupted execution of 'lua_pcallk'. After that, it +** calls the continuation of the interrupted function and finally it +** completes the job of the 'luaD_call' that called the function. In +** the call to 'adjustresults', we do not know the number of results +** of the function called by 'lua_callk'/'lua_pcallk', so we are +** conservative and use LUA_MULTRET (always adjust). +*/ +static void finishCcall (lua_State *L, CallInfo *ci) { + int n; /* actual number of results from C function */ + if (ci->callstatus & CIST_CLSRET) { /* was closing TBC variable? */ + lua_assert(ci->callstatus & CIST_TBC); + n = ci->u2.nres; /* just redo 'luaD_poscall' */ + /* don't need to reset CIST_CLSRET, as it will be set again anyway */ + } + else { + TStatus status = LUA_YIELD; /* default if there were no errors */ + lua_KFunction kf = ci->u.c.k; /* continuation function */ + /* must have a continuation and must be able to call it */ + lua_assert(kf != NULL && yieldable(L)); + if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */ + status = finishpcallk(L, ci); /* finish it */ + adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */ + lua_unlock(L); + n = (*kf)(L, APIstatus(status), ci->u.c.ctx); /* call continuation */ + lua_lock(L); + api_checknelems(L, n); + } luaD_poscall(L, ci, n); /* finish 'luaD_call' */ } @@ -557,18 +861,14 @@ static void finishCcall (lua_State *L, int status) { /* ** Executes "full continuation" (everything in the stack) of a ** previously interrupted coroutine until the stack is empty (or another -** interruption long-jumps out of the loop). If the coroutine is -** recovering from an error, 'ud' points to the error status, which must -** be passed to the first continuation function (otherwise the default -** status is LUA_YIELD). +** interruption long-jumps out of the loop). */ static void unroll (lua_State *L, void *ud) { CallInfo *ci; - if (ud != NULL) /* error status? */ - finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ + UNUSED(ud); while ((ci = L->ci) != &L->base_ci) { /* something in the stack */ if (!isLua(ci)) /* C function? */ - finishCcall(L, LUA_YIELD); /* complete its execution */ + finishCcall(L, ci); /* complete its execution */ else { /* Lua function */ luaV_finishOp(L); /* finish interrupted instruction */ luaV_execute(L, ci); /* execute down to higher C 'boundary' */ @@ -591,36 +891,15 @@ static CallInfo *findpcall (lua_State *L) { } -/* -** Recovers from an error in a coroutine. Finds a recover point (if -** there is one) and completes the execution of the interrupted -** 'luaD_pcall'. If there is no recover point, returns zero. -*/ -static int recover (lua_State *L, int status) { - StkId oldtop; - CallInfo *ci = findpcall(L); - if (ci == NULL) return 0; /* no recovery point */ - /* "finish" luaD_pcall */ - oldtop = restorestack(L, ci->u2.funcidx); - luaF_close(L, oldtop, status); /* may change the stack */ - oldtop = restorestack(L, ci->u2.funcidx); - luaD_seterrorobj(L, status, oldtop); - L->ci = ci; - L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ - luaD_shrinkstack(L); - L->errfunc = ci->u.c.old_errfunc; - return 1; /* continue running the coroutine */ -} - - /* ** Signal an error in the call to 'lua_resume', not in the execution ** of the coroutine itself. (Such errors should not be handled by any ** coroutine error handler and should not kill the coroutine.) */ static int resume_error (lua_State *L, const char *msg, int narg) { - L->top -= narg; /* remove args from the stack */ - setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ + api_checkpop(L, narg); + L->top.p -= narg; /* remove args from the stack */ + setsvalue2s(L, L->top.p, luaS_new(L, msg)); /* push error message */ api_incr_top(L); lua_unlock(L); return LUA_ERRRUN; @@ -636,16 +915,21 @@ static int resume_error (lua_State *L, const char *msg, int narg) { */ static void resume (lua_State *L, void *ud) { int n = *(cast(int*, ud)); /* number of arguments */ - StkId firstArg = L->top - n; /* first argument */ + StkId firstArg = L->top.p - n; /* first argument */ CallInfo *ci = L->ci; - if (L->status == LUA_OK) { /* starting a coroutine? */ - luaD_call(L, firstArg - 1, LUA_MULTRET); - } + if (L->status == LUA_OK) /* starting a coroutine? */ + ccall(L, firstArg - 1, LUA_MULTRET, 0); /* just call its body */ else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ - if (isLua(ci)) /* yielded inside a hook? */ + if (isLua(ci)) { /* yielded inside a hook? */ + /* undo increment made by 'luaG_traceexec': instruction was not + executed yet */ + lua_assert(ci->callstatus & CIST_HOOKYIELD); + ci->u.l.savedpc--; + L->top.p = firstArg; /* discard arguments */ luaV_execute(L, ci); /* just continue running Lua code */ + } else { /* 'common' yield */ if (ci->u.c.k != NULL) { /* does it have a continuation function? */ lua_unlock(L); @@ -659,43 +943,58 @@ static void resume (lua_State *L, void *ud) { } } + +/* +** Unrolls a coroutine in protected mode while there are recoverable +** errors, that is, errors inside a protected call. (Any error +** interrupts 'unroll', and this loop protects it again so it can +** continue.) Stops with a normal end (status == LUA_OK), an yield +** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't +** find a recover point). +*/ +static TStatus precover (lua_State *L, TStatus status) { + CallInfo *ci; + while (errorstatus(status) && (ci = findpcall(L)) != NULL) { + L->ci = ci; /* go down to recovery functions */ + setcistrecst(ci, status); /* status to finish 'pcall' */ + status = luaD_rawrunprotected(L, unroll, NULL); + } + return status; +} + + LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, int *nresults) { - int status; + TStatus status; lua_lock(L); if (L->status == LUA_OK) { /* may be starting a coroutine */ if (L->ci != &L->base_ci) /* not in base level? */ return resume_error(L, "cannot resume non-suspended coroutine", nargs); + else if (L->top.p - (L->ci->func.p + 1) == nargs) /* no function? */ + return resume_error(L, "cannot resume dead coroutine", nargs); } - else if (L->status != LUA_YIELD) + else if (L->status != LUA_YIELD) /* ended with errors? */ return resume_error(L, "cannot resume dead coroutine", nargs); - if (from == NULL) - L->nCcalls = 1; - else /* correct 'nCcalls' for this thread */ - L->nCcalls = getCcalls(from) - from->nci + L->nci + CSTACKCF; - if (L->nCcalls >= LUAI_MAXCCALLS) + L->nCcalls = (from) ? getCcalls(from) : 0; + if (getCcalls(L) >= LUAI_MAXCCALLS) return resume_error(L, "C stack overflow", nargs); + L->nCcalls++; luai_userstateresume(L, nargs); - api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); + api_checkpop(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); /* continue running after recoverable errors */ - while (errorstatus(status) && recover(L, status)) { - /* unroll continuation */ - status = luaD_rawrunprotected(L, unroll, &status); - } - if (likely(!errorstatus(status))) + status = precover(L, status); + if (l_likely(!errorstatus(status))) lua_assert(status == L->status); /* normal end or yield */ else { /* unrecoverable error */ - status = luaF_close(L, L->stack, status); /* close all upvalues */ - L->status = cast_byte(status); /* mark thread as 'dead' */ - luaD_seterrorobj(L, status, L->stack + 1); /* push error message */ - L->ci = &L->base_ci; /* back to the original C level */ - L->ci->top = L->top; + L->status = status; /* mark thread as 'dead' */ + luaD_seterrorobj(L, status, L->top.p); /* push error message */ + L->ci->top.p = L->top.p; } *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield - : cast_int(L->top - (L->ci->func + 1)); + : cast_int(L->top.p - (L->ci->func.p + 1)); lua_unlock(L); - return status; + return APIstatus(status); } @@ -706,26 +1005,27 @@ LUA_API int lua_isyieldable (lua_State *L) { LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, lua_KFunction k) { - CallInfo *ci = L->ci; + CallInfo *ci; luai_userstateyield(L, nresults); lua_lock(L); - api_checknelems(L, nresults); - if (unlikely(!yieldable(L))) { - if (L != G(L)->mainthread) + ci = L->ci; + api_checkpop(L, nresults); + if (l_unlikely(!yieldable(L))) { + if (L != mainthread(G(L))) luaG_runerror(L, "attempt to yield across a C-call boundary"); else luaG_runerror(L, "attempt to yield from outside a coroutine"); } L->status = LUA_YIELD; + ci->u2.nyield = nresults; /* save number of results */ if (isLua(ci)) { /* inside a hook? */ lua_assert(!isLuacode(ci)); + api_check(L, nresults == 0, "hooks cannot yield values"); api_check(L, k == NULL, "hooks cannot continue after yielding"); - ci->u2.nyield = 0; /* no results */ } else { if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ ci->u.c.ctx = ctx; /* save context */ - ci->u2.nyield = nresults; /* save number of results */ luaD_throw(L, LUA_YIELD); } lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ @@ -734,27 +1034,64 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, } +/* +** Auxiliary structure to call 'luaF_close' in protected mode. +*/ +struct CloseP { + StkId level; + TStatus status; +}; + + +/* +** Auxiliary function to call 'luaF_close' in protected mode. +*/ +static void closepaux (lua_State *L, void *ud) { + struct CloseP *pcl = cast(struct CloseP *, ud); + luaF_close(L, pcl->level, pcl->status, 0); +} + + +/* +** Calls 'luaF_close' in protected mode. Return the original status +** or, in case of errors, the new status. +*/ +TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level, TStatus status) { + CallInfo *old_ci = L->ci; + lu_byte old_allowhooks = L->allowhook; + for (;;) { /* keep closing upvalues until no more errors */ + struct CloseP pcl; + pcl.level = restorestack(L, level); pcl.status = status; + status = luaD_rawrunprotected(L, &closepaux, &pcl); + if (l_likely(status == LUA_OK)) /* no more errors? */ + return pcl.status; + else { /* an error occurred; restore saved state and repeat */ + L->ci = old_ci; + L->allowhook = old_allowhooks; + } + } +} + + /* ** Call the C function 'func' in protected mode, restoring basic ** thread information ('allowhook', etc.) and in particular ** its stack level in case of errors. */ -int luaD_pcall (lua_State *L, Pfunc func, void *u, - ptrdiff_t old_top, ptrdiff_t ef) { - int status; +TStatus luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top, + ptrdiff_t ef) { + TStatus status; CallInfo *old_ci = L->ci; lu_byte old_allowhooks = L->allowhook; ptrdiff_t old_errfunc = L->errfunc; L->errfunc = ef; status = luaD_rawrunprotected(L, func, u); - if (unlikely(status != LUA_OK)) { /* an error occurred? */ - StkId oldtop = restorestack(L, old_top); + if (l_unlikely(status != LUA_OK)) { /* an error occurred? */ L->ci = old_ci; L->allowhook = old_allowhooks; - status = luaF_close(L, oldtop, status); - oldtop = restorestack(L, old_top); /* previous call may change stack */ - luaD_seterrorobj(L, status, oldtop); - luaD_shrinkstack(L); + status = luaD_closeprotected(L, old_top, status); + luaD_seterrorobj(L, status, restorestack(L, old_top)); + luaD_shrinkstack(L); /* restore stack size in case of overflow */ } L->errfunc = old_errfunc; return status; @@ -775,7 +1112,7 @@ struct SParser { /* data to 'f_parser' */ static void checkmode (lua_State *L, const char *mode, const char *x) { - if (mode && strchr(mode, x[0]) == NULL) { + if (strchr(mode, x[0]) == NULL) { luaO_pushfstring(L, "attempt to load a %s chunk (mode is '%s')", x, mode); luaD_throw(L, LUA_ERRSYNTAX); @@ -786,13 +1123,18 @@ static void checkmode (lua_State *L, const char *mode, const char *x) { static void f_parser (lua_State *L, void *ud) { LClosure *cl; struct SParser *p = cast(struct SParser *, ud); + const char *mode = p->mode ? p->mode : "bt"; int c = zgetc(p->z); /* read first character */ if (c == LUA_SIGNATURE[0]) { - checkmode(L, p->mode, "binary"); - cl = luaU_undump(L, p->z, p->name); + int fixed = 0; + if (strchr(mode, 'B') != NULL) + fixed = 1; + else + checkmode(L, mode, "binary"); + cl = luaU_undump(L, p->z, p->name, fixed); } else { - checkmode(L, p->mode, "text"); + checkmode(L, mode, "text"); cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); } lua_assert(cl->nupvalues == cl->p->sizeupvalues); @@ -800,21 +1142,21 @@ static void f_parser (lua_State *L, void *ud) { } -int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, - const char *mode) { +TStatus luaD_protectedparser (lua_State *L, ZIO *z, const char *name, + const char *mode) { struct SParser p; - int status; + TStatus status; incnny(L); /* cannot yield during parsing */ p.z = z; p.name = name; p.mode = mode; p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; p.dyd.label.arr = NULL; p.dyd.label.size = 0; luaZ_initbuffer(L, &p.buff); - status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); + status = luaD_pcall(L, f_parser, &p, savestack(L, L->top.p), L->errfunc); luaZ_freebuffer(L, &p.buff); - luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); - luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); - luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); + luaM_freearray(L, p.dyd.actvar.arr, cast_sizet(p.dyd.actvar.size)); + luaM_freearray(L, p.dyd.gt.arr, cast_sizet(p.dyd.gt.size)); + luaM_freearray(L, p.dyd.label.arr, cast_sizet(p.dyd.label.size)); decnny(L); return status; } diff --git a/ldo.h b/ldo.h index 7760f853b2..b64729541c 100644 --- a/ldo.h +++ b/ldo.h @@ -8,6 +8,7 @@ #define ldo_h +#include "llimits.h" #include "lobject.h" #include "lstate.h" #include "lzio.h" @@ -17,59 +18,82 @@ ** Macro to check stack size and grow stack if needed. Parameters ** 'pre'/'pos' allow the macro to preserve a pointer into the ** stack across reallocations, doing the work only when needed. +** It also allows the running of one GC step when the stack is +** reallocated. ** 'condmovestack' is used in heavy tests to force a stack reallocation ** at every check. */ + +#if !defined(HARDSTACKTESTS) +#define condmovestack(L,pre,pos) ((void)0) +#else +/* realloc stack keeping its size */ +#define condmovestack(L,pre,pos) \ + { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; } +#endif + #define luaD_checkstackaux(L,n,pre,pos) \ - if (L->stack_last - L->top <= (n)) \ + if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \ { pre; luaD_growstack(L, n, 1); pos; } \ - else { condmovestack(L,pre,pos); } + else { condmovestack(L,pre,pos); } /* In general, 'pre'/'pos' are empty (nothing to save) */ #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) -#define savestack(L,p) ((char *)(p) - (char *)L->stack) -#define restorestack(L,n) ((StkId)((char *)L->stack + (n))) +#define savestack(L,pt) (cast_charp(pt) - cast_charp(L->stack.p)) +#define restorestack(L,n) cast(StkId, cast_charp(L->stack.p) + (n)) /* macro to check stack size, preserving 'p' */ #define checkstackp(L,n,p) \ luaD_checkstackaux(L, n, \ - ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ - luaC_checkGC(L), /* stack grow uses memory */ \ + ptrdiff_t t__ = savestack(L, p), /* save 'p' */ \ p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ -/* macro to check stack size and GC */ -#define checkstackGC(L,fsize) \ - luaD_checkstackaux(L, (fsize), (void)0, luaC_checkGC(L)) +/* +** Maximum depth for nested C calls, syntactical nested non-terminals, +** and other features implemented through recursion in C. (Value must +** fit in a 16-bit unsigned integer. It must also be compatible with +** the size of the C stack.) +*/ +#if !defined(LUAI_MAXCCALLS) +#define LUAI_MAXCCALLS 200 +#endif /* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); -LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); -LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, +LUAI_FUNC l_noret luaD_errerr (lua_State *L); +LUAI_FUNC void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop); +LUAI_FUNC TStatus luaD_protectedparser (lua_State *L, ZIO *z, + const char *name, const char *mode); LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); -LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); +LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, + int narg1, int delta); +LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); -LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); -LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, +LUAI_FUNC TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level, + TStatus status); +LUAI_FUNC TStatus luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres); LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); LUAI_FUNC void luaD_shrinkstack (lua_State *L); LUAI_FUNC void luaD_inctop (lua_State *L); +LUAI_FUNC int luaD_checkminstack (lua_State *L); -LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); -LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); +LUAI_FUNC l_noret luaD_throw (lua_State *L, TStatus errcode); +LUAI_FUNC l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode); +LUAI_FUNC TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); #endif diff --git a/ldump.c b/ldump.c index aca6245bf4..5795788922 100644 --- a/ldump.c +++ b/ldump.c @@ -10,12 +10,16 @@ #include "lprefix.h" +#include #include #include "lua.h" +#include "lapi.h" +#include "lgc.h" #include "lobject.h" #include "lstate.h" +#include "ltable.h" #include "lundump.h" @@ -23,208 +27,281 @@ typedef struct { lua_State *L; lua_Writer writer; void *data; + size_t offset; /* current position relative to beginning of dump */ int strip; int status; + Table *h; /* table to track saved strings */ + lua_Unsigned nstr; /* counter for counting saved strings */ } DumpState; /* -** All high-level dumps go through DumpVector; you can change it to +** All high-level dumps go through dumpVector; you can change it to ** change the endianness of the result */ -#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) +#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0])) -#define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D) +#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char)) -static void DumpBlock (const void *b, size_t size, DumpState *D) { - if (D->status == 0 && size > 0) { +/* +** Dump the block of memory pointed by 'b' with given 'size'. +** 'b' should not be NULL, except for the last call signaling the end +** of the dump. +*/ +static void dumpBlock (DumpState *D, const void *b, size_t size) { + if (D->status == 0) { /* do not write anything after an error */ lua_unlock(D->L); D->status = (*D->writer)(D->L, b, size, D->data); lua_lock(D->L); + D->offset += size; + } +} + + +/* +** Dump enough zeros to ensure that current position is a multiple of +** 'align'. +*/ +static void dumpAlign (DumpState *D, unsigned align) { + unsigned padding = align - cast_uint(D->offset % align); + if (padding < align) { /* padding == align means no padding */ + static lua_Integer paddingContent = 0; + lua_assert(align <= sizeof(lua_Integer)); + dumpBlock(D, &paddingContent, padding); } + lua_assert(D->offset % align == 0); } -#define DumpVar(x,D) DumpVector(&x,1,D) +#define dumpVar(D,x) dumpVector(D,&x,1) -static void DumpByte (int y, DumpState *D) { +static void dumpByte (DumpState *D, int y) { lu_byte x = (lu_byte)y; - DumpVar(x, D); + dumpVar(D, x); } -/* DumpInt Buff Size */ -#define DIBS ((sizeof(size_t) * 8 / 7) + 1) +/* +** size for 'dumpVarint' buffer: each byte can store up to 7 bits. +** (The "+6" rounds up the division.) +*/ +#define DIBS ((l_numbits(lua_Unsigned) + 6) / 7) -static void DumpSize (size_t x, DumpState *D) { +/* +** Dumps an unsigned integer using the MSB Varint encoding +*/ +static void dumpVarint (DumpState *D, lua_Unsigned x) { lu_byte buff[DIBS]; - int n = 0; - do { - buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */ - x >>= 7; - } while (x != 0); - buff[DIBS - 1] |= 0x80; /* mark last byte */ - DumpVector(buff + DIBS - n, n, D); + unsigned n = 1; + buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ + while ((x >>= 7) != 0) /* fill other bytes in reverse order */ + buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80); + dumpVector(D, buff + DIBS - n, n); } -static void DumpInt (int x, DumpState *D) { - DumpSize(x, D); +static void dumpSize (DumpState *D, size_t sz) { + dumpVarint(D, cast(lua_Unsigned, sz)); } -static void DumpNumber (lua_Number x, DumpState *D) { - DumpVar(x, D); +static void dumpInt (DumpState *D, int x) { + lua_assert(x >= 0); + dumpVarint(D, cast_uint(x)); } -static void DumpInteger (lua_Integer x, DumpState *D) { - DumpVar(x, D); +static void dumpNumber (DumpState *D, lua_Number x) { + dumpVar(D, x); } -static void DumpString (const TString *s, DumpState *D) { - if (s == NULL) - DumpSize(0, D); +/* +** Signed integers are coded to keep small values small. (Coding -1 as +** 0xfff...fff would use too many bytes to save a quite common value.) +** A non-negative x is coded as 2x; a negative x is coded as -2x - 1. +** (0 => 0; -1 => 1; 1 => 2; -2 => 3; 2 => 4; ...) +*/ +static void dumpInteger (DumpState *D, lua_Integer x) { + lua_Unsigned cx = (x >= 0) ? 2u * l_castS2U(x) + : (2u * ~l_castS2U(x)) + 1; + dumpVarint(D, cx); +} + + +/* +** Dump a String. First dump its "size": +** size==0 is followed by an index and means "reuse saved string with +** that index"; index==0 means NULL. +** size>=1 is followed by the string contents with real size==size-1 and +** means that string, which will be saved with the next available index. +** The real size does not include the ending '\0' (which is not dumped), +** so adding 1 to it cannot overflow a size_t. +*/ +static void dumpString (DumpState *D, TString *ts) { + if (ts == NULL) { + dumpVarint(D, 0); /* will "reuse" NULL */ + dumpVarint(D, 0); /* special index for NULL */ + } else { - size_t size = tsslen(s); - const char *str = getstr(s); - DumpSize(size + 1, D); - DumpVector(str, size, D); + TValue idx; + int tag = luaH_getstr(D->h, ts, &idx); + if (!tagisempty(tag)) { /* string already saved? */ + dumpVarint(D, 0); /* reuse a saved string */ + dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */ + } + else { /* must write and save the string */ + TValue key, value; /* to save the string in the hash */ + size_t size; + const char *s = getlstr(ts, size); + dumpSize(D, size + 1); + dumpVector(D, s, size + 1); /* include ending '\0' */ + D->nstr++; /* one more saved string */ + setsvalue(D->L, &key, ts); /* the string is the key */ + setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */ + luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ + /* integer value does not need barrier */ + } } } -static void DumpCode (const Proto *f, DumpState *D) { - DumpInt(f->sizecode, D); - DumpVector(f->code, f->sizecode, D); +static void dumpCode (DumpState *D, const Proto *f) { + dumpInt(D, f->sizecode); + dumpAlign(D, sizeof(f->code[0])); + lua_assert(f->code != NULL); + dumpVector(D, f->code, cast_uint(f->sizecode)); } -static void DumpFunction(const Proto *f, TString *psource, DumpState *D); +static void dumpFunction (DumpState *D, const Proto *f); -static void DumpConstants (const Proto *f, DumpState *D) { +static void dumpConstants (DumpState *D, const Proto *f) { int i; int n = f->sizek; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) { const TValue *o = &f->k[i]; - DumpByte(ttypetag(o), D); - switch (ttypetag(o)) { - case LUA_TNIL: - break; - case LUA_TBOOLEAN: - DumpByte(bvalue(o), D); - break; - case LUA_TNUMFLT: - DumpNumber(fltvalue(o), D); + int tt = ttypetag(o); + dumpByte(D, tt); + switch (tt) { + case LUA_VNUMFLT: + dumpNumber(D, fltvalue(o)); break; - case LUA_TNUMINT: - DumpInteger(ivalue(o), D); + case LUA_VNUMINT: + dumpInteger(D, ivalue(o)); break; - case LUA_TSHRSTR: - case LUA_TLNGSTR: - DumpString(tsvalue(o), D); + case LUA_VSHRSTR: + case LUA_VLNGSTR: + dumpString(D, tsvalue(o)); break; - default: lua_assert(0); + default: + lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE); } } } -static void DumpProtos (const Proto *f, DumpState *D) { +static void dumpProtos (DumpState *D, const Proto *f) { int i; int n = f->sizep; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) - DumpFunction(f->p[i], f->source, D); + dumpFunction(D, f->p[i]); } -static void DumpUpvalues (const Proto *f, DumpState *D) { +static void dumpUpvalues (DumpState *D, const Proto *f) { int i, n = f->sizeupvalues; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) { - DumpByte(f->upvalues[i].instack, D); - DumpByte(f->upvalues[i].idx, D); + dumpByte(D, f->upvalues[i].instack); + dumpByte(D, f->upvalues[i].idx); + dumpByte(D, f->upvalues[i].kind); } } -static void DumpDebug (const Proto *f, DumpState *D) { +static void dumpDebug (DumpState *D, const Proto *f) { int i, n; n = (D->strip) ? 0 : f->sizelineinfo; - DumpInt(n, D); - DumpVector(f->lineinfo, n, D); + dumpInt(D, n); + if (f->lineinfo != NULL) + dumpVector(D, f->lineinfo, cast_uint(n)); n = (D->strip) ? 0 : f->sizeabslineinfo; - DumpInt(n, D); - for (i = 0; i < n; i++) { - DumpInt(f->abslineinfo[i].pc, D); - DumpInt(f->abslineinfo[i].line, D); + dumpInt(D, n); + if (n > 0) { + /* 'abslineinfo' is an array of structures of int's */ + dumpAlign(D, sizeof(int)); + dumpVector(D, f->abslineinfo, cast_uint(n)); } n = (D->strip) ? 0 : f->sizelocvars; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) { - DumpString(f->locvars[i].varname, D); - DumpInt(f->locvars[i].startpc, D); - DumpInt(f->locvars[i].endpc, D); + dumpString(D, f->locvars[i].varname); + dumpInt(D, f->locvars[i].startpc); + dumpInt(D, f->locvars[i].endpc); } n = (D->strip) ? 0 : f->sizeupvalues; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) - DumpString(f->upvalues[i].name, D); + dumpString(D, f->upvalues[i].name); } -static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { - if (D->strip || f->source == psource) - DumpString(NULL, D); /* no debug info or same source as its parent */ - else - DumpString(f->source, D); - DumpInt(f->linedefined, D); - DumpInt(f->lastlinedefined, D); - DumpByte(f->numparams, D); - DumpByte(f->is_vararg, D); - DumpByte(f->maxstacksize, D); - DumpCode(f, D); - DumpConstants(f, D); - DumpUpvalues(f, D); - DumpProtos(f, D); - DumpDebug(f, D); +static void dumpFunction (DumpState *D, const Proto *f) { + dumpInt(D, f->linedefined); + dumpInt(D, f->lastlinedefined); + dumpByte(D, f->numparams); + dumpByte(D, f->flag); + dumpByte(D, f->maxstacksize); + dumpCode(D, f); + dumpConstants(D, f); + dumpUpvalues(D, f); + dumpProtos(D, f); + dumpString(D, D->strip ? NULL : f->source); + dumpDebug(D, f); } -static void DumpHeader (DumpState *D) { - DumpLiteral(LUA_SIGNATURE, D); - DumpByte(LUAC_VERSION, D); - DumpByte(LUAC_FORMAT, D); - DumpLiteral(LUAC_DATA, D); - DumpByte(sizeof(int), D); - DumpByte(sizeof(size_t), D); - DumpByte(sizeof(Instruction), D); - DumpByte(sizeof(lua_Integer), D); - DumpByte(sizeof(lua_Number), D); - DumpInteger(LUAC_INT, D); - DumpNumber(LUAC_NUM, D); +#define dumpNumInfo(D, tvar, value) \ + { tvar i = value; dumpByte(D, sizeof(tvar)); dumpVar(D, i); } + + +static void dumpHeader (DumpState *D) { + dumpLiteral(D, LUA_SIGNATURE); + dumpByte(D, LUAC_VERSION); + dumpByte(D, LUAC_FORMAT); + dumpLiteral(D, LUAC_DATA); + dumpNumInfo(D, int, LUAC_INT); + dumpNumInfo(D, Instruction, LUAC_INST); + dumpNumInfo(D, lua_Integer, LUAC_INT); + dumpNumInfo(D, lua_Number, LUAC_NUM); } /* ** dump Lua function as precompiled chunk */ -int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, - int strip) { +int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data, + int strip) { DumpState D; + D.h = luaH_new(L); /* aux. table to keep strings already dumped */ + sethvalue2s(L, L->top.p, D.h); /* anchor it */ + L->top.p++; D.L = L; D.writer = w; + D.offset = 0; D.data = data; D.strip = strip; D.status = 0; - DumpHeader(&D); - DumpByte(f->sizeupvalues, &D); - DumpFunction(f, NULL, &D); + D.nstr = 0; + dumpHeader(&D); + dumpByte(&D, f->sizeupvalues); + dumpFunction(&D, f); + dumpBlock(&D, NULL, 0); /* signal end of dump */ return D.status; } diff --git a/lfunc.c b/lfunc.c index 3e044b65b2..b6fd9ceb55 100644 --- a/lfunc.c +++ b/lfunc.c @@ -24,20 +24,20 @@ -CClosure *luaF_newCclosure (lua_State *L, int n) { - GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); +CClosure *luaF_newCclosure (lua_State *L, int nupvals) { + GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals)); CClosure *c = gco2ccl(o); - c->nupvalues = cast_byte(n); + c->nupvalues = cast_byte(nupvals); return c; } -LClosure *luaF_newLclosure (lua_State *L, int n) { - GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); +LClosure *luaF_newLclosure (lua_State *L, int nupvals) { + GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals)); LClosure *c = gco2lcl(o); c->p = NULL; - c->nupvalues = cast_byte(n); - while (n--) c->upvals[n] = NULL; + c->nupvalues = cast_byte(nupvals); + while (nupvals--) c->upvals[nupvals] = NULL; return c; } @@ -48,25 +48,25 @@ LClosure *luaF_newLclosure (lua_State *L, int n) { void luaF_initupvals (lua_State *L, LClosure *cl) { int i; for (i = 0; i < cl->nupvalues; i++) { - GCObject *o = luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal)); + GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); UpVal *uv = gco2upv(o); - uv->v = &uv->u.value; /* make it closed */ - setnilvalue(uv->v); + uv->v.p = &uv->u.value; /* make it closed */ + setnilvalue(uv->v.p); cl->upvals[i] = uv; - luaC_objbarrier(L, cl, o); + luaC_objbarrier(L, cl, uv); } } /* -** Create a new upvalue with the given tag at the given level, -** and link it to the list of open upvalues of 'L' after entry 'prev'. +** Create a new upvalue at the given level, and link it to the list of +** open upvalues of 'L' after entry 'prev'. **/ -static UpVal *newupval (lua_State *L, int tag, StkId level, UpVal **prev) { - GCObject *o = luaC_newobj(L, tag, sizeof(UpVal)); +static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) { + GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); UpVal *uv = gco2upv(o); UpVal *next = *prev; - uv->v = s2v(level); /* current value lives in the stack */ + uv->v.p = s2v(level); /* current value lives in the stack */ uv->u.open.next = next; /* link it to list of open upvalues */ uv->u.open.previous = prev; if (next) @@ -81,7 +81,7 @@ static UpVal *newupval (lua_State *L, int tag, StkId level, UpVal **prev) { /* -** Find and reuse, or create if it does not exist, a regular upvalue +** Find and reuse, or create if it does not exist, an upvalue ** at the given level. */ UpVal *luaF_findupval (lua_State *L, StkId level) { @@ -89,100 +89,97 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { UpVal *p; lua_assert(isintwups(L) || L->openupval == NULL); while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */ - if (uplevel(p) == level && !isdead(G(L), p)) /* corresponding upvalue? */ + lua_assert(!isdead(G(L), p)); + if (uplevel(p) == level) /* corresponding upvalue? */ return p; /* return it */ pp = &p->u.open.next; } /* not found: create a new upvalue after 'pp' */ - return newupval(L, LUA_TUPVAL, level, pp); -} - - -static void callclose (lua_State *L, void *ud) { - UNUSED(ud); - luaD_callnoyield(L, L->top - 3, 0); + return newupval(L, level, pp); } /* -** Prepare closing method plus its arguments for object 'obj' with -** error message 'err'. (This function assumes EXTRA_STACK.) +** Call closing method for object 'obj' with error object 'err'. The +** boolean 'yy' controls whether the call is yieldable. +** (This function assumes EXTRA_STACK.) */ -static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) { - StkId top = L->top; +static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { + StkId top = L->top.p; + StkId func = top; const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); - if (ttisnil(tm)) /* no metamethod? */ - return 0; /* nothing to call */ - setobj2s(L, top, tm); /* will call metamethod... */ - setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ - setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ - L->top = top + 3; /* add function and arguments */ - return 1; + setobj2s(L, top++, tm); /* will call metamethod... */ + setobj2s(L, top++, obj); /* with 'self' as the 1st argument */ + if (err != NULL) /* if there was an error... */ + setobj2s(L, top++, err); /* then error object will be 2nd argument */ + L->top.p = top; /* add function and arguments */ + if (yy) + luaD_call(L, func, 0); + else + luaD_callnoyield(L, func, 0); } /* -** Prepare and call a closing method. If status is OK, code is still -** inside the original protected call, and so any error will be handled -** there. Otherwise, a previous error already activated original -** protected call, and so the call to the closing method must be -** protected here. (A status = CLOSEPROTECT behaves like a previous -** error, to also run the closing method in protected mode). -** If status is OK, the call to the closing method will be pushed -** at the top of the stack. Otherwise, values are pushed after -** the 'level' of the upvalue being closed, as everything after -** that won't be used again. +** Check whether object at given level has a close metamethod and raise +** an error if not. */ -static int callclosemth (lua_State *L, TValue *uv, StkId level, int status) { - if (likely(status == LUA_OK)) { - if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */ - callclose(L, NULL); /* call closing method */ - else if (!ttisnil(uv)) { /* non-closable non-nil value? */ - int idx = cast_int(level - L->ci->func); - const char *vname = luaG_findlocal(L, L->ci, idx, NULL); - if (vname == NULL) vname = "?"; - luaG_runerror(L, "attempt to close non-closable variable '%s'", vname); - } +static void checkclosemth (lua_State *L, StkId level) { + const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE); + if (ttisnil(tm)) { /* no metamethod? */ + int idx = cast_int(level - L->ci->func.p); /* variable index */ + const char *vname = luaG_findlocal(L, L->ci, idx, NULL); + if (vname == NULL) vname = "?"; + luaG_runerror(L, "variable '%s' got a non-closable value", vname); } - else { /* there was an error */ - /* save error message and set stack top to 'level + 1' */ - luaD_seterrorobj(L, status, level); - if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */ - int newstatus = luaD_pcall(L, callclose, NULL, savestack(L, level), 0); - if (newstatus != LUA_OK) /* another error when closing? */ - status = newstatus; /* this will be the new error */ - } - /* else no metamethod; ignore this case and keep original error */ - } - return status; } /* -** Try to create a to-be-closed upvalue -** (can raise a memory-allocation error) +** Prepare and call a closing method. +** If status is CLOSEKTOP, the call to the closing method will be pushed +** at the top of the stack. Otherwise, values can be pushed right after +** the 'level' of the upvalue being closed, as everything after that +** won't be used again. */ -static void trynewtbcupval (lua_State *L, void *ud) { - StkId level = cast(StkId, ud); - lua_assert(L->openupval == NULL || uplevel(L->openupval) < level); - newupval(L, LUA_TUPVALTBC, level, &L->openupval); +static void prepcallclosemth (lua_State *L, StkId level, TStatus status, + int yy) { + TValue *uv = s2v(level); /* value being closed */ + TValue *errobj; + switch (status) { + case LUA_OK: + L->top.p = level + 1; /* call will be at this level */ + /* FALLTHROUGH */ + case CLOSEKTOP: /* don't need to change top */ + errobj = NULL; /* no error object */ + break; + default: /* 'luaD_seterrorobj' will set top to level + 2 */ + errobj = s2v(level + 1); /* error object goes after 'uv' */ + luaD_seterrorobj(L, status, level + 1); /* set error object */ + break; + } + callclosemethod(L, uv, errobj, yy); } +/* Maximum value for deltas in 'tbclist' */ +#define MAXDELTA USHRT_MAX + + /* -** Create a to-be-closed upvalue. If there is a memory error -** when creating the upvalue, the closing method must be called here, -** as there is no upvalue to call it later. +** Insert a variable in the list of to-be-closed variables. */ void luaF_newtbcupval (lua_State *L, StkId level) { - int status = luaD_rawrunprotected(L, trynewtbcupval, level); - if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */ - lua_assert(status == LUA_ERRMEM); - luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */ - if (prepclosingmethod(L, s2v(level), s2v(level + 1))) - callclose(L, NULL); /* call closing method */ - luaD_throw(L, LUA_ERRMEM); /* throw memory error */ + lua_assert(level > L->tbclist.p); + if (l_isfalse(s2v(level))) + return; /* false doesn't need to be closed */ + checkclosemth(L, level); /* value must have a close method */ + while (cast_uint(level - L->tbclist.p) > MAXDELTA) { + L->tbclist.p += MAXDELTA; /* create a dummy node at maximum delta */ + L->tbclist.p->tbclist.delta = 0; } + level->tbclist.delta = cast(unsigned short, level - L->tbclist.p); + L->tbclist.p = level; } @@ -194,30 +191,57 @@ void luaF_unlinkupval (UpVal *uv) { } -int luaF_close (lua_State *L, StkId level, int status) { +/* +** Close all upvalues up to the given stack level. +*/ +void luaF_closeupval (lua_State *L, StkId level) { UpVal *uv; while ((uv = L->openupval) != NULL && uplevel(uv) >= level) { - StkId upl = uplevel(uv); TValue *slot = &uv->u.value; /* new position for value */ - luaF_unlinkupval(uv); - setobj(L, slot, uv->v); /* move value to upvalue slot */ - uv->v = slot; /* now current value lives here */ - if (!iswhite(uv)) - gray2black(uv); /* closed upvalues cannot be gray */ - luaC_barrier(L, uv, slot); - if (uv->tt == LUA_TUPVALTBC && status != NOCLOSINGMETH) { - /* must run closing method */ - ptrdiff_t levelrel = savestack(L, level); - status = callclosemth(L, uv->v, upl, status); /* may change the stack */ - level = restorestack(L, levelrel); + lua_assert(uplevel(uv) < L->top.p); + luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */ + setobj(L, slot, uv->v.p); /* move value to upvalue slot */ + uv->v.p = slot; /* now current value lives here */ + if (!iswhite(uv)) { /* neither white nor dead? */ + nw2black(uv); /* closed upvalues cannot be gray */ + luaC_barrier(L, uv, slot); } } - return status; +} + + +/* +** Remove first element from the tbclist plus its dummy nodes. +*/ +static void poptbclist (lua_State *L) { + StkId tbc = L->tbclist.p; + lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */ + tbc -= tbc->tbclist.delta; + while (tbc > L->stack.p && tbc->tbclist.delta == 0) + tbc -= MAXDELTA; /* remove dummy nodes */ + L->tbclist.p = tbc; +} + + +/* +** Close all upvalues and to-be-closed variables up to the given stack +** level. Return restored 'level'. +*/ +StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy) { + ptrdiff_t levelrel = savestack(L, level); + luaF_closeupval(L, level); /* first, close the upvalues */ + while (L->tbclist.p >= level) { /* traverse tbc's down to that level */ + StkId tbc = L->tbclist.p; /* get variable index */ + poptbclist(L); /* remove it from list */ + prepcallclosemth(L, tbc, status, yy); /* close variable */ + level = restorestack(L, levelrel); + } + return level; } Proto *luaF_newproto (lua_State *L) { - GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); + GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto)); Proto *f = gco2p(o); f->k = NULL; f->sizek = 0; @@ -232,7 +256,7 @@ Proto *luaF_newproto (lua_State *L) { f->upvalues = NULL; f->sizeupvalues = 0; f->numparams = 0; - f->is_vararg = 0; + f->flag = 0; f->maxstacksize = 0; f->locvars = NULL; f->sizelocvars = 0; @@ -243,14 +267,31 @@ Proto *luaF_newproto (lua_State *L) { } +lu_mem luaF_protosize (Proto *p) { + lu_mem sz = cast(lu_mem, sizeof(Proto)) + + cast_uint(p->sizep) * sizeof(Proto*) + + cast_uint(p->sizek) * sizeof(TValue) + + cast_uint(p->sizelocvars) * sizeof(LocVar) + + cast_uint(p->sizeupvalues) * sizeof(Upvaldesc); + if (!(p->flag & PF_FIXED)) { + sz += cast_uint(p->sizecode) * sizeof(Instruction); + sz += cast_uint(p->sizelineinfo) * sizeof(lu_byte); + sz += cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo); + } + return sz; +} + + void luaF_freeproto (lua_State *L, Proto *f) { - luaM_freearray(L, f->code, f->sizecode); - luaM_freearray(L, f->p, f->sizep); - luaM_freearray(L, f->k, f->sizek); - luaM_freearray(L, f->lineinfo, f->sizelineinfo); - luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo); - luaM_freearray(L, f->locvars, f->sizelocvars); - luaM_freearray(L, f->upvalues, f->sizeupvalues); + if (!(f->flag & PF_FIXED)) { + luaM_freearray(L, f->code, cast_sizet(f->sizecode)); + luaM_freearray(L, f->lineinfo, cast_sizet(f->sizelineinfo)); + luaM_freearray(L, f->abslineinfo, cast_sizet(f->sizeabslineinfo)); + } + luaM_freearray(L, f->p, cast_sizet(f->sizep)); + luaM_freearray(L, f->k, cast_sizet(f->sizek)); + luaM_freearray(L, f->locvars, cast_sizet(f->sizelocvars)); + luaM_freearray(L, f->upvalues, cast_sizet(f->sizeupvalues)); luaM_free(L, f); } diff --git a/lfunc.h b/lfunc.h index 0ed79c48ab..d6aad3a6df 100644 --- a/lfunc.h +++ b/lfunc.h @@ -11,11 +11,11 @@ #include "lobject.h" -#define sizeCclosure(n) (cast_int(offsetof(CClosure, upvalue)) + \ - cast_int(sizeof(TValue)) * (n)) +#define sizeCclosure(n) \ + (offsetof(CClosure, upvalue) + sizeof(TValue) * cast_uint(n)) -#define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \ - cast_int(sizeof(TValue *)) * (n)) +#define sizeLclosure(n) \ + (offsetof(LClosure, upvals) + sizeof(UpVal *) * cast_uint(n)) /* test whether thread is in 'twups' list */ @@ -29,10 +29,10 @@ #define MAXUPVAL 255 -#define upisopen(up) ((up)->v != &(up)->u.value) +#define upisopen(up) ((up)->v.p != &(up)->u.value) -#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v)) +#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v.p)) /* @@ -42,25 +42,21 @@ #define MAXMISS 10 -/* -** Special "status" for 'luaF_close' -*/ - -/* close upvalues without running their closing methods */ -#define NOCLOSINGMETH (-1) -/* close upvalues running all closing methods in protected mode */ -#define CLOSEPROTECT (-2) +/* special status to close upvalues preserving the top of the stack */ +#define CLOSEKTOP (LUA_ERRERR + 1) LUAI_FUNC Proto *luaF_newproto (lua_State *L); -LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); -LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); +LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals); +LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); -LUAI_FUNC int luaF_close (lua_State *L, StkId level, int status); +LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); +LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); +LUAI_FUNC lu_mem luaF_protosize (Proto *p); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, int pc); diff --git a/lgc.c b/lgc.c index 8cb3e9fad5..c64d74b8e8 100644 --- a/lgc.c +++ b/lgc.c @@ -9,7 +9,6 @@ #include "lprefix.h" -#include #include @@ -32,44 +31,33 @@ ** (Large enough to dissipate fixed overheads but small enough ** to allow small steps for the collector.) */ -#define GCSWEEPMAX 100 - -/* -** Maximum number of finalizers to call in each single step. -*/ -#define GCFINMAX 10 +#define GCSWEEPMAX 20 /* -** Cost of calling one finalizer. +** Cost (in work units) of running one finalizer. */ -#define GCFINALIZECOST 50 - +#define CWUFIN 10 -/* -** The equivalent, in bytes, of one unit of "work" (visiting a slot, -** sweeping an object, etc.) -*/ -#define WORK2MEM sizeof(TValue) +/* mask with all color bits */ +#define maskcolors (bitmask(BLACKBIT) | WHITEBITS) -/* -** macro to adjust 'pause': 'pause' is actually used like -** 'pause / PAUSEADJ' (value chosen by tests) -*/ -#define PAUSEADJ 100 +/* mask with all GC bits */ +#define maskgcbits (maskcolors | AGEBITS) -/* mask to erase all color bits (plus gen. related stuff) */ -#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS | AGEBITS)) +/* macro to erase all color bits then set only the current white bit */ +#define makewhite(g,x) \ + (x->marked = cast_byte((x->marked & ~maskcolors) | luaC_white(g))) +/* make an object gray (neither white nor black) */ +#define set2gray(x) resetbits(x->marked, maskcolors) -/* macro to erase all color bits then sets only the current white bit */ -#define makewhite(g,x) \ - (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g))) -#define white2gray(x) resetbits(x->marked, WHITEBITS) -#define black2gray(x) resetbit(x->marked, BLACKBIT) +/* make an object black (coming from any color) */ +#define set2black(x) \ + (x->marked = cast_byte((x->marked & ~WHITEBITS) | bitmask(BLACKBIT))) #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) @@ -77,16 +65,20 @@ #define keyiswhite(n) (keyiscollectable(n) && iswhite(gckey(n))) -#define checkconsistency(obj) \ - lua_longassert(!iscollectable(obj) || righttt(obj)) - /* ** Protected access to objects in values */ #define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL) -#define markvalue(g,o) { checkconsistency(o); \ +/* +** Access to collectable objects in array part of tables +*/ +#define gcvalarr(t,i) \ + ((*getArrTag(t,i) & BIT_ISCOLLECTABLE) ? getArrVal(t,i)->gc : NULL) + + +#define markvalue(g,o) { checkliveness(mainthread(g),o); \ if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } #define markkey(g, n) { if keyiswhite(n) reallymarkobject(g,gckey(n)); } @@ -99,8 +91,9 @@ */ #define markobjectN(g,t) { if (t) markobject(g,t); } + static void reallymarkobject (global_State *g, GCObject *o); -static lu_mem atomic (lua_State *L); +static void atomic (lua_State *L); static void entersweep (lua_State *L); @@ -117,14 +110,64 @@ static void entersweep (lua_State *L); #define gnodelast(h) gnode(h, cast_sizet(sizenode(h))) +static l_mem objsize (GCObject *o) { + lu_mem res; + switch (o->tt) { + case LUA_VTABLE: { + res = luaH_size(gco2t(o)); + break; + } + case LUA_VLCL: { + LClosure *cl = gco2lcl(o); + res = sizeLclosure(cl->nupvalues); + break; + } + case LUA_VCCL: { + CClosure *cl = gco2ccl(o); + res = sizeCclosure(cl->nupvalues); + break; + } + case LUA_VUSERDATA: { + Udata *u = gco2u(o); + res = sizeudata(u->nuvalue, u->len); + break; + } + case LUA_VPROTO: { + res = luaF_protosize(gco2p(o)); + break; + } + case LUA_VTHREAD: { + res = luaE_threadsize(gco2th(o)); + break; + } + case LUA_VSHRSTR: { + TString *ts = gco2ts(o); + res = sizestrshr(cast_uint(ts->shrlen)); + break; + } + case LUA_VLNGSTR: { + TString *ts = gco2ts(o); + res = luaS_sizelngstr(ts->u.lnglen, ts->shrlen); + break; + } + case LUA_VUPVAL: { + res = sizeof(UpVal); + break; + } + default: res = 0; lua_assert(0); + } + return cast(l_mem, res); +} + + static GCObject **getgclist (GCObject *o) { switch (o->tt) { - case LUA_TTABLE: return &gco2t(o)->gclist; - case LUA_TLCL: return &gco2lcl(o)->gclist; - case LUA_TCCL: return &gco2ccl(o)->gclist; - case LUA_TTHREAD: return &gco2th(o)->gclist; - case LUA_TPROTO: return &gco2p(o)->gclist; - case LUA_TUSERDATA: { + case LUA_VTABLE: return &gco2t(o)->gclist; + case LUA_VLCL: return &gco2lcl(o)->gclist; + case LUA_VCCL: return &gco2ccl(o)->gclist; + case LUA_VTHREAD: return &gco2th(o)->gclist; + case LUA_VPROTO: return &gco2p(o)->gclist; + case LUA_VUSERDATA: { Udata *u = gco2u(o); lua_assert(u->nuvalue > 0); return &u->gclist; @@ -135,31 +178,38 @@ static GCObject **getgclist (GCObject *o) { /* -** Link a collectable object 'o' with a known type into list pointed by 'p'. +** Link a collectable object 'o' with a known type into the list 'p'. +** (Must be a macro to access the 'gclist' field in different types.) */ -#define linkgclist(o,p) ((o)->gclist = (p), (p) = obj2gco(o)) +#define linkgclist(o,p) linkgclist_(obj2gco(o), &(o)->gclist, &(p)) + +static void linkgclist_ (GCObject *o, GCObject **pnext, GCObject **list) { + lua_assert(!isgray(o)); /* cannot be in a gray list */ + *pnext = *list; + *list = o; + set2gray(o); /* now it is */ +} /* -** Link a generic collectable object 'o' into list pointed by 'p'. +** Link a generic collectable object 'o' into the list 'p'. */ -#define linkobjgclist(o,p) (*getgclist(o) = (p), (p) = obj2gco(o)) +#define linkobjgclist(o,p) linkgclist_(obj2gco(o), getgclist(o), &(p)) /* -** Clear keys for empty entries in tables. If entry is empty -** and its key is not marked, mark its entry as dead. This allows the -** collection of the key, but keeps its entry in the table (its removal -** could break a chain). The main feature of a dead key is that it must -** be different from any other value, to do not disturb searches. -** Other places never manipulate dead keys, because its associated empty -** value is enough to signal that the entry is logically empty. +** Clear keys for empty entries in tables. If entry is empty, mark its +** entry as dead. This allows the collection of the key, but keeps its +** entry in the table: its removal could break a chain and could break +** a table traversal. Other places never manipulate dead keys, because +** its associated empty value is enough to signal that the entry is +** logically empty. */ static void clearkey (Node *n) { lua_assert(isempty(gval(n))); - if (keyiswhite(n)) - setdeadkey(n); /* unused and unmarked key; remove it */ + if (keyiscollectable(n)) + setdeadkey(n); /* unused key; remove it */ } @@ -181,14 +231,17 @@ static int iscleared (global_State *g, const GCObject *o) { /* -** barrier that moves collector forward, that is, mark the white object -** 'v' being pointed by the black object 'o'. (If in sweep phase, clear -** the black object to white [sweep it] to avoid other barrier calls for -** this same object.) In the generational mode, 'v' must also become -** old, if 'o' is old; however, it cannot be changed directly to OLD, -** because it may still point to non-old objects. So, it is marked as -** OLD0. In the next cycle it will become OLD1, and in the next it -** will finally become OLD (regular old). +** Barrier that moves collector forward, that is, marks the white object +** 'v' being pointed by the black object 'o'. In the generational +** mode, 'v' must also become old, if 'o' is old; however, it cannot +** be changed directly to OLD, because it may still point to non-old +** objects. So, it is marked as OLD0. In the next cycle it will become +** OLD1, and in the next it will finally become OLD (regular old). By +** then, any object it points to will also be old. If called in the +** incremental sweep phase, it clears the black object to white (sweep +** it) to avoid other barrier calls for this same object. (That cannot +** be done is generational mode, as its sweep does not distinguish +** white from dead.) */ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { global_State *g = G(L); @@ -202,7 +255,8 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { } else { /* sweep phase */ lua_assert(issweepphase(g)); - makewhite(g, o); /* mark main obj. as white to avoid other barriers */ + if (g->gckind != KGC_GENMINOR) /* incremental mode? */ + makewhite(g, o); /* mark 'o' as white to avoid other barriers */ } } @@ -214,18 +268,21 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { void luaC_barrierback_ (lua_State *L, GCObject *o) { global_State *g = G(L); lua_assert(isblack(o) && !isdead(g, o)); - lua_assert(g->gckind != KGC_GEN || (isold(o) && getage(o) != G_TOUCHED1)); - if (getage(o) != G_TOUCHED2) /* not already in gray list? */ - linkobjgclist(o, g->grayagain); /* link it in 'grayagain' */ - black2gray(o); /* make object gray (again) */ - setage(o, G_TOUCHED1); /* touched in current cycle */ + lua_assert((g->gckind != KGC_GENMINOR) + || (isold(o) && getage(o) != G_TOUCHED1)); + if (getage(o) == G_TOUCHED2) /* already in gray list? */ + set2gray(o); /* make it gray to become touched1 */ + else /* link it in 'grayagain' and paint it gray */ + linkobjgclist(o, g->grayagain); + if (isold(o)) /* generational mode? */ + setage(o, G_TOUCHED1); /* touched in current cycle */ } void luaC_fix (lua_State *L, GCObject *o) { global_State *g = G(L); lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */ - white2gray(o); /* they will be gray forever */ + set2gray(o); /* they will be gray forever */ setage(o, G_OLD); /* and old forever */ g->allgc = o->next; /* remove object from 'allgc' list */ o->next = g->fixedgc; /* link it to 'fixedgc' list */ @@ -234,12 +291,13 @@ void luaC_fix (lua_State *L, GCObject *o) { /* -** create a new collectable object (with given type and size) and link -** it to 'allgc' list. +** create a new collectable object (with given type, size, and offset) +** and link it to 'allgc' list. */ -GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { +GCObject *luaC_newobjdt (lua_State *L, lu_byte tt, size_t sz, size_t offset) { global_State *g = G(L); - GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz)); + char *p = cast_charp(luaM_newobject(L, novariant(tt), sz)); + GCObject *o = cast(GCObject *, p + offset); o->marked = luaC_white(g); o->tt = tt; o->next = g->allgc; @@ -247,6 +305,14 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { return o; } + +/* +** create a new collectable object with no offset. +*/ +GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz) { + return luaC_newobjdt(L, tt, sz, 0); +} + /* }====================================================== */ @@ -259,40 +325,46 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { /* -** Mark an object. Userdata, strings, and closed upvalues are visited -** and turned black here. Other objects are marked gray and added -** to appropriate list to be visited (and turned black) later. (Open -** upvalues are already linked in 'headuv' list. They are kept gray -** to avoid barriers, as their values will be revisited by the thread.) +** Mark an object. Userdata with no user values, strings, and closed +** upvalues are visited and turned black here. Open upvalues are +** already indirectly linked through their respective threads in the +** 'twups' list, so they don't go to the gray list; nevertheless, they +** are kept gray to avoid barriers, as their values will be revisited +** by the thread or by 'remarkupvals'. Other objects are added to the +** gray list to be visited (and turned black) later. Both userdata and +** upvalues can call this function recursively, but this recursion goes +** for at most two levels: An upvalue cannot refer to another upvalue +** (only closures can), and a userdata's metatable must be a table. */ static void reallymarkobject (global_State *g, GCObject *o) { - white2gray(o); + g->GCmarked += objsize(o); switch (o->tt) { - case LUA_TSHRSTR: - case LUA_TLNGSTR: { - gray2black(o); + case LUA_VSHRSTR: + case LUA_VLNGSTR: { + set2black(o); /* nothing to visit */ break; } - case LUA_TUPVAL: - case LUA_TUPVALTBC: { + case LUA_VUPVAL: { UpVal *uv = gco2upv(o); - if (!upisopen(uv)) /* open upvalues are kept gray */ - gray2black(o); - markvalue(g, uv->v); /* mark its content */ + if (upisopen(uv)) + set2gray(uv); /* open upvalues are kept gray */ + else + set2black(uv); /* closed upvalues are visited here */ + markvalue(g, uv->v.p); /* mark its content */ break; } - case LUA_TUSERDATA: { + case LUA_VUSERDATA: { Udata *u = gco2u(o); if (u->nuvalue == 0) { /* no user values? */ markobjectN(g, u->metatable); /* mark its metatable */ - gray2black(o); /* nothing else to mark */ + set2black(u); /* nothing else to mark */ break; } /* else... */ } /* FALLTHROUGH */ - case LUA_TLCL: case LUA_TCCL: case LUA_TTABLE: - case LUA_TTHREAD: case LUA_TPROTO: { - linkobjgclist(o, g->gray); + case LUA_VLCL: case LUA_VCCL: case LUA_VTABLE: + case LUA_VTHREAD: case LUA_VPROTO: { + linkobjgclist(o, g->gray); /* to be visited later */ break; } default: lua_assert(0); break; @@ -305,7 +377,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { */ static void markmt (global_State *g) { int i; - for (i=0; i < LUA_NUMTAGS; i++) + for (i=0; i < LUA_NUMTYPES; i++) markobjectN(g, g->mt[i]); } @@ -313,54 +385,62 @@ static void markmt (global_State *g) { /* ** mark all objects in list of being-finalized */ -static lu_mem markbeingfnz (global_State *g) { +static void markbeingfnz (global_State *g) { GCObject *o; - lu_mem count = 0; - for (o = g->tobefnz; o != NULL; o = o->next) { - count++; + for (o = g->tobefnz; o != NULL; o = o->next) markobject(g, o); - } - return count; } /* -** Mark all values stored in marked open upvalues from non-marked threads. -** (Values from marked threads were already marked when traversing the -** thread.) Remove from the list threads that no longer have upvalues and -** not-marked threads. +** For each non-marked thread, simulates a barrier between each open +** upvalue and its value. (If the thread is collected, the value will be +** assigned to the upvalue, but then it can be too late for the barrier +** to act. The "barrier" does not need to check colors: A non-marked +** thread must be young; upvalues cannot be older than their threads; so +** any visited upvalue must be young too.) Also removes the thread from +** the list, as it was already visited. Removes also threads with no +** upvalues, as they have nothing to be checked. (If the thread gets an +** upvalue later, it will be linked in the list again.) */ -static int remarkupvals (global_State *g) { +static void remarkupvals (global_State *g) { lua_State *thread; lua_State **p = &g->twups; - int work = 0; while ((thread = *p) != NULL) { - work++; - lua_assert(!isblack(thread)); /* threads are never black */ - if (isgray(thread) && thread->openupval != NULL) + if (!iswhite(thread) && thread->openupval != NULL) p = &thread->twups; /* keep marked thread with upvalues in the list */ else { /* thread is not marked or without upvalues */ UpVal *uv; + lua_assert(!isold(thread) || thread->openupval == NULL); *p = thread->twups; /* remove thread from the list */ thread->twups = thread; /* mark that it is out of list */ for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { - work++; - if (!iswhite(uv)) /* upvalue already visited? */ - markvalue(g, uv->v); /* mark its value */ + lua_assert(getage(uv) <= getage(thread)); + if (!iswhite(uv)) { /* upvalue already visited? */ + lua_assert(upisopen(uv) && isgray(uv)); + markvalue(g, uv->v.p); /* mark its value */ + } } } } - return work; +} + + +static void cleargraylists (global_State *g) { + g->gray = g->grayagain = NULL; + g->weak = g->allweak = g->ephemeron = NULL; } /* -** mark root set and reset all gray lists, to start a new collection +** mark root set and reset all gray lists, to start a new collection. +** 'GCmarked' is initialized to count the total number of live bytes +** during a cycle. */ static void restartcollection (global_State *g) { - g->gray = g->grayagain = NULL; - g->weak = g->allweak = g->ephemeron = NULL; - markobject(g, g->mainthread); + cleargraylists(g); + g->GCmarked = 0; + markobject(g, mainthread(g)); markvalue(g, &g->l_registry); markmt(g); markbeingfnz(g); /* mark any finalizing object left from previous cycle */ @@ -375,17 +455,40 @@ static void restartcollection (global_State *g) { ** ======================================================= */ + +/* +** Check whether object 'o' should be kept in the 'grayagain' list for +** post-processing by 'correctgraylist'. (It could put all old objects +** in the list and leave all the work to 'correctgraylist', but it is +** more efficient to avoid adding elements that will be removed.) Only +** TOUCHED1 objects need to be in the list. TOUCHED2 doesn't need to go +** back to a gray list, but then it must become OLD. (That is what +** 'correctgraylist' does when it finds a TOUCHED2 object.) +** This function is a no-op in incremental mode, as objects cannot be +** marked as touched in that mode. +*/ +static void genlink (global_State *g, GCObject *o) { + lua_assert(isblack(o)); + if (getage(o) == G_TOUCHED1) { /* touched in this cycle? */ + linkobjgclist(o, g->grayagain); /* link it back in 'grayagain' */ + } /* everything else do not need to be linked back */ + else if (getage(o) == G_TOUCHED2) + setage(o, G_OLD); /* advance age */ +} + + /* ** Traverse a table with weak values and link it to proper list. During ** propagate phase, keep it in 'grayagain' list, to be revisited in the ** atomic phase. In the atomic phase, if table has any white value, -** put it in 'weak' list, to be cleared. +** put it in 'weak' list, to be cleared; otherwise, call 'genlink' +** to check table age in generational mode. */ static void traverseweakvalue (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); /* if there is array part, assume it may have white values (it is not worth traversing it now just to check) */ - int hasclears = (h->alimit > 0); + int hasclears = (h->asize > 0); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ if (isempty(gval(n))) /* entry is empty? */ clearkey(n); /* clear its key */ @@ -396,10 +499,30 @@ static void traverseweakvalue (global_State *g, Table *h) { hasclears = 1; /* table will have to be cleared */ } } - if (g->gcstate == GCSatomic && hasclears) - linkgclist(h, g->weak); /* has to be cleared later */ - else + if (g->gcstate == GCSpropagate) linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ + else if (hasclears) + linkgclist(h, g->weak); /* has to be cleared later */ + else + genlink(g, obj2gco(h)); +} + + +/* +** Traverse the array part of a table. +*/ +static int traversearray (global_State *g, Table *h) { + unsigned asize = h->asize; + int marked = 0; /* true if some object is marked in this traversal */ + unsigned i; + for (i = 0; i < asize; i++) { + GCObject *o = gcvalarr(h, i); + if (o != NULL && iswhite(o)) { + marked = 1; + reallymarkobject(g, o); + } + } + return marked; } @@ -411,25 +534,20 @@ static void traverseweakvalue (global_State *g, Table *h) { ** the atomic phase, if table has any white->white entry, it has to ** be revisited during ephemeron convergence (as that key may turn ** black). Otherwise, if it has any white key, table has to be cleared -** (in the atomic phase). In generational mode, it (like all visited -** tables) must be kept in some gray list for post-processing. +** (in the atomic phase). In generational mode, some tables +** must be kept in some gray list for post-processing; this is done +** by 'genlink'. */ -static int traverseephemeron (global_State *g, Table *h) { - int marked = 0; /* true if an object is marked in this traversal */ +static int traverseephemeron (global_State *g, Table *h, int inv) { int hasclears = 0; /* true if table has white keys */ int hasww = 0; /* true if table has entry "white-key -> white-value" */ - Node *n, *limit = gnodelast(h); unsigned int i; - unsigned int asize = luaH_realasize(h); - /* traverse array part */ - for (i = 0; i < asize; i++) { - if (valiswhite(&h->array[i])) { - marked = 1; - reallymarkobject(g, gcvalue(&h->array[i])); - } - } - /* traverse hash part */ - for (n = gnode(h, 0); n < limit; n++) { + unsigned int nsize = sizenode(h); + int marked = traversearray(g, h); /* traverse array part */ + /* traverse hash part; if 'inv', traverse descending + (see 'convergeephemerons') */ + for (i = 0; i < nsize; i++) { + Node *n = inv ? gnode(h, nsize - 1 - i) : gnode(h, i); if (isempty(gval(n))) /* entry is empty? */ clearkey(n); /* clear its key */ else if (iscleared(g, gckeyN(n))) { /* key is not marked (yet)? */ @@ -449,20 +567,15 @@ static int traverseephemeron (global_State *g, Table *h) { linkgclist(h, g->ephemeron); /* have to propagate again */ else if (hasclears) /* table has white keys? */ linkgclist(h, g->allweak); /* may have to clean white keys */ - else if (g->gckind == KGC_GEN) - linkgclist(h, g->grayagain); /* keep it in some list */ else - gray2black(h); + genlink(g, obj2gco(h)); /* check whether collector still needs to see it */ return marked; } static void traversestrongtable (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); - unsigned int i; - unsigned int asize = luaH_realasize(h); - for (i = 0; i < asize; i++) /* traverse array part */ - markvalue(g, &h->array[i]); + traversearray(g, h); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ if (isempty(gval(n))) /* entry is empty? */ clearkey(n); /* clear its key */ @@ -472,44 +585,55 @@ static void traversestrongtable (global_State *g, Table *h) { markvalue(g, gval(n)); } } - if (g->gckind == KGC_GEN) { - linkgclist(h, g->grayagain); /* keep it in some gray list */ - black2gray(h); - } + genlink(g, obj2gco(h)); } -static lu_mem traversetable (global_State *g, Table *h) { - const char *weakkey, *weakvalue; +/* +** (result & 1) iff weak values; (result & 2) iff weak keys. +*/ +static int getmode (global_State *g, Table *h) { const TValue *mode = gfasttm(g, h->metatable, TM_MODE); + if (mode == NULL || !ttisstring(mode)) + return 0; /* ignore non-string modes */ + else { + const char *smode = getstr(tsvalue(mode)); + const char *weakkey = strchr(smode, 'k'); + const char *weakvalue = strchr(smode, 'v'); + return ((weakkey != NULL) << 1) | (weakvalue != NULL); + } +} + + +static l_mem traversetable (global_State *g, Table *h) { markobjectN(g, h->metatable); - if (mode && ttisstring(mode) && /* is there a weak mode? */ - ((weakkey = strchr(svalue(mode), 'k')), - (weakvalue = strchr(svalue(mode), 'v')), - (weakkey || weakvalue))) { /* is really weak? */ - black2gray(h); /* keep table gray */ - if (!weakkey) /* strong keys? */ + switch (getmode(g, h)) { + case 0: /* not weak */ + traversestrongtable(g, h); + break; + case 1: /* weak values */ traverseweakvalue(g, h); - else if (!weakvalue) /* strong values? */ - traverseephemeron(g, h); - else /* all weak */ - linkgclist(h, g->allweak); /* nothing to traverse now */ + break; + case 2: /* weak keys */ + traverseephemeron(g, h, 0); + break; + case 3: /* all weak; nothing to traverse */ + if (g->gcstate == GCSpropagate) + linkgclist(h, g->grayagain); /* must visit again its metatable */ + else + linkgclist(h, g->allweak); /* must clear collected entries */ + break; } - else /* not weak */ - traversestrongtable(g, h); - return 1 + h->alimit + 2 * allocsizenode(h); + return cast(l_mem, 1 + 2*sizenode(h) + h->asize); } -static int traverseudata (global_State *g, Udata *u) { +static l_mem traverseudata (global_State *g, Udata *u) { int i; markobjectN(g, u->metatable); /* mark its metatable */ for (i = 0; i < u->nuvalue; i++) markvalue(g, &u->uv[i].uv); - if (g->gckind == KGC_GEN) { - linkgclist(u, g->grayagain); /* keep it in some gray list */ - black2gray(u); - } + genlink(g, obj2gco(u)); return 1 + u->nuvalue; } @@ -519,7 +643,7 @@ static int traverseudata (global_State *g, Udata *u) { ** arrays can be larger than needed; the extra slots are filled with ** NULL, so the use of 'markobjectN') */ -static int traverseproto (global_State *g, Proto *f) { +static l_mem traverseproto (global_State *g, Proto *f) { int i; markobjectN(g, f->source); for (i = 0; i < f->sizek; i++) /* mark literals */ @@ -534,7 +658,7 @@ static int traverseproto (global_State *g, Proto *f) { } -static int traverseCclosure (global_State *g, CClosure *cl) { +static l_mem traverseCclosure (global_State *g, CClosure *cl) { int i; for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ markvalue(g, &cl->upvalue[i]); @@ -545,7 +669,7 @@ static int traverseCclosure (global_State *g, CClosure *cl) { ** Traverse a Lua closure, marking its prototype and its upvalues. ** (Both can be NULL while closure is being created.) */ -static int traverseLclosure (global_State *g, LClosure *cl) { +static l_mem traverseLclosure (global_State *g, LClosure *cl) { int i; markobjectN(g, cl->p); /* mark its prototype */ for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */ @@ -558,86 +682,95 @@ static int traverseLclosure (global_State *g, LClosure *cl) { /* ** Traverse a thread, marking the elements in the stack up to its top -** and cleaning the rest of the stack in the final traversal. -** That ensures that the entire stack have valid (non-dead) objects. -*/ -static int traversethread (global_State *g, lua_State *th) { +** and cleaning the rest of the stack in the final traversal. That +** ensures that the entire stack have valid (non-dead) objects. +** Threads have no barriers. In gen. mode, old threads must be visited +** at every cycle, because they might point to young objects. In inc. +** mode, the thread can still be modified before the end of the cycle, +** and therefore it must be visited again in the atomic phase. To ensure +** these visits, threads must return to a gray list if they are not new +** (which can only happen in generational mode) or if the traverse is in +** the propagate phase (which can only happen in incremental mode). +*/ +static l_mem traversethread (global_State *g, lua_State *th) { UpVal *uv; - StkId o = th->stack; + StkId o = th->stack.p; + if (isold(th) || g->gcstate == GCSpropagate) + linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ if (o == NULL) - return 1; /* stack not completely built yet */ + return 0; /* stack not completely built yet */ lua_assert(g->gcstate == GCSatomic || th->openupval == NULL || isintwups(th)); - for (; o < th->top; o++) /* mark live elements in the stack */ + for (; o < th->top.p; o++) /* mark live elements in the stack */ markvalue(g, s2v(o)); - for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) { - if (uv->tt == LUA_TUPVALTBC) /* to be closed? */ - markobject(g, uv); /* cannot be collected */ - } + for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) + markobject(g, uv); /* open upvalues cannot be collected */ if (g->gcstate == GCSatomic) { /* final traversal? */ - StkId lim = th->stack + th->stacksize; /* real end of stack */ - for (; o < lim; o++) /* clear not-marked stack slice */ - setnilvalue(s2v(o)); + if (!g->gcemergency) + luaD_shrinkstack(th); /* do not change stack in emergency cycle */ + for (o = th->top.p; o < th->stack_last.p + EXTRA_STACK; o++) + setnilvalue(s2v(o)); /* clear dead stack slice */ /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { th->twups = g->twups; /* link it back to the list */ g->twups = th; } } - else if (!g->gcemergency) - luaD_shrinkstack(th); /* do not change stack in emergency cycle */ - return 1 + th->stacksize; + return 1 + (th->top.p - th->stack.p); } /* -** traverse one gray object, turning it to black (except for threads, -** which are always gray). +** traverse one gray object, turning it to black. Return an estimate +** of the number of slots traversed. */ -static lu_mem propagatemark (global_State *g) { +static l_mem propagatemark (global_State *g) { GCObject *o = g->gray; - gray2black(o); + nw2black(o); g->gray = *getgclist(o); /* remove from 'gray' list */ switch (o->tt) { - case LUA_TTABLE: return traversetable(g, gco2t(o)); - case LUA_TUSERDATA: return traverseudata(g, gco2u(o)); - case LUA_TLCL: return traverseLclosure(g, gco2lcl(o)); - case LUA_TCCL: return traverseCclosure(g, gco2ccl(o)); - case LUA_TPROTO: return traverseproto(g, gco2p(o)); - case LUA_TTHREAD: { - lua_State *th = gco2th(o); - linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ - black2gray(o); - return traversethread(g, th); - } + case LUA_VTABLE: return traversetable(g, gco2t(o)); + case LUA_VUSERDATA: return traverseudata(g, gco2u(o)); + case LUA_VLCL: return traverseLclosure(g, gco2lcl(o)); + case LUA_VCCL: return traverseCclosure(g, gco2ccl(o)); + case LUA_VPROTO: return traverseproto(g, gco2p(o)); + case LUA_VTHREAD: return traversethread(g, gco2th(o)); default: lua_assert(0); return 0; } } -static lu_mem propagateall (global_State *g) { - lu_mem tot = 0; +static void propagateall (global_State *g) { while (g->gray) - tot += propagatemark(g); - return tot; + propagatemark(g); } +/* +** Traverse all ephemeron tables propagating marks from keys to values. +** Repeat until it converges, that is, nothing new is marked. 'dir' +** inverts the direction of the traversals, trying to speed up +** convergence on chains in the same table. +*/ static void convergeephemerons (global_State *g) { int changed; + int dir = 0; do { GCObject *w; GCObject *next = g->ephemeron; /* get ephemeron list */ g->ephemeron = NULL; /* tables may return to this list when traversed */ changed = 0; - while ((w = next) != NULL) { - next = gco2t(w)->gclist; - if (traverseephemeron(g, gco2t(w))) { /* traverse marked some value? */ + while ((w = next) != NULL) { /* for each ephemeron table */ + Table *h = gco2t(w); + next = h->gclist; /* list is rebuilt during loop */ + nw2black(h); /* out of the list (for now) */ + if (traverseephemeron(g, h, dir)) { /* marked some value? */ propagateall(g); /* propagate changes */ changed = 1; /* will have to revisit all ephemeron tables */ } } - } while (changed); + dir = !dir; /* invert direction next time */ + } while (changed); /* repeat until no more changes */ } /* }====================================================== */ @@ -677,11 +810,11 @@ static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); unsigned int i; - unsigned int asize = luaH_realasize(h); + unsigned int asize = h->asize; for (i = 0; i < asize; i++) { - TValue *o = &h->array[i]; - if (iscleared(g, gcvalueN(o))) /* value was collected? */ - setempty(o); /* remove entry */ + GCObject *o = gcvalarr(h, i); + if (iscleared(g, o)) /* value was collected? */ + *getArrTag(h, i) = LUA_VEMPTY; /* remove entry */ } for (n = gnode(h, 0); n < limit; n++) { if (iscleared(g, gcvalueN(gval(n)))) /* unmarked value? */ @@ -701,70 +834,77 @@ static void freeupval (lua_State *L, UpVal *uv) { static void freeobj (lua_State *L, GCObject *o) { + assert_code(l_mem newmem = gettotalbytes(G(L)) - objsize(o)); switch (o->tt) { - case LUA_TPROTO: + case LUA_VPROTO: luaF_freeproto(L, gco2p(o)); break; - case LUA_TUPVAL: - case LUA_TUPVALTBC: + case LUA_VUPVAL: freeupval(L, gco2upv(o)); break; - case LUA_TLCL: - luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues)); + case LUA_VLCL: { + LClosure *cl = gco2lcl(o); + luaM_freemem(L, cl, sizeLclosure(cl->nupvalues)); break; - case LUA_TCCL: - luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues)); + } + case LUA_VCCL: { + CClosure *cl = gco2ccl(o); + luaM_freemem(L, cl, sizeCclosure(cl->nupvalues)); break; - case LUA_TTABLE: + } + case LUA_VTABLE: luaH_free(L, gco2t(o)); break; - case LUA_TTHREAD: + case LUA_VTHREAD: luaE_freethread(L, gco2th(o)); break; - case LUA_TUSERDATA: { + case LUA_VUSERDATA: { Udata *u = gco2u(o); luaM_freemem(L, o, sizeudata(u->nuvalue, u->len)); break; } - case LUA_TSHRSTR: - luaS_remove(L, gco2ts(o)); /* remove it from hash table */ - luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen)); + case LUA_VSHRSTR: { + TString *ts = gco2ts(o); + luaS_remove(L, ts); /* remove it from hash table */ + luaM_freemem(L, ts, sizestrshr(cast_uint(ts->shrlen))); break; - case LUA_TLNGSTR: - luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen)); + } + case LUA_VLNGSTR: { + TString *ts = gco2ts(o); + if (ts->shrlen == LSTRMEM) /* must free external string? */ + (*ts->falloc)(ts->ud, ts->contents, ts->u.lnglen + 1, 0); + luaM_freemem(L, ts, luaS_sizelngstr(ts->u.lnglen, ts->shrlen)); break; + } default: lua_assert(0); } + lua_assert(gettotalbytes(G(L)) == newmem); } /* ** sweep at most 'countin' elements from a list of GCObjects erasing dead ** objects, where a dead object is one marked with the old (non current) -** white; change all non-dead objects back to white, preparing for next -** collection cycle. Return where to continue the traversal or NULL if -** list is finished. ('*countout' gets the number of elements traversed.) +** white; change all non-dead objects back to white (and new), preparing +** for next collection cycle. Return where to continue the traversal or +** NULL if list is finished. */ -static GCObject **sweeplist (lua_State *L, GCObject **p, int countin, - int *countout) { +static GCObject **sweeplist (lua_State *L, GCObject **p, l_mem countin) { global_State *g = G(L); int ow = otherwhite(g); - int i; int white = luaC_white(g); /* current white */ - for (i = 0; *p != NULL && i < countin; i++) { + while (*p != NULL && countin-- > 0) { GCObject *curr = *p; int marked = curr->marked; if (isdeadm(ow, marked)) { /* is 'curr' dead? */ *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } - else { /* change mark to 'white' */ - curr->marked = cast_byte((marked & maskcolors) | white); + else { /* change mark to 'white' and age to 'new' */ + curr->marked = cast_byte((marked & ~maskgcbits) | white | G_NEW); p = &curr->next; /* go to next element */ } } - if (countout) - *countout = i; /* number of elements traversed */ return (*p == NULL) ? NULL : p; } @@ -775,7 +915,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, int countin, static GCObject **sweeptolive (lua_State *L, GCObject **p) { GCObject **old = p; do { - p = sweeplist(L, p, 1, NULL); + p = sweeplist(L, p, 1); } while (p == old); return p; } @@ -794,10 +934,8 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) { */ static void checkSizes (lua_State *L, global_State *g) { if (!g->gcemergency) { - l_mem olddebt = g->GCdebt; if (g->strt.nuse < g->strt.size / 4) /* string table too big? */ luaS_resize(L, g->strt.size / 2); - g->GCestimate += g->GCdebt - olddebt; /* correct estimate */ } } @@ -815,13 +953,15 @@ static GCObject *udata2finalize (global_State *g) { resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */ if (issweepphase(g)) makewhite(g, o); /* "sweep" object */ + else if (getage(o) == G_OLD1) + g->firstold1 = o; /* it is the first OLD1 object in the list */ return o; } static void dothecall (lua_State *L, void *ud) { UNUSED(ud); - luaD_callnoyield(L, L->top - 2, 0); + luaD_callnoyield(L, L->top.p - 2, 0); } @@ -833,43 +973,26 @@ static void GCTM (lua_State *L) { setgcovalue(L, &v, udata2finalize(g)); tm = luaT_gettmbyobj(L, &v, TM_GC); if (!notm(tm)) { /* is there a finalizer? */ - int status; + TStatus status; lu_byte oldah = L->allowhook; - int running = g->gcrunning; + lu_byte oldgcstp = g->gcstp; + g->gcstp |= GCSTPGC; /* avoid GC steps */ L->allowhook = 0; /* stop debug hooks during GC metamethod */ - g->gcrunning = 0; /* avoid GC steps */ - setobj2s(L, L->top, tm); /* push finalizer... */ - setobj2s(L, L->top + 1, &v); /* ... and its argument */ - L->top += 2; /* and (next line) call the finalizer */ + setobj2s(L, L->top.p++, tm); /* push finalizer... */ + setobj2s(L, L->top.p++, &v); /* ... and its argument */ L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ - status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); + status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top.p - 2), 0); L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ - g->gcrunning = running; /* restore state */ - if (status != LUA_OK) { /* error while running __gc? */ - const char *msg = (ttisstring(s2v(L->top - 1))) - ? svalue(s2v(L->top - 1)) - : "error object is not a string"; - luaE_warning(L, "error in __gc metamethod (", 1); - luaE_warning(L, msg, 1); - luaE_warning(L, ")", 0); + g->gcstp = oldgcstp; /* restore state */ + if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */ + luaE_warnerror(L, "__gc"); + L->top.p--; /* pops error object */ } } } -/* -** Call a few finalizers -*/ -static int runafewfinalizers (lua_State *L, int n) { - global_State *g = G(L); - int i; - for (i = 0; i < n && g->tobefnz; i++) - GCTM(L); /* call one finalizer */ - return i; -} - - /* ** call all pending finalizers */ @@ -893,15 +1016,15 @@ static GCObject **findlast (GCObject **p) { /* ** Move all unreachable objects (or 'all' objects) that need ** finalization from list 'finobj' to list 'tobefnz' (to be finalized). -** (Note that objects after 'finobjold' cannot be white, so they -** don't need to be traversed. In incremental mode, 'finobjold' is NULL, +** (Note that objects after 'finobjold1' cannot be white, so they +** don't need to be traversed. In incremental mode, 'finobjold1' is NULL, ** so the whole list is traversed.) */ static void separatetobefnz (global_State *g, int all) { GCObject *curr; GCObject **p = &g->finobj; GCObject **lastnext = findlast(&g->tobefnz); - while ((curr = *p) != g->finobjold) { /* traverse all finalizable objects */ + while ((curr = *p) != g->finobjold1) { /* traverse all finalizable objects */ lua_assert(tofinalize(curr)); if (!(iswhite(curr) || all)) /* not being collected? */ p = &curr->next; /* don't bother with it */ @@ -917,6 +1040,27 @@ static void separatetobefnz (global_State *g, int all) { } +/* +** If pointer 'p' points to 'o', move it to the next element. +*/ +static void checkpointer (GCObject **p, GCObject *o) { + if (o == *p) + *p = o->next; +} + + +/* +** Correct pointers to objects inside 'allgc' list when +** object 'o' is being removed from the list. +*/ +static void correctpointers (global_State *g, GCObject *o) { + checkpointer(&g->survival, o); + checkpointer(&g->old1, o); + checkpointer(&g->reallyold, o); + checkpointer(&g->firstold1, o); +} + + /* ** if object 'o' has a finalizer, remove it from 'allgc' list (must ** search the list to find it) and link it in 'finobj' list. @@ -924,7 +1068,8 @@ static void separatetobefnz (global_State *g, int all) { void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { global_State *g = G(L); if (tofinalize(o) || /* obj. is already marked... */ - gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */ + gfasttm(g, mt, TM_GC) == NULL || /* or has no finalizer... */ + (g->gcstp & GCSTPCLS)) /* or closing state? */ return; /* nothing to be done */ else { /* move 'o' to 'finobj' list */ GCObject **p; @@ -933,14 +1078,8 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */ } - else { /* correct pointers into 'allgc' list, if needed */ - if (o == g->survival) - g->survival = o->next; - if (o == g->old) - g->old = o->next; - if (o == g->reallyold) - g->reallyold = o->next; - } + else + correctpointers(g, o); /* search for pointer pointing to 'o' */ for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ } *p = o->next; /* remove 'o' from 'allgc' list */ @@ -959,27 +1098,60 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { ** ======================================================= */ -static void setpause (global_State *g); +/* +** Fields 'GCmarked' and 'GCmajorminor' are used to control the pace and +** the mode of the collector. They play several roles, depending on the +** mode of the collector: +** * KGC_INC: +** GCmarked: number of marked bytes during a cycle. +** GCmajorminor: not used. +** * KGC_GENMINOR +** GCmarked: number of bytes that became old since last major collection. +** GCmajorminor: number of bytes marked in last major collection. +** * KGC_GENMAJOR +** GCmarked: number of bytes that became old since last major collection. +** GCmajorminor: number of bytes marked in last major collection. +*/ -/* mask to erase all color bits, not changing gen-related stuff */ -#define maskgencolors (~(bitmask(BLACKBIT) | WHITEBITS)) +/* +** Set the "time" to wait before starting a new incremental cycle; +** cycle will start when number of bytes in use hits the threshold of +** approximately (marked * pause / 100). +*/ +static void setpause (global_State *g) { + l_mem threshold = applygcparam(g, PAUSE, g->GCmarked); + l_mem debt = threshold - gettotalbytes(g); + if (debt < 0) debt = 0; + luaE_setdebt(g, debt); +} /* -** Sweep a list of objects, deleting dead ones and turning -** the non dead to old (without changing their colors). +** Sweep a list of objects to enter generational mode. Deletes dead +** objects and turns the non dead to old. All non-dead threads---which +** are now old---must be in a gray list. Everything else is not in a +** gray list. Open upvalues are also kept gray. */ static void sweep2old (lua_State *L, GCObject **p) { GCObject *curr; + global_State *g = G(L); while ((curr = *p) != NULL) { if (iswhite(curr)) { /* is 'curr' dead? */ - lua_assert(isdead(G(L), curr)); + lua_assert(isdead(g, curr)); *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } else { /* all surviving objects become old */ setage(curr, G_OLD); + if (curr->tt == LUA_VTHREAD) { /* threads must be watched */ + lua_State *th = gco2th(curr); + linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ + } + else if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr))) + set2gray(curr); /* open upvalues are always gray */ + else /* everything else is black */ + nw2black(curr); p = &curr->next; /* go to next element */ } } @@ -992,10 +1164,15 @@ static void sweep2old (lua_State *L, GCObject **p) { ** during the sweep. So, any white object must be dead.) For ** non-dead objects, advance their ages and clear the color of ** new objects. (Old objects keep their colors.) +** The ages of G_TOUCHED1 and G_TOUCHED2 objects cannot be advanced +** here, because these old-generation objects are usually not swept +** here. They will all be advanced in 'correctgraylist'. That function +** will also remove objects turned white here from any gray list. */ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, - GCObject *limit) { - static lu_byte nextage[] = { + GCObject *limit, GCObject **pfirstold1, + l_mem *paddedold) { + static const lu_byte nextage[] = { G_SURVIVAL, /* from G_NEW */ G_OLD1, /* from G_SURVIVAL */ G_OLD1, /* from G_OLD0 */ @@ -1004,6 +1181,7 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, G_TOUCHED1, /* from G_TOUCHED1 (do not change) */ G_TOUCHED2 /* from G_TOUCHED2 (do not change) */ }; + l_mem addedold = 0; int white = luaC_white(g); GCObject *curr; while ((curr = *p) != limit) { @@ -1013,70 +1191,64 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, freeobj(L, curr); /* erase 'curr' */ } else { /* correct mark and age */ - if (getage(curr) == G_NEW) - curr->marked = cast_byte((curr->marked & maskgencolors) | white); - setage(curr, nextage[getage(curr)]); + int age = getage(curr); + if (age == G_NEW) { /* new objects go back to white */ + int marked = curr->marked & ~maskgcbits; /* erase GC bits */ + curr->marked = cast_byte(marked | G_SURVIVAL | white); + } + else { /* all other objects will be old, and so keep their color */ + lua_assert(age != G_OLD1); /* advanced in 'markold' */ + setage(curr, nextage[age]); + if (getage(curr) == G_OLD1) { + addedold += objsize(curr); /* bytes becoming old */ + if (*pfirstold1 == NULL) + *pfirstold1 = curr; /* first OLD1 object in the list */ + } + } p = &curr->next; /* go to next element */ } } + *paddedold += addedold; return p; } /* -** Traverse a list making all its elements white and clearing their -** age. -*/ -static void whitelist (global_State *g, GCObject *p) { - int white = luaC_white(g); - for (; p != NULL; p = p->next) - p->marked = cast_byte((p->marked & maskcolors) | white); -} - - -/* -** Correct a list of gray objects. +** Correct a list of gray objects. Return a pointer to the last element +** left on the list, so that we can link another list to the end of +** this one. ** Because this correction is done after sweeping, young objects might ** be turned white and still be in the list. They are only removed. -** For tables and userdata, advance 'touched1' to 'touched2'; 'touched2' -** objects become regular old and are removed from the list. -** For threads, just remove white ones from the list. +** 'TOUCHED1' objects are advanced to 'TOUCHED2' and remain on the list; +** Non-white threads also remain on the list. 'TOUCHED2' objects and +** anything else become regular old, are marked black, and are removed +** from the list. */ static GCObject **correctgraylist (GCObject **p) { GCObject *curr; while ((curr = *p) != NULL) { - switch (curr->tt) { - case LUA_TTABLE: case LUA_TUSERDATA: { - GCObject **next = getgclist(curr); - if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ - lua_assert(isgray(curr)); - gray2black(curr); /* make it black, for next barrier */ - changeage(curr, G_TOUCHED1, G_TOUCHED2); - p = next; /* go to next element */ - } - else { /* not touched in this cycle */ - if (!iswhite(curr)) { /* not white? */ - lua_assert(isold(curr)); - if (getage(curr) == G_TOUCHED2) /* advance from G_TOUCHED2... */ - changeage(curr, G_TOUCHED2, G_OLD); /* ... to G_OLD */ - gray2black(curr); /* make it black */ - } - /* else, object is white: just remove it from this list */ - *p = *next; /* remove 'curr' from gray list */ - } - break; - } - case LUA_TTHREAD: { - lua_State *th = gco2th(curr); - lua_assert(!isblack(th)); - if (iswhite(th)) /* new object? */ - *p = th->gclist; /* remove from gray list */ - else /* old threads remain gray */ - p = &th->gclist; /* go to next element */ - break; - } - default: lua_assert(0); /* nothing more could be gray here */ + GCObject **next = getgclist(curr); + if (iswhite(curr)) + goto remove; /* remove all white objects */ + else if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ + lua_assert(isgray(curr)); + nw2black(curr); /* make it black, for next barrier */ + setage(curr, G_TOUCHED2); + goto remain; /* keep it in the list and go to next element */ + } + else if (curr->tt == LUA_VTHREAD) { + lua_assert(isgray(curr)); + goto remain; /* keep non-white threads on the list */ } + else { /* everything else is removed */ + lua_assert(isold(curr)); /* young objects should be white here */ + if (getage(curr) == G_TOUCHED2) /* advance from TOUCHED2... */ + setage(curr, G_OLD); /* ... to OLD */ + nw2black(curr); /* make object black (to be removed) */ + goto remove; + } + remove: *p = *next; continue; + remain: p = next; continue; } return p; } @@ -1097,19 +1269,18 @@ static void correctgraylists (global_State *g) { /* -** Mark 'OLD1' objects when starting a new young collection. -** Gray objects are already in some gray list, and so will be visited -** in the atomic step. +** Mark black 'OLD1' objects when starting a new young collection. +** Gray objects are already in some gray list, and so will be visited in +** the atomic step. */ static void markold (global_State *g, GCObject *from, GCObject *to) { GCObject *p; for (p = from; p != to; p = p->next) { if (getage(p) == G_OLD1) { lua_assert(!iswhite(p)); - if (isblack(p)) { - black2gray(p); /* should be '2white', but gray works too */ + setage(p, G_OLD); /* now they are old */ + if (isblack(p)) reallymarkobject(g, p); - } } } } @@ -1122,95 +1293,144 @@ static void finishgencycle (lua_State *L, global_State *g) { correctgraylists(g); checkSizes(L, g); g->gcstate = GCSpropagate; /* skip restart */ - if (!g->gcemergency) + if (!g->gcemergency && luaD_checkminstack(L)) callallpendingfinalizers(L); } /* -** Does a young collection. First, mark 'OLD1' objects. (Only survival -** and "recent old" lists can contain 'OLD1' objects. New lists cannot -** contain 'OLD1' objects, at most 'OLD0' objects that were already -** visited when marked old.) Then does the atomic step. Then, +** Shifts from a minor collection to major collections. It starts in +** the "sweep all" state to clear all objects, which are mostly black +** in generational mode. +*/ +static void minor2inc (lua_State *L, global_State *g, lu_byte kind) { + g->GCmajorminor = g->GCmarked; /* number of live bytes */ + g->gckind = kind; + g->reallyold = g->old1 = g->survival = NULL; + g->finobjrold = g->finobjold1 = g->finobjsur = NULL; + entersweep(L); /* continue as an incremental cycle */ + /* set a debt equal to the step size */ + luaE_setdebt(g, applygcparam(g, STEPSIZE, 100)); +} + + +/* +** Decide whether to shift to major mode. It shifts if the accumulated +** number of added old bytes (counted in 'GCmarked') is larger than +** 'minormajor'% of the number of lived bytes after the last major +** collection. (This number is kept in 'GCmajorminor'.) +*/ +static int checkminormajor (global_State *g) { + l_mem limit = applygcparam(g, MINORMAJOR, g->GCmajorminor); + if (limit == 0) + return 0; /* special case: 'minormajor' 0 stops major collections */ + return (g->GCmarked >= limit); +} + +/* +** Does a young collection. First, mark 'OLD1' objects. Then does the +** atomic step. Then, check whether to continue in minor mode. If so, ** sweep all lists and advance pointers. Finally, finish the collection. */ static void youngcollection (lua_State *L, global_State *g) { + l_mem addedold1 = 0; + l_mem marked = g->GCmarked; /* preserve 'g->GCmarked' */ GCObject **psurvival; /* to point to first non-dead survival object */ + GCObject *dummy; /* dummy out parameter to 'sweepgen' */ lua_assert(g->gcstate == GCSpropagate); - markold(g, g->survival, g->reallyold); + if (g->firstold1) { /* are there regular OLD1 objects? */ + markold(g, g->firstold1, g->reallyold); /* mark them */ + g->firstold1 = NULL; /* no more OLD1 objects (for now) */ + } markold(g, g->finobj, g->finobjrold); - atomic(L); + markold(g, g->tobefnz, NULL); + + atomic(L); /* will lose 'g->marked' */ /* sweep nursery and get a pointer to its last live element */ - psurvival = sweepgen(L, g, &g->allgc, g->survival); - /* sweep 'survival' and 'old' */ - sweepgen(L, g, psurvival, g->reallyold); - g->reallyold = g->old; - g->old = *psurvival; /* 'survival' survivals are old now */ + g->gcstate = GCSswpallgc; + psurvival = sweepgen(L, g, &g->allgc, g->survival, &g->firstold1, &addedold1); + /* sweep 'survival' */ + sweepgen(L, g, psurvival, g->old1, &g->firstold1, &addedold1); + g->reallyold = g->old1; + g->old1 = *psurvival; /* 'survival' survivals are old now */ g->survival = g->allgc; /* all news are survivals */ /* repeat for 'finobj' lists */ - psurvival = sweepgen(L, g, &g->finobj, g->finobjsur); - /* sweep 'survival' and 'old' */ - sweepgen(L, g, psurvival, g->finobjrold); - g->finobjrold = g->finobjold; - g->finobjold = *psurvival; /* 'survival' survivals are old now */ + dummy = NULL; /* no 'firstold1' optimization for 'finobj' lists */ + psurvival = sweepgen(L, g, &g->finobj, g->finobjsur, &dummy, &addedold1); + /* sweep 'survival' */ + sweepgen(L, g, psurvival, g->finobjold1, &dummy, &addedold1); + g->finobjrold = g->finobjold1; + g->finobjold1 = *psurvival; /* 'survival' survivals are old now */ g->finobjsur = g->finobj; /* all news are survivals */ - sweepgen(L, g, &g->tobefnz, NULL); + sweepgen(L, g, &g->tobefnz, NULL, &dummy, &addedold1); - finishgencycle(L, g); + /* keep total number of added old1 bytes */ + g->GCmarked = marked + addedold1; + + /* decide whether to shift to major mode */ + if (checkminormajor(g)) { + minor2inc(L, g, KGC_GENMAJOR); /* go to major mode */ + g->GCmarked = 0; /* avoid pause in first major cycle (see 'setpause') */ + } + else + finishgencycle(L, g); /* still in minor mode; finish it */ } +/* +** Clears all gray lists, sweeps objects, and prepare sublists to enter +** generational mode. The sweeps remove dead objects and turn all +** surviving objects to old. Threads go back to 'grayagain'; everything +** else is turned black (not in any gray list). +*/ static void atomic2gen (lua_State *L, global_State *g) { + cleargraylists(g); /* sweep all elements making them old */ + g->gcstate = GCSswpallgc; sweep2old(L, &g->allgc); /* everything alive now is old */ - g->reallyold = g->old = g->survival = g->allgc; + g->reallyold = g->old1 = g->survival = g->allgc; + g->firstold1 = NULL; /* there are no OLD1 objects anywhere */ /* repeat for 'finobj' lists */ sweep2old(L, &g->finobj); - g->finobjrold = g->finobjold = g->finobjsur = g->finobj; + g->finobjrold = g->finobjold1 = g->finobjsur = g->finobj; sweep2old(L, &g->tobefnz); - g->gckind = KGC_GEN; - g->lastatomic = 0; - g->GCestimate = gettotalbytes(g); /* base for memory control */ + g->gckind = KGC_GENMINOR; + g->GCmajorminor = g->GCmarked; /* "base" for number of bytes */ + g->GCmarked = 0; /* to count the number of added old1 bytes */ finishgencycle(L, g); } /* -** Enter generational mode. Must go until the end of an atomic cycle -** to ensure that all threads and weak tables are in the gray lists. -** Then, turn all objects into old and finishes the collection. -*/ -static lu_mem entergen (lua_State *L, global_State *g) { - lu_mem numobjs; - luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */ - luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ - numobjs = atomic(L); /* propagates all and then do the atomic stuff */ - atomic2gen(L, g); - return numobjs; +** Set debt for the next minor collection, which will happen when +** total number of bytes grows 'genminormul'% in relation to +** the base, GCmajorminor, which is the number of bytes being used +** after the last major collection. +*/ +static void setminordebt (global_State *g) { + luaE_setdebt(g, applygcparam(g, MINORMUL, g->GCmajorminor)); } /* -** Enter incremental mode. Turn all objects white, make all -** intermediate lists point to NULL (to avoid invalid pointers), -** and go to the pause state. -*/ -static void enterinc (global_State *g) { - whitelist(g, g->allgc); - g->reallyold = g->old = g->survival = NULL; - whitelist(g, g->finobj); - whitelist(g, g->tobefnz); - g->finobjrold = g->finobjold = g->finobjsur = NULL; - g->gcstate = GCSpause; - g->gckind = KGC_INC; - g->lastatomic = 0; +** Enter generational mode. Must go until the end of an atomic cycle +** to ensure that all objects are correctly marked and weak tables +** are cleared. Then, turn all objects into old and finishes the +** collection. +*/ +static void entergen (lua_State *L, global_State *g) { + luaC_runtilstate(L, GCSpause, 1); /* prepare to start a new cycle */ + luaC_runtilstate(L, GCSpropagate, 1); /* start new cycle */ + atomic(L); /* propagates all and then do the atomic stuff */ + atomic2gen(L, g); + setminordebt(g); /* set debt assuming next cycle will be minor */ } @@ -1219,120 +1439,49 @@ static void enterinc (global_State *g) { */ void luaC_changemode (lua_State *L, int newmode) { global_State *g = G(L); - if (newmode != g->gckind) { - if (newmode == KGC_GEN) /* entering generational mode? */ + if (g->gckind == KGC_GENMAJOR) /* doing major collections? */ + g->gckind = KGC_INC; /* already incremental but in name */ + if (newmode != g->gckind) { /* does it need to change? */ + if (newmode == KGC_INC) /* entering incremental mode? */ + minor2inc(L, g, KGC_INC); /* entering incremental mode */ + else { + lua_assert(newmode == KGC_GENMINOR); entergen(L, g); - else - enterinc(g); /* entering incremental mode */ + } } - g->lastatomic = 0; } /* ** Does a full collection in generational mode. */ -static lu_mem fullgen (lua_State *L, global_State *g) { - enterinc(g); - return entergen(L, g); +static void fullgen (lua_State *L, global_State *g) { + minor2inc(L, g, KGC_INC); + entergen(L, g); } /* -** Set debt for the next minor collection, which will happen when -** memory grows 'genminormul'%. +** After an atomic incremental step from a major collection, +** check whether collector could return to minor collections. +** It checks whether the number of bytes 'tobecollected' +** is greater than 'majorminor'% of the number of bytes added +** since the last collection ('addedbytes'). */ -static void setminordebt (global_State *g) { - luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul)); -} - - -/* -** Does a major collection after last collection was a "bad collection". -** -** When the program is building a big struture, it allocates lots of -** memory but generates very little garbage. In those scenarios, -** the generational mode just wastes time doing small collections, and -** major collections are frequently what we call a "bad collection", a -** collection that frees too few objects. To avoid the cost of switching -** between generational mode and the incremental mode needed for full -** (major) collections, the collector tries to stay in incremental mode -** after a bad collection, and to switch back to generational mode only -** after a "good" collection (one that traverses less than 9/8 objects -** of the previous one). -** The collector must choose whether to stay in incremental mode or to -** switch back to generational mode before sweeping. At this point, it -** does not know the real memory in use, so it cannot use memory to -** decide whether to return to generational mode. Instead, it uses the -** number of objects traversed (returned by 'atomic') as a proxy. The -** field 'g->lastatomic' keeps this count from the last collection. -** ('g->lastatomic != 0' also means that the last collection was bad.) -*/ -static void stepgenfull (lua_State *L, global_State *g) { - lu_mem newatomic; /* count of traversed objects */ - lu_mem lastatomic = g->lastatomic; /* count from last collection */ - if (g->gckind == KGC_GEN) /* still in generational mode? */ - enterinc(g); /* enter incremental mode */ - luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ - newatomic = atomic(L); /* mark everybody */ - if (newatomic < lastatomic + (lastatomic >> 3)) { /* good collection? */ - atomic2gen(L, g); /* return to generational mode */ - setminordebt(g); - } - else { /* another bad collection; stay in incremental mode */ - g->GCestimate = gettotalbytes(g); /* first estimate */; - entersweep(L); - luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ - setpause(g); - g->lastatomic = newatomic; - } -} - - -/* -** Does a generational "step". -** Usually, this means doing a minor collection and setting the debt to -** make another collection when memory grows 'genminormul'% larger. -** -** However, there are exceptions. If memory grows 'genmajormul'% -** larger than it was at the end of the last major collection (kept -** in 'g->GCestimate'), the function does a major collection. At the -** end, it checks whether the major collection was able to free a -** decent amount of memory (at least half the growth in memory since -** previous major collection). If so, the collector keeps its state, -** and the next collection will probably be minor again. Otherwise, -** we have what we call a "bad collection". In that case, set the field -** 'g->lastatomic' to signal that fact, so that the next collection will -** go to 'stepgenfull'. -** -** 'GCdebt <= 0' means an explicit call to GC step with "size" zero; -** in that case, do a minor collection. -*/ -static void genstep (lua_State *L, global_State *g) { - if (g->lastatomic != 0) /* last collection was a bad one? */ - stepgenfull(L, g); /* do a full step */ - else { - lu_mem majorbase = g->GCestimate; /* memory after last major collection */ - lu_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul); - if (g->GCdebt > 0 && gettotalbytes(g) > majorbase + majorinc) { - lu_mem numobjs = fullgen(L, g); /* do a major collection */ - if (gettotalbytes(g) < majorbase + (majorinc / 2)) { - /* collected at least half of memory growth since last major - collection; keep doing minor collections */ - setminordebt(g); - } - else { /* bad collection */ - g->lastatomic = numobjs; /* signal that last collection was bad */ - setpause(g); /* do a long wait for next (major) collection */ - } - } - else { /* regular case; do a minor collection */ - youngcollection(L, g); +static int checkmajorminor (lua_State *L, global_State *g) { + if (g->gckind == KGC_GENMAJOR) { /* generational mode? */ + l_mem numbytes = gettotalbytes(g); + l_mem addedbytes = numbytes - g->GCmajorminor; + l_mem limit = applygcparam(g, MAJORMINOR, addedbytes); + l_mem tobecollected = numbytes - g->GCmarked; + if (tobecollected > limit) { + atomic2gen(L, g); /* return to generational mode */ setminordebt(g); - g->GCestimate = majorbase; /* preserve base value */ + return 1; /* exit incremental collection */ } } - lua_assert(isdecGCmodegen(g)); + g->GCmajorminor = g->GCmarked; /* prepare for next collection */ + return 0; /* stay doing incremental collections */ } /* }====================================================== */ @@ -1345,26 +1494,6 @@ static void genstep (lua_State *L, global_State *g) { */ -/* -** Set the "time" to wait before starting a new GC cycle; cycle will -** start when memory use hits the threshold of ('estimate' * pause / -** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero, -** because Lua cannot even start with less than PAUSEADJ bytes). -*/ -static void setpause (global_State *g) { - l_mem threshold, debt; - int pause = getgcparam(g->gcpause); - l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ - lua_assert(estimate > 0); - threshold = (pause < MAX_LMEM / estimate) /* overflow? */ - ? estimate * pause /* no overflow */ - : MAX_LMEM; /* overflow; truncate to maximum */ - debt = gettotalbytes(g) - threshold; - if (debt > 0) debt = 0; - luaE_setdebt(g, debt); -} - - /* ** Enter first sweep phase. ** The call to 'sweeptolive' makes the pointer point to an object @@ -1399,35 +1528,36 @@ static void deletelist (lua_State *L, GCObject *p, GCObject *limit) { */ void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); + g->gcstp = GCSTPCLS; /* no extra finalizers after here */ luaC_changemode(L, KGC_INC); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); callallpendingfinalizers(L); - deletelist(L, g->allgc, obj2gco(g->mainthread)); - deletelist(L, g->finobj, NULL); + deletelist(L, g->allgc, obj2gco(mainthread(g))); + lua_assert(g->finobj == NULL); /* no new finalizers */ deletelist(L, g->fixedgc, NULL); /* collect fixed objects */ lua_assert(g->strt.nuse == 0); } -static lu_mem atomic (lua_State *L) { +static void atomic (lua_State *L) { global_State *g = G(L); - lu_mem work = 0; GCObject *origweak, *origall; GCObject *grayagain = g->grayagain; /* save original list */ g->grayagain = NULL; lua_assert(g->ephemeron == NULL && g->weak == NULL); - lua_assert(!iswhite(g->mainthread)); + lua_assert(!iswhite(mainthread(g))); g->gcstate = GCSatomic; markobject(g, L); /* mark running thread */ /* registry and global metatables may be changed by API */ markvalue(g, &g->l_registry); markmt(g); /* mark global metatables */ + propagateall(g); /* empties 'gray' list */ /* remark occasional upvalues of (maybe) dead threads */ - work += remarkupvals(g); - work += propagateall(g); /* propagate changes */ + remarkupvals(g); + propagateall(g); /* propagate changes */ g->gray = grayagain; - work += propagateall(g); /* traverse 'grayagain' list */ + propagateall(g); /* traverse 'grayagain' list */ convergeephemerons(g); /* at this point, all strongly accessible objects are marked. */ /* Clear values from weak tables, before checking finalizers */ @@ -1435,138 +1565,197 @@ static lu_mem atomic (lua_State *L) { clearbyvalues(g, g->allweak, NULL); origweak = g->weak; origall = g->allweak; separatetobefnz(g, 0); /* separate objects to be finalized */ - work += markbeingfnz(g); /* mark objects that will be finalized */ - work += propagateall(g); /* remark, to propagate 'resurrection' */ + markbeingfnz(g); /* mark objects that will be finalized */ + propagateall(g); /* remark, to propagate 'resurrection' */ convergeephemerons(g); /* at this point, all resurrected objects are marked. */ /* remove dead objects from weak tables */ - clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron tables */ - clearbykeys(g, g->allweak); /* clear keys from all 'allweak' tables */ + clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron */ + clearbykeys(g, g->allweak); /* clear keys from all 'allweak' */ /* clear values from resurrected weak tables */ clearbyvalues(g, g->weak, origweak); clearbyvalues(g, g->allweak, origall); luaS_clearcache(g); g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ lua_assert(g->gray == NULL); - return work; /* estimate of slots marked by 'atomic' */ } -static int sweepstep (lua_State *L, global_State *g, - int nextstate, GCObject **nextlist) { - if (g->sweepgc) { - l_mem olddebt = g->GCdebt; - int count; - g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX, &count); - g->GCestimate += g->GCdebt - olddebt; /* update estimate */ - return count; - } +/* +** Do a sweep step. The normal case (not fast) sweeps at most GCSWEEPMAX +** elements. The fast case sweeps the whole list. +*/ +static void sweepstep (lua_State *L, global_State *g, + lu_byte nextstate, GCObject **nextlist, int fast) { + if (g->sweepgc) + g->sweepgc = sweeplist(L, g->sweepgc, fast ? MAX_LMEM : GCSWEEPMAX); else { /* enter next state */ g->gcstate = nextstate; g->sweepgc = nextlist; - return 0; /* no work done */ } } -static lu_mem singlestep (lua_State *L) { +/* +** Performs one incremental "step" in an incremental garbage collection. +** For indivisible work, a step goes to the next state. When marking +** (propagating), a step traverses one object. When sweeping, a step +** sweeps GCSWEEPMAX objects, to avoid a big overhead for sweeping +** objects one by one. (Sweeping is inexpensive, no matter the +** object.) When 'fast' is true, 'singlestep' tries to finish a state +** "as fast as possible". In particular, it skips the propagation +** phase and leaves all objects to be traversed by the atomic phase: +** That avoids traversing twice some objects, such as threads and +** weak tables. +*/ + +#define step2pause -3 /* finished collection; entered pause state */ +#define atomicstep -2 /* atomic step */ +#define step2minor -1 /* moved to minor collections */ + + +static l_mem singlestep (lua_State *L, int fast) { global_State *g = G(L); + l_mem stepresult; + lua_assert(!g->gcstopem); /* collector is not reentrant */ + g->gcstopem = 1; /* no emergency collections while collecting */ switch (g->gcstate) { case GCSpause: { restartcollection(g); g->gcstate = GCSpropagate; - return 1; + stepresult = 1; + break; } case GCSpropagate: { - if (g->gray == NULL) { /* no more gray objects? */ + if (fast || g->gray == NULL) { g->gcstate = GCSenteratomic; /* finish propagate phase */ - return 0; + stepresult = 1; } else - return propagatemark(g); /* traverse one gray object */ + stepresult = propagatemark(g); /* traverse one gray object */ + break; } case GCSenteratomic: { - lu_mem work = propagateall(g); /* make sure gray list is empty */ - work += atomic(L); /* work is what was traversed by 'atomic' */ - entersweep(L); - g->GCestimate = gettotalbytes(g); /* first estimate */; - return work; + atomic(L); + if (checkmajorminor(L, g)) + stepresult = step2minor; + else { + entersweep(L); + stepresult = atomicstep; + } + break; } case GCSswpallgc: { /* sweep "regular" objects */ - return sweepstep(L, g, GCSswpfinobj, &g->finobj); + sweepstep(L, g, GCSswpfinobj, &g->finobj, fast); + stepresult = GCSWEEPMAX; + break; } case GCSswpfinobj: { /* sweep objects with finalizers */ - return sweepstep(L, g, GCSswptobefnz, &g->tobefnz); + sweepstep(L, g, GCSswptobefnz, &g->tobefnz, fast); + stepresult = GCSWEEPMAX; + break; } case GCSswptobefnz: { /* sweep objects to be finalized */ - return sweepstep(L, g, GCSswpend, NULL); + sweepstep(L, g, GCSswpend, NULL, fast); + stepresult = GCSWEEPMAX; + break; } case GCSswpend: { /* finish sweeps */ checkSizes(L, g); g->gcstate = GCScallfin; - return 0; + stepresult = GCSWEEPMAX; + break; } - case GCScallfin: { /* call remaining finalizers */ - if (g->tobefnz && !g->gcemergency) { - int n = runafewfinalizers(L, GCFINMAX); - return n * GCFINALIZECOST; + case GCScallfin: { /* call finalizers */ + if (g->tobefnz && !g->gcemergency && luaD_checkminstack(L)) { + g->gcstopem = 0; /* ok collections during finalizers */ + GCTM(L); /* call one finalizer */ + stepresult = CWUFIN; } - else { /* emergency mode or no more finalizers */ + else { /* no more finalizers or emergency mode or no enough stack + to run finalizers */ g->gcstate = GCSpause; /* finish collection */ - return 0; + stepresult = step2pause; } + break; } default: lua_assert(0); return 0; } + g->gcstopem = 0; + return stepresult; } /* -** advances the garbage collector until it reaches a state allowed -** by 'statemask' +** Advances the garbage collector until it reaches the given state. +** (The option 'fast' is only for testing; in normal code, 'fast' +** here is always true.) */ -void luaC_runtilstate (lua_State *L, int statesmask) { +void luaC_runtilstate (lua_State *L, int state, int fast) { global_State *g = G(L); - while (!testbit(statesmask, g->gcstate)) - singlestep(L); + lua_assert(g->gckind == KGC_INC); + while (state != g->gcstate) + singlestep(L, fast); } + /* -** Performs a basic incremental step. The debt and step size are +** Performs a basic incremental step. The step size is ** converted from bytes to "units of work"; then the function loops ** running single steps until adding that many units of work or ** finishing a cycle (pause state). Finally, it sets the debt that ** controls when next step will be performed. */ static void incstep (lua_State *L, global_State *g) { - int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */ - l_mem debt = (g->GCdebt / WORK2MEM) * stepmul; - l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem)) - ? ((cast(l_mem, 1) << g->gcstepsize) / WORK2MEM) * stepmul - : MAX_LMEM; /* overflow; keep maximum value */ - do { /* repeat until pause or enough "credit" (negative debt) */ - lu_mem work = singlestep(L); /* perform one single step */ - debt -= work; - } while (debt > -stepsize && g->gcstate != GCSpause); + l_mem stepsize = applygcparam(g, STEPSIZE, 100); + l_mem work2do = applygcparam(g, STEPMUL, stepsize / cast_int(sizeof(void*))); + l_mem stres; + int fast = (work2do == 0); /* special case: do a full collection */ + do { /* repeat until enough work */ + stres = singlestep(L, fast); /* perform one single step */ + if (stres == step2minor) /* returned to minor collections? */ + return; /* nothing else to be done here */ + else if (stres == step2pause || (stres == atomicstep && !fast)) + break; /* end of cycle or atomic */ + else + work2do -= stres; + } while (fast || work2do > 0); if (g->gcstate == GCSpause) setpause(g); /* pause until next cycle */ - else { - debt = (debt / stepmul) * WORK2MEM; /* convert 'work units' to bytes */ - luaE_setdebt(g, debt); - } + else + luaE_setdebt(g, stepsize); } + +#if !defined(luai_tracegc) +#define luai_tracegc(L,f) ((void)0) +#endif + /* -** performs a basic GC step if collector is running +** Performs a basic GC step if collector is running. (If collector was +** stopped by the user, set a reasonable debt to avoid it being called +** at every single check.) */ void luaC_step (lua_State *L) { global_State *g = G(L); - if (g->gcrunning) { /* running? */ - if(isdecGCmodegen(g)) - genstep(L, g); - else - incstep(L, g); + lua_assert(!g->gcemergency); + if (!gcrunning(g)) { /* not running? */ + if (g->gcstp & GCSTPUSR) /* stopped by the user? */ + luaE_setdebt(g, 20000); + } + else { + luai_tracegc(L, 1); /* for internal debugging */ + switch (g->gckind) { + case KGC_INC: case KGC_GENMAJOR: + incstep(L, g); + break; + case KGC_GENMINOR: + youngcollection(L, g); + setminordebt(g); + break; + } + luai_tracegc(L, 0); /* for internal debugging */ } } @@ -1582,11 +1771,9 @@ static void fullinc (lua_State *L, global_State *g) { if (keepinvariant(g)) /* black objects? */ entersweep(L); /* sweep everything to turn them back to white */ /* finish any pending sweep phase to start a new cycle */ - luaC_runtilstate(L, bitmask(GCSpause)); - luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ - /* estimate must be correct after a full GC cycle */ - lua_assert(g->GCestimate == gettotalbytes(g)); - luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ + luaC_runtilstate(L, GCSpause, 1); + luaC_runtilstate(L, GCScallfin, 1); /* run up to finalizers */ + luaC_runtilstate(L, GCSpause, 1); /* finish collection */ setpause(g); } @@ -1599,11 +1786,16 @@ static void fullinc (lua_State *L, global_State *g) { void luaC_fullgc (lua_State *L, int isemergency) { global_State *g = G(L); lua_assert(!g->gcemergency); - g->gcemergency = isemergency; /* set flag */ - if (g->gckind == KGC_INC) - fullinc(L, g); - else - fullgen(L, g); + g->gcemergency = cast_byte(isemergency); /* set flag */ + switch (g->gckind) { + case KGC_GENMINOR: fullgen(L, g); break; + case KGC_INC: fullinc(L, g); break; + case KGC_GENMAJOR: + g->gckind = KGC_INC; + fullinc(L, g); + g->gckind = KGC_GENMAJOR; + break; + } g->gcemergency = 0; } diff --git a/lgc.h b/lgc.h index 9ba7ecb050..ee0541793b 100644 --- a/lgc.h +++ b/lgc.h @@ -8,20 +8,24 @@ #define lgc_h +#include + + #include "lobject.h" #include "lstate.h" /* -** Collectable objects may have one of three colors: white, which -** means the object is not marked; gray, which means the -** object is marked, but its references may be not marked; and -** black, which means that the object and all its references are marked. -** The main invariant of the garbage collector, while marking objects, -** is that a black object can never point to a white one. Moreover, -** any gray object must be in a "gray list" (gray, grayagain, weak, -** allweak, ephemeron) so that it can be visited again before finishing -** the collection cycle. These lists have no meaning when the invariant -** is not being enforced (e.g., sweep phase). +** Collectable objects may have one of three colors: white, which means +** the object is not marked; gray, which means the object is marked, but +** its references may be not marked; and black, which means that the +** object and all its references are marked. The main invariant of the +** garbage collector, while marking objects, is that a black object can +** never point to a white one. Moreover, any gray object must be in a +** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it +** can be visited again before finishing the collection cycle. (Open +** upvalues are an exception to this rule, as they are attached to +** a corresponding thread.) These lists have no meaning when the +** invariant is not being enforced (e.g., sweep phase). */ @@ -45,10 +49,10 @@ /* ** macro to tell when main invariant (white objects cannot point to black -** ones) must be kept. During a collection, the sweep -** phase may break the invariant, as objects turned white may point to -** still-black objects. The invariant is restored when sweep ends and -** all objects are white again. +** ones) must be kept. During a collection, the sweep phase may break +** the invariant, as objects turned white may point to still-black +** objects. The invariant is restored when sweep ends and all objects +** are white again. */ #define keepinvariant(g) ((g)->gcstate <= GCSatomic) @@ -69,14 +73,16 @@ /* ** Layout for bit use in 'marked' field. First three bits are -** used for object "age" in generational mode. Last bit is free -** to be used by respective objects. +** used for object "age" in generational mode. Last bit is used +** by tests. */ #define WHITE0BIT 3 /* object is white (type 0) */ #define WHITE1BIT 4 /* object is white (type 1) */ #define BLACKBIT 5 /* object is black */ #define FINALIZEDBIT 6 /* object has been marked for finalization */ +#define TESTBIT 7 + #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) @@ -94,7 +100,8 @@ #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) #define changewhite(x) ((x)->marked ^= WHITEBITS) -#define gray2black(x) l_setbit((x)->marked, BLACKBIT) +#define nw2black(x) \ + check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT)) #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS) @@ -114,69 +121,144 @@ #define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a)) #define isold(o) (getage(o) > G_SURVIVAL) -#define changeage(o,f,t) \ - check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) +/* +** In generational mode, objects are created 'new'. After surviving one +** cycle, they become 'survival'. Both 'new' and 'survival' can point +** to any other object, as they are traversed at the end of the cycle. +** We call them both 'young' objects. +** If a survival object survives another cycle, it becomes 'old1'. +** 'old1' objects can still point to survival objects (but not to +** new objects), so they still must be traversed. After another cycle +** (that, being old, 'old1' objects will "survive" no matter what) +** finally the 'old1' object becomes really 'old', and then they +** are no more traversed. +** +** To keep its invariants, the generational mode uses the same barriers +** also used by the incremental mode. If a young object is caught in a +** forward barrier, it cannot become old immediately, because it can +** still point to other young objects. Instead, it becomes 'old0', +** which in the next cycle becomes 'old1'. So, 'old0' objects is +** old but can point to new and survival objects; 'old1' is old +** but cannot point to new objects; and 'old' cannot point to any +** young object. +** +** If any old object ('old0', 'old1', 'old') is caught in a back +** barrier, it becomes 'touched1' and goes into a gray list, to be +** visited at the end of the cycle. There it evolves to 'touched2', +** which can point to survivals but not to new objects. In yet another +** cycle then it becomes 'old' again. +** +** The generational mode must also control the colors of objects, +** because of the barriers. While the mutator is running, young objects +** are kept white. 'old', 'old1', and 'touched2' objects are kept black, +** as they cannot point to new objects; exceptions are threads and open +** upvalues, which age to 'old1' and 'old' but are kept gray. 'old0' +** objects may be gray or black, as in the incremental mode. 'touched1' +** objects are kept gray, as they must be visited again at the end of +** the cycle. +*/ + + +/* +** {====================================================== +** Default Values for GC parameters +** ======================================================= +*/ -/* Default Values for GC parameters */ -#define LUAI_GENMAJORMUL 100 +/* +** Minor collections will shift to major ones after LUAI_MINORMAJOR% +** bytes become old. +*/ +#define LUAI_MINORMAJOR 70 + +/* +** Major collections will shift to minor ones after a collection +** collects at least LUAI_MAJORMINOR% of the new bytes. +*/ +#define LUAI_MAJORMINOR 50 + +/* +** A young (minor) collection will run after creating LUAI_GENMINORMUL% +** new bytes. +*/ #define LUAI_GENMINORMUL 20 -/* wait memory to double before starting new cycle */ -#define LUAI_GCPAUSE 200 + +/* incremental */ + +/* Number of bytes must be LUAI_GCPAUSE% before starting new cycle */ +#define LUAI_GCPAUSE 250 /* -** some gc parameters are stored divided by 4 to allow a maximum value -** larger than 1000 in a 'lu_byte'. +** Step multiplier: The collector handles LUAI_GCMUL% work units for +** each new allocated word. (Each "work unit" corresponds roughly to +** sweeping one object or traversing one slot.) */ -#define getgcparam(p) ((p) * 4) -#define setgcparam(p,v) ((p) = (v) / 4) +#define LUAI_GCMUL 200 + +/* How many bytes to allocate before next GC step */ +#define LUAI_GCSTEPSIZE (200 * sizeof(Table)) -#define LUAI_GCMUL 100 -/* how much to allocate before next GC step (log2) */ -#define LUAI_GCSTEPSIZE 13 /* 8 KB */ +#define setgcparam(g,p,v) (g->gcparams[LUA_GCP##p] = luaO_codeparam(v)) +#define applygcparam(g,p,x) luaO_applyparam(g->gcparams[LUA_GCP##p], x) + +/* }====================================================== */ /* -** Check whether the declared GC mode is generational. While in -** generational mode, the collector can go temporarily to incremental -** mode to improve performance. This is signaled by 'g->lastatomic != 0'. +** Control when GC is running: */ -#define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0) +#define GCSTPUSR 1 /* bit true when GC stopped by user */ +#define GCSTPGC 2 /* bit true when GC stopped by itself */ +#define GCSTPCLS 4 /* bit true when closing Lua state */ +#define gcrunning(g) ((g)->gcstp == 0) + /* -** Does one step of collection when debt becomes positive. 'pre'/'pos' +** Does one step of collection when debt becomes zero. 'pre'/'pos' ** allows some adjustments to be done only when needed. macro ** 'condchangemem' is used only for heavy tests (forcing a full ** GC cycle on every opportunity) */ + +#if !defined(HARDMEMTESTS) +#define condchangemem(L,pre,pos,emg) ((void)0) +#else +#define condchangemem(L,pre,pos,emg) \ + { if (gcrunning(G(L))) { pre; luaC_fullgc(L, emg); pos; } } +#endif + #define luaC_condGC(L,pre,pos) \ - { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ - condchangemem(L,pre,pos); } + { if (G(L)->GCdebt <= 0) { pre; luaC_step(L); pos;}; \ + condchangemem(L,pre,pos,0); } /* more often than not, 'pre'/'pos' are empty */ #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) -#define luaC_barrier(L,p,v) ( \ - (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ - luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) - -#define luaC_barrierback(L,p,v) ( \ - (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ - luaC_barrierback_(L,p) : cast_void(0)) - #define luaC_objbarrier(L,p,o) ( \ (isblack(p) && iswhite(o)) ? \ luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) +#define luaC_barrier(L,p,v) ( \ + iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0)) + +#define luaC_objbarrierback(L,p,o) ( \ + (isblack(p) && iswhite(o)) ? luaC_barrierback_(L,p) : cast_void(0)) + +#define luaC_barrierback(L,p,v) ( \ + iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0)) + LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); LUAI_FUNC void luaC_freeallobjects (lua_State *L); LUAI_FUNC void luaC_step (lua_State *L); -LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); +LUAI_FUNC void luaC_runtilstate (lua_State *L, int state, int fast); LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); -LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); +LUAI_FUNC GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz); +LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, lu_byte tt, size_t sz, + size_t offset); LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); diff --git a/linit.c b/linit.c index 69808f84f4..00d06f7ecb 100644 --- a/linit.c +++ b/linit.c @@ -8,21 +8,6 @@ #define linit_c #define LUA_LIB -/* -** If you embed Lua in your program and need to open the standard -** libraries, call luaL_openlibs in your program. If you need a -** different set of libraries, copy this file to your project and edit -** it to suit your needs. -** -** You can also *preload* libraries, so that a later 'require' can -** open the library, which is already linked to the application. -** For that, do the following code: -** -** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); -** lua_pushcfunction(L, luaopen_modname); -** lua_setfield(L, -2, modname); -** lua_pop(L, 1); // remove PRELOAD table -*/ #include "lprefix.h" @@ -33,33 +18,46 @@ #include "lualib.h" #include "lauxlib.h" +#include "llimits.h" /* -** these libs are loaded by lua.c and are readily available to any Lua -** program +** Standard Libraries. (Must be listed in the same ORDER of their +** respective constants LUA_K.) */ -static const luaL_Reg loadedlibs[] = { +static const luaL_Reg stdlibs[] = { {LUA_GNAME, luaopen_base}, {LUA_LOADLIBNAME, luaopen_package}, {LUA_COLIBNAME, luaopen_coroutine}, - {LUA_TABLIBNAME, luaopen_table}, + {LUA_DBLIBNAME, luaopen_debug}, {LUA_IOLIBNAME, luaopen_io}, + {LUA_MATHLIBNAME, luaopen_math}, {LUA_OSLIBNAME, luaopen_os}, {LUA_STRLIBNAME, luaopen_string}, - {LUA_MATHLIBNAME, luaopen_math}, + {LUA_TABLIBNAME, luaopen_table}, {LUA_UTF8LIBNAME, luaopen_utf8}, - {LUA_DBLIBNAME, luaopen_debug}, {NULL, NULL} }; -LUALIB_API void luaL_openlibs (lua_State *L) { +/* +** require and preload selected standard libraries +*/ +LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) { + int mask; const luaL_Reg *lib; - /* "require" functions from 'loadedlibs' and set results to global table */ - for (lib = loadedlibs; lib->func; lib++) { - luaL_requiref(L, lib->name, lib->func, 1); - lua_pop(L, 1); /* remove lib */ + luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); + for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) { + if (load & mask) { /* selected? */ + luaL_requiref(L, lib->name, lib->func, 1); /* require library */ + lua_pop(L, 1); /* remove result from the stack */ + } + else if (preload & mask) { /* selected? */ + lua_pushcfunction(L, lib->func); + lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */ + } } + lua_assert((mask >> 1) == LUA_UTF8LIBK); + lua_pop(L, 1); /* remove PRELOAD table */ } diff --git a/liolib.c b/liolib.c index 7d6d51e672..57615e6f32 100644 --- a/liolib.c +++ b/liolib.c @@ -21,8 +21,7 @@ #include "lauxlib.h" #include "lualib.h" - - +#include "llimits.h" /* @@ -39,7 +38,7 @@ /* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ static int l_checkmode (const char *mode) { return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && - (*mode != '+' || (++mode, 1)) && /* skip if char is '+' */ + (*mode != '+' || ((void)(++mode), 1)) && /* skip if char is '+' */ (strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */ } @@ -64,6 +63,12 @@ static int l_checkmode (const char *mode) { #define l_popen(L,c,m) (_popen(c,m)) #define l_pclose(L,file) (_pclose(file)) +#if !defined(l_checkmodep) +/* Windows accepts "[rw][bt]?" as valid modes */ +#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && \ + (m[1] == '\0' || ((m[1] == 'b' || m[1] == 't') && m[2] == '\0'))) +#endif + #else /* }{ */ /* ISO C definitions */ @@ -77,6 +82,12 @@ static int l_checkmode (const char *mode) { #endif /* } */ + +#if !defined(l_checkmodep) +/* By default, Lua accepts only "r" or "w" as valid modes */ +#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0') +#endif + /* }====================================================== */ @@ -103,7 +114,7 @@ static int l_checkmode (const char *mode) { #if !defined(l_fseek) /* { */ -#if defined(LUA_USE_POSIX) /* { */ +#if defined(LUA_USE_POSIX) || defined(LUA_USE_OFF_T) /* { */ #include @@ -153,7 +164,7 @@ static int io_type (lua_State *L) { luaL_checkany(L, 1); p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); if (p == NULL) - lua_pushnil(L); /* not a file */ + luaL_pushfail(L); /* not a file */ else if (isclosed(p)) lua_pushliteral(L, "closed file"); else @@ -174,7 +185,7 @@ static int f_tostring (lua_State *L) { static FILE *tofile (lua_State *L) { LStream *p = tolstream(L); - if (isclosed(p)) + if (l_unlikely(isclosed(p))) luaL_error(L, "attempt to use a closed file"); lua_assert(p->f); return p->f; @@ -215,7 +226,7 @@ static int f_close (lua_State *L) { static int io_close (lua_State *L) { if (lua_isnone(L, 1)) /* no argument? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ + lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use default output */ return f_close(L); } @@ -233,8 +244,8 @@ static int f_gc (lua_State *L) { */ static int io_fclose (lua_State *L) { LStream *p = tolstream(L); - int res = fclose(p->f); - return luaL_fileresult(L, (res == 0), NULL); + errno = 0; + return luaL_fileresult(L, (fclose(p->f) == 0), NULL); } @@ -249,7 +260,7 @@ static LStream *newfile (lua_State *L) { static void opencheck (lua_State *L, const char *fname, const char *mode) { LStream *p = newfile(L); p->f = fopen(fname, mode); - if (p->f == NULL) + if (l_unlikely(p->f == NULL)) luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); } @@ -260,6 +271,7 @@ static int io_open (lua_State *L) { LStream *p = newfile(L); const char *md = mode; /* to traverse/check mode */ luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); + errno = 0; p->f = fopen(filename, mode); return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } @@ -270,6 +282,7 @@ static int io_open (lua_State *L) { */ static int io_pclose (lua_State *L) { LStream *p = tolstream(L); + errno = 0; return luaL_execresult(L, l_pclose(L, p->f)); } @@ -278,6 +291,8 @@ static int io_popen (lua_State *L) { const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); + luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); + errno = 0; p->f = l_popen(L, filename, mode); p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; @@ -286,6 +301,7 @@ static int io_popen (lua_State *L) { static int io_tmpfile (lua_State *L) { LStream *p = newfile(L); + errno = 0; p->f = tmpfile(); return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; } @@ -295,8 +311,8 @@ static FILE *getiofile (lua_State *L, const char *findex) { LStream *p; lua_getfield(L, LUA_REGISTRYINDEX, findex); p = (LStream *)lua_touserdata(L, -1); - if (isclosed(p)) - luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN); + if (l_unlikely(isclosed(p))) + luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); return p->f; } @@ -338,7 +354,7 @@ static int io_readline (lua_State *L); #define MAXARGLINE 250 /* -** Auxiliar function to create the iteration function for 'lines'. +** Auxiliary function to create the iteration function for 'lines'. ** The iteration function is a closure over 'io_readline', with ** the following upvalues: ** 1) The file being read (first value in the stack) @@ -422,12 +438,12 @@ typedef struct { ** Add current char to buffer (if not out of space) and read next one */ static int nextc (RN *rn) { - if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */ + if (l_unlikely(rn->n >= L_MAXLENNUM)) { /* buffer overflow? */ rn->buff[0] = '\0'; /* invalidate result */ return 0; /* fail */ } else { - rn->buff[rn->n++] = rn->c; /* save current char */ + rn->buff[rn->n++] = cast_char(rn->c); /* save current char */ rn->c = l_getc(rn->f); /* read next one */ return 1; } @@ -485,8 +501,8 @@ static int read_number (lua_State *L, FILE *f) { ungetc(rn.c, rn.f); /* unread look-ahead char */ l_unlockfile(rn.f); rn.buff[rn.n] = '\0'; /* finish string */ - if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */ - return 1; /* ok */ + if (l_likely(lua_stringtonumber(L, rn.buff))) + return 1; /* ok, it is a valid number */ else { /* invalid format */ lua_pushnil(L); /* "result" to be removed */ return 0; /* read fails */ @@ -504,19 +520,19 @@ static int test_eof (lua_State *L, FILE *f) { static int read_line (lua_State *L, FILE *f, int chop) { luaL_Buffer b; - int c = '\0'; + int c; luaL_buffinit(L, &b); - while (c != EOF && c != '\n') { /* repeat until end of line */ - char *buff = luaL_prepbuffer(&b); /* preallocate buffer */ - int i = 0; + do { /* may need to read several chunks to get whole line */ + char *buff = luaL_prepbuffer(&b); /* preallocate buffer space */ + unsigned i = 0; l_lockfile(f); /* no memory errors can happen inside the lock */ while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') - buff[i++] = c; + buff[i++] = cast_char(c); /* read up to end of line or buffer limit */ l_unlockfile(f); luaL_addsize(&b, i); - } + } while (c != EOF && c != '\n'); /* repeat until end of line */ if (!chop && c == '\n') /* want a newline and have one? */ - luaL_addchar(&b, c); /* add ending newline to result */ + luaL_addchar(&b, '\n'); /* add ending newline to result */ luaL_pushresult(&b); /* close buffer */ /* return ok if read something (either a newline or something else) */ return (c == '\n' || lua_rawlen(L, -1) > 0); @@ -553,6 +569,7 @@ static int g_read (lua_State *L, FILE *f, int first) { int nargs = lua_gettop(L) - 1; int n, success; clearerr(f); + errno = 0; if (nargs == 0) { /* no arguments? */ success = read_line(L, f, 1); n = first + 1; /* to return 1 result */ @@ -593,7 +610,7 @@ static int g_read (lua_State *L, FILE *f, int first) { return luaL_fileresult(L, 0, NULL); if (!success) { lua_pop(L, 1); /* remove last result */ - lua_pushnil(L); /* push nil instead */ + luaL_pushfail(L); /* push nil instead */ } return n - first; } @@ -626,7 +643,7 @@ static int io_readline (lua_State *L) { lua_assert(n > 0); /* should return at least a nil */ if (lua_toboolean(L, -n)) /* read at least one value? */ return n; /* return them */ - else { /* first result is nil: EOF or error */ + else { /* first result is false: EOF or error */ if (n > 1) { /* is there error information? */ /* 2nd result is error message */ return luaL_error(L, "%s", lua_tostring(L, -n + 1)); @@ -645,25 +662,28 @@ static int io_readline (lua_State *L) { static int g_write (lua_State *L, FILE *f, int arg) { int nargs = lua_gettop(L) - arg; - int status = 1; - for (; nargs--; arg++) { - if (lua_type(L, arg) == LUA_TNUMBER) { - /* optimization: could be done exactly as for strings */ - int len = lua_isinteger(L, arg) - ? fprintf(f, LUA_INTEGER_FMT, - (LUAI_UACINT)lua_tointeger(L, arg)) - : fprintf(f, LUA_NUMBER_FMT, - (LUAI_UACNUMBER)lua_tonumber(L, arg)); - status = status && (len > 0); + size_t totalbytes = 0; /* total number of bytes written */ + errno = 0; + for (; nargs--; arg++) { /* for each argument */ + char buff[LUA_N2SBUFFSZ]; + const char *s; + size_t numbytes; /* bytes written in one call to 'fwrite' */ + size_t len = lua_numbertocstring(L, arg, buff); /* try as a number */ + if (len > 0) { /* did conversion work (value was a number)? */ + s = buff; + len--; } - else { - size_t l; - const char *s = luaL_checklstring(L, arg, &l); - status = status && (fwrite(s, sizeof(char), l, f) == l); + else /* must be a string */ + s = luaL_checklstring(L, arg, &len); + numbytes = fwrite(s, sizeof(char), len, f); + totalbytes += numbytes; + if (numbytes < len) { /* write error? */ + int n = luaL_fileresult(L, 0, NULL); + lua_pushinteger(L, cast_st2S(totalbytes)); + return n + 1; /* return fail, error msg., error code, and counter */ } } - if (status) return 1; /* file handle already on stack top */ - else return luaL_fileresult(L, status, NULL); + return 1; /* no errors; file handle already on stack top */ } @@ -688,8 +708,9 @@ static int f_seek (lua_State *L) { l_seeknum offset = (l_seeknum)p3; luaL_argcheck(L, (lua_Integer)offset == p3, 3, "not an integer in proper range"); + errno = 0; op = l_fseek(f, offset, mode[op]); - if (op) + if (l_unlikely(op)) return luaL_fileresult(L, 0, NULL); /* error */ else { lua_pushinteger(L, (lua_Integer)l_ftell(f)); @@ -704,19 +725,26 @@ static int f_setvbuf (lua_State *L) { FILE *f = tofile(L); int op = luaL_checkoption(L, 2, NULL, modenames); lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); - int res = setvbuf(f, NULL, mode[op], (size_t)sz); + int res; + errno = 0; + res = setvbuf(f, NULL, mode[op], (size_t)sz); return luaL_fileresult(L, res == 0, NULL); } - -static int io_flush (lua_State *L) { - return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); +static int aux_flush (lua_State *L, FILE *f) { + errno = 0; + return luaL_fileresult(L, fflush(f) == 0, NULL); } static int f_flush (lua_State *L) { - return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); + return aux_flush(L, tofile(L)); +} + + +static int io_flush (lua_State *L) { + return aux_flush(L, getiofile(L, IO_OUTPUT)); } @@ -742,14 +770,23 @@ static const luaL_Reg iolib[] = { /* ** methods for file handles */ -static const luaL_Reg flib[] = { - {"close", f_close}, - {"flush", f_flush}, - {"lines", f_lines}, +static const luaL_Reg meth[] = { {"read", f_read}, + {"write", f_write}, + {"lines", f_lines}, + {"flush", f_flush}, {"seek", f_seek}, + {"close", f_close}, {"setvbuf", f_setvbuf}, - {"write", f_write}, + {NULL, NULL} +}; + + +/* +** metamethods for file handles +*/ +static const luaL_Reg metameth[] = { + {"__index", NULL}, /* placeholder */ {"__gc", f_gc}, {"__close", f_gc}, {"__tostring", f_tostring}, @@ -758,11 +795,12 @@ static const luaL_Reg flib[] = { static void createmeta (lua_State *L) { - luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ - lua_pushvalue(L, -1); /* push metatable */ - lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ - luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */ - lua_pop(L, 1); /* pop new metatable */ + luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */ + luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */ + luaL_newlibtable(L, meth); /* create method table */ + luaL_setfuncs(L, meth, 0); /* add file methods to method table */ + lua_setfield(L, -2, "__index"); /* metatable.__index = method table */ + lua_pop(L, 1); /* pop metatable */ } @@ -772,7 +810,7 @@ static void createmeta (lua_State *L) { static int io_noclose (lua_State *L) { LStream *p = tolstream(L); p->closef = &io_noclose; /* keep file opened */ - lua_pushnil(L); + luaL_pushfail(L); lua_pushliteral(L, "cannot close standard file"); return 2; } diff --git a/ljumptab.h b/ljumptab.h index 9fa72a73c8..52fa6d746e 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -16,12 +16,12 @@ #define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i)); -static void *disptab[NUM_OPCODES] = { +static const void *const disptab[NUM_OPCODES] = { #if 0 ** you can update the following list with this command: ** -** sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h +** sed -n '/^OP_/!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h ** #endif @@ -30,7 +30,9 @@ static void *disptab[NUM_OPCODES] = { &&L_OP_LOADF, &&L_OP_LOADK, &&L_OP_LOADKX, -&&L_OP_LOADBOOL, +&&L_OP_LOADFALSE, +&&L_OP_LFALSESKIP, +&&L_OP_LOADTRUE, &&L_OP_LOADNIL, &&L_OP_GETUPVAL, &&L_OP_SETUPVAL, @@ -45,12 +47,6 @@ static void *disptab[NUM_OPCODES] = { &&L_OP_NEWTABLE, &&L_OP_SELF, &&L_OP_ADDI, -&&L_OP_SUBI, -&&L_OP_MULI, -&&L_OP_MODI, -&&L_OP_POWI, -&&L_OP_DIVI, -&&L_OP_IDIVI, &&L_OP_ADDK, &&L_OP_SUBK, &&L_OP_MULK, @@ -61,8 +57,8 @@ static void *disptab[NUM_OPCODES] = { &&L_OP_BANDK, &&L_OP_BORK, &&L_OP_BXORK, -&&L_OP_SHRI, &&L_OP_SHLI, +&&L_OP_SHRI, &&L_OP_ADD, &&L_OP_SUB, &&L_OP_MUL, @@ -75,6 +71,9 @@ static void *disptab[NUM_OPCODES] = { &&L_OP_BXOR, &&L_OP_SHL, &&L_OP_SHR, +&&L_OP_MMBIN, +&&L_OP_MMBINI, +&&L_OP_MMBINK, &&L_OP_UNM, &&L_OP_BNOT, &&L_OP_NOT, @@ -99,8 +98,6 @@ static void *disptab[NUM_OPCODES] = { &&L_OP_RETURN, &&L_OP_RETURN0, &&L_OP_RETURN1, -&&L_OP_FORLOOP1, -&&L_OP_FORPREP1, &&L_OP_FORLOOP, &&L_OP_FORPREP, &&L_OP_TFORPREP, @@ -109,7 +106,9 @@ static void *disptab[NUM_OPCODES] = { &&L_OP_SETLIST, &&L_OP_CLOSURE, &&L_OP_VARARG, -&&L_OP_PREPVARARG, +&&L_OP_GETVARG, +&&L_OP_ERRNNIL, +&&L_OP_VARARGPREP, &&L_OP_EXTRAARG }; diff --git a/llex.c b/llex.c index 1539f5258c..f8bb3ea4b4 100644 --- a/llex.c +++ b/llex.c @@ -29,9 +29,14 @@ -#define next(ls) (ls->current = zgetc(ls->z)) +#define next(ls) (ls->current = zgetc(ls->z)) +/* minimum size for string buffer */ +#if !defined(LUA_MINBUFFER) +#define LUA_MINBUFFER 32 +#endif + #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') @@ -39,7 +44,7 @@ /* ORDER RESERVED */ static const char *const luaX_tokens [] = { "and", "break", "do", "else", "elseif", - "end", "false", "for", "function", "goto", "if", + "end", "false", "for", "function", "global", "goto", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", "//", "..", "...", "==", ">=", "<=", "~=", @@ -57,10 +62,10 @@ static l_noret lexerror (LexState *ls, const char *msg, int token); static void save (LexState *ls, int c) { Mbuffer *b = ls->buff; if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { - size_t newsize; - if (luaZ_sizebuffer(b) >= MAX_SIZE/2) + size_t newsize = luaZ_sizebuffer(b); /* get old size */; + if (newsize >= (MAX_SIZE/3 * 2)) /* larger than MAX_SIZE/1.5 ? */ lexerror(ls, "lexical element too long", 0); - newsize = luaZ_sizebuffer(b) * 2; + newsize += (newsize >> 1); /* new size is 1.5 times the old one */ luaZ_resizebuffer(ls->L, b, newsize); } b->buffer[luaZ_bufflen(b)++] = cast_char(c); @@ -81,8 +86,10 @@ void luaX_init (lua_State *L) { const char *luaX_token2str (LexState *ls, int token) { if (token < FIRST_RESERVED) { /* single-byte symbols? */ - lua_assert(token == cast_uchar(token)); - return luaO_pushfstring(ls->L, "'%c'", token); + if (lisprint(token)) + return luaO_pushfstring(ls->L, "'%c'", token); + else /* control character */ + return luaO_pushfstring(ls->L, "'<\\%d>'", token); } else { const char *s = luaX_tokens[token - FIRST_RESERVED]; @@ -120,27 +127,34 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { /* -** creates a new string and anchors it in scanner's table so that -** it will not be collected until the end of the compilation -** (by that time it should be anchored somewhere) +** Anchors a string in scanner's table so that it will not be collected +** until the end of the compilation; by that time it should be anchored +** somewhere. It also internalizes long strings, ensuring there is only +** one copy of each unique string. */ -TString *luaX_newstring (LexState *ls, const char *str, size_t l) { +static TString *anchorstr (LexState *ls, TString *ts) { lua_State *L = ls->L; - TValue *o; /* entry for 'str' */ - TString *ts = luaS_newlstr(L, str, l); /* create new string */ - setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */ - o = luaH_set(L, ls->h, s2v(L->top - 1)); - if (isempty(o)) { /* not in use yet? */ - /* boolean value does not need GC barrier; - table is not a metatable, so it does not need to invalidate cache */ - setbvalue(o, 1); /* t[string] = true */ + TValue oldts; + int tag = luaH_getstr(ls->h, ts, &oldts); + if (!tagisempty(tag)) /* string already present? */ + return tsvalue(&oldts); /* use stored value */ + else { /* create a new entry */ + TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ + setsvalue(L, stv, ts); /* push (anchor) the string on the stack */ + luaH_set(L, ls->h, stv, stv); /* t[string] = string */ + /* table is not a metatable, so it does not need to invalidate cache */ luaC_checkGC(L); + L->top.p--; /* remove string from stack */ + return ts; } - else { /* string already present */ - ts = keystrval(nodefromval(o)); /* re-use value previously stored */ - } - L->top--; /* remove string from stack */ - return ts; +} + + +/* +** Creates a new string and anchors it in scanner's table. +*/ +TString *luaX_newstring (LexState *ls, const char *str, size_t l) { + return anchorstr(ls, luaS_newlstr(ls->L, str, l)); } @@ -154,7 +168,7 @@ static void inclinenumber (LexState *ls) { next(ls); /* skip '\n' or '\r' */ if (currIsNewline(ls) && ls->current != old) next(ls); /* skip '\n\r' or '\r\n' */ - if (++ls->linenumber >= MAX_INT) + if (++ls->linenumber >= INT_MAX) lexerror(ls, "chunk has too many lines", 0); } @@ -170,7 +184,15 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, ls->linenumber = 1; ls->lastline = 1; ls->source = source; - ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */ + /* all three strings here ("_ENV", "break", "global") were fixed, + so they cannot be collected */ + ls->envn = luaS_newliteral(L, LUA_ENV); /* get env string */ + ls->brkn = luaS_newliteral(L, "break"); /* get "break" string */ +#if defined(LUA_COMPAT_GLOBAL) + /* compatibility mode: "global" is not a reserved word */ + ls->glbn = luaS_newliteral(L, "global"); /* get "global" string */ + ls->glbn->extra = 0; /* mark it as not reserved */ +#endif luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ } @@ -208,8 +230,16 @@ static int check_next2 (LexState *ls, const char *set) { /* LUA_NUMBER */ /* -** this function is quite liberal in what it accepts, as 'luaO_str2num' -** will reject ill-formed numerals. +** This function is quite liberal in what it accepts, as 'luaO_str2num' +** will reject ill-formed numerals. Roughly, it accepts the following +** pattern: +** +** %d(%x|%.|([Ee][+-]?))* | 0[Xx](%x|%.|([Pp][+-]?))* +** +** The only tricky part is to accept [+-] only after a valid exponent +** mark, to avoid reading '3-4' or '0xe+1' as a single number. +** +** The caller might have already read an initial dot. */ static int read_numeral (LexState *ls, SemInfo *seminfo) { TValue obj; @@ -220,14 +250,14 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */ expo = "Pp"; for (;;) { - if (check_next2(ls, expo)) /* exponent part? */ + if (check_next2(ls, expo)) /* exponent mark? */ check_next2(ls, "-+"); /* optional exponent sign */ - if (lisxdigit(ls->current)) - save_and_next(ls); - else if (ls->current == '.') + else if (lisxdigit(ls->current) || ls->current == '.') /* '%x|%.' */ save_and_next(ls); else break; } + if (lislalpha(ls->current)) /* is numeral touching a letter? */ + save_and_next(ls); /* force an error */ save(ls, '\0'); if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */ lexerror(ls, "malformed number", TK_FLT); @@ -244,9 +274,10 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { /* -** reads a sequence '[=*[' or ']=*]', leaving the last bracket. -** If sequence is well formed, return its number of '='s + 2; otherwise, -** return 1 if there is no '='s or 0 otherwise (an unfinished '[==...'). +** read a sequence '[=*[' or ']=*]', leaving the last bracket. If +** sequence is well formed, return its number of '='s + 2; otherwise, +** return 1 if it is a single bracket (no '='s and no 2nd bracket); +** otherwise (an unfinished '[==...') return 0. */ static size_t skip_sep (LexState *ls) { size_t count = 0; @@ -326,16 +357,21 @@ static int readhexaesc (LexState *ls) { } -static unsigned long readutf8esc (LexState *ls) { - unsigned long r; - int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */ +/* +** When reading a UTF-8 escape sequence, save everything to the buffer +** for error reporting in case of errors; 'i' counts the number of +** saved characters, so that they can be removed if case of success. +*/ +static l_uint32 readutf8esc (LexState *ls) { + l_uint32 r; + int i = 4; /* number of chars to be removed: start with #"\u{X" */ save_and_next(ls); /* skip 'u' */ esccheck(ls, ls->current == '{', "missing '{'"); - r = gethexa(ls); /* must have at least one digit */ - while ((save_and_next(ls), lisxdigit(ls->current))) { + r = cast_uint(gethexa(ls)); /* must have at least one digit */ + while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) { i++; + esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large"); r = (r << 4) + luaO_hexavalue(ls->current); - esccheck(ls, r <= 0x7FFFFFFFu, "UTF-8 value too large"); } esccheck(ls, ls->current == '}', "missing '}'"); next(ls); /* skip '}' */ @@ -471,34 +507,34 @@ static int llex (LexState *ls, SemInfo *seminfo) { } case '=': { next(ls); - if (check_next1(ls, '=')) return TK_EQ; + if (check_next1(ls, '=')) return TK_EQ; /* '==' */ else return '='; } case '<': { next(ls); - if (check_next1(ls, '=')) return TK_LE; - else if (check_next1(ls, '<')) return TK_SHL; + if (check_next1(ls, '=')) return TK_LE; /* '<=' */ + else if (check_next1(ls, '<')) return TK_SHL; /* '<<' */ else return '<'; } case '>': { next(ls); - if (check_next1(ls, '=')) return TK_GE; - else if (check_next1(ls, '>')) return TK_SHR; + if (check_next1(ls, '=')) return TK_GE; /* '>=' */ + else if (check_next1(ls, '>')) return TK_SHR; /* '>>' */ else return '>'; } case '/': { next(ls); - if (check_next1(ls, '/')) return TK_IDIV; + if (check_next1(ls, '/')) return TK_IDIV; /* '//' */ else return '/'; } case '~': { next(ls); - if (check_next1(ls, '=')) return TK_NE; + if (check_next1(ls, '=')) return TK_NE; /* '~=' */ else return '~'; } case ':': { next(ls); - if (check_next1(ls, ':')) return TK_DBCOLON; + if (check_next1(ls, ':')) return TK_DBCOLON; /* '::' */ else return ':'; } case '"': case '\'': { /* short literal strings */ @@ -528,16 +564,17 @@ static int llex (LexState *ls, SemInfo *seminfo) { do { save_and_next(ls); } while (lislalnum(ls->current)); - ts = luaX_newstring(ls, luaZ_buffer(ls->buff), - luaZ_bufflen(ls->buff)); - seminfo->ts = ts; - if (isreserved(ts)) /* reserved word? */ + /* find or create string */ + ts = luaS_newlstr(ls->L, luaZ_buffer(ls->buff), + luaZ_bufflen(ls->buff)); + if (isreserved(ts)) /* reserved word? */ return ts->extra - 1 + FIRST_RESERVED; else { + seminfo->ts = anchorstr(ls, ts); return TK_NAME; } } - else { /* single-char tokens (+ - / ...) */ + else { /* single-char tokens ('+', '*', '%', '{', '}', ...) */ int c = ls->current; next(ls); return c; diff --git a/llex.h b/llex.h index d1a4cba7c2..37016e8a3f 100644 --- a/llex.h +++ b/llex.h @@ -7,11 +7,17 @@ #ifndef llex_h #define llex_h +#include + #include "lobject.h" #include "lzio.h" -#define FIRST_RESERVED 257 +/* +** Single-char tokens (terminal symbols) are represented by their own +** numeric code. Other tokens start at the following value. +*/ +#define FIRST_RESERVED (UCHAR_MAX + 1) #if !defined(LUA_ENV) @@ -27,8 +33,8 @@ enum RESERVED { /* terminal symbols denoted by reserved words */ TK_AND = FIRST_RESERVED, TK_BREAK, TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, - TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, - TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, + TK_GLOBAL, TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, + TK_REPEAT, TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, /* other terminal symbols */ TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_SHL, TK_SHR, @@ -53,7 +59,7 @@ typedef struct Token { } Token; -/* state of the lexer plus state of the parser when shared by all +/* state of the scanner plus state of the parser when shared by all functions */ typedef struct LexState { int current; /* current character (charint) */ @@ -69,6 +75,8 @@ typedef struct LexState { struct Dyndata *dyd; /* dynamic structures used by the parser */ TString *source; /* current source name */ TString *envn; /* environment variable name */ + TString *brkn; /* "break" name (used as a label) */ + TString *glbn; /* "global" name (when not a reserved word) */ } LexState; diff --git a/llimits.h b/llimits.h index 9d35d1c774..fc5cb276f6 100644 --- a/llimits.h +++ b/llimits.h @@ -14,50 +14,50 @@ #include "lua.h" + +#define l_numbits(t) cast_int(sizeof(t) * CHAR_BIT) + /* -** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count -** the total memory used by Lua (in bytes). Usually, 'size_t' and +** 'l_mem' is a signed integer big enough to count the total memory +** used by Lua. (It is signed due to the use of debt in several +** computations.) 'lu_mem' is a corresponding unsigned type. Usually, ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. */ #if defined(LUAI_MEM) /* { external definitions? */ -typedef LUAI_UMEM lu_mem; typedef LUAI_MEM l_mem; -#elif LUAI_BITSINT >= 32 /* }{ */ -typedef size_t lu_mem; +typedef LUAI_UMEM lu_mem; +#elif LUAI_IS32INT /* }{ */ typedef ptrdiff_t l_mem; +typedef size_t lu_mem; #else /* 16-bit ints */ /* }{ */ -typedef unsigned long lu_mem; typedef long l_mem; +typedef unsigned long lu_mem; #endif /* } */ +#define MAX_LMEM \ + cast(l_mem, (cast(lu_mem, 1) << (l_numbits(l_mem) - 1)) - 1) + /* chars used as small naturals (so that 'char' is reserved for characters) */ typedef unsigned char lu_byte; typedef signed char ls_byte; -/* maximum value for size_t */ -#define MAX_SIZET ((size_t)(~(size_t)0)) - -/* maximum size visible for Lua (must be representable in a lua_Integer */ -#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ - : (size_t)(LUA_MAXINTEGER)) - - -#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)) - -#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1)) +/* Type for thread status/error codes */ +typedef lu_byte TStatus; +/* The C API still uses 'int' for status/error codes */ +#define APIstatus(st) cast_int(st) -#define MAX_INT INT_MAX /* maximum value of an int */ - +/* maximum value for size_t */ +#define MAX_SIZET ((size_t)(~(size_t)0)) /* -** floor of the log2 of the maximum signed value for integral type 't'. -** (That is, maximum 'n' such that '2^n' fits in the given signed type.) +** Maximum size for strings and userdata visible for Lua; should be +** representable as a lua_Integer and as a size_t. */ -#define log2maxs(t) (sizeof(t) * 8 - 2) - +#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ + : cast_sizet(LUA_MAXINTEGER)) /* ** test whether an unsigned value is a power of 2 (or zero) @@ -65,12 +65,29 @@ typedef signed char ls_byte; #define ispow2(x) (((x) & ((x) - 1)) == 0) +/* number of chars of a literal string without the ending \0 */ +#define LL(x) (sizeof(x)/sizeof(char) - 1) + + /* -** conversion of pointer to unsigned integer: -** this is for hashing only; there is no problem if the integer -** cannot hold the whole pointer value +** conversion of pointer to unsigned integer: this is for hashing only; +** there is no problem if the integer cannot hold the whole pointer +** value. (In strict ISO C this may cause undefined behavior, but no +** actual machine seems to bother.) */ -#define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX)) +#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ + __STDC_VERSION__ >= 199901L +#include +#if defined(UINTPTR_MAX) /* even in C99 this type is optional */ +#define L_P2I uintptr_t +#else /* no 'intptr'? */ +#define L_P2I uintmax_t /* use the largest available integer */ +#endif +#else /* C89 option */ +#define L_P2I size_t +#endif + +#define point2uint(p) cast_uint((L_P2I)(p) & UINT_MAX) @@ -79,25 +96,25 @@ typedef LUAI_UACNUMBER l_uacNumber; typedef LUAI_UACINT l_uacInt; -/* internal assertions for in-house debugging */ +/* +** Internal assertions for in-house debugging +*/ +#if defined LUAI_ASSERT +#undef NDEBUG +#include +#define lua_assert(c) assert(c) +#define assert_code(c) c +#endif + #if defined(lua_assert) -#define check_exp(c,e) (lua_assert(c), (e)) -/* to avoid problems with conditions too long */ -#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0)) #else #define lua_assert(c) ((void)0) -#define check_exp(c,e) (e) -#define lua_longassert(c) ((void)0) -#endif - -/* -** assertion for checking API calls -*/ -#if !defined(luai_apicheck) -#define luai_apicheck(l,e) lua_assert(e) +#define assert_code(c) ((void)0) #endif -#define api_check(l,e,msg) luai_apicheck(l,(e) && msg) +#define check_exp(c,e) (lua_assert(c), (e)) +/* to avoid problems with conditions too long */ +#define lua_longassert(c) assert_code((c) ? (void)0 : lua_assert(0)) /* macro to avoid warnings about unused variables */ @@ -113,12 +130,15 @@ typedef LUAI_UACINT l_uacInt; #define cast_voidp(i) cast(void *, (i)) #define cast_num(i) cast(lua_Number, (i)) #define cast_int(i) cast(int, (i)) +#define cast_short(i) cast(short, (i)) #define cast_uint(i) cast(unsigned int, (i)) #define cast_byte(i) cast(lu_byte, (i)) #define cast_uchar(i) cast(unsigned char, (i)) #define cast_char(i) cast(char, (i)) #define cast_charp(i) cast(char *, (i)) #define cast_sizet(i) cast(size_t, (i)) +#define cast_Integer(i) cast(lua_Integer, (i)) +#define cast_Inst(i) cast(Instruction, (i)) /* cast a signed lua_Integer to lua_Unsigned */ @@ -135,21 +155,37 @@ typedef LUAI_UACINT l_uacInt; #define l_castU2S(i) ((lua_Integer)(i)) #endif +/* +** cast a size_t to lua_Integer: These casts are always valid for +** sizes of Lua objects (see MAX_SIZE) +*/ +#define cast_st2S(sz) ((lua_Integer)(sz)) + +/* Cast a ptrdiff_t to size_t, when it is known that the minuend +** comes from the subtrahend (the base) +*/ +#define ct_diff2sz(df) ((size_t)(df)) + +/* ptrdiff_t to lua_Integer */ +#define ct_diff2S(df) cast_st2S(ct_diff2sz(df)) /* -** macros to improve jump prediction (used mainly for error handling) +** Special type equivalent to '(void*)' for functions (to suppress some +** warnings when converting function pointers) */ -#if !defined(likely) +typedef void (*voidf)(void); +/* +** Macro to convert pointer-to-void* to pointer-to-function. This cast +** is undefined according to ISO C, but POSIX assumes that it works. +** (The '__extension__' in gnu compilers is only to avoid warnings.) +*/ #if defined(__GNUC__) -#define likely(x) (__builtin_expect(((x) != 0), 1)) -#define unlikely(x) (__builtin_expect(((x) != 0), 0)) +#define cast_func(p) (__extension__ (voidf)(p)) #else -#define likely(x) (x) -#define unlikely(x) (x) +#define cast_func(p) ((voidf)(p)) #endif -#endif /* @@ -169,116 +205,28 @@ typedef LUAI_UACINT l_uacInt; /* -** maximum depth for nested C calls and syntactical nested non-terminals -** in a program. (Value must fit in an unsigned short int. It must also -** be compatible with the size of the C stack.) +** Inline functions */ -#if !defined(LUAI_MAXCCALLS) -#define LUAI_MAXCCALLS 2200 +#if !defined(LUA_USE_C89) +#define l_inline inline +#elif defined(__GNUC__) +#define l_inline __inline__ +#else +#define l_inline /* empty */ #endif +#define l_sinline static l_inline /* -** type for virtual-machine instructions; -** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) +** An unsigned with (at least) 4 bytes */ -#if LUAI_BITSINT >= 32 +#if LUAI_IS32INT typedef unsigned int l_uint32; #else typedef unsigned long l_uint32; #endif -typedef l_uint32 Instruction; - - - -/* -** Maximum length for short strings, that is, strings that are -** internalized. (Cannot be smaller than reserved words or tags for -** metamethods, as these strings must be internalized; -** #("function") = 8, #("__newindex") = 10.) -*/ -#if !defined(LUAI_MAXSHORTLEN) -#define LUAI_MAXSHORTLEN 40 -#endif - - -/* -** Initial size for the string table (must be power of 2). -** The Lua core alone registers ~50 strings (reserved words + -** metaevent keys + a few others). Libraries would typically add -** a few dozens more. -*/ -#if !defined(MINSTRTABSIZE) -#define MINSTRTABSIZE 128 -#endif - - -/* -** Size of cache for strings in the API. 'N' is the number of -** sets (better be a prime) and "M" is the size of each set (M == 1 -** makes a direct cache.) -*/ -#if !defined(STRCACHE_N) -#define STRCACHE_N 53 -#define STRCACHE_M 2 -#endif - - -/* minimum size for string buffer */ -#if !defined(LUA_MINBUFFER) -#define LUA_MINBUFFER 32 -#endif - - -/* -** macros that are executed whenever program enters the Lua core -** ('lua_lock') and leaves the core ('lua_unlock') -*/ -#if !defined(lua_lock) -#define lua_lock(L) ((void) 0) -#define lua_unlock(L) ((void) 0) -#endif - -/* -** macro executed during Lua functions at points where the -** function can yield. -*/ -#if !defined(luai_threadyield) -#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} -#endif - - -/* -** these macros allow user-specific actions when a thread is -** created/deleted/resumed/yielded. -*/ -#if !defined(luai_userstateopen) -#define luai_userstateopen(L) ((void)L) -#endif - -#if !defined(luai_userstateclose) -#define luai_userstateclose(L) ((void)L) -#endif - -#if !defined(luai_userstatethread) -#define luai_userstatethread(L,L1) ((void)L) -#endif - -#if !defined(luai_userstatefree) -#define luai_userstatefree(L,L1) ((void)L) -#endif - -#if !defined(luai_userstateresume) -#define luai_userstateresume(L,n) ((void)L) -#endif - -#if !defined(luai_userstateyield) -#define luai_userstateyield(L,n) ((void)L) -#endif - - /* ** The luai_num* macros define the primitive operations over numbers. @@ -313,7 +261,8 @@ typedef l_uint32 Instruction; /* exponentiation */ #if !defined(luai_numpow) -#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) +#define luai_numpow(L,a,b) \ + ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b)) #endif /* the others are quite standard operations */ @@ -325,29 +274,84 @@ typedef l_uint32 Instruction; #define luai_numeq(a,b) ((a)==(b)) #define luai_numlt(a,b) ((a)<(b)) #define luai_numle(a,b) ((a)<=(b)) +#define luai_numgt(a,b) ((a)>(b)) +#define luai_numge(a,b) ((a)>=(b)) #define luai_numisnan(a) (!luai_numeq((a), (a))) #endif +/* +** lua_numbertointeger converts a float number with an integral value +** to an integer, or returns 0 if the float is not within the range of +** a lua_Integer. (The range comparisons are tricky because of +** rounding. The tests here assume a two-complement representation, +** where MININTEGER always has an exact representation as a float; +** MAXINTEGER may not have one, and therefore its conversion to float +** may have an ill-defined value.) +*/ +#define lua_numbertointeger(n,p) \ + ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ + (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ + (*(p) = (LUA_INTEGER)(n), 1)) + /* -** macro to control inclusion of some hard tests on stack reallocation +** LUAI_FUNC is a mark for all extern functions that are not to be +** exported to outside modules. +** LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, +** none of which to be exported to outside modules (LUAI_DDEF for +** definitions and LUAI_DDEC for declarations). +** Elf and MACH/gcc (versions 3.2 and later) mark them as "hidden" to +** optimize access when Lua is compiled as a shared library. Not all elf +** targets support this attribute. Unfortunately, gcc does not offer +** a way to check whether the target offers that support, and those +** without support give a warning about it. To avoid these warnings, +** change to the default definition. */ -#if !defined(HARDSTACKTESTS) -#define condmovestack(L,pre,pos) ((void)0) +#if !defined(LUAI_FUNC) + +#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ + (defined(__ELF__) || defined(__MACH__)) +#define LUAI_FUNC __attribute__((visibility("internal"))) extern #else -/* realloc stack keeping its size */ -#define condmovestack(L,pre,pos) \ - { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_, 0); pos; } +#define LUAI_FUNC extern #endif -#if !defined(HARDMEMTESTS) -#define condchangemem(L,pre,pos) ((void)0) -#else -#define condchangemem(L,pre,pos) \ - { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } } +#define LUAI_DDEC(dec) LUAI_FUNC dec +#define LUAI_DDEF /* empty */ + #endif + +/* Give these macros simpler names for internal use */ +#define l_likely(x) luai_likely(x) +#define l_unlikely(x) luai_unlikely(x) + +/* +** {================================================================== +** "Abstraction Layer" for basic report of messages and errors +** =================================================================== +*/ + +/* print a string */ +#if !defined(lua_writestring) +#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) +#endif + +/* print a newline and flush the output */ +#if !defined(lua_writeline) +#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) #endif + +/* print an error message */ +#if !defined(lua_writestringerror) +#define lua_writestringerror(s,p) \ + (fprintf(stderr, (s), (p)), fflush(stderr)) +#endif + +/* }================================================================== */ + +#endif + diff --git a/lmathlib.c b/lmathlib.c index e3ccc3ee58..a6b13f969c 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -20,6 +20,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" #undef PI @@ -37,31 +38,37 @@ static int math_abs (lua_State *L) { return 1; } + static int math_sin (lua_State *L) { lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); return 1; } + static int math_cos (lua_State *L) { lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); return 1; } + static int math_tan (lua_State *L) { lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); return 1; } + static int math_asin (lua_State *L) { lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); return 1; } + static int math_acos (lua_State *L) { lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); return 1; } + static int math_atan (lua_State *L) { lua_Number y = luaL_checknumber(L, 1); lua_Number x = luaL_optnumber(L, 2, 1); @@ -73,11 +80,11 @@ static int math_atan (lua_State *L) { static int math_toint (lua_State *L) { int valid; lua_Integer n = lua_tointegerx(L, 1, &valid); - if (valid) + if (l_likely(valid)) lua_pushinteger(L, n); else { luaL_checkany(L, 1); - lua_pushnil(L); /* value is not convertible to integer */ + luaL_pushfail(L); /* value is not convertible to integer */ } return 1; } @@ -105,7 +112,7 @@ static int math_floor (lua_State *L) { static int math_ceil (lua_State *L) { if (lua_isinteger(L, 1)) - lua_settop(L, 1); /* integer is its own ceil */ + lua_settop(L, 1); /* integer is its own ceiling */ else { lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1)); pushnumint(L, d); @@ -166,6 +173,7 @@ static int math_ult (lua_State *L) { return 1; } + static int math_log (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); lua_Number res; @@ -175,7 +183,8 @@ static int math_log (lua_State *L) { lua_Number base = luaL_checknumber(L, 2); #if !defined(LUA_USE_C89) if (base == l_mathop(2.0)) - res = l_mathop(log2)(x); else + res = l_mathop(log2)(x); + else #endif if (base == l_mathop(10.0)) res = l_mathop(log10)(x); @@ -186,22 +195,42 @@ static int math_log (lua_State *L) { return 1; } + static int math_exp (lua_State *L) { lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); return 1; } + static int math_deg (lua_State *L) { lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI)); return 1; } + static int math_rad (lua_State *L) { lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0))); return 1; } +static int math_frexp (lua_State *L) { + lua_Number x = luaL_checknumber(L, 1); + int ep; + lua_pushnumber(L, l_mathop(frexp)(x, &ep)); + lua_pushinteger(L, ep); + return 2; +} + + +static int math_ldexp (lua_State *L) { + lua_Number x = luaL_checknumber(L, 1); + int ep = (int)luaL_checkinteger(L, 2); + lua_pushnumber(L, l_mathop(ldexp)(x, ep)); + return 1; +} + + static int math_min (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ int imin = 1; /* index of current minimum value */ @@ -235,7 +264,7 @@ static int math_type (lua_State *L) { lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float"); else { luaL_checkany(L, 1); - lua_pushnil(L); + luaL_pushfail(L); } return 1; } @@ -248,8 +277,17 @@ static int math_type (lua_State *L) { ** =================================================================== */ +/* +** This code uses lots of shifts. ISO C does not allow shifts greater +** than or equal to the width of the type being shifted, so some shifts +** are written in convoluted ways to match that restriction. For +** preprocessor tests, it assumes a width of 32 bits, so the maximum +** shift there is 31 bits. +*/ + + /* number of binary digits in the mantissa of a float */ -#define FIGS l_mathlim(MANT_DIG) +#define FIGS l_floatatt(MANT_DIG) #if FIGS > 64 /* there are only 64 random bits; use them all */ @@ -266,20 +304,23 @@ static int math_type (lua_State *L) { /* try to find an integer type with at least 64 bits */ -#if (LONG_MAX >> 31 >> 31) >= 1 +#if ((ULONG_MAX >> 31) >> 31) >= 3 /* 'long' has at least 64 bits */ #define Rand64 unsigned long +#define SRand64 long #elif !defined(LUA_USE_C89) && defined(LLONG_MAX) /* there is a 'long long' type (which must have at least 64 bits) */ #define Rand64 unsigned long long +#define SRand64 long long -#elif (LUA_MAXINTEGER >> 31 >> 31) >= 1 +#elif ((LUA_MAXUNSIGNED >> 31) >> 31) >= 3 -/* 'lua_Integer' has at least 64 bits */ +/* 'lua_Unsigned' has at least 64 bits */ #define Rand64 lua_Unsigned +#define SRand64 lua_Integer #endif @@ -318,23 +359,30 @@ static Rand64 nextrand (Rand64 *state) { } -/* must take care to not shift stuff by more than 63 slots */ - - /* ** Convert bits from a random integer into a float in the ** interval [0,1), getting the higher FIG bits from the ** random unsigned integer and converting that to a float. +** Some old Microsoft compilers cannot cast an unsigned long +** to a floating-point number, so we use a signed long as an +** intermediary. When lua_Number is float or double, the shift ensures +** that 'sx' is non negative; in that case, a good compiler will remove +** the correction. */ /* must throw out the extra (64 - FIGS) bits */ -#define shift64_FIG (64 - FIGS) +#define shift64_FIG (64 - FIGS) -/* to scale to [0, 1), multiply by scaleFIG = 2^(-FIGS) */ +/* 2^(-FIGS) == 2^-1 / 2^(FIGS-1) */ #define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1))) static lua_Number I2d (Rand64 x) { - return (lua_Number)(trim64(x) >> shift64_FIG) * scaleFIG; + SRand64 sx = (SRand64)(trim64(x) >> shift64_FIG); + lua_Number res = (lua_Number)(sx) * scaleFIG; + if (sx < 0) + res += l_mathop(1.0); /* correct the two's complement if negative */ + lua_assert(0 <= res && res < 1); + return res; } /* convert a 'Rand64' to a 'lua_Unsigned' */ @@ -346,25 +394,17 @@ static lua_Number I2d (Rand64 x) { #else /* no 'Rand64' }{ */ -/* get an integer with at least 32 bits */ -#if (INT_MAX >> 30) >= 1 -typedef unsigned int lu_int32; -#else -typedef unsigned long lu_int32; -#endif - - /* ** Use two 32-bit integers to represent a 64-bit quantity. */ typedef struct Rand64 { - lu_int32 h; /* higher half */ - lu_int32 l; /* lower half */ + l_uint32 h; /* higher half */ + l_uint32 l; /* lower half */ } Rand64; /* -** If 'lu_int32' has more than 32 bits, the extra bits do not interfere +** If 'l_uint32' has more than 32 bits, the extra bits do not interfere ** with the 32 initial bits, except in a right shift and comparisons. ** Moreover, the final result has to discard the extra bits. */ @@ -378,7 +418,7 @@ typedef struct Rand64 { */ /* build a new Rand64 value */ -static Rand64 packI (lu_int32 h, lu_int32 l) { +static Rand64 packI (l_uint32 h, l_uint32 l) { Rand64 result; result.h = h; result.l = l; @@ -451,7 +491,7 @@ static Rand64 nextrand (Rand64 *state) { */ /* an unsigned 1 with proper type */ -#define UONE ((lu_int32)1) +#define UONE ((l_uint32)1) #if FIGS <= 32 @@ -470,11 +510,9 @@ static lua_Number I2d (Rand64 x) { #else /* 32 < FIGS <= 64 */ -/* must take care to not shift stuff by more than 31 slots */ - /* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */ #define scaleFIG \ - ((lua_Number)1.0 / (UONE << 30) / 8.0 / (UONE << (FIGS - 33))) + (l_mathop(1.0) / (UONE << 30) / l_mathop(8.0) / (UONE << (FIGS - 33))) /* ** use FIGS - 32 bits from lower half, throwing out the other @@ -485,7 +523,7 @@ static lua_Number I2d (Rand64 x) { /* ** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32) */ -#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * 2.0) +#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * l_mathop(2.0)) static lua_Number I2d (Rand64 x) { @@ -499,12 +537,12 @@ static lua_Number I2d (Rand64 x) { /* convert a 'Rand64' to a 'lua_Unsigned' */ static lua_Unsigned I2UInt (Rand64 x) { - return ((lua_Unsigned)trim32(x.h) << 31 << 1) | (lua_Unsigned)trim32(x.l); + return (((lua_Unsigned)trim32(x.h) << 31) << 1) | (lua_Unsigned)trim32(x.l); } /* convert a 'lua_Unsigned' to a 'Rand64' */ static Rand64 Int2I (lua_Unsigned n) { - return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n); + return packI((l_uint32)((n >> 31) >> 1), (l_uint32)n); } #endif /* } */ @@ -522,30 +560,20 @@ typedef struct { ** Project the random integer 'ran' into the interval [0, n]. ** Because 'ran' has 2^B possible values, the projection can only be ** uniform when the size of the interval is a power of 2 (exact -** division). To get a uniform projection into [0, n], we first compute -** 'lim', the smallest Mersenne number not smaller than 'n'. We then -** project 'ran' into the interval [0, lim]. If the result is inside -** [0, n], we are done. Otherwise, we try with another 'ran', until we -** have a result inside the interval. +** division). So, to get a uniform projection into [0, n], we +** first compute 'lim', the smallest Mersenne number not smaller than +** 'n'. We then project 'ran' into the interval [0, lim]. If the result +** is inside [0, n], we are done. Otherwise, we try with another 'ran', +** until we have a result inside the interval. */ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, RanState *state) { - lua_Unsigned lim = n; - if ((lim & (lim + 1)) > 0) { /* 'lim + 1' is not a power of 2? */ - /* compute the smallest (2^b - 1) not smaller than 'n' */ - lim |= (lim >> 1); - lim |= (lim >> 2); - lim |= (lim >> 4); - lim |= (lim >> 8); - lim |= (lim >> 16); -#if (LUA_MAXINTEGER >> 30 >> 1) > 0 - lim |= (lim >> 32); /* integer type has more than 32 bits */ -#endif - } - lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */ - && lim >= n /* not smaller than 'n', */ - && (lim == 0 || (lim >> 1) < n)); /* and it is the smallest one */ - while ((ran &= lim) > n) /* project 'ran' into [0..lim] */ + lua_Unsigned lim = n; /* to compute the Mersenne number */ + int sh; /* how much to spread bits to the right in 'lim' */ + /* spread '1' bits in 'lim' until it becomes a Mersenne number */ + for (sh = 1; (lim & (lim + 1)) != 0; sh *= 2) + lim |= (lim >> sh); /* spread '1's to the right */ + while ((ran &= lim) > n) /* project 'ran' into [0..lim] and test */ ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */ return ran; } @@ -565,7 +593,7 @@ static int math_random (lua_State *L) { low = 1; up = luaL_checkinteger(L, 1); if (up == 0) { /* single 0 as argument? */ - lua_pushinteger(L, I2UInt(rv)); /* full random integer */ + lua_pushinteger(L, l_castU2S(I2UInt(rv))); /* full random integer */ return 1; } break; @@ -580,13 +608,14 @@ static int math_random (lua_State *L) { /* random integer in the interval [low, up] */ luaL_argcheck(L, low <= up, 1, "interval is empty"); /* project random integer into the interval [0, up - low] */ - p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state); - lua_pushinteger(L, p + (lua_Unsigned)low); + p = project(I2UInt(rv), l_castS2U(up) - l_castS2U(low), state); + lua_pushinteger(L, l_castU2S(p + l_castS2U(low))); return 1; } -static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { +static void setseed (lua_State *L, Rand64 *state, + lua_Unsigned n1, lua_Unsigned n2) { int i; state[0] = Int2I(n1); state[1] = Int2I(0xff); /* avoid a zero state */ @@ -594,31 +623,24 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { state[3] = Int2I(0); for (i = 0; i < 16; i++) nextrand(state); /* discard initial values to "spread" seed */ -} - - -/* -** Set a "random" seed. To get some randomness, use the current time -** and the address of 'L' (in case the machine does address space layout -** randomization). -*/ -static void randseed (lua_State *L, RanState *state) { - lua_Unsigned seed1 = (lua_Unsigned)time(NULL); - lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; - setseed(state->s, seed1, seed2); + lua_pushinteger(L, l_castU2S(n1)); + lua_pushinteger(L, l_castU2S(n2)); } static int math_randomseed (lua_State *L) { RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); - if (lua_isnone(L, 1)) - randseed(L, state); + lua_Unsigned n1, n2; + if (lua_isnone(L, 1)) { + n1 = luaL_makeseed(L); /* "random" seed */ + n2 = I2UInt(nextrand(state->s)); /* in case seed is not that random... */ + } else { - lua_Integer n1 = luaL_checkinteger(L, 1); - lua_Integer n2 = luaL_optinteger(L, 2, 0); - setseed(state->s, n1, n2); + n1 = l_castS2U(luaL_checkinteger(L, 1)); + n2 = l_castS2U(luaL_optinteger(L, 2, 0)); } - return 0; + setseed(L, state->s, n1, n2); + return 2; /* return seeds */ } @@ -634,7 +656,8 @@ static const luaL_Reg randfuncs[] = { */ static void setrandfunc (lua_State *L) { RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); - randseed(L, state); /* initialize with a "random" seed */ + setseed(L, state->s, luaL_makeseed(L), 0); /* initialize with random seed */ + lua_pop(L, 2); /* remove pushed seeds */ luaL_setfuncs(L, randfuncs, 1); } @@ -670,20 +693,6 @@ static int math_pow (lua_State *L) { return 1; } -static int math_frexp (lua_State *L) { - int e; - lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); - lua_pushinteger(L, e); - return 2; -} - -static int math_ldexp (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - int ep = (int)luaL_checkinteger(L, 2); - lua_pushnumber(L, l_mathop(ldexp)(x, ep)); - return 1; -} - static int math_log10 (lua_State *L) { lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); return 1; @@ -706,7 +715,9 @@ static const luaL_Reg mathlib[] = { {"tointeger", math_toint}, {"floor", math_floor}, {"fmod", math_fmod}, + {"frexp", math_frexp}, {"ult", math_ult}, + {"ldexp", math_ldexp}, {"log", math_log}, {"max", math_max}, {"min", math_min}, @@ -722,8 +733,6 @@ static const luaL_Reg mathlib[] = { {"sinh", math_sinh}, {"tanh", math_tanh}, {"pow", math_pow}, - {"frexp", math_frexp}, - {"ldexp", math_ldexp}, {"log10", math_log10}, #endif /* placeholders */ diff --git a/lmem.c b/lmem.c index 53f8dcb9d6..de8503d91b 100644 --- a/lmem.c +++ b/lmem.c @@ -22,45 +22,86 @@ #include "lstate.h" -#if defined(HARDMEMTESTS) -#define hardtest(L,os,s) /* force a GC whenever possible */ \ - if ((s) > (os) && (G(L))->gcrunning) luaC_fullgc(L, 1); -#else -#define hardtest(L,os,s) ((void)0) -#endif - - /* ** About the realloc function: -** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); +** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize); ** ('osize' is the old size, 'nsize' is the new size) ** -** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no -** matter 'x'). +** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL. +** Particularly, frealloc(ud, NULL, 0, 0) does nothing, +** which is equivalent to free(NULL) in ISO C. ** -** * frealloc(ud, p, x, 0) frees the block 'p' -** (in this specific case, frealloc must return NULL); -** particularly, frealloc(ud, NULL, 0, 0) does nothing -** (which is equivalent to free(NULL) in ISO C) +** - frealloc(ud, NULL, x, s) creates a new block of size 's' +** (no matter 'x'). Returns NULL if it cannot create the new block. ** -** frealloc returns NULL if it cannot create or reallocate the area -** (any reallocation to an equal or smaller size cannot fail!) +** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from +** size 'x' to size 'y'. Returns NULL if it cannot reallocate the +** block to the new size. +*/ + + +/* +** Macro to call the allocation function. */ +#define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) + + +/* +** When an allocation fails, it will try again after an emergency +** collection, except when it cannot run a collection. The GC should +** not be called while the state is not fully built, as the collector +** is not yet fully initialized. Also, it should not be called when +** 'gcstopem' is true, because then the interpreter is in the middle of +** a collection step. +*/ +#define cantryagain(g) (completestate(g) && !g->gcstopem) + + + + +#if defined(EMERGENCYGCTESTS) +/* +** First allocation will fail except when freeing a block (frees never +** fail) and when it cannot try again; this fail will trigger 'tryagain' +** and a full GC cycle at every allocation. +*/ +static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { + if (ns > 0 && cantryagain(g)) + return NULL; /* fail */ + else /* normal allocation */ + return callfrealloc(g, block, os, ns); +} +#else +#define firsttry(g,block,os,ns) callfrealloc(g, block, os, ns) +#endif + + + +/* +** {================================================================== +** Functions to allocate/deallocate arrays for the Parser +** =================================================================== +*/ +/* +** Minimum size for arrays during parsing, to avoid overhead of +** reallocating to size 1, then 2, and then 4. All these arrays +** will be reallocated to exact sizes or erased when parsing ends. +*/ #define MINSIZEARRAY 4 void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, - int size_elems, int limit, const char *what) { + unsigned size_elems, int limit, const char *what) { void *newblock; int size = *psize; if (nelems + 1 <= size) /* does one extra element still fit? */ return block; /* nothing to be done */ if (size >= limit / 2) { /* cannot double it? */ - if (unlikely(size >= limit)) /* cannot grow even a little? */ + if (l_unlikely(size >= limit)) /* cannot grow even a little? */ luaG_runerror(L, "too many %s (limit is %d)", what, limit); size = limit; /* still have at least one free place */ } @@ -71,32 +112,32 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, } lua_assert(nelems + 1 <= size && size <= limit); /* 'limit' ensures that multiplication will not overflow */ - newblock = luaM_realloc_(L, block, cast_sizet(*psize) * size_elems, - cast_sizet(size) * size_elems); - if (unlikely(newblock == NULL)) - luaM_error(L); + newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems, + cast_sizet(size) * size_elems); *psize = size; /* update only when everything else is OK */ return newblock; } +/* +** In prototypes, the size of the array is also its number of +** elements (to save memory). So, if it cannot shrink an array +** to its number of elements, the only option is to raise an +** error. +*/ void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, - int final_n, int size_elem) { - global_State *g = G(L); + int final_n, unsigned size_elem) { void *newblock; - size_t oldsize = cast_sizet((*size) * size_elem); - size_t newsize = cast_sizet(final_n * size_elem); + size_t oldsize = cast_sizet(*size) * size_elem; + size_t newsize = cast_sizet(final_n) * size_elem; lua_assert(newsize <= oldsize); - newblock = (*g->frealloc)(g->ud, block, oldsize, newsize); - if (unlikely(newblock == NULL && final_n > 0)) /* allocation failed? */ - luaM_error(L); - else { - g->GCdebt += newsize - oldsize; - *size = final_n; - return newblock; - } + newblock = luaM_saferealloc_(L, block, oldsize, newsize); + *size = final_n; + return newblock; } +/* }================================================================== */ + l_noret luaM_toobig (lua_State *L) { luaG_runerror(L, "memory allocation error: block too big"); @@ -109,45 +150,41 @@ l_noret luaM_toobig (lua_State *L) { void luaM_free_ (lua_State *L, void *block, size_t osize) { global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); - (*g->frealloc)(g->ud, block, osize, 0); - g->GCdebt -= osize; + callfrealloc(g, block, osize, 0); + g->GCdebt += cast(l_mem, osize); } /* -** In case of allocation fail, this function will call the GC to try -** to free some memory and then try the allocation again. -** (It should not be called when shrinking a block, because then the -** interpreter may be in the middle of a collection step.) +** In case of allocation fail, this function will do an emergency +** collection to free some memory and then try the allocation again. */ static void *tryagain (lua_State *L, void *block, size_t osize, size_t nsize) { global_State *g = G(L); - if (ttisnil(&g->nilvalue)) { /* is state fully build? */ + if (cantryagain(g)) { luaC_fullgc(L, 1); /* try to free some memory... */ - return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ + return callfrealloc(g, block, osize, nsize); /* try again */ } - else return NULL; /* cannot free any memory without a full state */ + else return NULL; /* cannot run an emergency collection */ } /* -** generic allocation routine. +** Generic allocation routine. */ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock; global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); - hardtest(L, osize, nsize); - newblock = (*g->frealloc)(g->ud, block, osize, nsize); - if (unlikely(newblock == NULL && nsize > 0)) { - if (nsize > osize) /* not shrinking a block? */ - newblock = tryagain(L, block, osize, nsize); + newblock = firsttry(g, block, osize, nsize); + if (l_unlikely(newblock == NULL && nsize > 0)) { + newblock = tryagain(L, block, osize, nsize); if (newblock == NULL) /* still no memory? */ - return NULL; + return NULL; /* do not update 'GCdebt' */ } lua_assert((nsize == 0) == (newblock == NULL)); - g->GCdebt = (g->GCdebt + nsize) - osize; + g->GCdebt -= cast(l_mem, nsize) - cast(l_mem, osize); return newblock; } @@ -155,25 +192,24 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock = luaM_realloc_(L, block, osize, nsize); - if (unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ + if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ luaM_error(L); return newblock; } void *luaM_malloc_ (lua_State *L, size_t size, int tag) { - hardtest(L, 0, size); if (size == 0) return NULL; /* that's all */ else { global_State *g = G(L); - void *newblock = (*g->frealloc)(g->ud, NULL, tag, size); - if (unlikely(newblock == NULL)) { - newblock = tryagain(L, NULL, tag, size); + void *newblock = firsttry(g, NULL, cast_sizet(tag), size); + if (l_unlikely(newblock == NULL)) { + newblock = tryagain(L, NULL, cast_sizet(tag), size); if (newblock == NULL) luaM_error(L); } - g->GCdebt += size; + g->GCdebt -= cast(l_mem, size); return newblock; } } diff --git a/lmem.h b/lmem.h index 8c75a44beb..dc714fb2e4 100644 --- a/lmem.h +++ b/lmem.h @@ -39,11 +39,11 @@ ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that ** the result is not larger than 'n' and cannot overflow a 'size_t' ** when multiplied by the size of type 't'. (Assumes that 'n' is an -** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) +** 'int' and that 'int' is not larger than 'size_t'.) */ #define luaM_limitN(n,t) \ ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ - cast_uint((MAX_SIZET/sizeof(t)))) + cast_int((MAX_SIZET/sizeof(t)))) /* @@ -57,12 +57,15 @@ #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) -#define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) +#define luaM_newvector(L,n,t) \ + cast(t*, luaM_malloc_(L, cast_sizet(n)*sizeof(t), 0)) #define luaM_newvectorchecked(L,n,t) \ (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) +#define luaM_newblock(L, size) luaM_newvector(L, size, char) + #define luaM_growvector(L,v,nelems,size,t,limit,e) \ ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ luaM_limitN(limit,t),e))) @@ -83,10 +86,10 @@ LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, size_t size); LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, - int *size, int size_elem, int limit, + int *size, unsigned size_elem, int limit, const char *what); LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, - int final_n, int size_elem); + int final_n, unsigned size_elem); LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); #endif diff --git a/loadlib.c b/loadlib.c index a6ce30d4f3..8d2e68e261 100644 --- a/loadlib.c +++ b/loadlib.c @@ -22,15 +22,7 @@ #include "lauxlib.h" #include "lualib.h" - - -/* -** LUA_IGMARK is a mark to ignore all before it when building the -** luaopen_ function name. -*/ -#if !defined (LUA_IGMARK) -#define LUA_IGMARK "-" -#endif +#include "llimits.h" /* @@ -56,10 +48,10 @@ /* -** unique key for table in the registry that keeps handles +** key for table in the registry that keeps handles ** for all loaded C libraries */ -static const int CLIBS = 0; +static const char *const CLIBS = "_CLIBS"; #define LIB_FAIL "open" @@ -67,6 +59,10 @@ static const int CLIBS = 0; #define setprogdir(L) ((void)0) +/* cast void* to a Lua function */ +#define cast_Lfunc(p) cast(lua_CFunction, cast_func(p)) + + /* ** system-dependent functions */ @@ -97,26 +93,13 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym); #if defined(LUA_USE_DLOPEN) /* { */ /* ** {======================================================================== -** This is an implementation of loadlib based on the dlfcn interface. -** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, -** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least -** as an emulation layer on top of native functions. +** This is an implementation of loadlib based on the dlfcn interface, +** which is available in all POSIX systems. ** ========================================================================= */ #include -/* -** Macro to convert pointer-to-void* to pointer-to-function. This cast -** is undefined according to ISO C, but POSIX assumes that it works. -** (The '__extension__' in gnu compilers is only to avoid warnings.) -*/ -#if defined(__GNUC__) -#define cast_func(p) (__extension__ (lua_CFunction)(p)) -#else -#define cast_func(p) ((lua_CFunction)(p)) -#endif - static void lsys_unloadlib (void *lib) { dlclose(lib); @@ -125,14 +108,16 @@ static void lsys_unloadlib (void *lib) { static void *lsys_load (lua_State *L, const char *path, int seeglb) { void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL)); - if (lib == NULL) lua_pushstring(L, dlerror()); + if (l_unlikely(lib == NULL)) + lua_pushstring(L, dlerror()); return lib; } static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = cast_func(dlsym(lib, sym)); - if (f == NULL) lua_pushstring(L, dlerror()); + lua_CFunction f = cast_Lfunc(dlsym(lib, sym)); + if (l_unlikely(f == NULL)) + lua_pushstring(L, dlerror()); return f; } @@ -206,7 +191,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) { static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym); + lua_CFunction f = cast_Lfunc(GetProcAddress((HMODULE)lib, sym)); if (f == NULL) pusherror(L); return f; } @@ -269,8 +254,6 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { #endif -#define AUXMARK "\1" /* auxiliary mark */ - /* ** return registry.LUA_NOENV as a boolean @@ -285,38 +268,60 @@ static int noenv (lua_State *L) { /* -** Set a path +** Set a path. (If using the default path, assume it is a string +** literal in C and create it as an external string.) */ static void setpath (lua_State *L, const char *fieldname, const char *envname, const char *dft) { + const char *dftmark; const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); - const char *path = getenv(nver); /* use versioned name */ - if (path == NULL) /* no environment variable? */ + const char *path = getenv(nver); /* try versioned name */ + if (path == NULL) /* no versioned environment variable? */ path = getenv(envname); /* try unversioned name */ if (path == NULL || noenv(L)) /* no environment variable? */ - lua_pushstring(L, dft); /* use default */ - else { - /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ - path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, - LUA_PATH_SEP AUXMARK LUA_PATH_SEP); - luaL_gsub(L, path, AUXMARK, dft); - lua_remove(L, -2); /* remove result from 1st 'gsub' */ + lua_pushexternalstring(L, dft, strlen(dft), NULL, NULL); /* use default */ + else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL) + lua_pushstring(L, path); /* nothing to change */ + else { /* path contains a ";;": insert default path in its place */ + size_t len = strlen(path); + luaL_Buffer b; + luaL_buffinit(L, &b); + if (path < dftmark) { /* is there a prefix before ';;'? */ + luaL_addlstring(&b, path, ct_diff2sz(dftmark - path)); /* add it */ + luaL_addchar(&b, *LUA_PATH_SEP); + } + luaL_addstring(&b, dft); /* add default */ + if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */ + luaL_addchar(&b, *LUA_PATH_SEP); + luaL_addlstring(&b, dftmark + 2, ct_diff2sz((path + len - 2) - dftmark)); + } + luaL_pushresult(&b); } setprogdir(L); lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ - lua_pop(L, 1); /* pop versioned variable name */ + lua_pop(L, 1); /* pop versioned variable name ('nver') */ } /* }================================================================== */ +/* +** External strings created by DLLs may need the DLL code to be +** deallocated. This implies that a DLL can only be unloaded after all +** its strings were deallocated. To ensure that, we create a 'library +** string' to represent each DLL, and when this string is deallocated +** it closes its corresponding DLL. +** (The string itself is irrelevant; its userdata is the DLL pointer.) +*/ + + /* ** return registry.CLIBS[path] */ static void *checkclib (lua_State *L, const char *path) { void *plib; - lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); + lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); lua_getfield(L, -1, path); plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ lua_pop(L, 2); /* pop CLIBS table and 'plib' */ @@ -325,34 +330,41 @@ static void *checkclib (lua_State *L, const char *path) { /* -** registry.CLIBS[path] = plib -- for queries -** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries +** Deallocate function for library strings. +** Unload the DLL associated with the string being deallocated. */ -static void addtoclib (lua_State *L, const char *path, void *plib) { - lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); - lua_pushlightuserdata(L, plib); - lua_pushvalue(L, -1); - lua_setfield(L, -3, path); /* CLIBS[path] = plib */ - lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */ - lua_pop(L, 1); /* pop CLIBS table */ +static void *freelib (void *ud, void *ptr, size_t osize, size_t nsize) { + /* string itself is irrelevant and static */ + (void)ptr; (void)osize; (void)nsize; + lsys_unloadlib(ud); /* unload library represented by the string */ + return NULL; } /* -** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib -** handles in list CLIBS +** Create a library string that, when deallocated, will unload 'plib' */ -static int gctm (lua_State *L) { - lua_Integer n = luaL_len(L, 1); - for (; n >= 1; n--) { /* for each handle, in reverse order */ - lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ - lsys_unloadlib(lua_touserdata(L, -1)); - lua_pop(L, 1); /* pop handle */ - } - return 0; +static void createlibstr (lua_State *L, void *plib) { + /* common content for all library strings */ + static const char dummy[] = "01234567890"; + lua_pushexternalstring(L, dummy, sizeof(dummy) - 1, freelib, plib); } +/* +** registry.CLIBS[path] = plib -- for queries. +** Also create a reference to strlib, so that the library string will +** only be collected when registry.CLIBS is collected. +*/ +static void addtoclib (lua_State *L, const char *path, void *plib) { + lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); + lua_pushlightuserdata(L, plib); + lua_setfield(L, -2, path); /* CLIBS[path] = plib */ + createlibstr(L, plib); + luaL_ref(L, -2); /* keep library string in CLIBS */ + lua_pop(L, 1); /* pop CLIBS table */ +} + /* error codes for 'lookforfunc' */ #define ERRLIB 1 @@ -366,8 +378,8 @@ static int gctm (lua_State *L) { ** Then, if 'sym' is '*', return true (as library has been loaded). ** Otherwise, look for symbol 'sym' in the library and push a ** C function with that symbol. -** Return 0 and 'true' or a function in the stack; in case of -** errors, return an error code and an error message in the stack. +** Return 0 with 'true' or a function in the stack; in case of +** errors, return an error code with an error message in the stack. */ static int lookforfunc (lua_State *L, const char *path, const char *sym) { void *reg = checkclib(L, path); /* check loaded C libraries */ @@ -394,13 +406,13 @@ static int ll_loadlib (lua_State *L) { const char *path = luaL_checkstring(L, 1); const char *init = luaL_checkstring(L, 2); int stat = lookforfunc(L, path, init); - if (stat == 0) /* no errors? */ + if (l_likely(stat == 0)) /* no errors? */ return 1; /* return the loaded function */ else { /* error; error message is on stack top */ - lua_pushnil(L); + luaL_pushfail(L); lua_insert(L, -2); lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); - return 3; /* return nil, error message, and where */ + return 3; /* return fail, error message, and where */ } } @@ -421,33 +433,42 @@ static int readable (const char *filename) { } -static const char *pushnextfilename (lua_State *L, const char *path) { - const char *l; - if (*path == *LUA_PATH_SEP) - path++; /* skip separator */ - if (*path == '\0') +/* +** Get the next name in '*path' = 'name1;name2;name3;...', changing +** the ending ';' to '\0' to create a zero-terminated string. Return +** NULL when list ends. +*/ +static const char *getnextfilename (char **path, char *end) { + char *sep; + char *name = *path; + if (name == end) return NULL; /* no more names */ - l = strchr(path, *LUA_PATH_SEP); /* find next separator */ - if (l == NULL) /* no more separators? */ - l = path + strlen(path); /* go until the end */ - lua_pushlstring(L, path, l - path); /* file name */ - return l; /* rest of the path */ + else if (*name == '\0') { /* from previous iteration? */ + *name = *LUA_PATH_SEP; /* restore separator */ + name++; /* skip it */ + } + sep = strchr(name, *LUA_PATH_SEP); /* find next separator */ + if (sep == NULL) /* separator not found? */ + sep = end; /* name goes until the end */ + *sep = '\0'; /* finish file name */ + *path = sep; /* will start next search from here */ + return name; } /* ** Given a path such as ";blabla.so;blublu.so", pushes the string ** -** no file 'blabla.so' +** no file 'blabla.so' ** no file 'blublu.so' */ static void pusherrornotfound (lua_State *L, const char *path) { - if (*path == *LUA_PATH_SEP) - path++; /* skip separator */ - lua_pushstring(L, "\n\tno file '"); - luaL_gsub(L, path, LUA_PATH_SEP, "'\n\tno file '"); - lua_pushstring(L, "'"); - lua_concat(L, 3); + luaL_Buffer b; + luaL_buffinit(L, &b); + luaL_addstring(&b, "no file '"); + luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '"); + luaL_addstring(&b, "'"); + luaL_pushresult(&b); } @@ -455,17 +476,24 @@ static const char *searchpath (lua_State *L, const char *name, const char *path, const char *sep, const char *dirsep) { + luaL_Buffer buff; + char *pathname; /* path with name inserted */ + char *endpathname; /* its end */ + const char *filename; /* separator is non-empty and appears in 'name'? */ if (*sep != '\0' && strchr(name, *sep) != NULL) name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ - /* replace marks ('?') in 'path' by the file name */ - path = luaL_gsub(L, path, LUA_PATH_MARK, name); - while ((path = pushnextfilename(L, path)) != NULL) { - const char *filename = lua_tostring(L, -1); + luaL_buffinit(L, &buff); + /* add path to the buffer, replacing marks ('?') with the file name */ + luaL_addgsub(&buff, path, LUA_PATH_MARK, name); + luaL_addchar(&buff, '\0'); + pathname = luaL_buffaddr(&buff); /* writable list of file names */ + endpathname = pathname + luaL_bufflen(&buff) - 1; + while ((filename = getnextfilename(&pathname, endpathname)) != NULL) { if (readable(filename)) /* does file exist and is readable? */ - return filename; /* return that file name */ - lua_pop(L, 1); /* else remove file name */ + return lua_pushstring(L, filename); /* save and return name */ } + luaL_pushresult(&buff); /* push path to create error message */ pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */ return NULL; /* not found */ } @@ -478,9 +506,9 @@ static int ll_searchpath (lua_State *L) { luaL_optstring(L, 4, LUA_DIRSEP)); if (f != NULL) return 1; else { /* error message is on top of the stack */ - lua_pushnil(L); + luaL_pushfail(L); lua_insert(L, -2); - return 2; /* return nil + error message */ + return 2; /* return fail + error message */ } } @@ -491,14 +519,14 @@ static const char *findfile (lua_State *L, const char *name, const char *path; lua_getfield(L, lua_upvalueindex(1), pname); path = lua_tostring(L, -1); - if (path == NULL) + if (l_unlikely(path == NULL)) luaL_error(L, "'package.%s' must be a string", pname); return searchpath(L, name, path, ".", dirsep); } static int checkload (lua_State *L, int stat, const char *filename) { - if (stat) { /* module loaded successfully? */ + if (l_likely(stat)) { /* module loaded successfully? */ lua_pushstring(L, filename); /* will be 2nd argument to module */ return 2; /* return open function and file name */ } @@ -532,7 +560,7 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) { mark = strchr(modname, *LUA_IGMARK); if (mark) { int stat; - openfunc = lua_pushlstring(L, modname, mark - modname); + openfunc = lua_pushlstring(L, modname, ct_diff2sz(mark - modname)); openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc); stat = lookforfunc(L, filename, openfunc); if (stat != ERRFUNC) return stat; @@ -557,14 +585,14 @@ static int searcher_Croot (lua_State *L) { const char *p = strchr(name, '.'); int stat; if (p == NULL) return 0; /* is root */ - lua_pushlstring(L, name, p - name); + lua_pushlstring(L, name, ct_diff2sz(p - name)); filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP); if (filename == NULL) return 1; /* root not found */ if ((stat = loadfunc(L, filename, name)) != 0) { if (stat != ERRFUNC) return checkload(L, 0, filename); /* real error */ else { /* open function not found */ - lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename); + lua_pushfstring(L, "no module '%s' in file '%s'", name, filename); return 1; } } @@ -576,9 +604,14 @@ static int searcher_Croot (lua_State *L) { static int searcher_preload (lua_State *L) { const char *name = luaL_checkstring(L, 1); lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); - if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */ - lua_pushfstring(L, "\n\tno field package.preload['%s']", name); - return 1; + if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */ + lua_pushfstring(L, "no field package.preload['%s']", name); + return 1; + } + else { + lua_pushliteral(L, ":preload:"); + return 2; + } } @@ -586,13 +619,16 @@ static void findloader (lua_State *L, const char *name) { int i; luaL_Buffer msg; /* to build error message */ /* push 'package.searchers' to index 3 in the stack */ - if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) + if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers") + != LUA_TTABLE)) luaL_error(L, "'package.searchers' must be a table"); luaL_buffinit(L, &msg); + luaL_addstring(&msg, "\n\t"); /* error-message prefix for first message */ /* iterate over available searchers to find a loader */ for (i = 1; ; i++) { - if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ + if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */ lua_pop(L, 1); /* remove nil */ + luaL_buffsub(&msg, 2); /* remove last prefix */ luaL_pushresult(&msg); /* create error message */ luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); } @@ -603,8 +639,9 @@ static void findloader (lua_State *L, const char *name) { else if (lua_isstring(L, -2)) { /* searcher returned error message? */ lua_pop(L, 1); /* remove extra return */ luaL_addvalue(&msg); /* concatenate error message */ + luaL_addstring(&msg, "\n\t"); /* prefix for next message */ } - else + else /* no error message */ lua_pop(L, 2); /* remove both returns */ } } @@ -620,17 +657,23 @@ static int ll_require (lua_State *L) { /* else must load package */ lua_pop(L, 1); /* remove 'getfield' result */ findloader(L, name); - lua_pushstring(L, name); /* pass name as argument to module loader */ - lua_insert(L, -2); /* name is 1st argument (before search data) */ + lua_rotate(L, -2, 1); /* function <-> loader data */ + lua_pushvalue(L, 1); /* name is 1st argument to module loader */ + lua_pushvalue(L, -3); /* loader data is 2nd argument */ + /* stack: ...; loader data; loader function; mod. name; loader data */ lua_call(L, 2, 1); /* run loader to load module */ + /* stack: ...; loader data; result from loader */ if (!lua_isnil(L, -1)) /* non-nil return? */ lua_setfield(L, 2, name); /* LOADED[name] = returned value */ + else + lua_pop(L, 1); /* pop nil */ if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ lua_pushboolean(L, 1); /* use true as result */ - lua_pushvalue(L, -1); /* extra copy to be returned */ + lua_copy(L, -1, -2); /* replace loader result */ lua_setfield(L, 2, name); /* LOADED[name] = true */ } - return 1; + lua_rotate(L, -2, 1); /* loader data <-> module result */ + return 2; /* return module result and loader data */ } /* }====================================================== */ @@ -658,8 +701,13 @@ static const luaL_Reg ll_funcs[] = { static void createsearcherstable (lua_State *L) { - static const lua_CFunction searchers[] = - {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL}; + static const lua_CFunction searchers[] = { + searcher_preload, + searcher_Lua, + searcher_C, + searcher_Croot, + NULL + }; int i; /* create 'searchers' table */ lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); @@ -673,22 +721,9 @@ static void createsearcherstable (lua_State *L) { } -/* -** create table CLIBS to keep track of loaded C libraries, -** setting a finalizer to close all libraries when closing state. -*/ -static void createclibstable (lua_State *L) { - lua_newtable(L); /* create CLIBS table */ - lua_createtable(L, 0, 1); /* create metatable for CLIBS */ - lua_pushcfunction(L, gctm); - lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ - lua_setmetatable(L, -2); - lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */ -} - - LUAMOD_API int luaopen_package (lua_State *L) { - createclibstable(L); + luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */ + lua_pop(L, 1); /* will not use it now */ luaL_newlib(L, pk_funcs); /* create 'package' table */ createsearcherstable(L); /* set paths */ diff --git a/lobject.c b/lobject.c index 5d340de65e..763b484609 100644 --- a/lobject.c +++ b/lobject.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include #include @@ -30,36 +31,11 @@ /* -** converts an integer to a "floating point byte", represented as -** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if -** eeeee != 0 and (xxx) otherwise. +** Computes ceil(log2(x)), which is the smallest integer n such that +** x <= (1 << n). */ -int luaO_int2fb (unsigned int x) { - int e = 0; /* exponent */ - if (x < 8) return x; - while (x >= (8 << 4)) { /* coarse steps */ - x = (x + 0xf) >> 4; /* x = ceil(x / 16) */ - e += 4; - } - while (x >= (8 << 1)) { /* fine steps */ - x = (x + 1) >> 1; /* x = ceil(x / 2) */ - e++; - } - return ((e+1) << 3) | (cast_int(x) - 8); -} - - -/* converts back */ -int luaO_fb2int (int x) { - return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1); -} - - -/* -** Computes ceil(log2(x)) -*/ -int luaO_ceillog2 (unsigned int x) { - static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */ +lu_byte luaO_ceillog2 (unsigned int x) { + static const lu_byte log_2[256] = { /* log_2[i - 1] = ceil(log2(i)) */ 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, 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, 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, @@ -72,7 +48,67 @@ int luaO_ceillog2 (unsigned int x) { int l = 0; x--; while (x >= 256) { l += 8; x >>= 8; } - return l + log_2[x]; + return cast_byte(l + log_2[x]); +} + +/* +** Encodes 'p'% as a floating-point byte, represented as (eeeexxxx). +** The exponent is represented using excess-7. Mimicking IEEE 754, the +** representation normalizes the number when possible, assuming an extra +** 1 before the mantissa (xxxx) and adding one to the exponent (eeee) +** to signal that. So, the real value is (1xxxx) * 2^(eeee - 7 - 1) if +** eeee != 0, and (xxxx) * 2^-7 otherwise (subnormal numbers). +*/ +lu_byte luaO_codeparam (unsigned int p) { + if (p >= (cast(lu_mem, 0x1F) << (0xF - 7 - 1)) * 100u) /* overflow? */ + return 0xFF; /* return maximum value */ + else { + p = (cast(l_uint32, p) * 128 + 99) / 100; /* round up the division */ + if (p < 0x10) { /* subnormal number? */ + /* exponent bits are already zero; nothing else to do */ + return cast_byte(p); + } + else { /* p >= 0x10 implies ceil(log2(p + 1)) >= 5 */ + /* preserve 5 bits in 'p' */ + unsigned log = luaO_ceillog2(p + 1) - 5u; + return cast_byte(((p >> log) - 0x10) | ((log + 1) << 4)); + } + } +} + + +/* +** Computes 'p' times 'x', where 'p' is a floating-point byte. Roughly, +** we have to multiply 'x' by the mantissa and then shift accordingly to +** the exponent. If the exponent is positive, both the multiplication +** and the shift increase 'x', so we have to care only about overflows. +** For negative exponents, however, multiplying before the shift keeps +** more significant bits, as long as the multiplication does not +** overflow, so we check which order is best. +*/ +l_mem luaO_applyparam (lu_byte p, l_mem x) { + int m = p & 0xF; /* mantissa */ + int e = (p >> 4); /* exponent */ + if (e > 0) { /* normalized? */ + e--; /* correct exponent */ + m += 0x10; /* correct mantissa; maximum value is 0x1F */ + } + e -= 7; /* correct excess-7 */ + if (e >= 0) { + if (x < (MAX_LMEM / 0x1F) >> e) /* no overflow? */ + return (x * m) << e; /* order doesn't matter here */ + else /* real overflow */ + return MAX_LMEM; + } + else { /* negative exponent */ + e = -e; + if (x < MAX_LMEM / 0x1F) /* multiplication cannot overflow? */ + return (x * m) >> e; /* multiplying first gives more precision */ + else if ((x >> e) < MAX_LMEM / 0x1F) /* cannot overflow after shift? */ + return (x >> e) * m; + else /* real overflow */ + return MAX_LMEM; + } } @@ -88,7 +124,7 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, case LUA_OPBOR: return intop(|, v1, v2); case LUA_OPBXOR: return intop(^, v1, v2); case LUA_OPSHL: return luaV_shiftl(v1, v2); - case LUA_OPSHR: return luaV_shiftl(v1, -v2); + case LUA_OPSHR: return luaV_shiftr(v1, v2); case LUA_OPUNM: return intop(-, 0, v1); case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1); default: lua_assert(0); return 0; @@ -158,9 +194,10 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, } -int luaO_hexavalue (int c) { - if (lisdigit(c)) return c - '0'; - else return (ltolower(c) - 'a') + 10; +lu_byte luaO_hexavalue (int c) { + lua_assert(lisxdigit(c)); + if (lisdigit(c)) return cast_byte(c - '0'); + else return cast_byte((ltolower(c) - 'a') + 10); } @@ -190,7 +227,7 @@ static int isneg (const char **s) { */ static lua_Number lua_strx2number (const char *s, char **endptr) { int dot = lua_getlocaledecpoint(); - lua_Number r = 0.0; /* result (accumulator) */ + lua_Number r = l_mathop(0.0); /* result (accumulator) */ int sigdig = 0; /* number of significant digits */ int nosigdig = 0; /* number of non-significant digits */ int e = 0; /* exponent correction */ @@ -200,7 +237,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ neg = isneg(&s); /* check sign */ if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ - return 0.0; /* invalid format (no '0x') */ + return l_mathop(0.0); /* invalid format (no '0x') */ for (s += 2; ; s++) { /* skip '0x' and read numeral */ if (*s == dot) { if (hasdot) break; /* second dot? stop loop */ @@ -210,14 +247,14 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */ nosigdig++; else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ - r = (r * cast_num(16.0)) + luaO_hexavalue(*s); - else e++; /* too many digits; ignore, but still count for exponent */ + r = (r * l_mathop(16.0)) + luaO_hexavalue(*s); + else e++; /* too many digits; ignore, but still count for exponent */ if (hasdot) e--; /* decimal digit? correct exponent */ } else break; /* neither a dot nor a digit */ } if (nosigdig + sigdig == 0) /* no digits? */ - return 0.0; /* invalid format */ + return l_mathop(0.0); /* invalid format */ *endptr = cast_charp(s); /* valid up to here */ e *= 4; /* each digit multiplies/divides value by 2^4 */ if (*s == 'p' || *s == 'P') { /* exponent part? */ @@ -226,7 +263,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { s++; /* skip 'p' */ neg1 = isneg(&s); /* sign */ if (!lisdigit(cast_uchar(*s))) - return 0.0; /* invalid; must have at least one digit */ + return l_mathop(0.0); /* invalid; must have at least one digit */ while (lisdigit(cast_uchar(*s))) /* read exponent */ exp1 = exp1 * 10 + *(s++) - '0'; if (neg1) exp1 = -exp1; @@ -241,37 +278,42 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { /* }====================================================== */ -/* maximum length of a numeral */ +/* maximum length of a numeral to be converted to a number */ #if !defined (L_MAXLENNUM) #define L_MAXLENNUM 200 #endif +/* +** Convert string 's' to a Lua number (put in 'result'). Return NULL on +** fail or the address of the ending '\0' on success. ('mode' == 'x') +** means a hexadecimal numeral. +*/ static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { char *endptr; *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */ : lua_str2number(s, &endptr); if (endptr == s) return NULL; /* nothing recognized? */ while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */ - return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */ + return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */ } /* -** Convert string 's' to a Lua number (put in 'result'). Return NULL -** on fail or the address of the ending '\0' on success. -** 'pmode' points to (and 'mode' contains) special things in the string: -** - 'x'/'X' means a hexadecimal numeral -** - 'n'/'N' means 'inf' or 'nan' (which should be rejected) -** - '.' just optimizes the search for the common case (nothing special) +** Convert string 's' to a Lua number (put in 'result') handling the +** current locale. ** This function accepts both the current locale or a dot as the radix ** mark. If the conversion fails, it may mean number has a dot but ** locale accepts something else. In that case, the code copies 's' ** to a buffer (because 's' is read-only), changes the dot to the ** current locale radix mark, and tries to convert again. +** The variable 'mode' checks for special characters in the string: +** - 'n' means 'inf' or 'nan' (which should be rejected) +** - 'x' means a hexadecimal numeral +** - '.' just optimizes the search for the common case (no special chars) */ static const char *l_str2d (const char *s, lua_Number *result) { const char *endptr; - const char *pmode = strpbrk(s, ".xXnN"); + const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */ int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0; if (mode == 'n') /* reject 'inf' and 'nan' */ return NULL; @@ -279,7 +321,7 @@ static const char *l_str2d (const char *s, lua_Number *result) { if (endptr == NULL) { /* failed? may be a different locale */ char buff[L_MAXLENNUM + 1]; const char *pdot = strchr(s, '.'); - if (strlen(s) > L_MAXLENNUM || pdot == NULL) + if (pdot == NULL || strlen(s) > L_MAXLENNUM) return NULL; /* string too long or no dot; fail */ strcpy(buff, s); /* copy string to buffer */ buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */ @@ -313,7 +355,7 @@ static const char *l_str2int (const char *s, lua_Integer *result) { int d = *s - '0'; if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */ return NULL; /* do not accept it (as integer) */ - a = a * 10 + d; + a = a * 10 + cast_uint(d); empty = 0; } } @@ -337,14 +379,14 @@ size_t luaO_str2num (const char *s, TValue *o) { } else return 0; /* conversion failed */ - return (e - s) + 1; /* success; return string size */ + return ct_diff2sz(e - s) + 1; /* success; return string size */ } -int luaO_utf8esc (char *buff, unsigned long x) { +int luaO_utf8esc (char *buff, l_uint32 x) { int n = 1; /* number of bytes put in buffer (backwards) */ lua_assert(x <= 0x7FFFFFFFu); - if (x < 0x80) /* ascii? */ + if (x < 0x80) /* ASCII? */ buff[UTF8BUFFSZ - 1] = cast_char(x); else { /* need continuation bytes */ unsigned int mfb = 0x3f; /* maximum that fits in first byte */ @@ -359,107 +401,261 @@ int luaO_utf8esc (char *buff, unsigned long x) { } -/* maximum length of the conversion of a number to a string */ -#define MAXNUMBER2STR 50 +/* +** The size of the buffer for the conversion of a number to a string +** 'LUA_N2SBUFFSZ' must be enough to accommodate both LUA_INTEGER_FMT +** and LUA_NUMBER_FMT. For a long long int, this is 19 digits plus a +** sign and a final '\0', adding to 21. For a long double, it can go to +** a sign, the dot, an exponent letter, an exponent sign, 4 exponent +** digits, the final '\0', plus the significant digits, which are +** approximately the *_DIG attribute. +*/ +#if LUA_N2SBUFFSZ < (20 + l_floatatt(DIG)) +#error "invalid value for LUA_N2SBUFFSZ" +#endif /* -** Convert a number object to a string +** Convert a float to a string, adding it to a buffer. First try with +** a not too large number of digits, to avoid noise (for instance, +** 1.1 going to "1.1000000000000001"). If that lose precision, so +** that reading the result back gives a different number, then do the +** conversion again with extra precision. Moreover, if the numeral looks +** like an integer (without a decimal point or an exponent), add ".0" to +** its end. */ -void luaO_tostring (lua_State *L, TValue *obj) { - char buff[MAXNUMBER2STR]; - size_t len; +static int tostringbuffFloat (lua_Number n, char *buff) { + /* first conversion */ + int len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT, + (LUAI_UACNUMBER)n); + lua_Number check = lua_str2number(buff, NULL); /* read it back */ + if (check != n) { /* not enough precision? */ + /* convert again with more precision */ + len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT_N, + (LUAI_UACNUMBER)n); + } + /* looks like an integer? */ + if (buff[strspn(buff, "-0123456789")] == '\0') { + buff[len++] = lua_getlocaledecpoint(); + buff[len++] = '0'; /* adds '.0' to result */ + } + return len; +} + + +/* +** Convert a number object to a string, adding it to a buffer. +*/ +unsigned luaO_tostringbuff (const TValue *obj, char *buff) { + int len; lua_assert(ttisnumber(obj)); if (ttisinteger(obj)) - len = lua_integer2str(buff, sizeof(buff), ivalue(obj)); - else { - len = lua_number2str(buff, sizeof(buff), fltvalue(obj)); - if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ - buff[len++] = lua_getlocaledecpoint(); - buff[len++] = '0'; /* adds '.0' to result */ + len = lua_integer2str(buff, LUA_N2SBUFFSZ, ivalue(obj)); + else + len = tostringbuffFloat(fltvalue(obj), buff); + lua_assert(len < LUA_N2SBUFFSZ); + return cast_uint(len); +} + + +/* +** Convert a number object to a Lua string, replacing the value at 'obj' +*/ +void luaO_tostring (lua_State *L, TValue *obj) { + char buff[LUA_N2SBUFFSZ]; + unsigned len = luaO_tostringbuff(obj, buff); + setsvalue(L, obj, luaS_newlstr(L, buff, len)); +} + + + + +/* +** {================================================================== +** 'luaO_pushvfstring' +** =================================================================== +*/ + +/* +** Size for buffer space used by 'luaO_pushvfstring'. It should be +** (LUA_IDSIZE + LUA_N2SBUFFSZ) + a minimal space for basic messages, +** so that 'luaG_addinfo' can work directly on the static buffer. +*/ +#define BUFVFS cast_uint(LUA_IDSIZE + LUA_N2SBUFFSZ + 95) + +/* +** Buffer used by 'luaO_pushvfstring'. 'err' signals an error while +** building result (memory error [1] or buffer overflow [2]). +*/ +typedef struct BuffFS { + lua_State *L; + char *b; + size_t buffsize; + size_t blen; /* length of string in 'buff' */ + int err; + char space[BUFVFS]; /* initial buffer */ +} BuffFS; + + +static void initbuff (lua_State *L, BuffFS *buff) { + buff->L = L; + buff->b = buff->space; + buff->buffsize = sizeof(buff->space); + buff->blen = 0; + buff->err = 0; +} + + +/* +** Push final result from 'luaO_pushvfstring'. This function may raise +** errors explicitly or through memory errors, so it must run protected. +*/ +static void pushbuff (lua_State *L, void *ud) { + BuffFS *buff = cast(BuffFS*, ud); + switch (buff->err) { + case 1: /* memory error */ + luaD_throw(L, LUA_ERRMEM); + break; + case 2: /* length overflow: Add "..." at the end of result */ + if (buff->buffsize - buff->blen < 3) + strcpy(buff->b + buff->blen - 3, "..."); /* 'blen' must be > 3 */ + else { /* there is enough space left for the "..." */ + strcpy(buff->b + buff->blen, "..."); + buff->blen += 3; + } + /* FALLTHROUGH */ + default: { /* no errors, but it can raise one creating the new string */ + TString *ts = luaS_newlstr(L, buff->b, buff->blen); + setsvalue2s(L, L->top.p, ts); + L->top.p++; } } - setsvalue(L, obj, luaS_newlstr(L, buff, len)); } -static void pushstr (lua_State *L, const char *str, size_t l) { - setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); - L->top++; +static const char *clearbuff (BuffFS *buff) { + lua_State *L = buff->L; + const char *res; + if (luaD_rawrunprotected(L, pushbuff, buff) != LUA_OK) /* errors? */ + res = NULL; /* error message is on the top of the stack */ + else + res = getstr(tsvalue(s2v(L->top.p - 1))); + if (buff->b != buff->space) /* using dynamic buffer? */ + luaM_freearray(L, buff->b, buff->buffsize); /* free it */ + return res; +} + + +static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { + size_t left = buff->buffsize - buff->blen; /* space left in the buffer */ + if (buff->err) /* do nothing else after an error */ + return; + if (slen > left) { /* new string doesn't fit into current buffer? */ + if (slen > ((MAX_SIZE/2) - buff->blen)) { /* overflow? */ + memcpy(buff->b + buff->blen, str, left); /* copy what it can */ + buff->blen = buff->buffsize; + buff->err = 2; /* doesn't add anything else */ + return; + } + else { + size_t newsize = buff->buffsize + slen; /* limited to MAX_SIZE/2 */ + char *newb = + (buff->b == buff->space) /* still using static space? */ + ? luaM_reallocvector(buff->L, NULL, 0, newsize, char) + : luaM_reallocvector(buff->L, buff->b, buff->buffsize, newsize, + char); + if (newb == NULL) { /* allocation error? */ + buff->err = 1; /* signal a memory error */ + return; + } + if (buff->b == buff->space) /* new buffer (not reallocated)? */ + memcpy(newb, buff->b, buff->blen); /* copy previous content */ + buff->b = newb; /* set new (larger) buffer... */ + buff->buffsize = newsize; /* ...and its new size */ + } + } + memcpy(buff->b + buff->blen, str, slen); /* copy new content */ + buff->blen += slen; +} + + +/* +** Add a numeral to the buffer. +*/ +static void addnum2buff (BuffFS *buff, TValue *num) { + char numbuff[LUA_N2SBUFFSZ]; + unsigned len = luaO_tostringbuff(num, numbuff); + addstr2buff(buff, numbuff, len); } /* -** this function handles only '%d', '%c', '%f', '%p', and '%s' +** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%' conventional formats, plus Lua-specific '%I' and '%U' */ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { - int n = 0; /* number of strings in the stack to concatenate */ - const char *e; /* points to next conversion specifier */ + BuffFS buff; /* holds last part of the result */ + const char *e; /* points to next '%' */ + initbuff(L, &buff); while ((e = strchr(fmt, '%')) != NULL) { - pushstr(L, fmt, e - fmt); /* string up to conversion specifier */ - switch (*(e+1)) { + addstr2buff(&buff, fmt, ct_diff2sz(e - fmt)); /* add 'fmt' up to '%' */ + switch (*(e + 1)) { /* conversion specifier */ case 's': { /* zero-terminated string */ const char *s = va_arg(argp, char *); if (s == NULL) s = "(null)"; - pushstr(L, s, strlen(s)); + addstr2buff(&buff, s, strlen(s)); break; } case 'c': { /* an 'int' as a character */ - char buff = cast_char(va_arg(argp, int)); - if (lisprint(cast_uchar(buff))) - pushstr(L, &buff, 1); - else /* non-printable character; print its code */ - luaO_pushfstring(L, "<\\%d>", cast_uchar(buff)); + char c = cast_char(va_arg(argp, int)); + addstr2buff(&buff, &c, sizeof(char)); break; } case 'd': { /* an 'int' */ - setivalue(s2v(L->top), va_arg(argp, int)); - goto top2str; + TValue num; + setivalue(&num, va_arg(argp, int)); + addnum2buff(&buff, &num); + break; } case 'I': { /* a 'lua_Integer' */ - setivalue(s2v(L->top), cast(lua_Integer, va_arg(argp, l_uacInt))); - goto top2str; + TValue num; + setivalue(&num, cast_Integer(va_arg(argp, l_uacInt))); + addnum2buff(&buff, &num); + break; } case 'f': { /* a 'lua_Number' */ - setfltvalue(s2v(L->top), cast_num(va_arg(argp, l_uacNumber))); - top2str: /* convert the top element to a string */ - L->top++; - luaO_tostring(L, s2v(L->top - 1)); + TValue num; + setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber))); + addnum2buff(&buff, &num); break; } case 'p': { /* a pointer */ - char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ + char bf[LUA_N2SBUFFSZ]; /* enough space for '%p' */ void *p = va_arg(argp, void *); - int l = lua_pointer2str(buff, sizeof(buff), p); - pushstr(L, buff, l); + int len = lua_pointer2str(bf, LUA_N2SBUFFSZ, p); + addstr2buff(&buff, bf, cast_uint(len)); break; } - case 'U': { /* a 'long' as a UTF-8 sequence */ - char buff[UTF8BUFFSZ]; - int l = luaO_utf8esc(buff, va_arg(argp, long)); - pushstr(L, buff + UTF8BUFFSZ - l, l); + case 'U': { /* an 'unsigned long' as a UTF-8 sequence */ + char bf[UTF8BUFFSZ]; + unsigned long arg = va_arg(argp, unsigned long); + int len = luaO_utf8esc(bf, cast(l_uint32, arg)); + addstr2buff(&buff, bf + UTF8BUFFSZ - len, cast_uint(len)); break; } case '%': { - pushstr(L, "%", 1); + addstr2buff(&buff, "%", 1); break; } default: { - luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'", - *(e + 1)); + addstr2buff(&buff, e, 2); /* keep unknown format in the result */ + break; } } - n += 2; - if (L->top + 2 > L->stack_last) { /* no free stack space? */ - luaV_concat(L, n); - n = 1; - } - fmt = e + 2; + fmt = e + 2; /* skip '%' and the specifier */ } - pushstr(L, fmt, strlen(fmt)); - if (n > 0) luaV_concat(L, n + 1); - return svalue(s2v(L->top - 1)); + addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ + return clearbuff(&buff); /* empty buffer into a new string */ } @@ -469,12 +665,13 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); va_end(argp); + if (msg == NULL) /* error? */ + luaD_throw(L, LUA_ERRMEM); return msg; } +/* }================================================================== */ -/* number of chars of a literal string without the ending \0 */ -#define LL(x) (sizeof(x)/sizeof(char) - 1) #define RETS "..." #define PRE "[string \"" @@ -482,36 +679,37 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) -void luaO_chunkid (char *out, const char *source, size_t bufflen) { - size_t l = strlen(source); +void luaO_chunkid (char *out, const char *source, size_t srclen) { + size_t bufflen = LUA_IDSIZE; /* free space in buffer */ if (*source == '=') { /* 'literal' source */ - if (l <= bufflen) /* small enough? */ - memcpy(out, source + 1, l * sizeof(char)); + if (srclen <= bufflen) /* small enough? */ + memcpy(out, source + 1, srclen * sizeof(char)); else { /* truncate it */ addstr(out, source + 1, bufflen - 1); *out = '\0'; } } else if (*source == '@') { /* file name */ - if (l <= bufflen) /* small enough? */ - memcpy(out, source + 1, l * sizeof(char)); + if (srclen <= bufflen) /* small enough? */ + memcpy(out, source + 1, srclen * sizeof(char)); else { /* add '...' before rest of name */ addstr(out, RETS, LL(RETS)); bufflen -= LL(RETS); - memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); + memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char)); } } else { /* string; format as [string "source"] */ const char *nl = strchr(source, '\n'); /* find first new line (if any) */ addstr(out, PRE, LL(PRE)); /* add prefix */ bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ - if (l < bufflen && nl == NULL) { /* small one-line source? */ - addstr(out, source, l); /* keep it */ + if (srclen < bufflen && nl == NULL) { /* small one-line source? */ + addstr(out, source, srclen); /* keep it */ } else { - if (nl != NULL) l = nl - source; /* stop at first newline */ - if (l > bufflen) l = bufflen; - addstr(out, source, l); + if (nl != NULL) + srclen = ct_diff2sz(nl - source); /* stop at first newline */ + if (srclen > bufflen) srclen = bufflen; + addstr(out, source, srclen); addstr(out, RETS, LL(RETS)); } memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); diff --git a/lobject.h b/lobject.h index 53e67932a0..156c942f01 100644 --- a/lobject.h +++ b/lobject.h @@ -17,24 +17,30 @@ /* -** Extra tags for non-values +** Extra types for collectable non-values */ -#define LUA_TUPVAL LUA_NUMTAGS /* upvalues */ -#define LUA_TPROTO (LUA_NUMTAGS+1) /* function prototypes */ +#define LUA_TUPVAL LUA_NUMTYPES /* upvalues */ +#define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */ +#define LUA_TDEADKEY (LUA_NUMTYPES+2) /* removed keys in tables */ + + /* -** number of all possible tags (including LUA_TNONE) +** number of all possible types (including LUA_TNONE but excluding DEADKEY) */ -#define LUA_TOTALTAGS (LUA_TPROTO + 2) +#define LUA_TOTALTYPES (LUA_TPROTO + 2) /* ** tags for Tagged Values have the following use of bits: -** bits 0-3: actual tag (a LUA_T* value) +** bits 0-3: actual tag (a LUA_T* constant) ** bits 4-5: variant bits ** bit 6: whether value is collectable */ +/* add variant bits to a type */ +#define makevariant(t,v) ((t) | ((v) << 4)) + /* @@ -43,10 +49,11 @@ typedef union Value { struct GCObject *gc; /* collectable objects */ void *p; /* light userdata */ - int b; /* booleans */ lua_CFunction f; /* light C functions */ lua_Integer i; /* integer numbers */ lua_Number n; /* float numbers */ + /* not used, but may avoid warnings for uninitialized value */ + lu_byte ub; } Value; @@ -63,7 +70,7 @@ typedef struct TValue { #define val_(o) ((o)->value_) -#define valraw(o) (&val_(o)) +#define valraw(o) (val_(o)) /* raw type tag of a TValue */ @@ -86,24 +93,36 @@ typedef struct TValue { /* Macros for internal tests */ + +/* collectable object has the same tag as the original value */ #define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt) +/* +** Any value being manipulated by the program either is non +** collectable, or the collectable object has the right tag +** and it is not dead. The option 'L == NULL' allows other +** macros using this one to be used where L is not available. +*/ #define checkliveness(L,obj) \ - lua_longassert(!iscollectable(obj) || \ - (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))) + ((void)L, lua_longassert(!iscollectable(obj) || \ + (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))) /* Macros to set values */ + +/* set a value's tag */ #define settt_(o,t) ((o)->tt_=(t)) +/* main macro to copy values (from 'obj2' to 'obj1') */ #define setobj(L,obj1,obj2) \ { TValue *io1=(obj1); const TValue *io2=(obj2); \ - io1->value_ = io2->value_; io1->tt_ = io2->tt_; \ - (void)L; checkliveness(L,io1); lua_assert(!isreallyempty(io1)); } + io1->value_ = io2->value_; settt_(io1, io2->tt_); \ + checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } /* -** different types of assignments, according to destination +** Different types of assignments, according to source and destination. +** (They are mostly equal now, but may be different in the future.) */ /* from stack to stack */ @@ -118,13 +137,36 @@ typedef struct TValue { #define setobj2t setobj - +/* +** Entries in a Lua stack. Field 'tbclist' forms a list of all +** to-be-closed variables active in this stack. Dummy entries are +** used when the distance between two tbc variables does not fit +** in an unsigned short. They are represented by delta==0, and +** their real delta is always the maximum value that fits in +** that field. +*/ typedef union StackValue { TValue val; + struct { + TValuefields; + unsigned short delta; + } tbclist; } StackValue; -typedef StackValue *StkId; /* index to stack elements */ +/* index to stack elements */ +typedef StackValue *StkId; + + +/* +** When reallocating the stack, change all pointers to the stack into +** proper offsets. +*/ +typedef union { + StkId p; /* actual pointer */ + ptrdiff_t offset; /* used while the stack is being reallocated */ +} StkIdRel; + /* convert a 'StackValue' to a 'TValue' */ #define s2v(o) (&(o)->val) @@ -137,36 +179,45 @@ typedef StackValue *StkId; /* index to stack elements */ ** =================================================================== */ -/* macro to test for (any kind of) nil */ -#define ttisnil(v) checktype((v), LUA_TNIL) +/* Standard nil */ +#define LUA_VNIL makevariant(LUA_TNIL, 0) -/* macro to test for a "pure" nil */ -#define ttisstrictnil(o) checktag((o), LUA_TNIL) +/* Empty slot (which might be different from a slot containing nil) */ +#define LUA_VEMPTY makevariant(LUA_TNIL, 1) +/* Value returned for a key not found in a table (absent key) */ +#define LUA_VABSTKEY makevariant(LUA_TNIL, 2) -#define setnilvalue(obj) settt_(obj, LUA_TNIL) +/* Special variant to signal that a fast get is accessing a non-table */ +#define LUA_VNOTABLE makevariant(LUA_TNIL, 3) -/* -** Variant tag, used only in tables to signal an empty slot -** (which might be different from a slot containing nil) -*/ -#define LUA_TEMPTY (LUA_TNIL | (1 << 4)) +/* macro to test for (any kind of) nil */ +#define ttisnil(v) checktype((v), LUA_TNIL) /* -** Variant used only in the value returned for a key not found in a -** table (absent key). +** Macro to test the result of a table access. Formally, it should +** distinguish between LUA_VEMPTY/LUA_VABSTKEY/LUA_VNOTABLE and +** other tags. As currently nil is equivalent to LUA_VEMPTY, it is +** simpler to just test whether the value is nil. */ -#define LUA_TABSTKEY (LUA_TNIL | (2 << 4)) +#define tagisempty(tag) (novariant(tag) == LUA_TNIL) + + +/* macro to test for a standard nil */ +#define ttisstrictnil(o) checktag((o), LUA_VNIL) + + +#define setnilvalue(obj) settt_(obj, LUA_VNIL) -#define isabstkey(v) checktag((v), LUA_TABSTKEY) +#define isabstkey(v) checktag((v), LUA_VABSTKEY) /* ** macro to detect non-standard nils (used only in assertions) */ -#define isreallyempty(v) (ttisnil(v) && !ttisstrictnil(v)) +#define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v)) /* @@ -178,11 +229,11 @@ typedef StackValue *StkId; /* index to stack elements */ /* macro defining a value corresponding to an absent key */ -#define ABSTKEYCONSTANT {NULL}, LUA_TABSTKEY +#define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY /* mark an entry as empty */ -#define setempty(v) settt_(v, LUA_TEMPTY) +#define setempty(v) settt_(v, LUA_VEMPTY) @@ -195,16 +246,22 @@ typedef StackValue *StkId; /* index to stack elements */ ** =================================================================== */ -#define ttisboolean(o) checktag((o), LUA_TBOOLEAN) -#define bvalue(o) check_exp(ttisboolean(o), val_(o).b) +#define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0) +#define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1) -#define bvalueraw(v) ((v).b) +#define ttisboolean(o) checktype((o), LUA_TBOOLEAN) +#define ttisfalse(o) checktag((o), LUA_VFALSE) +#define ttistrue(o) checktag((o), LUA_VTRUE) -#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) -#define setbvalue(obj,x) \ - { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); } +#define l_isfalse(o) (ttisfalse(o) || ttisnil(o)) +#define tagisfalse(t) ((t) == LUA_VFALSE || novariant(t) == LUA_TNIL) + + + +#define setbfvalue(obj) settt_(obj, LUA_VFALSE) +#define setbtvalue(obj) settt_(obj, LUA_VTRUE) /* }================================================================== */ @@ -215,13 +272,15 @@ typedef StackValue *StkId; /* index to stack elements */ ** =================================================================== */ -#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) +#define LUA_VTHREAD makevariant(LUA_TTHREAD, 0) + +#define ttisthread(o) checktag((o), ctb(LUA_VTHREAD)) #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) #define setthvalue(L,obj,x) \ { TValue *io = (obj); lua_State *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \ checkliveness(L,io); } #define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t) @@ -274,12 +333,12 @@ typedef struct GCObject { */ /* Variant tags for numbers */ -#define LUA_TNUMFLT (LUA_TNUMBER | (1 << 4)) /* float numbers */ -#define LUA_TNUMINT (LUA_TNUMBER | (2 << 4)) /* integer numbers */ +#define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */ +#define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */ #define ttisnumber(o) checktype((o), LUA_TNUMBER) -#define ttisfloat(o) checktag((o), LUA_TNUMFLT) -#define ttisinteger(o) checktag((o), LUA_TNUMINT) +#define ttisfloat(o) checktag((o), LUA_VNUMFLT) +#define ttisinteger(o) checktag((o), LUA_VNUMINT) #define nvalue(o) check_exp(ttisnumber(o), \ (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) @@ -290,13 +349,13 @@ typedef struct GCObject { #define ivalueraw(v) ((v).i) #define setfltvalue(obj,x) \ - { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); } + { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); } #define chgfltvalue(obj,x) \ { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } #define setivalue(obj,x) \ - { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); } + { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); } #define chgivalue(obj,x) \ { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } @@ -311,12 +370,12 @@ typedef struct GCObject { */ /* Variant tags for strings */ -#define LUA_TSHRSTR (LUA_TSTRING | (1 << 4)) /* short strings */ -#define LUA_TLNGSTR (LUA_TSTRING | (2 << 4)) /* long strings */ +#define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */ +#define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */ #define ttisstring(o) checktype((o), LUA_TSTRING) -#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR)) -#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR)) +#define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR)) +#define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR)) #define tsvalueraw(v) (gco2ts((v).gc)) @@ -334,39 +393,54 @@ typedef struct GCObject { #define setsvalue2n setsvalue +/* Kinds of long strings (stored in 'shrlen') */ +#define LSTRREG -1 /* regular long string */ +#define LSTRFIX -2 /* fixed external long string */ +#define LSTRMEM -3 /* external long string with deallocation */ + + /* -** Header for string value; string bytes follow the end of this structure -** (aligned according to 'UTString'; see next). +** Header for a string value. */ typedef struct TString { CommonHeader; lu_byte extra; /* reserved words for short strings; "has hash" for longs */ - lu_byte shrlen; /* length for short strings */ + ls_byte shrlen; /* length for short strings, negative for long strings */ unsigned int hash; union { size_t lnglen; /* length for long strings */ struct TString *hnext; /* linked list for hash table */ } u; + char *contents; /* pointer to content in long strings */ + lua_Alloc falloc; /* deallocation function for external strings */ + void *ud; /* user data for external strings */ } TString; +#define strisshr(ts) ((ts)->shrlen >= 0) +#define isextstr(ts) (ttislngstring(ts) && tsvalue(ts)->shrlen != LSTRREG) + /* -** Get the actual string (array of bytes) from a 'TString'. -** (Access to 'extra' ensures that value is really a 'TString'.) +** Get the actual string (array of bytes) from a 'TString'. (Generic +** version and specialized versions for long and short strings.) */ -#define getstr(ts) \ - check_exp(sizeof((ts)->extra), cast_charp((ts)) + sizeof(TString)) - +#define rawgetshrstr(ts) (cast_charp(&(ts)->contents)) +#define getshrstr(ts) check_exp(strisshr(ts), rawgetshrstr(ts)) +#define getlngstr(ts) check_exp(!strisshr(ts), (ts)->contents) +#define getstr(ts) (strisshr(ts) ? rawgetshrstr(ts) : (ts)->contents) -/* get the actual string (array of bytes) from a Lua value */ -#define svalue(o) getstr(tsvalue(o)) -/* get string length from 'TString *s' */ -#define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen) +/* get string length from 'TString *ts' */ +#define tsslen(ts) \ + (strisshr(ts) ? cast_sizet((ts)->shrlen) : (ts)->u.lnglen) -/* get string length from 'TValue *o' */ -#define vslen(o) tsslen(tsvalue(o)) +/* +** Get string and length */ +#define getlstr(ts, len) \ + (strisshr(ts) \ + ? (cast_void((len) = cast_sizet((ts)->shrlen)), rawgetshrstr(ts)) \ + : (cast_void((len) = (ts)->u.lnglen), (ts)->contents)) /* }================================================================== */ @@ -377,8 +451,17 @@ typedef struct TString { ** =================================================================== */ -#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA) -#define ttisfulluserdata(o) checktype((o), LUA_TUSERDATA) + +/* +** Light userdata should be a variant of userdata, but for compatibility +** reasons they are also different types. +*/ +#define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0) + +#define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0) + +#define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA) +#define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA)) #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) @@ -386,11 +469,11 @@ typedef struct TString { #define pvalueraw(v) ((v).p) #define setpvalue(obj,x) \ - { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); } + { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); } #define setuvalue(L,obj,x) \ { TValue *io = (obj); Udata *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \ checkliveness(L,io); } @@ -435,8 +518,8 @@ typedef struct Udata0 { /* compute the offset of the memory area of a userdata */ #define udatamemoffset(nuv) \ - ((nuv) == 0 ? offsetof(Udata0, bindata) \ - : offsetof(Udata, uv) + (sizeof(UValue) * (nuv))) + ((nuv) == 0 ? offsetof(Udata0, bindata) \ + : offsetof(Udata, uv) + (sizeof(UValue) * (nuv))) /* get the address of the memory block inside 'Udata' */ #define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue)) @@ -453,6 +536,12 @@ typedef struct Udata0 { ** =================================================================== */ +#define LUA_VPROTO makevariant(LUA_TPROTO, 0) + + +typedef l_uint32 Instruction; + + /* ** Description of an upvalue for function prototypes */ @@ -460,6 +549,7 @@ typedef struct Upvaldesc { TString *name; /* upvalue name (for debug information) */ lu_byte instack; /* whether it is in stack (register) */ lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ + lu_byte kind; /* kind of corresponding variable */ } Upvaldesc; @@ -489,13 +579,30 @@ typedef struct AbsLineInfo { int line; } AbsLineInfo; + +/* +** Flags in Prototypes +*/ +#define PF_VAHID 1 /* function has hidden vararg arguments */ +#define PF_VATAB 2 /* function has vararg table */ +#define PF_FIXED 4 /* prototype has parts in fixed memory */ + +/* a vararg function either has hidden args. or a vararg table */ +#define isvararg(p) ((p)->flag & (PF_VAHID | PF_VATAB)) + +/* +** mark that a function needs a vararg table. (The flag PF_VAHID will +** be cleared later.) +*/ +#define needvatab(p) ((p)->flag |= PF_VATAB) + /* ** Function Prototypes */ typedef struct Proto { CommonHeader; lu_byte numparams; /* number of fixed (named) parameters */ - lu_byte is_vararg; + lu_byte flag; lu_byte maxstacksize; /* number of registers needed by this function */ int sizeupvalues; /* size of 'upvalues' */ int sizek; /* size of 'k' */ @@ -522,20 +629,24 @@ typedef struct Proto { /* ** {================================================================== -** Closures +** Functions ** =================================================================== */ +#define LUA_VUPVAL makevariant(LUA_TUPVAL, 0) + + /* Variant tags for functions */ -#define LUA_TLCL (LUA_TFUNCTION | (1 << 4)) /* Lua closure */ -#define LUA_TLCF (LUA_TFUNCTION | (2 << 4)) /* light C function */ -#define LUA_TCCL (LUA_TFUNCTION | (3 << 4)) /* C closure */ +#define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */ +#define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */ +#define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */ #define ttisfunction(o) checktype(o, LUA_TFUNCTION) -#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_TLCL) -#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL)) -#define ttislcf(o) checktag((o), LUA_TLCF) -#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL)) +#define ttisLclosure(o) checktag((o), ctb(LUA_VLCL)) +#define ttislcf(o) checktag((o), LUA_VLCF) +#define ttisCclosure(o) checktag((o), ctb(LUA_VCCL)) +#define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o)) + #define isLfunction(o) ttisLclosure(o) @@ -548,17 +659,17 @@ typedef struct Proto { #define setclLvalue(L,obj,x) \ { TValue *io = (obj); LClosure *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \ checkliveness(L,io); } #define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl) #define setfvalue(obj,x) \ - { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); } + { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); } #define setclCvalue(L,obj,x) \ { TValue *io = (obj); CClosure *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \ checkliveness(L,io); } @@ -567,7 +678,10 @@ typedef struct Proto { */ typedef struct UpVal { CommonHeader; - TValue *v; /* points to stack or to its own value */ + union { + TValue *p; /* points to stack or to its own value */ + ptrdiff_t offset; /* used while the stack is being reallocated */ + } v; union { struct { /* (when open) */ struct UpVal *next; /* linked list */ @@ -578,9 +692,6 @@ typedef struct UpVal { } UpVal; -/* variant for "To Be Closed" upvalues */ -#define LUA_TUPVALTBC (LUA_TUPVAL | (1 << 4)) - #define ClosureHeader \ CommonHeader; lu_byte nupvalues; GCObject *gclist @@ -616,13 +727,15 @@ typedef union Closure { ** =================================================================== */ -#define ttistable(o) checktag((o), ctb(LUA_TTABLE)) +#define LUA_VTABLE makevariant(LUA_TTABLE, 0) + +#define ttistable(o) checktag((o), ctb(LUA_VTABLE)) #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) #define sethvalue(L,obj,x) \ { TValue *io = (obj); Table *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \ checkliveness(L,io); } #define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h) @@ -647,40 +760,26 @@ typedef union Node { /* copy a value into a key */ -#define setnodekey(L,node,obj) \ +#define setnodekey(node,obj) \ { Node *n_=(node); const TValue *io_=(obj); \ - n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \ - (void)L; checkliveness(L,io_); } + n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; } /* copy a value from a key */ #define getnodekey(L,obj,node) \ { TValue *io_=(obj); const Node *n_=(node); \ io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \ - (void)L; checkliveness(L,io_); } + checkliveness(L,io_); } -/* -** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the -** real size of 'array'. Otherwise, the real size of 'array' is the -** smallest power of two not smaller than 'alimit' (or zero iff 'alimit' -** is zero); 'alimit' is then used as a hint for #t. -*/ - -#define BITRAS (1 << 7) -#define isrealasize(t) (!((t)->marked & BITRAS)) -#define setrealasize(t) ((t)->marked &= cast_byte(~BITRAS)) -#define setnorealasize(t) ((t)->marked |= BITRAS) - typedef struct Table { CommonHeader; lu_byte flags; /* 1<

u.key_val) #define keyisnil(node) (keytt(node) == LUA_TNIL) -#define keyisinteger(node) (keytt(node) == LUA_TNUMINT) +#define keyisinteger(node) (keytt(node) == LUA_VNUMINT) #define keyival(node) (keyval(node).i) -#define keyisshrstr(node) (keytt(node) == ctb(LUA_TSHRSTR)) +#define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR)) #define keystrval(node) (gco2ts(keyval(node).gc)) #define setnilkey(node) (keytt(node) = LUA_TNIL) @@ -707,13 +806,13 @@ typedef struct Table { /* -** Use a "nil table" to mark dead keys in a table. Those keys serve -** to keep space for removed entries, which may still be part of -** chains. Note that the 'keytt' does not have the BIT_ISCOLLECTABLE -** set, so these values are considered not collectable and are different -** from any valid value. +** Dead keys in tables have the tag DEADKEY but keep their original +** gcvalue. This distinguishes them from regular keys but allows them to +** be found when searched in a special way. ('next' needs that to find +** keys removed from a table during a traversal.) */ -#define setdeadkey(n) (keytt(n) = LUA_TTABLE, gckey(n) = NULL) +#define setdeadkey(node) (keytt(node) = LUA_TDEADKEY) +#define keyisdead(node) (keytt(node) == LUA_TDEADKEY) /* }================================================================== */ @@ -723,31 +822,42 @@ typedef struct Table { ** 'module' operation for hashing (size is always a power of 2) */ #define lmod(s,size) \ - (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1))))) + (check_exp((size&(size-1))==0, (cast_uint(s) & cast_uint((size)-1)))) -#define twoto(x) (1<<(x)) +#define twoto(x) (1u<<(x)) #define sizenode(t) (twoto((t)->lsizenode)) /* size of buffer for 'luaO_utf8esc' function */ #define UTF8BUFFSZ 8 -LUAI_FUNC int luaO_int2fb (unsigned int x); -LUAI_FUNC int luaO_fb2int (int x); -LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); -LUAI_FUNC int luaO_ceillog2 (unsigned int x); + +/* macro to call 'luaO_pushvfstring' correctly */ +#define pushvfstring(L, argp, fmt, msg) \ + { va_start(argp, fmt); \ + msg = luaO_pushvfstring(L, fmt, argp); \ + va_end(argp); \ + if (msg == NULL) luaD_throw(L, LUA_ERRMEM); /* only after 'va_end' */ } + + +LUAI_FUNC int luaO_utf8esc (char *buff, l_uint32 x); +LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x); +LUAI_FUNC lu_byte luaO_codeparam (unsigned int p); +LUAI_FUNC l_mem luaO_applyparam (lu_byte p, l_mem x); + LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, TValue *res); LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, StkId res); LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); -LUAI_FUNC int luaO_hexavalue (int c); +LUAI_FUNC unsigned luaO_tostringbuff (const TValue *obj, char *buff); +LUAI_FUNC lu_byte luaO_hexavalue (int c); LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj); LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp); LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); -LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); +LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen); #endif diff --git a/lopcodes.c b/lopcodes.c index 3f0d551a99..7e182315bc 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -10,100 +10,131 @@ #include "lprefix.h" -#include - #include "lopcodes.h" +#define opmode(mm,ot,it,t,a,m) \ + (((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m)) + + /* ORDER OP */ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { -/* OT IT T A mode opcode */ - opmode(0, 0, 0, 1, iABC) /* OP_MOVE */ - ,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADI */ - ,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADF */ - ,opmode(0, 0, 0, 1, iABx) /* OP_LOADK */ - ,opmode(0, 0, 0, 1, iABx) /* OP_LOADKX */ - ,opmode(0, 0, 0, 1, iABC) /* OP_LOADBOOL */ - ,opmode(0, 0, 0, 1, iABC) /* OP_LOADNIL */ - ,opmode(0, 0, 0, 1, iABC) /* OP_GETUPVAL */ - ,opmode(0, 0, 0, 0, iABC) /* OP_SETUPVAL */ - ,opmode(0, 0, 0, 1, iABC) /* OP_GETTABUP */ - ,opmode(0, 0, 0, 1, iABC) /* OP_GETTABLE */ - ,opmode(0, 0, 0, 1, iABC) /* OP_GETI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_GETFIELD */ - ,opmode(0, 0, 0, 0, iABC) /* OP_SETTABUP */ - ,opmode(0, 0, 0, 0, iABC) /* OP_SETTABLE */ - ,opmode(0, 0, 0, 0, iABC) /* OP_SETI */ - ,opmode(0, 0, 0, 0, iABC) /* OP_SETFIELD */ - ,opmode(0, 0, 0, 1, iABC) /* OP_NEWTABLE */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SELF */ - ,opmode(0, 0, 0, 1, iABC) /* OP_ADDI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SUBI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_MULI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_MODI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_POWI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_DIVI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_IDIVI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_ADDK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SUBK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_MULK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_MODK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_POWK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_DIVK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_IDIVK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BANDK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BORK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BXORK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SHRI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SHLI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_ADD */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SUB */ - ,opmode(0, 0, 0, 1, iABC) /* OP_MUL */ - ,opmode(0, 0, 0, 1, iABC) /* OP_MOD */ - ,opmode(0, 0, 0, 1, iABC) /* OP_POW */ - ,opmode(0, 0, 0, 1, iABC) /* OP_DIV */ - ,opmode(0, 0, 0, 1, iABC) /* OP_IDIV */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BAND */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BOR */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BXOR */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SHL */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SHR */ - ,opmode(0, 0, 0, 1, iABC) /* OP_UNM */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BNOT */ - ,opmode(0, 0, 0, 1, iABC) /* OP_NOT */ - ,opmode(0, 0, 0, 1, iABC) /* OP_LEN */ - ,opmode(0, 0, 0, 1, iABC) /* OP_CONCAT */ - ,opmode(0, 0, 0, 0, iABC) /* OP_CLOSE */ - ,opmode(0, 0, 0, 0, iABC) /* OP_TBC */ - ,opmode(0, 0, 0, 0, isJ) /* OP_JMP */ - ,opmode(0, 0, 1, 0, iABC) /* OP_EQ */ - ,opmode(0, 0, 1, 0, iABC) /* OP_LT */ - ,opmode(0, 0, 1, 0, iABC) /* OP_LE */ - ,opmode(0, 0, 1, 0, iABC) /* OP_EQK */ - ,opmode(0, 0, 1, 0, iABC) /* OP_EQI */ - ,opmode(0, 0, 1, 0, iABC) /* OP_LTI */ - ,opmode(0, 0, 1, 0, iABC) /* OP_LEI */ - ,opmode(0, 0, 1, 0, iABC) /* OP_GTI */ - ,opmode(0, 0, 1, 0, iABC) /* OP_GEI */ - ,opmode(0, 0, 1, 0, iABC) /* OP_TEST */ - ,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */ - ,opmode(1, 1, 0, 1, iABC) /* OP_CALL */ - ,opmode(1, 1, 0, 1, iABC) /* OP_TAILCALL */ - ,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */ - ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN0 */ - ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN1 */ - ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP1 */ - ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP1 */ - ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP */ - ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP */ - ,opmode(0, 0, 0, 0, iABx) /* OP_TFORPREP */ - ,opmode(0, 0, 0, 0, iABC) /* OP_TFORCALL */ - ,opmode(0, 0, 0, 1, iABx) /* OP_TFORLOOP */ - ,opmode(0, 1, 0, 0, iABC) /* OP_SETLIST */ - ,opmode(0, 0, 0, 1, iABx) /* OP_CLOSURE */ - ,opmode(1, 0, 0, 1, iABC) /* OP_VARARG */ - ,opmode(0, 0, 0, 1, iABC) /* OP_PREPVARARG */ - ,opmode(0, 0, 0, 0, iAx) /* OP_EXTRAARG */ +/* MM OT IT T A mode opcode */ + opmode(0, 0, 0, 0, 1, iABC) /* OP_MOVE */ + ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */ + ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */ + ,opmode(0, 0, 0, 0, 1, ivABC) /* OP_NEWTABLE */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */ + ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */ + ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI */ + ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LEN */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_CONCAT */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_CLOSE */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TBC */ + ,opmode(0, 0, 0, 0, 0, isJ) /* OP_JMP */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQ */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LT */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LE */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQK */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LTI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LEI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_GTI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_GEI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_TEST */ + ,opmode(0, 0, 0, 1, 1, iABC) /* OP_TESTSET */ + ,opmode(0, 1, 1, 0, 1, iABC) /* OP_CALL */ + ,opmode(0, 1, 1, 0, 1, iABC) /* OP_TAILCALL */ + ,opmode(0, 0, 1, 0, 0, iABC) /* OP_RETURN */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN0 */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN1 */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORLOOP */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORPREP */ + ,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */ + ,opmode(0, 0, 1, 0, 0, ivABC) /* OP_SETLIST */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */ + ,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETVARG */ + ,opmode(0, 0, 0, 0, 0, iABx) /* OP_ERRNNIL */ + ,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */ + ,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */ }; + + +/* +** Check whether instruction sets top for next instruction, that is, +** it results in multiple values. +*/ +int luaP_isOT (Instruction i) { + OpCode op = GET_OPCODE(i); + switch (op) { + case OP_TAILCALL: return 1; + default: + return testOTMode(op) && GETARG_C(i) == 0; + } +} + + +/* +** Check whether instruction uses top from previous instruction, that is, +** it accepts multiple results. +*/ +int luaP_isIT (Instruction i) { + OpCode op = GET_OPCODE(i); + switch (op) { + case OP_SETLIST: + return testITMode(GET_OPCODE(i)) && GETARG_vB(i) == 0; + default: + return testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0; + } +} + diff --git a/lopcodes.h b/lopcodes.h index 3e100259b7..b6bd182ea2 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -8,6 +8,7 @@ #define lopcodes_h #include "llimits.h" +#include "lobject.h" /*=========================================================================== @@ -17,26 +18,31 @@ 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 -iABC C(8) | B(8) |k| A(8) | Op(7) | -iABx Bx(17) | A(8) | Op(7) | -iAsB sBx (signed)(17) | A(8) | Op(7) | -iAx Ax(25) | Op(7) | -isJ sJ(25) | Op(7) | - - A signed argument is represented in excess K: the represented value is - the written unsigned value minus K, where K is half the maximum for the - corresponding unsigned argument. +iABC C(8) | B(8) |k| A(8) | Op(7) | +ivABC vC(10) | vB(6) |k| A(8) | Op(7) | +iABx Bx(17) | A(8) | Op(7) | +iAsBx sBx (signed)(17) | A(8) | Op(7) | +iAx Ax(25) | Op(7) | +isJ sJ (signed)(25) | Op(7) | + + ('v' stands for "variant", 's' for "signed", 'x' for "extended".) + A signed argument is represented in excess K: The represented value is + the written unsigned value minus K, where K is half (rounded down) the + maximum value for the corresponding unsigned argument. ===========================================================================*/ -enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ +/* basic instruction formats */ +enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ}; /* ** size and position of opcode arguments. */ #define SIZE_C 8 +#define SIZE_vC 10 #define SIZE_B 8 +#define SIZE_vB 6 #define SIZE_Bx (SIZE_C + SIZE_B + 1) #define SIZE_A 8 #define SIZE_Ax (SIZE_Bx + SIZE_A) @@ -49,7 +55,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define POS_A (POS_OP + SIZE_OP) #define POS_k (POS_A + SIZE_A) #define POS_B (POS_k + 1) +#define POS_vB (POS_k + 1) #define POS_C (POS_B + SIZE_B) +#define POS_vC (POS_vB + SIZE_vB) #define POS_Bx POS_k @@ -57,30 +65,39 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define POS_sJ POS_A + /* ** limits for opcode arguments. -** we use (signed) int to manipulate most arguments, -** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) +** we use (signed) 'int' to manipulate most arguments, +** so they must fit in ints. +*/ + +/* +** Check whether type 'int' has at least 'b' + 1 bits. +** 'b' < 32; +1 for the sign bit. */ -#if SIZE_Bx < LUAI_BITSINT-1 +#define L_INTHASBITS(b) ((UINT_MAX >> (b)) >= 1) + + +#if L_INTHASBITS(SIZE_Bx) #define MAXARG_Bx ((1<>1) /* 'sBx' is signed */ -#if SIZE_Ax < LUAI_BITSINT-1 +#if L_INTHASBITS(SIZE_Ax) #define MAXARG_Ax ((1<> 1) @@ -88,9 +105,14 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define MAXARG_A ((1<> 1) +#define int2sC(i) ((i) + OFFSET_sC) +#define sC2int(i) ((i) - OFFSET_sC) + /* creates a mask with 'n' 1 bits at position 'p' */ #define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p)) @@ -104,28 +126,36 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0))) #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ - ((cast(Instruction, o)<>(pos)) & MASK1(size,0))) #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ - ((cast(Instruction, v)<> C */ -OP_SHLI,/* A B sC R(A) := C << R(B) */ - -OP_ADD,/* A B C R(A) := R(B) + R(C) */ -OP_SUB,/* A B C R(A) := R(B) - R(C) */ -OP_MUL,/* A B C R(A) := R(B) * R(C) */ -OP_MOD,/* A B C R(A) := R(B) % R(C) */ -OP_POW,/* A B C R(A) := R(B) ^ R(C) */ -OP_DIV,/* A B C R(A) := R(B) / R(C) */ -OP_IDIV,/* A B C R(A) := R(B) // R(C) */ - -OP_BAND,/* A B C R(A) := R(B) & R(C) */ -OP_BOR,/* A B C R(A) := R(B) | R(C) */ -OP_BXOR,/* A B C R(A) := R(B) ~ R(C) */ -OP_SHL,/* A B C R(A) := R(B) << R(C) */ -OP_SHR,/* A B C R(A) := R(B) >> R(C) */ - -OP_UNM,/* A B R(A) := -R(B) */ -OP_BNOT,/* A B R(A) := ~R(B) */ -OP_NOT,/* A B R(A) := not R(B) */ -OP_LEN,/* A B R(A) := length of R(B) */ - -OP_CONCAT,/* A B R(A) := R(A).. ... ..R(A + B - 1) */ - -OP_CLOSE,/* A close all upvalues >= R(A) */ +OP_MOVE,/* A B R[A] := R[B] */ +OP_LOADI,/* A sBx R[A] := sBx */ +OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */ +OP_LOADK,/* A Bx R[A] := K[Bx] */ +OP_LOADKX,/* A R[A] := K[extra arg] */ +OP_LOADFALSE,/* A R[A] := false */ +OP_LFALSESKIP,/*A R[A] := false; pc++ */ +OP_LOADTRUE,/* A R[A] := true */ +OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */ +OP_GETUPVAL,/* A B R[A] := UpValue[B] */ +OP_SETUPVAL,/* A B UpValue[B] := R[A] */ + +OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:shortstring] */ +OP_GETTABLE,/* A B C R[A] := R[B][R[C]] */ +OP_GETI,/* A B C R[A] := R[B][C] */ +OP_GETFIELD,/* A B C R[A] := R[B][K[C]:shortstring] */ + +OP_SETTABUP,/* A B C UpValue[A][K[B]:shortstring] := RK(C) */ +OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */ +OP_SETI,/* A B C R[A][B] := RK(C) */ +OP_SETFIELD,/* A B C R[A][K[B]:shortstring] := RK(C) */ + +OP_NEWTABLE,/* A vB vC k R[A] := {} */ + +OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][K[C]:shortstring] */ + +OP_ADDI,/* A B sC R[A] := R[B] + sC */ + +OP_ADDK,/* A B C R[A] := R[B] + K[C]:number */ +OP_SUBK,/* A B C R[A] := R[B] - K[C]:number */ +OP_MULK,/* A B C R[A] := R[B] * K[C]:number */ +OP_MODK,/* A B C R[A] := R[B] % K[C]:number */ +OP_POWK,/* A B C R[A] := R[B] ^ K[C]:number */ +OP_DIVK,/* A B C R[A] := R[B] / K[C]:number */ +OP_IDIVK,/* A B C R[A] := R[B] // K[C]:number */ + +OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */ +OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */ +OP_BXORK,/* A B C R[A] := R[B] ~ K[C]:integer */ + +OP_SHLI,/* A B sC R[A] := sC << R[B] */ +OP_SHRI,/* A B sC R[A] := R[B] >> sC */ + +OP_ADD,/* A B C R[A] := R[B] + R[C] */ +OP_SUB,/* A B C R[A] := R[B] - R[C] */ +OP_MUL,/* A B C R[A] := R[B] * R[C] */ +OP_MOD,/* A B C R[A] := R[B] % R[C] */ +OP_POW,/* A B C R[A] := R[B] ^ R[C] */ +OP_DIV,/* A B C R[A] := R[B] / R[C] */ +OP_IDIV,/* A B C R[A] := R[B] // R[C] */ + +OP_BAND,/* A B C R[A] := R[B] & R[C] */ +OP_BOR,/* A B C R[A] := R[B] | R[C] */ +OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */ +OP_SHL,/* A B C R[A] := R[B] << R[C] */ +OP_SHR,/* A B C R[A] := R[B] >> R[C] */ + +OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] */ +OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */ +OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */ + +OP_UNM,/* A B R[A] := -R[B] */ +OP_BNOT,/* A B R[A] := ~R[B] */ +OP_NOT,/* A B R[A] := not R[B] */ +OP_LEN,/* A B R[A] := #R[B] (length operator) */ + +OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */ + +OP_CLOSE,/* A close all upvalues >= R[A] */ OP_TBC,/* A mark variable A "to be closed" */ -OP_JMP,/* k sJ pc += sJ (k is used in code generation) */ -OP_EQ,/* A B if ((R(A) == R(B)) ~= k) then pc++ */ -OP_LT,/* A B if ((R(A) < R(B)) ~= k) then pc++ */ -OP_LE,/* A B if ((R(A) <= R(B)) ~= k) then pc++ */ +OP_JMP,/* sJ pc += sJ */ +OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */ +OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */ +OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */ -OP_EQK,/* A B if ((R(A) == K(B)) ~= k) then pc++ */ -OP_EQI,/* A sB if ((R(A) == sB) ~= k) then pc++ */ -OP_LTI,/* A sB if ((R(A) < sB) ~= k) then pc++ */ -OP_LEI,/* A sB if ((R(A) <= sB) ~= k) then pc++ */ -OP_GTI,/* A sB if ((R(A) > sB) ~= k) then pc++ */ -OP_GEI,/* A sB if ((R(A) >= sB) ~= k) then pc++ */ +OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */ +OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */ +OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */ +OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */ +OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */ +OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */ -OP_TEST,/* A if (not R(A) == k) then pc++ */ -OP_TESTSET,/* A B if (not R(B) == k) then pc++ else R(A) := R(B) */ +OP_TEST,/* A k if (not R[A] == k) then pc++ */ +OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */ -OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ -OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ +OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */ +OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */ -OP_RETURN,/* A B C return R(A), ... ,R(A+B-2) (see note) */ -OP_RETURN0,/* return */ -OP_RETURN1,/* A return R(A) */ +OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] */ +OP_RETURN0,/* return */ +OP_RETURN1,/* A return R[A] */ -OP_FORLOOP1,/* A Bx R(A)++; - if R(A) <= R(A+1) then { pc-=Bx; R(A+3)=R(A) } */ -OP_FORPREP1,/* A Bx R(A)--; pc+=Bx */ +OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */ +OP_FORPREP,/* A Bx ; + if not to run then pc+=Bx+1; */ -OP_FORLOOP,/* A Bx R(A)+=R(A+2); - if R(A) 0 means the function is vararg and (C - 1) is its number of + function builds upvalues, which may need to be closed. C > 0 means + the function has hidden vararg arguments, so that its 'func' must be + corrected before returning; in this case, (C - 1) is its number of fixed parameters. + (*) In comparisons with an immediate operand, C signals whether the + original operand was a float. (It must be corrected in case of + metamethods.) + ===========================================================================*/ @@ -345,6 +419,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ ** bit 4: operator is a test (next instruction must be a jump) ** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0) ** bit 6: instruction sets 'L->top' for next instruction (when C == 0) +** bit 7: instruction is an MM instruction (call a metamethod) */ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) @@ -354,20 +429,11 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) #define testTMode(m) (luaP_opmodes[m] & (1 << 4)) #define testITMode(m) (luaP_opmodes[m] & (1 << 5)) #define testOTMode(m) (luaP_opmodes[m] & (1 << 6)) - -/* "out top" (set top for next instruction) */ -#define isOT(i) \ - ((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \ - GET_OPCODE(i) == OP_TAILCALL) - -/* "in top" (uses top from previous instruction) */ -#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0) - -#define opmode(ot,it,t,a,m) (((ot)<<6) | ((it)<<5) | ((t)<<4) | ((a)<<3) | (m)) +#define testMMMode(m) (luaP_opmodes[m] & (1 << 7)) -/* number of list items to accumulate before a SETLIST instruction */ -#define LFIELDS_PER_FLUSH 50 +LUAI_FUNC int luaP_isOT (Instruction i); +LUAI_FUNC int luaP_isIT (Instruction i); #endif diff --git a/lopnames.h b/lopnames.h index 96d901ac68..0554a2e9a1 100644 --- a/lopnames.h +++ b/lopnames.h @@ -7,6 +7,9 @@ #if !defined(lopnames_h) #define lopnames_h +#include + + /* ORDER OP */ static const char *const opnames[] = { @@ -15,7 +18,9 @@ static const char *const opnames[] = { "LOADF", "LOADK", "LOADKX", - "LOADBOOL", + "LOADFALSE", + "LFALSESKIP", + "LOADTRUE", "LOADNIL", "GETUPVAL", "SETUPVAL", @@ -30,12 +35,6 @@ static const char *const opnames[] = { "NEWTABLE", "SELF", "ADDI", - "SUBI", - "MULI", - "MODI", - "POWI", - "DIVI", - "IDIVI", "ADDK", "SUBK", "MULK", @@ -46,8 +45,8 @@ static const char *const opnames[] = { "BANDK", "BORK", "BXORK", - "SHRI", "SHLI", + "SHRI", "ADD", "SUB", "MUL", @@ -60,6 +59,9 @@ static const char *const opnames[] = { "BXOR", "SHL", "SHR", + "MMBIN", + "MMBINI", + "MMBINK", "UNM", "BNOT", "NOT", @@ -84,8 +86,6 @@ static const char *const opnames[] = { "RETURN", "RETURN0", "RETURN1", - "FORLOOP1", - "FORPREP1", "FORLOOP", "FORPREP", "TFORPREP", @@ -94,7 +94,9 @@ static const char *const opnames[] = { "SETLIST", "CLOSURE", "VARARG", - "PREPVARARG", + "GETVARG", + "ERRNNIL", + "VARARGPREP", "EXTRAARG", NULL }; diff --git a/loslib.c b/loslib.c index 8809e5ea2a..b7a2b0d15f 100644 --- a/loslib.c +++ b/loslib.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include #include @@ -19,6 +20,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* @@ -29,23 +31,14 @@ */ #if !defined(LUA_STRFTIMEOPTIONS) /* { */ -/* options for ANSI C 89 (only 1-char options) */ -#define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%" - -/* options for ISO C 99 and POSIX */ -#define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ - "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */ - -/* options for Windows */ -#define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \ - "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */ - #if defined(LUA_USE_WINDOWS) -#define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN -#elif defined(LUA_USE_C89) -#define LUA_STRFTIMEOPTIONS L_STRFTIMEC89 +#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%" \ + "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */ +#elif defined(LUA_USE_C89) /* C89 (only 1-char options) */ +#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYZ%" #else /* C99 specification */ -#define LUA_STRFTIMEOPTIONS L_STRFTIMEC99 +#define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ + "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */ #endif #endif /* } */ @@ -58,18 +51,20 @@ ** =================================================================== */ -#if !defined(l_time_t) /* { */ /* ** type to represent time_t in Lua */ +#if !defined(LUA_NUMTIME) /* { */ + #define l_timet lua_Integer #define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) +#define l_gettime(L,arg) luaL_checkinteger(L, arg) -static time_t l_checktime (lua_State *L, int arg) { - lua_Integer t = luaL_checkinteger(L, arg); - luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds"); - return (time_t)t; -} +#else /* }{ */ + +#define l_timet lua_Number +#define l_pushtime(L,t) lua_pushnumber(L,(lua_Number)(t)) +#define l_gettime(L,arg) luaL_checknumber(L, arg) #endif /* } */ @@ -89,7 +84,7 @@ static time_t l_checktime (lua_State *L, int arg) { /* ISO C definitions */ #define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t)) -#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) +#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) #endif /* } */ @@ -135,11 +130,21 @@ static time_t l_checktime (lua_State *L, int arg) { /* }================================================================== */ +#if !defined(l_system) +#if defined(LUA_USE_IOS) +/* Despite claiming to be ISO C, iOS does not implement 'system'. */ +#define l_system(cmd) ((cmd) == NULL ? 0 : -1) +#else +#define l_system(cmd) system(cmd) /* default definition */ +#endif +#endif static int os_execute (lua_State *L) { const char *cmd = luaL_optstring(L, 1, NULL); - int stat = system(cmd); + int stat; + errno = 0; + stat = l_system(cmd); if (cmd != NULL) return luaL_execresult(L, stat); else { @@ -151,6 +156,7 @@ static int os_execute (lua_State *L) { static int os_remove (lua_State *L) { const char *filename = luaL_checkstring(L, 1); + errno = 0; return luaL_fileresult(L, remove(filename) == 0, filename); } @@ -158,6 +164,7 @@ static int os_remove (lua_State *L) { static int os_rename (lua_State *L) { const char *fromname = luaL_checkstring(L, 1); const char *toname = luaL_checkstring(L, 2); + errno = 0; return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); } @@ -166,7 +173,7 @@ static int os_tmpname (lua_State *L) { char buff[LUA_TMPNAMBUFSIZE]; int err; lua_tmpnam(buff, err); - if (err) + if (l_unlikely(err)) return luaL_error(L, "unable to generate a unique filename"); lua_pushstring(L, buff); return 1; @@ -193,11 +200,25 @@ static int os_clock (lua_State *L) { ** ======================================================= */ -static void setfield (lua_State *L, const char *key, int value) { - lua_pushinteger(L, value); +/* +** About the overflow check: an overflow cannot occur when time +** is represented by a lua_Integer, because either lua_Integer is +** large enough to represent all int fields or it is not large enough +** to represent a time that cause a field to overflow. However, if +** times are represented as doubles and lua_Integer is int, then the +** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900 +** to compute the year. +*/ +static void setfield (lua_State *L, const char *key, int value, int delta) { + #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX) + if (l_unlikely(value > LUA_MAXINTEGER - delta)) + luaL_error(L, "field '%s' is out-of-bound", key); + #endif + lua_pushinteger(L, (lua_Integer)value + delta); lua_setfield(L, -2, key); } + static void setboolfield (lua_State *L, const char *key, int value) { if (value < 0) /* undefined? */ return; /* does not set field */ @@ -210,14 +231,14 @@ static void setboolfield (lua_State *L, const char *key, int value) { ** Set all fields from structure 'tm' in the table on top of the stack */ static void setallfields (lua_State *L, struct tm *stm) { - setfield(L, "sec", stm->tm_sec); - setfield(L, "min", stm->tm_min); - setfield(L, "hour", stm->tm_hour); - setfield(L, "day", stm->tm_mday); - setfield(L, "month", stm->tm_mon + 1); - setfield(L, "year", stm->tm_year + 1900); - setfield(L, "wday", stm->tm_wday + 1); - setfield(L, "yday", stm->tm_yday + 1); + setfield(L, "year", stm->tm_year, 1900); + setfield(L, "month", stm->tm_mon, 1); + setfield(L, "day", stm->tm_mday, 0); + setfield(L, "hour", stm->tm_hour, 0); + setfield(L, "min", stm->tm_min, 0); + setfield(L, "sec", stm->tm_sec, 0); + setfield(L, "yday", stm->tm_yday, 1); + setfield(L, "wday", stm->tm_wday, 1); setboolfield(L, "isdst", stm->tm_isdst); } @@ -230,24 +251,19 @@ static int getboolfield (lua_State *L, const char *key) { } -/* maximum value for date fields (to avoid arithmetic overflows with 'int') */ -#if !defined(L_MAXDATEFIELD) -#define L_MAXDATEFIELD (INT_MAX / 2) -#endif - static int getfield (lua_State *L, const char *key, int d, int delta) { int isnum; int t = lua_getfield(L, -1, key); /* get field and its type */ lua_Integer res = lua_tointegerx(L, -1, &isnum); if (!isnum) { /* field is not an integer? */ - if (t != LUA_TNIL) /* some other value? */ + if (l_unlikely(t != LUA_TNIL)) /* some other value? */ return luaL_error(L, "field '%s' is not an integer", key); - else if (d < 0) /* absent field; no default? */ + else if (l_unlikely(d < 0)) /* absent field; no default? */ return luaL_error(L, "field '%s' missing in date table", key); res = d; } else { - if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD)) + if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res)) return luaL_error(L, "field '%s' is out-of-bound", key); res -= delta; } @@ -257,9 +273,9 @@ static int getfield (lua_State *L, const char *key, int d, int delta) { static const char *checkoption (lua_State *L, const char *conv, - ptrdiff_t convlen, char *buff) { + size_t convlen, char *buff) { const char *option = LUA_STRFTIMEOPTIONS; - int oplen = 1; /* length of options being checked */ + unsigned oplen = 1; /* length of options being checked */ for (; *option != '\0' && oplen <= convlen; option += oplen) { if (*option == '|') /* next block? */ oplen++; /* will check options with next length (+1) */ @@ -275,6 +291,13 @@ static const char *checkoption (lua_State *L, const char *conv, } +static time_t l_checktime (lua_State *L, int arg) { + l_timet t = l_gettime(L, arg); + luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds"); + return (time_t)t; +} + + /* maximum size for an individual 'strftime' item */ #define SIZETIMEFMT 250 @@ -293,7 +316,7 @@ static int os_date (lua_State *L) { stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ return luaL_error(L, - "time result cannot be represented in this installation"); + "date result cannot be represented in this installation"); if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setallfields(L, stm); @@ -310,7 +333,8 @@ static int os_date (lua_State *L) { size_t reslen; char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); s++; /* skip '%' */ - s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */ + /* copy specifier to 'cc' */ + s = checkoption(L, s, ct_diff2sz(se - s), cc + 1); reslen = strftime(buff, SIZETIMEFMT, cc, stm); luaL_addsize(&b, reslen); } @@ -329,12 +353,12 @@ static int os_time (lua_State *L) { struct tm ts; luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 1); /* make sure table is at the top */ - ts.tm_sec = getfield(L, "sec", 0, 0); - ts.tm_min = getfield(L, "min", 0, 0); - ts.tm_hour = getfield(L, "hour", 12, 0); - ts.tm_mday = getfield(L, "day", -1, 0); - ts.tm_mon = getfield(L, "month", -1, 1); ts.tm_year = getfield(L, "year", -1, 1900); + ts.tm_mon = getfield(L, "month", -1, 1); + ts.tm_mday = getfield(L, "day", -1, 0); + ts.tm_hour = getfield(L, "hour", 12, 0); + ts.tm_min = getfield(L, "min", 0, 0); + ts.tm_sec = getfield(L, "sec", 0, 0); ts.tm_isdst = getboolfield(L, "isdst"); t = mktime(&ts); setallfields(L, &ts); /* update fields with normalized values */ diff --git a/lparser.c b/lparser.c index 3887958ef7..b3855d4cb6 100644 --- a/lparser.c +++ b/lparser.c @@ -30,8 +30,8 @@ -/* maximum number of local variables per function (must be smaller - than 250, due to the bytecode format) */ +/* maximum number of variable declarations per function (must be + smaller than 250, due to the bytecode format) */ #define MAXVARS 200 @@ -50,9 +50,9 @@ typedef struct BlockCnt { struct BlockCnt *previous; /* chain */ int firstlabel; /* index of first label in this block */ int firstgoto; /* index of first pending goto in this block */ - lu_byte nactvar; /* # active locals outside the block */ + short nactvar; /* number of active declarations at block entry */ lu_byte upval; /* true if some variable in the block is an upvalue */ - lu_byte isloop; /* true if 'block' is a loop */ + lu_byte isloop; /* 1 if 'block' is a loop; 2 if it has pending breaks */ lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */ } BlockCnt; @@ -84,8 +84,8 @@ static l_noret errorlimit (FuncState *fs, int limit, const char *what) { } -static void checklimit (FuncState *fs, int v, int l, const char *what) { - if (v > l) errorlimit(fs, l, what); +void luaY_checklimit (FuncState *fs, int v, int l, const char *what) { + if (l_unlikely(v > l)) errorlimit(fs, l, what); } @@ -128,7 +128,7 @@ static void checknext (LexState *ls, int c) { ** in line 'where' (if that is not the current line). */ static void check_match (LexState *ls, int what, int who, int where) { - if (unlikely(!testnext(ls, what))) { + if (l_unlikely(!testnext(ls, what))) { if (where == ls->linenumber) /* all in the same line? */ error_expected(ls, what); /* do not need a complex message */ else { @@ -156,13 +156,15 @@ static void init_exp (expdesc *e, expkind k, int i) { } -static void codestring (LexState *ls, expdesc *e, TString *s) { - init_exp(e, VK, luaK_stringK(ls->fs, s)); +static void codestring (expdesc *e, TString *s) { + e->f = e->t = NO_JUMP; + e->k = VKSTR; + e->u.strval = s; } static void codename (LexState *ls, expdesc *e) { - codestring(ls, e, str_checkname(ls)); + codestring(e, str_checkname(ls)); } @@ -170,57 +172,169 @@ static void codename (LexState *ls, expdesc *e) { ** Register a new local variable in the active 'Proto' (for debug ** information). */ -static int registerlocalvar (LexState *ls, TString *varname) { - FuncState *fs = ls->fs; +static short registerlocalvar (LexState *ls, FuncState *fs, + TString *varname) { Proto *f = fs->f; int oldsize = f->sizelocvars; - luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, + luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars, LocVar, SHRT_MAX, "local variables"); while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; - f->locvars[fs->nlocvars].varname = varname; + f->locvars[fs->ndebugvars].varname = varname; + f->locvars[fs->ndebugvars].startpc = fs->pc; luaC_objbarrier(ls->L, f, varname); - return fs->nlocvars++; + return fs->ndebugvars++; } /* -** Create a new local variable with the given 'name'. +** Create a new variable with the given 'name' and given 'kind'. +** Return its index in the function. */ -static void new_localvar (LexState *ls, TString *name) { +static int new_varkind (LexState *ls, TString *name, lu_byte kind) { + lua_State *L = ls->L; FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; - int reg = registerlocalvar(ls, name); - checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, - MAXVARS, "local variables"); - luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1, - dyd->actvar.size, Vardesc, MAX_INT, "local variables"); - dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg); + Vardesc *var; + luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, + dyd->actvar.size, Vardesc, SHRT_MAX, "variable declarations"); + var = &dyd->actvar.arr[dyd->actvar.n++]; + var->vd.kind = kind; /* default */ + var->vd.name = name; + return dyd->actvar.n - 1 - fs->firstlocal; +} + + +/* +** Create a new local variable with the given 'name' and regular kind. +*/ +static int new_localvar (LexState *ls, TString *name) { + return new_varkind(ls, name, VDKREG); } #define new_localvarliteral(ls,v) \ - new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); + new_localvar(ls, \ + luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); + + + +/* +** Return the "variable description" (Vardesc) of a given variable. +** (Unless noted otherwise, all variables are referred to by their +** compiler indices.) +*/ +static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { + return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx]; +} + + +/* +** Convert 'nvar', a compiler index level, to its corresponding +** register. For that, search for the highest variable below that level +** that is in a register and uses its register index ('ridx') plus one. +*/ +static lu_byte reglevel (FuncState *fs, int nvar) { + while (nvar-- > 0) { + Vardesc *vd = getlocalvardesc(fs, nvar); /* get previous variable */ + if (varinreg(vd)) /* is in a register? */ + return cast_byte(vd->vd.ridx + 1); + } + return 0; /* no variables in registers */ +} + + +/* +** Return the number of variables in the register stack for the given +** function. +*/ +lu_byte luaY_nvarstack (FuncState *fs) { + return reglevel(fs, fs->nactvar); +} + + +/* +** Get the debug-information entry for current variable 'vidx'. +*/ +static LocVar *localdebuginfo (FuncState *fs, int vidx) { + Vardesc *vd = getlocalvardesc(fs, vidx); + if (!varinreg(vd)) + return NULL; /* no debug info. for constants */ + else { + int idx = vd->vd.pidx; + lua_assert(idx < fs->ndebugvars); + return &fs->f->locvars[idx]; + } +} /* -** Get the debug-information entry for current variable 'i'. +** Create an expression representing variable 'vidx' */ -static LocVar *getlocvar (FuncState *fs, int i) { - int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx; - lua_assert(idx < fs->nlocvars); - return &fs->f->locvars[idx]; +static void init_var (FuncState *fs, expdesc *e, int vidx) { + e->f = e->t = NO_JUMP; + e->k = VLOCAL; + e->u.var.vidx = cast_short(vidx); + e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx; +} + + +/* +** Raises an error if variable described by 'e' is read only; moreover, +** if 'e' is t[exp] where t is the vararg parameter, change it to index +** a real table. (Virtual vararg tables cannot be changed.) +*/ +static void check_readonly (LexState *ls, expdesc *e) { + FuncState *fs = ls->fs; + TString *varname = NULL; /* to be set if variable is const */ + switch (e->k) { + case VCONST: { + varname = ls->dyd->actvar.arr[e->u.info].vd.name; + break; + } + case VLOCAL: case VVARGVAR: { + Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); + if (vardesc->vd.kind != VDKREG) /* not a regular variable? */ + varname = vardesc->vd.name; + break; + } + case VUPVAL: { + Upvaldesc *up = &fs->f->upvalues[e->u.info]; + if (up->kind != VDKREG) + varname = up->name; + break; + } + case VVARGIND: { + needvatab(fs->f); /* function will need a vararg table */ + e->k = VINDEXED; + } /* FALLTHROUGH */ + case VINDEXUP: case VINDEXSTR: case VINDEXED: { /* global variable */ + if (e->u.ind.ro) /* read-only? */ + varname = tsvalue(&fs->f->k[e->u.ind.keystr]); + break; + } + default: + lua_assert(e->k == VINDEXI); /* this one doesn't need any check */ + return; /* integer index cannot be read-only */ + } + if (varname) + luaK_semerror(ls, "attempt to assign to const variable '%s'", + getstr(varname)); } /* ** Start the scope for the last 'nvars' created variables. -** (debug info.) */ static void adjustlocalvars (LexState *ls, int nvars) { FuncState *fs = ls->fs; - fs->nactvar = cast_byte(fs->nactvar + nvars); - for (; nvars; nvars--) { - getlocvar(fs, fs->nactvar - nvars)->startpc = fs->pc; + int reglevel = luaY_nvarstack(fs); + int i; + for (i = 0; i < nvars; i++) { + int vidx = fs->nactvar++; + Vardesc *var = getlocalvardesc(fs, vidx); + var->vd.ridx = cast_byte(reglevel++); + var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); + luaY_checklimit(fs, reglevel, MAXVARS, "local variables"); } } @@ -231,8 +345,11 @@ static void adjustlocalvars (LexState *ls, int nvars) { */ static void removevars (FuncState *fs, int tolevel) { fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); - while (fs->nactvar > tolevel) - getlocvar(fs, --fs->nactvar)->endpc = fs->pc; + while (fs->nactvar > tolevel) { + LocVar *var = localdebuginfo(fs, --fs->nactvar); + if (var) /* does it have debug information? */ + var->endpc = fs->pc; + } } @@ -250,31 +367,78 @@ static int searchupvalue (FuncState *fs, TString *name) { } -static int newupvalue (FuncState *fs, TString *name, expdesc *v) { +static Upvaldesc *allocupvalue (FuncState *fs) { Proto *f = fs->f; int oldsize = f->sizeupvalues; - checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); + luaY_checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, Upvaldesc, MAXUPVAL, "upvalues"); while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL; - f->upvalues[fs->nups].instack = (v->k == VLOCAL); - f->upvalues[fs->nups].idx = cast_byte(v->u.info); - f->upvalues[fs->nups].name = name; - luaC_objbarrier(fs->ls->L, f, name); - return fs->nups++; + return &f->upvalues[fs->nups++]; +} + + +static int newupvalue (FuncState *fs, TString *name, expdesc *v) { + Upvaldesc *up = allocupvalue(fs); + FuncState *prev = fs->prev; + if (v->k == VLOCAL) { + up->instack = 1; + up->idx = v->u.var.ridx; + up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind; + lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name)); + } + else { + up->instack = 0; + up->idx = cast_byte(v->u.info); + up->kind = prev->f->upvalues[v->u.info].kind; + lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name)); + } + up->name = name; + luaC_objbarrier(fs->ls->L, fs->f, name); + return fs->nups - 1; } /* -** Look for an active local variable with the name 'n' in the -** function 'fs'. +** Look for an active variable with the name 'n' in the +** function 'fs'. If found, initialize 'var' with it and return +** its expression kind; otherwise return -1. While searching, +** var->u.info==-1 means that the preambular global declaration is +** active (the default while there is no other global declaration); +** var->u.info==-2 means there is no active collective declaration +** (some previous global declaration but no collective declaration); +** and var->u.info>=0 points to the inner-most (the first one found) +** collective declaration, if there is one. */ -static int searchvar (FuncState *fs, TString *n) { +static int searchvar (FuncState *fs, TString *n, expdesc *var) { int i; for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { - if (eqstr(n, getlocvar(fs, i)->varname)) - return i; + Vardesc *vd = getlocalvardesc(fs, i); + if (varglobal(vd)) { /* global declaration? */ + if (vd->vd.name == NULL) { /* collective declaration? */ + if (var->u.info < 0) /* no previous collective declaration? */ + var->u.info = fs->firstlocal + i; /* this is the first one */ + } + else { /* global name */ + if (eqstr(n, vd->vd.name)) { /* found? */ + init_exp(var, VGLOBAL, fs->firstlocal + i); + return VGLOBAL; + } + else if (var->u.info == -1) /* active preambular declaration? */ + var->u.info = -2; /* invalidate preambular declaration */ + } + } + else if (eqstr(n, vd->vd.name)) { /* found? */ + if (vd->vd.kind == RDKCTC) /* compile-time constant? */ + init_exp(var, VCONST, fs->firstlocal + i); + else { /* local variable */ + init_var(fs, var, i); + if (vd->vd.kind == RDKVAVAR) /* vararg parameter? */ + var->k = VVARGVAR; + } + return cast_int(var->k); + } } return -1; /* not found */ } @@ -293,54 +457,89 @@ static void markupval (FuncState *fs, int level) { } +/* +** Mark that current block has a to-be-closed variable. +*/ +static void marktobeclosed (FuncState *fs) { + BlockCnt *bl = fs->bl; + bl->upval = 1; + bl->insidetbc = 1; + fs->needclose = 1; +} + + /* ** Find a variable with the given name 'n'. If it is an upvalue, add ** this upvalue into all intermediate functions. If it is a global, set ** 'var' as 'void' as a flag. */ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { - if (fs == NULL) /* no more levels? */ - init_exp(var, VVOID, 0); /* default is global */ - else { - int v = searchvar(fs, n); /* look up locals at current level */ - if (v >= 0) { /* found? */ - init_exp(var, VLOCAL, v); /* variable is local */ - if (!base) - markupval(fs, v); /* local will be used as an upval */ + int v = searchvar(fs, n, var); /* look up variables at current level */ + if (v >= 0) { /* found? */ + if (!base) { + if (var->k == VVARGVAR) /* vararg parameter? */ + luaK_vapar2local(fs, var); /* change it to a regular local */ + if (var->k == VLOCAL) + markupval(fs, var->u.var.vidx); /* will be used as an upvalue */ } - else { /* not found as local at current level; try upvalues */ - int idx = searchupvalue(fs, n); /* try existing upvalues */ - if (idx < 0) { /* not found? */ + /* else nothing else to be done */ + } + else { /* not found at current level; try upvalues */ + int idx = searchupvalue(fs, n); /* try existing upvalues */ + if (idx < 0) { /* not found? */ + if (fs->prev != NULL) /* more levels? */ singlevaraux(fs->prev, n, var, 0); /* try upper levels */ - if (var->k == VVOID) /* not found? */ - return; /* it is a global */ - /* else was LOCAL or UPVAL */ + if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ idx = newupvalue(fs, n, var); /* will be a new upvalue */ - } - init_exp(var, VUPVAL, idx); /* new or old upvalue */ + else /* it is a global or a constant */ + return; /* don't need to do anything at this level */ } + init_exp(var, VUPVAL, idx); /* new or old upvalue */ } } +static void buildglobal (LexState *ls, TString *varname, expdesc *var) { + FuncState *fs = ls->fs; + expdesc key; + init_exp(var, VGLOBAL, -1); /* global by default */ + singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ + if (var->k == VGLOBAL) + luaK_semerror(ls, "%s is global when accessing variable '%s'", + LUA_ENV, getstr(varname)); + luaK_exp2anyregup(fs, var); /* _ENV could be a constant */ + codestring(&key, varname); /* key is variable name */ + luaK_indexed(fs, var, &key); /* 'var' represents _ENV[varname] */ +} + + /* ** Find a variable with the given name 'n', handling global variables ** too. */ -static void singlevar (LexState *ls, expdesc *var) { - TString *varname = str_checkname(ls); +static void buildvar (LexState *ls, TString *varname, expdesc *var) { FuncState *fs = ls->fs; + init_exp(var, VGLOBAL, -1); /* global by default */ singlevaraux(fs, varname, var, 1); - if (var->k == VVOID) { /* global name? */ - expdesc key; - singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ - lua_assert(var->k != VVOID); /* this one must exist */ - codestring(ls, &key, varname); /* key is variable name */ - luaK_indexed(fs, var, &key); /* env[varname] */ + if (var->k == VGLOBAL) { /* global name? */ + int info = var->u.info; + /* global by default in the scope of a global declaration? */ + if (info == -2) + luaK_semerror(ls, "variable '%s' not declared", getstr(varname)); + buildglobal(ls, varname, var); + if (info != -1 && ls->dyd->actvar.arr[info].vd.kind == GDKCONST) + var->u.ind.ro = 1; /* mark variable as read-only */ + else /* anyway must be a global */ + lua_assert(info == -1 || ls->dyd->actvar.arr[info].vd.kind == GDKREG); } } +static void singlevar (LexState *ls, expdesc *var) { + buildvar(ls, str_checkname(ls), var); +} + + /* ** Adjust the number of results from an expression list 'e' with 'nexps' ** expressions to 'nvars' values. @@ -348,6 +547,7 @@ static void singlevar (LexState *ls, expdesc *var) { static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { FuncState *fs = ls->fs; int needed = nvars - nexps; /* extra values needed */ + luaK_checkstack(fs, needed); if (hasmultret(e->k)) { /* last expression has multiple returns? */ int extra = needed + 1; /* discount last expression itself */ if (extra < 0) @@ -363,43 +563,55 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { if (needed > 0) luaK_reserveregs(fs, needed); /* registers for extra values */ else /* adding 'needed' is actually a subtraction */ - fs->freereg += needed; /* remove extra values */ + fs->freereg = cast_byte(fs->freereg + needed); /* remove extra values */ } -/* -** Macros to limit the maximum recursion depth while parsing -*/ -#define enterlevel(ls) luaE_enterCcall((ls)->L) +#define enterlevel(ls) luaE_incCstack(ls->L) + -#define leavelevel(ls) luaE_exitCcall((ls)->L) +#define leavelevel(ls) ((ls)->L->nCcalls--) /* ** Generates an error that a goto jumps into the scope of some -** local variable. +** variable declaration. */ static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { - const char *varname = getstr(getlocvar(ls->fs, gt->nactvar)->varname); - const char *msg = " at line %d jumps into the scope of local '%s'"; - msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); - luaK_semerror(ls, msg); /* raise the error */ + TString *tsname = getlocalvardesc(ls->fs, gt->nactvar)->vd.name; + const char *varname = (tsname != NULL) ? getstr(tsname) : "*"; + luaK_semerror(ls, + " at line %d jumps into the scope of '%s'", + getstr(gt->name), gt->line, varname); /* raise the error */ } /* -** Solves the goto at index 'g' to given 'label' and removes it -** from the list of pending goto's. +** Closes the goto at index 'g' to given 'label' and removes it +** from the list of pending gotos. ** If it jumps into the scope of some variable, raises an error. +** The goto needs a CLOSE if it jumps out of a block with upvalues, +** or out of the scope of some variable and the block has upvalues +** (signaled by parameter 'bup'). */ -static void solvegoto (LexState *ls, int g, Labeldesc *label) { +static void closegoto (LexState *ls, int g, Labeldesc *label, int bup) { int i; - Labellist *gl = &ls->dyd->gt; /* list of goto's */ + FuncState *fs = ls->fs; + Labellist *gl = &ls->dyd->gt; /* list of gotos */ Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ lua_assert(eqstr(gt->name, label->name)); - if (unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ + if (l_unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ jumpscopeerror(ls, gt); - luaK_patchlist(ls->fs, gt->pc, label->pc); + if (gt->close || + (label->nactvar < gt->nactvar && bup)) { /* needs close? */ + lu_byte stklevel = reglevel(fs, label->nactvar); + /* move jump to CLOSE position */ + fs->f->code[gt->pc + 1] = fs->f->code[gt->pc]; + /* put CLOSE instruction at original position */ + fs->f->code[gt->pc] = CREATE_ABCk(OP_CLOSE, stklevel, 0, 0, 0); + gt->pc++; /* must point to jump instruction */ + } + luaK_patchlist(ls->fs, gt->pc, label->pc); /* goto jumps to label */ for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ gl->arr[i] = gl->arr[i + 1]; gl->n--; @@ -407,14 +619,14 @@ static void solvegoto (LexState *ls, int g, Labeldesc *label) { /* -** Search for an active label with the given name. +** Search for an active label with the given name, starting at +** index 'ilb' (so that it can search for all labels in current block +** or all labels in current function). */ -static Labeldesc *findlabel (LexState *ls, TString *name) { - int i; +static Labeldesc *findlabel (LexState *ls, TString *name, int ilb) { Dyndata *dyd = ls->dyd; - /* check labels in current function for a match */ - for (i = ls->fs->firstlabel; i < dyd->label.n; i++) { - Labeldesc *lb = &dyd->label.arr[i]; + for (; ilb < dyd->label.n; ilb++) { + Labeldesc *lb = &dyd->label.arr[ilb]; if (eqstr(lb->name, name)) /* correct label? */ return lb; } @@ -440,41 +652,30 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name, } -static int newgotoentry (LexState *ls, TString *name, int line, int pc) { - return newlabelentry(ls, &ls->dyd->gt, name, line, pc); -} - - /* -** Solves forward jumps. Check whether new label 'lb' matches any -** pending gotos in current block and solves them. Return true -** if any of the goto's need to close upvalues. +** Create an entry for the goto and the code for it. As it is not known +** at this point whether the goto may need a CLOSE, the code has a jump +** followed by an CLOSE. (As the CLOSE comes after the jump, it is a +** dead instruction; it works as a placeholder.) When the goto is closed +** against a label, if it needs a CLOSE, the two instructions swap +** positions, so that the CLOSE comes before the jump. */ -static int solvegotos (LexState *ls, Labeldesc *lb) { - Labellist *gl = &ls->dyd->gt; - int i = ls->fs->bl->firstgoto; - int needsclose = 0; - while (i < gl->n) { - if (eqstr(gl->arr[i].name, lb->name)) { - needsclose |= gl->arr[i].close; - solvegoto(ls, i, lb); /* will remove 'i' from the list */ - } - else - i++; - } - return needsclose; +static int newgotoentry (LexState *ls, TString *name, int line) { + FuncState *fs = ls->fs; + int pc = luaK_jump(fs); /* create jump */ + luaK_codeABC(fs, OP_CLOSE, 0, 1, 0); /* spaceholder, marked as dead */ + return newlabelentry(ls, &ls->dyd->gt, name, line, pc); } /* ** Create a new label with the given 'name' at the given 'line'. ** 'last' tells whether label is the last non-op statement in its -** block. Solves all pending goto's to this new label and adds +** block. Solves all pending gotos to this new label and adds ** a close instruction if necessary. ** Returns true iff it added a close instruction. */ -static int createlabel (LexState *ls, TString *name, int line, - int last) { +static void createlabel (LexState *ls, TString *name, int line, int last) { FuncState *fs = ls->fs; Labellist *ll = &ls->dyd->label; int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs)); @@ -482,28 +683,37 @@ static int createlabel (LexState *ls, TString *name, int line, /* assume that locals are already out of scope */ ll->arr[l].nactvar = fs->bl->nactvar; } - if (solvegotos(ls, &ll->arr[l])) { /* need close? */ - luaK_codeABC(fs, OP_CLOSE, fs->nactvar, 0, 0); - return 1; - } - return 0; } /* -** Adjust pending gotos to outer level of a block. +** Traverse the pending gotos of the finishing block checking whether +** each match some label of that block. Those that do not match are +** "exported" to the outer block, to be solved there. In particular, +** its 'nactvar' is updated with the level of the inner block, +** as the variables of the inner block are now out of scope. */ -static void movegotosout (FuncState *fs, BlockCnt *bl) { - int i; - Labellist *gl = &fs->ls->dyd->gt; - /* correct pending gotos to current block */ - for (i = bl->firstgoto; i < gl->n; i++) { /* for each pending goto */ - Labeldesc *gt = &gl->arr[i]; - if (gt->nactvar > bl->nactvar) { /* leaving a variable scope? */ - gt->nactvar = bl->nactvar; /* update goto level */ - gt->close |= bl->upval; /* jump may need a close */ +static void solvegotos (FuncState *fs, BlockCnt *bl) { + LexState *ls = fs->ls; + Labellist *gl = &ls->dyd->gt; + int outlevel = reglevel(fs, bl->nactvar); /* level outside the block */ + int igt = bl->firstgoto; /* first goto in the finishing block */ + while (igt < gl->n) { /* for each pending goto */ + Labeldesc *gt = &gl->arr[igt]; + /* search for a matching label in the current block */ + Labeldesc *lb = findlabel(ls, gt->name, bl->firstlabel); + if (lb != NULL) /* found a match? */ + closegoto(ls, igt, lb, bl->upval); /* close and remove goto */ + else { /* adjust 'goto' for outer block */ + /* block has variables to be closed and goto escapes the scope of + some variable? */ + if (bl->upval && reglevel(fs, gt->nactvar) > outlevel) + gt->close = 1; /* jump may need a close */ + gt->nactvar = bl->nactvar; /* correct level for outer block */ + igt++; /* go to next goto */ } } + ls->dyd->label.n = bl->firstlabel; /* remove local labels */ } @@ -513,10 +723,11 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { bl->firstlabel = fs->ls->dyd->label.n; bl->firstgoto = fs->ls->dyd->gt.n; bl->upval = 0; + /* inherit 'insidetbc' from enclosing block */ bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); - bl->previous = fs->bl; + bl->previous = fs->bl; /* link block in function's block list */ fs->bl = bl; - lua_assert(fs->freereg == fs->nactvar); + lua_assert(fs->freereg == luaY_nvarstack(fs)); } @@ -524,38 +735,30 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { ** generates an error for an undefined 'goto'. */ static l_noret undefgoto (LexState *ls, Labeldesc *gt) { - const char *msg; - if (eqstr(gt->name, luaS_newliteral(ls->L, "break"))) { - msg = "break outside loop at line %d"; - msg = luaO_pushfstring(ls->L, msg, gt->line); - } - else { - msg = "no visible label '%s' for at line %d"; - msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); - } - luaK_semerror(ls, msg); + /* breaks are checked when created, cannot be undefined */ + lua_assert(!eqstr(gt->name, ls->brkn)); + luaK_semerror(ls, "no visible label '%s' for at line %d", + getstr(gt->name), gt->line); } static void leaveblock (FuncState *fs) { BlockCnt *bl = fs->bl; LexState *ls = fs->ls; - int hasclose = 0; - if (bl->isloop) /* fix pending breaks? */ - hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); - if (!hasclose && bl->previous && bl->upval) - luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); - fs->bl = bl->previous; - removevars(fs, bl->nactvar); - lua_assert(bl->nactvar == fs->nactvar); - fs->freereg = fs->nactvar; /* free registers */ - ls->dyd->label.n = bl->firstlabel; /* remove local labels */ - if (bl->previous) /* inner block? */ - movegotosout(fs, bl); /* update pending gotos to outer block */ - else { - if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ + lu_byte stklevel = reglevel(fs, bl->nactvar); /* level outside block */ + if (bl->previous && bl->upval) /* need a 'close'? */ + luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); + fs->freereg = stklevel; /* free registers */ + removevars(fs, bl->nactvar); /* remove block locals */ + lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */ + if (bl->isloop == 2) /* has to fix pending breaks? */ + createlabel(ls, ls->brkn, 0, 0); + solvegotos(fs, bl); + if (bl->previous == NULL) { /* was it the last block? */ + if (bl->firstgoto < ls->dyd->gt.n) /* still pending gotos? */ undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ } + fs->bl = bl->previous; /* current block now is previous one */ } @@ -581,9 +784,10 @@ static Proto *addprototype (LexState *ls) { /* ** codes instruction to create new closure in parent function. -** The OP_CLOSURE instruction must use the last available register, +** The OP_CLOSURE instruction uses the last available register, ** so that, if it invokes the GC, the GC knows which registers ** are in use at that time. + */ static void codeclosure (LexState *ls, expdesc *v) { FuncState *fs = ls->fs->prev; @@ -593,6 +797,7 @@ static void codeclosure (LexState *ls, expdesc *v) { static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { + lua_State *L = ls->L; Proto *f = fs->f; fs->prev = ls->fs; /* linked list of funcstates */ fs->ls = ls; @@ -606,14 +811,18 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { fs->nabslineinfo = 0; fs->np = 0; fs->nups = 0; - fs->nlocvars = 0; + fs->ndebugvars = 0; fs->nactvar = 0; fs->needclose = 0; fs->firstlocal = ls->dyd->actvar.n; fs->firstlabel = ls->dyd->label.n; fs->bl = NULL; f->source = ls->source; + luaC_objbarrier(L, f, f->source); f->maxstacksize = 2; /* registers 0/1 are always valid */ + fs->kcache = luaH_new(L); /* create table for function */ + sethvalue2s(L, L->top.p, fs->kcache); /* anchor it */ + luaD_inctop(L); enterblock(fs, bl, 0); } @@ -622,7 +831,7 @@ static void close_func (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; - luaK_ret(fs, fs->nactvar, 0); /* final return */ + luaK_ret(fs, luaY_nvarstack(fs), 0); /* final return */ leaveblock(fs); lua_assert(fs->bl == NULL); luaK_finish(fs); @@ -632,17 +841,19 @@ static void close_func (LexState *ls) { fs->nabslineinfo, AbsLineInfo); luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue); luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); - luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); + luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); ls->fs = fs->prev; + L->top.p--; /* pop kcache table */ luaC_checkGC(L); } - -/*============================================================*/ -/* GRAMMAR RULES */ -/*============================================================*/ +/* +** {====================================================================== +** GRAMMAR RULES +** ======================================================================= +*/ /* @@ -699,25 +910,36 @@ static void yindex (LexState *ls, expdesc *v) { ** ======================================================================= */ - -struct ConsControl { +typedef struct ConsControl { expdesc v; /* last list item read */ expdesc *t; /* table descriptor */ int nh; /* total number of 'record' elements */ - int na; /* total number of array elements */ + int na; /* number of array elements already stored */ int tostore; /* number of array elements pending to be stored */ -}; + int maxtostore; /* maximum number of pending elements */ +} ConsControl; + +/* +** Maximum number of elements in a constructor, to control the following: +** * counter overflows; +** * overflows in 'extra' for OP_NEWTABLE and OP_SETLIST; +** * overflows when adding multiple returns in OP_SETLIST. +*/ +#define MAX_CNST (INT_MAX/2) +#if MAX_CNST/(MAXARG_vC + 1) > MAXARG_Ax +#undef MAX_CNST +#define MAX_CNST (MAXARG_Ax * (MAXARG_vC + 1)) +#endif -static void recfield (LexState *ls, struct ConsControl *cc) { + +static void recfield (LexState *ls, ConsControl *cc) { /* recfield -> (NAME | '['exp']') = exp */ FuncState *fs = ls->fs; - int reg = ls->fs->freereg; + lu_byte reg = ls->fs->freereg; expdesc tab, key, val; - if (ls->t.token == TK_NAME) { - checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); + if (ls->t.token == TK_NAME) codename(ls, &key); - } else /* ls->t.token == '[' */ yindex(ls, &key); cc->nh++; @@ -730,18 +952,19 @@ static void recfield (LexState *ls, struct ConsControl *cc) { } -static void closelistfield (FuncState *fs, struct ConsControl *cc) { - if (cc->v.k == VVOID) return; /* there is no list item */ +static void closelistfield (FuncState *fs, ConsControl *cc) { + lua_assert(cc->tostore > 0); luaK_exp2nextreg(fs, &cc->v); cc->v.k = VVOID; - if (cc->tostore == LFIELDS_PER_FLUSH) { + if (cc->tostore >= cc->maxtostore) { luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ + cc->na += cc->tostore; cc->tostore = 0; /* no more items pending */ } } -static void lastlistfield (FuncState *fs, struct ConsControl *cc) { +static void lastlistfield (FuncState *fs, ConsControl *cc) { if (cc->tostore == 0) return; if (hasmultret(cc->v.k)) { luaK_setmultret(fs, &cc->v); @@ -753,19 +976,18 @@ static void lastlistfield (FuncState *fs, struct ConsControl *cc) { luaK_exp2nextreg(fs, &cc->v); luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); } + cc->na += cc->tostore; } -static void listfield (LexState *ls, struct ConsControl *cc) { +static void listfield (LexState *ls, ConsControl *cc) { /* listfield -> exp */ expr(ls, &cc->v); - checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); - cc->na++; cc->tostore++; } -static void field (LexState *ls, struct ConsControl *cc) { +static void field (LexState *ls, ConsControl *cc) { /* field -> listfield | recfield */ switch(ls->t.token) { case TK_NAME: { /* may be 'listfield' or 'recfield' */ @@ -787,68 +1009,94 @@ static void field (LexState *ls, struct ConsControl *cc) { } +/* +** Compute a limit for how many registers a constructor can use before +** emitting a 'SETLIST' instruction, based on how many registers are +** available. +*/ +static int maxtostore (FuncState *fs) { + int numfreeregs = MAX_FSTACK - fs->freereg; + if (numfreeregs >= 160) /* "lots" of registers? */ + return numfreeregs / 5; /* use up to 1/5 of them */ + else if (numfreeregs >= 80) /* still "enough" registers? */ + return 10; /* one 'SETLIST' instruction for each 10 values */ + else /* save registers for potential more nesting */ + return 1; +} + + static void constructor (LexState *ls, expdesc *t) { /* constructor -> '{' [ field { sep field } [sep] ] '}' sep -> ',' | ';' */ FuncState *fs = ls->fs; int line = ls->linenumber; - int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); - struct ConsControl cc; + int pc = luaK_codevABCk(fs, OP_NEWTABLE, 0, 0, 0, 0); + ConsControl cc; + luaK_code(fs, 0); /* space for extra arg. */ cc.na = cc.nh = cc.tostore = 0; cc.t = t; - init_exp(t, VRELOC, pc); + init_exp(t, VNONRELOC, fs->freereg); /* table will be at stack top */ + luaK_reserveregs(fs, 1); init_exp(&cc.v, VVOID, 0); /* no value (yet) */ - luaK_exp2nextreg(ls->fs, t); /* fix it at stack top */ - checknext(ls, '{'); + checknext(ls, '{' /*}*/); + cc.maxtostore = maxtostore(fs); do { - lua_assert(cc.v.k == VVOID || cc.tostore > 0); - if (ls->t.token == '}') break; - closelistfield(fs, &cc); + if (ls->t.token == /*{*/ '}') break; + if (cc.v.k != VVOID) /* is there a previous list item? */ + closelistfield(fs, &cc); /* close it */ field(ls, &cc); + luaY_checklimit(fs, cc.tostore + cc.na + cc.nh, MAX_CNST, + "items in a constructor"); } while (testnext(ls, ',') || testnext(ls, ';')); - check_match(ls, '}', '{', line); + check_match(ls, /*{*/ '}', '{' /*}*/, line); lastlistfield(fs, &cc); - SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ - SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ + luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh); } /* }====================================================================== */ -static void setvararg (FuncState *fs, int nparams) { - fs->f->is_vararg = 1; - luaK_codeABC(fs, OP_PREPVARARG, nparams, 0, 0); +static void setvararg (FuncState *fs) { + fs->f->flag |= PF_VAHID; /* by default, use hidden vararg arguments */ + luaK_codeABC(fs, OP_VARARGPREP, 0, 0, 0); } static void parlist (LexState *ls) { - /* parlist -> [ param { ',' param } ] */ + /* parlist -> [ {NAME ','} (NAME | '...') ] */ FuncState *fs = ls->fs; Proto *f = fs->f; int nparams = 0; - int isvararg = 0; + int varargk = 0; if (ls->t.token != ')') { /* is 'parlist' not empty? */ do { switch (ls->t.token) { - case TK_NAME: { /* param -> NAME */ + case TK_NAME: { new_localvar(ls, str_checkname(ls)); nparams++; break; } - case TK_DOTS: { /* param -> '...' */ - luaX_next(ls); - isvararg = 1; + case TK_DOTS: { + varargk = 1; + luaX_next(ls); /* skip '...' */ + if (ls->t.token == TK_NAME) + new_varkind(ls, str_checkname(ls), RDKVAVAR); + else + new_localvarliteral(ls, "(vararg table)"); break; } default: luaX_syntaxerror(ls, " or '...' expected"); } - } while (!isvararg && testnext(ls, ',')); + } while (!varargk && testnext(ls, ',')); } adjustlocalvars(ls, nparams); f->numparams = cast_byte(fs->nactvar); - if (isvararg) - setvararg(fs, f->numparams); /* declared vararg */ - luaK_reserveregs(fs, fs->nactvar); /* reserve registers for parameters */ + if (varargk) { + setvararg(fs); /* declared vararg */ + adjustlocalvars(ls, 1); /* vararg parameter */ + } + /* reserve registers for parameters (plus vararg parameter, if present) */ + luaK_reserveregs(fs, fs->nactvar); } @@ -887,10 +1135,11 @@ static int explist (LexState *ls, expdesc *v) { } -static void funcargs (LexState *ls, expdesc *f, int line) { +static void funcargs (LexState *ls, expdesc *f) { FuncState *fs = ls->fs; expdesc args; int base, nparams; + int line = ls->linenumber; switch (ls->t.token) { case '(': { /* funcargs -> '(' [ explist ] ')' */ luaX_next(ls); @@ -898,17 +1147,18 @@ static void funcargs (LexState *ls, expdesc *f, int line) { args.k = VVOID; else { explist(ls, &args); - luaK_setmultret(fs, &args); + if (hasmultret(args.k)) + luaK_setmultret(fs, &args); } check_match(ls, ')', '(', line); break; } - case '{': { /* funcargs -> constructor */ + case '{' /*}*/: { /* funcargs -> constructor */ constructor(ls, &args); break; } case TK_STRING: { /* funcargs -> STRING */ - codestring(ls, &args, ls->t.seminfo.ts); + codestring(&args, ls->t.seminfo.ts); luaX_next(ls); /* must use 'seminfo' before 'next' */ break; } @@ -927,8 +1177,9 @@ static void funcargs (LexState *ls, expdesc *f, int line) { } init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); luaK_fixline(fs, line); - fs->freereg = base+1; /* call remove function and arguments and leaves - (unless changed) one result */ + /* call removes function and arguments and leaves one result (unless + changed later) */ + fs->freereg = cast_byte(base + 1); } @@ -967,7 +1218,6 @@ static void suffixedexp (LexState *ls, expdesc *v) { /* suffixedexp -> primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */ FuncState *fs = ls->fs; - int line = ls->linenumber; primaryexp(ls, v); for (;;) { switch (ls->t.token) { @@ -987,12 +1237,12 @@ static void suffixedexp (LexState *ls, expdesc *v) { luaX_next(ls); codename(ls, &key); luaK_self(fs, v, &key); - funcargs(ls, v, line); + funcargs(ls, v); break; } - case '(': case TK_STRING: case '{': { /* funcargs */ + case '(': case TK_STRING: case '{' /*}*/: { /* funcargs */ luaK_exp2nextreg(fs, v); - funcargs(ls, v, line); + funcargs(ls, v); break; } default: return; @@ -1016,7 +1266,7 @@ static void simpleexp (LexState *ls, expdesc *v) { break; } case TK_STRING: { - codestring(ls, v, ls->t.seminfo.ts); + codestring(v, ls->t.seminfo.ts); break; } case TK_NIL: { @@ -1033,12 +1283,12 @@ static void simpleexp (LexState *ls, expdesc *v) { } case TK_DOTS: { /* vararg */ FuncState *fs = ls->fs; - check_condition(ls, fs->f->is_vararg, + check_condition(ls, isvararg(fs->f), "cannot use '...' outside a vararg function"); - init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1)); + init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, fs->f->numparams, 1)); break; } - case '{': { /* constructor */ + case '{' /*}*/: { /* constructor */ constructor(ls, v); return; } @@ -1194,7 +1444,7 @@ struct LHS_assign { */ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { FuncState *fs = ls->fs; - int extra = fs->freereg; /* eventual position to save local variable */ + lu_byte extra = fs->freereg; /* eventual position to save local variable */ int conflict = 0; for (; lh; lh = lh->prev) { /* check all previous assignments */ if (vkisindexed(lh->v.k)) { /* assignment to table field? */ @@ -1206,13 +1456,13 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { } } else { /* table is a register */ - if (v->k == VLOCAL && lh->v.u.ind.t == v->u.info) { + if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.ridx) { conflict = 1; /* table is the local being assigned now */ lh->v.u.ind.t = extra; /* assignment will use safe copy */ } /* is index the local being assigned? */ if (lh->v.k == VINDEXED && v->k == VLOCAL && - lh->v.u.ind.idx == v->u.info) { + lh->v.u.ind.idx == v->u.var.ridx) { conflict = 1; lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ } @@ -1221,14 +1471,25 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { } if (conflict) { /* copy upvalue/local value to a temporary (in position 'extra') */ - OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; - luaK_codeABC(fs, op, extra, v->u.info, 0); + if (v->k == VLOCAL) + luaK_codeABC(fs, OP_MOVE, extra, v->u.var.ridx, 0); + else + luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0); luaK_reserveregs(fs, 1); } } + +/* Create code to store the "top" register in 'var' */ +static void storevartop (FuncState *fs, expdesc *var) { + expdesc e; + init_exp(&e, VNONRELOC, fs->freereg - 1); + luaK_storevar(fs, var, &e); /* will also free the top register */ +} + + /* -** Parse and compile a mulitple assignment. The first "variable" +** Parse and compile a multiple assignment. The first "variable" ** (a 'suffixedexp') was already read by the caller. ** ** assignment -> suffixedexp restassign @@ -1237,6 +1498,7 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) { expdesc e; check_condition(ls, vkisvar(lh->v.k), "syntax error"); + check_readonly(ls, &lh->v); if (testnext(ls, ',')) { /* restassign -> ',' suffixedexp restassign */ struct LHS_assign nv; nv.prev = lh; @@ -1259,8 +1521,7 @@ static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) { return; /* avoid default */ } } - init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ - luaK_storevar(ls->fs, &lh->v, &e); + storevartop(ls->fs, &lh->v); /* default assignment */ } @@ -1274,44 +1535,38 @@ static int cond (LexState *ls) { } -static void gotostat (LexState *ls) { - FuncState *fs = ls->fs; - int line = ls->linenumber; +static void gotostat (LexState *ls, int line) { TString *name = str_checkname(ls); /* label's name */ - Labeldesc *lb = findlabel(ls, name); - if (lb == NULL) /* no label? */ - /* forward jump; will be resolved when the label is declared */ - newgotoentry(ls, name, line, luaK_jump(fs)); - else { /* found a label */ - /* backward jump; will be resolved here */ - if (fs->nactvar > lb->nactvar) /* leaving the scope of some variable? */ - luaK_codeABC(fs, OP_CLOSE, lb->nactvar, 0, 0); - /* create jump and link it to the label */ - luaK_patchlist(fs, luaK_jump(fs), lb->pc); - } + newgotoentry(ls, name, line); } /* ** Break statement. Semantically equivalent to "goto break". */ -static void breakstat (LexState *ls) { - int line = ls->linenumber; +static void breakstat (LexState *ls, int line) { + BlockCnt *bl; /* to look for an enclosing loop */ + for (bl = ls->fs->bl; bl != NULL; bl = bl->previous) { + if (bl->isloop) /* found one? */ + goto ok; + } + luaX_syntaxerror(ls, "break outside loop"); + ok: + bl->isloop = 2; /* signal that block has pending breaks */ luaX_next(ls); /* skip break */ - newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, luaK_jump(ls->fs)); + newgotoentry(ls, ls->brkn, line); } /* -** Check whether there is already a label with the given 'name'. +** Check whether there is already a label with the given 'name' at +** current function. */ static void checkrepeated (LexState *ls, TString *name) { - Labeldesc *lb = findlabel(ls, name); - if (unlikely(lb != NULL)) { /* already defined? */ - const char *msg = "label '%s' already defined on line %d"; - msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); - luaK_semerror(ls, msg); /* error */ - } + Labeldesc *lb = findlabel(ls, name, ls->fs->firstlabel); + if (l_unlikely(lb != NULL)) /* already defined? */ + luaK_semerror(ls, "label '%s' already defined on line %d", + getstr(name), lb->line); /* error */ } @@ -1360,7 +1615,7 @@ static void repeatstat (LexState *ls, int line) { if (bl2.upval) { /* upvalues? */ int exit = luaK_jump(fs); /* normal exit must jump over fix */ luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ - luaK_codeABC(fs, OP_CLOSE, bl2.nactvar, 0, 0); + luaK_codeABC(fs, OP_CLOSE, reglevel(fs, bl2.nactvar), 0, 0); condexit = luaK_jump(fs); /* repeat after closing upvalues */ luaK_patchtohere(fs, exit); /* normal exit comes to here */ } @@ -1371,18 +1626,14 @@ static void repeatstat (LexState *ls, int line) { /* ** Read an expression and generate code to put its results in next -** stack slot. Return true if expression is a constant integer and, -** if 'i' is not-zero, its value is equal to 'i'. +** stack slot. ** */ -static int exp1 (LexState *ls, int i) { +static void exp1 (LexState *ls) { expdesc e; - int res; expr(ls, &e); - res = luaK_isKint(&e) && (i == 0 || i == e.u.ival); luaK_exp2nextreg(ls->fs, &e); lua_assert(e.k == VNONRELOC); - return res; } @@ -1396,38 +1647,36 @@ static void fixforjump (FuncState *fs, int pc, int dest, int back) { int offset = dest - (pc + 1); if (back) offset = -offset; - if (unlikely(offset > MAXARG_Bx)) + if (l_unlikely(offset > MAXARG_Bx)) luaX_syntaxerror(fs->ls, "control structure too long"); SETARG_Bx(*jmp, offset); } /* -** Generate code for a 'for' loop. 'kind' can be zero (a common for -** loop), one (a basic for loop, with integer values and increment of -** 1), or two (a generic for loop). +** Generate code for a 'for' loop. */ -static void forbody (LexState *ls, int base, int line, int nvars, int kind) { +static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { /* forbody -> DO block */ - static OpCode forprep[3] = {OP_FORPREP, OP_FORPREP1, OP_TFORPREP}; - static OpCode forloop[3] = {OP_FORLOOP, OP_FORLOOP1, OP_TFORLOOP}; + static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP}; + static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP}; BlockCnt bl; FuncState *fs = ls->fs; int prep, endfor; checknext(ls, TK_DO); - prep = luaK_codeABx(fs, forprep[kind], base, 0); + prep = luaK_codeABx(fs, forprep[isgen], base, 0); + fs->freereg--; /* both 'forprep' remove one register from the stack */ enterblock(fs, &bl, 0); /* scope for declared variables */ adjustlocalvars(ls, nvars); luaK_reserveregs(fs, nvars); block(ls); leaveblock(fs); /* end of scope for declared variables */ fixforjump(fs, prep, luaK_getlabel(fs), 0); - if (kind == 2) { /* generic for? */ + if (isgen) { /* generic for? */ luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); luaK_fixline(fs, line); - base += 2; /* base for 'OP_TFORLOOP' (skips function and state) */ } - endfor = luaK_codeABx(fs, forloop[kind], base, 0); + endfor = luaK_codeABx(fs, forloop[isgen], base, 0); fixforjump(fs, endfor, prep + 1, 1); luaK_fixline(fs, line); } @@ -1437,26 +1686,21 @@ static void fornum (LexState *ls, TString *varname, int line) { /* fornum -> NAME = exp,exp[,exp] forbody */ FuncState *fs = ls->fs; int base = fs->freereg; - int basicfor = 1; /* true if it is a "basic" 'for' (integer + 1) */ - new_localvarliteral(ls, "(for index)"); - new_localvarliteral(ls, "(for limit)"); - new_localvarliteral(ls, "(for step)"); - new_localvar(ls, varname); + new_localvarliteral(ls, "(for state)"); + new_localvarliteral(ls, "(for state)"); + new_varkind(ls, varname, RDKCONST); /* control variable */ checknext(ls, '='); - if (!exp1(ls, 0)) /* initial value not an integer? */ - basicfor = 0; /* not a basic 'for' */ + exp1(ls); /* initial value */ checknext(ls, ','); - exp1(ls, 0); /* limit */ - if (testnext(ls, ',')) { - if (!exp1(ls, 1)) /* optional step not 1? */ - basicfor = 0; /* not a basic 'for' */ - } + exp1(ls); /* limit */ + if (testnext(ls, ',')) + exp1(ls); /* optional step */ else { /* default step = 1 */ luaK_int(fs, fs->freereg, 1); luaK_reserveregs(fs, 1); } - adjustlocalvars(ls, 3); /* control variables */ - forbody(ls, base, line, 1, basicfor); + adjustlocalvars(ls, 2); /* start scope for internal variables */ + forbody(ls, base, line, 1, 0); } @@ -1464,17 +1708,15 @@ static void forlist (LexState *ls, TString *indexname) { /* forlist -> NAME {,NAME} IN explist forbody */ FuncState *fs = ls->fs; expdesc e; - int nvars = 5; /* gen, state, control, toclose, 'indexname' */ + int nvars = 4; /* function, state, closing, control */ int line; int base = fs->freereg; - /* create control variables */ - new_localvarliteral(ls, "(for generator)"); - new_localvarliteral(ls, "(for state)"); - markupval(fs, fs->nactvar); /* state may create an upvalue */ - new_localvarliteral(ls, "(for control)"); - new_localvarliteral(ls, "(for toclose)"); - /* create declared variables */ - new_localvar(ls, indexname); + /* create internal variables */ + new_localvarliteral(ls, "(for state)"); /* iterator function */ + new_localvarliteral(ls, "(for state)"); /* state */ + new_localvarliteral(ls, "(for state)"); /* closing var. (after swap) */ + new_varkind(ls, indexname, RDKCONST); /* control variable */ + /* other declared variables */ while (testnext(ls, ',')) { new_localvar(ls, str_checkname(ls)); nvars++; @@ -1482,9 +1724,10 @@ static void forlist (LexState *ls, TString *indexname) { checknext(ls, TK_IN); line = ls->linenumber; adjust_assign(ls, 4, explist(ls, &e), &e); - adjustlocalvars(ls, 4); /* control variables */ - luaK_checkstack(fs, 3); /* extra space to call generator */ - forbody(ls, base, line, nvars - 4, 2); + adjustlocalvars(ls, 3); /* start scope for internal variables */ + marktobeclosed(fs); /* last internal var. must be closed */ + luaK_checkstack(fs, 2); /* extra space to call iterator */ + forbody(ls, base, line, nvars - 3, 1); } @@ -1506,77 +1749,18 @@ static void forstat (LexState *ls, int line) { } -/* -** Check whether next instruction is a single jump (a 'break', a 'goto' -** to a forward label, or a 'goto' to a backward label with no variable -** to close). If so, set the name of the 'label' it is jumping to -** ("break" for a 'break') or to where it is jumping to ('target') and -** return true. If not a single jump, leave input unchanged, to be -** handled as a regular statement. -*/ -static int issinglejump (LexState *ls, TString **label, int *target) { - if (testnext(ls, TK_BREAK)) { /* a break? */ - *label = luaS_newliteral(ls->L, "break"); - return 1; - } - else if (ls->t.token != TK_GOTO || luaX_lookahead(ls) != TK_NAME) - return 0; /* not a valid goto */ - else { - TString *lname = ls->lookahead.seminfo.ts; /* label's id */ - Labeldesc *lb = findlabel(ls, lname); - if (lb) { /* a backward jump? */ - if (ls->fs->nactvar > lb->nactvar) /* needs to close variables? */ - return 0; /* not a single jump; cannot optimize */ - *target = lb->pc; - } - else /* jump forward */ - *label = lname; - luaX_next(ls); /* skip goto */ - luaX_next(ls); /* skip name */ - return 1; - } -} - - static void test_then_block (LexState *ls, int *escapelist) { /* test_then_block -> [IF | ELSEIF] cond THEN block */ - BlockCnt bl; - int line; FuncState *fs = ls->fs; - TString *jlb = NULL; - int target = NO_JUMP; - expdesc v; - int jf; /* instruction to skip 'then' code (if condition is false) */ + int condtrue; luaX_next(ls); /* skip IF or ELSEIF */ - expr(ls, &v); /* read condition */ + condtrue = cond(ls); /* read condition */ checknext(ls, TK_THEN); - line = ls->linenumber; - if (issinglejump(ls, &jlb, &target)) { /* 'if x then goto' ? */ - luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ - enterblock(fs, &bl, 0); /* must enter block before 'goto' */ - if (jlb != NULL) /* forward jump? */ - newgotoentry(ls, jlb, line, v.t); /* will be resolved later */ - else /* backward jump */ - luaK_patchlist(fs, v.t, target); /* jump directly to 'target' */ - while (testnext(ls, ';')) {} /* skip semicolons */ - if (block_follow(ls, 0)) { /* jump is the entire block? */ - leaveblock(fs); - return; /* and that is it */ - } - else /* must skip over 'then' part if condition is false */ - jf = luaK_jump(fs); - } - else { /* regular case (not a jump) */ - luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ - enterblock(fs, &bl, 0); - jf = v.f; - } - statlist(ls); /* 'then' part */ - leaveblock(fs); + block(ls); /* 'then' part */ if (ls->t.token == TK_ELSE || ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */ - luaK_patchtohere(fs, jf); + luaK_patchtohere(fs, condtrue); } @@ -1597,57 +1781,191 @@ static void ifstat (LexState *ls, int line) { static void localfunc (LexState *ls) { expdesc b; FuncState *fs = ls->fs; + int fvar = fs->nactvar; /* function's variable index */ new_localvar(ls, str_checkname(ls)); /* new local variable */ adjustlocalvars(ls, 1); /* enter its scope */ body(ls, &b, 0, ls->linenumber); /* function created in next register */ /* debug information will only see the variable after this point! */ - getlocvar(fs, b.u.info)->startpc = fs->pc; + localdebuginfo(fs, fvar)->startpc = fs->pc; +} + + +static lu_byte getvarattribute (LexState *ls, lu_byte df) { + /* attrib -> ['<' NAME '>'] */ + if (testnext(ls, '<')) { + TString *ts = str_checkname(ls); + const char *attr = getstr(ts); + checknext(ls, '>'); + if (strcmp(attr, "const") == 0) + return RDKCONST; /* read-only variable */ + else if (strcmp(attr, "close") == 0) + return RDKTOCLOSE; /* to-be-closed variable */ + else + luaK_semerror(ls, "unknown attribute '%s'", attr); + } + return df; /* return default value */ } -static void commonlocalstat (LexState *ls) { - /* stat -> LOCAL NAME {',' NAME} ['=' explist] */ +static void checktoclose (FuncState *fs, int level) { + if (level != -1) { /* is there a to-be-closed variable? */ + marktobeclosed(fs); + luaK_codeABC(fs, OP_TBC, reglevel(fs, level), 0, 0); + } +} + + +static void localstat (LexState *ls) { + /* stat -> LOCAL NAME attrib { ',' NAME attrib } ['=' explist] */ + FuncState *fs = ls->fs; + int toclose = -1; /* index of to-be-closed variable (if any) */ + Vardesc *var; /* last variable */ + int vidx; /* index of last variable */ int nvars = 0; int nexps; expdesc e; - do { - new_localvar(ls, str_checkname(ls)); + /* get prefixed attribute (if any); default is regular local variable */ + lu_byte defkind = getvarattribute(ls, VDKREG); + do { /* for each variable */ + TString *vname = str_checkname(ls); /* get its name */ + lu_byte kind = getvarattribute(ls, defkind); /* postfixed attribute */ + vidx = new_varkind(ls, vname, kind); /* predeclare it */ + if (kind == RDKTOCLOSE) { /* to-be-closed? */ + if (toclose != -1) /* one already present? */ + luaK_semerror(ls, "multiple to-be-closed variables in local list"); + toclose = fs->nactvar + nvars; + } nvars++; } while (testnext(ls, ',')); - if (testnext(ls, '=')) + if (testnext(ls, '=')) /* initialization? */ nexps = explist(ls, &e); else { e.k = VVOID; nexps = 0; } - adjust_assign(ls, nvars, nexps, &e); - adjustlocalvars(ls, nvars); + var = getlocalvardesc(fs, vidx); /* retrieve last variable */ + if (nvars == nexps && /* no adjustments? */ + var->vd.kind == RDKCONST && /* last variable is const? */ + luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ + var->vd.kind = RDKCTC; /* variable is a compile-time constant */ + adjustlocalvars(ls, nvars - 1); /* exclude last variable */ + fs->nactvar++; /* but count it */ + } + else { + adjust_assign(ls, nvars, nexps, &e); + adjustlocalvars(ls, nvars); + } + checktoclose(fs, toclose); +} + + +static lu_byte getglobalattribute (LexState *ls, lu_byte df) { + lu_byte kind = getvarattribute(ls, df); + switch (kind) { + case RDKTOCLOSE: + luaK_semerror(ls, "global variables cannot be to-be-closed"); + return kind; /* to avoid warnings */ + case RDKCONST: + return GDKCONST; /* adjust kind for global variable */ + default: + return kind; + } } -static void tocloselocalstat (LexState *ls) { +static void checkglobal (LexState *ls, TString *varname, int line) { FuncState *fs = ls->fs; - TString *attr = str_checkname(ls); - if (strcmp(getstr(attr), "toclose") != 0) - luaK_semerror(ls, - luaO_pushfstring(ls->L, "unknown attribute '%s'", getstr(attr))); - new_localvar(ls, str_checkname(ls)); - checknext(ls, '='); - exp1(ls, 0); - markupval(fs, fs->nactvar); - fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ - adjustlocalvars(ls, 1); - luaK_codeABC(fs, OP_TBC, fs->nactvar - 1, 0, 0); + expdesc var; + int k; + buildglobal(ls, varname, &var); /* create global variable in 'var' */ + k = var.u.ind.keystr; /* index of global name in 'k' */ + luaK_codecheckglobal(fs, &var, k, line); } -static void localstat (LexState *ls) { - /* stat -> LOCAL NAME {',' NAME} ['=' explist] - | LOCAL *toclose NAME '=' exp */ - if (testnext(ls, '*')) - tocloselocalstat(ls); +/* +** Recursively traverse list of globals to be initalized. When +** going, generate table description for the global. In the end, +** after all indices have been generated, read list of initializing +** expressions. When returning, generate the assignment of the value on +** the stack to the corresponding table description. 'n' is the variable +** being handled, range [0, nvars - 1]. +*/ +static void initglobal (LexState *ls, int nvars, int firstidx, int n, + int line) { + if (n == nvars) { /* traversed all variables? */ + expdesc e; + int nexps = explist(ls, &e); /* read list of expressions */ + adjust_assign(ls, nvars, nexps, &e); + } + else { /* handle variable 'n' */ + FuncState *fs = ls->fs; + expdesc var; + TString *varname = getlocalvardesc(fs, firstidx + n)->vd.name; + buildglobal(ls, varname, &var); /* create global variable in 'var' */ + enterlevel(ls); /* control recursion depth */ + initglobal(ls, nvars, firstidx, n + 1, line); + leavelevel(ls); + checkglobal(ls, varname, line); + storevartop(fs, &var); + } +} + + +static void globalnames (LexState *ls, lu_byte defkind) { + FuncState *fs = ls->fs; + int nvars = 0; + int lastidx; /* index of last registered variable */ + do { /* for each name */ + TString *vname = str_checkname(ls); + lu_byte kind = getglobalattribute(ls, defkind); + lastidx = new_varkind(ls, vname, kind); + nvars++; + } while (testnext(ls, ',')); + if (testnext(ls, '=')) /* initialization? */ + initglobal(ls, nvars, lastidx - nvars + 1, 0, ls->linenumber); + fs->nactvar = cast_short(fs->nactvar + nvars); /* activate declaration */ +} + + +static void globalstat (LexState *ls) { + /* globalstat -> (GLOBAL) attrib '*' + globalstat -> (GLOBAL) attrib NAME attrib {',' NAME attrib} */ + FuncState *fs = ls->fs; + /* get prefixed attribute (if any); default is regular global variable */ + lu_byte defkind = getglobalattribute(ls, GDKREG); + if (!testnext(ls, '*')) + globalnames(ls, defkind); + else { + /* use NULL as name to represent '*' entries */ + new_varkind(ls, NULL, defkind); + fs->nactvar++; /* activate declaration */ + } +} + + +static void globalfunc (LexState *ls, int line) { + /* globalfunc -> (GLOBAL FUNCTION) NAME body */ + expdesc var, b; + FuncState *fs = ls->fs; + TString *fname = str_checkname(ls); + new_varkind(ls, fname, GDKREG); /* declare global variable */ + fs->nactvar++; /* enter its scope */ + buildglobal(ls, fname, &var); + body(ls, &b, 0, ls->linenumber); /* compile and return closure in 'b' */ + checkglobal(ls, fname, line); + luaK_storevar(fs, &var, &b); + luaK_fixline(fs, line); /* definition "happens" in the first line */ +} + + +static void globalstatfunc (LexState *ls, int line) { + /* stat -> GLOBAL globalfunc | GLOBAL globalstat */ + luaX_next(ls); /* skip 'global' */ + if (testnext(ls, TK_FUNCTION)) + globalfunc(ls, line); else - commonlocalstat(ls); + globalstat(ls); } @@ -1671,6 +1989,7 @@ static void funcstat (LexState *ls, int line) { expdesc v, b; luaX_next(ls); /* skip FUNCTION */ ismethod = funcname(ls, &v); + check_readonly(ls, &v); body(ls, &b, ismethod, line); luaK_storevar(ls->fs, &v, &b); luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ @@ -1687,8 +2006,9 @@ static void exprstat (LexState *ls) { restassign(ls, &v, 1); } else { /* stat -> func */ - Instruction *inst = &getinstruction(fs, &v.v); + Instruction *inst; check_condition(ls, v.v.k == VCALL, "syntax error"); + inst = &getinstruction(fs, &v.v); SETARG_C(*inst, 1); /* call statement uses no results */ } } @@ -1699,7 +2019,7 @@ static void retstat (LexState *ls) { FuncState *fs = ls->fs; expdesc e; int nret; /* number of values being returned */ - int first = fs->nactvar; /* first slot to be returned */ + int first = luaY_nvarstack(fs); /* first slot to be returned */ if (block_follow(ls, 1) || ls->t.token == ';') nret = 0; /* return no values */ else { @@ -1708,7 +2028,7 @@ static void retstat (LexState *ls) { luaK_setmultret(fs, &e); if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) { /* tail call? */ SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); - lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar); + lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs)); } nret = LUA_MULTRET; /* return all values */ } @@ -1768,6 +2088,10 @@ static void statement (LexState *ls) { localstat(ls); break; } + case TK_GLOBAL: { /* stat -> globalstatfunc */ + globalstatfunc(ls, line); + break; + } case TK_DBCOLON: { /* stat -> label */ luaX_next(ls); /* skip double colon */ labelstat(ls, str_checkname(ls), line); @@ -1779,27 +2103,45 @@ static void statement (LexState *ls) { break; } case TK_BREAK: { /* stat -> breakstat */ - breakstat(ls); + breakstat(ls, line); break; } case TK_GOTO: { /* stat -> 'goto' NAME */ luaX_next(ls); /* skip 'goto' */ - gotostat(ls); + gotostat(ls, line); break; } +#if defined(LUA_COMPAT_GLOBAL) + case TK_NAME: { + /* compatibility code to parse global keyword when "global" + is not reserved */ + if (ls->t.seminfo.ts == ls->glbn) { /* current = "global"? */ + int lk = luaX_lookahead(ls); + if (lk == '<' || lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) { + /* 'global ' or 'global name' or 'global *' or + 'global function' */ + globalstatfunc(ls, line); + break; + } + } /* else... */ + } +#endif + /* FALLTHROUGH */ default: { /* stat -> func | assignment */ exprstat(ls); break; } } lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && - ls->fs->freereg >= ls->fs->nactvar); - ls->fs->freereg = ls->fs->nactvar; /* free registers */ + ls->fs->freereg >= luaY_nvarstack(ls->fs)); + ls->fs->freereg = luaY_nvarstack(ls->fs); /* free registers */ leavelevel(ls); } /* }====================================================================== */ +/* }====================================================================== */ + /* ** compiles the main function, which is a regular vararg function with an @@ -1807,11 +2149,15 @@ static void statement (LexState *ls) { */ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; - expdesc v; + Upvaldesc *env; open_func(ls, fs, &bl); - setvararg(fs, 0); /* main function is always declared vararg */ - init_exp(&v, VLOCAL, 0); /* create and... */ - newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ + setvararg(fs); /* main function is always vararg */ + env = allocupvalue(fs); /* ...set environment upvalue */ + env->instack = 1; + env->idx = 0; + env->kind = VDKREG; + env->name = ls->envn; + luaC_objbarrier(ls->L, fs->f, env->name); luaX_next(ls); /* read first token */ statlist(ls); /* parse main body */ check(ls, TK_EOS); @@ -1824,12 +2170,13 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, LexState lexstate; FuncState funcstate; LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ - setclLvalue2s(L, L->top, cl); /* anchor it (to avoid being collected) */ + setclLvalue2s(L, L->top.p, cl); /* anchor it (to avoid being collected) */ luaD_inctop(L); lexstate.h = luaH_new(L); /* create table for scanner */ - sethvalue2s(L, L->top, lexstate.h); /* anchor it */ + sethvalue2s(L, L->top.p, lexstate.h); /* anchor it */ luaD_inctop(L); funcstate.f = cl->p = luaF_newproto(L); + luaC_objbarrier(L, cl, cl->p); funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ luaC_objbarrier(L, funcstate.f, funcstate.f->source); lexstate.buff = buff; @@ -1840,7 +2187,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); /* all scopes should be correctly finished */ lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); - L->top--; /* remove scanner's table */ + L->top.p--; /* remove scanner's table */ return cl; /* closure is on the stack, too */ } diff --git a/lparser.h b/lparser.h index 3d6bd97853..a30df04f77 100644 --- a/lparser.h +++ b/lparser.h @@ -23,30 +23,45 @@ /* kinds of variables/expressions */ typedef enum { - VVOID, /* when 'expdesc' describes the last expression a list, + VVOID, /* when 'expdesc' describes the last expression of a list, this kind means an empty list (so, no expression) */ VNIL, /* constant nil */ VTRUE, /* constant true */ VFALSE, /* constant false */ VK, /* constant in 'k'; info = index of constant in 'k' */ VKFLT, /* floating constant; nval = numerical float value */ - VKINT, /* integer constant; nval = numerical integer value */ + VKINT, /* integer constant; ival = numerical integer value */ + VKSTR, /* string constant; strval = TString address; + (string is fixed by the scanner) */ VNONRELOC, /* expression has its value in a fixed register; info = result register */ - VLOCAL, /* local variable; info = local register */ + VLOCAL, /* local variable; var.ridx = register index; + var.vidx = relative index in 'actvar.arr' */ + VVARGVAR, /* vararg parameter; var.ridx = register index; + var.vidx = relative index in 'actvar.arr' */ + VGLOBAL, /* global variable; + info = relative index in 'actvar.arr' (or -1 for + implicit declaration) */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ + VCONST, /* compile-time variable; + info = absolute index in 'actvar.arr' */ VINDEXED, /* indexed variable; ind.t = table register; - ind.idx = key's R index */ + ind.idx = key's R index; + ind.ro = true if it represents a read-only global; + ind.keystr = if key is a string, index in 'k' of that string; + -1 if key is not a string */ + VVARGIND, /* indexed vararg parameter; + ind.* as in VINDEXED */ VINDEXUP, /* indexed upvalue; - ind.t = table upvalue; - ind.idx = key's K index */ + ind.idx = key's K index; + ind.* as in VINDEXED */ VINDEXI, /* indexed variable with constant integer; ind.t = table register; ind.idx = key's value */ VINDEXSTR, /* indexed variable with literal string; - ind.t = table register; - ind.idx = key's K index */ + ind.idx = key's K index; + ind.* as in VINDEXED */ VJMP, /* expression is a test/comparison; info = pc of corresponding jump instruction */ VRELOC, /* expression can put result in any register; @@ -58,37 +73,68 @@ typedef enum { #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR) #define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR) -#define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) + typedef struct expdesc { expkind k; union { lua_Integer ival; /* for VKINT */ lua_Number nval; /* for VKFLT */ + TString *strval; /* for VKSTR */ int info; /* for generic use */ struct { /* for indexed variables */ short idx; /* index (R or "long" K) */ lu_byte t; /* table (register or upvalue) */ + lu_byte ro; /* true if variable is read-only */ + int keystr; /* index in 'k' of string key, or -1 if not a string */ } ind; + struct { /* for local variables */ + lu_byte ridx; /* register holding the variable */ + short vidx; /* index in 'actvar.arr' */ + } var; } u; int t; /* patch list of 'exit when true' */ int f; /* patch list of 'exit when false' */ } expdesc; -/* description of active local variable */ -typedef struct Vardesc { - short idx; /* index of the variable in the Proto's 'locvars' array */ +/* kinds of variables */ +#define VDKREG 0 /* regular local */ +#define RDKCONST 1 /* local constant */ +#define RDKVAVAR 2 /* vararg parameter */ +#define RDKTOCLOSE 3 /* to-be-closed */ +#define RDKCTC 4 /* local compile-time constant */ +#define GDKREG 5 /* regular global */ +#define GDKCONST 6 /* global constant */ + +/* variables that live in registers */ +#define varinreg(v) ((v)->vd.kind <= RDKTOCLOSE) + +/* test for global variables */ +#define varglobal(v) ((v)->vd.kind >= GDKREG) + + +/* description of an active variable */ +typedef union Vardesc { + struct { + TValuefields; /* constant value (if it is a compile-time constant) */ + lu_byte kind; + lu_byte ridx; /* register holding the variable */ + short pidx; /* index of the variable in the Proto's 'locvars' array */ + TString *name; /* variable name */ + } vd; + TValue k; /* constant value (if any) */ } Vardesc; + /* description of pending goto statements and label statements */ typedef struct Labeldesc { TString *name; /* label identifier */ int pc; /* position in code */ int line; /* line where it appeared */ - lu_byte nactvar; /* local level where it appears in current block */ - lu_byte close; /* goto that escapes upvalues */ + short nactvar; /* number of active variables in that position */ + lu_byte close; /* true for goto that escapes upvalues */ } Labeldesc; @@ -102,7 +148,7 @@ typedef struct Labellist { /* dynamic structures used by the parser */ typedef struct Dyndata { - struct { /* list of active local variables */ + struct { /* list of all active local variables */ Vardesc *arr; int n; int size; @@ -122,6 +168,7 @@ typedef struct FuncState { struct FuncState *prev; /* enclosing function */ struct LexState *ls; /* lexical state */ struct BlockCnt *bl; /* chain of current blocks */ + Table *kcache; /* cache for reusing constants */ int pc; /* next position to code (equivalent to 'ncode') */ int lasttarget; /* 'label' of last 'jump label' */ int previousline; /* last line that was saved in 'lineinfo' */ @@ -130,8 +177,8 @@ typedef struct FuncState { int nabslineinfo; /* number of elements in 'abslineinfo' */ int firstlocal; /* index of first local var (in Dyndata array) */ int firstlabel; /* index of first label (in 'dyd->label->arr') */ - short nlocvars; /* number of elements in 'f->locvars' */ - lu_byte nactvar; /* number of active local variables */ + short ndebugvars; /* number of elements in 'f->locvars' */ + short nactvar; /* number of active variable declarations */ lu_byte nups; /* number of upvalues */ lu_byte freereg; /* first free register */ lu_byte iwthabs; /* instructions issued since last absolute line info */ @@ -139,6 +186,9 @@ typedef struct FuncState { } FuncState; +LUAI_FUNC lu_byte luaY_nvarstack (FuncState *fs); +LUAI_FUNC void luaY_checklimit (FuncState *fs, int v, int l, + const char *what); LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Dyndata *dyd, const char *name, int firstchar); diff --git a/lprefix.h b/lprefix.h index dd14767ed2..484f2ad6fb 100644 --- a/lprefix.h +++ b/lprefix.h @@ -33,7 +33,7 @@ /* ** Windows stuff */ -#if defined(_WIN32) /* { */ +#if defined(_WIN32) /* { */ #if !defined(_CRT_SECURE_NO_WARNINGS) #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ diff --git a/lstate.c b/lstate.c index f5579a6613..70a11aaec6 100644 --- a/lstate.c +++ b/lstate.c @@ -29,112 +29,48 @@ -/* -** thread state + extra space -*/ -typedef struct LX { - lu_byte extra_[LUA_EXTRASPACE]; - lua_State l; -} LX; - - -/* -** Main thread combines a thread state and the global state -*/ -typedef struct LG { - LX l; - global_State g; -} LG; - - - #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) /* -** A macro to create a "random" seed when a state is created; -** the seed is used to randomize string hashes. +** these macros allow user-specific actions when a thread is +** created/deleted */ -#if !defined(luai_makeseed) +#if !defined(luai_userstateopen) +#define luai_userstateopen(L) ((void)L) +#endif -#include +#if !defined(luai_userstateclose) +#define luai_userstateclose(L) ((void)L) +#endif -/* -** Compute an initial seed with some level of randomness. -** Rely on Address Space Layout Randomization (if present) and -** current time. -*/ -#define addbuff(b,p,e) \ - { size_t t = cast_sizet(e); \ - memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } - -static unsigned int luai_makeseed (lua_State *L) { - char buff[3 * sizeof(size_t)]; - unsigned int h = cast_uint(time(NULL)); - int p = 0; - addbuff(buff, p, L); /* heap variable */ - addbuff(buff, p, &h); /* local variable */ - addbuff(buff, p, &lua_newstate); /* public function */ - lua_assert(p == sizeof(buff)); - return luaS_hash(buff, p, h); -} +#if !defined(luai_userstatethread) +#define luai_userstatethread(L,L1) ((void)L) +#endif +#if !defined(luai_userstatefree) +#define luai_userstatefree(L,L1) ((void)L) #endif /* -** set GCdebt to a new value keeping the value (totalbytes + GCdebt) -** invariant (and avoiding underflows in 'totalbytes') +** set GCdebt to a new value keeping the real number of allocated +** objects (GCtotalobjs - GCdebt) invariant and avoiding overflows in +** 'GCtotalobjs'. */ void luaE_setdebt (global_State *g, l_mem debt) { l_mem tb = gettotalbytes(g); lua_assert(tb > 0); - if (debt < tb - MAX_LMEM) - debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */ - g->totalbytes = tb - debt; + if (debt > MAX_LMEM - tb) + debt = MAX_LMEM - tb; /* will make GCtotalbytes == MAX_LMEM */ + g->GCtotalbytes = tb + debt; g->GCdebt = debt; } -/* -** Increment count of "C calls" and check for overflows. In case of -** a stack overflow, check appropriate error ("regular" overflow or -** overflow while handling stack overflow). -** If 'nCcalls' is larger than LUAI_MAXCCALLS but smaller than -** LUAI_MAXCCALLS + CSTACKCF (plus 2 to avoid by-one errors), it means -** it has just entered the "overflow zone", so the function raises an -** overflow error. -** If 'nCcalls' is larger than LUAI_MAXCCALLS + CSTACKCF + 2 -** (which means it is already handling an overflow) but smaller than -** 9/8 of LUAI_MAXCCALLS, does not report an error (to allow message -** handling to work). -** Otherwise, report a stack overflow while handling a stack overflow -** (probably caused by a repeating error in the message handling -** function). -*/ -void luaE_enterCcall (lua_State *L) { - int ncalls = getCcalls(L); - L->nCcalls++; - if (ncalls >= LUAI_MAXCCALLS) { /* possible overflow? */ - luaE_freeCI(L); /* release unused CIs */ - ncalls = getCcalls(L); /* update call count */ - if (ncalls >= LUAI_MAXCCALLS) { /* still overflow? */ - if (ncalls <= LUAI_MAXCCALLS + CSTACKCF + 2) { - /* no error before increments; raise the error now */ - L->nCcalls += (CSTACKCF + 4); /* avoid raising it again */ - luaG_runerror(L, "C stack overflow"); - } - else if (ncalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS >> 3))) - luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ - } - } -} - - CallInfo *luaE_extendCI (lua_State *L) { CallInfo *ci; lua_assert(L->ci->next == NULL); - luaE_enterCcall(L); ci = luaM_new(L, CallInfo); lua_assert(L->ci->next == NULL); L->ci->next = ci; @@ -149,67 +85,98 @@ CallInfo *luaE_extendCI (lua_State *L) { /* ** free all CallInfo structures not in use by a thread */ -void luaE_freeCI (lua_State *L) { +static void freeCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next = ci->next; ci->next = NULL; - L->nCcalls -= L->nci; /* subtract removed elements from 'nCcalls' */ while ((ci = next) != NULL) { next = ci->next; luaM_free(L, ci); L->nci--; } - L->nCcalls += L->nci; /* adjust result */ } /* -** free half of the CallInfo structures not in use by a thread +** free half of the CallInfo structures not in use by a thread, +** keeping the first one. */ void luaE_shrinkCI (lua_State *L) { - CallInfo *ci = L->ci; - CallInfo *next2; /* next's next */ - L->nCcalls -= L->nci; /* subtract removed elements from 'nCcalls' */ - /* while there are two nexts */ - while (ci->next != NULL && (next2 = ci->next->next) != NULL) { - luaM_free(L, ci->next); /* free next */ + CallInfo *ci = L->ci->next; /* first free CallInfo */ + CallInfo *next; + if (ci == NULL) + return; /* no extra elements */ + while ((next = ci->next) != NULL) { /* two extra elements? */ + CallInfo *next2 = next->next; /* next's next */ + ci->next = next2; /* remove next from the list */ L->nci--; - ci->next = next2; /* remove 'next' from the list */ - next2->previous = ci; - ci = next2; /* keep next's next */ + luaM_free(L, next); /* free next */ + if (next2 == NULL) + break; /* no more elements */ + else { + next2->previous = ci; + ci = next2; /* continue */ + } } - L->nCcalls += L->nci; /* adjust result */ +} + + +/* +** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS. +** If equal, raises an overflow error. If value is larger than +** LUAI_MAXCCALLS (which means it is handling an overflow) but +** not much larger, does not report an error (to allow overflow +** handling to work). +*/ +void luaE_checkcstack (lua_State *L) { + if (getCcalls(L) == LUAI_MAXCCALLS) + luaG_runerror(L, "C stack overflow"); + else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) + luaD_errerr(L); /* error while handling stack error */ +} + + +LUAI_FUNC void luaE_incCstack (lua_State *L) { + L->nCcalls++; + if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) + luaE_checkcstack(L); +} + + +static void resetCI (lua_State *L) { + CallInfo *ci = L->ci = &L->base_ci; + ci->func.p = L->stack.p; + setnilvalue(s2v(ci->func.p)); /* 'function' entry for basic 'ci' */ + ci->top.p = ci->func.p + 1 + LUA_MINSTACK; /* +1 for 'function' entry */ + ci->u.c.k = NULL; + ci->callstatus = CIST_C; + L->status = LUA_OK; + L->errfunc = 0; /* stack unwind can "throw away" the error function */ } static void stack_init (lua_State *L1, lua_State *L) { - int i; CallInfo *ci; + int i; /* initialize stack array */ - L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue); - L1->stacksize = BASIC_STACK_SIZE; - for (i = 0; i < BASIC_STACK_SIZE; i++) - setnilvalue(s2v(L1->stack + i)); /* erase new stack */ - L1->top = L1->stack; - L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; + L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); + L1->tbclist.p = L1->stack.p; + for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) + setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */ + L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE; /* initialize first ci */ - ci = &L1->base_ci; - ci->next = ci->previous = NULL; - ci->callstatus = CIST_C; - ci->func = L1->top; - setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ - L1->top++; - ci->top = L1->top + LUA_MINSTACK; - L1->ci = ci; + resetCI(L1); + L1->top.p = L1->stack.p + 1; /* +1 for 'function' entry */ } static void freestack (lua_State *L) { - if (L->stack == NULL) + if (L->stack.p == NULL) return; /* stack not completely built yet */ L->ci = &L->base_ci; /* free the entire 'ci' list */ - luaE_freeCI(L); + freeCI(L); lua_assert(L->nci == 0); - luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ + /* free stack */ + luaM_freearray(L, L->stack.p, cast_sizet(stacksize(L) + EXTRA_STACK)); } @@ -217,24 +184,25 @@ static void freestack (lua_State *L) { ** Create registry table and its predefined values */ static void init_registry (lua_State *L, global_State *g) { - TValue temp; /* create registry */ + TValue aux; Table *registry = luaH_new(L); sethvalue(L, &g->l_registry, registry); luaH_resize(L, registry, LUA_RIDX_LAST, 0); + /* registry[1] = false */ + setbfvalue(&aux); + luaH_setint(L, registry, 1, &aux); /* registry[LUA_RIDX_MAINTHREAD] = L */ - setthvalue(L, &temp, L); /* temp = L */ - luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp); - /* registry[LUA_RIDX_GLOBALS] = table of globals */ - sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */ - luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp); + setthvalue(L, &aux, L); + luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &aux); + /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */ + sethvalue(L, &aux, luaH_new(L)); + luaH_setint(L, registry, LUA_RIDX_GLOBALS, &aux); } /* ** open parts of the state that may cause memory-allocation errors. -** ('g->nilvalue' being a nil value flags that the state was completely -** build.) */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); @@ -244,8 +212,8 @@ static void f_luaopen (lua_State *L, void *ud) { luaS_init(L); luaT_init(L); luaX_init(L); - g->gcrunning = 1; /* allow gc */ - setnilvalue(&g->nilvalue); + g->gcstp = 0; /* allow gc */ + setnilvalue(&g->nilvalue); /* now state is complete */ luai_userstateopen(L); } @@ -256,13 +224,12 @@ static void f_luaopen (lua_State *L, void *ud) { */ static void preinit_thread (lua_State *L, global_State *g) { G(L) = g; - L->stack = NULL; + L->stack.p = NULL; L->ci = NULL; L->nci = 0; - L->stacksize = 0; L->twups = L; /* thread has no upvalues */ - L->errorJmp = NULL; L->nCcalls = 0; + L->errorJmp = NULL; L->hook = NULL; L->hookmask = 0; L->basehookcount = 0; @@ -271,36 +238,49 @@ static void preinit_thread (lua_State *L, global_State *g) { L->openupval = NULL; L->status = LUA_OK; L->errfunc = 0; + L->oldpc = 0; + L->base_ci.previous = L->base_ci.next = NULL; +} + + +lu_mem luaE_threadsize (lua_State *L) { + lu_mem sz = cast(lu_mem, sizeof(LX)) + + cast_uint(L->nci) * sizeof(CallInfo); + if (L->stack.p != NULL) + sz += cast_uint(stacksize(L) + EXTRA_STACK) * sizeof(StackValue); + return sz; } static void close_state (lua_State *L) { global_State *g = G(L); - luaF_close(L, L->stack, CLOSEPROTECT); /* close all upvalues */ - luaC_freeallobjects(L); /* collect all objects */ - if (ttisnil(&g->nilvalue)) /* closing a fully built state? */ + if (!completestate(g)) /* closing a partially built state? */ + luaC_freeallobjects(L); /* just collect its objects */ + else { /* closing a fully built state */ + resetCI(L); + luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ + L->top.p = L->stack.p + 1; /* empty the stack to run finalizers */ + luaC_freeallobjects(L); /* collect all objects */ luai_userstateclose(L); - luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); + } + luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size)); freestack(L); - lua_assert(gettotalbytes(g) == sizeof(LG)); - (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ + lua_assert(gettotalbytes(g) == sizeof(global_State)); + (*g->frealloc)(g->ud, g, sizeof(global_State), 0); /* free main block */ } LUA_API lua_State *lua_newthread (lua_State *L) { global_State *g = G(L); + GCObject *o; lua_State *L1; lua_lock(L); luaC_checkGC(L); /* create new thread */ - L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; - L1->marked = luaC_white(g); - L1->tt = LUA_TTHREAD; - /* link it on list 'allgc' */ - L1->next = g->allgc; - g->allgc = obj2gco(L1); + o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l)); + L1 = gco2th(o); /* anchor it on L stack */ - setthvalue2s(L, L->top, L1); + setthvalue2s(L, L->top.p, L1); api_incr_top(L); preinit_thread(L1, g); L1->hookmask = L->hookmask; @@ -308,7 +288,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { L1->hook = L->hook; resethookcount(L1); /* initialize L1 extra space */ - memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread), + memcpy(lua_getextraspace(L1), lua_getextraspace(mainthread(g)), LUA_EXTRASPACE); luai_userstatethread(L, L1); stack_init(L1, L); /* init stack */ @@ -319,7 +299,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { void luaE_freethread (lua_State *L, lua_State *L1) { LX *l = fromstate(L1); - luaF_close(L1, L1->stack, NOCLOSINGMETH); /* close all upvalues */ + luaF_closeupval(L1, L1->stack.p); /* close all upvalues */ lua_assert(L1->openupval == NULL); luai_userstatefree(L, L1); freestack(L1); @@ -327,74 +307,78 @@ void luaE_freethread (lua_State *L, lua_State *L1) { } -int lua_resetthread (lua_State *L) { - CallInfo *ci; - int status; - lua_lock(L); - ci = &L->base_ci; - status = luaF_close(L, L->stack, CLOSEPROTECT); - setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */ - if (status != CLOSEPROTECT) /* real errors? */ - luaD_seterrorobj(L, status, L->stack + 1); - else { +TStatus luaE_resetthread (lua_State *L, TStatus status) { + resetCI(L); + if (status == LUA_YIELD) status = LUA_OK; - L->top = L->stack + 1; - } - ci->callstatus = CIST_C; - ci->func = L->stack; - ci->top = L->top + LUA_MINSTACK; - L->ci = ci; - L->status = status; - lua_unlock(L); + status = luaD_closeprotected(L, 1, status); + if (status != LUA_OK) /* errors? */ + luaD_seterrorobj(L, status, L->stack.p + 1); + else + L->top.p = L->stack.p + 1; + luaD_reallocstack(L, cast_int(L->ci->top.p - L->stack.p), 0); return status; } -LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { +LUA_API int lua_closethread (lua_State *L, lua_State *from) { + TStatus status; + lua_lock(L); + L->nCcalls = (from) ? getCcalls(from) : 0; + status = luaE_resetthread(L, L->status); + if (L == from) /* closing itself? */ + luaD_throwbaselevel(L, status); + lua_unlock(L); + return APIstatus(status); +} + + +LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) { int i; lua_State *L; - global_State *g; - LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); - if (l == NULL) return NULL; - L = &l->l.l; - g = &l->g; - L->tt = LUA_TTHREAD; + global_State *g = cast(global_State*, + (*f)(ud, NULL, LUA_TTHREAD, sizeof(global_State))); + if (g == NULL) return NULL; + L = &g->mainth.l; + L->tt = LUA_VTHREAD; g->currentwhite = bitmask(WHITE0BIT); L->marked = luaC_white(g); preinit_thread(L, g); g->allgc = obj2gco(L); /* by now, only object is the main thread */ L->next = NULL; + incnny(L); /* main thread is always non yieldable */ g->frealloc = f; g->ud = ud; g->warnf = NULL; g->ud_warn = NULL; - g->mainthread = L; - g->seed = luai_makeseed(L); - g->gcrunning = 0; /* no GC while building state */ + g->seed = seed; + g->gcstp = GCSTPGC; /* no GC while building state */ g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(&g->l_registry); g->panic = NULL; g->gcstate = GCSpause; g->gckind = KGC_INC; + g->gcstopem = 0; g->gcemergency = 0; g->finobj = g->tobefnz = g->fixedgc = NULL; - g->survival = g->old = g->reallyold = NULL; - g->finobjsur = g->finobjold = g->finobjrold = NULL; + g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; + g->finobjsur = g->finobjold1 = g->finobjrold = NULL; g->sweepgc = NULL; g->gray = g->grayagain = NULL; g->weak = g->ephemeron = g->allweak = NULL; g->twups = NULL; - g->totalbytes = sizeof(LG); + g->GCtotalbytes = sizeof(global_State); + g->GCmarked = 0; g->GCdebt = 0; - g->lastatomic = 0; setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ - setgcparam(g->gcpause, LUAI_GCPAUSE); - setgcparam(g->gcstepmul, LUAI_GCMUL); - g->gcstepsize = LUAI_GCSTEPSIZE; - setgcparam(g->genmajormul, LUAI_GENMAJORMUL); - g->genminormul = LUAI_GENMINORMUL; - for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; + setgcparam(g, PAUSE, LUAI_GCPAUSE); + setgcparam(g, STEPMUL, LUAI_GCMUL); + setgcparam(g, STEPSIZE, LUAI_GCSTEPSIZE); + setgcparam(g, MINORMUL, LUAI_GENMINORMUL); + setgcparam(g, MINORMAJOR, LUAI_MINORMAJOR); + setgcparam(g, MAJORMINOR, LUAI_MAJORMINOR); + for (i=0; i < LUA_NUMTYPES; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ close_state(L); @@ -405,8 +389,8 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { LUA_API void lua_close (lua_State *L) { - L = G(L)->mainthread; /* only the main thread can be closed */ lua_lock(L); + L = mainthread(G(L)); /* only the main thread can be closed */ close_state(L); } @@ -418,3 +402,19 @@ void luaE_warning (lua_State *L, const char *msg, int tocont) { } +/* +** Generate a warning from an error message +*/ +void luaE_warnerror (lua_State *L, const char *where) { + TValue *errobj = s2v(L->top.p - 1); /* error object */ + const char *msg = (ttisstring(errobj)) + ? getstr(tsvalue(errobj)) + : "error object is not a string"; + /* produce warning "error in %s (%s)" (where, msg) */ + luaE_warning(L, "error in ", 1); + luaE_warning(L, where, 1); + luaE_warning(L, " (", 1); + luaE_warning(L, msg, 1); + luaE_warning(L, ")", 0); +} + diff --git a/lstate.h b/lstate.h index e35f89625b..20dc4d24f0 100644 --- a/lstate.h +++ b/lstate.h @@ -9,6 +9,11 @@ #include "lua.h" + +/* Some header files included here need this definition */ +typedef struct CallInfo CallInfo; + + #include "lobject.h" #include "ltm.h" #include "lzio.h" @@ -26,12 +31,44 @@ ** 'fixedgc': all objects that are not to be collected (currently ** only small strings, such as reserved words). ** +** For the generational collector, some of these lists have marks for +** generations. Each mark points to the first element in the list for +** that particular generation; that generation goes until the next mark. +** +** 'allgc' -> 'survival': new objects; +** 'survival' -> 'old': objects that survived one collection; +** 'old1' -> 'reallyold': objects that became old in last collection; +** 'reallyold' -> NULL: objects old for more than one cycle. +** +** 'finobj' -> 'finobjsur': new objects marked for finalization; +** 'finobjsur' -> 'finobjold1': survived """"; +** 'finobjold1' -> 'finobjrold': just old """"; +** 'finobjrold' -> NULL: really old """". +** +** All lists can contain elements older than their main ages, due +** to 'luaC_checkfinalizer' and 'udata2finalize', which move +** objects between the normal lists and the "marked for finalization" +** lists. Moreover, barriers can age young objects in young lists as +** OLD0, which then become OLD1. However, a list never contains +** elements younger than their main ages. +** +** The generational collector also uses a pointer 'firstold1', which +** points to the first OLD1 object in the list. It is used to optimize +** 'markold'. (Potentially OLD1 objects can be anywhere between 'allgc' +** and 'reallyold', but often the list has no OLD1 objects or they are +** after 'old1'.) Note the difference between it and 'old1': +** 'firstold1': no OLD1 objects before this point; there can be all +** ages after it. +** 'old1': no objects younger than OLD1 after this point. +*/ + +/* ** Moreover, there is another set of lists that control gray objects. ** These lists are linked by fields 'gclist'. (All objects that ** can become gray have such a field. The field is not the same ** in all objects, but it always has this name.) Any gray object ** must belong to one of these lists, and all objects in these lists -** must be gray: +** must be gray (with two exceptions explained below): ** ** 'gray': regular gray objects, still waiting to be visited. ** 'grayagain': objects that must be revisited at the atomic phase. @@ -42,33 +79,26 @@ ** 'weak': tables with weak values to be cleared; ** 'ephemeron': ephemeron tables with white->white entries; ** 'allweak': tables with weak keys and/or weak values to be cleared. +** +** The exceptions to that "gray rule" are: +** - TOUCHED2 objects in generational mode stay in a gray list (because +** they must be visited again at the end of the cycle), but they are +** marked black because assignments to them must activate barriers (to +** move them back to TOUCHED1). +** - Open upvalues are kept gray to avoid barriers, but they stay out +** of gray lists. (They don't even have a 'gclist' field.) */ /* -** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of -** how many "C calls" it can do in the C stack, to avoid C-stack overflow. -** This count is very rough approximation; it considers only recursive -** functions inside the interpreter, as non-recursive calls can be -** considered using a fixed (although unknown) amount of stack space. -** -** The count itself has two parts: the lower part is the count itself; -** the higher part counts the number of non-yieldable calls in the stack. -** -** Because calls to external C functions can use of unkown amount -** of space (e.g., functions using an auxiliary buffer), calls -** to these functions add more than one to the count. -** -** The proper count also includes the number of CallInfo structures -** allocated by Lua, as a kind of "potential" calls. So, when Lua -** calls a function (and "consumes" one CallInfo), it needs neither to -** increment nor to check 'nCcalls', as its use of C stack is already -** accounted for. +** About 'nCcalls': This count has two parts: the lower 16 bits counts +** the number of recursive invocations in the C stack; the higher +** 16 bits counts the number of non-yieldable calls in the stack. +** (They are together so that we can change and save both with one +** instruction.) */ -/* number of "C stack slots" used by an external C function */ -#define CSTACKCF 10 /* true if this thread does not have non-yieldable calls in the stack */ #define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0) @@ -83,13 +113,8 @@ /* Decrement the number of non-yieldable calls */ #define decnny(L) ((L)->nCcalls -= 0x10000) -/* Increment the number of non-yieldable calls and nCcalls */ -#define incXCcalls(L) ((L)->nCcalls += 0x10000 + CSTACKCF) - -/* Decrement the number of non-yieldable calls and nCcalls */ -#define decXCcalls(L) ((L)->nCcalls -= 0x10000 + CSTACKCF) - - +/* Non-yieldable call increment */ +#define nyci (0x10000 | 1) @@ -107,36 +132,66 @@ struct lua_longjmp; /* defined in ldo.c */ #endif -/* extra stack space to handle TM calls and some other extras */ +/* +** Extra stack space to handle TM calls and some other extras. This +** space is not included in 'stack_last'. It is used only to avoid stack +** checks, either because the element will be promptly popped or because +** there will be a stack check soon after the push. Function frames +** never use this extra space, so it does not need to be kept clean. +*/ #define EXTRA_STACK 5 +/* +** Size of cache for strings in the API. 'N' is the number of +** sets (better be a prime) and "M" is the size of each set. +** (M == 1 makes a direct cache.) +*/ +#if !defined(STRCACHE_N) +#define STRCACHE_N 53 +#define STRCACHE_M 2 +#endif + + #define BASIC_STACK_SIZE (2*LUA_MINSTACK) +#define stacksize(th) cast_int((th)->stack_last.p - (th)->stack.p) + /* kinds of Garbage Collection */ #define KGC_INC 0 /* incremental gc */ -#define KGC_GEN 1 /* generational gc */ +#define KGC_GENMINOR 1 /* generational gc in minor (regular) mode */ +#define KGC_GENMAJOR 2 /* generational in major mode */ typedef struct stringtable { - TString **hash; + TString **hash; /* array of buckets (linked lists of strings) */ int nuse; /* number of elements */ - int size; + int size; /* number of buckets */ } stringtable; /* ** Information about a call. +** About union 'u': +** - field 'l' is used only for Lua functions; +** - field 'c' is used only for C functions. +** About union 'u2': +** - field 'funcidx' is used only by C functions while doing a +** protected call; +** - field 'nyield' is used only while a function is "doing" an +** yield (from the yield until the next resume); +** - field 'nres' is used only while closing tbc variables when +** returning from a function; */ -typedef struct CallInfo { - StkId func; /* function index in the stack */ - StkId top; /* top for this function */ +struct CallInfo { + StkIdRel func; /* function index in the stack */ + StkIdRel top; /* top for this function */ struct CallInfo *previous, *next; /* dynamic call link */ union { struct { /* only for Lua functions */ const Instruction *savedpc; - l_signalT trap; + volatile l_signalT trap; /* function is tracing lines/counts */ int nextraargs; /* # of extra arguments in vararg functions */ } l; struct { /* only for C functions */ @@ -148,30 +203,68 @@ typedef struct CallInfo { union { int funcidx; /* called-function index */ int nyield; /* number of values yielded */ - struct { /* info about transfered values (for call/return hooks) */ - unsigned short ftransfer; /* offset of first value transfered */ - unsigned short ntransfer; /* number of values transfered */ - } transferinfo; + int nres; /* number of values returned */ } u2; - short nresults; /* expected number of results from this function */ - unsigned short callstatus; -} CallInfo; + l_uint32 callstatus; +}; + + +/* +** Maximum expected number of results from a function +** (must fit in CIST_NRESULTS). +*/ +#define MAXRESULTS 250 /* ** Bits in CallInfo status */ -#define CIST_OAH (1<<0) /* original value of 'allowhook' */ -#define CIST_C (1<<1) /* call is running a C function */ -#define CIST_HOOKED (1<<2) /* call is running a debug hook */ -#define CIST_YPCALL (1<<3) /* call is a yieldable protected call */ -#define CIST_TAIL (1<<4) /* call was tail called */ -#define CIST_HOOKYIELD (1<<5) /* last hook called yielded */ -#define CIST_FIN (1<<6) /* call is running a finalizer */ -#define CIST_TRAN (1<<7) /* 'ci' has transfer information */ -#if defined(LUA_COMPAT_LT_LE) -#define CIST_LEQ (1<<8) /* using __lt for __le */ -#endif +/* bits 0-7 are the expected number of results from this function + 1 */ +#define CIST_NRESULTS 0xffu + +/* bits 8-11 count call metamethods (and their extra arguments) */ +#define CIST_CCMT 8 /* the offset, not the mask */ +#define MAX_CCMT (0xfu << CIST_CCMT) + +/* Bits 12-14 are used for CIST_RECST (see below) */ +#define CIST_RECST 12 /* the offset, not the mask */ + +/* call is running a C function (still in first 16 bits) */ +#define CIST_C (1u << (CIST_RECST + 3)) +/* call is on a fresh "luaV_execute" frame */ +#define CIST_FRESH (cast(l_uint32, CIST_C) << 1) +/* function is closing tbc variables */ +#define CIST_CLSRET (CIST_FRESH << 1) +/* function has tbc variables to close */ +#define CIST_TBC (CIST_CLSRET << 1) +/* original value of 'allowhook' */ +#define CIST_OAH (CIST_TBC << 1) +/* call is running a debug hook */ +#define CIST_HOOKED (CIST_OAH << 1) +/* doing a yieldable protected call */ +#define CIST_YPCALL (CIST_HOOKED << 1) +/* call was tail called */ +#define CIST_TAIL (CIST_YPCALL << 1) +/* last hook called yielded */ +#define CIST_HOOKYIELD (CIST_TAIL << 1) +/* function "called" a finalizer */ +#define CIST_FIN (CIST_HOOKYIELD << 1) + + +#define get_nresults(cs) (cast_int((cs) & CIST_NRESULTS) - 1) + +/* +** Field CIST_RECST stores the "recover status", used to keep the error +** status while closing to-be-closed variables in coroutines, so that +** Lua can correctly resume after an yield from a __close method called +** because of an error. (Three bits are enough for error status.) +*/ +#define getcistrecst(ci) (((ci)->callstatus >> CIST_RECST) & 7) +#define setcistrecst(ci,st) \ + check_exp(((st) & 7) == (st), /* status must fit in three bits */ \ + ((ci)->callstatus = ((ci)->callstatus & ~(7u << CIST_RECST)) \ + | (cast(l_uint32, st) << CIST_RECST))) + /* active function is a Lua function */ #define isLua(ci) (!((ci)->callstatus & CIST_C)) @@ -179,9 +272,53 @@ typedef struct CallInfo { /* call is running Lua code (not a hook) */ #define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED))) -/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ -#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) -#define getoah(st) ((st) & CIST_OAH) + +#define setoah(ci,v) \ + ((ci)->callstatus = ((v) ? (ci)->callstatus | CIST_OAH \ + : (ci)->callstatus & ~CIST_OAH)) +#define getoah(ci) (((ci)->callstatus & CIST_OAH) ? 1 : 0) + + +/* +** 'per thread' state +*/ +struct lua_State { + CommonHeader; + lu_byte allowhook; + TStatus status; + StkIdRel top; /* first free slot in the stack */ + struct global_State *l_G; + CallInfo *ci; /* call info for current function */ + StkIdRel stack_last; /* end of stack (last element + 1) */ + StkIdRel stack; /* stack base */ + UpVal *openupval; /* list of open upvalues in this stack */ + StkIdRel tbclist; /* list of to-be-closed variables */ + GCObject *gclist; + struct lua_State *twups; /* list of threads with open upvalues */ + struct lua_longjmp *errorJmp; /* current error recover point */ + CallInfo base_ci; /* CallInfo for first level (C host) */ + volatile lua_Hook hook; + ptrdiff_t errfunc; /* current error handling function (stack index) */ + l_uint32 nCcalls; /* number of nested non-yieldable or C calls */ + int oldpc; /* last pc traced */ + int nci; /* number of items in 'ci' list */ + int basehookcount; + int hookcount; + volatile l_signalT hookmask; + struct { /* info about transferred values (for call/return hooks) */ + int ftransfer; /* offset of first value transferred */ + int ntransfer; /* number of values transferred */ + } transferinfo; +}; + + +/* +** thread state + extra space +*/ +typedef struct LX { + lu_byte extra_[LUA_EXTRASPACE]; + lua_State l; +} LX; /* @@ -190,24 +327,21 @@ typedef struct CallInfo { typedef struct global_State { lua_Alloc frealloc; /* function to reallocate memory */ void *ud; /* auxiliary data to 'frealloc' */ - l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ - l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ - lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ - lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */ + l_mem GCtotalbytes; /* number of bytes currently allocated + debt */ + l_mem GCdebt; /* bytes counted but not yet allocated */ + l_mem GCmarked; /* number of objects marked in a GC cycle */ + l_mem GCmajorminor; /* auxiliary counter to control major-minor shifts */ stringtable strt; /* hash table for strings */ TValue l_registry; TValue nilvalue; /* a nil value */ unsigned int seed; /* randomized seed for hashes */ + lu_byte gcparams[LUA_GCPN]; lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ - lu_byte genminormul; /* control for minor generational collections */ - lu_byte genmajormul; /* control for major generational collections */ - lu_byte gcrunning; /* true if GC is running */ + lu_byte gcstopem; /* stops emergency collections */ + lu_byte gcstp; /* control whether GC is running */ lu_byte gcemergency; /* true if this is an emergency collection */ - lu_byte gcpause; /* size of pause between successive GCs */ - lu_byte gcstepmul; /* GC "speed" */ - lu_byte gcstepsize; /* (log2 of) GC granularity */ GCObject *allgc; /* list of all collectable objects */ GCObject **sweepgc; /* current position of sweep in list */ GCObject *finobj; /* list of collectable objects with finalizers */ @@ -220,57 +354,42 @@ typedef struct global_State { GCObject *fixedgc; /* list of objects not to be collected */ /* fields for generational collector */ GCObject *survival; /* start of objects that survived one GC cycle */ - GCObject *old; /* start of old objects */ - GCObject *reallyold; /* old objects with more than one cycle */ + GCObject *old1; /* start of old1 objects */ + GCObject *reallyold; /* objects more than one cycle old ("really old") */ + GCObject *firstold1; /* first OLD1 object in the list (if any) */ GCObject *finobjsur; /* list of survival objects with finalizers */ - GCObject *finobjold; /* list of old objects with finalizers */ + GCObject *finobjold1; /* list of old1 objects with finalizers */ GCObject *finobjrold; /* list of really old objects with finalizers */ struct lua_State *twups; /* list of threads with open upvalues */ lua_CFunction panic; /* to be called in unprotected errors */ - struct lua_State *mainthread; TString *memerrmsg; /* message for memory-allocation errors */ TString *tmname[TM_N]; /* array with tag-method names */ - struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ + struct Table *mt[LUA_NUMTYPES]; /* metatables for basic types */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ lua_WarnFunction warnf; /* warning function */ void *ud_warn; /* auxiliary data to 'warnf' */ + LX mainth; /* main thread of this state */ } global_State; +#define G(L) (L->l_G) +#define mainthread(G) (&(G)->mainth.l) + /* -** 'per thread' state +** 'g->nilvalue' being a nil value flags that the state was completely +** build. */ -struct lua_State { - CommonHeader; - lu_byte status; - lu_byte allowhook; - unsigned short nci; /* number of items in 'ci' list */ - StkId top; /* first free slot in the stack */ - global_State *l_G; - CallInfo *ci; /* call info for current function */ - const Instruction *oldpc; /* last pc traced */ - StkId stack_last; /* last free slot in the stack */ - StkId stack; /* stack base */ - UpVal *openupval; /* list of open upvalues in this stack */ - GCObject *gclist; - struct lua_State *twups; /* list of threads with open upvalues */ - struct lua_longjmp *errorJmp; /* current error recover point */ - CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ - volatile lua_Hook hook; - ptrdiff_t errfunc; /* current error handling function (stack index) */ - l_uint32 nCcalls; /* number of allowed nested C calls - 'nci' */ - int stacksize; - int basehookcount; - int hookcount; - l_signalT hookmask; -}; - - -#define G(L) (L->l_G) +#define completestate(g) ttisnil(&g->nilvalue) /* ** Union of all collectable objects (only for conversions) +** ISO C99, 6.5.2.3 p.5: +** "if a union contains several structures that share a common initial +** sequence [...], and if the union object currently contains one +** of these structures, it is permitted to inspect the common initial +** part of any of them anywhere that a declaration of the complete type +** of the union is visible." */ union GCUnion { GCObject gc; /* common header */ @@ -284,43 +403,49 @@ union GCUnion { }; +/* +** ISO C99, 6.7.2.1 p.14: +** "A pointer to a union object, suitably converted, points to each of +** its members [...], and vice versa." +*/ #define cast_u(o) cast(union GCUnion *, (o)) /* macros to convert a GCObject into a specific value */ #define gco2ts(o) \ check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) -#define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u)) -#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l)) -#define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c)) +#define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u)) +#define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l)) +#define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c)) #define gco2cl(o) \ check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) -#define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) -#define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) -#define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) -#define gco2upv(o) \ - check_exp(novariant((o)->tt) == LUA_TUPVAL, &((cast_u(o))->upv)) +#define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h)) +#define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p)) +#define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th)) +#define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv)) /* ** macro to convert a Lua object into a GCObject -** (The access to 'tt' tries to ensure that 'v' is actually a Lua object.) */ -#define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc)) +#define obj2gco(v) \ + check_exp(novariant((v)->tt) >= LUA_TSTRING, &(cast_u(v)->gc)) -/* actual number of total bytes allocated */ -#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt) +/* actual number of total memory allocated */ +#define gettotalbytes(g) ((g)->GCtotalbytes - (g)->GCdebt) + LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); +LUAI_FUNC lu_mem luaE_threadsize (lua_State *L); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); -LUAI_FUNC void luaE_freeCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); -LUAI_FUNC void luaE_enterCcall (lua_State *L); +LUAI_FUNC void luaE_checkcstack (lua_State *L); +LUAI_FUNC void luaE_incCstack (lua_State *L); LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); +LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); +LUAI_FUNC TStatus luaE_resetthread (lua_State *L, TStatus status); -#define luaE_exitCcall(L) ((L)->nCcalls--) - #endif diff --git a/lstring.c b/lstring.c index c52539a6d5..75635142e9 100644 --- a/lstring.c +++ b/lstring.c @@ -23,46 +23,46 @@ /* -** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to -** compute its hash +** Maximum size for string table. */ -#if !defined(LUAI_HASHLIMIT) -#define LUAI_HASHLIMIT 5 -#endif - - +#define MAXSTRTB cast_int(luaM_limitN(INT_MAX, TString*)) /* -** Maximum size for string table. +** Initial size for the string table (must be power of 2). +** The Lua core alone registers ~50 strings (reserved words + +** metaevent keys + a few others). Libraries would typically add +** a few dozens more. */ -#define MAXSTRTB cast_int(luaM_limitN(MAX_INT, TString*)) +#if !defined(MINSTRTABSIZE) +#define MINSTRTABSIZE 128 +#endif /* -** equality for long strings +** generic equality for strings */ -int luaS_eqlngstr (TString *a, TString *b) { - size_t len = a->u.lnglen; - lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); - return (a == b) || /* same instance or... */ - ((len == b->u.lnglen) && /* equal length and ... */ - (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ +int luaS_eqstr (TString *a, TString *b) { + size_t len1, len2; + const char *s1 = getlstr(a, len1); + const char *s2 = getlstr(b, len2); + return ((len1 == len2) && /* equal length and ... */ + (memcmp(s1, s2, len1) == 0)); /* equal contents */ } -unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { +static unsigned luaS_hash (const char *str, size_t l, unsigned seed) { unsigned int h = seed ^ cast_uint(l); - size_t step = (l >> LUAI_HASHLIMIT) + 1; - for (; l >= step; l -= step) + for (; l > 0; l--) h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); return h; } -unsigned int luaS_hashlongstr (TString *ts) { - lua_assert(ts->tt == LUA_TLNGSTR); +unsigned luaS_hashlongstr (TString *ts) { + lua_assert(ts->tt == LUA_VLNGSTR); if (ts->extra == 0) { /* no hash? */ - ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash); + size_t len = ts->u.lnglen; + ts->hash = luaS_hash(getlngstr(ts), len, ts->hash); ts->extra = 1; /* now it has its hash */ } return ts->hash; @@ -99,7 +99,7 @@ void luaS_resize (lua_State *L, int nsize) { if (nsize < osize) /* shrinking table? */ tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */ newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*); - if (unlikely(newvect == NULL)) { /* reallocation failed? */ + if (l_unlikely(newvect == NULL)) { /* reallocation failed? */ if (nsize < osize) /* was it shrinking table? */ tablerehash(tb->hash, nsize, osize); /* restore to original size */ /* leave table as it was */ @@ -121,8 +121,8 @@ void luaS_clearcache (global_State *g) { int i, j; for (i = 0; i < STRCACHE_N; i++) for (j = 0; j < STRCACHE_M; j++) { - if (iswhite(g->strcache[i][j])) /* will entry be collected? */ - g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ + if (iswhite(g->strcache[i][j])) /* will entry be collected? */ + g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ } } @@ -146,27 +146,43 @@ void luaS_init (lua_State *L) { } +size_t luaS_sizelngstr (size_t len, int kind) { + switch (kind) { + case LSTRREG: /* regular long string */ + /* don't need 'falloc'/'ud', but need space for content */ + return offsetof(TString, falloc) + (len + 1) * sizeof(char); + case LSTRFIX: /* fixed external long string */ + /* don't need 'falloc'/'ud' */ + return offsetof(TString, falloc); + default: /* external long string with deallocation */ + lua_assert(kind == LSTRMEM); + return sizeof(TString); + } +} + /* ** creates a new string object */ -static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { +static TString *createstrobj (lua_State *L, size_t totalsize, lu_byte tag, + unsigned h) { TString *ts; GCObject *o; - size_t totalsize; /* total size of TString object */ - totalsize = sizelstring(l); o = luaC_newobj(L, tag, totalsize); ts = gco2ts(o); ts->hash = h; ts->extra = 0; - getstr(ts)[l] = '\0'; /* ending 0 */ return ts; } TString *luaS_createlngstrobj (lua_State *L, size_t l) { - TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed); + size_t totalsize = luaS_sizelngstr(l, LSTRREG); + TString *ts = createstrobj(L, totalsize, LUA_VLNGSTR, G(L)->seed); ts->u.lnglen = l; + ts->shrlen = LSTRREG; /* signals that it is a regular long string */ + ts->contents = cast_charp(ts) + offsetof(TString, falloc); + ts->contents[l] = '\0'; /* ending 0 */ return ts; } @@ -182,9 +198,9 @@ void luaS_remove (lua_State *L, TString *ts) { static void growstrtab (lua_State *L, stringtable *tb) { - if (unlikely(tb->nuse == MAX_INT)) { /* too many strings? */ + if (l_unlikely(tb->nuse == INT_MAX)) { /* too many strings? */ luaC_fullgc(L, 1); /* try to free some... */ - if (tb->nuse == MAX_INT) /* still too many? */ + if (tb->nuse == INT_MAX) /* still too many? */ luaM_error(L); /* cannot even create a message... */ } if (tb->size <= MAXSTRTB / 2) /* can grow string table? */ @@ -203,7 +219,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { TString **list = &tb->hash[lmod(h, tb->size)]; lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ for (ts = *list; ts != NULL; ts = ts->u.hnext) { - if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { + if (l == cast_uint(ts->shrlen) && + (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) { /* found! */ if (isdead(g, ts)) /* dead (but not collected yet)? */ changewhite(ts); /* resurrect it */ @@ -215,9 +232,10 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { growstrtab(L, tb); list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ } - ts = createstrobj(L, l, LUA_TSHRSTR, h); - memcpy(getstr(ts), str, l * sizeof(char)); - ts->shrlen = cast_byte(l); + ts = createstrobj(L, sizestrshr(l), LUA_VSHRSTR, h); + ts->shrlen = cast(ls_byte, l); + getshrstr(ts)[l] = '\0'; /* ending 0 */ + memcpy(getshrstr(ts), str, l * sizeof(char)); ts->u.hnext = *list; *list = ts; tb->nuse++; @@ -233,10 +251,10 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { return internshrstr(L, str, l); else { TString *ts; - if (unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) + if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString)))) luaM_toobig(L); ts = luaS_createlngstrobj(L, l); - memcpy(getstr(ts), str, l * sizeof(char)); + memcpy(getlngstr(ts), str, l * sizeof(char)); return ts; } } @@ -265,13 +283,13 @@ TString *luaS_new (lua_State *L, const char *str) { } -Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { +Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue) { Udata *u; int i; GCObject *o; - if (unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) + if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) luaM_toobig(L); - o = luaC_newobj(L, LUA_TUSERDATA, sizeudata(nuvalue, s)); + o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s)); u = gco2u(o); u->len = s; u->nuvalue = nuvalue; @@ -281,3 +299,55 @@ Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { return u; } + +struct NewExt { + ls_byte kind; + const char *s; + size_t len; + TString *ts; /* output */ +}; + + +static void f_newext (lua_State *L, void *ud) { + struct NewExt *ne = cast(struct NewExt *, ud); + size_t size = luaS_sizelngstr(0, ne->kind); + ne->ts = createstrobj(L, size, LUA_VLNGSTR, G(L)->seed); +} + + +TString *luaS_newextlstr (lua_State *L, + const char *s, size_t len, lua_Alloc falloc, void *ud) { + struct NewExt ne; + if (!falloc) { + ne.kind = LSTRFIX; + f_newext(L, &ne); /* just create header */ + } + else { + ne.kind = LSTRMEM; + if (luaD_rawrunprotected(L, f_newext, &ne) != LUA_OK) { /* mem. error? */ + (*falloc)(ud, cast_voidp(s), len + 1, 0); /* free external string */ + luaM_error(L); /* re-raise memory error */ + } + ne.ts->falloc = falloc; + ne.ts->ud = ud; + } + ne.ts->shrlen = ne.kind; + ne.ts->u.lnglen = len; + ne.ts->contents = cast_charp(s); + return ne.ts; +} + + +/* +** Normalize an external string: If it is short, internalize it. +*/ +TString *luaS_normstr (lua_State *L, TString *ts) { + size_t len = ts->u.lnglen; + if (len > LUAI_MAXSHORTLEN) + return ts; /* long string; keep the original */ + else { + const char *str = getlngstr(ts); + return internshrstr(L, str, len); + } +} + diff --git a/lstring.h b/lstring.h index b25502187b..1643c3d82b 100644 --- a/lstring.h +++ b/lstring.h @@ -19,7 +19,24 @@ #define MEMERRMSG "not enough memory" -#define sizelstring(l) (sizeof(TString) + ((l) + 1) * sizeof(char)) +/* +** Maximum length for short strings, that is, strings that are +** internalized. (Cannot be smaller than reserved words or tags for +** metamethods, as these strings must be internalized; +** #("function") = 8, #("__newindex") = 10.) +*/ +#if !defined(LUAI_MAXSHORTLEN) +#define LUAI_MAXSHORTLEN 40 +#endif + + +/* +** Size of a short TString: Size of the header plus space for the string +** itself (including final '\0'). +*/ +#define sizestrshr(l) \ + (offsetof(TString, contents) + ((l) + 1) * sizeof(char)) + #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ (sizeof(s)/sizeof(char))-1)) @@ -28,26 +45,29 @@ /* ** test whether a string is a reserved word */ -#define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) +#define isreserved(s) (strisshr(s) && (s)->extra > 0) /* ** equality for short strings, which are always internalized */ -#define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) +#define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b)) -LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); -LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); -LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); +LUAI_FUNC unsigned luaS_hashlongstr (TString *ts); +LUAI_FUNC int luaS_eqstr (TString *a, TString *b); LUAI_FUNC void luaS_resize (lua_State *L, int newsize); LUAI_FUNC void luaS_clearcache (global_State *g); LUAI_FUNC void luaS_init (lua_State *L); LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); -LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue); +LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, + unsigned short nuvalue); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); - +LUAI_FUNC TString *luaS_newextlstr (lua_State *L, + const char *s, size_t len, lua_Alloc falloc, void *ud); +LUAI_FUNC size_t luaS_sizelngstr (size_t len, int kind); +LUAI_FUNC TString *luaS_normstr (lua_State *L, TString *ts); #endif diff --git a/lstrlib.c b/lstrlib.c index 6230cd0c00..23df839ea0 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -24,6 +24,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* @@ -36,22 +37,6 @@ #endif -/* macro to 'unsign' a character */ -#define uchar(c) ((unsigned char)(c)) - - -/* -** Some sizes are better limited to fit in 'int', but must also fit in -** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.) -*/ -#define MAX_SIZET ((size_t)(~(size_t)0)) - -#define MAXSIZE \ - (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX)) - - - - static int str_len (lua_State *L) { size_t l; luaL_checklstring(L, 1, &l); @@ -128,7 +113,7 @@ static int str_lower (lua_State *L) { const char *s = luaL_checklstring(L, 1, &l); char *p = luaL_buffinitsize(L, &b, l); for (i=0; i MAXSIZE / n) /* may overflow? */ + if (n <= 0) + lua_pushliteral(L, ""); + else if (l_unlikely(len > MAX_SIZE - lsep || + cast_st2S(len + lsep) > cast_st2S(MAX_SIZE) / n)) return luaL_error(L, "resulting string too large"); else { - size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep; + size_t totallen = (cast_sizet(n) * (len + lsep)) - lsep; luaL_Buffer b; char *p = luaL_buffinitsize(L, &b, totallen); while (n-- > 1) { /* first n-1 copies (followed by separator) */ - memcpy(p, s, l * sizeof(char)); p += l; + memcpy(p, s, len * sizeof(char)); p += len; if (lsep > 0) { /* empty 'memcpy' is not that cheap */ - memcpy(p, sep, lsep * sizeof(char)); - p += lsep; + memcpy(p, sep, lsep * sizeof(char)); p += lsep; } } - memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */ + memcpy(p, s, len * sizeof(char)); /* last copy without separator */ luaL_pushresultsize(&b, totallen); } return 1; @@ -181,12 +171,12 @@ static int str_byte (lua_State *L) { size_t pose = getendpos(L, 3, pi, l); int n, i; if (posi > pose) return 0; /* empty interval; return no values */ - if (pose - posi >= (size_t)INT_MAX) /* arithmetic overflow? */ + if (l_unlikely(pose - posi >= (size_t)INT_MAX)) /* arithmetic overflow? */ return luaL_error(L, "string slice too long"); n = (int)(pose - posi) + 1; luaL_checkstack(L, n, "string slice too long"); for (i=0; iinit) { + state->init = 1; + luaL_buffinit(L, &state->B); + } + if (b == NULL) { /* finishing dump? */ + luaL_pushresult(&state->B); /* push result */ + lua_replace(L, 1); /* move it to reserved slot */ + } + else + luaL_addlstring(&state->B, (const char *)b, size); return 0; } static int str_dump (lua_State *L) { - luaL_Buffer b; + struct str_Writer state; int strip = lua_toboolean(L, 2); - luaL_checktype(L, 1, LUA_TFUNCTION); - lua_settop(L, 1); - luaL_buffinit(L,&b); - if (lua_dump(L, writer, &b, strip) != 0) - return luaL_error(L, "unable to dump given function"); - luaL_pushresult(&b); + luaL_argcheck(L, lua_type(L, 1) == LUA_TFUNCTION && !lua_iscfunction(L, 1), + 1, "Lua function expected"); + /* ensure function is on the top of the stack and vacate slot 1 */ + lua_pushvalue(L, 1); + state.init = 0; + lua_dump(L, writer, &state, strip); + lua_settop(L, 1); /* leave final result on top */ return 1; } @@ -233,6 +245,17 @@ static int str_dump (lua_State *L) { ** ======================================================= */ +#if defined(LUA_NOCVTS2N) /* { */ + +/* no coercion from strings to numbers */ + +static const luaL_Reg stringmetamethods[] = { + {"__index", NULL}, /* placeholder */ + {NULL, NULL} +}; + +#else /* }{ */ + static int tonum (lua_State *L, int arg) { if (lua_type(L, arg) == LUA_TNUMBER) { /* already a number? */ lua_pushvalue(L, arg); @@ -246,10 +269,18 @@ static int tonum (lua_State *L, int arg) { } -static void trymt (lua_State *L, const char *mtname) { +/* +** To be here, either the first operand was a string or the first +** operand didn't have a corresponding metamethod. (Otherwise, that +** other metamethod would have been called.) So, if this metamethod +** doesn't work, the only other option would be for the second +** operand to have a different metamethod. +*/ +static void trymt (lua_State *L, const char *mtkey, const char *opname) { lua_settop(L, 2); /* back to the original arguments */ - if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname)) - luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2, + if (l_unlikely(lua_type(L, 2) == LUA_TSTRING || + !luaL_getmetafield(L, 2, mtkey))) + luaL_error(L, "attempt to %s a '%s' with a '%s'", opname, luaL_typename(L, -2), luaL_typename(L, -1)); lua_insert(L, -3); /* put metamethod before arguments */ lua_call(L, 2, 1); /* call metamethod */ @@ -260,7 +291,7 @@ static int arith (lua_State *L, int op, const char *mtname) { if (tonum(L, 1) && tonum(L, 2)) lua_arith(L, op); /* result will be on the top */ else - trymt(L, mtname); + trymt(L, mtname, mtname + 2); return 1; } @@ -311,6 +342,8 @@ static const luaL_Reg stringmetamethods[] = { {NULL, NULL} }; +#endif /* } */ + /* }====================================================== */ /* @@ -330,10 +363,10 @@ typedef struct MatchState { const char *p_end; /* end ('\0') of pattern */ lua_State *L; int matchdepth; /* control for recursive depth (to avoid C stack overflow) */ - unsigned char level; /* total number of captures (finished or unfinished) */ + int level; /* total number of captures (finished or unfinished) */ struct { const char *init; - ptrdiff_t len; + ptrdiff_t len; /* length or special value (CAP_*) */ } capture[LUA_MAXCAPTURES]; } MatchState; @@ -354,7 +387,8 @@ static const char *match (MatchState *ms, const char *s, const char *p); static int check_capture (MatchState *ms, int l) { l -= '1'; - if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) + if (l_unlikely(l < 0 || l >= ms->level || + ms->capture[l].len == CAP_UNFINISHED)) return luaL_error(ms->L, "invalid capture index %%%d", l + 1); return l; } @@ -371,14 +405,14 @@ static int capture_to_close (MatchState *ms) { static const char *classend (MatchState *ms, const char *p) { switch (*p++) { case L_ESC: { - if (p == ms->p_end) + if (l_unlikely(p == ms->p_end)) luaL_error(ms->L, "malformed pattern (ends with '%%')"); return p+1; } case '[': { if (*p == '^') p++; do { /* look for a ']' */ - if (p == ms->p_end) + if (l_unlikely(p == ms->p_end)) luaL_error(ms->L, "malformed pattern (missing ']')"); if (*(p++) == L_ESC && p < ms->p_end) p++; /* skip escapes (e.g. '%]') */ @@ -421,15 +455,15 @@ static int matchbracketclass (int c, const char *p, const char *ec) { while (++p < ec) { if (*p == L_ESC) { p++; - if (match_class(c, uchar(*p))) + if (match_class(c, cast_uchar(*p))) return sig; } else if ((*(p+1) == '-') && (p+2 < ec)) { p+=2; - if (uchar(*(p-2)) <= c && c <= uchar(*p)) + if (cast_uchar(*(p-2)) <= c && c <= cast_uchar(*p)) return sig; } - else if (uchar(*p) == c) return sig; + else if (cast_uchar(*p) == c) return sig; } return !sig; } @@ -440,12 +474,12 @@ static int singlematch (MatchState *ms, const char *s, const char *p, if (s >= ms->src_end) return 0; else { - int c = uchar(*s); + int c = cast_uchar(*s); switch (*p) { case '.': return 1; /* matches any char */ - case L_ESC: return match_class(c, uchar(*(p+1))); + case L_ESC: return match_class(c, cast_uchar(*(p+1))); case '[': return matchbracketclass(c, p, ep-1); - default: return (uchar(*p) == c); + default: return (cast_uchar(*p) == c); } } } @@ -453,7 +487,7 @@ static int singlematch (MatchState *ms, const char *s, const char *p, static const char *matchbalance (MatchState *ms, const char *s, const char *p) { - if (p >= ms->p_end - 1) + if (l_unlikely(p >= ms->p_end - 1)) luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')"); if (*s != *p) return NULL; else { @@ -527,7 +561,7 @@ static const char *end_capture (MatchState *ms, const char *s, static const char *match_capture (MatchState *ms, const char *s, int l) { size_t len; l = check_capture(ms, l); - len = ms->capture[l].len; + len = cast_sizet(ms->capture[l].len); if ((size_t)(ms->src_end-s) >= len && memcmp(ms->capture[l].init, s, len) == 0) return s+len; @@ -536,9 +570,9 @@ static const char *match_capture (MatchState *ms, const char *s, int l) { static const char *match (MatchState *ms, const char *s, const char *p) { - if (ms->matchdepth-- == 0) + if (l_unlikely(ms->matchdepth-- == 0)) luaL_error(ms->L, "pattern too complex"); - init: /* using goto's to optimize tail recursion */ + init: /* using goto to optimize tail recursion */ if (p != ms->p_end) { /* end of pattern? */ switch (*p) { case '(': { /* start capture */ @@ -570,12 +604,12 @@ static const char *match (MatchState *ms, const char *s, const char *p) { case 'f': { /* frontier? */ const char *ep; char previous; p += 2; - if (*p != '[') + if (l_unlikely(*p != '[')) luaL_error(ms->L, "missing '[' after '%%f' in pattern"); ep = classend(ms, p); /* points to what is next */ previous = (s == ms->src_init) ? '\0' : *(s - 1); - if (!matchbracketclass(uchar(previous), p, ep - 1) && - matchbracketclass(uchar(*s), p, ep - 1)) { + if (!matchbracketclass(cast_uchar(previous), p, ep - 1) && + matchbracketclass(cast_uchar(*s), p, ep - 1)) { p = ep; goto init; /* return match(ms, s, ep); */ } s = NULL; /* match failed */ @@ -584,7 +618,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { /* capture results (%0-%9)? */ - s = match_capture(ms, s, uchar(*(p + 1))); + s = match_capture(ms, s, cast_uchar(*(p + 1))); if (s != NULL) { p += 2; goto init; /* return match(ms, s, p + 2) */ } @@ -651,7 +685,7 @@ static const char *lmemfind (const char *s1, size_t l1, if (memcmp(init, s2+1, l2) == 0) return init-1; else { /* correct 'l1' and 's1' to try again */ - l1 -= init-s1; + l1 -= ct_diff2sz(init - s1); s1 = init; } } @@ -660,25 +694,47 @@ static const char *lmemfind (const char *s1, size_t l1, } -static void push_onecapture (MatchState *ms, int i, const char *s, - const char *e) { +/* +** get information about the i-th capture. If there are no captures +** and 'i==0', return information about the whole match, which +** is the range 's'..'e'. If the capture is a string, return +** its length and put its address in '*cap'. If it is an integer +** (a position), push it on the stack and return CAP_POSITION. +*/ +static ptrdiff_t get_onecapture (MatchState *ms, int i, const char *s, + const char *e, const char **cap) { if (i >= ms->level) { - if (i == 0) /* ms->level == 0, too */ - lua_pushlstring(ms->L, s, e - s); /* add whole match */ - else + if (l_unlikely(i != 0)) luaL_error(ms->L, "invalid capture index %%%d", i + 1); + *cap = s; + return (e - s); } else { - ptrdiff_t l = ms->capture[i].len; - if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); - if (l == CAP_POSITION) - lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); - else - lua_pushlstring(ms->L, ms->capture[i].init, l); + ptrdiff_t capl = ms->capture[i].len; + *cap = ms->capture[i].init; + if (l_unlikely(capl == CAP_UNFINISHED)) + luaL_error(ms->L, "unfinished capture"); + else if (capl == CAP_POSITION) + lua_pushinteger(ms->L, + ct_diff2S(ms->capture[i].init - ms->src_init) + 1); + return capl; } } +/* +** Push the i-th capture on the stack. +*/ +static void push_onecapture (MatchState *ms, int i, const char *s, + const char *e) { + const char *cap; + ptrdiff_t l = get_onecapture(ms, i, s, e, &cap); + if (l != CAP_POSITION) + lua_pushlstring(ms->L, cap, cast_sizet(l)); + /* else position was already pushed */ +} + + static int push_captures (MatchState *ms, const char *s, const char *e) { int i; int nlevels = (ms->level == 0 && s) ? 1 : ms->level; @@ -723,7 +779,7 @@ static int str_find_aux (lua_State *L, int find) { const char *p = luaL_checklstring(L, 2, &lp); size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1; if (init > ls) { /* start after string's end? */ - lua_pushnil(L); /* cannot find anything */ + luaL_pushfail(L); /* cannot find anything */ return 1; } /* explicit request or no special characters? */ @@ -731,8 +787,8 @@ static int str_find_aux (lua_State *L, int find) { /* do a plain search */ const char *s2 = lmemfind(s + init, ls - init, p, lp); if (s2) { - lua_pushinteger(L, (s2 - s) + 1); - lua_pushinteger(L, (s2 - s) + lp); + lua_pushinteger(L, ct_diff2S(s2 - s) + 1); + lua_pushinteger(L, cast_st2S(ct_diff2sz(s2 - s) + lp)); return 2; } } @@ -749,8 +805,8 @@ static int str_find_aux (lua_State *L, int find) { reprepstate(&ms); if ((res=match(&ms, s1, p)) != NULL) { if (find) { - lua_pushinteger(L, (s1 - s) + 1); /* start */ - lua_pushinteger(L, res - s); /* end */ + lua_pushinteger(L, ct_diff2S(s1 - s) + 1); /* start */ + lua_pushinteger(L, ct_diff2S(res - s)); /* end */ return push_captures(&ms, NULL, 0) + 2; } else @@ -758,7 +814,7 @@ static int str_find_aux (lua_State *L, int find) { } } while (s1++ < ms.src_end && !anchor); } - lua_pushnil(L); /* not found */ + luaL_pushfail(L); /* not found */ return 1; } @@ -817,60 +873,72 @@ static int gmatch (lua_State *L) { static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, const char *e) { - size_t l, i; + size_t l; lua_State *L = ms->L; const char *news = lua_tolstring(L, 3, &l); - for (i = 0; i < l; i++) { - if (news[i] != L_ESC) - luaL_addchar(b, news[i]); - else { - i++; /* skip ESC */ - if (!isdigit(uchar(news[i]))) { - if (news[i] != L_ESC) - luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); - luaL_addchar(b, news[i]); - } - else if (news[i] == '0') - luaL_addlstring(b, s, e - s); - else { - push_onecapture(ms, news[i] - '1', s, e); - luaL_tolstring(L, -1, NULL); /* if number, convert it to string */ - lua_remove(L, -2); /* remove original value */ - luaL_addvalue(b); /* add capture to accumulated result */ - } + const char *p; + while ((p = (char *)memchr(news, L_ESC, l)) != NULL) { + luaL_addlstring(b, news, ct_diff2sz(p - news)); + p++; /* skip ESC */ + if (*p == L_ESC) /* '%%' */ + luaL_addchar(b, *p); + else if (*p == '0') /* '%0' */ + luaL_addlstring(b, s, ct_diff2sz(e - s)); + else if (isdigit(cast_uchar(*p))) { /* '%n' */ + const char *cap; + ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap); + if (resl == CAP_POSITION) + luaL_addvalue(b); /* add position to accumulated result */ + else + luaL_addlstring(b, cap, cast_sizet(resl)); } + else + luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); + l -= ct_diff2sz(p + 1 - news); + news = p + 1; } + luaL_addlstring(b, news, l); } -static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, - const char *e, int tr) { +/* +** Add the replacement value to the string buffer 'b'. +** Return true if the original string was changed. (Function calls and +** table indexing resulting in nil or false do not change the subject.) +*/ +static int add_value (MatchState *ms, luaL_Buffer *b, const char *s, + const char *e, int tr) { lua_State *L = ms->L; switch (tr) { - case LUA_TFUNCTION: { + case LUA_TFUNCTION: { /* call the function */ int n; - lua_pushvalue(L, 3); - n = push_captures(ms, s, e); - lua_call(L, n, 1); + lua_pushvalue(L, 3); /* push the function */ + n = push_captures(ms, s, e); /* all captures as arguments */ + lua_call(L, n, 1); /* call it */ break; } - case LUA_TTABLE: { - push_onecapture(ms, 0, s, e); + case LUA_TTABLE: { /* index the table */ + push_onecapture(ms, 0, s, e); /* first capture is the index */ lua_gettable(L, 3); break; } default: { /* LUA_TNUMBER or LUA_TSTRING */ - add_s(ms, b, s, e); - return; + add_s(ms, b, s, e); /* add value to the buffer */ + return 1; /* something changed */ } } if (!lua_toboolean(L, -1)) { /* nil or false? */ - lua_pop(L, 1); - lua_pushlstring(L, s, e - s); /* keep original text */ + lua_pop(L, 1); /* remove value */ + luaL_addlstring(b, s, ct_diff2sz(e - s)); /* keep original text */ + return 0; /* no changes */ + } + else if (l_unlikely(!lua_isstring(L, -1))) + return luaL_error(L, "invalid replacement value (a %s)", + luaL_typename(L, -1)); + else { + luaL_addvalue(b); /* add result to accumulator */ + return 1; /* something changed */ } - else if (!lua_isstring(L, -1)) - luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); - luaL_addvalue(b); /* add result to accumulator */ } @@ -880,9 +948,11 @@ static int str_gsub (lua_State *L) { const char *p = luaL_checklstring(L, 2, &lp); /* pattern */ const char *lastmatch = NULL; /* end of last match */ int tr = lua_type(L, 3); /* replacement type */ - lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */ + /* max replacements */ + lua_Integer max_s = luaL_optinteger(L, 4, cast_st2S(srcl) + 1); int anchor = (*p == '^'); lua_Integer n = 0; /* replacement count */ + int changed = 0; /* change flag */ MatchState ms; luaL_Buffer b; luaL_argexpected(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || @@ -898,7 +968,7 @@ static int str_gsub (lua_State *L) { reprepstate(&ms); /* (re)prepare state for new match */ if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */ n++; - add_value(&ms, &b, src, e, tr); /* add replacement to buffer */ + changed = add_value(&ms, &b, src, e, tr) | changed; src = lastmatch = e; } else if (src < ms.src_end) /* otherwise, skip one character */ @@ -906,8 +976,12 @@ static int str_gsub (lua_State *L) { else break; /* end of subject */ if (anchor) break; } - luaL_addlstring(&b, src, ms.src_end-src); - luaL_pushresult(&b); + if (!changed) /* no changes? */ + lua_pushvalue(L, 1); /* return original string */ + else { /* something changed */ + luaL_addlstring(&b, src, ct_diff2sz(ms.src_end - src)); + luaL_pushresult(&b); /* create and return new string */ + } lua_pushinteger(L, n); /* number of substitutions */ return 2; } @@ -937,21 +1011,21 @@ static int str_gsub (lua_State *L) { ** to nibble boundaries by making what is left after that first digit a ** multiple of 4. */ -#define L_NBFD ((l_mathlim(MANT_DIG) - 1)%4 + 1) +#define L_NBFD ((l_floatatt(MANT_DIG) - 1)%4 + 1) /* ** Add integer part of 'x' to buffer and return new 'x' */ -static lua_Number adddigit (char *buff, int n, lua_Number x) { +static lua_Number adddigit (char *buff, unsigned n, lua_Number x) { lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */ int d = (int)dd; - buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */ + buff[n] = cast_char(d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */ return x - dd; /* return what is left */ } -static int num2straux (char *buff, int sz, lua_Number x) { +static int num2straux (char *buff, unsigned sz, lua_Number x) { /* if 'inf' or 'NaN', format it like '%g' */ if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL) return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x); @@ -962,7 +1036,7 @@ static int num2straux (char *buff, int sz, lua_Number x) { else { int e; lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */ - int n = 0; /* character count */ + unsigned n = 0; /* character count */ if (m < 0) { /* is number negative? */ buff[n++] = '-'; /* add sign */ m = -m; /* make it positive */ @@ -976,22 +1050,22 @@ static int num2straux (char *buff, int sz, lua_Number x) { m = adddigit(buff, n++, m * 16); } while (m > 0); } - n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */ + n += cast_uint(l_sprintf(buff + n, sz - n, "p%+d", e)); /* add exponent */ lua_assert(n < sz); - return n; + return cast_int(n); } } -static int lua_number2strx (lua_State *L, char *buff, int sz, +static int lua_number2strx (lua_State *L, char *buff, unsigned sz, const char *fmt, lua_Number x) { int n = num2straux(buff, sz, x); if (fmt[SIZELENMOD] == 'A') { int i; for (i = 0; i < n; i++) - buff[i] = toupper(uchar(buff[i])); + buff[i] = cast_char(toupper(cast_uchar(buff[i]))); } - else if (fmt[SIZELENMOD] != 'a') + else if (l_unlikely(fmt[SIZELENMOD] != 'a')) return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); return n; } @@ -1000,20 +1074,51 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, /* -** Maximum size of each formatted item. This maximum size is produced +** Maximum size for items formatted with '%f'. This size is produced ** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.', ** and '\0') + number of decimal digits to represent maxfloat (which -** is maximum exponent + 1). (99+3+1 then rounded to 120 for "extra -** expenses", such as locale-dependent stuff) +** is maximum exponent + 1). (99+3+1, adding some extra, 110) */ -#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP)) +#define MAX_ITEMF (110 + l_floatatt(MAX_10_EXP)) + + +/* +** All formats except '%f' do not need that large limit. The other +** float formats use exponents, so that they fit in the 99 limit for +** significant digits; 's' for large strings and 'q' add items directly +** to the buffer; all integer formats also fit in the 99 limit. The +** worst case are floats: they may need 99 significant digits, plus +** '0x', '-', '.', 'e+XXXX', and '\0'. Adding some extra, 120. +*/ +#define MAX_ITEM 120 /* valid flags in a format specification */ -#define FLAGS "-+ #0" +#if !defined(L_FMTFLAGSF) + +/* valid flags for a, A, e, E, f, F, g, and G conversions */ +#define L_FMTFLAGSF "-+#0 " + +/* valid flags for o, x, and X conversions */ +#define L_FMTFLAGSX "-#0" + +/* valid flags for d and i conversions */ +#define L_FMTFLAGSI "-+0 " + +/* valid flags for u conversions */ +#define L_FMTFLAGSU "-0" + +/* valid flags for c, p, and s conversions */ +#define L_FMTFLAGSC "-" + +#endif + /* -** maximum size of each format specification (such as "%-099.99d") +** Maximum size of each format specification (such as "%-099.99d"): +** Initial '%', flags (up to 5), width (2), period, precision (2), +** length modifier (8), conversion specifier, and final '\0', plus some +** extra. */ #define MAX_FORMAT 32 @@ -1025,12 +1130,12 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) { luaL_addchar(b, '\\'); luaL_addchar(b, *s); } - else if (iscntrl(uchar(*s))) { + else if (iscntrl(cast_uchar(*s))) { char buff[10]; - if (!isdigit(uchar(*(s+1)))) - l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s)); + if (!isdigit(cast_uchar(*(s+1)))) + l_sprintf(buff, sizeof(buff), "\\%d", (int)cast_uchar(*s)); else - l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s)); + l_sprintf(buff, sizeof(buff), "\\%03d", (int)cast_uchar(*s)); luaL_addstring(b, buff); } else @@ -1059,9 +1164,9 @@ static int quotefloat (lua_State *L, char *buff, lua_Number n) { int nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n); /* ensures that 'buff' string uses a dot as the radix character */ - if (memchr(buff, '.', nb) == NULL) { /* no dot? */ + if (memchr(buff, '.', cast_uint(nb)) == NULL) { /* no dot? */ char point = lua_getlocaledecpoint(); /* try locale point */ - char *ppoint = (char *)memchr(buff, point, nb); + char *ppoint = (char *)memchr(buff, point, cast_uint(nb)); if (ppoint) *ppoint = '.'; /* change it to a dot */ } return nb; @@ -1091,7 +1196,7 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { : LUA_INTEGER_FMT; /* else use default format */ nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n); } - luaL_addsize(b, nb); + luaL_addsize(b, cast_uint(nb)); break; } case LUA_TNIL: case LUA_TBOOLEAN: { @@ -1106,25 +1211,53 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { } -static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { - const char *p = strfrmt; - while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ - if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char)) - luaL_error(L, "invalid format (repeated flags)"); - if (isdigit(uchar(*p))) p++; /* skip width */ - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ - if (*p == '.') { - p++; - if (isdigit(uchar(*p))) p++; /* skip precision */ - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ +static const char *get2digits (const char *s) { + if (isdigit(cast_uchar(*s))) { + s++; + if (isdigit(cast_uchar(*s))) s++; /* (2 digits at most) */ + } + return s; +} + + +/* +** Check whether a conversion specification is valid. When called, +** first character in 'form' must be '%' and last character must +** be a valid conversion specifier. 'flags' are the accepted flags; +** 'precision' signals whether to accept a precision. +*/ +static void checkformat (lua_State *L, const char *form, const char *flags, + int precision) { + const char *spec = form + 1; /* skip '%' */ + spec += strspn(spec, flags); /* skip flags */ + if (*spec != '0') { /* a width cannot start with '0' */ + spec = get2digits(spec); /* skip width */ + if (*spec == '.' && precision) { + spec++; + spec = get2digits(spec); /* skip precision */ + } } - if (isdigit(uchar(*p))) - luaL_error(L, "invalid format (width or precision too long)"); + if (!isalpha(cast_uchar(*spec))) /* did not go to the end? */ + luaL_error(L, "invalid conversion specification: '%s'", form); +} + + +/* +** Get a conversion specification and copy it to 'form'. +** Return the address of its last character. +*/ +static const char *getformat (lua_State *L, const char *strfrmt, + char *form) { + /* spans flags, width, and precision ('0' is included as a flag) */ + size_t len = strspn(strfrmt, L_FMTFLAGSF "123456789."); + len++; /* adds following character (should be the specifier) */ + /* still needs space for '%', '\0', plus a length modifier */ + if (len >= MAX_FORMAT - 10) + luaL_error(L, "invalid format (too long)"); *(form++) = '%'; - memcpy(form, strfrmt, ((p - strfrmt) + 1) * sizeof(char)); - form += (p - strfrmt) + 1; - *form = '\0'; - return p; + memcpy(form, strfrmt, len * sizeof(char)); + *(form + len) = '\0'; + return strfrmt + len - 1; } @@ -1147,6 +1280,7 @@ static int str_format (lua_State *L) { size_t sfl; const char *strfrmt = luaL_checklstring(L, arg, &sfl); const char *strfrmt_end = strfrmt+sfl; + const char *flags; luaL_Buffer b; luaL_buffinit(L, &b); while (strfrmt < strfrmt_end) { @@ -1156,38 +1290,58 @@ static int str_format (lua_State *L) { luaL_addchar(&b, *strfrmt++); /* %% */ else { /* format item */ char form[MAX_FORMAT]; /* to store the format ('%...') */ - char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */ - int nb = 0; /* number of bytes in added item */ + unsigned maxitem = MAX_ITEM; /* maximum length for the result */ + char *buff = luaL_prepbuffsize(&b, maxitem); /* to put result */ + int nb = 0; /* number of bytes in result */ if (++arg > top) return luaL_argerror(L, arg, "no value"); - strfrmt = scanformat(L, strfrmt, form); + strfrmt = getformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { - nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg)); + checkformat(L, form, L_FMTFLAGSC, 0); + nb = l_sprintf(buff, maxitem, form, (int)luaL_checkinteger(L, arg)); break; } case 'd': case 'i': - case 'o': case 'u': case 'x': case 'X': { + flags = L_FMTFLAGSI; + goto intcase; + case 'u': + flags = L_FMTFLAGSU; + goto intcase; + case 'o': case 'x': case 'X': + flags = L_FMTFLAGSX; + intcase: { lua_Integer n = luaL_checkinteger(L, arg); + checkformat(L, form, flags, 1); addlenmod(form, LUA_INTEGER_FRMLEN); - nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACINT)n); + nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n); break; } case 'a': case 'A': + checkformat(L, form, L_FMTFLAGSF, 1); addlenmod(form, LUA_NUMBER_FRMLEN); - nb = lua_number2strx(L, buff, MAX_ITEM, form, + nb = lua_number2strx(L, buff, maxitem, form, luaL_checknumber(L, arg)); break; - case 'e': case 'E': case 'f': - case 'g': case 'G': { + case 'f': + maxitem = MAX_ITEMF; /* extra space for '%f' */ + buff = luaL_prepbuffsize(&b, maxitem); + /* FALLTHROUGH */ + case 'e': case 'E': case 'g': case 'G': { lua_Number n = luaL_checknumber(L, arg); + checkformat(L, form, L_FMTFLAGSF, 1); addlenmod(form, LUA_NUMBER_FRMLEN); - nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACNUMBER)n); + nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n); break; } case 'p': { const void *p = lua_topointer(L, arg); - nb = l_sprintf(buff, MAX_ITEM, form, p); + checkformat(L, form, L_FMTFLAGSC, 0); + if (p == NULL) { /* avoid calling 'printf' with argument NULL */ + p = "(null)"; /* result */ + form[strlen(form) - 1] = 's'; /* format it as a string */ + } + nb = l_sprintf(buff, maxitem, form, p); break; } case 'q': { @@ -1203,12 +1357,13 @@ static int str_format (lua_State *L) { luaL_addvalue(&b); /* keep entire string */ else { luaL_argcheck(L, l == strlen(s), arg, "string contains zeros"); - if (!strchr(form, '.') && l >= 100) { + checkformat(L, form, L_FMTFLAGSC, 1); + if (strchr(form, '.') == NULL && l >= 100) { /* no precision and string is too long to be formatted */ luaL_addvalue(&b); /* keep entire string */ } else { /* format the string into 'buff' */ - nb = l_sprintf(buff, MAX_ITEM, form, s); + nb = l_sprintf(buff, maxitem, form, s); lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ } } @@ -1218,8 +1373,8 @@ static int str_format (lua_State *L) { return luaL_error(L, "invalid conversion '%s' to 'format'", form); } } - lua_assert(nb < MAX_ITEM); - luaL_addsize(&b, nb); + lua_assert(cast_uint(nb) < maxitem); + luaL_addsize(&b, cast_uint(nb)); } } luaL_pushresult(&b); @@ -1261,33 +1416,13 @@ static const union { } nativeendian = {1}; -/* dummy structure to get native alignment requirements */ -struct cD { - char c; - union { double d; void *p; lua_Integer i; lua_Number n; } u; -}; - -#define MAXALIGN (offsetof(struct cD, u)) - - -/* -** Union for serializing floats -*/ -typedef union Ftypes { - float f; - double d; - lua_Number n; - char buff[5 * sizeof(lua_Number)]; /* enough for any float type */ -} Ftypes; - - /* ** information to pack/unpack stuff */ typedef struct Header { lua_State *L; int islittle; - int maxalign; + unsigned maxalign; } Header; @@ -1297,7 +1432,9 @@ typedef struct Header { typedef enum KOption { Kint, /* signed integers */ Kuint, /* unsigned integers */ - Kfloat, /* floating-point numbers */ + Kfloat, /* single-precision floating-point numbers */ + Knumber, /* Lua "native" floating-point numbers */ + Kdouble, /* double-precision floating-point numbers */ Kchar, /* fixed-length strings */ Kstring, /* strings with prefixed length */ Kzstr, /* zero-terminated strings */ @@ -1313,14 +1450,14 @@ typedef enum KOption { */ static int digit (int c) { return '0' <= c && c <= '9'; } -static int getnum (const char **fmt, int df) { +static size_t getnum (const char **fmt, size_t df) { if (!digit(**fmt)) /* no number? */ return df; /* return default value */ else { - int a = 0; + size_t a = 0; do { - a = a*10 + (*((*fmt)++) - '0'); - } while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10); + a = a*10 + cast_uint(*((*fmt)++) - '0'); + } while (digit(**fmt) && a <= (MAX_SIZE - 9)/10); return a; } } @@ -1328,14 +1465,14 @@ static int getnum (const char **fmt, int df) { /* ** Read an integer numeral and raises an error if it is larger -** than the maximum size for integers. +** than the maximum size of integers. */ -static int getnumlimit (Header *h, const char **fmt, int df) { - int sz = getnum(fmt, df); - if (sz > MAXINTSIZE || sz <= 0) - return luaL_error(h->L, "integral size (%d) out of limits [1,%d]", - sz, MAXINTSIZE); - return sz; +static unsigned getnumlimit (Header *h, const char **fmt, size_t df) { + size_t sz = getnum(fmt, df); + if (l_unlikely((sz - 1u) >= MAXINTSIZE)) + return cast_uint(luaL_error(h->L, + "integral size (%d) out of limits [1,%d]", sz, MAXINTSIZE)); + return cast_uint(sz); } @@ -1352,7 +1489,9 @@ static void initheader (lua_State *L, Header *h) { /* ** Read and classify next option. 'size' is filled with option's size. */ -static KOption getoption (Header *h, const char **fmt, int *size) { +static KOption getoption (Header *h, const char **fmt, size_t *size) { + /* dummy structure to get native alignment requirements */ + struct cD { char c; union { LUAI_MAXALIGN; } u; }; int opt = *((*fmt)++); *size = 0; /* default */ switch (opt) { @@ -1366,14 +1505,14 @@ static KOption getoption (Header *h, const char **fmt, int *size) { case 'J': *size = sizeof(lua_Integer); return Kuint; case 'T': *size = sizeof(size_t); return Kuint; case 'f': *size = sizeof(float); return Kfloat; - case 'd': *size = sizeof(double); return Kfloat; - case 'n': *size = sizeof(lua_Number); return Kfloat; + case 'n': *size = sizeof(lua_Number); return Knumber; + case 'd': *size = sizeof(double); return Kdouble; case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint; case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint; case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; case 'c': - *size = getnum(fmt, -1); - if (*size == -1) + *size = getnum(fmt, cast_sizet(-1)); + if (l_unlikely(*size == cast_sizet(-1))) luaL_error(h->L, "missing size for format option 'c'"); return Kchar; case 'z': return Kzstr; @@ -1383,7 +1522,11 @@ static KOption getoption (Header *h, const char **fmt, int *size) { case '<': h->islittle = 1; break; case '>': h->islittle = 0; break; case '=': h->islittle = nativeendian.little; break; - case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); break; + case '!': { + const size_t maxalign = offsetof(struct cD, u); + h->maxalign = getnumlimit(h, fmt, maxalign); + break; + } default: luaL_error(h->L, "invalid format option '%c'", opt); } return Knop; @@ -1399,10 +1542,10 @@ static KOption getoption (Header *h, const char **fmt, int *size) { ** the maximum alignment ('maxalign'). Kchar option needs no alignment ** despite its size. */ -static KOption getdetails (Header *h, size_t totalsize, - const char **fmt, int *psize, int *ntoalign) { +static KOption getdetails (Header *h, size_t totalsize, const char **fmt, + size_t *psize, unsigned *ntoalign) { KOption opt = getoption(h, fmt, psize); - int align = *psize; /* usually, alignment follows size */ + size_t align = *psize; /* usually, alignment follows size */ if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0) luaL_argerror(h->L, 1, "invalid next option for option 'X'"); @@ -1412,9 +1555,15 @@ static KOption getdetails (Header *h, size_t totalsize, else { if (align > h->maxalign) /* enforce maximum alignment */ align = h->maxalign; - if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */ + if (l_unlikely(!ispow2(align))) { /* not a power of 2? */ + *ntoalign = 0; /* to avoid warnings */ luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); - *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1); + } + else { + /* 'szmoda' = totalsize % align */ + unsigned szmoda = cast_uint(totalsize & (align - 1)); + *ntoalign = cast_uint((align - szmoda) & (align - 1)); + } } return opt; } @@ -1427,9 +1576,9 @@ static KOption getdetails (Header *h, size_t totalsize, ** bytes if necessary (by default they would be zeros). */ static void packint (luaL_Buffer *b, lua_Unsigned n, - int islittle, int size, int neg) { + int islittle, unsigned size, int neg) { char *buff = luaL_prepbuffsize(b, size); - int i; + unsigned i; buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */ for (i = 1; i < size; i++) { n >>= NB; @@ -1447,12 +1596,10 @@ static void packint (luaL_Buffer *b, lua_Unsigned n, ** Copy 'size' bytes from 'src' to 'dest', correcting endianness if ** given 'islittle' is different from native endianness. */ -static void copywithendian (volatile char *dest, volatile const char *src, - int size, int islittle) { - if (islittle == nativeendian.little) { - while (size-- != 0) - *(dest++) = *(src++); - } +static void copywithendian (char *dest, const char *src, + unsigned size, int islittle) { + if (islittle == nativeendian.little) + memcpy(dest, src, size); else { dest += size - 1; while (size-- != 0) @@ -1471,8 +1618,11 @@ static int str_pack (lua_State *L) { lua_pushnil(L); /* mark to separate arguments from string buffer */ luaL_buffinit(L, &b); while (*fmt != '\0') { - int size, ntoalign; + unsigned ntoalign; + size_t size; KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); + luaL_argcheck(L, size + ntoalign <= MAX_SIZE - totalsize, arg, + "result too long"); totalsize += ntoalign + size; while (ntoalign-- > 0) luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */ @@ -1484,7 +1634,7 @@ static int str_pack (lua_State *L) { lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1); luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow"); } - packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0)); + packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), (n < 0)); break; } case Kuint: { /* unsigned integers */ @@ -1492,38 +1642,54 @@ static int str_pack (lua_State *L) { if (size < SZINT) /* need overflow check? */ luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)), arg, "unsigned overflow"); - packint(&b, (lua_Unsigned)n, h.islittle, size, 0); + packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), 0); + break; + } + case Kfloat: { /* C float */ + float f = (float)luaL_checknumber(L, arg); /* get argument */ + char *buff = luaL_prepbuffsize(&b, sizeof(f)); + /* move 'f' to final result, correcting endianness if needed */ + copywithendian(buff, (char *)&f, sizeof(f), h.islittle); + luaL_addsize(&b, size); + break; + } + case Knumber: { /* Lua float */ + lua_Number f = luaL_checknumber(L, arg); /* get argument */ + char *buff = luaL_prepbuffsize(&b, sizeof(f)); + /* move 'f' to final result, correcting endianness if needed */ + copywithendian(buff, (char *)&f, sizeof(f), h.islittle); + luaL_addsize(&b, size); break; } - case Kfloat: { /* floating-point options */ - volatile Ftypes u; - char *buff = luaL_prepbuffsize(&b, size); - lua_Number n = luaL_checknumber(L, arg); /* get argument */ - if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */ - else if (size == sizeof(u.d)) u.d = (double)n; - else u.n = n; - /* move 'u' to final result, correcting endianness if needed */ - copywithendian(buff, u.buff, size, h.islittle); + case Kdouble: { /* C double */ + double f = (double)luaL_checknumber(L, arg); /* get argument */ + char *buff = luaL_prepbuffsize(&b, sizeof(f)); + /* move 'f' to final result, correcting endianness if needed */ + copywithendian(buff, (char *)&f, sizeof(f), h.islittle); luaL_addsize(&b, size); break; } case Kchar: { /* fixed-size string */ size_t len; const char *s = luaL_checklstring(L, arg, &len); - luaL_argcheck(L, len <= (size_t)size, arg, - "string longer than given size"); + luaL_argcheck(L, len <= size, arg, "string longer than given size"); luaL_addlstring(&b, s, len); /* add string */ - while (len++ < (size_t)size) /* pad extra space */ - luaL_addchar(&b, LUAL_PACKPADBYTE); + if (len < size) { /* does it need padding? */ + size_t psize = size - len; /* pad size */ + char *buff = luaL_prepbuffsize(&b, psize); + memset(buff, LUAL_PACKPADBYTE, psize); + luaL_addsize(&b, psize); + } break; } case Kstring: { /* strings with length count */ size_t len; const char *s = luaL_checklstring(L, arg, &len); - luaL_argcheck(L, size >= (int)sizeof(size_t) || - len < ((size_t)1 << (size * NB)), + luaL_argcheck(L, size >= sizeof(lua_Unsigned) || + len < ((lua_Unsigned)1 << (size * NB)), arg, "string length does not fit in given size"); - packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */ + /* pack length */ + packint(&b, (lua_Unsigned)len, h.islittle, cast_uint(size), 0); luaL_addlstring(&b, s, len); totalsize += len; break; @@ -1554,16 +1720,17 @@ static int str_packsize (lua_State *L) { size_t totalsize = 0; /* accumulate total size of result */ initheader(L, &h); while (*fmt != '\0') { - int size, ntoalign; + unsigned ntoalign; + size_t size; KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1, "variable-length format"); size += ntoalign; /* total space used by option */ - luaL_argcheck(L, totalsize <= MAXSIZE - size, 1, - "format result too large"); + luaL_argcheck(L, totalsize <= LUA_MAXINTEGER - size, + 1, "format result too large"); totalsize += size; } - lua_pushinteger(L, (lua_Integer)totalsize); + lua_pushinteger(L, cast_st2S(totalsize)); return 1; } @@ -1594,7 +1761,7 @@ static lua_Integer unpackint (lua_State *L, const char *str, else if (size > SZINT) { /* must check unread bytes */ int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC; for (i = limit; i < size; i++) { - if ((unsigned char)str[islittle ? i : size - 1 - i] != mask) + if (l_unlikely((unsigned char)str[islittle ? i : size - 1 - i] != mask)) luaL_error(L, "%d-byte integer does not fit into Lua Integer", size); } } @@ -1612,9 +1779,10 @@ static int str_unpack (lua_State *L) { luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); initheader(L, &h); while (*fmt != '\0') { - int size, ntoalign; + unsigned ntoalign; + size_t size; KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); - luaL_argcheck(L, (size_t)ntoalign + size <= ld - pos, 2, + luaL_argcheck(L, ntoalign + size <= ld - pos, 2, "data string too short"); pos += ntoalign; /* skip alignment */ /* stack space for item + next position */ @@ -1623,19 +1791,27 @@ static int str_unpack (lua_State *L) { switch (opt) { case Kint: case Kuint: { - lua_Integer res = unpackint(L, data + pos, h.islittle, size, - (opt == Kint)); + lua_Integer res = unpackint(L, data + pos, h.islittle, + cast_int(size), (opt == Kint)); lua_pushinteger(L, res); break; } case Kfloat: { - volatile Ftypes u; - lua_Number num; - copywithendian(u.buff, data + pos, size, h.islittle); - if (size == sizeof(u.f)) num = (lua_Number)u.f; - else if (size == sizeof(u.d)) num = (lua_Number)u.d; - else num = u.n; - lua_pushnumber(L, num); + float f; + copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); + lua_pushnumber(L, (lua_Number)f); + break; + } + case Knumber: { + lua_Number f; + copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); + lua_pushnumber(L, f); + break; + } + case Kdouble: { + double f; + copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); + lua_pushnumber(L, (lua_Number)f); break; } case Kchar: { @@ -1643,14 +1819,15 @@ static int str_unpack (lua_State *L) { break; } case Kstring: { - size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0); + lua_Unsigned len = (lua_Unsigned)unpackint(L, data + pos, + h.islittle, cast_int(size), 0); luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short"); - lua_pushlstring(L, data + pos + size, len); - pos += len; /* skip string */ + lua_pushlstring(L, data + pos + size, cast_sizet(len)); + pos += cast_sizet(len); /* skip string */ break; } case Kzstr: { - size_t len = (int)strlen(data + pos); + size_t len = strlen(data + pos); luaL_argcheck(L, pos + len < ld, 2, "unfinished string for format 'z'"); lua_pushlstring(L, data + pos, len); @@ -1663,7 +1840,7 @@ static int str_unpack (lua_State *L) { } pos += size; } - lua_pushinteger(L, pos + 1); /* next position */ + lua_pushinteger(L, cast_st2S(pos) + 1); /* next position */ return n + 1; } diff --git a/ltable.c b/ltable.c index e12381b2d8..b7f88f6ffe 100644 --- a/ltable.c +++ b/ltable.c @@ -25,6 +25,7 @@ #include #include +#include #include "lua.h" @@ -40,18 +41,48 @@ /* -** MAXABITS is the largest integer such that MAXASIZE fits in an +** Only hash parts with at least 2^LIMFORLAST have a 'lastfree' field +** that optimizes finding a free slot. That field is stored just before +** the array of nodes, in the same block. Smaller tables do a complete +** search when looking for a free slot. +*/ +#define LIMFORLAST 3 /* log2 of real limit (8) */ + +/* +** The union 'Limbox' stores 'lastfree' and ensures that what follows it +** is properly aligned to store a Node. +*/ +typedef struct { Node *dummy; Node follows_pNode; } Limbox_aux; + +typedef union { + Node *lastfree; + char padding[offsetof(Limbox_aux, follows_pNode)]; +} Limbox; + +#define haslastfree(t) ((t)->lsizenode >= LIMFORLAST) +#define getlastfree(t) ((cast(Limbox *, (t)->node) - 1)->lastfree) + + +/* +** MAXABITS is the largest integer such that 2^MAXABITS fits in an ** unsigned int. */ -#define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1) +#define MAXABITS (l_numbits(int) - 1) + + +/* +** MAXASIZEB is the maximum number of elements in the array part such +** that the size of the array fits in 'size_t'. +*/ +#define MAXASIZEB (MAX_SIZET/(sizeof(Value) + 1)) /* ** MAXASIZE is the maximum size of the array part. It is the minimum -** between 2^MAXABITS and the maximum size such that, measured in bytes, -** it fits in a 'size_t'. +** between 2^MAXABITS and MAXASIZEB. */ -#define MAXASIZE luaM_limitN(1u << MAXABITS, TValue) +#define MAXASIZE \ + (((1u << MAXABITS) < MAXASIZEB) ? (1u << MAXABITS) : cast_uint(MAXASIZEB)) /* ** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a @@ -65,21 +96,24 @@ ** between 2^MAXHBITS and the maximum size such that, measured in bytes, ** it fits in a 'size_t'. */ -#define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node) +#define MAXHSIZE luaM_limitN(1 << MAXHBITS, Node) +/* +** When the original hash value is good, hashing by a power of 2 +** avoids the cost of '%'. +*/ #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) -#define hashstr(t,str) hashpow2(t, (str)->hash) -#define hashboolean(t,p) hashpow2(t, p) -#define hashint(t,i) hashpow2(t, i) - - /* -** for some types, it is better to avoid modulus by power of 2, as -** they tend to have many 2 factors. +** for other types, it is better to avoid modulo by power of 2, as +** they can have many 2 factors. */ -#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) +#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1u)|1u)))) + + +#define hashstr(t,str) hashpow2(t, (str)->hash) +#define hashboolean(t,p) hashpow2(t, p) #define hashpointer(t,p) hashmod(t, point2uint(p)) @@ -87,22 +121,42 @@ #define dummynode (&dummynode_) +/* +** Common hash part for tables with empty hash parts. That allows all +** tables to have a hash part, avoiding an extra check ("is there a hash +** part?") when indexing. Its sole node has an empty value and a key +** (DEADKEY, NULL) that is different from any valid TValue. +*/ static const Node dummynode_ = { - {{NULL}, LUA_TEMPTY, /* value's value and type */ - LUA_TNIL, 0, {NULL}} /* key type, next, and key value */ + {{NULL}, LUA_VEMPTY, /* value's value and type */ + LUA_TDEADKEY, 0, {NULL}} /* key type, next, and key value */ }; static const TValue absentkey = {ABSTKEYCONSTANT}; +/* +** Hash for integers. To allow a good hash, use the remainder operator +** ('%'). If integer fits as a non-negative int, compute an int +** remainder, which is faster. Otherwise, use an unsigned-integer +** remainder, which uses all bits and ensures a non-negative result. +*/ +static Node *hashint (const Table *t, lua_Integer i) { + lua_Unsigned ui = l_castS2U(i); + if (ui <= cast_uint(INT_MAX)) + return gnode(t, cast_int(ui) % cast_int((sizenode(t)-1) | 1)); + else + return hashmod(t, ui); +} + /* ** Hash for floating-point numbers. ** The main computation should be just ** n = frexp(n, &i); return (n * INT_MAX) + i ** but there are some numerical subtleties. -** In a two-complement representation, INT_MAX does not has an exact +** In a two-complement representation, INT_MAX may not have an exact ** representation as a float, but INT_MIN does; because the absolute ** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the ** absolute value of the product 'frexp * -INT_MIN' is smaller or equal @@ -111,7 +165,7 @@ static const TValue absentkey = {ABSTKEYCONSTANT}; ** INT_MIN. */ #if !defined(l_hashfloat) -static int l_hashfloat (lua_Number n) { +static unsigned l_hashfloat (lua_Number n) { int i; lua_Integer ni; n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN); @@ -121,7 +175,7 @@ static int l_hashfloat (lua_Number n) { } else { /* normal case */ unsigned int u = cast_uint(i) + cast_uint(ni); - return cast_int(u <= cast_uint(INT_MAX) ? u : ~u); + return (u <= cast_uint(INT_MAX) ? u : ~u); } } #endif @@ -129,130 +183,115 @@ static int l_hashfloat (lua_Number n) { /* ** returns the 'main' position of an element in a table (that is, -** the index of its hash value). The key comes broken (tag in 'ktt' -** and value in 'vkl') so that we can call it on keys inserted into -** nodes. -*/ -static Node *mainposition (const Table *t, int ktt, const Value *kvl) { - switch (withvariant(ktt)) { - case LUA_TNUMINT: - return hashint(t, ivalueraw(*kvl)); - case LUA_TNUMFLT: - return hashmod(t, l_hashfloat(fltvalueraw(*kvl))); - case LUA_TSHRSTR: - return hashstr(t, tsvalueraw(*kvl)); - case LUA_TLNGSTR: - return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl))); - case LUA_TBOOLEAN: - return hashboolean(t, bvalueraw(*kvl)); - case LUA_TLIGHTUSERDATA: - return hashpointer(t, pvalueraw(*kvl)); - case LUA_TLCF: - return hashpointer(t, fvalueraw(*kvl)); - default: - return hashpointer(t, gcvalueraw(*kvl)); +** the index of its hash value). +*/ +static Node *mainpositionTV (const Table *t, const TValue *key) { + switch (ttypetag(key)) { + case LUA_VNUMINT: { + lua_Integer i = ivalue(key); + return hashint(t, i); + } + case LUA_VNUMFLT: { + lua_Number n = fltvalue(key); + return hashmod(t, l_hashfloat(n)); + } + case LUA_VSHRSTR: { + TString *ts = tsvalue(key); + return hashstr(t, ts); + } + case LUA_VLNGSTR: { + TString *ts = tsvalue(key); + return hashpow2(t, luaS_hashlongstr(ts)); + } + case LUA_VFALSE: + return hashboolean(t, 0); + case LUA_VTRUE: + return hashboolean(t, 1); + case LUA_VLIGHTUSERDATA: { + void *p = pvalue(key); + return hashpointer(t, p); + } + case LUA_VLCF: { + lua_CFunction f = fvalue(key); + return hashpointer(t, f); + } + default: { + GCObject *o = gcvalue(key); + return hashpointer(t, o); + } } } -static Node *mainpositionTV (const Table *t, const TValue *key) { - return mainposition(t, rawtt(key), valraw(key)); +l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) { + TValue key; + getnodekey(cast(lua_State *, NULL), &key, nd); + return mainpositionTV(t, &key); } /* -** Check whether key 'k1' is equal to the key in node 'n2'. -** This equality is raw, so there are no metamethods. Floats -** with integer values have been normalized, so integers cannot -** be equal to floats. It is assumed that 'eqshrstr' is simply -** pointer equality, so that short strings are handled in the -** default case. +** Check whether key 'k1' is equal to the key in node 'n2'. This +** equality is raw, so there are no metamethods. Floats with integer +** values have been normalized, so integers cannot be equal to +** floats. It is assumed that 'eqshrstr' is simply pointer equality, +** so that short strings are handled in the default case. The flag +** 'deadok' means to accept dead keys as equal to their original values. +** (Only collectable objects can produce dead keys.) Note that dead +** long strings are also compared by identity. Once a key is dead, +** its corresponding value may be collected, and then another value +** can be created with the same address. If this other value is given +** to 'next', 'equalkey' will signal a false positive. In a regular +** traversal, this situation should never happen, as all keys given to +** 'next' came from the table itself, and therefore could not have been +** collected. Outside a regular traversal, we have garbage in, garbage +** out. What is relevant is that this false positive does not break +** anything. (In particular, 'next' will return some other valid item +** on the table or nil.) */ -static int equalkey (const TValue *k1, const Node *n2) { - if (rawtt(k1) != keytt(n2)) /* not the same variants? */ - return 0; /* cannot be same key */ - switch (ttypetag(k1)) { - case LUA_TNIL: - return 1; - case LUA_TNUMINT: - return (ivalue(k1) == keyival(n2)); - case LUA_TNUMFLT: - return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); - case LUA_TBOOLEAN: - return bvalue(k1) == bvalueraw(keyval(n2)); - case LUA_TLIGHTUSERDATA: - return pvalue(k1) == pvalueraw(keyval(n2)); - case LUA_TLCF: - return fvalue(k1) == fvalueraw(keyval(n2)); - case LUA_TLNGSTR: - return luaS_eqlngstr(tsvalue(k1), keystrval(n2)); - default: +static int equalkey (const TValue *k1, const Node *n2, int deadok) { + if (rawtt(k1) != keytt(n2)) { /* not the same variants? */ + if (keyisshrstr(n2) && ttislngstring(k1)) { + /* an external string can be equal to a short-string key */ + return luaS_eqstr(tsvalue(k1), keystrval(n2)); + } + else if (deadok && keyisdead(n2) && iscollectable(k1)) { + /* a collectable value can be equal to a dead key */ return gcvalue(k1) == gcvalueraw(keyval(n2)); + } + else + return 0; /* otherwise, different variants cannot be equal */ } -} - - -/* -** True if value of 'alimit' is equal to the real size of the array -** part of table 't'. (Otherwise, the array part must be larger than -** 'alimit'.) -*/ -#define limitequalsasize(t) (isrealasize(t) || ispow2((t)->alimit)) - - -/* -** Returns the real size of the 'array' array -*/ -LUAI_FUNC unsigned int luaH_realasize (const Table *t) { - if (limitequalsasize(t)) - return t->alimit; /* this is the size */ - else { - unsigned int size = t->alimit; - /* compute the smallest power of 2 not smaller than 'n' */ - size |= (size >> 1); - size |= (size >> 2); - size |= (size >> 4); - size |= (size >> 8); - size |= (size >> 16); -#if (INT_MAX >> 30 >> 1) > 0 - size |= (size >> 32); /* int has more than 32 bits */ -#endif - size++; - lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size); - return size; + else { /* equal variants */ + switch (keytt(n2)) { + case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: + return 1; + case LUA_VNUMINT: + return (ivalue(k1) == keyival(n2)); + case LUA_VNUMFLT: + return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); + case LUA_VLIGHTUSERDATA: + return pvalue(k1) == pvalueraw(keyval(n2)); + case LUA_VLCF: + return fvalue(k1) == fvalueraw(keyval(n2)); + case ctb(LUA_VLNGSTR): + return luaS_eqstr(tsvalue(k1), keystrval(n2)); + default: + return gcvalue(k1) == gcvalueraw(keyval(n2)); + } } } -/* -** Check whether real size of the array is a power of 2. -** (If it is not, 'alimit' cannot be changed to any other value -** without changing the real size.) -*/ -static int ispow2realasize (const Table *t) { - return (!isrealasize(t) || ispow2(t->alimit)); -} - - -static unsigned int setlimittosize (Table *t) { - t->alimit = luaH_realasize(t); - setrealasize(t); - return t->alimit; -} - - -#define limitasasize(t) check_exp(isrealasize(t), t->alimit) - - - /* ** "Generic" get version. (Not that generic: not valid for integers, ** which may be in array part, nor for floats with integral values.) +** See explanation about 'deadok' in function 'equalkey'. */ -static const TValue *getgeneric (Table *t, const TValue *key) { +static const TValue *getgeneric (Table *t, const TValue *key, int deadok) { Node *n = mainpositionTV(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ - if (equalkey(key, n)) + if (equalkey(key, n, deadok)) return gval(n); /* that's it */ else { int nx = gnext(n); @@ -265,14 +304,34 @@ static const TValue *getgeneric (Table *t, const TValue *key) { /* -** returns the index for 'k' if 'k' is an appropriate key to live in -** the array part of a table, 0 otherwise. +** Return the index 'k' (converted to an unsigned) if it is inside +** the range [1, limit]. */ -static unsigned int arrayindex (lua_Integer k) { - if (0 < k && l_castS2U(k) <= MAXASIZE) - return cast_uint(k); /* 'key' is an appropriate array index */ - else - return 0; +static unsigned checkrange (lua_Integer k, unsigned limit) { + return (l_castS2U(k) - 1u < limit) ? cast_uint(k) : 0; +} + + +/* +** Return the index 'k' if 'k' is an appropriate key to live in the +** array part of a table, 0 otherwise. +*/ +#define arrayindex(k) checkrange(k, MAXASIZE) + + +/* +** Check whether an integer key is in the array part of a table and +** return its index there, or zero. +*/ +#define ikeyinarray(t,k) checkrange(k, t->asize) + + +/* +** Check whether a key is in the array part of a table and return its +** index there, or zero. +*/ +static unsigned keyinarray (Table *t, const TValue *key) { + return (ttisinteger(key)) ? ikeyinarray(t, ivalue(key)) : 0; } @@ -281,18 +340,18 @@ static unsigned int arrayindex (lua_Integer k) { ** elements in the array part, then elements in the hash part. The ** beginning of a traversal is signaled by 0. */ -static unsigned int findindex (lua_State *L, Table *t, TValue *key, - unsigned int asize) { +static unsigned findindex (lua_State *L, Table *t, TValue *key, + unsigned asize) { unsigned int i; if (ttisnil(key)) return 0; /* first iteration */ - i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0; - if (i != 0 && i <= asize) /* is 'key' inside array part? */ + i = keyinarray(t, key); + if (i != 0) /* is 'key' inside array part? */ return i; /* yes; that's the index */ else { - const TValue *n = getgeneric(t, key); - if (unlikely(isabstkey(n))) + const TValue *n = getgeneric(t, key, 1); + if (l_unlikely(isabstkey(n))) luaG_runerror(L, "invalid key to 'next'"); /* key not found */ - i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ + i = cast_uint(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ return (i + 1) + asize; } @@ -300,16 +359,17 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key, int luaH_next (lua_State *L, Table *t, StkId key) { - unsigned int asize = luaH_realasize(t); + unsigned int asize = t->asize; unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */ for (; i < asize; i++) { /* try first array part */ - if (!isempty(&t->array[i])) { /* a non-empty entry? */ - setivalue(s2v(key), i + 1); - setobj2s(L, key + 1, &t->array[i]); + lu_byte tag = *getArrTag(t, i); + if (!tagisempty(tag)) { /* a non-empty entry? */ + setivalue(s2v(key), cast_int(i) + 1); + farr2val(t, i, tag, s2v(key + 1)); return 1; } } - for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */ + for (i -= asize; i < sizenode(t); i++) { /* hash part */ if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */ Node *n = gnode(t, i); getnodekey(L, s2v(key), n); @@ -321,9 +381,21 @@ int luaH_next (lua_State *L, Table *t, StkId key) { } +/* Extra space in Node array if it has a lastfree entry */ +#define extraLastfree(t) (haslastfree(t) ? sizeof(Limbox) : 0) + +/* 'node' size in bytes */ +static size_t sizehash (Table *t) { + return cast_sizet(sizenode(t)) * sizeof(Node) + extraLastfree(t); +} + + static void freehash (lua_State *L, Table *t) { - if (!isdummy(t)) - luaM_freearray(L, t->node, cast_sizet(sizenode(t))); + if (!isdummy(t)) { + /* get pointer to the beginning of Node array */ + char *arr = cast_charp(t->node) - extraLastfree(t); + luaM_freearray(L, arr, sizehash(t)); + } } @@ -333,58 +405,92 @@ static void freehash (lua_State *L, Table *t) { ** ============================================================== */ +static int insertkey (Table *t, const TValue *key, TValue *value); +static void newcheckedkey (Table *t, const TValue *key, TValue *value); + + /* -** Compute the optimal size for the array part of table 't'. 'nums' is a -** "count array" where 'nums[i]' is the number of integers in the table -** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of -** integer keys in the table and leaves with the number of keys that -** will go to the array part; return the optimal size. (The condition -** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.) +** Structure to count the keys in a table. +** 'total' is the total number of keys in the table. +** 'na' is the number of *array indices* in the table (see 'arrayindex'). +** 'deleted' is true if there are deleted nodes in the hash part. +** 'nums' is a "count array" where 'nums[i]' is the number of integer +** keys between 2^(i - 1) + 1 and 2^i. Note that 'na' is the summation +** of 'nums'. */ -static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { +typedef struct { + unsigned total; + unsigned na; + int deleted; + unsigned nums[MAXABITS + 1]; +} Counters; + + +/* +** Check whether it is worth to use 'na' array entries instead of 'nh' +** hash nodes. (A hash node uses ~3 times more memory than an array +** entry: Two values plus 'next' versus one value.) Evaluate with size_t +** to avoid overflows. +*/ +#define arrayXhash(na,nh) (cast_sizet(na) <= cast_sizet(nh) * 3) + +/* +** Compute the optimal size for the array part of table 't'. +** This size maximizes the number of elements going to the array part +** while satisfying the condition 'arrayXhash' with the use of memory if +** all those elements went to the hash part. +** 'ct->na' enters with the total number of array indices in the table +** and leaves with the number of keys that will go to the array part; +** return the optimal size for the array part. +*/ +static unsigned computesizes (Counters *ct) { int i; unsigned int twotoi; /* 2^i (candidate for optimal size) */ unsigned int a = 0; /* number of elements smaller than 2^i */ unsigned int na = 0; /* number of elements to go to array part */ unsigned int optimal = 0; /* optimal size for array part */ - /* loop while keys can fill more than half of total size */ + /* traverse slices while 'twotoi' does not overflow and total of array + indices still can satisfy 'arrayXhash' against the array size */ for (i = 0, twotoi = 1; - twotoi > 0 && *pna > twotoi / 2; + twotoi > 0 && arrayXhash(twotoi, ct->na); i++, twotoi *= 2) { - a += nums[i]; - if (a > twotoi/2) { /* more than half elements present? */ + unsigned nums = ct->nums[i]; + a += nums; + if (nums > 0 && /* grows array only if it gets more elements... */ + arrayXhash(twotoi, a)) { /* ...while using "less memory" */ optimal = twotoi; /* optimal size (till now) */ na = a; /* all elements up to 'optimal' will go to array part */ } } - lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); - *pna = na; + ct->na = na; return optimal; } -static int countint (lua_Integer key, unsigned int *nums) { +static void countint (lua_Integer key, Counters *ct) { unsigned int k = arrayindex(key); - if (k != 0) { /* is 'key' an appropriate array index? */ - nums[luaO_ceillog2(k)]++; /* count as such */ - return 1; + if (k != 0) { /* is 'key' an array index? */ + ct->nums[luaO_ceillog2(k)]++; /* count as such */ + ct->na++; } - else - return 0; +} + + +l_sinline int arraykeyisempty (const Table *t, unsigned key) { + int tag = *getArrTag(t, key - 1); + return tagisempty(tag); } /* -** Count keys in array part of table 't': Fill 'nums[i]' with -** number of keys that will go into corresponding slice and return -** total number of non-nil keys. +** Count keys in array part of table 't'. */ -static unsigned int numusearray (const Table *t, unsigned int *nums) { +static void numusearray (const Table *t, Counters *ct) { int lg; unsigned int ttlg; /* 2^lg */ unsigned int ause = 0; /* summation of 'nums' */ - unsigned int i = 1; /* count to traverse all array keys */ - unsigned int asize = limitasasize(t); /* real array size */ + unsigned int i = 1; /* index to traverse all array keys */ + unsigned int asize = t->asize; /* traverse each slice */ for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) { unsigned int lc = 0; /* counter */ @@ -396,30 +502,93 @@ static unsigned int numusearray (const Table *t, unsigned int *nums) { } /* count elements in range (2^(lg - 1), 2^lg] */ for (; i <= lim; i++) { - if (!isempty(&t->array[i-1])) + if (!arraykeyisempty(t, i)) lc++; } - nums[lg] += lc; + ct->nums[lg] += lc; ause += lc; } - return ause; + ct->total += ause; + ct->na += ause; } -static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { - int totaluse = 0; /* total number of elements */ - int ause = 0; /* elements added to 'nums' (can go to array part) */ - int i = sizenode(t); +/* +** Count keys in hash part of table 't'. As this only happens during +** a rehash, all nodes have been used. A node can have a nil value only +** if it was deleted after being created. +*/ +static void numusehash (const Table *t, Counters *ct) { + unsigned i = sizenode(t); + unsigned total = 0; while (i--) { Node *n = &t->node[i]; - if (!isempty(gval(n))) { + if (isempty(gval(n))) { + lua_assert(!keyisnil(n)); /* entry was deleted; key cannot be nil */ + ct->deleted = 1; + } + else { + total++; if (keyisinteger(n)) - ause += countint(keyival(n), nums); - totaluse++; + countint(keyival(n), ct); + } + } + ct->total += total; +} + + +/* +** Convert an "abstract size" (number of slots in an array) to +** "concrete size" (number of bytes in the array). +*/ +static size_t concretesize (unsigned int size) { + if (size == 0) + return 0; + else /* space for the two arrays plus an unsigned in between */ + return size * (sizeof(Value) + 1) + sizeof(unsigned); +} + + +/* +** Resize the array part of a table. If new size is equal to the old, +** do nothing. Else, if new size is zero, free the old array. (It must +** be present, as the sizes are different.) Otherwise, allocate a new +** array, move the common elements to new proper position, and then +** frees the old array. +** We could reallocate the array, but we still would need to move the +** elements to their new position, so the copy implicit in realloc is a +** waste. Moreover, most allocators will move the array anyway when the +** new size is double the old one (the most common case). +*/ +static Value *resizearray (lua_State *L , Table *t, + unsigned oldasize, + unsigned newasize) { + if (oldasize == newasize) + return t->array; /* nothing to be done */ + else if (newasize == 0) { /* erasing array? */ + Value *op = t->array - oldasize; /* original array's real address */ + luaM_freemem(L, op, concretesize(oldasize)); /* free it */ + return NULL; + } + else { + size_t newasizeb = concretesize(newasize); + Value *np = cast(Value *, + luaM_reallocvector(L, NULL, 0, newasizeb, lu_byte)); + if (np == NULL) /* allocation error? */ + return NULL; + np += newasize; /* shift pointer to the end of value segment */ + if (oldasize > 0) { + /* move common elements to new position */ + size_t oldasizeb = concretesize(oldasize); + Value *op = t->array; /* original array */ + unsigned tomove = (oldasize < newasize) ? oldasize : newasize; + size_t tomoveb = (oldasize < newasize) ? oldasizeb : newasizeb; + lua_assert(tomoveb > 0); + memcpy(np - tomove, op - tomove, tomoveb); + luaM_freemem(L, op - oldasize, oldasizeb); /* free old block */ } + return np; } - *pna += ause; - return totaluse; } @@ -430,27 +599,34 @@ static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { ** comparison ensures that the shift in the second one does not ** overflow. */ -static void setnodevector (lua_State *L, Table *t, unsigned int size) { +static void setnodevector (lua_State *L, Table *t, unsigned size) { if (size == 0) { /* no elements to hash part? */ t->node = cast(Node *, dummynode); /* use common 'dummynode' */ t->lsizenode = 0; - t->lastfree = NULL; /* signal that it is using dummy node */ + setdummy(t); /* signal that it is using dummy node */ } else { int i; int lsize = luaO_ceillog2(size); - if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE) + if (lsize > MAXHBITS || (1 << lsize) > MAXHSIZE) luaG_runerror(L, "table overflow"); size = twoto(lsize); - t->node = luaM_newvector(L, size, Node); - for (i = 0; i < (int)size; i++) { + if (lsize < LIMFORLAST) /* no 'lastfree' field? */ + t->node = luaM_newvector(L, size, Node); + else { + size_t bsize = size * sizeof(Node) + sizeof(Limbox); + char *node = luaM_newblock(L, bsize); + t->node = cast(Node *, node + sizeof(Limbox)); + getlastfree(t) = gnode(t, size); /* all positions are free */ + } + t->lsizenode = cast_byte(lsize); + setnodummy(t); + for (i = 0; i < cast_int(size); i++) { Node *n = gnode(t, i); gnext(n) = 0; setnilkey(n); setempty(gval(n)); } - t->lsizenode = cast_byte(lsize); - t->lastfree = gnode(t, size); /* all positions are free */ } } @@ -458,9 +634,9 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { /* ** (Re)insert all elements from the hash part of 'ot' into table 't'. */ -static void reinsert (lua_State *L, Table *ot, Table *t) { - int j; - int size = sizenode(ot); +static void reinserthash (lua_State *L, Table *ot, Table *t) { + unsigned j; + unsigned size = sizenode(ot); for (j = 0; j < size; j++) { Node *old = gnode(ot, j); if (!isempty(gval(old))) { @@ -468,25 +644,56 @@ static void reinsert (lua_State *L, Table *ot, Table *t) { already present in the table */ TValue k; getnodekey(L, &k, old); - setobjt2t(L, luaH_set(L, t, &k), gval(old)); + newcheckedkey(t, &k, gval(old)); } } } /* -** Exchange the hash part of 't1' and 't2'. +** Exchange the hash part of 't1' and 't2'. (In 'flags', only the +** dummy bit must be exchanged: The 'isrealasize' is not related +** to the hash part, and the metamethod bits do not change during +** a resize, so the "real" table can keep their values.) */ static void exchangehashpart (Table *t1, Table *t2) { lu_byte lsizenode = t1->lsizenode; Node *node = t1->node; - Node *lastfree = t1->lastfree; + int bitdummy1 = t1->flags & BITDUMMY; t1->lsizenode = t2->lsizenode; t1->node = t2->node; - t1->lastfree = t2->lastfree; + t1->flags = cast_byte((t1->flags & NOTBITDUMMY) | (t2->flags & BITDUMMY)); t2->lsizenode = lsizenode; t2->node = node; - t2->lastfree = lastfree; + t2->flags = cast_byte((t2->flags & NOTBITDUMMY) | bitdummy1); +} + + +/* +** Re-insert into the new hash part of a table the elements from the +** vanishing slice of the array part. +*/ +static void reinsertOldSlice (Table *t, unsigned oldasize, + unsigned newasize) { + unsigned i; + for (i = newasize; i < oldasize; i++) { /* traverse vanishing slice */ + lu_byte tag = *getArrTag(t, i); + if (!tagisempty(tag)) { /* a non-empty entry? */ + TValue key, aux; + setivalue(&key, l_castU2S(i) + 1); /* make the key */ + farr2val(t, i, tag, &aux); /* copy value into 'aux' */ + insertkey(t, &key, &aux); /* insert entry into the hash part */ + } + } +} + + +/* +** Clear new slice of the array. +*/ +static void clearNewSlice (Table *t, unsigned oldasize, unsigned newasize) { + for (; oldasize < newasize; oldasize++) + *getArrTag(t, oldasize) = LUA_VEMPTY; } @@ -502,105 +709,138 @@ static void exchangehashpart (Table *t1, Table *t2) { ** into the table, initializes the new part of the array (if any) with ** nils and reinserts the elements of the old hash back into the new ** parts of the table. +** Note that if the new size for the array part ('newasize') is equal to +** the old one ('oldasize'), this function will do nothing with that +** part. */ -void luaH_resize (lua_State *L, Table *t, unsigned int newasize, - unsigned int nhsize) { - unsigned int i; +void luaH_resize (lua_State *L, Table *t, unsigned newasize, + unsigned nhsize) { Table newt; /* to keep the new hash part */ - unsigned int oldasize = setlimittosize(t); - TValue *newarray; + unsigned oldasize = t->asize; + Value *newarray; + if (newasize > MAXASIZE) + luaG_runerror(L, "table overflow"); /* create new hash part with appropriate size into 'newt' */ + newt.flags = 0; setnodevector(L, &newt, nhsize); if (newasize < oldasize) { /* will array shrink? */ - t->alimit = newasize; /* pretend array has new size... */ - exchangehashpart(t, &newt); /* and new hash */ /* re-insert into the new hash the elements from vanishing slice */ - for (i = newasize; i < oldasize; i++) { - if (!isempty(&t->array[i])) - luaH_setint(L, t, i + 1, &t->array[i]); - } - t->alimit = oldasize; /* restore current size... */ - exchangehashpart(t, &newt); /* and hash (in case of errors) */ + exchangehashpart(t, &newt); /* pretend table has new hash */ + reinsertOldSlice(t, oldasize, newasize); + exchangehashpart(t, &newt); /* restore old hash (in case of errors) */ } /* allocate new array */ - newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue); - if (unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ + newarray = resizearray(L, t, oldasize, newasize); + if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ freehash(L, &newt); /* release new hash part */ luaM_error(L); /* raise error (with array unchanged) */ } /* allocation ok; initialize new part of the array */ exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */ t->array = newarray; /* set new array part */ - t->alimit = newasize; - for (i = oldasize; i < newasize; i++) /* clear new slice of the array */ - setempty(&t->array[i]); + t->asize = newasize; + if (newarray != NULL) + *lenhint(t) = newasize / 2u; /* set an initial hint */ + clearNewSlice(t, oldasize, newasize); /* re-insert elements from old hash part into new parts */ - reinsert(L, &newt, t); /* 'newt' now has the old hash */ + reinserthash(L, &newt, t); /* 'newt' now has the old hash */ freehash(L, &newt); /* free old hash part */ } void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { - int nsize = allocsizenode(t); + unsigned nsize = allocsizenode(t); luaH_resize(L, t, nasize, nsize); } + /* -** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i +** Rehash a table. First, count its keys. If there are array indices +** outside the array part, compute the new best size for that part. +** Then, resize the table. */ static void rehash (lua_State *L, Table *t, const TValue *ek) { - unsigned int asize; /* optimal size for array part */ - unsigned int na; /* number of keys in the array part */ - unsigned int nums[MAXABITS + 1]; - int i; - int totaluse; - for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ - setlimittosize(t); - na = numusearray(t, nums); /* count keys in array part */ - totaluse = na; /* all those keys are integer keys */ - totaluse += numusehash(t, nums, &na); /* count keys in hash part */ - /* count extra key */ + unsigned asize; /* optimal size for array part */ + Counters ct; + unsigned i; + unsigned nsize; /* size for the hash part */ + /* reset counts */ + for (i = 0; i <= MAXABITS; i++) ct.nums[i] = 0; + ct.na = 0; + ct.deleted = 0; + ct.total = 1; /* count extra key */ if (ttisinteger(ek)) - na += countint(ivalue(ek), nums); - totaluse++; - /* compute new size for array part */ - asize = computesizes(nums, &na); + countint(ivalue(ek), &ct); /* extra key may go to array */ + numusehash(t, &ct); /* count keys in hash part */ + if (ct.na == 0) { + /* no new keys to enter array part; keep it with the same size */ + asize = t->asize; + } + else { /* compute best size for array part */ + numusearray(t, &ct); /* count keys in array part */ + asize = computesizes(&ct); /* compute new size for array part */ + } + /* all keys not in the array part go to the hash part */ + nsize = ct.total - ct.na; + if (ct.deleted) { /* table has deleted entries? */ + /* insertion-deletion-insertion: give hash some extra size to + avoid repeated resizings */ + nsize += nsize >> 2; + } /* resize the table to new computed sizes */ - luaH_resize(L, t, asize, totaluse - na); + luaH_resize(L, t, asize, nsize); } - - /* ** }============================================================= */ Table *luaH_new (lua_State *L) { - GCObject *o = luaC_newobj(L, LUA_TTABLE, sizeof(Table)); + GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table)); Table *t = gco2t(o); t->metatable = NULL; - t->flags = cast_byte(~0); + t->flags = maskflags; /* table has no metamethod fields */ t->array = NULL; - t->alimit = 0; + t->asize = 0; setnodevector(L, t, 0); return t; } +lu_mem luaH_size (Table *t) { + lu_mem sz = cast(lu_mem, sizeof(Table)) + concretesize(t->asize); + if (!isdummy(t)) + sz += sizehash(t); + return sz; +} + + +/* +** Frees a table. +*/ void luaH_free (lua_State *L, Table *t) { freehash(L, t); - luaM_freearray(L, t->array, luaH_realasize(t)); + resizearray(L, t, t->asize, 0); luaM_free(L, t); } static Node *getfreepos (Table *t) { - if (!isdummy(t)) { - while (t->lastfree > t->node) { - t->lastfree--; - if (keyisnil(t->lastfree)) - return t->lastfree; + if (haslastfree(t)) { /* does it have 'lastfree' information? */ + /* look for a spot before 'lastfree', updating 'lastfree' */ + while (getlastfree(t) > t->node) { + Node *free = --getlastfree(t); + if (keyisnil(free)) + return free; + } + } + else { /* no 'lastfree' information */ + unsigned i = sizenode(t); + while (i--) { /* do a linear search */ + Node *free = gnode(t, i); + if (keyisnil(free)) + return free; } } return NULL; /* could not find a free place */ @@ -609,38 +849,24 @@ static Node *getfreepos (Table *t) { /* -** inserts a new key into a hash table; first, check whether key's main +** Inserts a new key into a hash table; first, check whether key's main ** position is free. If not, check whether colliding node is in its main -** position or not: if it is not, move colliding node to an empty place and -** put new key in its main position; otherwise (colliding node is in its main -** position), new key goes to an empty position. -*/ -TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { - Node *mp; - TValue aux; - if (unlikely(ttisnil(key))) - luaG_runerror(L, "table index is nil"); - else if (ttisfloat(key)) { - lua_Number f = fltvalue(key); - lua_Integer k; - if (luaV_flttointeger(f, &k, 0)) { /* does key fit in an integer? */ - setivalue(&aux, k); - key = &aux; /* insert it as an integer */ - } - else if (unlikely(luai_numisnan(f))) - luaG_runerror(L, "table index is NaN"); - } - mp = mainpositionTV(t, key); +** position or not: if it is not, move colliding node to an empty place +** and put new key in its main position; otherwise (colliding node is in +** its main position), new key goes to an empty position. Return 0 if +** could not insert key (could not find a free space). +*/ +static int insertkey (Table *t, const TValue *key, TValue *value) { + Node *mp = mainpositionTV(t, key); + /* table cannot already contain the key */ + lua_assert(isabstkey(getgeneric(t, key, 0))); if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */ Node *othern; Node *f = getfreepos(t); /* get a free place */ - if (f == NULL) { /* cannot find a free place? */ - rehash(L, t, key); /* grow table */ - /* whatever called 'newkey' takes care of TM cache */ - return luaH_set(L, t, key); /* insert key into grown table */ - } + if (f == NULL) /* cannot find a free place? */ + return 0; lua_assert(!isdummy(t)); - othern = mainposition(t, keytt(mp), &keyval(mp)); + othern = mainpositionfromnode(t, mp); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ while (othern + gnext(othern) != mp) /* find previous */ @@ -662,52 +888,93 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { mp = f; } } - setnodekey(L, mp, key); - luaC_barrierback(L, obj2gco(t), key); + setnodekey(mp, key); lua_assert(isempty(gval(mp))); - return gval(mp); + setobj2t(cast(lua_State *, 0), gval(mp), value); + return 1; } /* -** Search function for integers. If integer is inside 'alimit', get it -** directly from the array part. Otherwise, if 'alimit' is not equal to -** the real size of the array, key still can be in the array part. In -** this case, try to avoid a call to 'luaH_realasize' when key is just -** one more than the limit (so that it can be incremented without -** changing the real size of the array). +** Insert a key in a table where there is space for that key, the +** key is valid, and the value is not nil. */ -const TValue *luaH_getint (Table *t, lua_Integer key) { - if (l_castS2U(key) - 1u < t->alimit) /* (1 <= key && key <= t->alimit)? */ - return &t->array[key - 1]; - else if (!limitequalsasize(t) && /* key still may be in the array part? */ - (l_castS2U(key) == t->alimit + 1 || - l_castS2U(key) - 1u < luaH_realasize(t))) { - t->alimit = cast_uint(key); /* probably '#t' is here now */ - return &t->array[key - 1]; - } +static void newcheckedkey (Table *t, const TValue *key, TValue *value) { + unsigned i = keyinarray(t, key); + if (i > 0) /* is key in the array part? */ + obj2arr(t, i - 1, value); /* set value in the array */ else { - Node *n = hashint(t, key); - for (;;) { /* check whether 'key' is somewhere in the chain */ - if (keyisinteger(n) && keyival(n) == key) - return gval(n); /* that's it */ - else { - int nx = gnext(n); - if (nx == 0) break; - n += nx; - } + int done = insertkey(t, key, value); /* insert key in the hash part */ + lua_assert(done); /* it cannot fail */ + cast(void, done); /* to avoid warnings */ + } +} + + +static void luaH_newkey (lua_State *L, Table *t, const TValue *key, + TValue *value) { + if (!ttisnil(value)) { /* do not insert nil values */ + int done = insertkey(t, key, value); + if (!done) { /* could not find a free place? */ + rehash(L, t, key); /* grow table */ + newcheckedkey(t, key, value); /* insert key in grown table */ + } + luaC_barrierback(L, obj2gco(t), key); + /* for debugging only: any new key may force an emergency collection */ + condchangemem(L, (void)0, (void)0, 1); + } +} + + +static const TValue *getintfromhash (Table *t, lua_Integer key) { + Node *n = hashint(t, key); + lua_assert(!ikeyinarray(t, key)); + for (;;) { /* check whether 'key' is somewhere in the chain */ + if (keyisinteger(n) && keyival(n) == key) + return gval(n); /* that's it */ + else { + int nx = gnext(n); + if (nx == 0) break; + n += nx; } - return &absentkey; } + return &absentkey; +} + + +static int hashkeyisempty (Table *t, lua_Unsigned key) { + const TValue *val = getintfromhash(t, l_castU2S(key)); + return isempty(val); +} + + +static lu_byte finishnodeget (const TValue *val, TValue *res) { + if (!ttisnil(val)) { + setobj(((lua_State*)NULL), res, val); + } + return ttypetag(val); +} + + +lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res) { + unsigned k = ikeyinarray(t, key); + if (k > 0) { + lu_byte tag = *getArrTag(t, k - 1); + if (!tagisempty(tag)) + farr2val(t, k - 1, tag, res); + return tag; + } + else + return finishnodeget(getintfromhash(t, key), res); } /* ** search function for short strings */ -const TValue *luaH_getshortstr (Table *t, TString *key) { +const TValue *luaH_Hgetshortstr (Table *t, TString *key) { Node *n = hashstr(t, key); - lua_assert(key->tt == LUA_TSHRSTR); + lua_assert(strisshr(key)); for (;;) { /* check whether 'key' is somewhere in the chain */ if (keyisshrstr(n) && eqshrstr(keystrval(n), key)) return gval(n); /* that's it */ @@ -721,33 +988,202 @@ const TValue *luaH_getshortstr (Table *t, TString *key) { } -const TValue *luaH_getstr (Table *t, TString *key) { - if (key->tt == LUA_TSHRSTR) - return luaH_getshortstr(t, key); - else { /* for long strings, use generic case */ - TValue ko; - setsvalue(cast(lua_State *, NULL), &ko, key); - return getgeneric(t, &ko); - } +lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res) { + return finishnodeget(luaH_Hgetshortstr(t, key), res); +} + + +static const TValue *Hgetlongstr (Table *t, TString *key) { + TValue ko; + lua_assert(!strisshr(key)); + setsvalue(cast(lua_State *, NULL), &ko, key); + return getgeneric(t, &ko, 0); /* for long strings, use generic case */ +} + + +static const TValue *Hgetstr (Table *t, TString *key) { + if (strisshr(key)) + return luaH_Hgetshortstr(t, key); + else + return Hgetlongstr(t, key); +} + + +lu_byte luaH_getstr (Table *t, TString *key, TValue *res) { + return finishnodeget(Hgetstr(t, key), res); } /* ** main search function */ -const TValue *luaH_get (Table *t, const TValue *key) { +lu_byte luaH_get (Table *t, const TValue *key, TValue *res) { + const TValue *slot; + switch (ttypetag(key)) { + case LUA_VSHRSTR: + slot = luaH_Hgetshortstr(t, tsvalue(key)); + break; + case LUA_VNUMINT: + return luaH_getint(t, ivalue(key), res); + case LUA_VNIL: + slot = &absentkey; + break; + case LUA_VNUMFLT: { + lua_Integer k; + if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ + return luaH_getint(t, k, res); /* use specialized version */ + /* else... */ + } /* FALLTHROUGH */ + default: + slot = getgeneric(t, key, 0); + break; + } + return finishnodeget(slot, res); +} + + +/* +** When a 'pset' cannot be completed, this function returns an encoding +** of its result, to be used by 'luaH_finishset'. +*/ +static int retpsetcode (Table *t, const TValue *slot) { + if (isabstkey(slot)) + return HNOTFOUND; /* no slot with that key */ + else /* return node encoded */ + return cast_int((cast(Node*, slot) - t->node)) + HFIRSTNODE; +} + + +static int finishnodeset (Table *t, const TValue *slot, TValue *val) { + if (!ttisnil(slot)) { + setobj(((lua_State*)NULL), cast(TValue*, slot), val); + return HOK; /* success */ + } + else + return retpsetcode(t, slot); +} + + +static int rawfinishnodeset (const TValue *slot, TValue *val) { + if (isabstkey(slot)) + return 0; /* no slot with that key */ + else { + setobj(((lua_State*)NULL), cast(TValue*, slot), val); + return 1; /* success */ + } +} + + +int luaH_psetint (Table *t, lua_Integer key, TValue *val) { + lua_assert(!ikeyinarray(t, key)); + return finishnodeset(t, getintfromhash(t, key), val); +} + + +static int psetint (Table *t, lua_Integer key, TValue *val) { + int hres; + luaH_fastseti(t, key, val, hres); + return hres; +} + + +/* +** This function could be just this: +** return finishnodeset(t, luaH_Hgetshortstr(t, key), val); +** However, it optimizes the common case created by constructors (e.g., +** {x=1, y=2}), which creates a key in a table that has no metatable, +** it is not old/black, and it already has space for the key. +*/ + +int luaH_psetshortstr (Table *t, TString *key, TValue *val) { + const TValue *slot = luaH_Hgetshortstr(t, key); + if (!ttisnil(slot)) { /* key already has a value? (all too common) */ + setobj(((lua_State*)NULL), cast(TValue*, slot), val); /* update it */ + return HOK; /* done */ + } + else if (checknoTM(t->metatable, TM_NEWINDEX)) { /* no metamethod? */ + if (ttisnil(val)) /* new value is nil? */ + return HOK; /* done (value is already nil/absent) */ + if (isabstkey(slot) && /* key is absent? */ + !(isblack(t) && iswhite(key))) { /* and don't need barrier? */ + TValue tk; /* key as a TValue */ + setsvalue(cast(lua_State *, NULL), &tk, key); + if (insertkey(t, &tk, val)) { /* insert key, if there is space */ + invalidateTMcache(t); + return HOK; + } + } + } + /* Else, either table has new-index metamethod, or it needs barrier, + or it needs to rehash for the new key. In any of these cases, the + operation cannot be completed here. Return a code for the caller. */ + return retpsetcode(t, slot); +} + + +int luaH_psetstr (Table *t, TString *key, TValue *val) { + if (strisshr(key)) + return luaH_psetshortstr(t, key, val); + else + return finishnodeset(t, Hgetlongstr(t, key), val); +} + + +int luaH_pset (Table *t, const TValue *key, TValue *val) { switch (ttypetag(key)) { - case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key)); - case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); - case LUA_TNIL: return &absentkey; - case LUA_TNUMFLT: { + case LUA_VSHRSTR: return luaH_psetshortstr(t, tsvalue(key), val); + case LUA_VNUMINT: return psetint(t, ivalue(key), val); + case LUA_VNIL: return HNOTFOUND; + case LUA_VNUMFLT: { lua_Integer k; - if (luaV_flttointeger(fltvalue(key), &k, 0)) /* index is an integral? */ - return luaH_getint(t, k); /* use specialized version */ + if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ + return psetint(t, k, val); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ default: - return getgeneric(t, key); + return finishnodeset(t, getgeneric(t, key, 0), val); + } +} + +/* +** Finish a raw "set table" operation, where 'hres' encodes where the +** value should have been (the result of a previous 'pset' operation). +** Beware: when using this function the caller probably need to check a +** GC barrier and invalidate the TM cache. +*/ +void luaH_finishset (lua_State *L, Table *t, const TValue *key, + TValue *value, int hres) { + lua_assert(hres != HOK); + if (hres == HNOTFOUND) { + TValue aux; + if (l_unlikely(ttisnil(key))) + luaG_runerror(L, "table index is nil"); + else if (ttisfloat(key)) { + lua_Number f = fltvalue(key); + lua_Integer k; + if (luaV_flttointeger(f, &k, F2Ieq)) { + setivalue(&aux, k); /* key is equal to an integer */ + key = &aux; /* insert it as an integer */ + } + else if (l_unlikely(luai_numisnan(f))) + luaG_runerror(L, "table index is NaN"); + } + else if (isextstr(key)) { /* external string? */ + /* If string is short, must internalize it to be used as table key */ + TString *ts = luaS_normstr(L, tsvalue(key)); + setsvalue2s(L, L->top.p++, ts); /* anchor 'ts' (EXTRA_STACK) */ + luaH_newkey(L, t, s2v(L->top.p - 1), value); + L->top.p--; + return; + } + luaH_newkey(L, t, key, value); + } + else if (hres > 0) { /* regular Node? */ + setobj2t(L, gval(gnode(t, hres - HFIRSTNODE)), value); + } + else { /* array entry */ + hres = ~hres; /* real index */ + obj2arr(t, cast_uint(hres), value); } } @@ -756,162 +1192,164 @@ const TValue *luaH_get (Table *t, const TValue *key) { ** beware: when using this function you probably need to check a GC ** barrier and invalidate the TM cache. */ -TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { - const TValue *p = luaH_get(t, key); - if (!isabstkey(p)) - return cast(TValue *, p); - else return luaH_newkey(L, t, key); +void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { + int hres = luaH_pset(t, key, value); + if (hres != HOK) + luaH_finishset(L, t, key, value, hres); } +/* +** Ditto for a GC barrier. (No need to invalidate the TM cache, as +** integers cannot be keys to metamethods.) +*/ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { - const TValue *p = luaH_getint(t, key); - TValue *cell; - if (!isabstkey(p)) - cell = cast(TValue *, p); + unsigned ik = ikeyinarray(t, key); + if (ik > 0) + obj2arr(t, ik - 1, value); else { - TValue k; - setivalue(&k, key); - cell = luaH_newkey(L, t, &k); + int ok = rawfinishnodeset(getintfromhash(t, key), value); + if (!ok) { + TValue k; + setivalue(&k, key); + luaH_newkey(L, t, &k, value); + } } - setobj2t(L, cell, value); } /* ** Try to find a boundary in the hash part of table 't'. From the -** caller, we know that 'j' is zero or present and that 'j + 1' is -** present. We want to find a larger key that is absent from the -** table, so that we can do a binary search between the two keys to -** find a boundary. We keep doubling 'j' until we get an absent index. -** If the doubling would overflow, we try LUA_MAXINTEGER. If it is -** absent, we are ready for the binary search. ('j', being max integer, -** is larger or equal to 'i', but it cannot be equal because it is -** absent while 'i' is present; so 'j > i'.) Otherwise, 'j' is a -** boundary. ('j + 1' cannot be a present integer key because it is -** not a valid integer in Lua.) -*/ -static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { - lua_Unsigned i; - if (j == 0) j++; /* the caller ensures 'j + 1' is present */ - do { +** caller, we know that 'asize + 1' is present. We want to find a larger +** key that is absent from the table, so that we can do a binary search +** between the two keys to find a boundary. We keep doubling 'j' until +** we get an absent index. If the doubling would overflow, we try +** LUA_MAXINTEGER. If it is absent, we are ready for the binary search. +** ('j', being max integer, is larger or equal to 'i', but it cannot be +** equal because it is absent while 'i' is present.) Otherwise, 'j' is a +** boundary. ('j + 1' cannot be a present integer key because it is not +** a valid integer in Lua.) +** About 'rnd': If we used a fixed algorithm, a bad actor could fill +** a table with only the keys that would be probed, in such a way that +** a small table could result in a huge length. To avoid that, we use +** the state's seed as a source of randomness. For the first probe, +** we "randomly double" 'i' by adding to it a random number roughly its +** width. +*/ +static lua_Unsigned hash_search (lua_State *L, Table *t, unsigned asize) { + lua_Unsigned i = asize + 1; /* caller ensures t[i] is present */ + unsigned rnd = G(L)->seed; + int n = (asize > 0) ? luaO_ceillog2(asize) : 0; /* width of 'asize' */ + unsigned mask = (1u << n) - 1; /* 11...111 with the width of 'asize' */ + unsigned incr = (rnd & mask) + 1; /* first increment (at least 1) */ + lua_Unsigned j = (incr <= l_castS2U(LUA_MAXINTEGER) - i) ? i + incr : i + 1; + rnd >>= n; /* used 'n' bits from 'rnd' */ + while (!hashkeyisempty(t, j)) { /* repeat until an absent t[j] */ i = j; /* 'i' is a present index */ - if (j <= l_castS2U(LUA_MAXINTEGER) / 2) - j *= 2; + if (j <= l_castS2U(LUA_MAXINTEGER)/2 - 1) { + j = j*2 + (rnd & 1); /* try again with 2j or 2j+1 */ + rnd >>= 1; + } else { j = LUA_MAXINTEGER; - if (isempty(luaH_getint(t, j))) /* t[j] not present? */ + if (hashkeyisempty(t, j)) /* t[j] not present? */ break; /* 'j' now is an absent index */ else /* weird case */ return j; /* well, max integer is a boundary... */ } - } while (!isempty(luaH_getint(t, j))); /* repeat until an absent t[j] */ + } /* i < j && t[i] present && t[j] absent */ while (j - i > 1u) { /* do a binary search between them */ lua_Unsigned m = (i + j) / 2; - if (isempty(luaH_getint(t, m))) j = m; + if (hashkeyisempty(t, m)) j = m; else i = m; } return i; } -static unsigned int binsearch (const TValue *array, unsigned int i, - unsigned int j) { +static unsigned int binsearch (Table *array, unsigned int i, unsigned int j) { + lua_assert(i <= j); while (j - i > 1u) { /* binary search */ unsigned int m = (i + j) / 2; - if (isempty(&array[m - 1])) j = m; + if (arraykeyisempty(array, m)) j = m; else i = m; } return i; } +/* return a border, saving it as a hint for next call */ +static lua_Unsigned newhint (Table *t, unsigned hint) { + lua_assert(hint <= t->asize); + *lenhint(t) = hint; + return hint; +} + + /* -** Try to find a boundary in table 't'. (A 'boundary' is an integer index -** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent -** and 'maxinteger' if t[maxinteger] is present.) -** (In the next explanation, we use Lua indices, that is, with base 1. -** The code itself uses base 0 when indexing the array part of the table.) -** The code starts with 'limit', a position in the array part that may -** be a boundary. -** (1) If 't[limit]' is empty, there must be a boundary before it. -** As a common case (e.g., after 't[#t]=nil'), check whether 'hint-1' -** is present. If so, it is a boundary. Otherwise, do a binary search -** between 0 and limit to find a boundary. In both cases, try to -** use this boundary as the new 'limit', as a hint for the next call. -** (2) If 't[limit]' is not empty and the array has more elements -** after 'limit', try to find a boundary there. Again, try first -** the special case (which should be quite frequent) where 'limit+1' -** is empty, so that 'limit' is a boundary. Otherwise, check the -** last element of the array part (set it as a new limit). If it is empty, -** there must be a boundary between the old limit (present) and the new -** limit (absent), which is found with a binary search. (This boundary -** always can be a new limit.) -** (3) The last case is when there are no elements in the array part -** (limit == 0) or its last element (the new limit) is present. -** In this case, must check the hash part. If there is no hash part, -** the boundary is 0. Otherwise, if 'limit+1' is absent, 'limit' is -** a boundary. Finally, if 'limit+1' is present, call 'hash_search' -** to find a boundary in the hash part of the table. (In those -** cases, the boundary is not inside the array part, and therefore -** cannot be used as a new limit.) -*/ -lua_Unsigned luaH_getn (Table *t) { - unsigned int limit = t->alimit; - if (limit > 0 && isempty(&t->array[limit - 1])) { - /* (1) there must be a boundary before 'limit' */ - if (limit >= 2 && !isempty(&t->array[limit - 2])) { - /* 'limit - 1' is a boundary; can it be a new limit? */ - if (ispow2realasize(t) && !ispow2(limit - 1)) { - t->alimit = limit - 1; - setnorealasize(t); - } - return limit - 1; - } - else { /* must search for a boundary in [0, limit] */ - unsigned int boundary = binsearch(t->array, 0, limit); - /* can this boundary represent the real size of the array? */ - if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) { - t->alimit = boundary; /* use it as the new limit */ - setnorealasize(t); +** Try to find a border in table 't'. (A 'border' is an integer index +** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent, +** or 'maxinteger' if t[maxinteger] is present.) +** If there is an array part, try to find a border there. First try +** to find it in the vicinity of the previous result (hint), to handle +** cases like 't[#t + 1] = val' or 't[#t] = nil', that move the border +** by one entry. Otherwise, do a binary search to find the border. +** If there is no array part, or its last element is non empty, the +** border may be in the hash part. +*/ +lua_Unsigned luaH_getn (lua_State *L, Table *t) { + unsigned asize = t->asize; + if (asize > 0) { /* is there an array part? */ + const unsigned maxvicinity = 4; + unsigned limit = *lenhint(t); /* start with the hint */ + if (limit == 0) + limit = 1; /* make limit a valid index in the array */ + if (arraykeyisempty(t, limit)) { /* t[limit] empty? */ + /* there must be a border before 'limit' */ + unsigned i; + /* look for a border in the vicinity of the hint */ + for (i = 0; i < maxvicinity && limit > 1; i++) { + limit--; + if (!arraykeyisempty(t, limit)) + return newhint(t, limit); /* 'limit' is a border */ } - return boundary; + /* t[limit] still empty; search for a border in [0, limit) */ + return newhint(t, binsearch(t, 0, limit)); } - } - /* 'limit' is zero or present in table */ - if (!limitequalsasize(t)) { - /* (2) 'limit' > 0 and array has more elements after 'limit' */ - if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */ - return limit; /* this is the boundary */ - /* else, try last element in the array */ - limit = luaH_realasize(t); - if (isempty(&t->array[limit - 1])) { /* empty? */ - /* there must be a boundary in the array after old limit, - and it must be a valid new limit */ - unsigned int boundary = binsearch(t->array, t->alimit, limit); - t->alimit = boundary; - return boundary; + else { /* 'limit' is present in table; look for a border after it */ + unsigned i; + /* look for a border in the vicinity of the hint */ + for (i = 0; i < maxvicinity && limit < asize; i++) { + limit++; + if (arraykeyisempty(t, limit)) + return newhint(t, limit - 1); /* 'limit - 1' is a border */ + } + if (arraykeyisempty(t, asize)) { /* last element empty? */ + /* t[limit] not empty; search for a border in [limit, asize) */ + return newhint(t, binsearch(t, limit, asize)); + } } - /* else, new limit is present in the table; check the hash part */ + /* last element non empty; set a hint to speed up finding that again */ + /* (keys in the hash part cannot be hints) */ + *lenhint(t) = asize; } - /* (3) 'limit' is the last element and either is zero or present in table */ - lua_assert(limit == luaH_realasize(t) && - (limit == 0 || !isempty(&t->array[limit - 1]))); - if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1)))) - return limit; /* 'limit + 1' is absent... */ - else /* 'limit + 1' is also present */ - return hash_search(t, limit); + /* no array part or t[asize] is not empty; check the hash part */ + lua_assert(asize == 0 || !arraykeyisempty(t, asize)); + if (isdummy(t) || hashkeyisempty(t, asize + 1)) + return asize; /* 'asize + 1' is empty */ + else /* 'asize + 1' is also non empty */ + return hash_search(L, t, asize); } #if defined(LUA_DEBUG) +/* export this function for the test library */ + Node *luaH_mainposition (const Table *t, const TValue *key) { return mainpositionTV(t, key); } -int luaH_isdummy (const Table *t) { return isdummy(t); } - #endif diff --git a/ltable.h b/ltable.h index 9565833f64..f3b7bc7e7e 100644 --- a/ltable.h +++ b/ltable.h @@ -15,11 +15,26 @@ #define gnext(n) ((n)->u.next) -#define invalidateTMcache(t) ((t)->flags = 0) +/* +** Clear all bits of fast-access metamethods, which means that the table +** may have any of these metamethods. (First access that fails after the +** clearing will set the bit again.) +*/ +#define invalidateTMcache(t) ((t)->flags &= cast_byte(~maskflags)) + + +/* +** Bit BITDUMMY set in 'flags' means the table is using the dummy node +** for its hash part. +*/ +#define BITDUMMY (1 << 6) +#define NOTBITDUMMY cast_byte(~BITDUMMY) +#define isdummy(t) ((t)->flags & BITDUMMY) + +#define setnodummy(t) ((t)->flags &= NOTBITDUMMY) +#define setdummy(t) ((t)->flags |= BITDUMMY) -/* true when 't' is using 'dummynode' as its hash part */ -#define isdummy(t) ((t)->lastfree == NULL) /* allocated size for hash nodes */ @@ -27,30 +42,142 @@ /* returns the Node, given the value of a table entry */ -#define nodefromval(v) cast(Node *, (v)) +#define nodefromval(v) cast(Node *, (v)) + + + +#define luaH_fastgeti(t,k,res,tag) \ + { Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \ + if ((u < h->asize)) { \ + tag = *getArrTag(h, u); \ + if (!tagisempty(tag)) { farr2val(h, u, tag, res); }} \ + else { tag = luaH_getint(h, (k), res); }} + + +#define luaH_fastseti(t,k,val,hres) \ + { Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \ + if ((u < h->asize)) { \ + lu_byte *tag = getArrTag(h, u); \ + if (checknoTM(h->metatable, TM_NEWINDEX) || !tagisempty(*tag)) \ + { fval2arr(h, u, tag, val); hres = HOK; } \ + else hres = ~cast_int(u); } \ + else { hres = luaH_psetint(h, k, val); }} + + +/* results from pset */ +#define HOK 0 +#define HNOTFOUND 1 +#define HNOTATABLE 2 +#define HFIRSTNODE 3 + +/* +** 'luaH_get*' operations set 'res', unless the value is absent, and +** return the tag of the result. +** The 'luaH_pset*' (pre-set) operations set the given value and return +** HOK, unless the original value is absent. In that case, if the key +** is really absent, they return HNOTFOUND. Otherwise, if there is a +** slot with that key but with no value, 'luaH_pset*' return an encoding +** of where the key is (usually called 'hres'). (pset cannot set that +** value because there might be a metamethod.) If the slot is in the +** hash part, the encoding is (HFIRSTNODE + hash index); if the slot is +** in the array part, the encoding is (~array index), a negative value. +** The value HNOTATABLE is used by the fast macros to signal that the +** value being indexed is not a table. +** (The size for the array part is limited by the maximum power of two +** that fits in an unsigned integer; that is INT_MAX+1. So, the C-index +** ranges from 0, which encodes to -1, to INT_MAX, which encodes to +** INT_MIN. The size of the hash part is limited by the maximum power of +** two that fits in a signed integer; that is (INT_MAX+1)/2. So, it is +** safe to add HFIRSTNODE to any index there.) +*/ + + +/* +** The array part of a table is represented by an inverted array of +** values followed by an array of tags, to avoid wasting space with +** padding. In between them there is an unsigned int, explained later. +** The 'array' pointer points between the two arrays, so that values are +** indexed with negative indices and tags with non-negative indices. + + Values Tags + -------------------------------------------------------- + ... | Value 1 | Value 0 |unsigned|0|1|... + -------------------------------------------------------- + ^ t->array + +** All accesses to 't->array' should be through the macros 'getArrTag' +** and 'getArrVal'. +*/ +/* Computes the address of the tag for the abstract C-index 'k' */ +#define getArrTag(t,k) (cast(lu_byte*, (t)->array) + sizeof(unsigned) + (k)) + +/* Computes the address of the value for the abstract C-index 'k' */ +#define getArrVal(t,k) ((t)->array - 1 - (k)) + + +/* +** The unsigned between the two arrays is used as a hint for #t; +** see luaH_getn. It is stored there to avoid wasting space in +** the structure Table for tables with no array part. +*/ +#define lenhint(t) cast(unsigned*, (t)->array) + + +/* +** Move TValues to/from arrays, using C indices +*/ +#define arr2obj(h,k,val) \ + ((val)->tt_ = *getArrTag(h,(k)), (val)->value_ = *getArrVal(h,(k))) + +#define obj2arr(h,k,val) \ + (*getArrTag(h,(k)) = (val)->tt_, *getArrVal(h,(k)) = (val)->value_) + + +/* +** Often, we need to check the tag of a value before moving it. The +** following macros also move TValues to/from arrays, but receive the +** precomputed tag value or address as an extra argument. +*/ +#define farr2val(h,k,tag,res) \ + ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,(k))) + +#define fval2arr(h,k,tag,val) \ + (*tag = (val)->tt_, *getArrVal(h,(k)) = (val)->value_) + + +LUAI_FUNC lu_byte luaH_get (Table *t, const TValue *key, TValue *res); +LUAI_FUNC lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res); +LUAI_FUNC lu_byte luaH_getstr (Table *t, TString *key, TValue *res); +LUAI_FUNC lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res); + +/* Special get for metamethods */ +LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key); + +LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val); +LUAI_FUNC int luaH_psetshortstr (Table *t, TString *key, TValue *val); +LUAI_FUNC int luaH_psetstr (Table *t, TString *key, TValue *val); +LUAI_FUNC int luaH_pset (Table *t, const TValue *key, TValue *val); -LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value); -LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); -LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); -LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); -LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); -LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); +LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, + TValue *value); + +LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, + TValue *value, int hres); LUAI_FUNC Table *luaH_new (lua_State *L); -LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, - unsigned int nhsize); -LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); +LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned nasize, + unsigned nhsize); +LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned nasize); +LUAI_FUNC lu_mem luaH_size (Table *t); LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); -LUAI_FUNC lua_Unsigned luaH_getn (Table *t); -LUAI_FUNC unsigned int luaH_realasize (const Table *t); +LUAI_FUNC lua_Unsigned luaH_getn (lua_State *L, Table *t); #if defined(LUA_DEBUG) LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); -LUAI_FUNC int luaH_isdummy (const Table *t); #endif diff --git a/ltablib.c b/ltablib.c index 29c53e9483..46ecb5e024 100644 --- a/ltablib.c +++ b/ltablib.c @@ -18,6 +18,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* @@ -58,9 +59,20 @@ static void checktab (lua_State *L, int arg, int what) { } +static int tcreate (lua_State *L) { + lua_Unsigned sizeseq = (lua_Unsigned)luaL_checkinteger(L, 1); + lua_Unsigned sizerest = (lua_Unsigned)luaL_optinteger(L, 2, 0); + luaL_argcheck(L, sizeseq <= cast_uint(INT_MAX), 1, "out of range"); + luaL_argcheck(L, sizerest <= cast_uint(INT_MAX), 2, "out of range"); + lua_createtable(L, cast_int(sizeseq), cast_int(sizerest)); + return 1; +} + + static int tinsert (lua_State *L) { - lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ lua_Integer pos; /* where to insert new element */ + lua_Integer e = aux_getn(L, 1, TAB_RW); + e = luaL_intop(+, e, 1); /* first empty element */ switch (lua_gettop(L)) { case 2: { /* called with only 2 arguments */ pos = e; /* insert new element at the end */ @@ -69,7 +81,9 @@ static int tinsert (lua_State *L) { case 3: { lua_Integer i; pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ - luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); + /* check whether 'pos' is in [1, e] */ + luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2, + "position out of bounds"); for (i = e; i > pos; i--) { /* move up elements */ lua_geti(L, 1, i - 1); lua_seti(L, 1, i); /* t[i] = t[i - 1] */ @@ -89,7 +103,9 @@ static int tremove (lua_State *L) { lua_Integer size = aux_getn(L, 1, TAB_RW); lua_Integer pos = luaL_optinteger(L, 2, size); if (pos != size) /* validate 'pos' if given */ - luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); + /* check whether 'pos' is in [1, size + 1] */ + luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 2, + "position out of bounds"); lua_geti(L, 1, pos); /* result = t[pos] */ for ( ; pos < size; pos++) { lua_geti(L, 1, pos + 1); @@ -141,9 +157,9 @@ static int tmove (lua_State *L) { static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { lua_geti(L, 1, i); - if (!lua_isstring(L, -1)) - luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", - luaL_typename(L, -1), i); + if (l_unlikely(!lua_isstring(L, -1))) + luaL_error(L, "invalid value (%s) at index %I in table for 'concat'", + luaL_typename(L, -1), (LUAI_UACINT)i); luaL_addvalue(b); } @@ -191,8 +207,9 @@ static int tunpack (lua_State *L) { lua_Integer i = luaL_optinteger(L, 2, 1); lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); if (i > e) return 0; /* empty range */ - n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ - if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) + n = l_castS2U(e) - l_castS2U(i); /* number of elements minus 1 */ + if (l_unlikely(n >= (unsigned int)INT_MAX || + !lua_checkstack(L, (int)(++n)))) return luaL_error(L, "too many results to unpack"); for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ lua_geti(L, 1, i); @@ -214,41 +231,26 @@ static int tunpack (lua_State *L) { */ -/* type for array indices */ +/* +** Type for array indices. These indices are always limited by INT_MAX, +** so it is safe to cast them to lua_Integer even for Lua 32 bits. +*/ typedef unsigned int IdxT; +/* Versions of lua_seti/lua_geti specialized for IdxT */ +#define geti(L,idt,idx) lua_geti(L, idt, l_castU2S(idx)) +#define seti(L,idt,idx) lua_seti(L, idt, l_castU2S(idx)) + + /* ** Produce a "random" 'unsigned int' to randomize pivot choice. This ** macro is used only when 'sort' detects a big imbalance in the result ** of a partition. (If you don't want/need this "randomness", ~0 is a ** good choice.) */ -#if !defined(l_randomizePivot) /* { */ - -#include - -/* size of 'e' measured in number of 'unsigned int's */ -#define sof(e) (sizeof(e) / sizeof(unsigned int)) - -/* -** Use 'time' and 'clock' as sources of "randomness". Because we don't -** know the types 'clock_t' and 'time_t', we cannot cast them to -** anything without risking overflows. A safe way to use their values -** is to copy them to an array of a known type and use the array values. -*/ -static unsigned int l_randomizePivot (void) { - clock_t c = clock(); - time_t t = time(NULL); - unsigned int buff[sof(c) + sof(t)]; - unsigned int i, rnd = 0; - memcpy(buff, &c, sof(c) * sizeof(unsigned int)); - memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int)); - for (i = 0; i < sof(buff); i++) - rnd += buff[i]; - return rnd; -} - +#if !defined(l_randomizePivot) +#define l_randomizePivot(L) luaL_makeseed(L) #endif /* } */ @@ -257,8 +259,8 @@ static unsigned int l_randomizePivot (void) { static void set2 (lua_State *L, IdxT i, IdxT j) { - lua_seti(L, 1, i); - lua_seti(L, 1, j); + seti(L, 1, i); + seti(L, 1, j); } @@ -295,15 +297,15 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ for (;;) { /* next loop: repeat ++i while a[i] < P */ - while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { - if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */ + while ((void)geti(L, 1, ++i), sort_comp(L, -1, -2)) { + if (l_unlikely(i == up - 1)) /* a[up - 1] < P == a[up - 1] */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[i] */ } - /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ + /* after the loop, a[i] >= P and a[lo .. i - 1] < P (a) */ /* next loop: repeat --j while P < a[j] */ - while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { - if (j < i) /* j < i but a[j] > P ?? */ + while ((void)geti(L, 1, --j), sort_comp(L, -3, -1)) { + if (l_unlikely(j < i)) /* j <= i - 1 and a[j] > P, contradicts (a) */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[j] */ } @@ -327,23 +329,22 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { */ static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { IdxT r4 = (up - lo) / 4; /* range/4 */ - IdxT p = rnd % (r4 * 2) + (lo + r4); + IdxT p = (rnd ^ lo ^ up) % (r4 * 2) + (lo + r4); lua_assert(lo + r4 <= p && p <= up - r4); return p; } /* -** QuickSort algorithm (recursive function) +** Quicksort algorithm (recursive function) */ -static void auxsort (lua_State *L, IdxT lo, IdxT up, - unsigned int rnd) { +static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) { while (lo < up) { /* loop for tail recursion */ IdxT p; /* Pivot index */ IdxT n; /* to be used later */ /* sort elements 'lo', 'p', and 'up' */ - lua_geti(L, 1, lo); - lua_geti(L, 1, up); + geti(L, 1, lo); + geti(L, 1, up); if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ set2(L, lo, up); /* swap a[lo] - a[up] */ else @@ -354,13 +355,13 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, p = (lo + up)/2; /* middle element is a good pivot */ else /* for larger intervals, it is worth a random pivot */ p = choosePivot(lo, up, rnd); - lua_geti(L, 1, p); - lua_geti(L, 1, lo); + geti(L, 1, p); + geti(L, 1, lo); if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ set2(L, p, lo); /* swap a[p] - a[lo] */ else { lua_pop(L, 1); /* remove a[lo] */ - lua_geti(L, 1, up); + geti(L, 1, up); if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ set2(L, p, up); /* swap a[up] - a[p] */ else @@ -368,9 +369,9 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, } if (up - lo == 2) /* only 3 elements? */ return; /* already sorted */ - lua_geti(L, 1, p); /* get middle element (Pivot) */ + geti(L, 1, p); /* get middle element (Pivot) */ lua_pushvalue(L, -1); /* push Pivot */ - lua_geti(L, 1, up - 1); /* push a[up - 1] */ + geti(L, 1, up - 1); /* push a[up - 1] */ set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ p = partition(L, lo, up); /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ @@ -385,7 +386,7 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ } if ((up - lo) / 128 > n) /* partition too imbalanced? */ - rnd = l_randomizePivot(); /* try a new randomization */ + rnd = l_randomizePivot(L); /* try a new randomization */ } /* tail call auxsort(L, lo, up, rnd) */ } @@ -407,6 +408,7 @@ static int sort (lua_State *L) { static const luaL_Reg tab_funcs[] = { {"concat", tconcat}, + {"create", tcreate}, {"insert", tinsert}, {"pack", tpack}, {"unpack", tunpack}, diff --git a/ltests.c b/ltests.c index 40de22927b..c4905f9487 100644 --- a/ltests.c +++ b/ltests.c @@ -44,27 +44,28 @@ void *l_Trick = 0; -#define obj_at(L,k) s2v(L->ci->func + (k)) +#define obj_at(L,k) s2v(L->ci->func.p + (k)) static int runC (lua_State *L, lua_State *L1, const char *pc); static void setnameval (lua_State *L, const char *name, int val) { - lua_pushstring(L, name); lua_pushinteger(L, val); - lua_settable(L, -3); + lua_setfield(L, -2, name); } static void pushobject (lua_State *L, const TValue *o) { - setobj2s(L, L->top, o); + setobj2s(L, L->top.p, o); api_incr_top(L); } -static void badexit (const char *fmt, const char *s) { - fprintf(stderr, fmt, s); +static void badexit (const char *fmt, const char *s1, const char *s2) { + fprintf(stderr, fmt, s1); + if (s2) + fprintf(stderr, "extra info: %s\n", s2); /* avoid assertion failures when exiting */ l_memcontrol.numblocks = l_memcontrol.total = 0; exit(EXIT_FAILURE); @@ -72,40 +73,84 @@ static void badexit (const char *fmt, const char *s) { static int tpanic (lua_State *L) { + const char *msg = (lua_type(L, -1) == LUA_TSTRING) + ? lua_tostring(L, -1) + : "error object is not a string"; return (badexit("PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)), + msg, NULL), 0); /* do not return to Lua */ } /* -** Warning function for tests. Fist, it concatenates all parts of -** a warning in buffer 'buff'. Then: -** - messages starting with '#' are shown on standard output (used to -** test explicit warnings); -** - messages containing '@' are stored in global '_WARN' (used to test -** errors that generate warnings); +** Warning function for tests. First, it concatenates all parts of +** a warning in buffer 'buff'. Then, it has three modes: +** - 0.normal: messages starting with '#' are shown on standard output; ** - other messages abort the tests (they represent real warning ** conditions; the standard tests should not generate these conditions -** unexpectedly). +** unexpectedly); +** - 1.allow: all messages are shown; +** - 2.store: all warnings go to the global '_WARN'; */ static void warnf (void *ud, const char *msg, int tocont) { + lua_State *L = cast(lua_State *, ud); static char buff[200] = ""; /* should be enough for tests... */ + static int onoff = 0; + static int mode = 0; /* start in normal mode */ + static int lasttocont = 0; + if (!lasttocont && !tocont && *msg == '@') { /* control message? */ + if (buff[0] != '\0') + badexit("Control warning during warning: %s\naborting...\n", msg, buff); + if (strcmp(msg, "@off") == 0) + onoff = 0; + else if (strcmp(msg, "@on") == 0) + onoff = 1; + else if (strcmp(msg, "@normal") == 0) + mode = 0; + else if (strcmp(msg, "@allow") == 0) + mode = 1; + else if (strcmp(msg, "@store") == 0) + mode = 2; + else + badexit("Invalid control warning in test mode: %s\naborting...\n", + msg, NULL); + return; + } + lasttocont = tocont; if (strlen(msg) >= sizeof(buff) - strlen(buff)) - badexit("%s", "warnf-buffer overflow"); + badexit("warnf-buffer overflow (%s)\n", msg, buff); strcat(buff, msg); /* add new message to current warning */ if (!tocont) { /* message finished? */ - if (buff[0] == '#') /* expected warning? */ - printf("Expected Lua warning: %s\n", buff); /* print it */ - else if (strchr(buff, '@') != NULL) { /* warning for test purposes? */ - lua_State *L = cast(lua_State *, ud); - lua_unlock(L); - lua_pushstring(L, buff); - lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ - lua_lock(L); - } - else /* a real warning; should not happen during tests */ - badexit("Unexpected warning in test mode: %s\naborting...\n", buff); + lua_unlock(L); + luaL_checkstack(L, 1, "warn stack space"); + lua_getglobal(L, "_WARN"); + if (!lua_toboolean(L, -1)) + lua_pop(L, 1); /* ok, no previous unexpected warning */ + else { + badexit("Unhandled warning in store mode: %s\naborting...\n", + lua_tostring(L, -1), buff); + } + lua_lock(L); + switch (mode) { + case 0: { /* normal */ + if (buff[0] != '#' && onoff) /* unexpected warning? */ + badexit("Unexpected warning in test mode: %s\naborting...\n", + buff, NULL); + } /* FALLTHROUGH */ + case 1: { /* allow */ + if (onoff) + fprintf(stderr, "Lua warning: %s\n", buff); /* print warning */ + break; + } + case 2: { /* store */ + lua_unlock(L); + luaL_checkstack(L, 1, "warn stack space"); + lua_pushstring(L, buff); + lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ + lua_lock(L); + break; + } + } buff[0] = '\0'; /* prepare buffer for next warning */ } } @@ -119,13 +164,13 @@ static void warnf (void *ud, const char *msg, int tocont) { #define MARK 0x55 /* 01010101 (a nice pattern) */ -typedef union Header { +typedef union memHeader { LUAI_MAXALIGN; struct { size_t size; int type; } d; -} Header; +} memHeader; #if !defined(EXTERNMEMCHECK) @@ -144,17 +189,18 @@ typedef union Header { Memcontrol l_memcontrol = - {0UL, 0UL, 0UL, 0UL, (~0UL), {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}}; + {0, 0UL, 0UL, 0UL, 0UL, (~0UL), + {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}}; -static void freeblock (Memcontrol *mc, Header *block) { +static void freeblock (Memcontrol *mc, memHeader *block) { if (block) { size_t size = block->d.size; int i; for (i = 0; i < MARKSIZE; i++) /* check marks after block */ lua_assert(*(cast_charp(block + 1) + size + i) == MARK); mc->objcount[block->d.type]--; - fillmem(block, sizeof(Header) + size + MARKSIZE); /* erase block */ + fillmem(block, sizeof(memHeader) + size + MARKSIZE); /* erase block */ free(block); /* actually free block */ mc->numblocks--; /* update counts */ mc->total -= size; @@ -164,14 +210,14 @@ static void freeblock (Memcontrol *mc, Header *block) { void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { Memcontrol *mc = cast(Memcontrol *, ud); - Header *block = cast(Header *, b); + memHeader *block = cast(memHeader *, b); int type; if (mc->memlimit == 0) { /* first time? */ char *limit = getenv("MEMLIMIT"); /* initialize memory limit */ mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX; } if (block == NULL) { - type = (oldsize < LUA_NUMTAGS) ? oldsize : 0; + type = (oldsize < LUA_NUMTYPES) ? cast_int(oldsize) : 0; oldsize = 0; } else { @@ -183,6 +229,10 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { freeblock(mc, block); return NULL; } + if (mc->failnext) { + mc->failnext = 0; + return NULL; /* fake a single memory allocation error */ + } if (mc->countlimit != ~0UL && size != oldsize) { /* count limit in use? */ if (mc->countlimit == 0) return NULL; /* fake a memory allocation error */ @@ -191,12 +241,12 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { if (size > oldsize && mc->total+size-oldsize > mc->memlimit) return NULL; /* fake a memory allocation error */ else { - Header *newblock; + memHeader *newblock; int i; size_t commonsize = (oldsize < size) ? oldsize : size; - size_t realsize = sizeof(Header) + size + MARKSIZE; + size_t realsize = sizeof(memHeader) + size + MARKSIZE; if (realsize < size) return NULL; /* arithmetic overflow! */ - newblock = cast(Header *, malloc(realsize)); /* alloc a new block */ + newblock = cast(memHeader *, malloc(realsize)); /* alloc a new block */ if (newblock == NULL) return NULL; /* really out of memory? */ if (block) { @@ -225,11 +275,15 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { /* -** {====================================================== -** Functions to check memory consistency -** ======================================================= +** {===================================================================== +** Functions to check memory consistency. +** Most of these checks are done through asserts, so this code does +** not make sense with asserts off. For this reason, it uses 'assert' +** directly, instead of 'lua_assert'. +** ====================================================================== */ +#include /* ** Check GC invariants. For incremental mode, a black object cannot @@ -244,13 +298,13 @@ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) { if (isdead(g,t)) return 0; if (issweepphase(g)) return 1; /* no invariants */ - else if (g->gckind == KGC_INC) + else if (g->gckind != KGC_GENMINOR) return !(isblack(f) && iswhite(t)); /* basic incremental invariant */ else { /* generational mode */ if ((getage(f) == G_OLD && isblack(f)) && !isold(t)) return 0; - if (((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) && isblack(f)) && - getage(t) == G_NEW) + if ((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) && + getage(t) == G_NEW) return 0; return 1; } @@ -262,11 +316,55 @@ static void printobj (global_State *g, GCObject *o) { ttypename(novariant(o->tt)), (void *)o, isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g', "ns01oTt"[getage(o)], o->marked); - if (o->tt == LUA_TSHRSTR || o->tt == LUA_TLNGSTR) + if (o->tt == LUA_VSHRSTR || o->tt == LUA_VLNGSTR) printf(" '%s'", getstr(gco2ts(o))); } +void lua_printobj (lua_State *L, struct GCObject *o) { + printobj(G(L), o); +} + + +void lua_printvalue (TValue *v) { + switch (ttypetag(v)) { + case LUA_VNUMINT: case LUA_VNUMFLT: { + char buff[LUA_N2SBUFFSZ]; + unsigned len = luaO_tostringbuff(v, buff); + buff[len] = '\0'; + printf("%s", buff); + break; + } + case LUA_VSHRSTR: + printf("'%s'", getstr(tsvalue(v))); break; + case LUA_VLNGSTR: + printf("'%.30s...'", getstr(tsvalue(v))); break; + case LUA_VFALSE: + printf("%s", "false"); break; + case LUA_VTRUE: + printf("%s", "true"); break; + case LUA_VLIGHTUSERDATA: + printf("light udata: %p", pvalue(v)); break; + case LUA_VUSERDATA: + printf("full udata: %p", uvalue(v)); break; + case LUA_VNIL: + printf("nil"); break; + case LUA_VLCF: + printf("light C function: %p", fvalue(v)); break; + case LUA_VCCL: + printf("C closure: %p", clCvalue(v)); break; + case LUA_VLCL: + printf("Lua function: %p", clLvalue(v)); break; + case LUA_VTHREAD: + printf("thread: %p", thvalue(v)); break; + case LUA_VTABLE: + printf("table: %p", hvalue(v)); break; + default: + lua_assert(0); + } +} + + static int testobjref (global_State *g, GCObject *f, GCObject *t) { int r1 = testobjref1(g, f, t); if (!r1) { @@ -279,29 +377,42 @@ static int testobjref (global_State *g, GCObject *f, GCObject *t) { return r1; } -#define checkobjref(g,f,t) \ - { if (t) lua_longassert(testobjref(g,f,obj2gco(t))); } + +static void checkobjref (global_State *g, GCObject *f, GCObject *t) { + assert(testobjref(g, f, t)); +} + + +/* +** Version where 't' can be NULL. In that case, it should not apply the +** macro 'obj2gco' over the object. ('t' may have several types, so this +** definition must be a macro.) Most checks need this version, because +** the check may run while an object is still being created. +*/ +#define checkobjrefN(g,f,t) { if (t) checkobjref(g,f,obj2gco(t)); } static void checkvalref (global_State *g, GCObject *f, const TValue *t) { - lua_assert(!iscollectable(t) || - (righttt(t) && testobjref(g, f, gcvalue(t)))); + assert(!iscollectable(t) || (righttt(t) && testobjref(g, f, gcvalue(t)))); } static void checktable (global_State *g, Table *h) { unsigned int i; - unsigned int asize = luaH_realasize(h); + unsigned int asize = h->asize; Node *n, *limit = gnode(h, sizenode(h)); GCObject *hgc = obj2gco(h); - checkobjref(g, hgc, h->metatable); - for (i = 0; i < asize; i++) - checkvalref(g, hgc, &h->array[i]); + checkobjrefN(g, hgc, h->metatable); + for (i = 0; i < asize; i++) { + TValue aux; + arr2obj(h, i, &aux); + checkvalref(g, hgc, &aux); + } for (n = gnode(h, 0); n < limit; n++) { if (!isempty(gval(n))) { TValue k; - getnodekey(g->mainthread, &k, n); - lua_assert(!keyisnil(n)); + getnodekey(mainthread(g), &k, n); + assert(!keyisnil(n)); checkvalref(g, hgc, &k); checkvalref(g, hgc, gval(n)); } @@ -312,30 +423,26 @@ static void checktable (global_State *g, Table *h) { static void checkudata (global_State *g, Udata *u) { int i; GCObject *hgc = obj2gco(u); - checkobjref(g, hgc, u->metatable); + checkobjrefN(g, hgc, u->metatable); for (i = 0; i < u->nuvalue; i++) checkvalref(g, hgc, &u->uv[i].uv); } -/* -** All marks are conditional because a GC may happen while the -** prototype is still being created -*/ static void checkproto (global_State *g, Proto *f) { int i; GCObject *fgc = obj2gco(f); - checkobjref(g, fgc, f->source); + checkobjrefN(g, fgc, f->source); for (i=0; isizek; i++) { - if (ttisstring(f->k + i)) - checkobjref(g, fgc, tsvalue(f->k + i)); + if (iscollectable(f->k + i)) + checkobjref(g, fgc, gcvalue(f->k + i)); } for (i=0; isizeupvalues; i++) - checkobjref(g, fgc, f->upvalues[i].name); + checkobjrefN(g, fgc, f->upvalues[i].name); for (i=0; isizep; i++) - checkobjref(g, fgc, f->p[i]); + checkobjrefN(g, fgc, f->p[i]); for (i=0; isizelocvars; i++) - checkobjref(g, fgc, f->locvars[i].varname); + checkobjrefN(g, fgc, f->locvars[i].varname); } @@ -350,13 +457,13 @@ static void checkCclosure (global_State *g, CClosure *cl) { static void checkLclosure (global_State *g, LClosure *cl) { GCObject *clgc = obj2gco(cl); int i; - checkobjref(g, clgc, cl->p); + checkobjrefN(g, clgc, cl->p); for (i=0; inupvalues; i++) { UpVal *uv = cl->upvals[i]; if (uv) { - checkobjref(g, clgc, uv); + checkobjrefN(g, clgc, uv); if (!upisopen(uv)) - checkvalref(g, obj2gco(uv), uv->v); + checkvalref(g, obj2gco(uv), uv->v.p); } } } @@ -365,7 +472,7 @@ static void checkLclosure (global_State *g, LClosure *cl) { static int lua_checkpc (CallInfo *ci) { if (!isLua(ci)) return 1; else { - StkId f = ci->func; + StkId f = ci->func.p; Proto *p = clLvalue(s2v(f))->p; return p->code <= ci->u.l.savedpc && ci->u.l.savedpc <= p->code + p->sizecode; @@ -373,62 +480,64 @@ static int lua_checkpc (CallInfo *ci) { } -static void checkstack (global_State *g, lua_State *L1) { +static void check_stack (global_State *g, lua_State *L1) { StkId o; CallInfo *ci; UpVal *uv; - lua_assert(!isdead(g, L1)); + assert(!isdead(g, L1)); + if (L1->stack.p == NULL) { /* incomplete thread? */ + assert(L1->openupval == NULL && L1->ci == NULL); + return; + } for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) - lua_assert(upisopen(uv)); /* must be open */ + assert(upisopen(uv)); /* must be open */ + assert(L1->top.p <= L1->stack_last.p); + assert(L1->tbclist.p <= L1->top.p); for (ci = L1->ci; ci != NULL; ci = ci->previous) { - lua_assert(ci->top <= L1->stack_last); - lua_assert(lua_checkpc(ci)); - } - if (L1->stack) { /* complete thread? */ - for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++) - checkliveness(L1, s2v(o)); /* entire stack must have valid values */ + assert(ci->top.p <= L1->stack_last.p); + assert(lua_checkpc(ci)); } - else lua_assert(L1->stacksize == 0); + for (o = L1->stack.p; o < L1->stack_last.p; o++) + checkliveness(L1, s2v(o)); /* entire stack must have valid values */ } static void checkrefs (global_State *g, GCObject *o) { switch (o->tt) { - case LUA_TUSERDATA: { + case LUA_VUSERDATA: { checkudata(g, gco2u(o)); break; } - case LUA_TUPVAL: - case LUA_TUPVALTBC: { - checkvalref(g, o, gco2upv(o)->v); + case LUA_VUPVAL: { + checkvalref(g, o, gco2upv(o)->v.p); break; } - case LUA_TTABLE: { + case LUA_VTABLE: { checktable(g, gco2t(o)); break; } - case LUA_TTHREAD: { - checkstack(g, gco2th(o)); + case LUA_VTHREAD: { + check_stack(g, gco2th(o)); break; } - case LUA_TLCL: { + case LUA_VLCL: { checkLclosure(g, gco2lcl(o)); break; } - case LUA_TCCL: { + case LUA_VCCL: { checkCclosure(g, gco2ccl(o)); break; } - case LUA_TPROTO: { + case LUA_VPROTO: { checkproto(g, gco2p(o)); break; } - case LUA_TSHRSTR: - case LUA_TLNGSTR: { - lua_assert(!isgray(o)); /* strings are never gray */ + case LUA_VSHRSTR: + case LUA_VLNGSTR: { + assert(!isgray(o)); /* strings are never gray */ break; } - default: lua_assert(0); + default: assert(0); } } @@ -436,83 +545,126 @@ static void checkrefs (global_State *g, GCObject *o) { /* ** Check consistency of an object: ** - Dead objects can only happen in the 'allgc' list during a sweep -** phase (controled by the caller through 'maybedead'). +** phase (controlled by the caller through 'maybedead'). ** - During pause, all objects must be white. ** - In generational mode: ** * objects must be old enough for their lists ('listage'). ** * old objects cannot be white. ** * old objects must be black, except for 'touched1', 'old0', -** threads, and open upvalues. +** threads, and open upvalues. +** * 'touched1' objects must be gray. */ static void checkobject (global_State *g, GCObject *o, int maybedead, int listage) { if (isdead(g, o)) - lua_assert(maybedead); + assert(maybedead); else { - lua_assert(g->gcstate != GCSpause || iswhite(o)); - if (g->gckind == KGC_GEN) { /* generational mode? */ - lua_assert(getage(o) >= listage); - lua_assert(!iswhite(o) || !isold(o)); + assert(g->gcstate != GCSpause || iswhite(o)); + if (g->gckind == KGC_GENMINOR) { /* generational mode? */ + assert(getage(o) >= listage); if (isold(o)) { - lua_assert(isblack(o) || + assert(!iswhite(o)); + assert(isblack(o) || getage(o) == G_TOUCHED1 || getage(o) == G_OLD0 || - o->tt == LUA_TTHREAD || - (o->tt == LUA_TUPVAL && upisopen(gco2upv(o)))); + o->tt == LUA_VTHREAD || + (o->tt == LUA_VUPVAL && upisopen(gco2upv(o)))); } + assert(getage(o) != G_TOUCHED1 || isgray(o)); } checkrefs(g, o); } } -static void checkgraylist (global_State *g, GCObject *o) { - ((void)g); /* better to keep it available if we need to print an object */ +static l_mem checkgraylist (global_State *g, GCObject *o) { + int total = 0; /* count number of elements in the list */ + cast_void(g); /* better to keep it if we need to print an object */ while (o) { - lua_assert(isgray(o) || getage(o) == G_TOUCHED2); + assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2)); + assert(!testbit(o->marked, TESTBIT)); + if (keepinvariant(g)) + l_setbit(o->marked, TESTBIT); /* mark that object is in a gray list */ + total++; switch (o->tt) { - case LUA_TTABLE: o = gco2t(o)->gclist; break; - case LUA_TLCL: o = gco2lcl(o)->gclist; break; - case LUA_TCCL: o = gco2ccl(o)->gclist; break; - case LUA_TTHREAD: o = gco2th(o)->gclist; break; - case LUA_TPROTO: o = gco2p(o)->gclist; break; - default: lua_assert(0); /* other objects cannot be in a gray list */ + case LUA_VTABLE: o = gco2t(o)->gclist; break; + case LUA_VLCL: o = gco2lcl(o)->gclist; break; + case LUA_VCCL: o = gco2ccl(o)->gclist; break; + case LUA_VTHREAD: o = gco2th(o)->gclist; break; + case LUA_VPROTO: o = gco2p(o)->gclist; break; + case LUA_VUSERDATA: + assert(gco2u(o)->nuvalue > 0); + o = gco2u(o)->gclist; + break; + default: assert(0); /* other objects cannot be in a gray list */ } } + return total; } /* ** Check objects in gray lists. */ -static void checkgrays (global_State *g) { - if (!keepinvariant(g)) return; - checkgraylist(g, g->gray); - checkgraylist(g, g->grayagain); - checkgraylist(g, g->weak); - checkgraylist(g, g->ephemeron); +static l_mem checkgrays (global_State *g) { + l_mem total = 0; /* count number of elements in all lists */ + if (!keepinvariant(g)) return total; + total += checkgraylist(g, g->gray); + total += checkgraylist(g, g->grayagain); + total += checkgraylist(g, g->weak); + total += checkgraylist(g, g->allweak); + total += checkgraylist(g, g->ephemeron); + return total; +} + + +/* +** Check whether 'o' should be in a gray list. If so, increment +** 'count' and check its TESTBIT. (It must have been previously set by +** 'checkgraylist'.) +*/ +static void incifingray (global_State *g, GCObject *o, l_mem *count) { + if (!keepinvariant(g)) + return; /* gray lists not being kept in these phases */ + if (o->tt == LUA_VUPVAL) { + /* only open upvalues can be gray */ + assert(!isgray(o) || upisopen(gco2upv(o))); + return; /* upvalues are never in gray lists */ + } + /* these are the ones that must be in gray lists */ + if (isgray(o) || getage(o) == G_TOUCHED2) { + (*count)++; + assert(testbit(o->marked, TESTBIT)); + resetbit(o->marked, TESTBIT); /* prepare for next cycle */ + } } -static void checklist (global_State *g, int maybedead, int tof, +static l_mem checklist (global_State *g, int maybedead, int tof, GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) { GCObject *o; + l_mem total = 0; /* number of object that should be in gray lists */ for (o = newl; o != survival; o = o->next) { checkobject(g, o, maybedead, G_NEW); - lua_assert(!tof == !tofinalize(o)); + incifingray(g, o, &total); + assert(!tof == !tofinalize(o)); } for (o = survival; o != old; o = o->next) { checkobject(g, o, 0, G_SURVIVAL); - lua_assert(!tof == !tofinalize(o)); + incifingray(g, o, &total); + assert(!tof == !tofinalize(o)); } for (o = old; o != reallyold; o = o->next) { checkobject(g, o, 0, G_OLD1); - lua_assert(!tof == !tofinalize(o)); + incifingray(g, o, &total); + assert(!tof == !tofinalize(o)); } for (o = reallyold; o != NULL; o = o->next) { checkobject(g, o, 0, G_OLD); - lua_assert(!tof == !tofinalize(o)); + incifingray(g, o, &total); + assert(!tof == !tofinalize(o)); } + return total; } @@ -520,32 +672,39 @@ int lua_checkmemory (lua_State *L) { global_State *g = G(L); GCObject *o; int maybedead; + l_mem totalin; /* total of objects that are in gray lists */ + l_mem totalshould; /* total of objects that should be in gray lists */ if (keepinvariant(g)) { - lua_assert(!iswhite(g->mainthread)); - lua_assert(!iswhite(gcvalue(&g->l_registry))); + assert(!iswhite(mainthread(g))); + assert(!iswhite(gcvalue(&g->l_registry))); } - lua_assert(!isdead(g, gcvalue(&g->l_registry))); - lua_assert(g->sweepgc == NULL || issweepphase(g)); - checkgrays(g); + assert(!isdead(g, gcvalue(&g->l_registry))); + assert(g->sweepgc == NULL || issweepphase(g)); + totalin = checkgrays(g); /* check 'fixedgc' list */ for (o = g->fixedgc; o != NULL; o = o->next) { - lua_assert(o->tt == LUA_TSHRSTR && isgray(o) && getage(o) == G_OLD); + assert(o->tt == LUA_VSHRSTR && isgray(o) && getage(o) == G_OLD); } /* check 'allgc' list */ maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc); - checklist(g, maybedead, 0, g->allgc, g->survival, g->old, g->reallyold); + totalshould = checklist(g, maybedead, 0, g->allgc, + g->survival, g->old1, g->reallyold); /* check 'finobj' list */ - checklist(g, 0, 1, g->finobj, g->finobjsur, g->finobjold, g->finobjrold); + totalshould += checklist(g, 0, 1, g->finobj, + g->finobjsur, g->finobjold1, g->finobjrold); /* check 'tobefnz' list */ for (o = g->tobefnz; o != NULL; o = o->next) { checkobject(g, o, 0, G_NEW); - lua_assert(tofinalize(o)); - lua_assert(o->tt == LUA_TUSERDATA || o->tt == LUA_TTABLE); + incifingray(g, o, &totalshould); + assert(tofinalize(o)); + assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE); } + if (keepinvariant(g)) + assert(totalin == totalshould); return 0; } @@ -578,6 +737,11 @@ static char *buildop (Proto *p, int pc, char *buff) { GETARG_A(i), GETARG_B(i), GETARG_C(i), GETARG_k(i) ? " (k)" : ""); break; + case ivABC: + sprintf(buff, "%-12s%4d %4d %4d%s", name, + GETARG_A(i), GETARG_vB(i), GETARG_vC(i), + GETARG_k(i) ? " (k)" : ""); + break; case iABx: sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i)); break; @@ -698,35 +862,68 @@ static int listlocals (lua_State *L) { -static void printstack (lua_State *L) { +void lua_printstack (lua_State *L) { int i; int n = lua_gettop(L); + printf("stack: >>\n"); for (i = 1; i <= n; i++) { - printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL)); - lua_pop(L, 1); + printf("%3d: ", i); + lua_printvalue(s2v(L->ci->func.p + i)); + printf("\n"); + } + printf("<<\n"); +} + + +int lua_printallstack (lua_State *L) { + StkId p; + int i = 1; + CallInfo *ci = &L->base_ci; + printf("stack: >>\n"); + for (p = L->stack.p; p < L->top.p; p++) { + if (ci != NULL && p == ci->func.p) { + printf(" ---\n"); + if (ci == L->ci) + ci = NULL; /* printed last frame */ + else + ci = ci->next; + } + printf("%3d: ", i++); + lua_printvalue(s2v(p)); + printf("\n"); } - printf("\n"); + printf("<<\n"); + return 0; } static int get_limits (lua_State *L) { lua_createtable(L, 0, 5); - setnameval(L, "BITS_INT", LUAI_BITSINT); + setnameval(L, "IS32INT", LUAI_IS32INT); setnameval(L, "MAXARG_Ax", MAXARG_Ax); setnameval(L, "MAXARG_Bx", MAXARG_Bx); setnameval(L, "OFFSET_sBx", OFFSET_sBx); - setnameval(L, "BITS_INT", LUAI_BITSINT); - setnameval(L, "LFPF", LFIELDS_PER_FLUSH); setnameval(L, "NUM_OPCODES", NUM_OPCODES); return 1; } +static int get_sizes (lua_State *L) { + lua_newtable(L); + setnameval(L, "Lua state", sizeof(lua_State)); + setnameval(L, "global state", sizeof(global_State)); + setnameval(L, "TValue", sizeof(TValue)); + setnameval(L, "Node", sizeof(Node)); + setnameval(L, "stack Value", sizeof(StackValue)); + return 1; +} + + static int mem_query (lua_State *L) { if (lua_isnone(L, 1)) { - lua_pushinteger(L, l_memcontrol.total); - lua_pushinteger(L, l_memcontrol.numblocks); - lua_pushinteger(L, l_memcontrol.maxmem); + lua_pushinteger(L, cast_Integer(l_memcontrol.total)); + lua_pushinteger(L, cast_Integer(l_memcontrol.numblocks)); + lua_pushinteger(L, cast_Integer(l_memcontrol.maxmem)); return 3; } else if (lua_isnumber(L, 1)) { @@ -738,25 +935,32 @@ static int mem_query (lua_State *L) { else { const char *t = luaL_checkstring(L, 1); int i; - for (i = LUA_NUMTAGS - 1; i >= 0; i--) { + for (i = LUA_NUMTYPES - 1; i >= 0; i--) { if (strcmp(t, ttypename(i)) == 0) { - lua_pushinteger(L, l_memcontrol.objcount[i]); + lua_pushinteger(L, cast_Integer(l_memcontrol.objcount[i])); return 1; } } - return luaL_error(L, "unkown type '%s'", t); + return luaL_error(L, "unknown type '%s'", t); } } static int alloc_count (lua_State *L) { if (lua_isnone(L, 1)) - l_memcontrol.countlimit = ~0L; + l_memcontrol.countlimit = cast(unsigned long, ~0L); else - l_memcontrol.countlimit = luaL_checkinteger(L, 1); + l_memcontrol.countlimit = cast(unsigned long, luaL_checkinteger(L, 1)); + return 0; +} + + +static int alloc_failnext (lua_State *L) { + UNUSED(L); + l_memcontrol.failnext = 1; return 0; } - + static int settrick (lua_State *L) { if (ttisnil(obj_at(L, 1))) @@ -814,57 +1018,90 @@ static int gc_printobj (lua_State *L) { } +static const char *const statenames[] = { + "propagate", "enteratomic", "atomic", "sweepallgc", "sweepfinobj", + "sweeptobefnz", "sweepend", "callfin", "pause", ""}; + static int gc_state (lua_State *L) { - static const char *statenames[] = { - "propagate", "atomic", "enteratomic", "sweepallgc", "sweepfinobj", - "sweeptobefnz", "sweepend", "callfin", "pause", ""}; static const int states[] = { GCSpropagate, GCSenteratomic, GCSatomic, GCSswpallgc, GCSswpfinobj, GCSswptobefnz, GCSswpend, GCScallfin, GCSpause, -1}; int option = states[luaL_checkoption(L, 1, "", statenames)]; + global_State *g = G(L); if (option == -1) { - lua_pushstring(L, statenames[G(L)->gcstate]); + lua_pushstring(L, statenames[g->gcstate]); return 1; } else { - global_State *g = G(L); - if (G(L)->gckind == KGC_GEN) + if (g->gckind != KGC_INC) luaL_error(L, "cannot change states in generational mode"); lua_lock(L); if (option < g->gcstate) { /* must cross 'pause'? */ - luaC_runtilstate(L, bitmask(GCSpause)); /* run until pause */ + luaC_runtilstate(L, GCSpause, 1); /* run until pause */ } - luaC_runtilstate(L, bitmask(option)); - lua_assert(G(L)->gcstate == option); + luaC_runtilstate(L, option, 0); /* do not skip propagation state */ + lua_assert(g->gcstate == option); lua_unlock(L); return 0; } } +static int tracinggc = 0; +void luai_tracegctest (lua_State *L, int first) { + if (!tracinggc) return; + else { + global_State *g = G(L); + lua_unlock(L); + g->gcstp = GCSTPGC; + lua_checkstack(L, 10); + lua_getfield(L, LUA_REGISTRYINDEX, "tracegc"); + lua_pushboolean(L, first); + lua_call(L, 1, 0); + g->gcstp = 0; + lua_lock(L); + } +} + + +static int tracegc (lua_State *L) { + if (lua_isnil(L, 1)) + tracinggc = 0; + else { + tracinggc = 1; + lua_setfield(L, LUA_REGISTRYINDEX, "tracegc"); + } + return 0; +} + + static int hash_query (lua_State *L) { if (lua_isnone(L, 2)) { + TString *ts; luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected"); - lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash); + ts = tsvalue(obj_at(L, 1)); + if (ts->tt == LUA_VLNGSTR) + luaS_hashlongstr(ts); /* make sure long string has a hash */ + lua_pushinteger(L, cast_int(ts->hash)); } else { TValue *o = obj_at(L, 1); Table *t; luaL_checktype(L, 2, LUA_TTABLE); t = hvalue(obj_at(L, 2)); - lua_pushinteger(L, luaH_mainposition(t, o) - t->node); + lua_pushinteger(L, cast_Integer(luaH_mainposition(t, o) - t->node)); } return 1; } static int stacklevel (lua_State *L) { - unsigned long a = 0; - lua_pushinteger(L, (L->top - L->stack)); - lua_pushinteger(L, (L->stack_last - L->stack)); - lua_pushinteger(L, L->nCcalls); + int a = 0; + lua_pushinteger(L, cast_Integer(L->top.p - L->stack.p)); + lua_pushinteger(L, stacksize(L)); + lua_pushinteger(L, cast_Integer(L->nCcalls)); lua_pushinteger(L, L->nci); - lua_pushinteger(L, (unsigned long)&a); + lua_pushinteger(L, (lua_Integer)(size_t)&a); return 5; } @@ -875,20 +1112,23 @@ static int table_query (lua_State *L) { unsigned int asize; luaL_checktype(L, 1, LUA_TTABLE); t = hvalue(obj_at(L, 1)); - asize = luaH_realasize(t); + asize = t->asize; if (i == -1) { - lua_pushinteger(L, asize); - lua_pushinteger(L, allocsizenode(t)); - lua_pushinteger(L, isdummy(t) ? 0 : t->lastfree - t->node); - lua_pushinteger(L, t->alimit); - return 4; + lua_pushinteger(L, cast_Integer(asize)); + lua_pushinteger(L, cast_Integer(allocsizenode(t))); + lua_pushinteger(L, cast_Integer(asize > 0 ? *lenhint(t) : 0)); + return 3; } - else if ((unsigned int)i < asize) { + else if (cast_uint(i) < asize) { lua_pushinteger(L, i); - pushobject(L, &t->array[i]); + if (!tagisempty(*getArrTag(t, i))) + arr2obj(t, cast_uint(i), s2v(L->top.p)); + else + setnilvalue(s2v(L->top.p)); + api_incr_top(L); lua_pushnil(L); } - else if ((i -= asize) < sizenode(t)) { + else if (cast_uint(i -= cast_int(asize)) < sizenode(t)) { TValue k; getnodekey(L, &k, gnode(t, i)); if (!isempty(gval(gnode(t, i))) || @@ -898,16 +1138,45 @@ static int table_query (lua_State *L) { } else lua_pushliteral(L, ""); - pushobject(L, gval(gnode(t, i))); - if (gnext(&t->node[i]) != 0) - lua_pushinteger(L, gnext(&t->node[i])); + if (!isempty(gval(gnode(t, i)))) + pushobject(L, gval(gnode(t, i))); else lua_pushnil(L); + lua_pushinteger(L, gnext(&t->node[i])); } return 3; } +static int gc_query (lua_State *L) { + global_State *g = G(L); + lua_pushstring(L, g->gckind == KGC_INC ? "inc" + : g->gckind == KGC_GENMAJOR ? "genmajor" + : "genminor"); + lua_pushstring(L, statenames[g->gcstate]); + lua_pushinteger(L, cast_st2S(gettotalbytes(g))); + lua_pushinteger(L, cast_st2S(g->GCdebt)); + lua_pushinteger(L, cast_st2S(g->GCmarked)); + lua_pushinteger(L, cast_st2S(g->GCmajorminor)); + return 6; +} + + +static int test_codeparam (lua_State *L) { + lua_Integer p = luaL_checkinteger(L, 1); + lua_pushinteger(L, luaO_codeparam(cast_uint(p))); + return 1; +} + + +static int test_applyparam (lua_State *L) { + lua_Integer p = luaL_checkinteger(L, 1); + lua_Integer x = luaL_checkinteger(L, 2); + lua_pushinteger(L, cast_Integer(luaO_applyparam(cast_byte(p), x))); + return 1; +} + + static int string_query (lua_State *L) { stringtable *tb = &G(L)->strt; int s = cast_int(luaL_optinteger(L, 1, 0)) - 1; @@ -920,7 +1189,7 @@ static int string_query (lua_State *L) { TString *ts; int n = 0; for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) { - setsvalue2s(L, L->top, ts); + setsvalue2s(L, L->top.p, ts); api_incr_top(L); n++; } @@ -930,25 +1199,40 @@ static int string_query (lua_State *L) { } +static int getreftable (lua_State *L) { + if (lua_istable(L, 2)) /* is there a table as second argument? */ + return 2; /* use it as the table */ + else + return LUA_REGISTRYINDEX; /* default is to use the register */ +} + + static int tref (lua_State *L) { + int t = getreftable(L); int level = lua_gettop(L); luaL_checkany(L, 1); lua_pushvalue(L, 1); - lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX)); + lua_pushinteger(L, luaL_ref(L, t)); + cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); /* +1 for result */ return 1; } + static int getref (lua_State *L) { + int t = getreftable(L); int level = lua_gettop(L); - lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1)); + lua_rawgeti(L, t, luaL_checkinteger(L, 1)); + cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); return 1; } static int unref (lua_State *L) { + int t = getreftable(L); int level = lua_gettop(L); - luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1))); + luaL_unref(L, t, cast_int(luaL_checkinteger(L, 1))); + cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level); return 0; } @@ -973,7 +1257,7 @@ static int upvalue (lua_State *L) { static int newuserdata (lua_State *L) { size_t size = cast_sizet(luaL_optinteger(L, 1, 0)); - int nuv = luaL_optinteger(L, 2, 0); + int nuv = cast_int(luaL_optinteger(L, 2, 0)); char *p = cast_charp(lua_newuserdatauv(L, size, nuv)); while (size--) *p++ = '\0'; return 1; @@ -988,7 +1272,7 @@ static int pushuserdata (lua_State *L) { static int udataval (lua_State *L) { - lua_pushinteger(L, cast(long, lua_touserdata(L, 1))); + lua_pushinteger(L, cast_st2S(cast_sizet(lua_touserdata(L, 1)))); return 1; } @@ -1024,10 +1308,16 @@ static int num2int (lua_State *L) { } +static int makeseed (lua_State *L) { + lua_pushinteger(L, cast_Integer(luaL_makeseed(L))); + return 1; +} + + static int newstate (lua_State *L) { void *ud; lua_Alloc f = lua_getallocf(L, &ud); - lua_State *L1 = lua_newstate(f, ud); + lua_State *L1 = lua_newstate(f, ud, 0); if (L1) { lua_atpanic(L1, tpanic); lua_pushlightuserdata(L, L1); @@ -1046,31 +1336,16 @@ static lua_State *getstate (lua_State *L) { static int loadlib (lua_State *L) { - static const luaL_Reg libs[] = { - {LUA_GNAME, luaopen_base}, - {"coroutine", luaopen_coroutine}, - {"debug", luaopen_debug}, - {"io", luaopen_io}, - {"os", luaopen_os}, - {"math", luaopen_math}, - {"string", luaopen_string}, - {"table", luaopen_table}, - {"T", luaB_opentests}, - {NULL, NULL} - }; lua_State *L1 = getstate(L); - int i; - luaL_requiref(L1, "package", luaopen_package, 0); + int load = cast_int(luaL_checkinteger(L, 2)); + int preload = cast_int(luaL_checkinteger(L, 3)); + luaL_openselectedlibs(L1, load, preload); + luaL_requiref(L1, "T", luaB_opentests, 0); lua_assert(lua_type(L1, -1) == LUA_TTABLE); /* 'requiref' should not reload module already loaded... */ - luaL_requiref(L1, "package", NULL, 1); /* seg. fault if it reloads */ + luaL_requiref(L1, "T", NULL, 1); /* seg. fault if it reloads */ /* ...but should return the same module */ lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ)); - luaL_getsubtable(L1, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); - for (i = 0; libs[i].name; i++) { - lua_pushcfunction(L1, libs[i].func); - lua_setfield(L1, -2, libs[i].name); - } return 0; } @@ -1105,14 +1380,6 @@ static int doremote (lua_State *L) { } -static int int2fb_aux (lua_State *L) { - int b = luaO_int2fb((unsigned int)luaL_checkinteger(L, 1)); - lua_pushinteger(L, b); - lua_pushinteger(L, (unsigned int)luaO_fb2int(b)); - return 2; -} - - static int log2_aux (lua_State *L) { unsigned int x = (unsigned int)luaL_checkinteger(L, 1); lua_pushinteger(L, luaO_ceillog2(x)); @@ -1144,9 +1411,9 @@ static int checkpanic (lua_State *L) { lua_Alloc f = lua_getallocf(L, &ud); b.paniccode = luaL_optstring(L, 2, ""); b.L = L; - L1 = lua_newstate(f, ud); /* create new state */ + L1 = lua_newstate(f, ud, 0); /* create new state */ if (L1 == NULL) { /* error? */ - lua_pushnil(L); + lua_pushstring(L, MEMERRMSG); return 1; } lua_atpanic(L1, panicback); /* set its panic function */ @@ -1165,6 +1432,37 @@ static int checkpanic (lua_State *L) { } +static int externKstr (lua_State *L) { + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + lua_pushexternalstring(L, s, len, NULL, NULL); + return 1; +} + + +/* +** Create a buffer with the content of a given string and then +** create an external string using that buffer. Use the allocation +** function from Lua to create and free the buffer. +*/ +static int externstr (lua_State *L) { + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + void *ud; + lua_Alloc allocf = lua_getallocf(L, &ud); /* get allocation function */ + /* create the buffer */ + char *buff = cast_charp((*allocf)(ud, NULL, 0, len + 1)); + if (buff == NULL) { /* memory error? */ + lua_pushliteral(L, "not enough memory"); + lua_error(L); /* raise a memory error */ + } + /* copy string content to buffer, including ending 0 */ + memcpy(buff, s, (len + 1) * sizeof(char)); + /* create external string */ + lua_pushexternalstring(L, buff, len, allocf, ud); + return 1; +} + /* ** {==================================================================== @@ -1203,6 +1501,16 @@ static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) { (*pc)++; return res; } + else if (**pc == '!') { + (*pc)++; + if (**pc == 'G') + res = LUA_RIDX_GLOBALS; + else if (**pc == 'M') + res = LUA_RIDX_MAINTHREAD; + else lua_assert(0); + (*pc)++; + return res; + } else if (**pc == '-') { sig = -1; (*pc)++; @@ -1237,17 +1545,31 @@ static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) { skip(pc); switch (*(*pc)++) { case 'R': return LUA_REGISTRYINDEX; - case 'G': return luaL_error(L, "deprecated index 'G'"); case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc)); - default: (*pc)--; return getnum_aux(L, L1, pc); + default: { + int n; + (*pc)--; /* to read again */ + n = getnum_aux(L, L1, pc); + if (n == 0) return 0; + else return lua_absindex(L1, n); + } } } -static void pushcode (lua_State *L, int code) { - static const char *const codes[] = {"OK", "YIELD", "ERRRUN", - "ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"}; - lua_pushstring(L, codes[code]); +static const char *const statcodes[] = {"OK", "YIELD", "ERRRUN", + "ERRSYNTAX", MEMERRMSG, "ERRERR"}; + +/* +** Avoid these stat codes from being collected, to avoid possible +** memory error when pushing them. +*/ +static void regcodes (lua_State *L) { + unsigned int i; + for (i = 0; i < sizeof(statcodes) / sizeof(statcodes[0]); i++) { + lua_pushboolean(L, 1); + lua_setfield(L, LUA_REGISTRYINDEX, statcodes[i]); + } } @@ -1279,17 +1601,17 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { const char *inst = getstring; if EQ("") return 0; else if EQ("absindex") { - lua_pushnumber(L1, lua_absindex(L1, getindex)); + lua_pushinteger(L1, getindex); } else if EQ("append") { int t = getindex; - int i = lua_rawlen(L1, t); + int i = cast_int(lua_rawlen(L1, t)); lua_rawseti(L1, t, i + 1); } else if EQ("arith") { int op; skip(&pc); - op = strchr(ops, *pc++) - ops; + op = cast_int(strchr(ops, *pc++) - ops); lua_arith(L1, op); } else if EQ("call") { @@ -1331,11 +1653,12 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else if EQ("func2num") { lua_CFunction func = lua_tocfunction(L1, getindex); - lua_pushnumber(L1, cast_sizet(func)); + lua_pushinteger(L1, cast_st2S(cast_sizet(func))); } else if EQ("getfield") { int t = getindex; - lua_getfield(L1, t, getstring); + int tp = lua_getfield(L1, t, getstring); + lua_assert(tp == lua_type(L1, -1)); } else if EQ("getglobal") { lua_getglobal(L1, getstring); @@ -1345,7 +1668,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { lua_pushnil(L1); } else if EQ("gettable") { - lua_gettable(L1, getindex); + int tp = lua_gettable(L1, getindex); + lua_assert(tp == lua_type(L1, -1)); } else if EQ("gettop") { lua_pushinteger(L1, lua_gettop(L1)); @@ -1396,8 +1720,11 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { luaL_loadfile(L1, luaL_checkstring(L1, getnum)); } else if EQ("loadstring") { - const char *s = luaL_checkstring(L1, getnum); - luaL_loadstring(L1, s); + size_t slen; + const char *s = luaL_checklstring(L1, getnum, &slen); + const char *name = getstring; + const char *mode = getstring; + luaL_loadbufferx(L1, s, slen, name, mode); } else if EQ("newmetatable") { lua_pushboolean(L1, luaL_newmetatable(L1, getstring)); @@ -1409,16 +1736,16 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { lua_newthread(L1); } else if EQ("resetthread") { - lua_pushinteger(L1, lua_resetthread(L1)); + lua_pushinteger(L1, lua_resetthread(L1)); /* deprecated */ } else if EQ("newuserdata") { - lua_newuserdata(L1, getnum); + lua_newuserdata(L1, cast_sizet(getnum)); } else if EQ("next") { lua_next(L1, -2); } else if EQ("objsize") { - lua_pushinteger(L1, lua_rawlen(L1, getindex)); + lua_pushinteger(L1, l_castU2S(lua_rawlen(L1, getindex))); } else if EQ("pcall") { int narg = getnum; @@ -1437,10 +1764,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { else if EQ("printstack") { int n = getnum; if (n != 0) { - printf("%s\n", luaL_tolstring(L1, n, NULL)); - lua_pop(L1, 1); + lua_printvalue(s2v(L->ci->func.p + n)); + printf("\n"); } - else printstack(L1); + else lua_printstack(L1); } else if EQ("print") { const char *msg = getstring; @@ -1470,7 +1797,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { lua_pushnumber(L1, (lua_Number)getnum); } else if EQ("pushstatus") { - pushcode(L1, status); + lua_pushstring(L1, statcodes[status]); } else if EQ("pushstring") { lua_pushstring(L1, getstring); @@ -1481,6 +1808,19 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { else if EQ("pushvalue") { lua_pushvalue(L1, getindex); } + else if EQ("pushfstringI") { + lua_pushfstring(L1, lua_tostring(L, -2), (int)lua_tointeger(L, -1)); + } + else if EQ("pushfstringS") { + lua_pushfstring(L1, lua_tostring(L, -2), lua_tostring(L, -1)); + } + else if EQ("pushfstringP") { + lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1)); + } + else if EQ("rawget") { + int t = getindex; + lua_rawget(L1, t); + } else if EQ("rawgeti") { int t = getindex; lua_rawgeti(L1, t, getnum); @@ -1489,6 +1829,14 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { int t = getindex; lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum))); } + else if EQ("rawset") { + int t = getindex; + lua_rawset(L1, t); + } + else if EQ("rawseti") { + int t = getindex; + lua_rawseti(L1, t, getnum); + } else if EQ("rawsetp") { int t = getindex; lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum))); @@ -1504,6 +1852,17 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { int nres; status = lua_resume(lua_tothread(L1, i), L, getnum, &nres); } + else if EQ("traceback") { + const char *msg = getstring; + int level = getnum; + luaL_traceback(L1, L1, msg, level); + } + else if EQ("threadstatus") { + lua_pushstring(L1, statcodes[lua_status(L1)]); + } + else if EQ("alloccount") { + l_memcontrol.countlimit = cast_uint(getnum); + } else if EQ("return") { int n = getnum; if (L1 != L) { @@ -1531,6 +1890,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { const char *s = getstring; lua_setfield(L1, t, s); } + else if EQ("seti") { + int t = getindex; + lua_seti(L1, t, getnum); + } else if EQ("setglobal") { const char *s = getstring; lua_setglobal(L1, s); @@ -1558,6 +1921,9 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { else if EQ("error") { lua_error(L1); } + else if EQ("abort") { + abort(); + } else if EQ("throw") { #if defined(__cplusplus) static struct X { int x; } x; @@ -1588,8 +1954,12 @@ static struct X { int x; } x; else if EQ("tostring") { const char *s = lua_tostring(L1, getindex); const char *s1 = lua_pushstring(L1, s); + cast_void(s1); /* to avoid warnings */ lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0); } + else if EQ("Ltolstring") { + luaL_tolstring(L1, getindex, NULL); + } else if EQ("type") { lua_pushstring(L1, luaL_typename(L1, getnum)); } @@ -1602,6 +1972,9 @@ static struct X { int x; } x; if (n == 0) n = lua_gettop(fs); lua_xmove(fs, ts, n); } + else if EQ("isyieldable") { + lua_pushboolean(L1, lua_isyieldable(lua_tothread(L1, getindex))); + } else if EQ("yield") { return lua_yield(L1, getnum); } @@ -1613,6 +1986,13 @@ static struct X { int x; } x; else if EQ("toclose") { lua_toclose(L1, getnum); } + else if EQ("closeslot") { + lua_closeslot(L1, getnum); + } + else if EQ("argerror") { + int arg = getnum; + luaL_argerror(L1, arg, getstring); + } else luaL_error(L, "unknown instruction %s", buff); } return 0; @@ -1644,11 +2024,11 @@ static int Cfunc (lua_State *L) { static int Cfunck (lua_State *L, int status, lua_KContext ctx) { - pushcode(L, status); + lua_pushstring(L, statcodes[status]); lua_setglobal(L, "status"); - lua_pushinteger(L, ctx); + lua_pushinteger(L, cast_Integer(ctx)); lua_setglobal(L, "ctx"); - return runC(L, L, lua_tostring(L, ctx)); + return runC(L, L, lua_tostring(L, cast_int(ctx))); } @@ -1741,6 +2121,25 @@ static int coresume (lua_State *L) { } } +#if !defined(LUA_USE_POSIX) + +#define nonblock NULL + +#else + +#include +#include + +static int nonblock (lua_State *L) { + FILE *f = cast(luaL_Stream*, luaL_checkudata(L, 1, LUA_FILEHANDLE))->f; + int fd = fileno(f); + int flags = fcntl(fd, F_GETFL, 0); + flags |= O_NONBLOCK; + fcntl(fd, F_SETFL, flags); + return 0; +} +#endif + /* }====================================================== */ @@ -1754,14 +2153,15 @@ static const struct luaL_Reg tests_funcs[] = { {"gccolor", gc_color}, {"gcage", gc_age}, {"gcstate", gc_state}, + {"tracegc", tracegc}, {"pobj", gc_printobj}, {"getref", getref}, {"hash", hash_query}, - {"int2fb", int2fb_aux}, {"log2", log2_aux}, {"limits", get_limits}, {"listcode", listcode}, {"printcode", printcode}, + {"printallstack", lua_printallstack}, {"listk", listk}, {"listabslineinfo", listabslineinfo}, {"listlocals", listlocals}, @@ -1770,22 +2170,31 @@ static const struct luaL_Reg tests_funcs[] = { {"newstate", newstate}, {"newuserdata", newuserdata}, {"num2int", num2int}, + {"makeseed", makeseed}, {"pushuserdata", pushuserdata}, + {"gcquery", gc_query}, {"querystr", string_query}, {"querytab", table_query}, + {"codeparam", test_codeparam}, + {"applyparam", test_applyparam}, {"ref", tref}, {"resume", coresume}, {"s2d", s2d}, {"sethook", sethook}, {"stacklevel", stacklevel}, + {"sizes", get_sizes}, {"testC", testC}, {"makeCfunc", makeCfunc}, {"totalmem", mem_query}, {"alloccount", alloc_count}, + {"allocfailnext", alloc_failnext}, {"trick", settrick}, {"udataval", udataval}, {"unref", unref}, {"upvalue", upvalue}, + {"externKstr", externKstr}, + {"externstr", externstr}, + {"nonblock", nonblock}, {NULL, NULL} }; @@ -1798,12 +2207,15 @@ static void checkfinalmem (void) { int luaB_opentests (lua_State *L) { void *ud; + lua_Alloc f = lua_getallocf(L, &ud); lua_atpanic(L, &tpanic); lua_setwarnf(L, &warnf, L); + lua_pushboolean(L, 0); + lua_setglobal(L, "_WARN"); /* _WARN = false */ + regcodes(L); atexit(checkfinalmem); - lua_assert(lua_getallocf(L, &ud) == debug_realloc); - lua_assert(ud == cast_voidp(&l_memcontrol)); - lua_setallocf(L, lua_getallocf(L, NULL), ud); + lua_assert(f == debug_realloc && ud == cast_voidp(&l_memcontrol)); + lua_setallocf(L, f, ud); /* exercise this function */ luaL_newlib(L, tests_funcs); return 1; } diff --git a/ltests.h b/ltests.h index 997e1c4b08..93096da810 100644 --- a/ltests.h +++ b/ltests.h @@ -13,26 +13,16 @@ /* test Lua with compatibility code */ #define LUA_COMPAT_MATHLIB -#define LUA_COMPAT_LT_LE +#undef LUA_COMPAT_GLOBAL #define LUA_DEBUG /* turn on assertions */ -#undef NDEBUG -#include -#define lua_assert(c) assert(c) +#define LUAI_ASSERT -/* include opcode names */ -#define LUAI_DEFOPNAMES - - -/* compiled with -O0, Lua uses a lot of C stack space... */ -#undef LUAI_MAXCCALLS -#define LUAI_MAXCCALLS 400 - /* to avoid warnings, and to make sure value is really unused */ #define UNUSED(x) (x=0, (void)(x)) @@ -54,30 +44,57 @@ #define LUA_RAND32 +/* test stack reallocation without strict address use */ +#define LUAI_STRICT_ADDRESS 0 + + /* memory-allocator control variables */ typedef struct Memcontrol { + int failnext; unsigned long numblocks; unsigned long total; unsigned long maxmem; unsigned long memlimit; unsigned long countlimit; - unsigned long objcount[LUA_NUMTAGS]; + unsigned long objcount[LUA_NUMTYPES]; } Memcontrol; LUA_API Memcontrol l_memcontrol; +#define luai_tracegc(L,f) luai_tracegctest(L, f) +extern void luai_tracegctest (lua_State *L, int first); + + /* ** generic variable for debug tricks */ extern void *l_Trick; - /* ** Function to traverse and check all memory used by Lua */ -int lua_checkmemory (lua_State *L); +extern int lua_checkmemory (lua_State *L); + +/* +** Function to print an object GC-friendly +*/ +struct GCObject; +extern void lua_printobj (lua_State *L, struct GCObject *o); + + +/* +** Function to print a value +*/ +struct TValue; +extern void lua_printvalue (struct TValue *v); + +/* +** Function to print the stack +*/ +extern void lua_printstack (lua_State *L); +extern int lua_printallstack (lua_State *L); /* test for lock/unlock */ @@ -104,13 +121,14 @@ LUA_API int luaB_opentests (lua_State *L); LUA_API void *debug_realloc (void *ud, void *block, size_t osize, size_t nsize); -#if defined(lua_c) -#define luaL_newstate() lua_newstate(debug_realloc, &l_memcontrol) -#define luaL_openlibs(L) \ - { (luaL_openlibs)(L); \ + +#define luaL_newstate() \ + lua_newstate(debug_realloc, &l_memcontrol, luaL_makeseed(NULL)) +#define luai_openlibs(L) \ + { luaL_openlibs(L); \ luaL_requiref(L, "T", luaB_opentests, 1); \ lua_pop(L, 1); } -#endif + @@ -121,18 +139,28 @@ LUA_API void *debug_realloc (void *ud, void *block, #define MINSTRTABSIZE 2 #define MAXIWTHABS 3 +#define STRCACHE_N 23 +#define STRCACHE_M 5 + +#define MAXINDEXRK 1 + + +/* +** Reduce maximum stack size to make stack-overflow tests run faster. +** (But value is still large enough to overflow smaller integers.) +*/ +#define LUAI_MAXSTACK 68000 -/* make stack-overflow tests run faster */ -#undef LUAI_MAXSTACK -#define LUAI_MAXSTACK 50000 +/* test mode uses more stack space */ +#undef LUAI_MAXCCALLS +#define LUAI_MAXCCALLS 180 -#undef LUAI_USER_ALIGNMENT_T -#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; } +/* force Lua to use its own implementations */ +#undef lua_strx2number +#undef lua_number2strx -#define STRCACHE_N 23 -#define STRCACHE_M 5 #endif diff --git a/ltm.c b/ltm.c index 23a97a62cc..f2a373f86c 100644 --- a/ltm.c +++ b/ltm.c @@ -27,7 +27,7 @@ static const char udatatypename[] = "userdata"; -LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { +LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = { "no value", "nil", "boolean", udatatypename, "number", "string", "table", "function", udatatypename, "thread", @@ -58,7 +58,7 @@ void luaT_init (lua_State *L) { ** tag methods */ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { - const TValue *tm = luaH_getshortstr(events, ename); + const TValue *tm = luaH_Hgetshortstr(events, ename); lua_assert(event <= TM_EQ); if (notm(tm)) { /* no tag method? */ events->flags |= cast_byte(1u<mt[ttype(o)]; } - return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue); + return (mt ? luaH_Hgetshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue); } @@ -92,7 +92,7 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) { Table *mt; if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { - const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); + const TValue *name = luaH_Hgetshortstr(mt, luaS_new(L, "__name")); if (ttisstring(name)) /* is '__name' a string? */ return getstr(tsvalue(name)); /* use it as type name */ } @@ -102,12 +102,12 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) { void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3) { - StkId func = L->top; + StkId func = L->top.p; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ setobj2s(L, func + 1, p1); /* 1st argument */ setobj2s(L, func + 2, p2); /* 2nd argument */ setobj2s(L, func + 3, p3); /* 3rd argument */ - L->top = func + 4; + L->top.p = func + 4; /* metamethod may yield only when called from Lua code */ if (isLuacode(L->ci)) luaD_call(L, func, 0); @@ -116,21 +116,22 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, } -void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, StkId res) { +lu_byte luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, StkId res) { ptrdiff_t result = savestack(L, res); - StkId func = L->top; + StkId func = L->top.p; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ setobj2s(L, func + 1, p1); /* 1st argument */ setobj2s(L, func + 2, p2); /* 2nd argument */ - L->top += 3; + L->top.p += 3; /* metamethod may yield only when called from Lua code */ if (isLuacode(L->ci)) luaD_call(L, func, 1); else luaD_callnoyield(L, func, 1); res = restorestack(L, result); - setobjs2s(L, res, --L->top); /* move result to its place */ + setobjs2s(L, res, --L->top.p); /* move result to its place */ + return ttypetag(s2v(res)); /* return tag of the result */ } @@ -139,19 +140,17 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ if (notm(tm)) tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ - if (notm(tm)) return 0; - luaT_callTMres(L, tm, p1, p2, res); - return 1; + if (notm(tm)) + return -1; /* tag method not found */ + else /* call tag method and return the tag of the result */ + return luaT_callTMres(L, tm, p1, p2, res); } void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event) { - if (!callbinTM(L, p1, p2, res, event)) { + if (l_unlikely(callbinTM(L, p1, p2, res, event) < 0)) { switch (event) { - case TM_CONCAT: - luaG_concaterror(L, p1, p2); - /* call never returns, but to avoid warnings: *//* FALLTHROUGH */ case TM_BAND: case TM_BOR: case TM_BXOR: case TM_SHL: case TM_SHR: case TM_BNOT: { if (ttisnumber(p1) && ttisnumber(p2)) @@ -167,9 +166,20 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, } +/* +** The use of 'p1' after 'callbinTM' is safe because, when a tag +** method is not found, 'callbinTM' cannot change the stack. +*/ +void luaT_tryconcatTM (lua_State *L) { + StkId p1 = L->top.p - 2; /* first argument */ + if (l_unlikely(callbinTM(L, s2v(p1), s2v(p1 + 1), p1, TM_CONCAT) < 0)) + luaG_concaterror(L, s2v(p1), s2v(p1 + 1)); +} + + void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, int inv, TMS event) { - if (inv) + int flip, StkId res, TMS event) { + if (flip) luaT_trybinTM(L, p2, p1, res, event); else luaT_trybinTM(L, p1, p2, res, event); @@ -177,38 +187,35 @@ void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, - int inv, StkId res, TMS event) { + int flip, StkId res, TMS event) { TValue aux; setivalue(&aux, i2); - luaT_trybinassocTM(L, p1, &aux, res, inv, event); + luaT_trybinassocTM(L, p1, &aux, flip, res, event); } +/* +** Calls an order tag method. +*/ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { - if (callbinTM(L, p1, p2, L->top, event)) /* try original event */ - return !l_isfalse(s2v(L->top)); -#if defined(LUA_COMPAT_LT_LE) - else if (event == TM_LE) { - /* try '!(p2 < p1)' for '(p1 <= p2)' */ - L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ - if (callbinTM(L, p2, p1, L->top, TM_LT)) { - L->ci->callstatus ^= CIST_LEQ; /* clear mark */ - return l_isfalse(s2v(L->top)); - } - /* else error will remove this 'ci'; no need to clear mark */ - } -#endif + int tag = callbinTM(L, p1, p2, L->top.p, event); /* try original event */ + if (tag >= 0) /* found tag method? */ + return !tagisfalse(tag); luaG_ordererror(L, p1, p2); /* no metamethod found */ return 0; /* to avoid warnings */ } int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, - int inv, TMS event) { + int flip, int isfloat, TMS event) { TValue aux; const TValue *p2; - setivalue(&aux, v2); - if (inv) { /* arguments were exchanged? */ + if (isfloat) { + setfltvalue(&aux, cast_num(v2)); + } + else + setivalue(&aux, v2); + if (flip) { /* arguments were exchanged? */ p2 = p1; p1 = &aux; /* correct them */ } else @@ -217,36 +224,140 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, } -void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, - const Proto *p) { +/* +** Create a vararg table at the top of the stack, with 'n' elements +** starting at 'f'. +*/ +static void createvarargtab (lua_State *L, StkId f, int n) { + int i; + TValue key, value; + Table *t = luaH_new(L); + sethvalue(L, s2v(L->top.p), t); + L->top.p++; + luaH_resize(L, t, cast_uint(n), 1); + setsvalue(L, &key, luaS_new(L, "n")); /* key is "n" */ + setivalue(&value, n); /* value is n */ + /* No need to anchor the key: Due to the resize, the next operation + cannot trigger a garbage collection */ + luaH_set(L, t, &key, &value); /* t.n = n */ + for (i = 0; i < n; i++) + luaH_setint(L, t, i + 1, s2v(f + i)); + luaC_checkGC(L); +} + + +/* +** initial stack: func arg1 ... argn extra1 ... +** ^ ci->func ^ L->top +** final stack: func nil ... nil extra1 ... func arg1 ... argn +** ^ ci->func +*/ +static void buildhiddenargs (lua_State *L, CallInfo *ci, const Proto *p, + int totalargs, int nfixparams, int nextra) { int i; - int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */ - int nextra = actual - nfixparams; /* number of extra arguments */ ci->u.l.nextraargs = nextra; - checkstackGC(L, p->maxstacksize + 1); - /* copy function to the top of the stack */ - setobjs2s(L, L->top++, ci->func); - /* move fixed parameters to the top of the stack */ + luaD_checkstack(L, p->maxstacksize + 1); + /* copy function to the top of the stack, after extra arguments */ + setobjs2s(L, L->top.p++, ci->func.p); + /* move fixed parameters to after the copied function */ for (i = 1; i <= nfixparams; i++) { - setobjs2s(L, L->top++, ci->func + i); - setnilvalue(s2v(ci->func + i)); /* erase original parameter (for GC) */ + setobjs2s(L, L->top.p++, ci->func.p + i); + setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ } - ci->func += actual + 1; - ci->top += actual + 1; - lua_assert(L->top <= ci->top && ci->top <= L->stack_last); + ci->func.p += totalargs + 1; /* 'func' now lives after hidden arguments */ + ci->top.p += totalargs + 1; } -void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { - int i; +void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) { + int totalargs = cast_int(L->top.p - ci->func.p) - 1; + int nfixparams = p->numparams; + int nextra = totalargs - nfixparams; /* number of extra arguments */ + if (p->flag & PF_VATAB) { /* does it need a vararg table? */ + lua_assert(!(p->flag & PF_VAHID)); + createvarargtab(L, ci->func.p + nfixparams + 1, nextra); + /* move table to proper place (last parameter) */ + setobjs2s(L, ci->func.p + nfixparams + 1, L->top.p - 1); + } + else { /* no table */ + lua_assert(p->flag & PF_VAHID); + buildhiddenargs(L, ci, p, totalargs, nfixparams, nextra); + /* set vararg parameter to nil */ + setnilvalue(s2v(ci->func.p + nfixparams + 1)); + lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p); + } +} + + +void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc) { int nextra = ci->u.l.nextraargs; + lua_Integer n; + if (tointegerns(rc, &n)) { /* integral value? */ + if (l_castS2U(n) - 1 < cast_uint(nextra)) { + StkId slot = ci->func.p - nextra + cast_int(n) - 1; + setobjs2s(((lua_State*)NULL), ra, slot); + return; + } + } + else if (ttisstring(rc)) { /* string value? */ + size_t len; + const char *s = getlstr(tsvalue(rc), len); + if (len == 1 && s[0] == 'n') { /* key is "n"? */ + setivalue(s2v(ra), nextra); + return; + } + } + setnilvalue(s2v(ra)); /* else produce nil */ +} + + +/* +** Get the number of extra arguments in a vararg function. If vararg +** table has been optimized away, that number is in the call info. +** Otherwise, get the field 'n' from the vararg table and check that it +** has a proper value (non-negative integer not larger than the stack +** limit). +*/ +static int getnumargs (lua_State *L, CallInfo *ci, Table *h) { + if (h == NULL) /* no vararg table? */ + return ci->u.l.nextraargs; + else { + TValue res; + if (luaH_getshortstr(h, luaS_new(L, "n"), &res) != LUA_VNUMINT || + l_castS2U(ivalue(&res)) > cast_uint(INT_MAX/2)) + luaG_runerror(L, "vararg table has no proper 'n'"); + return cast_int(ivalue(&res)); + } +} + + +/* +** Get 'wanted' vararg arguments and put them in 'where'. 'vatab' is +** the register of the vararg table or -1 if there is no vararg table. +*/ +void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted, + int vatab) { + Table *h = (vatab < 0) ? NULL : hvalue(s2v(ci->func.p + vatab + 1)); + int nargs = getnumargs(L, ci, h); /* number of available vararg args. */ + int i, touse; /* 'touse' is minimum between 'wanted' and 'nargs' */ if (wanted < 0) { - wanted = nextra; /* get all extra arguments available */ - checkstackp(L, nextra, where); /* ensure stack space */ - L->top = where + nextra; /* next instruction will need top */ + touse = wanted = nargs; /* get all extra arguments available */ + checkstackp(L, nargs, where); /* ensure stack space */ + L->top.p = where + nargs; /* next instruction will need top */ + } + else + touse = (nargs > wanted) ? wanted : nargs; + if (h == NULL) { /* no vararg table? */ + for (i = 0; i < touse; i++) /* get vararg values from the stack */ + setobjs2s(L, where + i, ci->func.p - nargs + i); + } + else { /* get vararg values from vararg table */ + for (i = 0; i < touse; i++) { + lu_byte tag = luaH_getint(h, i + 1, s2v(where + i)); + if (tagisempty(tag)) + setnilvalue(s2v(where + i)); + } } - for (i = 0; i < wanted && i < nextra; i++) - setobjs2s(L, where + i, ci->func - nextra + i); for (; i < wanted; i++) /* complete required results with nil */ setnilvalue(s2v(where + i)); } diff --git a/ltm.h b/ltm.h index fad47842a1..07fc8c1c98 100644 --- a/ltm.h +++ b/ltm.h @@ -45,21 +45,31 @@ typedef enum { } TMS; +/* +** Mask with 1 in all fast-access methods. A 1 in any of these bits +** in the flag of a (meta)table means the metatable does not have the +** corresponding metamethod field. (Bit 6 of the flag indicates that +** the table is using the dummy node; bit 7 is used for 'isrealasize'.) +*/ +#define maskflags cast_byte(~(~0u << (TM_EQ + 1))) + + /* ** Test whether there is no tagmethod. ** (Because tagmethods use raw accesses, the result may be an "empty" nil.) */ #define notm(tm) ttisnil(tm) +#define checknoTM(mt,e) ((mt) == NULL || (mt)->flags & (1u<<(e))) -#define gfasttm(g,et,e) ((et) == NULL ? NULL : \ - ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) +#define gfasttm(g,mt,e) \ + (checknoTM(mt, e) ? NULL : luaT_gettm(mt, e, (g)->tmname[e])) -#define fasttm(l,et,e) gfasttm(G(l), et, e) +#define fasttm(l,mt,e) gfasttm(G(l), mt, e) #define ttypename(x) luaT_typenames_[(x) + 1] -LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTAGS];) +LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];) LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); @@ -71,23 +81,25 @@ LUAI_FUNC void luaT_init (lua_State *L); LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3); -LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, - const TValue *p1, const TValue *p2, StkId p3); +LUAI_FUNC lu_byte luaT_callTMres (lua_State *L, const TValue *f, + const TValue *p1, const TValue *p2, StkId p3); LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); +LUAI_FUNC void luaT_tryconcatTM (lua_State *L); LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, - const TValue *p2, StkId res, int inv, TMS event); + const TValue *p2, int inv, StkId res, TMS event); LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, int inv, StkId res, TMS event); LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event); LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, - int inv, TMS event); + int inv, int isfloat, TMS event); -LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, - struct CallInfo *ci, const Proto *p); -LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, - StkId where, int wanted); +LUAI_FUNC void luaT_adjustvarargs (lua_State *L, struct CallInfo *ci, + const Proto *p); +LUAI_FUNC void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc); +LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where, + int wanted, int vatab); #endif diff --git a/lua.c b/lua.c index fa534ba2b4..5054583de9 100644 --- a/lua.c +++ b/lua.c @@ -19,6 +19,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" #if !defined(LUA_PROGNAME) @@ -37,6 +38,26 @@ static lua_State *globalL = NULL; static const char *progname = LUA_PROGNAME; +#if defined(LUA_USE_POSIX) /* { */ + +/* +** Use 'sigaction' when available. +*/ +static void setsignal (int sig, void (*handler)(int)) { + struct sigaction sa; + sa.sa_handler = handler; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); /* do not mask any signal */ + sigaction(sig, &sa, NULL); +} + +#else /* }{ */ + +#define setsignal signal + +#endif /* } */ + + /* ** Hook set by signal function to stop the interpreter. */ @@ -54,8 +75,9 @@ static void lstop (lua_State *L, lua_Debug *ar) { ** interpreter. */ static void laction (int i) { - signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ - lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); + int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT; + setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ + lua_sethook(globalL, lstop, flag, 1); } @@ -68,13 +90,15 @@ static void print_usage (const char *badoption) { lua_writestringerror( "usage: %s [options] [script [args]]\n" "Available options are:\n" - " -e stat execute string 'stat'\n" - " -i enter interactive mode after executing 'script'\n" - " -l name require library 'name' into global 'name'\n" - " -v show version information\n" - " -E ignore environment variables\n" - " -- stop handling options\n" - " - stop handling options and execute stdin\n" + " -e stat execute string 'stat'\n" + " -i enter interactive mode after executing 'script'\n" + " -l mod require library 'mod' into global 'mod'\n" + " -l g=mod require library 'mod' into global 'g'\n" + " -v show version information\n" + " -E ignore environment variables\n" + " -W turn warnings on\n" + " -- stop handling options\n" + " - stop handling options and execute stdin\n" , progname); } @@ -92,12 +116,13 @@ static void l_message (const char *pname, const char *msg) { /* ** Check whether 'status' is not OK and, if so, prints the error -** message on the top of the stack. It assumes that the error object -** is a string, as it was either generated by Lua or by 'msghandler'. +** message on the top of the stack. */ static int report (lua_State *L, int status) { if (status != LUA_OK) { const char *msg = lua_tostring(L, -1); + if (msg == NULL) + msg = "(error message not a string)"; l_message(progname, msg); lua_pop(L, 1); /* remove message */ } @@ -133,9 +158,9 @@ static int docall (lua_State *L, int narg, int nres) { lua_pushcfunction(L, msghandler); /* push message handler */ lua_insert(L, base); /* put it under function and args */ globalL = L; /* to be available to 'laction' */ - signal(SIGINT, laction); /* set C-signal handler */ + setsignal(SIGINT, laction); /* set C-signal handler */ status = lua_pcall(L, narg, nres, base); - signal(SIGINT, SIG_DFL); /* reset C-signal handler */ + setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */ lua_remove(L, base); /* remove message handler from the stack */ return status; } @@ -154,10 +179,11 @@ static void print_version (void) { ** to the script (everything after 'script') go to positive indices; ** other arguments (before the script name) go to negative indices. ** If there is no script name, assume interpreter's name as base. +** (If there is no interpreter's name either, 'script' is -1, so +** table sizes are zero.) */ static void createargtable (lua_State *L, char **argv, int argc, int script) { int i, narg; - if (script == argc) script = 0; /* no script name? */ narg = argc - (script + 1); /* number of positive indices */ lua_createtable(L, narg, script + 1); for (i = 0; i < argc; i++) { @@ -185,16 +211,30 @@ static int dostring (lua_State *L, const char *s, const char *name) { /* -** Calls 'require(name)' and stores the result in a global variable -** with the given name. +** Receives 'globname[=modname]' and runs 'globname = require(modname)'. +** If there is no explicit modname and globname contains a '-', cut +** the suffix after '-' (the "version") to make the global name. */ -static int dolibrary (lua_State *L, const char *name) { +static int dolibrary (lua_State *L, char *globname) { int status; + char *suffix = NULL; + char *modname = strchr(globname, '='); + if (modname == NULL) { /* no explicit name? */ + modname = globname; /* module name is equal to global name */ + suffix = strchr(modname, *LUA_IGMARK); /* look for a suffix mark */ + } + else { + *modname = '\0'; /* global name ends here */ + modname++; /* module name starts after the '=' */ + } lua_getglobal(L, "require"); - lua_pushstring(L, name); - status = docall(L, 1, 1); /* call 'require(name)' */ - if (status == LUA_OK) - lua_setglobal(L, name); /* global[name] = require return */ + lua_pushstring(L, modname); + status = docall(L, 1, 1); /* call 'require(modname)' */ + if (status == LUA_OK) { + if (suffix != NULL) /* is there a suffix mark? */ + *suffix = '\0'; /* remove suffix from global name */ + lua_setglobal(L, globname); /* globname = require(modname) */ + } return report(L, status); } @@ -239,14 +279,23 @@ static int handle_script (lua_State *L, char **argv) { /* ** Traverses all arguments from 'argv', returning a mask with those -** needed before running any Lua code (or an error code if it finds -** any invalid argument). 'first' returns the first not-handled argument -** (either the script name or a bad argument in case of error). +** needed before running any Lua code or an error code if it finds any +** invalid argument. In case of error, 'first' is the index of the bad +** argument. Otherwise, 'first' is -1 if there is no program name, +** 0 if there is no script name, or the index of the script name. */ static int collectargs (char **argv, int *first) { int args = 0; int i; - for (i = 1; argv[i] != NULL; i++) { + if (argv[0] != NULL) { /* is there a program name? */ + if (argv[0][0]) /* not empty? */ + progname = argv[0]; /* save it */ + } + else { /* no program name */ + *first = -1; + return 0; + } + for (i = 1; argv[i] != NULL; i++) { /* handle arguments */ *first = i; if (argv[i][0] != '-') /* not an option? */ return args; /* stop handling options */ @@ -254,19 +303,24 @@ static int collectargs (char **argv, int *first) { case '-': /* '--' */ if (argv[i][2] != '\0') /* extra characters after '--'? */ return has_error; /* invalid option */ - *first = i + 1; + /* if there is a script name, it comes after '--' */ + *first = (argv[i + 1] != NULL) ? i + 1 : 0; return args; case '\0': /* '-' */ return args; /* script "name" is '-' */ case 'E': - if (argv[i][2] != '\0') /* extra characters after 1st? */ + if (argv[i][2] != '\0') /* extra characters? */ return has_error; /* invalid option */ args |= has_E; break; + case 'W': + if (argv[i][2] != '\0') /* extra characters? */ + return has_error; /* invalid option */ + break; case 'i': args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ case 'v': - if (argv[i][2] != '\0') /* extra characters after 1st? */ + if (argv[i][2] != '\0') /* extra characters? */ return has_error; /* invalid option */ args |= has_v; break; @@ -283,29 +337,37 @@ static int collectargs (char **argv, int *first) { return has_error; } } - *first = i; /* no script name */ + *first = 0; /* no script name */ return args; } /* -** Processes options 'e' and 'l', which involve running Lua code. +** Processes options 'e' and 'l', which involve running Lua code, and +** 'W', which also affects the state. ** Returns 0 if some code raises an error. */ static int runargs (lua_State *L, char **argv, int n) { int i; + lua_warning(L, "@off", 0); /* by default, Lua stand-alone has warnings off */ for (i = 1; i < n; i++) { int option = argv[i][1]; lua_assert(argv[i][0] == '-'); /* already checked */ - if (option == 'e' || option == 'l') { - int status; - const char *extra = argv[i] + 2; /* both options need an argument */ - if (*extra == '\0') extra = argv[++i]; - lua_assert(extra != NULL); - status = (option == 'e') - ? dostring(L, extra, "=(command line)") - : dolibrary(L, extra); - if (status != LUA_OK) return 0; + switch (option) { + case 'e': case 'l': { + int status; + char *extra = argv[i] + 2; /* both options need an argument */ + if (*extra == '\0') extra = argv[++i]; + lua_assert(extra != NULL); + status = (option == 'e') + ? dostring(L, extra, "=(command line)") + : dolibrary(L, extra); + if (status != LUA_OK) return 0; + break; + } + case 'W': + lua_warning(L, "@on", 0); /* warnings on */ + break; } } return 1; @@ -372,30 +434,91 @@ static int handle_luainit (lua_State *L) { /* -** lua_readline defines how to show a prompt and then read a line from -** the standard input. -** lua_saveline defines how to "save" a read line in a "history". -** lua_freeline defines how to free a line read by lua_readline. +** * lua_initreadline initializes the readline system. +** * lua_readline defines how to show a prompt and then read a line from +** the standard input. +** * lua_saveline defines how to "save" a read line in a "history". +** * lua_freeline defines how to free a line read by lua_readline. */ + #if !defined(lua_readline) /* { */ +/* Otherwise, all previously listed functions should be defined. */ #if defined(LUA_USE_READLINE) /* { */ +/* Lua will be linked with '-lreadline' */ #include #include + #define lua_initreadline(L) ((void)L, rl_readline_name="lua") -#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) -#define lua_saveline(L,line) ((void)L, add_history(line)) -#define lua_freeline(L,b) ((void)L, free(b)) +#define lua_readline(buff,prompt) ((void)buff, readline(prompt)) +#define lua_saveline(line) add_history(line) +#define lua_freeline(line) free(line) -#else /* }{ */ +#else /* }{ */ +/* use dynamically loaded readline (or nothing) */ + +/* pointer to 'readline' function (if any) */ +typedef char *(*l_readlineT) (const char *prompt); +static l_readlineT l_readline = NULL; + +/* pointer to 'add_history' function (if any) */ +typedef void (*l_addhistT) (const char *string); +static l_addhistT l_addhist = NULL; + + +static char *lua_readline (char *buff, const char *prompt) { + if (l_readline != NULL) /* is there a 'readline'? */ + return (*l_readline)(prompt); /* use it */ + else { /* emulate 'readline' over 'buff' */ + fputs(prompt, stdout); + fflush(stdout); /* show prompt */ + return fgets(buff, LUA_MAXINPUT, stdin); /* read line */ + } +} -#define lua_initreadline(L) ((void)L) -#define lua_readline(L,b,p) \ - ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ - fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ -#define lua_saveline(L,line) { (void)L; (void)line; } -#define lua_freeline(L,b) { (void)L; (void)b; } + +static void lua_saveline (const char *line) { + if (l_addhist != NULL) /* is there an 'add_history'? */ + (*l_addhist)(line); /* use it */ + /* else nothing to be done */ +} + + +static void lua_freeline (char *line) { + if (l_readline != NULL) /* is there a 'readline'? */ + free(line); /* free line created by it */ + /* else 'lua_readline' used an automatic buffer; nothing to free */ +} + + +#if defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* { */ +/* try to load 'readline' dynamically */ + +#include + +static void lua_initreadline (lua_State *L) { + void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL); + if (lib == NULL) + lua_warning(L, "library '" LUA_READLINELIB "' not found", 0); + else { + const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); + if (name != NULL) + *name = "lua"; + l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); + l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); + if (l_readline == NULL) + lua_warning(L, "unable to load 'readline'", 0); + } +} + +#else /* }{ */ +/* no dlopen or LUA_READLINELIB undefined */ + +/* Leave pointers with NULL */ +#define lua_initreadline(L) ((void)L) + +#endif /* } */ #endif /* } */ @@ -403,14 +526,18 @@ static int handle_luainit (lua_State *L) { /* -** Returns the string to be used as a prompt by the interpreter. +** Return the string to be used as a prompt by the interpreter. Leave +** the string (or nil, if using the default value) on the stack, to keep +** it anchored. */ static const char *get_prompt (lua_State *L, int firstline) { - const char *p; - lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); - p = lua_tostring(L, -1); - if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); - return p; + if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL) + return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */ + else { /* apply 'tostring' over the value */ + const char *p = luaL_tolstring(L, -1, NULL); + lua_remove(L, -2); /* remove original value */ + return p; + } } /* mark in error messages for incomplete statements */ @@ -427,10 +554,8 @@ static int incomplete (lua_State *L, int status) { if (status == LUA_ERRSYNTAX) { size_t lmsg; const char *msg = lua_tolstring(L, -1, &lmsg); - if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) { - lua_pop(L, 1); + if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) return 1; - } } return 0; /* else... */ } @@ -441,21 +566,17 @@ static int incomplete (lua_State *L, int status) { */ static int pushline (lua_State *L, int firstline) { char buffer[LUA_MAXINPUT]; - char *b = buffer; size_t l; const char *prmt = get_prompt(L, firstline); - int readstatus = lua_readline(L, b, prmt); - if (readstatus == 0) - return 0; /* no input (prompt will be popped by caller) */ + char *b = lua_readline(buffer, prmt); lua_pop(L, 1); /* remove prompt */ + if (b == NULL) + return 0; /* no input */ l = strlen(b); if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ b[--l] = '\0'; /* remove it */ - if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */ - lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ - else - lua_pushlstring(L, b, l); - lua_freeline(L, b); + lua_pushlstring(L, b, l); + lua_freeline(b); return 1; } @@ -468,32 +589,44 @@ static int addreturn (lua_State *L) { const char *line = lua_tostring(L, -1); /* original line */ const char *retline = lua_pushfstring(L, "return %s;", line); int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin"); - if (status == LUA_OK) { + if (status == LUA_OK) lua_remove(L, -2); /* remove modified line */ - if (line[0] != '\0') /* non empty? */ - lua_saveline(L, line); /* keep history */ - } else lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ return status; } +static void checklocal (const char *line) { + static const size_t szloc = sizeof("local") - 1; + static const char space[] = " \t"; + line += strspn(line, space); /* skip spaces */ + if (strncmp(line, "local", szloc) == 0 && /* "local"? */ + strchr(space, *(line + szloc)) != NULL) { /* followed by a space? */ + lua_writestringerror("%s\n", + "warning: locals do not survive across lines in interactive mode"); + } +} + + /* -** Read multiple lines until a complete Lua statement +** Read multiple lines until a complete Lua statement or an error not +** for an incomplete statement. Start with first line already read in +** the stack. */ static int multiline (lua_State *L) { + size_t len; + const char *line = lua_tolstring(L, 1, &len); /* get first line */ + checklocal(line); for (;;) { /* repeat until gets a complete statement */ - size_t len; - const char *line = lua_tolstring(L, 1, &len); /* get what it has */ int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ - if (!incomplete(L, status) || !pushline(L, 0)) { - lua_saveline(L, line); /* keep history */ - return status; /* cannot or should not try to add continuation line */ - } + if (!incomplete(L, status) || !pushline(L, 0)) + return status; /* should not or cannot try to add continuation line */ + lua_remove(L, -2); /* remove error message (from incomplete line) */ lua_pushliteral(L, "\n"); /* add newline... */ lua_insert(L, -2); /* ...between the two lines */ lua_concat(L, 3); /* join them */ + line = lua_tolstring(L, 1, &len); /* get what is has */ } } @@ -505,12 +638,16 @@ static int multiline (lua_State *L) { ** in the top of the stack. */ static int loadline (lua_State *L) { + const char *line; int status; lua_settop(L, 0); if (!pushline(L, 1)) return -1; /* no input */ if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ status = multiline(L); /* try as command, maybe with continuation lines */ + line = lua_tostring(L, 1); + if (line[0] != '\0') /* non empty? */ + lua_saveline(line); /* keep history */ lua_remove(L, 1); /* remove line from the stack */ lua_assert(lua_gettop(L) == 1); return status; @@ -555,6 +692,10 @@ static void doREPL (lua_State *L) { /* }================================================================== */ +#if !defined(luai_openlibs) +#define luai_openlibs(L) luaL_openselectedlibs(L, ~0, 0) +#endif + /* ** Main body of stand-alone interpreter (to be called in protected mode). @@ -565,8 +706,8 @@ static int pmain (lua_State *L) { char **argv = (char **)lua_touserdata(L, 2); int script; int args = collectargs(argv, &script); + int optlim = (script > 0) ? script : argc; /* first argv not an option */ luaL_checkversion(L); /* check that interpreter has correct version */ - if (argv[0] && argv[0][0]) progname = argv[0]; if (args == has_error) { /* bad arg? */ print_usage(argv[script]); /* 'script' has index of bad arg. */ return 0; @@ -577,21 +718,23 @@ static int pmain (lua_State *L) { lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); } - luaL_openlibs(L); /* open standard libraries */ + luai_openlibs(L); /* open standard libraries */ createargtable(L, argv, argc, script); /* create table 'arg' */ - lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */ + lua_gc(L, LUA_GCRESTART); /* start GC... */ + lua_gc(L, LUA_GCGEN); /* ...in generational mode */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ } - if (!runargs(L, argv, script)) /* execute arguments -e and -l */ + if (!runargs(L, argv, optlim)) /* execute arguments -e, -l, and -W */ return 0; /* something failed */ - if (script < argc && /* execute main script (if there is one) */ - handle_script(L, argv + script) != LUA_OK) - return 0; + if (script > 0) { /* execute main script (if there is one) */ + if (handle_script(L, argv + script) != LUA_OK) + return 0; /* interrupt in case of error */ + } if (args & has_i) /* -i option? */ doREPL(L); /* do read-eval-print loop */ - else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ + else if (script < 1 && !(args & (has_e | has_v))) { /* no active option? */ if (lua_stdin_is_tty()) { /* running in interactive mode? */ print_version(); doREPL(L); /* do read-eval-print loop */ @@ -610,6 +753,7 @@ int main (int argc, char **argv) { l_message(argv[0], "cannot create state: not enough memory"); return EXIT_FAILURE; } + lua_gc(L, LUA_GCSTOP); /* stop GC while building state */ lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ lua_pushinteger(L, argc); /* 1st argument */ lua_pushlightuserdata(L, argv); /* 2nd argument */ diff --git a/lua.h b/lua.h index 09611db574..ab473dc3e4 100644 --- a/lua.h +++ b/lua.h @@ -1,7 +1,7 @@ /* ** $Id: lua.h $ ** Lua - A Scripting Language -** Lua.org, PUC-Rio, Brazil (http://www.lua.org) +** Lua.org, PUC-Rio, Brazil (www.lua.org) ** See Copyright Notice at the end of this file */ @@ -13,20 +13,19 @@ #include -#include "luaconf.h" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2025 Lua.org, PUC-Rio" +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" -#define LUA_VERSION_MAJOR "5" -#define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "0" +#define LUA_VERSION_MAJOR_N 5 +#define LUA_VERSION_MINOR_N 5 +#define LUA_VERSION_RELEASE_N 0 -#define LUA_VERSION_NUM 504 -#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 0) +#define LUA_VERSION_NUM (LUA_VERSION_MAJOR_N * 100 + LUA_VERSION_MINOR_N) +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + LUA_VERSION_RELEASE_N) -#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR -#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2018 Lua.org, PUC-Rio" -#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" + +#include "luaconf.h" /* mark for precompiled code ('Lua') */ @@ -38,10 +37,10 @@ /* ** Pseudo-indices -** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty -** space after that to help overflow detection) +** (The stack size is limited to INT_MAX/2; we keep some free empty +** space after that to help overflow detection.) */ -#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) +#define LUA_REGISTRYINDEX (-(INT_MAX/2 + 1000)) #define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) @@ -72,7 +71,7 @@ typedef struct lua_State lua_State; #define LUA_TUSERDATA 7 #define LUA_TTHREAD 8 -#define LUA_NUMTAGS 9 +#define LUA_NUMTYPES 9 @@ -81,9 +80,10 @@ typedef struct lua_State lua_State; /* predefined values in the registry */ -#define LUA_RIDX_MAINTHREAD 1 +/* index 1 is reserved for the reference mechanism */ #define LUA_RIDX_GLOBALS 2 -#define LUA_RIDX_LAST LUA_RIDX_GLOBALS +#define LUA_RIDX_MAINTHREAD 3 +#define LUA_RIDX_LAST 3 /* type of numbers in Lua */ @@ -131,6 +131,16 @@ typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont); +/* +** Type used by the debug API to collect debug information +*/ +typedef struct lua_Debug lua_Debug; + + +/* +** Functions to be called by the debugger in specific events +*/ +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); /* @@ -150,10 +160,10 @@ extern const char lua_ident[]; /* ** state manipulation */ -LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); +LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud, unsigned seed); LUA_API void (lua_close) (lua_State *L); LUA_API lua_State *(lua_newthread) (lua_State *L); -LUA_API int (lua_resetthread) (lua_State *L); +LUA_API int (lua_closethread) (lua_State *L, lua_State *from); LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); @@ -234,6 +244,8 @@ LUA_API void (lua_pushnil) (lua_State *L); LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); +LUA_API const char *(lua_pushexternalstring) (lua_State *L, + const char *s, size_t len, lua_Alloc falloc, void *ud); LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, va_list argp); @@ -313,7 +325,7 @@ LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); /* -** garbage-collection function and options +** garbage-collection options */ #define LUA_GCSTOP 0 @@ -322,11 +334,28 @@ LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); #define LUA_GCCOUNT 3 #define LUA_GCCOUNTB 4 #define LUA_GCSTEP 5 -#define LUA_GCSETPAUSE 6 -#define LUA_GCSETSTEPMUL 7 -#define LUA_GCISRUNNING 9 -#define LUA_GCGEN 10 -#define LUA_GCINC 11 +#define LUA_GCISRUNNING 6 +#define LUA_GCGEN 7 +#define LUA_GCINC 8 +#define LUA_GCPARAM 9 + + +/* +** garbage-collection parameters +*/ +/* parameters for generational mode */ +#define LUA_GCPMINORMUL 0 /* control minor collections */ +#define LUA_GCPMAJORMINOR 1 /* control shift major->minor */ +#define LUA_GCPMINORMAJOR 2 /* control shift minor->major */ + +/* parameters for incremental mode */ +#define LUA_GCPPAUSE 3 /* size of pause between successive GCs */ +#define LUA_GCPSTEPMUL 4 /* GC "speed" */ +#define LUA_GCPSTEPSIZE 5 /* GC granularity */ + +/* number of parameters */ +#define LUA_GCPN 6 + LUA_API int (lua_gc) (lua_State *L, int what, ...); @@ -342,12 +371,15 @@ LUA_API int (lua_next) (lua_State *L, int idx); LUA_API void (lua_concat) (lua_State *L, int n); LUA_API void (lua_len) (lua_State *L, int idx); -LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); +#define LUA_N2SBUFFSZ 64 +LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff); +LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); -LUA_API void (lua_toclose) (lua_State *L, int idx); +LUA_API void (lua_toclose) (lua_State *L, int idx); +LUA_API void (lua_closeslot) (lua_State *L, int idx); /* @@ -400,18 +432,13 @@ LUA_API void (lua_toclose) (lua_State *L, int idx); ** compatibility macros ** =============================================================== */ -#if defined(LUA_COMPAT_APIINTCASTS) - -#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) -#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) -#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) - -#endif #define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1) #define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1) #define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1) +#define lua_resetthread(L) lua_closethread(L,NULL) + /* }============================================================== */ /* @@ -439,12 +466,6 @@ LUA_API void (lua_toclose) (lua_State *L, int idx); #define LUA_MASKLINE (1 << LUA_HOOKLINE) #define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) -typedef struct lua_Debug lua_Debug; /* activation record */ - - -/* Functions to be called by the debugger in specific events */ -typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); - LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); @@ -469,15 +490,17 @@ struct lua_Debug { const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ const char *source; /* (S) */ + size_t srclen; /* (S) */ int currentline; /* (l) */ int linedefined; /* (S) */ int lastlinedefined; /* (S) */ unsigned char nups; /* (u) number of upvalues */ unsigned char nparams;/* (u) number of parameters */ char isvararg; /* (u) */ + unsigned char extraargs; /* (t) number of extra arguments */ char istailcall; /* (t) */ - unsigned short ftransfer; /* (r) index of first value transferred */ - unsigned short ntransfer; /* (r) number of transferred values */ + int ftransfer; /* (r) index of first value transferred */ + int ntransfer; /* (r) number of transferred values */ char short_src[LUA_IDSIZE]; /* (S) */ /* private part */ struct CallInfo *i_ci; /* active function */ @@ -486,8 +509,19 @@ struct lua_Debug { /* }====================================================================== */ +#define LUAI_TOSTRAUX(x) #x +#define LUAI_TOSTR(x) LUAI_TOSTRAUX(x) + +#define LUA_VERSION_MAJOR LUAI_TOSTR(LUA_VERSION_MAJOR_N) +#define LUA_VERSION_MINOR LUAI_TOSTR(LUA_VERSION_MINOR_N) +#define LUA_VERSION_RELEASE LUAI_TOSTR(LUA_VERSION_RELEASE_N) + +#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE + + /****************************************************************************** -* Copyright (C) 1994-2018 Lua.org, PUC-Rio. +* Copyright (C) 1994-2025 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/luaconf.h b/luaconf.h index 0fc161a4b3..96a77802b9 100644 --- a/luaconf.h +++ b/luaconf.h @@ -14,6 +14,16 @@ /* ** =================================================================== +** General Configuration File for Lua +** +** Some definitions here can be changed externally, through the compiler +** (e.g., with '-D' options): They are commented out or protected +** by '#if !defined' guards. However, several other definitions +** should be changed directly here, either because they affect the +** Lua ABI (by making the changes here, you ensure that all software +** connected to Lua, such as C libraries, will be compiled with the same +** configuration); or because they are seldom changed. +** ** Search for "@@" to find all configurable definitions. ** =================================================================== */ @@ -22,20 +32,10 @@ /* ** {==================================================================== ** System Configuration: macros to adapt (if needed) Lua to some -** particular platform, for instance compiling it with 32-bit numbers or -** restricting it to C89. +** particular platform, for instance restricting it to C89. ** ===================================================================== */ -/* -@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You -** can also define LUA_32BITS in the make file, but changing here you -** ensure that all software connected to Lua will be compiled with the -** same configuration. -*/ -/* #define LUA_32BITS */ - - /* @@ LUA_USE_C89 controls the use of non-ISO-C89 features. ** Define it if you want Lua to avoid the use of a few C99 features @@ -58,46 +58,62 @@ #endif +/* +** When POSIX DLL ('LUA_USE_DLOPEN') is enabled, the Lua stand-alone +** application will try to dynamically link a 'readline' facility +** for its REPL. In that case, LUA_READLINELIB is the name of the +** library it will look for those facilities. If lua.c cannot open +** the specified library, it will generate a warning and then run +** without 'readline'. If that macro is not defined, lua.c will not +** use 'readline'. +*/ #if defined(LUA_USE_LINUX) #define LUA_USE_POSIX #define LUA_USE_DLOPEN /* needs an extra library: -ldl */ +#define LUA_READLINELIB "libreadline.so" #endif #if defined(LUA_USE_MACOSX) #define LUA_USE_POSIX -#define LUA_USE_DLOPEN /* MacOS does not need -ldl */ +#define LUA_USE_DLOPEN /* macOS does not need -ldl */ +#define LUA_READLINELIB "libedit.dylib" #endif -/* -@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for -** C89 ('long' and 'double'); Windows always has '__int64', so it does -** not need to use this case. -*/ -#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) -#define LUA_C89_NUMBERS +#if defined(LUA_USE_IOS) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN #endif +#if defined(LUA_USE_C89) && defined(LUA_USE_POSIX) +#error "POSIX is not compatible with C89" +#endif + /* -@@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'. +@@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits. */ -/* avoid undefined shifts */ -#if ((INT_MAX >> 15) >> 15) >= 1 -#define LUAI_BITSINT 32 -#else -/* 'int' always must have at least 16 bits */ -#define LUAI_BITSINT 16 -#endif +#define LUAI_IS32INT ((UINT_MAX >> 30) >= 3) + +/* }================================================================== */ + + +/* +** {================================================================== +** Configuration for Number types. These options should not be +** set externally, because any other code connected to Lua must +** use the same configuration. +** =================================================================== +*/ /* @@ LUA_INT_TYPE defines the type for Lua integers. @@ LUA_FLOAT_TYPE defines the type for Lua floats. -** Lua should work fine with any mix of these options (if supported -** by your C compiler). The usual configurations are 64-bit integers +** Lua should work fine with any mix of these options supported +** by your C compiler. The usual configurations are 64-bit integers ** and 'double' (the default), 32-bit integers and 'float' (for ** restricted platforms), and 'long'/'double' (for C compilers not ** compliant with C99, which may not have support for 'long long'). @@ -113,43 +129,61 @@ #define LUA_FLOAT_DOUBLE 2 #define LUA_FLOAT_LONGDOUBLE 3 -#if defined(LUA_32BITS) /* { */ + +/* Default configuration ('long long' and 'double', for 64-bit Lua) */ +#define LUA_INT_DEFAULT LUA_INT_LONGLONG +#define LUA_FLOAT_DEFAULT LUA_FLOAT_DOUBLE + + +/* +@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. +*/ +/* #define LUA_32BITS */ + + +/* +@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for +** C89 ('long' and 'double'); Windows always has '__int64', so it does +** not need to use this case. +*/ +#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) +#define LUA_C89_NUMBERS 1 +#else +#define LUA_C89_NUMBERS 0 +#endif + + +#if defined(LUA_32BITS) /* { */ /* ** 32-bit integers and 'float' */ -#if LUAI_BITSINT >= 32 /* use 'int' if big enough */ +#if LUAI_IS32INT /* use 'int' if big enough */ #define LUA_INT_TYPE LUA_INT_INT #else /* otherwise use 'long' */ #define LUA_INT_TYPE LUA_INT_LONG #endif #define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT -#elif defined(LUA_C89_NUMBERS) /* }{ */ +#elif LUA_C89_NUMBERS /* }{ */ /* ** largest types available for C89 ('long' and 'double') */ #define LUA_INT_TYPE LUA_INT_LONG #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE -#endif /* } */ +#else /* }{ */ +/* use defaults */ +#define LUA_INT_TYPE LUA_INT_DEFAULT +#define LUA_FLOAT_TYPE LUA_FLOAT_DEFAULT -/* -** default configuration for 64-bit Lua ('long long' and 'double') -*/ -#if !defined(LUA_INT_TYPE) -#define LUA_INT_TYPE LUA_INT_LONGLONG -#endif +#endif /* } */ -#if !defined(LUA_FLOAT_TYPE) -#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE -#endif /* }================================================================== */ - /* ** {================================================================== ** Configuration for Paths. @@ -177,6 +211,7 @@ ** hierarchy or if you want to install your libraries in ** non-conventional directories. */ + #define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #if defined(_WIN32) /* { */ /* @@ -186,27 +221,40 @@ #define LUA_LDIR "!\\lua\\" #define LUA_CDIR "!\\" #define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" + +#if !defined(LUA_PATH_DEFAULT) #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ ".\\?.lua;" ".\\?\\init.lua" +#endif + +#if !defined(LUA_CPATH_DEFAULT) #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.dll;" \ LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ LUA_CDIR"loadall.dll;" ".\\?.dll" +#endif #else /* }{ */ #define LUA_ROOT "/usr/local/" #define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" #define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" + +#if !defined(LUA_PATH_DEFAULT) #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ "./?.lua;" "./?/init.lua" +#endif + +#if !defined(LUA_CPATH_DEFAULT) #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" +#endif + #endif /* } */ @@ -215,12 +263,25 @@ ** CHANGE it if your machine does not use "/" as the directory separator ** and is not Windows. (On Windows Lua automatically uses "\".) */ +#if !defined(LUA_DIRSEP) + #if defined(_WIN32) #define LUA_DIRSEP "\\" #else #define LUA_DIRSEP "/" #endif +#endif + + +/* +** LUA_IGMARK is a mark to ignore all after it when building the +** module name (e.g., used to build the luaopen_ function name). +** Typically, the suffix after the mark is the module version, +** as in "mod-v1.2.so". +*/ +#define LUA_IGMARK "-" + /* }================================================================== */ @@ -258,32 +319,13 @@ ** More often than not the libs go together with the core. */ #define LUALIB_API LUA_API -#define LUAMOD_API LUA_API - -/* -@@ LUAI_FUNC is a mark for all extern functions that are not to be -** exported to outside modules. -@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, -** none of which to be exported to outside modules (LUAI_DDEF for -** definitions and LUAI_DDEC for declarations). -** CHANGE them if you need to mark them in some special way. Elf/gcc -** (versions 3.2 and later) mark them as "hidden" to optimize access -** when Lua is compiled as a shared library. Not all elf targets support -** this attribute. Unfortunately, gcc does not offer a way to check -** whether the target offers that support, and those without support -** give a warning about it. To avoid these warnings, change to the -** default definition. -*/ -#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ - defined(__ELF__) /* { */ -#define LUAI_FUNC __attribute__((visibility("internal"))) extern -#else /* }{ */ -#define LUAI_FUNC extern -#endif /* } */ - -#define LUAI_DDEC(dec) LUAI_FUNC dec -#define LUAI_DDEF /* empty */ +#if defined(__cplusplus) +/* Lua uses the "C name" when calling open functions */ +#define LUAMOD_API extern "C" +#else +#define LUAMOD_API LUA_API +#endif /* }================================================================== */ @@ -295,41 +337,26 @@ */ /* -@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.3. -** You can define it to get all options, or change specific options -** to fit your specific needs. +@@ LUA_COMPAT_GLOBAL avoids 'global' being a reserved word */ -#if defined(LUA_COMPAT_5_3) /* { */ +#define LUA_COMPAT_GLOBAL + /* @@ LUA_COMPAT_MATHLIB controls the presence of several deprecated ** functions in the mathematical library. -** (These functions were already officially removed in 5.3, but -** nevertheless they are available by default there.) -*/ -#define LUA_COMPAT_MATHLIB - -/* -@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for -** manipulating other integer types (lua_pushunsigned, lua_tounsigned, -** luaL_checkint, luaL_checklong, etc.) -*/ -#define LUA_COMPAT_APIINTCASTS - -/* -@@ LUA_COMPAT_LT_LE controls the emulation of the '__le' metamethod -** using '__lt'. +** (These functions were already officially removed in 5.3; +** nevertheless they are still available here.) */ -#define LUA_COMPAT_LT_LE - -#endif /* } */ - +/* #define LUA_COMPAT_MATHLIB */ /* @@ The following macros supply trivial compatibility for some ** changes in the API. The macros themselves document how to ** change your code to avoid using them. +** (Once more, these macros were officially removed in 5.3, but they are +** still available here.) */ #define lua_strlen(L,i) lua_rawlen(L, (i)) @@ -344,48 +371,35 @@ /* ** {================================================================== -** Configuration for Numbers. +** Configuration for Numbers (low-level part). ** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* ** satisfy your needs. ** =================================================================== */ /* -@@ LUA_NUMBER is the floating-point type used by Lua. @@ LUAI_UACNUMBER is the result of a 'default argument promotion' @@ over a floating number. -@@ l_mathlim(x) corrects limit name 'x' to the proper float type +@@ l_floatatt(x) corrects float attribute 'x' to the proper float type ** by prefixing it with one of FLT/DBL/LDBL. @@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. -@@ LUA_NUMBER_FMT is the format for writing floats. -@@ lua_number2str converts a float to a string. +@@ LUA_NUMBER_FMT is the format for writing floats with the maximum +** number of digits that respects tostring(tonumber(numeral)) == numeral. +** (That would be floor(log10(2^n)), where n is the number of bits in +** the float mantissa.) +@@ LUA_NUMBER_FMT_N is the format for writing floats with the minimum +** number of digits that ensures tonumber(tostring(number)) == number. +** (That would be LUA_NUMBER_FMT+2.) @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. @@ l_floor takes the floor of a float. -@@ lua_str2number converts a decimal numeric string to a number. +@@ lua_str2number converts a decimal numeral to a number. */ -/* The following definitions are good for most cases here */ +/* The following definition is good for most cases here */ #define l_floor(x) (l_mathop(floor)(x)) -#define lua_number2str(s,sz,n) \ - l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n)) - -/* -@@ lua_numbertointeger converts a float number with an integral value -** to an integer, or returns 0 if float is not within the range of -** a lua_Integer. (The range comparisons are tricky because of -** rounding. The tests here assume a two-complement representation, -** where MININTEGER always has an exact representation as a float; -** MAXINTEGER may not have one, and therefore its conversion to float -** may have an ill-defined value.) -*/ -#define lua_numbertointeger(n,p) \ - ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ - (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ - (*(p) = (LUA_INTEGER)(n), 1)) - /* now the variable definitions */ @@ -393,12 +407,13 @@ #define LUA_NUMBER float -#define l_mathlim(n) (FLT_##n) +#define l_floatatt(n) (FLT_##n) #define LUAI_UACNUMBER double #define LUA_NUMBER_FRMLEN "" #define LUA_NUMBER_FMT "%.7g" +#define LUA_NUMBER_FMT_N "%.9g" #define l_mathop(op) op##f @@ -409,12 +424,13 @@ #define LUA_NUMBER long double -#define l_mathlim(n) (LDBL_##n) +#define l_floatatt(n) (LDBL_##n) #define LUAI_UACNUMBER long double #define LUA_NUMBER_FRMLEN "L" #define LUA_NUMBER_FMT "%.19Lg" +#define LUA_NUMBER_FMT_N "%.21Lg" #define l_mathop(op) op##l @@ -424,12 +440,13 @@ #define LUA_NUMBER double -#define l_mathlim(n) (DBL_##n) +#define l_floatatt(n) (DBL_##n) #define LUAI_UACNUMBER double #define LUA_NUMBER_FRMLEN "" -#define LUA_NUMBER_FMT "%.14g" +#define LUA_NUMBER_FMT "%.15g" +#define LUA_NUMBER_FMT_N "%.17g" #define l_mathop(op) op @@ -444,18 +461,14 @@ /* -@@ LUA_INTEGER is the integer type used by Lua. -** @@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. -** @@ LUAI_UACINT is the result of a 'default argument promotion' -@@ over a lUA_INTEGER. +@@ over a LUA_INTEGER. @@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. @@ LUA_INTEGER_FMT is the format for writing integers. @@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. @@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. @@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED. -@@ LUA_UNSIGNEDBITS is the number of bits in a LUA_UNSIGNED. @@ lua_integer2str converts an integer to a string. */ @@ -475,10 +488,6 @@ */ #define LUA_UNSIGNED unsigned LUAI_UACINT -#define LUA_MAXUNSIGNED (~(lua_Unsigned)0) - -#define LUA_UNSIGNEDBITS (sizeof(LUA_UNSIGNED) * CHAR_BIT) - /* now the variable definitions */ @@ -490,6 +499,8 @@ #define LUA_MAXINTEGER INT_MAX #define LUA_MININTEGER INT_MIN +#define LUA_MAXUNSIGNED UINT_MAX + #elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ #define LUA_INTEGER long @@ -498,6 +509,8 @@ #define LUA_MAXINTEGER LONG_MAX #define LUA_MININTEGER LONG_MIN +#define LUA_MAXUNSIGNED ULONG_MAX + #elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ /* use presence of macro LLONG_MAX as proxy for C99 compliance */ @@ -510,6 +523,8 @@ #define LUA_MAXINTEGER LLONG_MAX #define LUA_MININTEGER LLONG_MIN +#define LUA_MAXUNSIGNED ULLONG_MAX + #elif defined(LUA_USE_WINDOWS) /* }{ */ /* in Windows, can use specific Windows types */ @@ -519,6 +534,8 @@ #define LUA_MAXINTEGER _I64_MAX #define LUA_MININTEGER _I64_MIN +#define LUA_MAXUNSIGNED _UI64_MAX + #else /* }{ */ #error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ @@ -553,7 +570,7 @@ /* -@@ lua_strx2number converts a hexadecimal numeric string to a number. +@@ lua_strx2number converts a hexadecimal numeral to a number. ** In C99, 'strtod' does that conversion. Otherwise, you can ** leave 'lua_strx2number' undefined and Lua will provide its own ** implementation. @@ -571,7 +588,7 @@ /* -@@ lua_number2strx converts a float to a hexadecimal numeric string. +@@ lua_number2strx converts a float to a hexadecimal numeral. ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. ** Otherwise, you can leave 'lua_number2strx' undefined and Lua will ** provide its own implementation. @@ -617,12 +634,33 @@ /* @@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). ** Change that if you do not want to use C locales. (Code using this -** macro must include header 'locale.h'.) +** macro must include the header 'locale.h'.) */ #if !defined(lua_getlocaledecpoint) #define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) #endif + +/* +** macros to improve jump prediction, used mostly for error handling +** and debug facilities. (Some macros in the Lua API use these macros. +** Define LUA_NOBUILTIN if you do not want '__builtin_expect' in your +** code.) +*/ +#if !defined(luai_likely) + +#if defined(__GNUC__) && !defined(LUA_NOBUILTIN) +#define luai_likely(x) (__builtin_expect(((x) != 0), 1)) +#define luai_unlikely(x) (__builtin_expect(((x) != 0), 0)) +#else +#define luai_likely(x) (x) +#define luai_unlikely(x) (x) +#endif + +#endif + + + /* }================================================================== */ @@ -646,10 +684,7 @@ @@ LUA_USE_APICHECK turns on several consistency checks on the C API. ** Define it as a help when debugging C code. */ -#if defined(LUA_USE_APICHECK) -#include -#define luai_apicheck(l,e) assert(e) -#endif +/* #define LUA_USE_APICHECK */ /* }================================================================== */ @@ -658,24 +693,10 @@ ** {================================================================== ** Macros that affect the API and must be stable (that is, must be the ** same when you compile Lua and when you compile code that links to -** Lua). You probably do not want/need to change them. +** Lua). ** ===================================================================== */ -/* -@@ LUAI_MAXSTACK limits the size of the Lua stack. -** CHANGE it if you need a different limit. This limit is arbitrary; -** its only purpose is to stop Lua from consuming unlimited stack -** space (and to reserve some numbers for pseudo-indices). -** (It must fit into max(size_t)/32.) -*/ -#if LUAI_BITSINT >= 32 -#define LUAI_MAXSTACK 1000000 -#else -#define LUAI_MAXSTACK 15000 -#endif - - /* @@ LUA_EXTRASPACE defines the size of a raw memory area associated with ** a Lua state with very fast access. @@ -686,24 +707,18 @@ /* @@ LUA_IDSIZE gives the maximum size for the description of the source -@@ of a function in debug information. +** of a function in debug information. ** CHANGE it if you want a different size. */ #define LUA_IDSIZE 60 /* -@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. -** CHANGE it if it uses too much C-stack space. (For long double, -** 'string.format("%.99f", -1e4932)' needs 5052 bytes, so a -** smaller buffer would force a memory allocation for each call to -** 'string.format'.) +@@ LUAL_BUFFERSIZE is the initial buffer size used by the lauxlib +** buffer system. */ -#if LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE -#define LUAL_BUFFERSIZE 8192 -#else #define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number))) -#endif + /* @@ LUAI_MAXALIGN defines fields that, when used in a union, ensure @@ -726,7 +741,5 @@ - - #endif diff --git a/lualib.h b/lualib.h index eb08b530a6..068f60ab3b 100644 --- a/lualib.h +++ b/lualib.h @@ -14,45 +14,52 @@ /* version suffix for environment variable names */ #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR - +#define LUA_GLIBK 1 LUAMOD_API int (luaopen_base) (lua_State *L); +#define LUA_LOADLIBNAME "package" +#define LUA_LOADLIBK (LUA_GLIBK << 1) +LUAMOD_API int (luaopen_package) (lua_State *L); + + #define LUA_COLIBNAME "coroutine" +#define LUA_COLIBK (LUA_LOADLIBK << 1) LUAMOD_API int (luaopen_coroutine) (lua_State *L); -#define LUA_TABLIBNAME "table" -LUAMOD_API int (luaopen_table) (lua_State *L); +#define LUA_DBLIBNAME "debug" +#define LUA_DBLIBK (LUA_COLIBK << 1) +LUAMOD_API int (luaopen_debug) (lua_State *L); #define LUA_IOLIBNAME "io" +#define LUA_IOLIBK (LUA_DBLIBK << 1) LUAMOD_API int (luaopen_io) (lua_State *L); +#define LUA_MATHLIBNAME "math" +#define LUA_MATHLIBK (LUA_IOLIBK << 1) +LUAMOD_API int (luaopen_math) (lua_State *L); + #define LUA_OSLIBNAME "os" +#define LUA_OSLIBK (LUA_MATHLIBK << 1) LUAMOD_API int (luaopen_os) (lua_State *L); #define LUA_STRLIBNAME "string" +#define LUA_STRLIBK (LUA_OSLIBK << 1) LUAMOD_API int (luaopen_string) (lua_State *L); +#define LUA_TABLIBNAME "table" +#define LUA_TABLIBK (LUA_STRLIBK << 1) +LUAMOD_API int (luaopen_table) (lua_State *L); + #define LUA_UTF8LIBNAME "utf8" +#define LUA_UTF8LIBK (LUA_TABLIBK << 1) LUAMOD_API int (luaopen_utf8) (lua_State *L); -#define LUA_MATHLIBNAME "math" -LUAMOD_API int (luaopen_math) (lua_State *L); -#define LUA_DBLIBNAME "debug" -LUAMOD_API int (luaopen_debug) (lua_State *L); +/* open selected libraries */ +LUALIB_API void (luaL_openselectedlibs) (lua_State *L, int load, int preload); -#define LUA_LOADLIBNAME "package" -LUAMOD_API int (luaopen_package) (lua_State *L); - - -/* open all previous libraries */ -LUALIB_API void (luaL_openlibs) (lua_State *L); - - - -#if !defined(lua_assert) -#define lua_assert(x) ((void)0) -#endif +/* open all libraries */ +#define luaL_openlibs(L) luaL_openselectedlibs(L, ~0, 0) #endif diff --git a/lundump.c b/lundump.c index 00ff6deffb..3b61cc8cbb 100644 --- a/lundump.c +++ b/lundump.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include "lua.h" @@ -20,12 +21,13 @@ #include "lmem.h" #include "lobject.h" #include "lstring.h" +#include "ltable.h" #include "lundump.h" #include "lzio.h" #if !defined(luai_verifycode) -#define luai_verifycode(L,b,f) /* empty */ +#define luai_verifycode(L,f) /* empty */ #endif @@ -33,275 +35,390 @@ typedef struct { lua_State *L; ZIO *Z; const char *name; + Table *h; /* list for string reuse */ + size_t offset; /* current position relative to beginning of dump */ + lua_Unsigned nstr; /* number of strings in the list */ + lu_byte fixed; /* dump is fixed in memory */ } LoadState; static l_noret error (LoadState *S, const char *why) { - luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); + luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why); luaD_throw(S->L, LUA_ERRSYNTAX); } /* -** All high-level loads go through LoadVector; you can change it to +** All high-level loads go through loadVector; you can change it to ** adapt to the endianness of the input */ -#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) +#define loadVector(S,b,n) loadBlock(S,b,cast_sizet(n)*sizeof((b)[0])) -static void LoadBlock (LoadState *S, void *b, size_t size) { +static void loadBlock (LoadState *S, void *b, size_t size) { if (luaZ_read(S->Z, b, size) != 0) - error(S, "truncated"); + error(S, "truncated chunk"); + S->offset += size; } -#define LoadVar(S,x) LoadVector(S,&x,1) +static void loadAlign (LoadState *S, unsigned align) { + unsigned padding = align - cast_uint(S->offset % align); + if (padding < align) { /* (padding == align) means no padding */ + lua_Integer paddingContent; + loadBlock(S, &paddingContent, padding); + lua_assert(S->offset % align == 0); + } +} + + +#define getaddr(S,n,t) cast(t *, getaddr_(S,cast_sizet(n) * sizeof(t))) + +static const void *getaddr_ (LoadState *S, size_t size) { + const void *block = luaZ_getaddr(S->Z, size); + S->offset += size; + if (block == NULL) + error(S, "truncated fixed buffer"); + return block; +} -static lu_byte LoadByte (LoadState *S) { +#define loadVar(S,x) loadVector(S,&x,1) + + +static lu_byte loadByte (LoadState *S) { int b = zgetc(S->Z); if (b == EOZ) - error(S, "truncated"); + error(S, "truncated chunk"); + S->offset++; return cast_byte(b); } -static size_t LoadSize (LoadState *S) { - size_t x = 0; +static lua_Unsigned loadVarint (LoadState *S, lua_Unsigned limit) { + lua_Unsigned x = 0; int b; + limit >>= 7; do { - b = LoadByte(S); + b = loadByte(S); + if (x > limit) + error(S, "integer overflow"); x = (x << 7) | (b & 0x7f); - } while ((b & 0x80) == 0); + } while ((b & 0x80) != 0); return x; } -static int LoadInt (LoadState *S) { - return cast_int(LoadSize(S)); +static size_t loadSize (LoadState *S) { + return cast_sizet(loadVarint(S, MAX_SIZE)); } -static lua_Number LoadNumber (LoadState *S) { - lua_Number x; - LoadVar(S, x); - return x; +static int loadInt (LoadState *S) { + return cast_int(loadVarint(S, cast_sizet(INT_MAX))); } -static lua_Integer LoadInteger (LoadState *S) { - lua_Integer x; - LoadVar(S, x); + +static lua_Number loadNumber (LoadState *S) { + lua_Number x; + loadVar(S, x); return x; } -/* -** Load a nullable string -*/ -static TString *LoadStringN (LoadState *S) { - size_t size = LoadSize(S); - if (size == 0) - return NULL; - else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ - char buff[LUAI_MAXSHORTLEN]; - LoadVector(S, buff, size); - return luaS_newlstr(S->L, buff, size); - } - else { /* long string */ - TString *ts = luaS_createlngstrobj(S->L, size); - LoadVector(S, getstr(ts), size); /* load directly in final place */ - return ts; - } +static lua_Integer loadInteger (LoadState *S) { + lua_Unsigned cx = loadVarint(S, LUA_MAXUNSIGNED); + /* decode unsigned to signed */ + if ((cx & 1) != 0) + return l_castU2S(~(cx >> 1)); + else + return l_castU2S(cx >> 1); } /* -** Load a non-nullable string. +** Load a nullable string into slot 'sl' from prototype 'p'. The +** assignment to the slot and the barrier must be performed before any +** possible GC activity, to anchor the string. (Both 'loadVector' and +** 'luaH_setint' can call the GC.) */ -static TString *LoadString (LoadState *S) { - TString *st = LoadStringN(S); - if (st == NULL) - error(S, "bad format for constant string"); - return st; +static void loadString (LoadState *S, Proto *p, TString **sl) { + lua_State *L = S->L; + TString *ts; + TValue sv; + size_t size = loadSize(S); + if (size == 0) { /* previously saved string? */ + lua_Unsigned idx = loadVarint(S, LUA_MAXUNSIGNED); /* get its index */ + TValue stv; + if (idx == 0) { /* no string? */ + lua_assert(*sl == NULL); /* must be prefilled */ + return; + } + if (novariant(luaH_getint(S->h, l_castU2S(idx), &stv)) != LUA_TSTRING) + error(S, "invalid string index"); + *sl = ts = tsvalue(&stv); /* get its value */ + luaC_objbarrier(L, p, ts); + return; /* do not save it again */ + } + else if ((size -= 1) <= LUAI_MAXSHORTLEN) { /* short string? */ + char buff[LUAI_MAXSHORTLEN + 1]; /* extra space for '\0' */ + loadVector(S, buff, size + 1); /* load string into buffer */ + *sl = ts = luaS_newlstr(L, buff, size); /* create string */ + luaC_objbarrier(L, p, ts); + } + else if (S->fixed) { /* for a fixed buffer, use a fixed string */ + const char *s = getaddr(S, size + 1, char); /* get content address */ + *sl = ts = luaS_newextlstr(L, s, size, NULL, NULL); + luaC_objbarrier(L, p, ts); + } + else { /* create internal copy */ + *sl = ts = luaS_createlngstrobj(L, size); /* create string */ + luaC_objbarrier(L, p, ts); + loadVector(S, getlngstr(ts), size + 1); /* load directly in final place */ + } + /* add string to list of saved strings */ + S->nstr++; + setsvalue(L, &sv, ts); + luaH_setint(L, S->h, l_castU2S(S->nstr), &sv); + luaC_objbarrierback(L, obj2gco(S->h), ts); } -static void LoadCode (LoadState *S, Proto *f) { - int n = LoadInt(S); - f->code = luaM_newvectorchecked(S->L, n, Instruction); - f->sizecode = n; - LoadVector(S, f->code, n); +static void loadCode (LoadState *S, Proto *f) { + int n = loadInt(S); + loadAlign(S, sizeof(f->code[0])); + if (S->fixed) { + f->code = getaddr(S, n, Instruction); + f->sizecode = n; + } + else { + f->code = luaM_newvectorchecked(S->L, n, Instruction); + f->sizecode = n; + loadVector(S, f->code, n); + } } -static void LoadFunction(LoadState *S, Proto *f, TString *psource); +static void loadFunction(LoadState *S, Proto *f); -static void LoadConstants (LoadState *S, Proto *f) { +static void loadConstants (LoadState *S, Proto *f) { int i; - int n = LoadInt(S); + int n = loadInt(S); f->k = luaM_newvectorchecked(S->L, n, TValue); f->sizek = n; for (i = 0; i < n; i++) setnilvalue(&f->k[i]); for (i = 0; i < n; i++) { TValue *o = &f->k[i]; - int t = LoadByte(S); + int t = loadByte(S); switch (t) { - case LUA_TNIL: + case LUA_VNIL: setnilvalue(o); break; - case LUA_TBOOLEAN: - setbvalue(o, LoadByte(S)); + case LUA_VFALSE: + setbfvalue(o); + break; + case LUA_VTRUE: + setbtvalue(o); break; - case LUA_TNUMFLT: - setfltvalue(o, LoadNumber(S)); + case LUA_VNUMFLT: + setfltvalue(o, loadNumber(S)); break; - case LUA_TNUMINT: - setivalue(o, LoadInteger(S)); + case LUA_VNUMINT: + setivalue(o, loadInteger(S)); break; - case LUA_TSHRSTR: - case LUA_TLNGSTR: - setsvalue2n(S->L, o, LoadString(S)); + case LUA_VSHRSTR: + case LUA_VLNGSTR: { + lua_assert(f->source == NULL); + loadString(S, f, &f->source); /* use 'source' to anchor string */ + if (f->source == NULL) + error(S, "bad format for constant string"); + setsvalue2n(S->L, o, f->source); /* save it in the right place */ + f->source = NULL; break; - default: lua_assert(0); + } + default: error(S, "invalid constant"); } } } -static void LoadProtos (LoadState *S, Proto *f) { +static void loadProtos (LoadState *S, Proto *f) { int i; - int n = LoadInt(S); + int n = loadInt(S); f->p = luaM_newvectorchecked(S->L, n, Proto *); f->sizep = n; for (i = 0; i < n; i++) f->p[i] = NULL; for (i = 0; i < n; i++) { f->p[i] = luaF_newproto(S->L); - LoadFunction(S, f->p[i], f->source); + luaC_objbarrier(S->L, f, f->p[i]); + loadFunction(S, f->p[i]); } } -static void LoadUpvalues (LoadState *S, Proto *f) { - int i, n; - n = LoadInt(S); +/* +** Load the upvalues for a function. The names must be filled first, +** because the filling of the other fields can raise read errors and +** the creation of the error message can call an emergency collection; +** in that case all prototypes must be consistent for the GC. +*/ +static void loadUpvalues (LoadState *S, Proto *f) { + int i; + int n = loadInt(S); f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); f->sizeupvalues = n; - for (i = 0; i < n; i++) + for (i = 0; i < n; i++) /* make array valid for GC */ f->upvalues[i].name = NULL; - for (i = 0; i < n; i++) { - f->upvalues[i].instack = LoadByte(S); - f->upvalues[i].idx = LoadByte(S); + for (i = 0; i < n; i++) { /* following calls can raise errors */ + f->upvalues[i].instack = loadByte(S); + f->upvalues[i].idx = loadByte(S); + f->upvalues[i].kind = loadByte(S); } } -static void LoadDebug (LoadState *S, Proto *f) { - int i, n; - n = LoadInt(S); - f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); - f->sizelineinfo = n; - LoadVector(S, f->lineinfo, n); - n = LoadInt(S); - f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); - f->sizeabslineinfo = n; - for (i = 0; i < n; i++) { - f->abslineinfo[i].pc = LoadInt(S); - f->abslineinfo[i].line = LoadInt(S); +static void loadDebug (LoadState *S, Proto *f) { + int i; + int n = loadInt(S); + if (S->fixed) { + f->lineinfo = getaddr(S, n, ls_byte); + f->sizelineinfo = n; + } + else { + f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); + f->sizelineinfo = n; + loadVector(S, f->lineinfo, n); + } + n = loadInt(S); + if (n > 0) { + loadAlign(S, sizeof(int)); + if (S->fixed) { + f->abslineinfo = getaddr(S, n, AbsLineInfo); + f->sizeabslineinfo = n; + } + else { + f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); + f->sizeabslineinfo = n; + loadVector(S, f->abslineinfo, n); + } } - n = LoadInt(S); + n = loadInt(S); f->locvars = luaM_newvectorchecked(S->L, n, LocVar); f->sizelocvars = n; for (i = 0; i < n; i++) f->locvars[i].varname = NULL; for (i = 0; i < n; i++) { - f->locvars[i].varname = LoadStringN(S); - f->locvars[i].startpc = LoadInt(S); - f->locvars[i].endpc = LoadInt(S); + loadString(S, f, &f->locvars[i].varname); + f->locvars[i].startpc = loadInt(S); + f->locvars[i].endpc = loadInt(S); } - n = LoadInt(S); + n = loadInt(S); + if (n != 0) /* does it have debug information? */ + n = f->sizeupvalues; /* must be this many */ for (i = 0; i < n; i++) - f->upvalues[i].name = LoadStringN(S); + loadString(S, f, &f->upvalues[i].name); } -static void LoadFunction (LoadState *S, Proto *f, TString *psource) { - f->source = LoadStringN(S); - if (f->source == NULL) /* no source in dump? */ - f->source = psource; /* reuse parent's source */ - f->linedefined = LoadInt(S); - f->lastlinedefined = LoadInt(S); - f->numparams = LoadByte(S); - f->is_vararg = LoadByte(S); - f->maxstacksize = LoadByte(S); - LoadCode(S, f); - LoadConstants(S, f); - LoadUpvalues(S, f); - LoadProtos(S, f); - LoadDebug(S, f); +static void loadFunction (LoadState *S, Proto *f) { + f->linedefined = loadInt(S); + f->lastlinedefined = loadInt(S); + f->numparams = loadByte(S); + /* get only the meaningful flags */ + f->flag = cast_byte(loadByte(S) & ~PF_FIXED); + if (S->fixed) + f->flag |= PF_FIXED; /* signal that code is fixed */ + f->maxstacksize = loadByte(S); + loadCode(S, f); + loadConstants(S, f); + loadUpvalues(S, f); + loadProtos(S, f); + loadString(S, f, &f->source); + loadDebug(S, f); } static void checkliteral (LoadState *S, const char *s, const char *msg) { char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ size_t len = strlen(s); - LoadVector(S, buff, len); + loadVector(S, buff, len); if (memcmp(s, buff, len) != 0) error(S, msg); } -static void fchecksize (LoadState *S, size_t size, const char *tname) { - if (LoadByte(S) != size) - error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); +static l_noret numerror (LoadState *S, const char *what, const char *tname) { + const char *msg = luaO_pushfstring(S->L, "%s %s mismatch", tname, what); + error(S, msg); +} + + +static void checknumsize (LoadState *S, int size, const char *tname) { + if (size != loadByte(S)) + numerror(S, "size", tname); } -#define checksize(S,t) fchecksize(S,sizeof(t),#t) +static void checknumformat (LoadState *S, int eq, const char *tname) { + if (!eq) + numerror(S, "format", tname); +} + + +#define checknum(S,tvar,value,tname) \ + { tvar i; checknumsize(S, sizeof(i), tname); \ + loadVar(S, i); \ + checknumformat(S, i == value, tname); } + static void checkHeader (LoadState *S) { - checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ - if (LoadByte(S) != LUAC_VERSION) - error(S, "version mismatch in"); - if (LoadByte(S) != LUAC_FORMAT) - error(S, "format mismatch in"); - checkliteral(S, LUAC_DATA, "corrupted"); - checksize(S, int); - checksize(S, size_t); - checksize(S, Instruction); - checksize(S, lua_Integer); - checksize(S, lua_Number); - if (LoadInteger(S) != LUAC_INT) - error(S, "endianness mismatch in"); - if (LoadNumber(S) != LUAC_NUM) - error(S, "float format mismatch in"); + /* skip 1st char (already read and checked) */ + checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk"); + if (loadByte(S) != LUAC_VERSION) + error(S, "version mismatch"); + if (loadByte(S) != LUAC_FORMAT) + error(S, "format mismatch"); + checkliteral(S, LUAC_DATA, "corrupted chunk"); + checknum(S, int, LUAC_INT, "int"); + checknum(S, Instruction, LUAC_INST, "instruction"); + checknum(S, lua_Integer, LUAC_INT, "Lua integer"); + checknum(S, lua_Number, LUAC_NUM, "Lua number"); } /* -** load precompiled chunk +** Load precompiled chunk. */ -LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { +LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) { LoadState S; LClosure *cl; if (*name == '@' || *name == '=') - S.name = name + 1; + name = name + 1; else if (*name == LUA_SIGNATURE[0]) - S.name = "binary string"; - else - S.name = name; + name = "binary string"; + S.name = name; S.L = L; S.Z = Z; + S.fixed = cast_byte(fixed); + S.offset = 1; /* fist byte was already read */ checkHeader(&S); - cl = luaF_newLclosure(L, LoadByte(&S)); - setclLvalue2s(L, L->top, cl); + cl = luaF_newLclosure(L, loadByte(&S)); + setclLvalue2s(L, L->top.p, cl); + luaD_inctop(L); + S.h = luaH_new(L); /* create list of saved strings */ + S.nstr = 0; + sethvalue2s(L, L->top.p, S.h); /* anchor it */ luaD_inctop(L); cl->p = luaF_newproto(L); - LoadFunction(&S, cl->p, NULL); - lua_assert(cl->nupvalues == cl->p->sizeupvalues); - luai_verifycode(L, buff, cl->p); + luaC_objbarrier(L, cl, cl->p); + loadFunction(&S, cl->p); + if (cl->nupvalues != cl->p->sizeupvalues) + error(&S, "corrupted chunk"); + luai_verifycode(L, cl->p); + L->top.p--; /* pop table */ return cl; } diff --git a/lundump.h b/lundump.h index 739c7fcde0..c4e06f9ebd 100644 --- a/lundump.h +++ b/lundump.h @@ -7,6 +7,8 @@ #ifndef lundump_h #define lundump_h +#include + #include "llimits.h" #include "lobject.h" #include "lzio.h" @@ -15,15 +17,21 @@ /* data to catch conversion errors */ #define LUAC_DATA "\x19\x93\r\n\x1a\n" -#define LUAC_INT 0x5678 -#define LUAC_NUM cast_num(370.5) +#define LUAC_INT -0x5678 +#define LUAC_INST 0x12345678 +#define LUAC_NUM cast_num(-370.5) + +/* +** Encode major-minor version in one byte, one nibble for each +*/ +#define LUAC_VERSION (LUA_VERSION_MAJOR_N*16+LUA_VERSION_MINOR_N) -#define MYINT(s) (s[0]-'0') -#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) #define LUAC_FORMAT 0 /* this is the official format */ + /* load one chunk; from lundump.c */ -LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); +LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name, + int fixed); /* dump one chunk; from ldump.c */ LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, diff --git a/lutf8lib.c b/lutf8lib.c index ec711c9a10..b7f3fe1e16 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -10,7 +10,6 @@ #include "lprefix.h" -#include #include #include #include @@ -19,23 +18,19 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" #define MAXUNICODE 0x10FFFFu #define MAXUTF 0x7FFFFFFFu -/* -** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits. -*/ -#if LUAI_BITSINT >= 31 -typedef unsigned int utfint; -#else -typedef unsigned long utfint; -#endif + +#define MSGInvalid "invalid UTF-8 code" -#define iscont(p) ((*(p) & 0xC0) == 0x80) +#define iscont(c) (((c) & 0xC0) == 0x80) +#define iscontp(p) iscont(*(p)) /* from strlib */ @@ -51,25 +46,25 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) { ** Decode one UTF-8 sequence, returning NULL if byte sequence is ** invalid. The array 'limits' stores the minimum value for each ** sequence length, to check for overlong representations. Its first -** entry forces an error for non-ascii bytes with no continuation +** entry forces an error for non-ASCII bytes with no continuation ** bytes (count == 0). */ -static const char *utf8_decode (const char *s, utfint *val, int strict) { - static const utfint limits[] = - {~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; +static const char *utf8_decode (const char *s, l_uint32 *val, int strict) { + static const l_uint32 limits[] = + {~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; unsigned int c = (unsigned char)s[0]; - utfint res = 0; /* final result */ - if (c < 0x80) /* ascii? */ + l_uint32 res = 0; /* final result */ + if (c < 0x80) /* ASCII? */ res = c; else { int count = 0; /* to count number of continuation bytes */ for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */ unsigned int cc = (unsigned char)s[++count]; /* read next byte */ - if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ + if (!iscont(cc)) /* not a continuation byte? */ return NULL; /* invalid byte sequence */ res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ } - res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */ + res |= ((l_uint32)(c & 0x7F) << (count * 5)); /* add first byte */ if (count > 5 || res > MAXUTF || res < limits[count]) return NULL; /* invalid byte sequence */ s += count; /* skip continuation bytes read */ @@ -85,7 +80,7 @@ static const char *utf8_decode (const char *s, utfint *val, int strict) { /* -** utf8len(s [, i [, j [, nonstrict]]]) --> number of characters that +** utf8len(s [, i [, j [, lax]]]) --> number of characters that ** start in the range [i,j], or nil + current position if 's' is not ** well formed in that interval */ @@ -95,19 +90,19 @@ static int utflen (lua_State *L) { const char *s = luaL_checklstring(L, 1, &len); lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); - int nonstrict = lua_toboolean(L, 4); + int lax = lua_toboolean(L, 4); luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, - "initial position out of string"); + "initial position out of bounds"); luaL_argcheck(L, --posj < (lua_Integer)len, 3, - "final position out of string"); + "final position out of bounds"); while (posi <= posj) { - const char *s1 = utf8_decode(s + posi, NULL, !nonstrict); + const char *s1 = utf8_decode(s + posi, NULL, !lax); if (s1 == NULL) { /* conversion error? */ - lua_pushnil(L); /* return nil ... */ + luaL_pushfail(L); /* return fail ... */ lua_pushinteger(L, posi + 1); /* ... and current position */ return 2; } - posi = s1 - s; + posi = ct_diff2S(s1 - s); n++; } lua_pushinteger(L, n); @@ -116,7 +111,7 @@ static int utflen (lua_State *L) { /* -** codepoint(s, [i, [j [, nonstrict]]]) -> returns codepoints for all +** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all ** characters that start in the range [i,j] */ static int codepoint (lua_State *L) { @@ -124,11 +119,11 @@ static int codepoint (lua_State *L) { const char *s = luaL_checklstring(L, 1, &len); lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); - int nonstrict = lua_toboolean(L, 4); + int lax = lua_toboolean(L, 4); int n; const char *se; - luaL_argcheck(L, posi >= 1, 2, "out of range"); - luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range"); + luaL_argcheck(L, posi >= 1, 2, "out of bounds"); + luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds"); if (posi > pose) return 0; /* empty interval; return no values */ if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ return luaL_error(L, "string slice too long"); @@ -137,11 +132,11 @@ static int codepoint (lua_State *L) { n = 0; /* count the number of returns */ se = s + pose; /* string end */ for (s += posi - 1; s < se;) { - utfint code; - s = utf8_decode(s, &code, !nonstrict); + l_uint32 code; + s = utf8_decode(s, &code, !lax); if (s == NULL) - return luaL_error(L, "invalid UTF-8 code"); - lua_pushinteger(L, code); + return luaL_error(L, MSGInvalid); + lua_pushinteger(L, l_castU2S(code)); n++; } return n; @@ -177,69 +172,75 @@ static int utfchar (lua_State *L) { /* -** offset(s, n, [i]) -> index where n-th character counting from -** position 'i' starts; 0 means character at 'i'. +** offset(s, n, [i]) -> indices where n-th character counting from +** position 'i' starts and ends; 0 means character at 'i'. */ static int byteoffset (lua_State *L) { size_t len; const char *s = luaL_checklstring(L, 1, &len); lua_Integer n = luaL_checkinteger(L, 2); - lua_Integer posi = (n >= 0) ? 1 : len + 1; + lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1; posi = u_posrelat(luaL_optinteger(L, 3, posi), len); luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, - "position out of range"); + "position out of bounds"); if (n == 0) { /* find beginning of current byte sequence */ - while (posi > 0 && iscont(s + posi)) posi--; + while (posi > 0 && iscontp(s + posi)) posi--; } else { - if (iscont(s + posi)) + if (iscontp(s + posi)) return luaL_error(L, "initial position is a continuation byte"); if (n < 0) { - while (n < 0 && posi > 0) { /* move back */ - do { /* find beginning of previous character */ - posi--; - } while (posi > 0 && iscont(s + posi)); - n++; - } - } - else { - n--; /* do not move for 1st character */ - while (n > 0 && posi < (lua_Integer)len) { - do { /* find beginning of next character */ - posi++; - } while (iscont(s + posi)); /* (cannot pass final '\0') */ - n--; - } - } + while (n < 0 && posi > 0) { /* move back */ + do { /* find beginning of previous character */ + posi--; + } while (posi > 0 && iscontp(s + posi)); + n++; + } + } + else { + n--; /* do not move for 1st character */ + while (n > 0 && posi < (lua_Integer)len) { + do { /* find beginning of next character */ + posi++; + } while (iscontp(s + posi)); /* (cannot pass final '\0') */ + n--; + } + } } - if (n == 0) /* did it find given character? */ - lua_pushinteger(L, posi + 1); - else /* no such character */ - lua_pushnil(L); - return 1; + if (n != 0) { /* did not find given character? */ + luaL_pushfail(L); + return 1; + } + lua_pushinteger(L, posi + 1); /* initial position */ + if ((s[posi] & 0x80) != 0) { /* multi-byte character? */ + if (iscont(s[posi])) + return luaL_error(L, "initial position is a continuation byte"); + while (iscontp(s + posi + 1)) + posi++; /* skip to last continuation byte */ + } + /* else one-byte character: final position is the initial one */ + lua_pushinteger(L, posi + 1); /* 'posi' now is the final position */ + return 2; } static int iter_aux (lua_State *L, int strict) { size_t len; const char *s = luaL_checklstring(L, 1, &len); - lua_Integer n = lua_tointeger(L, 2) - 1; - if (n < 0) /* first iteration? */ - n = 0; /* start from here */ - else if (n < (lua_Integer)len) { - n++; /* skip current byte */ - while (iscont(s + n)) n++; /* and its continuations */ + lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2); + if (n < len) { + while (iscontp(s + n)) n++; /* go to next character */ } - if (n >= (lua_Integer)len) + if (n >= len) /* (also handles original 'n' being negative) */ return 0; /* no more codepoints */ else { - utfint code; + l_uint32 code; const char *next = utf8_decode(s + n, &code, strict); - if (next == NULL) - return luaL_error(L, "invalid UTF-8 code"); - lua_pushinteger(L, n + 1); - lua_pushinteger(L, code); + if (next == NULL || iscontp(next)) + return luaL_error(L, MSGInvalid); + lua_pushinteger(L, l_castU2S(n + 1)); + lua_pushinteger(L, l_castU2S(code)); return 2; } } @@ -249,15 +250,16 @@ static int iter_auxstrict (lua_State *L) { return iter_aux(L, 1); } -static int iter_auxnostrict (lua_State *L) { +static int iter_auxlax (lua_State *L) { return iter_aux(L, 0); } static int iter_codes (lua_State *L) { - int nonstrict = lua_toboolean(L, 2); - luaL_checkstring(L, 1); - lua_pushcfunction(L, nonstrict ? iter_auxnostrict : iter_auxstrict); + int lax = lua_toboolean(L, 2); + const char *s = luaL_checkstring(L, 1); + luaL_argcheck(L, !iscontp(s), 1, MSGInvalid); + lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict); lua_pushvalue(L, 1); lua_pushinteger(L, 0); return 3; diff --git a/lvm.c b/lvm.c index 23e7ff700c..c70e2b8a8a 100644 --- a/lvm.c +++ b/lvm.c @@ -18,6 +18,7 @@ #include "lua.h" +#include "lapi.h" #include "ldebug.h" #include "ldo.h" #include "lfunc.h" @@ -55,7 +56,7 @@ */ /* number of bits in the mantissa of a float */ -#define NBM (l_mathlim(MANT_DIG)) +#define NBM (l_floatatt(MANT_DIG)) /* ** Check whether some integers may not fit in a float, testing whether @@ -80,6 +81,25 @@ #endif +/* +** Try to convert a value from string to a number value. +** If the value is not a string or is a string not representing +** a valid numeral (or if coercions from strings to numbers +** are disabled via macro 'cvt2num'), do not modify 'result' +** and return 0. +*/ +static int l_strton (const TValue *obj, TValue *result) { + lua_assert(obj != result); + if (!cvt2num(obj)) /* is object not a string? */ + return 0; + else { + TString *st = tsvalue(obj); + size_t stlen; + const char *s = getlstr(st, stlen); + return (luaO_str2num(s, result) == stlen + 1); + } +} + /* ** Try to convert a value to a float. The float case is already handled @@ -91,8 +111,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { *n = cast_num(ivalue(obj)); return 1; } - else if (cvt2num(obj) && /* string coercible to number? */ - luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { + else if (l_strton(obj, &v)) { /* string coercible to number? */ *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ return 1; } @@ -102,17 +121,14 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { /* -** try to convert a float to an integer, rounding according to 'mode': -** mode == 0: accepts only integral values -** mode == 1: takes the floor of the number -** mode == 2: takes the ceil of the number +** try to convert a float to an integer, rounding according to 'mode'. */ -int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) { +int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) { lua_Number f = l_floor(n); if (n != f) { /* not an integral value? */ - if (mode == 0) return 0; /* fails if mode demands integral value */ - else if (mode > 1) /* needs ceil? */ - f += 1; /* convert floor to ceil (remember: n != f) */ + if (mode == F2Ieq) return 0; /* fails if mode demands integral value */ + else if (mode == F2Iceil) /* needs ceiling? */ + f += 1; /* convert floor to ceiling (remember: n != f) */ } return lua_numbertointeger(f, p); } @@ -123,7 +139,7 @@ int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) { ** without string coercion. ** ("Fast track" handled by macro 'tointegerns'.) */ -int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) { +int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) { if (ttisfloat(obj)) return luaV_flttointeger(fltvalue(obj), p, mode); else if (ttisinteger(obj)) { @@ -138,113 +154,196 @@ int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) { /* ** try to convert a value to an integer. */ -int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { +int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) { TValue v; - if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) - obj = &v; /* change string to its corresponding number */ + if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */ + obj = &v; /* change it to point to its corresponding number */ return luaV_tointegerns(obj, p, mode); } /* ** Try to convert a 'for' limit to an integer, preserving the semantics -** of the loop. (The following explanation assumes a non-negative step; -** it is valid for negative steps mutatis mutandis.) +** of the loop. Return true if the loop must not run; otherwise, '*p' +** gets the integer limit. +** (The following explanation assumes a positive step; it is valid for +** negative steps mutatis mutandis.) ** If the limit is an integer or can be converted to an integer, -** rounding down, that is it. -** Otherwise, check whether the limit can be converted to a float. If -** the number is too large, it is OK to set the limit as LUA_MAXINTEGER, -** which means no limit. If the number is too negative, the loop -** should not run, because any initial integer value is larger than the -** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects -** the extreme case when the initial value is LUA_MININTEGER, in which -** case the LUA_MININTEGER limit would still run the loop once. +** rounding down, that is the limit. +** Otherwise, check whether the limit can be converted to a float. If +** the float is too large, clip it to LUA_MAXINTEGER. If the float +** is too negative, the loop should not run, because any initial +** integer value is greater than such limit; so, the function returns +** true to signal that. (For this latter case, no integer limit would be +** correct; even a limit of LUA_MININTEGER would run the loop once for +** an initial value equal to LUA_MININTEGER.) */ -static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, - int *stopnow) { - *stopnow = 0; /* usually, let loops run */ - if (ttisinteger(obj)) - *p = ivalue(obj); - else if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) { +static int forlimit (lua_State *L, lua_Integer init, const TValue *lim, + lua_Integer *p, lua_Integer step) { + if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) { /* not coercible to in integer */ - lua_Number n; /* try to convert to float */ - if (!tonumber(obj, &n)) /* cannot convert to float? */ - return 0; /* not a number */ - if (luai_numlt(0, n)) { /* if true, float is larger than max integer */ - *p = LUA_MAXINTEGER; - if (step < 0) *stopnow = 1; + lua_Number flim; /* try to convert to float */ + if (!tonumber(lim, &flim)) /* cannot convert to float? */ + luaG_forerror(L, lim, "limit"); + /* else 'flim' is a float out of integer bounds */ + if (luai_numlt(0, flim)) { /* if it is positive, it is too large */ + if (step < 0) return 1; /* initial value must be less than it */ + *p = LUA_MAXINTEGER; /* truncate */ + } + else { /* it is less than min integer */ + if (step > 0) return 1; /* initial value must be greater than it */ + *p = LUA_MININTEGER; /* truncate */ } - else { /* float is less than min integer */ - *p = LUA_MININTEGER; - if (step >= 0) *stopnow = 1; + } + return (step > 0 ? init > *p : init < *p); /* not to run? */ +} + + +/* +** Prepare a numerical for loop (opcode OP_FORPREP). +** Before execution, stack is as follows: +** ra : initial value +** ra + 1 : limit +** ra + 2 : step +** Return true to skip the loop. Otherwise, +** after preparation, stack will be as follows: +** ra : loop counter (integer loops) or limit (float loops) +** ra + 1 : step +** ra + 2 : control variable +*/ +static int forprep (lua_State *L, StkId ra) { + TValue *pinit = s2v(ra); + TValue *plimit = s2v(ra + 1); + TValue *pstep = s2v(ra + 2); + if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */ + lua_Integer init = ivalue(pinit); + lua_Integer step = ivalue(pstep); + lua_Integer limit; + if (step == 0) + luaG_runerror(L, "'for' step is zero"); + if (forlimit(L, init, plimit, &limit, step)) + return 1; /* skip the loop */ + else { /* prepare loop counter */ + lua_Unsigned count; + if (step > 0) { /* ascending loop? */ + count = l_castS2U(limit) - l_castS2U(init); + if (step != 1) /* avoid division in the too common case */ + count /= l_castS2U(step); + } + else { /* step < 0; descending loop */ + count = l_castS2U(init) - l_castS2U(limit); + /* 'step+1' avoids negating 'mininteger' */ + count /= l_castS2U(-(step + 1)) + 1u; + } + /* use 'chgivalue' for places that for sure had integers */ + chgivalue(s2v(ra), l_castU2S(count)); /* change init to count */ + setivalue(s2v(ra + 1), step); /* change limit to step */ + chgivalue(s2v(ra + 2), init); /* change step to init */ + } + } + else { /* try making all values floats */ + lua_Number init; lua_Number limit; lua_Number step; + if (l_unlikely(!tonumber(plimit, &limit))) + luaG_forerror(L, plimit, "limit"); + if (l_unlikely(!tonumber(pstep, &step))) + luaG_forerror(L, pstep, "step"); + if (l_unlikely(!tonumber(pinit, &init))) + luaG_forerror(L, pinit, "initial value"); + if (step == 0) + luaG_runerror(L, "'for' step is zero"); + if (luai_numlt(0, step) ? luai_numlt(limit, init) + : luai_numlt(init, limit)) + return 1; /* skip the loop */ + else { + /* make sure all values are floats */ + setfltvalue(s2v(ra), limit); + setfltvalue(s2v(ra + 1), step); + setfltvalue(s2v(ra + 2), init); /* control variable */ } } - return 1; + return 0; } /* -** Finish the table access 'val = t[key]'. -** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to -** t[k] entry (which must be empty). +** Execute a step of a float numerical for loop, returning +** true iff the loop must continue. (The integer case is +** written online with opcode OP_FORLOOP, for performance.) */ -void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, - const TValue *slot) { +static int floatforloop (StkId ra) { + lua_Number step = fltvalue(s2v(ra + 1)); + lua_Number limit = fltvalue(s2v(ra)); + lua_Number idx = fltvalue(s2v(ra + 2)); /* control variable */ + idx = luai_numadd(L, idx, step); /* increment index */ + if (luai_numlt(0, step) ? luai_numle(idx, limit) + : luai_numle(limit, idx)) { + chgfltvalue(s2v(ra + 2), idx); /* update control variable */ + return 1; /* jump back */ + } + else + return 0; /* finish the loop */ +} + + +/* +** Finish the table access 'val = t[key]' and return the tag of the result. +*/ +lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key, + StkId val, lu_byte tag) { int loop; /* counter to avoid infinite loops */ const TValue *tm; /* metamethod */ for (loop = 0; loop < MAXTAGLOOP; loop++) { - if (slot == NULL) { /* 't' is not a table? */ + if (tag == LUA_VNOTABLE) { /* 't' is not a table? */ lua_assert(!ttistable(t)); tm = luaT_gettmbyobj(L, t, TM_INDEX); - if (unlikely(notm(tm))) + if (l_unlikely(notm(tm))) luaG_typeerror(L, t, "index"); /* no metamethod */ /* else will try the metamethod */ } else { /* 't' is a table */ - lua_assert(isempty(slot)); tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ if (tm == NULL) { /* no metamethod? */ setnilvalue(s2v(val)); /* result is nil */ - return; + return LUA_VNIL; } /* else will try the metamethod */ } if (ttisfunction(tm)) { /* is metamethod a function? */ - luaT_callTMres(L, tm, t, key, val); /* call it */ - return; + tag = luaT_callTMres(L, tm, t, key, val); /* call it */ + return tag; /* return tag of the result */ } t = tm; /* else try to access 'tm[key]' */ - if (luaV_fastget(L, t, key, slot, luaH_get)) { /* fast track? */ - setobj2s(L, val, slot); /* done */ - return; - } + luaV_fastget(t, key, s2v(val), luaH_get, tag); + if (!tagisempty(tag)) + return tag; /* done */ /* else repeat (tail call 'luaV_finishget') */ } luaG_runerror(L, "'__index' chain too long; possible loop"); + return 0; /* to avoid warnings */ } /* ** Finish a table assignment 't[key] = val'. -** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points -** to the entry 't[key]', or to a value with an absent key if there -** is no such entry. (The value at 'slot' must be empty, otherwise -** 'luaV_fastget' would have done the job.) +** About anchoring the table before the call to 'luaH_finishset': +** This call may trigger an emergency collection. When loop>0, +** the table being accessed is a field in some metatable. If this +** metatable is weak and the table is not anchored, this collection +** could collect that table while it is being updated. */ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - TValue *val, const TValue *slot) { + TValue *val, int hres) { int loop; /* counter to avoid infinite loops */ for (loop = 0; loop < MAXTAGLOOP; loop++) { const TValue *tm; /* '__newindex' metamethod */ - if (slot != NULL) { /* is 't' a table? */ + if (hres != HNOTATABLE) { /* is 't' a table? */ Table *h = hvalue(t); /* save 't' table */ - lua_assert(isempty(slot)); /* slot must be empty */ tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ if (tm == NULL) { /* no metamethod? */ - if (isabstkey(slot)) /* no previous entry? */ - slot = luaH_newkey(L, h, key); /* create one */ - /* no metamethod and (now) there is an entry with given key */ - setobj2t(L, cast(TValue *, slot), val); /* set its new value */ + sethvalue2s(L, L->top.p, h); /* anchor 't' */ + L->top.p++; /* assume EXTRA_STACK */ + luaH_finishset(L, h, key, val, hres); /* set new value */ + L->top.p--; invalidateTMcache(h); luaC_barrierback(L, obj2gco(h), val); return; @@ -253,7 +352,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, } else { /* not a table; check metamethod */ tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); - if (unlikely(notm(tm))) + if (l_unlikely(notm(tm))) luaG_typeerror(L, t, "index"); } /* try the metamethod */ @@ -262,8 +361,9 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, return; } t = tm; /* else repeat assignment over 'tm' */ - if (luaV_fastget(L, t, key, slot, luaH_get)) { - luaV_finishfastset(L, t, slot, val); + luaV_fastset(t, key, val, hres, luaH_pset); + if (hres == HOK) { + luaV_finishfastset(L, t, val); return; /* done */ } /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */ @@ -273,30 +373,40 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, /* -** Compare two strings 'ls' x 'rs', returning an integer less-equal- -** -greater than zero if 'ls' is less-equal-greater than 'rs'. +** Function to be used for 0-terminated string order comparison +*/ +#if !defined(l_strcoll) +#define l_strcoll strcoll +#endif + + +/* +** Compare two strings 'ts1' x 'ts2', returning an integer less-equal- +** -greater than zero if 'ts1' is less-equal-greater than 'ts2'. ** The code is a little tricky because it allows '\0' in the strings -** and it uses 'strcoll' (to respect locales) for each segments -** of the strings. +** and it uses 'strcoll' (to respect locales) for each segment +** of the strings. Note that segments can compare equal but still +** have different lengths. */ -static int l_strcmp (const TString *ls, const TString *rs) { - const char *l = getstr(ls); - size_t ll = tsslen(ls); - const char *r = getstr(rs); - size_t lr = tsslen(rs); +static int l_strcmp (const TString *ts1, const TString *ts2) { + size_t rl1; /* real length */ + const char *s1 = getlstr(ts1, rl1); + size_t rl2; + const char *s2 = getlstr(ts2, rl2); for (;;) { /* for each segment */ - int temp = strcoll(l, r); + int temp = l_strcoll(s1, s2); if (temp != 0) /* not equal? */ return temp; /* done */ else { /* strings are equal up to a '\0' */ - size_t len = strlen(l); /* index of first '\0' in both strings */ - if (len == lr) /* 'rs' is finished? */ - return (len == ll) ? 0 : 1; /* check 'ls' */ - else if (len == ll) /* 'ls' is finished? */ - return -1; /* 'ls' is less than 'rs' ('rs' is not finished) */ - /* both strings longer than 'len'; go on comparing after the '\0' */ - len++; - l += len; ll -= len; r += len; lr -= len; + size_t zl1 = strlen(s1); /* index of first '\0' in 's1' */ + size_t zl2 = strlen(s2); /* index of first '\0' in 's2' */ + if (zl2 == rl2) /* 's2' is finished? */ + return (zl1 == rl1) ? 0 : 1; /* check 's1' */ + else if (zl1 == rl1) /* 's1' is finished? */ + return -1; /* 's1' is less than 's2' ('s2' is not finished) */ + /* both strings longer than 'zl'; go on comparing after the '\0' */ + zl1++; zl2++; + s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2; } } } @@ -313,12 +423,12 @@ static int l_strcmp (const TString *ls, const TString *rs) { ** from float to int.) ** When 'f' is NaN, comparisons must result in false. */ -static int LTintfloat (lua_Integer i, lua_Number f) { +l_sinline int LTintfloat (lua_Integer i, lua_Number f) { if (l_intfitsf(i)) return luai_numlt(cast_num(i), f); /* compare them as floats */ else { /* i < f <=> i < ceil(f) */ lua_Integer fi; - if (luaV_flttointeger(f, &fi, 2)) /* fi = ceil(f) */ + if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ return i < fi; /* compare them as integers */ else /* 'f' is either greater or less than all integers */ return f > 0; /* greater? */ @@ -330,12 +440,12 @@ static int LTintfloat (lua_Integer i, lua_Number f) { ** Check whether integer 'i' is less than or equal to float 'f'. ** See comments on previous function. */ -static int LEintfloat (lua_Integer i, lua_Number f) { +l_sinline int LEintfloat (lua_Integer i, lua_Number f) { if (l_intfitsf(i)) return luai_numle(cast_num(i), f); /* compare them as floats */ else { /* i <= f <=> i <= floor(f) */ lua_Integer fi; - if (luaV_flttointeger(f, &fi, 1)) /* fi = floor(f) */ + if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ return i <= fi; /* compare them as integers */ else /* 'f' is either greater or less than all integers */ return f > 0; /* greater? */ @@ -347,12 +457,12 @@ static int LEintfloat (lua_Integer i, lua_Number f) { ** Check whether float 'f' is less than integer 'i'. ** See comments on previous function. */ -static int LTfloatint (lua_Number f, lua_Integer i) { +l_sinline int LTfloatint (lua_Number f, lua_Integer i) { if (l_intfitsf(i)) return luai_numlt(f, cast_num(i)); /* compare them as floats */ else { /* f < i <=> floor(f) < i */ lua_Integer fi; - if (luaV_flttointeger(f, &fi, 1)) /* fi = floor(f) */ + if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ return fi < i; /* compare them as integers */ else /* 'f' is either greater or less than all integers */ return f < 0; /* less? */ @@ -364,12 +474,12 @@ static int LTfloatint (lua_Number f, lua_Integer i) { ** Check whether float 'f' is less than or equal to integer 'i'. ** See comments on previous function. */ -static int LEfloatint (lua_Number f, lua_Integer i) { +l_sinline int LEfloatint (lua_Number f, lua_Integer i) { if (l_intfitsf(i)) return luai_numle(f, cast_num(i)); /* compare them as floats */ else { /* f <= i <=> ceil(f) <= i */ lua_Integer fi; - if (luaV_flttointeger(f, &fi, 2)) /* fi = ceil(f) */ + if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ return fi <= i; /* compare them as integers */ else /* 'f' is either greater or less than all integers */ return f < 0; /* less? */ @@ -380,7 +490,7 @@ static int LEfloatint (lua_Number f, lua_Integer i) { /* ** Return 'l < r', for numbers. */ -static int LTnum (const TValue *l, const TValue *r) { +l_sinline int LTnum (const TValue *l, const TValue *r) { lua_assert(ttisnumber(l) && ttisnumber(r)); if (ttisinteger(l)) { lua_Integer li = ivalue(l); @@ -402,7 +512,7 @@ static int LTnum (const TValue *l, const TValue *r) { /* ** Return 'l <= r', for numbers. */ -static int LEnum (const TValue *l, const TValue *r) { +l_sinline int LEnum (const TValue *l, const TValue *r) { lua_assert(ttisnumber(l) && ttisnumber(r)); if (ttisinteger(l)) { lua_Integer li = ivalue(l); @@ -445,11 +555,6 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { /* ** return 'l <= r' for non-numbers. -** If it needs a metamethod and there is no '__le', try '__lt', based -** on l <= r iff !(r < l) (assuming a total order). If the metamethod -** yields during this substitution, the continuation has to know about -** it (to negate the result of rmetatable, TM_EQ); - if (tm == NULL) - tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); - break; /* will try TM */ + else { /* equal variants */ + switch (ttypetag(t1)) { + case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: + return 1; + case LUA_VNUMINT: + return (ivalue(t1) == ivalue(t2)); + case LUA_VNUMFLT: + return (fltvalue(t1) == fltvalue(t2)); + case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); + case LUA_VSHRSTR: + return eqshrstr(tsvalue(t1), tsvalue(t2)); + case LUA_VLNGSTR: + return luaS_eqstr(tsvalue(t1), tsvalue(t2)); + case LUA_VUSERDATA: { + if (uvalue(t1) == uvalue(t2)) return 1; + else if (L == NULL) return 0; + tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); + if (tm == NULL) + tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); + break; /* will try TM */ + } + case LUA_VTABLE: { + if (hvalue(t1) == hvalue(t2)) return 1; + else if (L == NULL) return 0; + tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); + if (tm == NULL) + tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); + break; /* will try TM */ + } + case LUA_VLCF: + return (fvalue(t1) == fvalue(t2)); + default: /* functions and threads */ + return (gcvalue(t1) == gcvalue(t2)); } - case LUA_TTABLE: { - if (hvalue(t1) == hvalue(t2)) return 1; - else if (L == NULL) return 0; - tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); - if (tm == NULL) - tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); - break; /* will try TM */ + if (tm == NULL) /* no TM? */ + return 0; /* objects are different */ + else { + int tag = luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */ + return !tagisfalse(tag); } - default: - return gcvalue(t1) == gcvalue(t2); } - if (tm == NULL) /* no TM? */ - return 0; /* objects are different */ - luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */ - return !l_isfalse(s2v(L->top)); } @@ -524,14 +657,21 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { #define tostring(L,o) \ (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) +/* +** Check whether object is a short empty string to optimize concatenation. +** (External strings can be empty too; they will be concatenated like +** non-empty ones.) +*/ #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) /* copy strings in stack from top - n up to top - 1 to buffer */ static void copy2buff (StkId top, int n, char *buff) { size_t tl = 0; /* size already copied */ do { - size_t l = vslen(s2v(top - n)); /* length of string being copied */ - memcpy(buff + tl, svalue(s2v(top - n)), l * sizeof(char)); + TString *st = tsvalue(s2v(top - n)); + size_t l; /* length of string being copied */ + const char *s = getlstr(st, l); + memcpy(buff + tl, s, l * sizeof(char)); tl += l; } while (--n > 0); } @@ -539,30 +679,33 @@ static void copy2buff (StkId top, int n, char *buff) { /* ** Main operation for concatenation: concat 'total' values in the stack, -** from 'L->top - total' up to 'L->top - 1'. +** from 'L->top.p - total' up to 'L->top.p - 1'. */ void luaV_concat (lua_State *L, int total) { - lua_assert(total >= 2); + if (total == 1) + return; /* "all" values already concatenated */ do { - StkId top = L->top; + StkId top = L->top.p; int n = 2; /* number of elements handled in this pass (at least 2) */ if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || !tostring(L, s2v(top - 1))) - luaT_trybinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT); + luaT_tryconcatTM(L); /* may invalidate 'top' */ else if (isemptystr(s2v(top - 1))) /* second operand is empty? */ cast_void(tostring(L, s2v(top - 2))); /* result is first operand */ else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */ setobjs2s(L, top - 2, top - 1); /* result is second op. */ } else { - /* at least two non-empty string values; get as many as possible */ - size_t tl = vslen(s2v(top - 1)); + /* at least two string values; get as many as possible */ + size_t tl = tsslen(tsvalue(s2v(top - 1))); /* total length */ TString *ts; /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { - size_t l = vslen(s2v(top - n - 1)); - if (unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) + size_t l = tsslen(tsvalue(s2v(top - n - 1))); + if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - tl)) { + L->top.p = top - total; /* pop strings to avoid wasting stack */ luaG_runerror(L, "string length overflow"); + } tl += l; } if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ @@ -572,12 +715,12 @@ void luaV_concat (lua_State *L, int total) { } else { /* long string; copy strings directly to final result */ ts = luaS_createlngstrobj(L, tl); - copy2buff(top, n, getstr(ts)); + copy2buff(top, n, getlngstr(ts)); } setsvalue2s(L, top - n, ts); /* create result */ } - total -= n-1; /* got 'n' strings to create 1 new */ - L->top -= n-1; /* popped 'n' strings and pushed one */ + total -= n - 1; /* got 'n' strings to create one new */ + L->top.p -= n - 1; /* popped 'n' strings and pushed one */ } while (total > 1); /* repeat until only 1 result left */ } @@ -588,24 +731,24 @@ void luaV_concat (lua_State *L, int total) { void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { const TValue *tm; switch (ttypetag(rb)) { - case LUA_TTABLE: { + case LUA_VTABLE: { Table *h = hvalue(rb); tm = fasttm(L, h->metatable, TM_LEN); if (tm) break; /* metamethod? break switch to call it */ - setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */ + setivalue(s2v(ra), l_castU2S(luaH_getn(L, h))); /* else primitive len */ return; } - case LUA_TSHRSTR: { + case LUA_VSHRSTR: { setivalue(s2v(ra), tsvalue(rb)->shrlen); return; } - case LUA_TLNGSTR: { - setivalue(s2v(ra), tsvalue(rb)->u.lnglen); + case LUA_VLNGSTR: { + setivalue(s2v(ra), cast_st2S(tsvalue(rb)->u.lnglen)); return; } default: { /* try metamethod */ tm = luaT_gettmbyobj(L, rb, TM_LEN); - if (unlikely(notm(tm))) /* no metamethod? */ + if (l_unlikely(notm(tm))) /* no metamethod? */ luaG_typeerror(L, rb, "get length of"); break; } @@ -621,7 +764,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { ** otherwise 'floor(q) == trunc(q) - 1'. */ lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { - if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ + if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ if (n == 0) luaG_runerror(L, "attempt to divide by zero"); return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ @@ -641,7 +784,7 @@ lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { ** about luaV_idiv.) */ lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { - if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ + if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ if (n == 0) luaG_runerror(L, "attempt to perform 'n%%0'"); return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ @@ -666,7 +809,8 @@ lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { /* number of bits in an integer */ -#define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) +#define NBITS l_numbits(lua_Integer) + /* ** Shift left operation. (Shift right just negates 'y'.) @@ -710,51 +854,51 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, */ void luaV_finishOp (lua_State *L) { CallInfo *ci = L->ci; - StkId base = ci->func + 1; + StkId base = ci->func.p + 1; Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ - case OP_ADDI: case OP_SUBI: - case OP_MULI: case OP_DIVI: case OP_IDIVI: - case OP_MODI: case OP_POWI: - case OP_ADD: case OP_SUB: - case OP_MUL: case OP_DIV: case OP_IDIV: - case OP_BANDK: case OP_BORK: case OP_BXORK: - case OP_BAND: case OP_BOR: case OP_BXOR: - case OP_SHRI: case OP_SHL: case OP_SHR: - case OP_MOD: case OP_POW: + case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { + setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top.p); + break; + } case OP_UNM: case OP_BNOT: case OP_LEN: case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: case OP_GETFIELD: case OP_SELF: { - setobjs2s(L, base + GETARG_A(inst), --L->top); + setobjs2s(L, base + GETARG_A(inst), --L->top.p); break; } case OP_LT: case OP_LE: case OP_LTI: case OP_LEI: case OP_GTI: case OP_GEI: case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ - int res = !l_isfalse(s2v(L->top - 1)); - L->top--; -#if defined(LUA_COMPAT_LT_LE) - if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ - ci->callstatus ^= CIST_LEQ; /* clear mark */ - res = !res; /* negate result */ - } -#endif + int res = !l_isfalse(s2v(L->top.p - 1)); + L->top.p--; lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); if (res != GETARG_k(inst)) /* condition failed? */ ci->u.l.savedpc++; /* skip jump instruction */ break; } case OP_CONCAT: { - StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */ + StkId top = L->top.p - 1; /* top when 'luaT_tryconcatTM' was called */ int a = GETARG_A(inst); /* first element to concatenate */ int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ setobjs2s(L, top - 2, top); /* put TM result in proper position */ - if (total > 1) { /* are there elements to concat? */ - L->top = top - 1; /* top is one after last element (at top-2) */ - luaV_concat(L, total); /* concat them (may yield again) */ - } + L->top.p = top - 1; /* top is one after last element (at top-2) */ + luaV_concat(L, total); /* concat them (may yield again) */ + break; + } + case OP_CLOSE: { /* yielded closing variables */ + ci->u.l.savedpc--; /* repeat instruction to close other vars. */ + break; + } + case OP_RETURN: { /* yielded closing variables */ + StkId ra = base + GETARG_A(inst); + /* adjust top to signal correct number of returns, in case the + return is "up to top" ('isIT') */ + L->top.p = ra + ci->u2.nres; + /* repeat instruction to close other vars. and complete the return */ + ci->u.l.savedpc--; break; } default: { @@ -772,149 +916,174 @@ void luaV_finishOp (lua_State *L) { /* ** {================================================================== -** Macros for arithmetic/bitwise opcodes in 'luaV_execute' +** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute' +** +** All these macros are to be used exclusively inside the main +** iterpreter loop (function luaV_execute) and may access directly +** the local variables of that function (L, i, pc, ci, etc.). ** =================================================================== */ - #define l_addi(L,a,b) intop(+, a, b) #define l_subi(L,a,b) intop(-, a, b) #define l_muli(L,a,b) intop(*, a, b) -#define l_band(L,a,b) intop(&, a, b) -#define l_bor(L,a,b) intop(|, a, b) -#define l_bxor(L,a,b) intop(^, a, b) - - -/* -** Auxiliary macro for arithmetic operations over floats and others -** with immediate operand. 'fop' is the float operation; 'tm' is the -** corresponding metamethod; 'flip' is true if operands were flipped. -*/ -#define op_arithfI_aux(L,v1,imm,fop,tm,flip) { \ - lua_Number nb; \ - if (tonumberns(v1, nb)) { \ - setfltvalue(s2v(ra), fop(L, nb, cast_num(imm))); \ - } \ - else \ - Protect(luaT_trybiniTM(L, v1, imm, flip, ra, tm)); } +#define l_band(a,b) intop(&, a, b) +#define l_bor(a,b) intop(|, a, b) +#define l_bxor(a,b) intop(^, a, b) +#define l_lti(a,b) (a < b) +#define l_lei(a,b) (a <= b) +#define l_gti(a,b) (a > b) +#define l_gei(a,b) (a >= b) -/* -** Arithmetic operations over floats and others with immediate operand. -*/ -#define op_arithfI(L,fop,tm) { \ - TValue *v1 = vRB(i); \ - int imm = GETARG_sC(i); \ - op_arithfI_aux(L, v1, imm, fop, tm, 0); } /* ** Arithmetic operations with immediate operands. 'iop' is the integer -** operation. +** operation, 'fop' is the float operation. */ -#define op_arithI(L,iop,fop,tm,flip) { \ +#define op_arithI(L,iop,fop) { \ + TValue *ra = vRA(i); \ TValue *v1 = vRB(i); \ int imm = GETARG_sC(i); \ if (ttisinteger(v1)) { \ - setivalue(s2v(ra), iop(L, ivalue(v1), imm)); \ + lua_Integer iv1 = ivalue(v1); \ + pc++; setivalue(ra, iop(L, iv1, imm)); \ } \ - else op_arithfI_aux(L, v1, imm, fop, tm, flip); } + else if (ttisfloat(v1)) { \ + lua_Number nb = fltvalue(v1); \ + lua_Number fimm = cast_num(imm); \ + pc++; setfltvalue(ra, fop(L, nb, fimm)); \ + }} /* ** Auxiliary function for arithmetic operations over floats and others -** with two register operands. +** with two operands. */ -#define op_arithf_aux(L,v1,v2,fop,tm) { \ +#define op_arithf_aux(L,v1,v2,fop) { \ lua_Number n1; lua_Number n2; \ if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ - setfltvalue(s2v(ra), fop(L, n1, n2)); \ - } \ - else \ - Protect(luaT_trybinTM(L, v1, v2, ra, tm)); } + StkId ra = RA(i); \ + pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ + }} /* ** Arithmetic operations over floats and others with register operands. */ -#define op_arithf(L,fop,tm) { \ +#define op_arithf(L,fop) { \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ - op_arithf_aux(L, v1, v2, fop, tm); } + op_arithf_aux(L, v1, v2, fop); } /* -** Arithmetic operations with register operands. +** Arithmetic operations with K operands for floats. */ -#define op_arith(L,iop,fop,tm) { \ +#define op_arithfK(L,fop) { \ TValue *v1 = vRB(i); \ - TValue *v2 = vRC(i); \ + TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ + op_arithf_aux(L, v1, v2, fop); } + + +/* +** Arithmetic operations over integers and floats. +*/ +#define op_arith_aux(L,v1,v2,iop,fop) { \ if (ttisinteger(v1) && ttisinteger(v2)) { \ + StkId ra = RA(i); \ lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ - setivalue(s2v(ra), iop(L, i1, i2)); \ + pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ } \ - else op_arithf_aux(L, v1, v2, fop, tm); } + else op_arithf_aux(L, v1, v2, fop); } /* -** Arithmetic operations with K operands. +** Arithmetic operations with register operands. */ -#define op_arithK(L,iop,fop,tm,flip) { \ +#define op_arith(L,iop,fop) { \ TValue *v1 = vRB(i); \ - TValue *v2 = KC(i); \ - if (ttisinteger(v1) && ttisinteger(v2)) { \ - lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ - setivalue(s2v(ra), iop(L, i1, i2)); \ - } \ - else { \ - lua_Number n1; lua_Number n2; \ - if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ - setfltvalue(s2v(ra), fop(L, n1, n2)); \ - } \ - else \ - Protect(luaT_trybinassocTM(L, v1, v2, ra, flip, tm)); } } + TValue *v2 = vRC(i); \ + op_arith_aux(L, v1, v2, iop, fop); } /* -** Arithmetic operations with K operands for floats. +** Arithmetic operations with K operands. */ -#define op_arithfK(L,fop,tm) { \ +#define op_arithK(L,iop,fop) { \ TValue *v1 = vRB(i); \ - TValue *v2 = KC(i); \ - lua_Number n1; lua_Number n2; \ - if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ - setfltvalue(s2v(ra), fop(L, n1, n2)); \ - } \ - else \ - Protect(luaT_trybinTM(L, v1, v2, ra, tm)); } + TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ + op_arith_aux(L, v1, v2, iop, fop); } /* ** Bitwise operations with constant operand. */ -#define op_bitwiseK(L,op,tm) { \ +#define op_bitwiseK(L,op) { \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); \ lua_Integer i1; \ lua_Integer i2 = ivalue(v2); \ if (tointegerns(v1, &i1)) { \ - setivalue(s2v(ra), op(L, i1, i2)); \ - } \ - else \ - Protect(luaT_trybiniTM(L, v1, i2, TESTARG_k(i), ra, tm)); } + StkId ra = RA(i); \ + pc++; setivalue(s2v(ra), op(i1, i2)); \ + }} /* ** Bitwise operations with register operands. */ -#define op_bitwise(L,op,tm) { \ +#define op_bitwise(L,op) { \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ lua_Integer i1; lua_Integer i2; \ if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \ - setivalue(s2v(ra), op(L, i1, i2)); \ + StkId ra = RA(i); \ + pc++; setivalue(s2v(ra), op(i1, i2)); \ + }} + + +/* +** Order operations with register operands. 'opn' actually works +** for all numbers, but the fast track improves performance for +** integers. +*/ +#define op_order(L,opi,opn,other) { \ + TValue *ra = vRA(i); \ + int cond; \ + TValue *rb = vRB(i); \ + if (ttisinteger(ra) && ttisinteger(rb)) { \ + lua_Integer ia = ivalue(ra); \ + lua_Integer ib = ivalue(rb); \ + cond = opi(ia, ib); \ } \ + else if (ttisnumber(ra) && ttisnumber(rb)) \ + cond = opn(ra, rb); \ else \ - Protect(luaT_trybinTM(L, v1, v2, ra, tm)); } + Protect(cond = other(L, ra, rb)); \ + docondjump(); } + + +/* +** Order operations with immediate operand. (Immediate operand is +** always small enough to have an exact representation as a float.) +*/ +#define op_orderI(L,opi,opf,inv,tm) { \ + TValue *ra = vRA(i); \ + int cond; \ + int im = GETARG_sB(i); \ + if (ttisinteger(ra)) \ + cond = opi(ivalue(ra), im); \ + else if (ttisfloat(ra)) { \ + lua_Number fa = fltvalue(ra); \ + lua_Number fim = cast_num(im); \ + cond = opf(fa, fim); \ + } \ + else { \ + int isf = GETARG_C(i); \ + Protect(cond = luaT_callorderiTM(L, ra, im, inv, isf, tm)); \ + } \ + docondjump(); } /* }================================================================== */ @@ -931,6 +1100,7 @@ void luaV_finishOp (lua_State *L) { #define RA(i) (base+GETARG_A(i)) +#define vRA(i) s2v(RA(i)) #define RB(i) (base+GETARG_B(i)) #define vRB(i) s2v(RB(i)) #define KB(i) (k+GETARG_B(i)) @@ -943,10 +1113,11 @@ void luaV_finishOp (lua_State *L) { #define updatetrap(ci) (trap = ci->u.l.trap) -#define updatebase(ci) (base = ci->func + 1) +#define updatebase(ci) (base = ci->func.p + 1) -#define updatestack(ci) { if (trap) { updatebase(ci); ra = RA(i); } } +#define updatestack(ci) \ + { if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } } /* @@ -957,7 +1128,7 @@ void luaV_finishOp (lua_State *L) { /* for test instructions, execute the jump instruction that follows it */ -#define donextjump(ci) { i = *pc; dojump(ci, i, 1); } +#define donextjump(ci) { Instruction ni = *pc; dojump(ci, ni, 1); } /* ** do a conditional jump: skip next instruction if 'cond' is not what @@ -970,14 +1141,14 @@ void luaV_finishOp (lua_State *L) { /* ** Correct global 'pc'. */ -#define savepc(L) (ci->u.l.savedpc = pc) +#define savepc(ci) (ci->u.l.savedpc = pc) /* ** Whenever code can raise errors, the global 'pc' and the global ** 'top' must be correct to report occasional errors. */ -#define savestate(L,ci) (savepc(L), L->top = ci->top) +#define savestate(L,ci) (savepc(ci), L->top.p = ci->top.p) /* @@ -987,30 +1158,36 @@ void luaV_finishOp (lua_State *L) { #define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci)) /* special version that does not change the top */ -#define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) +#define ProtectNT(exp) (savepc(ci), (exp), updatetrap(ci)) /* -** Protect code that will finish the loop (returns) or can only raise -** errors. (That is, it will not return to the interpreter main loop -** after changing the stack or hooks.) +** Protect code that can only raise errors. (That is, it cannot change +** the stack or hooks.) */ -#define halfProtect(exp) (savepc(L), (exp)) +#define halfProtect(exp) (savestate(L,ci), (exp)) +/* +** macro executed during Lua functions at points where the +** function can yield. +*/ +#if !defined(luai_threadyield) +#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} +#endif +/* 'c' is the limit of live values in the stack */ #define checkGC(L,c) \ - { luaC_condGC(L, L->top = (c), /* limit of live values */ \ + { luaC_condGC(L, (savepc(ci), L->top.p = (c)), \ updatetrap(ci)); \ luai_threadyield(L); } /* fetch an instruction and prepare its execution */ #define vmfetch() { \ - if (trap) { /* stack reallocation or hooks? */ \ + if (l_unlikely(trap)) { /* stack reallocation or hooks? */ \ trap = luaG_traceexec(L, pc); /* handle hooks */ \ updatebase(ci); /* correct stack */ \ } \ i = *(pc++); \ - ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \ } #define vmdispatch(o) switch(o) @@ -1027,60 +1204,80 @@ void luaV_execute (lua_State *L, CallInfo *ci) { #if LUA_USE_JUMPTABLE #include "ljumptab.h" #endif - tailcall: + startfunc: trap = L->hookmask; - cl = clLvalue(s2v(ci->func)); + returning: /* trap already set */ + cl = ci_func(ci); k = cl->p->k; pc = ci->u.l.savedpc; - if (trap) { - if (cl->p->is_vararg) - trap = 0; /* hooks will start after PREPVARARG instruction */ - else if (pc == cl->p->code) /* first instruction (not resuming)? */ - luaD_hookcall(L, ci); - ci->u.l.trap = 1; /* there may be other hooks */ - } - base = ci->func + 1; + if (l_unlikely(trap)) + trap = luaG_tracecall(L); + base = ci->func.p + 1; /* main loop of interpreter */ for (;;) { - int cond; /* flag for conditional jumps */ Instruction i; /* instruction being executed */ - StkId ra; /* instruction's A register */ vmfetch(); - lua_assert(base == ci->func + 1); - lua_assert(base <= L->top && L->top < L->stack + L->stacksize); - lua_assert(ci->top < L->stack + L->stacksize); + #if 0 + { /* low-level line tracing for debugging Lua */ + #include "lopnames.h" + int pcrel = pcRel(pc, cl->p); + printf("line: %d; %s (%d)\n", luaG_getfuncline(cl->p, pcrel), + opnames[GET_OPCODE(i)], pcrel); + } + #endif + lua_assert(base == ci->func.p + 1); + lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p); + /* for tests, invalidate top for instructions not expecting it */ + lua_assert(luaP_isIT(i) || (cast_void(L->top.p = base), 1)); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { + StkId ra = RA(i); setobjs2s(L, ra, RB(i)); vmbreak; } - vmcase(OP_LOADK) { - TValue *rb = k + GETARG_Bx(i); - setobj2s(L, ra, rb); - vmbreak; - } vmcase(OP_LOADI) { + StkId ra = RA(i); lua_Integer b = GETARG_sBx(i); setivalue(s2v(ra), b); vmbreak; } vmcase(OP_LOADF) { + StkId ra = RA(i); int b = GETARG_sBx(i); setfltvalue(s2v(ra), cast_num(b)); vmbreak; } + vmcase(OP_LOADK) { + StkId ra = RA(i); + TValue *rb = k + GETARG_Bx(i); + setobj2s(L, ra, rb); + vmbreak; + } vmcase(OP_LOADKX) { + StkId ra = RA(i); TValue *rb; rb = k + GETARG_Ax(*pc); pc++; setobj2s(L, ra, rb); vmbreak; } - vmcase(OP_LOADBOOL) { - setbvalue(s2v(ra), GETARG_B(i)); - if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ + vmcase(OP_LOADFALSE) { + StkId ra = RA(i); + setbfvalue(s2v(ra)); + vmbreak; + } + vmcase(OP_LFALSESKIP) { + StkId ra = RA(i); + setbfvalue(s2v(ra)); + pc++; /* skip next instruction */ + vmbreak; + } + vmcase(OP_LOADTRUE) { + StkId ra = RA(i); + setbtvalue(s2v(ra)); vmbreak; } vmcase(OP_LOADNIL) { + StkId ra = RA(i); int b = GETARG_B(i); do { setnilvalue(s2v(ra++)); @@ -1088,304 +1285,306 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_GETUPVAL) { + StkId ra = RA(i); int b = GETARG_B(i); - setobj2s(L, ra, cl->upvals[b]->v); + setobj2s(L, ra, cl->upvals[b]->v.p); vmbreak; } vmcase(OP_SETUPVAL) { + StkId ra = RA(i); UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v, s2v(ra)); + setobj(L, uv->v.p, s2v(ra)); luaC_barrier(L, uv, s2v(ra)); vmbreak; } vmcase(OP_GETTABUP) { - const TValue *slot; - TValue *upval = cl->upvals[GETARG_B(i)]->v; + StkId ra = RA(i); + TValue *upval = cl->upvals[GETARG_B(i)]->v.p; TValue *rc = KC(i); - TString *key = tsvalue(rc); /* key must be a string */ - if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { - setobj2s(L, ra, slot); - } - else - Protect(luaV_finishget(L, upval, rc, ra, slot)); + TString *key = tsvalue(rc); /* key must be a short string */ + lu_byte tag; + luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, tag); + if (tagisempty(tag)) + Protect(luaV_finishget(L, upval, rc, ra, tag)); vmbreak; } vmcase(OP_GETTABLE) { - const TValue *slot; + StkId ra = RA(i); TValue *rb = vRB(i); TValue *rc = vRC(i); - lua_Unsigned n; - if (ttisinteger(rc) /* fast track for integers? */ - ? (n = ivalue(rc), luaV_fastgeti(L, rb, n, slot)) - : luaV_fastget(L, rb, rc, slot, luaH_get)) { - setobj2s(L, ra, slot); + lu_byte tag; + if (ttisinteger(rc)) { /* fast track for integers? */ + luaV_fastgeti(rb, ivalue(rc), s2v(ra), tag); } else - Protect(luaV_finishget(L, rb, rc, ra, slot)); + luaV_fastget(rb, rc, s2v(ra), luaH_get, tag); + if (tagisempty(tag)) + Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; } vmcase(OP_GETI) { - const TValue *slot; + StkId ra = RA(i); TValue *rb = vRB(i); int c = GETARG_C(i); - if (luaV_fastgeti(L, rb, c, slot)) { - setobj2s(L, ra, slot); - } - else { + lu_byte tag; + luaV_fastgeti(rb, c, s2v(ra), tag); + if (tagisempty(tag)) { TValue key; setivalue(&key, c); - Protect(luaV_finishget(L, rb, &key, ra, slot)); + Protect(luaV_finishget(L, rb, &key, ra, tag)); } vmbreak; } vmcase(OP_GETFIELD) { - const TValue *slot; + StkId ra = RA(i); TValue *rb = vRB(i); TValue *rc = KC(i); - TString *key = tsvalue(rc); /* key must be a string */ - if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) { - setobj2s(L, ra, slot); - } - else - Protect(luaV_finishget(L, rb, rc, ra, slot)); + TString *key = tsvalue(rc); /* key must be a short string */ + lu_byte tag; + luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag); + if (tagisempty(tag)) + Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; } vmcase(OP_SETTABUP) { - const TValue *slot; - TValue *upval = cl->upvals[GETARG_A(i)]->v; + int hres; + TValue *upval = cl->upvals[GETARG_A(i)]->v.p; TValue *rb = KB(i); TValue *rc = RKC(i); - TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { - luaV_finishfastset(L, upval, slot, rc); - } + TString *key = tsvalue(rb); /* key must be a short string */ + luaV_fastset(upval, key, rc, hres, luaH_psetshortstr); + if (hres == HOK) + luaV_finishfastset(L, upval, rc); else - Protect(luaV_finishset(L, upval, rb, rc, slot)); + Protect(luaV_finishset(L, upval, rb, rc, hres)); vmbreak; } vmcase(OP_SETTABLE) { - const TValue *slot; + StkId ra = RA(i); + int hres; TValue *rb = vRB(i); /* key (table is in 'ra') */ TValue *rc = RKC(i); /* value */ - lua_Unsigned n; - if (ttisinteger(rb) /* fast track for integers? */ - ? (n = ivalue(rb), luaV_fastgeti(L, s2v(ra), n, slot)) - : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) { - luaV_finishfastset(L, s2v(ra), slot, rc); + if (ttisinteger(rb)) { /* fast track for integers? */ + luaV_fastseti(s2v(ra), ivalue(rb), rc, hres); + } + else { + luaV_fastset(s2v(ra), rb, rc, hres, luaH_pset); } + if (hres == HOK) + luaV_finishfastset(L, s2v(ra), rc); else - Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); + Protect(luaV_finishset(L, s2v(ra), rb, rc, hres)); vmbreak; } vmcase(OP_SETI) { - const TValue *slot; - int c = GETARG_B(i); + StkId ra = RA(i); + int hres; + int b = GETARG_B(i); TValue *rc = RKC(i); - if (luaV_fastgeti(L, s2v(ra), c, slot)) { - luaV_finishfastset(L, s2v(ra), slot, rc); - } + luaV_fastseti(s2v(ra), b, rc, hres); + if (hres == HOK) + luaV_finishfastset(L, s2v(ra), rc); else { TValue key; - setivalue(&key, c); - Protect(luaV_finishset(L, s2v(ra), &key, rc, slot)); + setivalue(&key, b); + Protect(luaV_finishset(L, s2v(ra), &key, rc, hres)); } vmbreak; } vmcase(OP_SETFIELD) { - const TValue *slot; + StkId ra = RA(i); + int hres; TValue *rb = KB(i); TValue *rc = RKC(i); - TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) { - luaV_finishfastset(L, s2v(ra), slot, rc); - } + TString *key = tsvalue(rb); /* key must be a short string */ + luaV_fastset(s2v(ra), key, rc, hres, luaH_psetshortstr); + if (hres == HOK) + luaV_finishfastset(L, s2v(ra), rc); else - Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); + Protect(luaV_finishset(L, s2v(ra), rb, rc, hres)); vmbreak; } vmcase(OP_NEWTABLE) { - int b = GETARG_B(i); - int c = GETARG_C(i); + StkId ra = RA(i); + unsigned b = cast_uint(GETARG_vB(i)); /* log2(hash size) + 1 */ + unsigned c = cast_uint(GETARG_vC(i)); /* array size */ Table *t; - L->top = ci->top; /* correct top in case of GC */ + if (b > 0) + b = 1u << (b - 1); /* hash size is 2^(b - 1) */ + if (TESTARG_k(i)) { /* non-zero extra argument? */ + lua_assert(GETARG_Ax(*pc) != 0); + /* add it to array size */ + c += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1); + } + pc++; /* skip extra argument */ + L->top.p = ra + 1; /* correct top in case of emergency GC */ t = luaH_new(L); /* memory allocation */ sethvalue2s(L, ra, t); if (b != 0 || c != 0) - luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); /* idem */ + luaH_resize(L, t, c, b); /* idem */ checkGC(L, ra + 1); vmbreak; } vmcase(OP_SELF) { - const TValue *slot; + StkId ra = RA(i); + lu_byte tag; TValue *rb = vRB(i); - TValue *rc = RKC(i); - TString *key = tsvalue(rc); /* key must be a string */ + TValue *rc = KC(i); + TString *key = tsvalue(rc); /* key must be a short string */ setobj2s(L, ra + 1, rb); - if (luaV_fastget(L, rb, key, slot, luaH_getstr)) { - setobj2s(L, ra, slot); - } - else - Protect(luaV_finishget(L, rb, rc, ra, slot)); + luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag); + if (tagisempty(tag)) + Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; } vmcase(OP_ADDI) { - op_arithI(L, l_addi, luai_numadd, TM_ADD, GETARG_k(i)); + op_arithI(L, l_addi, luai_numadd); vmbreak; } - vmcase(OP_SUBI) { - op_arithI(L, l_subi, luai_numsub, TM_SUB, 0); - vmbreak; - } - vmcase(OP_MULI) { - op_arithI(L, l_muli, luai_nummul, TM_MUL, GETARG_k(i)); + vmcase(OP_ADDK) { + op_arithK(L, l_addi, luai_numadd); vmbreak; } - vmcase(OP_MODI) { - op_arithI(L, luaV_mod, luaV_modf, TM_MOD, 0); + vmcase(OP_SUBK) { + op_arithK(L, l_subi, luai_numsub); vmbreak; } - vmcase(OP_POWI) { - op_arithfI(L, luai_numpow, TM_POW); + vmcase(OP_MULK) { + op_arithK(L, l_muli, luai_nummul); vmbreak; } - vmcase(OP_DIVI) { - op_arithfI(L, luai_numdiv, TM_DIV); + vmcase(OP_MODK) { + savestate(L, ci); /* in case of division by 0 */ + op_arithK(L, luaV_mod, luaV_modf); vmbreak; } - vmcase(OP_IDIVI) { - op_arithI(L, luaV_idiv, luai_numidiv, TM_IDIV, 0); + vmcase(OP_POWK) { + op_arithfK(L, luai_numpow); vmbreak; } - vmcase(OP_ADDK) { - op_arithK(L, l_addi, luai_numadd, TM_ADD, GETARG_k(i)); + vmcase(OP_DIVK) { + op_arithfK(L, luai_numdiv); vmbreak; } - vmcase(OP_SUBK) { - op_arithK(L, l_subi, luai_numsub, TM_SUB, 0); + vmcase(OP_IDIVK) { + savestate(L, ci); /* in case of division by 0 */ + op_arithK(L, luaV_idiv, luai_numidiv); vmbreak; } - vmcase(OP_MULK) { - op_arithK(L, l_muli, luai_nummul, TM_MUL, GETARG_k(i)); + vmcase(OP_BANDK) { + op_bitwiseK(L, l_band); vmbreak; } - vmcase(OP_MODK) { - op_arithK(L, luaV_mod, luaV_modf, TM_MOD, 0); + vmcase(OP_BORK) { + op_bitwiseK(L, l_bor); vmbreak; } - vmcase(OP_POWK) { - op_arithfK(L, luai_numpow, TM_POW); + vmcase(OP_BXORK) { + op_bitwiseK(L, l_bxor); vmbreak; } - vmcase(OP_DIVK) { - op_arithfK(L, luai_numdiv, TM_DIV); + vmcase(OP_SHLI) { + StkId ra = RA(i); + TValue *rb = vRB(i); + int ic = GETARG_sC(i); + lua_Integer ib; + if (tointegerns(rb, &ib)) { + pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib)); + } vmbreak; } - vmcase(OP_IDIVK) { - op_arithK(L, luaV_idiv, luai_numidiv, TM_IDIV, 0); + vmcase(OP_SHRI) { + StkId ra = RA(i); + TValue *rb = vRB(i); + int ic = GETARG_sC(i); + lua_Integer ib; + if (tointegerns(rb, &ib)) { + pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic)); + } vmbreak; } vmcase(OP_ADD) { - op_arith(L, l_addi, luai_numadd, TM_ADD); + op_arith(L, l_addi, luai_numadd); vmbreak; } vmcase(OP_SUB) { - op_arith(L, l_subi, luai_numsub, TM_SUB); + op_arith(L, l_subi, luai_numsub); vmbreak; } vmcase(OP_MUL) { - op_arith(L, l_muli, luai_nummul, TM_MUL); + op_arith(L, l_muli, luai_nummul); vmbreak; } vmcase(OP_MOD) { - op_arith(L, luaV_mod, luaV_modf, TM_MOD); + savestate(L, ci); /* in case of division by 0 */ + op_arith(L, luaV_mod, luaV_modf); vmbreak; } vmcase(OP_POW) { - op_arithf(L, luai_numpow, TM_POW); + op_arithf(L, luai_numpow); vmbreak; } vmcase(OP_DIV) { /* float division (always with floats) */ - op_arithf(L, luai_numdiv, TM_DIV); + op_arithf(L, luai_numdiv); vmbreak; } vmcase(OP_IDIV) { /* floor division */ - op_arith(L, luaV_idiv, luai_numidiv, TM_IDIV); - vmbreak; - } - vmcase(OP_BANDK) { - op_bitwiseK(L, l_band, TM_BAND); - vmbreak; - } - vmcase(OP_BORK) { - op_bitwiseK(L, l_bor, TM_BOR); - vmbreak; - } - vmcase(OP_BXORK) { - op_bitwiseK(L, l_bxor, TM_BXOR); + savestate(L, ci); /* in case of division by 0 */ + op_arith(L, luaV_idiv, luai_numidiv); vmbreak; } vmcase(OP_BAND) { - op_bitwise(L, l_band, TM_BAND); + op_bitwise(L, l_band); vmbreak; } vmcase(OP_BOR) { - op_bitwise(L, l_bor, TM_BOR); + op_bitwise(L, l_bor); vmbreak; } vmcase(OP_BXOR) { - op_bitwise(L, l_bxor, TM_BXOR); + op_bitwise(L, l_bxor); vmbreak; } - vmcase(OP_SHRI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Integer ib; - if (tointegerns(rb, &ib)) { - setivalue(s2v(ra), luaV_shiftl(ib, -ic)); - } - else { - TMS ev = TM_SHR; - if (TESTARG_k(i)) { - ic = -ic; ev = TM_SHL; - } - Protect(luaT_trybiniTM(L, rb, ic, 0, ra, ev)); - } + vmcase(OP_SHL) { + op_bitwise(L, luaV_shiftl); vmbreak; } - vmcase(OP_SHLI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Integer ib; - if (tointegerns(rb, &ib)) { - setivalue(s2v(ra), luaV_shiftl(ic, ib)); - } - else - Protect(luaT_trybiniTM(L, rb, ic, 1, ra, TM_SHL)); + vmcase(OP_SHR) { + op_bitwise(L, luaV_shiftr); vmbreak; } - vmcase(OP_SHR) { + vmcase(OP_MMBIN) { + StkId ra = RA(i); + Instruction pi = *(pc - 2); /* original arith. expression */ TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Integer ib; lua_Integer ic; - if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), luaV_shiftl(ib, -ic)); - } - else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); + TMS tm = (TMS)GETARG_C(i); + StkId result = RA(pi); + lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR); + Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm)); vmbreak; } - vmcase(OP_SHL) { - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Integer ib; lua_Integer ic; - if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), luaV_shiftl(ib, ic)); - } - else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); + vmcase(OP_MMBINI) { + StkId ra = RA(i); + Instruction pi = *(pc - 2); /* original arith. expression */ + int imm = GETARG_sB(i); + TMS tm = (TMS)GETARG_C(i); + int flip = GETARG_k(i); + StkId result = RA(pi); + Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm)); + vmbreak; + } + vmcase(OP_MMBINK) { + StkId ra = RA(i); + Instruction pi = *(pc - 2); /* original arith. expression */ + TValue *imm = KB(i); + TMS tm = (TMS)GETARG_C(i); + int flip = GETARG_k(i); + StkId result = RA(pi); + Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm)); vmbreak; } vmcase(OP_UNM) { + StkId ra = RA(i); TValue *rb = vRB(i); lua_Number nb; if (ttisinteger(rb)) { @@ -1400,6 +1599,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_BNOT) { + StkId ra = RA(i); TValue *rb = vRB(i); lua_Integer ib; if (tointegerns(rb, &ib)) { @@ -1410,28 +1610,35 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_NOT) { + StkId ra = RA(i); TValue *rb = vRB(i); - int nrb = l_isfalse(rb); /* next assignment may change this value */ - setbvalue(s2v(ra), nrb); + if (l_isfalse(rb)) + setbtvalue(s2v(ra)); + else + setbfvalue(s2v(ra)); vmbreak; } vmcase(OP_LEN) { + StkId ra = RA(i); Protect(luaV_objlen(L, ra, vRB(i))); vmbreak; } vmcase(OP_CONCAT) { + StkId ra = RA(i); int n = GETARG_B(i); /* number of elements to concatenate */ - L->top = ra + n; /* mark the end of concat operands */ + L->top.p = ra + n; /* mark the end of concat operands */ ProtectNT(luaV_concat(L, n)); - checkGC(L, L->top); /* 'luaV_concat' ensures correct top */ + checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */ vmbreak; } vmcase(OP_CLOSE) { - L->top = ra + 1; /* everything is free after this slot */ - Protect(luaF_close(L, ra, LUA_OK)); + StkId ra = RA(i); + lua_assert(!GETARG_B(i)); /* 'close must be alive */ + Protect(luaF_close(L, ra, LUA_OK, 1)); vmbreak; } vmcase(OP_TBC) { + StkId ra = RA(i); /* create new to-be-closed upvalue */ halfProtect(luaF_newtbcupval(L, ra)); vmbreak; @@ -1441,41 +1648,32 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_EQ) { + StkId ra = RA(i); + int cond; TValue *rb = vRB(i); Protect(cond = luaV_equalobj(L, s2v(ra), rb)); docondjump(); vmbreak; } vmcase(OP_LT) { - TValue *rb = vRB(i); - if (ttisinteger(s2v(ra)) && ttisinteger(rb)) - cond = (ivalue(s2v(ra)) < ivalue(rb)); - else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) - cond = LTnum(s2v(ra), rb); - else - Protect(cond = lessthanothers(L, s2v(ra), rb)); - docondjump(); + op_order(L, l_lti, LTnum, lessthanothers); vmbreak; } vmcase(OP_LE) { - TValue *rb = vRB(i); - if (ttisinteger(s2v(ra)) && ttisinteger(rb)) - cond = (ivalue(s2v(ra)) <= ivalue(rb)); - else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) - cond = LEnum(s2v(ra), rb); - else - Protect(cond = lessequalothers(L, s2v(ra), rb)); - docondjump(); + op_order(L, l_lei, LEnum, lessequalothers); vmbreak; } vmcase(OP_EQK) { + StkId ra = RA(i); TValue *rb = KB(i); /* basic types do not use '__eq'; we can use raw equality */ - cond = luaV_equalobj(NULL, s2v(ra), rb); + int cond = luaV_rawequalobj(s2v(ra), rb); docondjump(); vmbreak; } vmcase(OP_EQI) { + StkId ra = RA(i); + int cond; int im = GETARG_sB(i); if (ttisinteger(s2v(ra))) cond = (ivalue(s2v(ra)) == im); @@ -1487,55 +1685,29 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_LTI) { - int im = GETARG_sB(i); - if (ttisinteger(s2v(ra))) - cond = (ivalue(s2v(ra)) < im); - else if (ttisfloat(s2v(ra))) - cond = luai_numlt(fltvalue(s2v(ra)), cast_num(im)); - else - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, 0, TM_LT)); - docondjump(); + op_orderI(L, l_lti, luai_numlt, 0, TM_LT); vmbreak; } vmcase(OP_LEI) { - int im = GETARG_sB(i); - if (ttisinteger(s2v(ra))) - cond = (ivalue(s2v(ra)) <= im); - else if (ttisfloat(s2v(ra))) - cond = luai_numle(fltvalue(s2v(ra)), cast_num(im)); - else - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, 0, TM_LE)); - docondjump(); + op_orderI(L, l_lei, luai_numle, 0, TM_LE); vmbreak; } vmcase(OP_GTI) { - int im = GETARG_sB(i); - if (ttisinteger(s2v(ra))) - cond = (im < ivalue(s2v(ra))); - else if (ttisfloat(s2v(ra))) - cond = luai_numlt(cast_num(im), fltvalue(s2v(ra))); - else - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, 1, TM_LT)); - docondjump(); + op_orderI(L, l_gti, luai_numgt, 1, TM_LT); vmbreak; } vmcase(OP_GEI) { - int im = GETARG_sB(i); - if (ttisinteger(s2v(ra))) - cond = (im <= ivalue(s2v(ra))); - else if (ttisfloat(s2v(ra))) - cond = luai_numle(cast_num(im), fltvalue(s2v(ra))); - else - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, 1, TM_LE)); - docondjump(); + op_orderI(L, l_gei, luai_numge, 1, TM_LE); vmbreak; } vmcase(OP_TEST) { - cond = !l_isfalse(s2v(ra)); + StkId ra = RA(i); + int cond = !l_isfalse(s2v(ra)); docondjump(); vmbreak; } vmcase(OP_TESTSET) { + StkId ra = RA(i); TValue *rb = vRB(i); if (l_isfalse(rb) == GETARG_k(i)) pc++; @@ -1546,253 +1718,245 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CALL) { + StkId ra = RA(i); + CallInfo *newci; int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; if (b != 0) /* fixed number of arguments? */ - L->top = ra + b; /* top signals number of arguments */ + L->top.p = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ - ProtectNT(luaD_call(L, ra, nresults)); + savepc(ci); /* in case of errors */ + if ((newci = luaD_precall(L, ra, nresults)) == NULL) + updatetrap(ci); /* C call; nothing else to be done */ + else { /* Lua call: run function in this same C frame */ + ci = newci; + goto startfunc; + } vmbreak; } vmcase(OP_TAILCALL) { + StkId ra = RA(i); int b = GETARG_B(i); /* number of arguments + 1 (function) */ - int delta = 0; /* virtual 'func' - real 'func' (vararg functions) */ + int n; /* number of results when calling a C function */ + int nparams1 = GETARG_C(i); + /* delta is virtual 'func' - real 'func' (vararg functions) */ + int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; if (b != 0) - L->top = ra + b; + L->top.p = ra + b; else /* previous instruction set top */ - b = cast_int(L->top - ra); + b = cast_int(L->top.p - ra); + savepc(ci); /* several calls here can raise errors */ if (TESTARG_k(i)) { - int nparams1 = GETARG_C(i); - if (nparams1) /* vararg function? */ - delta = ci->u.l.nextraargs + nparams1; - /* close upvalues from current call */ - luaF_close(L, base, LUA_OK); - updatestack(ci); + luaF_closeupval(L, base); /* close upvalues from current call */ + lua_assert(L->tbclist.p < base); /* no pending tbc variables */ + lua_assert(base == ci->func.p + 1); } - if (!ttisfunction(s2v(ra))) { /* not a function? */ - luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ - b++; /* there is now one extra argument */ + if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */ + goto startfunc; /* execute the callee */ + else { /* C function? */ + ci->func.p -= delta; /* restore 'func' (if vararg) */ + luaD_poscall(L, ci, n); /* finish caller */ + updatetrap(ci); /* 'luaD_poscall' can change hooks */ + goto ret; /* caller returns after the tail call */ } - if (!ttisLclosure(s2v(ra))) { /* C function? */ - ProtectNT(luaD_call(L, ra, LUA_MULTRET)); /* call it */ - updatestack(ci); /* stack may have been relocated */ - ci->func -= delta; - luaD_poscall(L, ci, cast_int(L->top - ra)); - return; - } - else { /* Lua tail call */ - ci->func -= delta; - luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - goto tailcall; - } - vmbreak; } vmcase(OP_RETURN) { + StkId ra = RA(i); int n = GETARG_B(i) - 1; /* number of results */ + int nparams1 = GETARG_C(i); if (n < 0) /* not fixed? */ - n = cast_int(L->top - ra); /* get what is available */ - else - L->top = ra + n; /* set call for 'luaD_poscall' */ + n = cast_int(L->top.p - ra); /* get what is available */ savepc(ci); - if (TESTARG_k(i)) { - int nparams1 = GETARG_C(i); - if (nparams1) /* vararg function? */ - ci->func -= ci->u.l.nextraargs + nparams1; - luaF_close(L, base, LUA_OK); /* there may be open upvalues */ + if (TESTARG_k(i)) { /* may there be open upvalues? */ + ci->u2.nres = n; /* save number of returns */ + if (L->top.p < ci->top.p) + L->top.p = ci->top.p; + luaF_close(L, base, CLOSEKTOP, 1); + updatetrap(ci); + updatestack(ci); } + if (nparams1) /* vararg function? */ + ci->func.p -= ci->u.l.nextraargs + nparams1; + L->top.p = ra + n; /* set call for 'luaD_poscall' */ luaD_poscall(L, ci, n); - return; + updatetrap(ci); /* 'luaD_poscall' can change hooks */ + goto ret; } vmcase(OP_RETURN0) { - if (L->hookmask) { - L->top = ra; - halfProtect(luaD_poscall(L, ci, 0)); /* no hurry... */ + if (l_unlikely(L->hookmask)) { + StkId ra = RA(i); + L->top.p = ra; + savepc(ci); + luaD_poscall(L, ci, 0); /* no hurry... */ + trap = 1; } else { /* do the 'poscall' here */ - int nres = ci->nresults; + int nres = get_nresults(ci->callstatus); L->ci = ci->previous; /* back to caller */ - L->top = base - 1; - while (nres-- > 0) - setnilvalue(s2v(L->top++)); /* all results are nil */ + L->top.p = base - 1; + for (; l_unlikely(nres > 0); nres--) + setnilvalue(s2v(L->top.p++)); /* all results are nil */ } - return; + goto ret; } vmcase(OP_RETURN1) { - if (L->hookmask) { - L->top = ra + 1; - halfProtect(luaD_poscall(L, ci, 1)); /* no hurry... */ + if (l_unlikely(L->hookmask)) { + StkId ra = RA(i); + L->top.p = ra + 1; + savepc(ci); + luaD_poscall(L, ci, 1); /* no hurry... */ + trap = 1; } else { /* do the 'poscall' here */ - int nres = ci->nresults; + int nres = get_nresults(ci->callstatus); L->ci = ci->previous; /* back to caller */ if (nres == 0) - L->top = base - 1; /* asked for no results */ + L->top.p = base - 1; /* asked for no results */ else { + StkId ra = RA(i); setobjs2s(L, base - 1, ra); /* at least this result */ - L->top = base; - while (--nres > 0) /* complete missing results */ - setnilvalue(s2v(L->top++)); + L->top.p = base; + for (; l_unlikely(nres > 1); nres--) + setnilvalue(s2v(L->top.p++)); /* complete missing results */ } } - return; - } - vmcase(OP_FORLOOP1) { - lua_Integer idx = intop(+, ivalue(s2v(ra)), 1); /* increment index */ - lua_Integer limit = ivalue(s2v(ra + 1)); - if (idx <= limit) { - pc -= GETARG_Bx(i); /* jump back */ - chgivalue(s2v(ra), idx); /* update internal index... */ - setivalue(s2v(ra + 3), idx); /* ...and external index */ - } - updatetrap(ci); - vmbreak; - } - vmcase(OP_FORPREP1) { - TValue *init = s2v(ra); - TValue *plimit = s2v(ra + 1); - lua_Integer ilimit, initv; - int stopnow; - if (unlikely(!forlimit(plimit, &ilimit, 1, &stopnow))) { - savestate(L, ci); /* for the error message */ - luaG_forerror(L, plimit, "limit"); + ret: /* return from a Lua function */ + if (ci->callstatus & CIST_FRESH) + return; /* end this frame */ + else { + ci = ci->previous; + goto returning; /* continue running caller in this frame */ } - initv = (stopnow ? 0 : ivalue(init)); - setivalue(plimit, ilimit); - setivalue(init, intop(-, initv, 1)); - pc += GETARG_Bx(i); - vmbreak; } vmcase(OP_FORLOOP) { - if (ttisinteger(s2v(ra))) { /* integer loop? */ - lua_Integer step = ivalue(s2v(ra + 2)); - lua_Integer idx = intop(+, ivalue(s2v(ra)), step); /* new index */ - lua_Integer limit = ivalue(s2v(ra + 1)); - if ((0 < step) ? (idx <= limit) : (limit <= idx)) { - pc -= GETARG_Bx(i); /* jump back */ - chgivalue(s2v(ra), idx); /* update internal index... */ - setivalue(s2v(ra + 3), idx); /* ...and external index */ - } - } - else { /* floating loop */ - lua_Number step = fltvalue(s2v(ra + 2)); - lua_Number limit = fltvalue(s2v(ra + 1)); - lua_Number idx = fltvalue(s2v(ra)); - idx = luai_numadd(L, idx, step); /* inc. index */ - if (luai_numlt(0, step) ? luai_numle(idx, limit) - : luai_numle(limit, idx)) { + StkId ra = RA(i); + if (ttisinteger(s2v(ra + 1))) { /* integer loop? */ + lua_Unsigned count = l_castS2U(ivalue(s2v(ra))); + if (count > 0) { /* still more iterations? */ + lua_Integer step = ivalue(s2v(ra + 1)); + lua_Integer idx = ivalue(s2v(ra + 2)); /* control variable */ + chgivalue(s2v(ra), l_castU2S(count - 1)); /* update counter */ + idx = intop(+, idx, step); /* add step to index */ + chgivalue(s2v(ra + 2), idx); /* update control variable */ pc -= GETARG_Bx(i); /* jump back */ - chgfltvalue(s2v(ra), idx); /* update internal index... */ - setfltvalue(s2v(ra + 3), idx); /* ...and external index */ } } - updatetrap(ci); + else if (floatforloop(ra)) /* float loop */ + pc -= GETARG_Bx(i); /* jump back */ + updatetrap(ci); /* allows a signal to break the loop */ vmbreak; } vmcase(OP_FORPREP) { - TValue *init = s2v(ra); - TValue *plimit = s2v(ra + 1); - TValue *pstep = s2v(ra + 2); - lua_Integer ilimit; - int stopnow; - if (ttisinteger(init) && ttisinteger(pstep) && - forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) { - /* all values are integer */ - lua_Integer initv = (stopnow ? 0 : ivalue(init)); - setivalue(plimit, ilimit); - setivalue(init, intop(-, initv, ivalue(pstep))); - } - else { /* try making all values floats */ - lua_Number ninit; lua_Number nlimit; lua_Number nstep; - savestate(L, ci); /* in case of errors */ - if (unlikely(!tonumber(plimit, &nlimit))) - luaG_forerror(L, plimit, "limit"); - setfltvalue(plimit, nlimit); - if (unlikely(!tonumber(pstep, &nstep))) - luaG_forerror(L, pstep, "step"); - setfltvalue(pstep, nstep); - if (unlikely(!tonumber(init, &ninit))) - luaG_forerror(L, init, "initial value"); - setfltvalue(init, luai_numsub(L, ninit, nstep)); - } - pc += GETARG_Bx(i); + StkId ra = RA(i); + savestate(L, ci); /* in case of errors */ + if (forprep(L, ra)) + pc += GETARG_Bx(i) + 1; /* skip the loop */ vmbreak; } vmcase(OP_TFORPREP) { - if (!ttisnil(s2v(ra + 3))) { /* is 'toclose' not nil? */ - /* create to-be-closed upvalue for it */ - halfProtect(luaF_newtbcupval(L, ra + 3)); - } - pc += GETARG_Bx(i); - i = *(pc++); /* go to next instruction */ + /* before: 'ra' has the iterator function, 'ra + 1' has the state, + 'ra + 2' has the initial value for the control variable, and + 'ra + 3' has the closing variable. This opcode then swaps the + control and the closing variables and marks the closing variable + as to-be-closed. + */ + StkId ra = RA(i); + TValue temp; /* to swap control and closing variables */ + setobj(L, &temp, s2v(ra + 3)); + setobjs2s(L, ra + 3, ra + 2); + setobj2s(L, ra + 2, &temp); + /* create to-be-closed upvalue (if closing var. is not nil) */ + halfProtect(luaF_newtbcupval(L, ra + 2)); + pc += GETARG_Bx(i); /* go to end of the loop */ + i = *(pc++); /* fetch next instruction */ lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i)); goto l_tforcall; } vmcase(OP_TFORCALL) { - l_tforcall: + l_tforcall: { /* 'ra' has the iterator function, 'ra + 1' has the state, - 'ra + 2' has the control variable, and 'ra + 3' has the - to-be-closed variable. The call will use the stack after - these values (starting at 'ra + 4') + 'ra + 2' has the closing variable, and 'ra + 3' has the control + variable. The call will use the stack starting at 'ra + 3', + so that it preserves the first three values, and the first + return will be the new value for the control variable. */ - /* push function, state, and control variable */ - memcpy(ra + 4, ra, 3 * sizeof(*ra)); - L->top = ra + 4 + 3; - Protect(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ + StkId ra = RA(i); + setobjs2s(L, ra + 5, ra + 3); /* copy the control variable */ + setobjs2s(L, ra + 4, ra + 1); /* copy state */ + setobjs2s(L, ra + 3, ra); /* copy function */ + L->top.p = ra + 3 + 3; + ProtectNT(luaD_call(L, ra + 3, GETARG_C(i))); /* do the call */ updatestack(ci); /* stack may have changed */ i = *(pc++); /* go to next instruction */ - ra += 2; /* adjust for next instruction */ lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); goto l_tforloop; - } + }} vmcase(OP_TFORLOOP) { - l_tforloop: - if (!ttisnil(s2v(ra + 2))) { /* continue loop? */ - setobjs2s(L, ra, ra + 2); /* save control variable */ + l_tforloop: { + StkId ra = RA(i); + if (!ttisnil(s2v(ra + 3))) /* continue loop? */ pc -= GETARG_Bx(i); /* jump back */ - } vmbreak; - } + }} vmcase(OP_SETLIST) { - int n = GETARG_B(i); - int c = GETARG_C(i); - unsigned int last; - Table *h; + StkId ra = RA(i); + unsigned n = cast_uint(GETARG_vB(i)); + unsigned last = cast_uint(GETARG_vC(i)); + Table *h = hvalue(s2v(ra)); if (n == 0) - n = cast_int(L->top - ra) - 1; + n = cast_uint(L->top.p - ra) - 1; /* get up to the top */ else - L->top = ci->top; /* correct top in case of GC */ - if (c == 0) { - c = GETARG_Ax(*pc); pc++; + L->top.p = ci->top.p; /* correct top in case of emergency GC */ + last += n; + if (TESTARG_k(i)) { + last += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1); + pc++; } - h = hvalue(s2v(ra)); - last = ((c-1)*LFIELDS_PER_FLUSH) + n; - if (last > luaH_realasize(h)) /* needs more space? */ + /* when 'n' is known, table should have proper size */ + if (last > h->asize) { /* needs more space? */ + /* fixed-size sets should have space preallocated */ + lua_assert(GETARG_vB(i) == 0); luaH_resizearray(L, h, last); /* preallocate it at once */ + } for (; n > 0; n--) { TValue *val = s2v(ra + n); - setobj2t(L, &h->array[last - 1], val); + obj2arr(h, last - 1, val); last--; luaC_barrierback(L, obj2gco(h), val); } vmbreak; } vmcase(OP_CLOSURE) { + StkId ra = RA(i); Proto *p = cl->p->p[GETARG_Bx(i)]; halfProtect(pushclosure(L, p, cl->upvals, base, ra)); checkGC(L, ra + 1); vmbreak; } vmcase(OP_VARARG) { - int n = GETARG_C(i) - 1; /* required results */ - Protect(luaT_getvarargs(L, ci, ra, n)); + StkId ra = RA(i); + int n = GETARG_C(i) - 1; /* required results (-1 means all) */ + int vatab = GETARG_k(i) ? GETARG_B(i) : -1; + Protect(luaT_getvarargs(L, ci, ra, n, vatab)); + vmbreak; + } + vmcase(OP_GETVARG) { + StkId ra = RA(i); + TValue *rc = vRC(i); + luaT_getvararg(ci, ra, rc); + vmbreak; + } + vmcase(OP_ERRNNIL) { + TValue *ra = vRA(i); + if (!ttisnil(ra)) + halfProtect(luaG_errnnil(L, cl, GETARG_Bx(i))); vmbreak; } - vmcase(OP_PREPVARARG) { - luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p); - updatetrap(ci); - if (trap) { + vmcase(OP_VARARGPREP) { + ProtectNT(luaT_adjustvarargs(L, ci, cl->p)); + if (l_unlikely(trap)) { /* previous "Protect" updated trap */ luaD_hookcall(L, ci); - L->oldpc = pc + 1; /* next opcode will be seen as a "new" line */ + L->oldpc = 1; /* next opcode will be seen as a "new" line */ } updatebase(ci); /* function has new base after adjustment */ vmbreak; @@ -1806,4 +1970,3 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } /* }================================================================== */ - diff --git a/lvm.h b/lvm.h index 7e8ec7155e..be7b9cb0ea 100644 --- a/lvm.h +++ b/lvm.h @@ -33,10 +33,20 @@ ** integral values) */ #if !defined(LUA_FLOORN2I) -#define LUA_FLOORN2I 0 +#define LUA_FLOORN2I F2Ieq #endif +/* +** Rounding modes for float->integer coercion + */ +typedef enum { + F2Ieq, /* no rounding; accepts only integral values */ + F2Ifloor, /* takes the floor of the number */ + F2Iceil /* takes the ceiling of the number */ +} F2Imod; + + /* convert an object to a float (including string coercion) */ #define tonumber(o,n) \ (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) @@ -50,12 +60,14 @@ /* convert an object to an integer (including string coercion) */ #define tointeger(o,i) \ - (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) + (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ + : luaV_tointeger(o,i,LUA_FLOORN2I)) /* convert an object to an integer (without string coercion) */ #define tointegerns(o,i) \ - (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o,i,LUA_FLOORN2I)) + (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ + : luaV_tointegerns(o,i,LUA_FLOORN2I)) #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) @@ -64,39 +76,39 @@ /* -** fast track for 'gettable': if 't' is a table and 't[k]' is present, -** return 1 with 'slot' pointing to 't[k]' (position of final result). -** Otherwise, return 0 (meaning it will have to check metamethod) -** with 'slot' pointing to an empty 't[k]' (if 't' is a table) or NULL -** (otherwise). 'f' is the raw get function to use. +** fast track for 'gettable' */ -#define luaV_fastget(L,t,k,slot,f) \ - (!ttistable(t) \ - ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ - : (slot = f(hvalue(t), k), /* else, do raw access */ \ - !isempty(slot))) /* result not empty? */ +#define luaV_fastget(t,k,res,f, tag) \ + (tag = (!ttistable(t) ? LUA_VNOTABLE : f(hvalue(t), k, res))) /* ** Special case of 'luaV_fastget' for integers, inlining the fast case ** of 'luaH_getint'. */ -#define luaV_fastgeti(L,t,k,slot) \ - (!ttistable(t) \ - ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ - : (slot = (l_castS2U(k) - 1u < hvalue(t)->alimit) \ - ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ - !isempty(slot))) /* result not empty? */ +#define luaV_fastgeti(t,k,res,tag) \ + if (!ttistable(t)) tag = LUA_VNOTABLE; \ + else { luaH_fastgeti(hvalue(t), k, res, tag); } + + +#define luaV_fastset(t,k,val,hres,f) \ + (hres = (!ttistable(t) ? HNOTATABLE : f(hvalue(t), k, val))) + +#define luaV_fastseti(t,k,val,hres) \ + if (!ttistable(t)) hres = HNOTATABLE; \ + else { luaH_fastseti(hvalue(t), k, val, hres); } /* -** Finish a fast set operation (when fast get succeeds). In that case, -** 'slot' points to the place to put the value. +** Finish a fast set operation (when fast set succeeds). */ -#define luaV_finishfastset(L,t,slot,v) \ - { setobj2t(L, cast(TValue *,slot), v); \ - luaC_barrierback(L, gcvalue(t), v); } +#define luaV_finishfastset(L,t,v) luaC_barrierback(L, gcvalue(t), v) + +/* +** Shift right is the same as shift left with a negative 'y' +*/ +#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y)) @@ -104,13 +116,14 @@ LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); -LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); -LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode); -LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode); -LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *slot); +LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); +LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, + F2Imod mode); +LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); +LUAI_FUNC lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key, + StkId val, lu_byte tag); LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - TValue *val, const TValue *slot); + TValue *val, int aux); LUAI_FUNC void luaV_finishOp (lua_State *L); LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); LUAI_FUNC void luaV_concat (lua_State *L, int total); diff --git a/lzio.c b/lzio.c index cd0a02d5f9..301df4b94e 100644 --- a/lzio.c +++ b/lzio.c @@ -14,6 +14,7 @@ #include "lua.h" +#include "lapi.h" #include "llimits.h" #include "lmem.h" #include "lstate.h" @@ -45,17 +46,25 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { /* --------------------------------------------------------------- read --- */ + +static int checkbuffer (ZIO *z) { + if (z->n == 0) { /* no bytes in buffer? */ + if (luaZ_fill(z) == EOZ) /* try to read more */ + return 0; /* no more input */ + else { + z->n++; /* luaZ_fill consumed first byte; put it back */ + z->p--; + } + } + return 1; /* now buffer has something */ +} + + size_t luaZ_read (ZIO *z, void *b, size_t n) { while (n) { size_t m; - if (z->n == 0) { /* no bytes in buffer? */ - if (luaZ_fill(z) == EOZ) /* try to read more */ - return n; /* no more input; return number of missing bytes */ - else { - z->n++; /* luaZ_fill consumed first byte; put it back */ - z->p--; - } - } + if (!checkbuffer(z)) + return n; /* no more input; return number of missing bytes */ m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ memcpy(b, z->p, m); z->n -= m; @@ -66,3 +75,15 @@ size_t luaZ_read (ZIO *z, void *b, size_t n) { return 0; } + +const void *luaZ_getaddr (ZIO* z, size_t n) { + const void *res; + if (!checkbuffer(z)) + return NULL; /* no more input */ + if (z->n < n) /* not enough bytes? */ + return NULL; /* block not whole; cannot give an address */ + res = z->p; /* get block address */ + z->n -= n; /* consume these bytes */ + z->p += n; + return res; +} diff --git a/lzio.h b/lzio.h index 38f397fd28..49047c98cb 100644 --- a/lzio.h +++ b/lzio.h @@ -32,7 +32,7 @@ typedef struct Mbuffer { #define luaZ_sizebuffer(buff) ((buff)->buffsize) #define luaZ_bufflen(buff) ((buff)->n) -#define luaZ_buffremove(buff,i) ((buff)->n -= (i)) +#define luaZ_buffremove(buff,i) ((buff)->n -= cast_sizet(i)) #define luaZ_resetbuffer(buff) ((buff)->n = 0) @@ -48,6 +48,7 @@ LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data); LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ +LUAI_FUNC const void *luaZ_getaddr (ZIO* z, size_t n); /* --------- Private Part ------------------ */ diff --git a/makefile b/makefile index 3bd319bea9..8674519f5f 100644 --- a/makefile +++ b/makefile @@ -1,32 +1,35 @@ -# makefile for building Lua -# see INSTALL for installation instructions -# see ../Makefile and luaconf.h for further customization +# Developer's makefile for building Lua +# see luaconf.h for further customization # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= # Warnings valid for both C and C++ CWARNSCPP= \ + -Wfatal-errors \ -Wextra \ -Wshadow \ - -Wsign-compare \ -Wundef \ -Wwrite-strings \ -Wredundant-decls \ -Wdisabled-optimization \ -Wdouble-promotion \ - #-Wno-aggressive-loop-optimizations \ - #-Wlogical-op \ - #-Wfatal-errors \ - #-Wstrict-aliasing=3 \ + -Wmissing-declarations \ + -Wconversion \ + # the next warnings might be useful sometimes, + # but usually they generate too much noise + # -Wstrict-overflow=2 \ # -Werror \ # -pedantic # warns if we use jump tables \ - # the next warnings generate too much noise, so they are disabled - # -Wconversion -Wno-sign-conversion \ - # -Wsign-conversion \ - # -Wstrict-overflow=2 \ # -Wformat=2 \ # -Wcast-qual \ + +# Warnings for gcc, not valid for clang +CWARNGCC= \ + -Wlogical-op \ + -Wno-aggressive-loop-optimizations \ + + # The next warnings are neither valid nor needed for C++ CWARNSC= -Wdeclaration-after-statement \ -Wmissing-prototypes \ @@ -36,26 +39,41 @@ CWARNSC= -Wdeclaration-after-statement \ -Wold-style-definition \ -CWARNS= $(CWARNSCPP) $(CWARNSC) +CWARNS= $(CWARNSCPP) $(CWARNSC) $(CWARNGCC) +# Some useful compiler options for internal tests: +# -DLUAI_ASSERT turns on all assertions inside Lua. +# -DHARDSTACKTESTS forces a reallocation of the stack at every point where +# the stack can be reallocated. +# -DHARDMEMTESTS forces a full collection at all points where the collector +# can run. +# -DEMERGENCYGCTESTS forces an emergency collection at every single allocation. +# -DEXTERNMEMCHECK removes internal consistency checking of blocks being +# deallocated (useful when an external tool like valgrind does the check). +# -DMAXINDEXRK=k limits range of constants in RK instruction operands. +# -DLUA_COMPAT_5_3 -# -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"' -# -DMAXINDEXRK=1 -# -g -DLUA_USER_H='"ltests.h"' # -pg -malign-double # -DLUA_USE_CTYPE -DLUA_USE_APICHECK -# ('-ftrapv' for runtime checks of integer overflows) -# -fsanitize=undefined -ftrapv -fno-inline -TESTS= -DLUA_USER_H='"ltests.h"' -O0 + +# The following options help detect "undefined behavior"s that seldom +# create problems; some are only available in newer gcc versions. To +# use some of them, we also have to define an environment variable +# ASAN_OPTIONS="detect_invalid_pointer_pairs=2". +# -fsanitize=undefined +# -fsanitize=pointer-subtract -fsanitize=address -fsanitize=pointer-compare +# TESTS= -DLUA_USER_H='"ltests.h"' -Og -g -# LOCAL = $(TESTS) $(CWARNS) -g +LOCAL = $(TESTS) $(CWARNS) -# enable Linux goodies -MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -DLUA_USE_READLINE -MYLDFLAGS= $(LOCAL) -Wl,-E -MYLIBS= -ldl -lreadline +# To enable Linux goodies, -DLUA_USE_LINUX +# For C89, "-std=c89 -DLUA_USE_C89" +# Note that Linux/Posix options are not compatible with C89 +MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX +MYLDFLAGS= -Wl,-E +MYLIBS= -ldl CC= gcc @@ -82,11 +100,9 @@ LIB_O= lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o \ LUA_T= lua LUA_O= lua.o -# LUAC_T= luac -# LUAC_O= luac.o print.o -ALL_T= $(CORE_T) $(LUA_T) $(LUAC_T) -ALL_O= $(CORE_O) $(LUA_O) $(LUAC_O) $(AUX_O) $(LIB_O) +ALL_T= $(CORE_T) $(LUA_T) +ALL_O= $(CORE_O) $(LUA_O) $(AUX_O) $(LIB_O) ALL_A= $(CORE_T) all: $(ALL_T) @@ -103,11 +119,8 @@ $(CORE_T): $(CORE_O) $(AUX_O) $(LIB_O) $(LUA_T): $(LUA_O) $(CORE_T) $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(CORE_T) $(LIBS) $(MYLIBS) $(DL) -$(LUAC_T): $(LUAC_O) $(CORE_T) - $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(CORE_T) $(LIBS) $(MYLIBS) clean: - rcsclean -u $(RM) $(ALL_T) $(ALL_O) depend: @@ -132,40 +145,45 @@ $(ALL_O): makefile ltests.h lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \ ltable.h lundump.h lvm.h -lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h -lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h llimits.h +lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ - ldo.h lgc.h lstring.h ltable.h lvm.h -lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h + ldo.h lgc.h lstring.h ltable.h lvm.h lopnames.h +lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h -ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \ ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ lparser.h lstring.h ltable.h lundump.h lvm.h -ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \ - ltm.h lzio.h lmem.h lundump.h +ldump.o: ldump.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h lgc.h ltable.h lundump.h lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h -linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h -liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h llimits.h +liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \ lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \ lstring.h ltable.h -lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h -loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \ ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \ lvm.h -lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h -loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h \ + lobject.h +loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ ldo.h lfunc.h lstring.h lgc.h ltable.h @@ -174,25 +192,28 @@ lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lstring.h ltable.h lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h -lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h -ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h ltests.o: ltests.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h \ lparser.h lctype.h ldebug.h ldo.h lfunc.h lopnames.h lstring.h lgc.h \ ltable.h lualib.h ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h -lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ - lundump.h -lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \ - ltable.h lvm.h ljumptab.h -lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \ - lobject.h ltm.h lzio.h + ltable.h lundump.h +lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h +lvm.o: lvm.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ + lstring.h ltable.h lvm.h ljumptab.h +lzio.o: lzio.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h # (end of Makefile) diff --git a/manual/2html b/manual/2html index 04b2c61ed0..b7afd2a6e4 100755 --- a/manual/2html +++ b/manual/2html @@ -8,11 +8,11 @@ --------------------------------------------------------------- header = [[ - + -Lua 5.4 Reference Manual +Lua 5.5 Reference Manual @@ -23,14 +23,14 @@ header = [[


[Lua logo] -Lua 5.4 Reference Manual +Lua 5.5 Reference Manual

by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes

Copyright -© 2018 Lua.org, PUC-Rio. All rights reserved. +© 2025 Lua.org, PUC-Rio. All rights reserved.


@@ -324,6 +324,7 @@ N = function (s) return (string.gsub(s, " ", " ")) end, NE = id, -- tag"foreignphrase", num = id, ["nil"] = fixed(Tag.b"nil"), +fail = fixed(Tag.b"fail"), Open = fixed"{", part = section("h1", true), Pat = compose(verbfixed, prepos("'", "'")), @@ -357,7 +358,7 @@ item = function (s) local t, p = string.match(s, "^([^\n|]+)|()") if t then s = string.sub(s, p) - s = Tag.b(t..": ") .. s + s = Tag.b(t) ..": " .. s end return Tag.li(fixpara(s)) end, diff --git a/manual/manual.of b/manual/manual.of index 8a8ebad5df..5fa4e097e1 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -15,12 +15,12 @@ Lua is dynamically typed, runs by interpreting bytecode with a register-based virtual machine, and has automatic memory management with -incremental garbage collection, +a generational garbage collection, making it ideal for configuration, scripting, and rapid prototyping. Lua is implemented as a library, written in @emphx{clean C}, -the common subset of @N{Standard C} and C++. +the common subset of @N{standard C} and C++. The Lua distribution includes a host program called @id{lua}, which uses the Lua library to offer a complete, standalone Lua interpreter, @@ -59,8 +59,12 @@ see Roberto's book, @emphx{Programming in Lua}. @C{-------------------------------------------------------------------------} @sect1{basic| @title{Basic Concepts} +@simplesect{ + This section describes the basic concepts of the language. +} + @sect2{TypesSec| @title{Values and Types} Lua is a dynamically typed language. @@ -79,10 +83,16 @@ There are eight @x{basic types} in Lua: @def{thread}, and @def{table}. The type @emph{nil} has one single value, @nil, whose main property is to be different from any other value; -it usually represents the absence of a useful value. +it often represents the absence of a useful value. The type @emph{boolean} has two values, @false and @true. Both @nil and @false make a condition false; -any other value makes it true. +they are collectively called @def{false values}. +Any other value makes a condition true. +Despite its name, +@false is frequently used as an alternative to @nil, +with the key difference that @false behaves +like a regular value in a table, +while a @nil in a table represents an absent key. The type @emph{number} represents both integer numbers and real (floating-point) numbers, @@ -95,6 +105,14 @@ is particularly attractive for small machines and embedded systems. (See macro @id{LUA_32BITS} in file @id{luaconf.h}.) +Unless stated otherwise, +any overflow when manipulating integer values @def{wrap around}, +according to the usual rules of two's complement arithmetic. +(In other words, +the actual result is the unique representable integer +that is equal modulo @M{2@sp{n}} to the mathematical result, +where @M{n} is the number of bits of the integer type.) + Lua has explicit rules about when each subtype is used, but it also converts between them automatically as needed @see{coercion}. Therefore, @@ -109,7 +127,8 @@ strings can contain any 8-bit value, including @x{embedded zeros} (@Char{\0}). Lua is also encoding-agnostic; it makes no assumptions about the contents of a string. -The length of any string in Lua must fit in a Lua integer. +The length of any string in Lua must fit in a Lua integer, +and the string plus a small header must fit in @id{size_t}. Lua can call (and manipulate) functions written in Lua and functions written in C @see{functioncall}. @@ -130,7 +149,8 @@ the programmer can define operations for full userdata values @see{metatable}. Userdata values cannot be created or modified in Lua, only through the @N{C API}. -This guarantees the integrity of data owned by the host program. +This guarantees the integrity of data owned by +the host program and @N{C libraries}. The type @def{thread} represents independent threads of execution and it is used to implement coroutines @see{coroutine}. @@ -146,7 +166,7 @@ used by the @x{IEEE 754} standard to represent undefined numerical results, such as @T{0/0}.) Tables can be @emph{heterogeneous}; that is, they can contain values of all types (except @nil). -Any key with value @nil is not considered part of the table. +Any key associated to the value @nil is not considered part of the table. Conversely, any key that is not part of a table has an associated value @nil. @@ -176,14 +196,10 @@ In particular, floats with integral values are equal to their respective integers (e.g., @T{1.0 == 1}). To avoid ambiguities, -any float with integral value used as a key -is converted to its respective integer. +any float used as a key that is equal to an integer +is converted to that integer. For instance, if you write @T{a[2.0] = true}, -the actual key inserted into the table will be the -integer @T{2}. -(On the other hand, -2 and @St{2} are different Lua values and therefore -denote different table entries.) +the actual key inserted into the table will be the integer @T{2}. Tables, functions, threads, and (full) userdata values are @emph{objects}: @@ -194,15 +210,93 @@ always manipulate references to such values; these operations do not imply any kind of copy. The library function @Lid{type} returns a string describing the type -of a given value @see{predefined}. +of a given value @seeF{type}. + +} + +@sect2{globalenv| @title{Scopes, Variables, and Environments} +@index{visibility} + +A variable name refers to a global or a local variable according +to the declaration that is in context at that point of the code. +(For the purposes of this discussion, +a function's formal parameter is equivalent to a local variable.) +All chunks start with an implicit declaration @T{global *}, +which declares all free names as global variables; +this preambular declaration becomes void inside the scope of any other +@Rw{global} declaration, +as the following example illustrates: +@verbatim{ +X = 1 -- Ok, global by default +do + global Y -- voids the implicit initial declaration + Y = 1 -- Ok, Y declared as global + X = 1 -- ERROR, X not declared +end +X = 2 -- Ok, global by default again +} +So, outside any global declaration, +Lua works as @x{global-by-default}. +Inside any global declaration, +Lua works without a default: +All variables must be declared. + +Lua is a lexically scoped language. +The scope of a variable declaration begins at the first statement after +the declaration and lasts until the last non-void statement +of the innermost block that includes the declaration. +(@emph{Void statements} are labels and empty statements.) + +A declaration shadows any declaration for the same name that +is in context at the point of the declaration. Inside this +shadow, any outer declaration for that name is void. +See the next example: +@verbatim{ +global print, x +x = 10 -- global variable +do -- new block + local x = x -- new 'x', with value 10 + print(x) --> 10 + x = x+1 + do -- another block + local x = x+1 -- another 'x' + print(x) --> 12 + end + print(x) --> 11 +end +print(x) --> 10 (the global one) } -@sect2{globalenv| @title{Environments and the Global Environment} +Notice that, in a declaration like @T{local x = x}, +the new @id{x} being declared is not in scope yet, +and so the @id{x} on the right-hand side refers to the outside variable. + +Because of the @x{lexical scoping} rules, +local variables can be freely accessed by functions +defined inside their scope. +A local variable used by an inner function is called an @def{upvalue} +(or @emphx{external local variable}, or simply @emphx{external variable}) +inside the inner function. + +Notice that each execution of a @Rw{local} statement +defines new local variables. +Consider the following example: +@verbatim{ +a = {} +local x = 20 +for i = 1, 10 do + local y = 0 + a[i] = function () y = y + 1; return x + y end +end +} +The loop creates ten closures +(that is, ten instances of the anonymous function). +Each of these closures uses a different @id{y} variable, +while all of them share the same @id{x}. -As will be discussed in @refsec{variables} and @refsec{assignment}, -any reference to a free name -(that is, a name not bound to any declaration) @id{var} +As we will discuss further in @refsec{variables} and @refsec{assignment}, +any reference to a global variable @id{var} is syntactically translated to @T{_ENV.var}. Moreover, every chunk is compiled in the scope of an external local variable named @id{_ENV} @see{chunks}, @@ -210,65 +304,84 @@ so @id{_ENV} itself is never a free name in a chunk. Despite the existence of this external @id{_ENV} variable and the translation of free names, -@id{_ENV} is a completely regular name. +@id{_ENV} is a regular name. In particular, you can define new variables and parameters with that name. -Each reference to a free name uses the @id{_ENV} that is -visible at that point in the program, -following the usual visibility rules of Lua @see{visibility}. +(However, you should not define @id{_ENV} as a global variable, +otherwise @T{_ENV.var} would translate to +@T{_ENV._ENV.var} and so on, in an infinite loop.) +Each reference to a global variable name uses the @id{_ENV} that is +visible at that point in the program. Any table used as the value of @id{_ENV} is called an @def{environment}. Lua keeps a distinguished environment called the @def{global environment}. This value is kept at a special index in the C registry @see{registry}. In Lua, the global variable @Lid{_G} is initialized with this same value. -(@Lid{_G} is never used internally.) +(@Lid{_G} is never used internally, +so changing its value will affect only your own code.) When Lua loads a chunk, -the default value for its @id{_ENV} upvalue +the default value for its @id{_ENV} variable is the global environment @seeF{load}. Therefore, by default, -free names in Lua code refer to entries in the global environment -(and, therefore, they are also called @def{global variables}). +global variables in Lua code refer to entries in the global environment +and, therefore, they act as conventional global variables. Moreover, all standard libraries are loaded in the global environment and some functions there operate on that environment. You can use @Lid{load} (or @Lid{loadfile}) to load a chunk with a different environment. (In C, you have to load the chunk and then change the value -of its first upvalue.) +of its first upvalue; see @See{lua_setupvalue}.) } @sect2{error| @title{Error Handling} +Several operations in Lua can @emph{raise} an error. +An error interrupts the normal flow of the program, +which can continue by @emph{catching} the error. + +Lua code can explicitly raise an error by calling the +@Lid{error} function. +(This function never returns.) + +To catch errors in Lua, +you can do a @def{protected call}, +using @Lid{pcall} (or @Lid{xpcall}). +The function @Lid{pcall} calls a given function in @def{protected mode}. +Any error while running the function stops its execution, +and control returns immediately to @id{pcall}, +which returns a status code. + Because Lua is an embedded extension language, -all Lua actions start from @N{C code} in the host program -calling a function from the Lua library. +Lua code starts running by a call +from @N{C code} in the host program. (When you use Lua standalone, the @id{lua} application is the host program.) -Whenever an error occurs during +Usually, this call is protected; +so, when an otherwise unprotected error occurs during the compilation or execution of a Lua chunk, control returns to the host, -which can take appropriate measures -(such as printing an error message). - -Lua code can explicitly generate an error by calling the -@Lid{error} function. -If you need to catch errors in Lua, -you can use @Lid{pcall} or @Lid{xpcall} -to call a given function in @emphx{protected mode}. +which can take appropriate measures, +such as printing an error message. Whenever there is an error, -an @def{error object} (also called an @def{error message}) +an @def{error object} is propagated with information about the error. Lua itself only generates errors whose error object is a string, -but programs may generate errors with -any value as the error object. +but programs can generate errors with +any value as the error object, +except @nil. +(Lua will change a @nil as error object to a string message.) It is up to the Lua program or its host to handle such error objects. +For historical reasons, +an error object is often called an @def{error message}, +even though it does not have to be a string. -When you use @Lid{xpcall} or @Lid{lua_pcall}, -you may give a @def{message handler} +When you use @Lid{xpcall} (or @Lid{lua_pcall}, in C) +you can give a @def{message handler} to be called in case of errors. This function is called with the original error object and returns a new error object. @@ -280,9 +393,15 @@ so, an error inside the message handler will call the message handler again. If this loop goes on for too long, Lua breaks it and returns an appropriate message. -(The message handler is called only for regular runtime errors. +The message handler is called only for regular runtime errors. It is not called for memory-allocation errors -nor for errors while running finalizers.) +nor for errors while running finalizers or other message handlers. + +Lua also offers a system of @emph{warnings} @seeF{warn}. +Unlike errors, warnings do not interfere +in any way with program execution. +They typically only generate a message to the user, +although this behavior can be adapted from C @seeC{lua_setwarnf}. } @@ -291,79 +410,63 @@ nor for errors while running finalizers.) Every value in Lua can have a @emph{metatable}. This @def{metatable} is an ordinary Lua table that defines the behavior of the original value -under certain special operations. +under certain events. You can change several aspects of the behavior -of operations over a value by setting specific fields in its metatable. +of a value by setting specific fields in its metatable. For instance, when a non-numeric value is the operand of an addition, -Lua checks for a function in the field @St{__add} of the value's metatable. +Lua checks for a function in the field @idx{__add} of the value's metatable. If it finds one, Lua calls this function to perform the addition. The key for each event in a metatable is a string with the event name prefixed by two underscores; -the corresponding values are called @def{metamethods}. -In the previous example, the key is @St{__add} +the corresponding value is called a @def{metavalue}. +For most events, the metavalue must be a function, +which is then called a @def{metamethod}. +In the previous example, the key is the string @St{__add} and the metamethod is the function that performs the addition. Unless stated otherwise, -metamethods should be function values. +a metamethod can in fact be any @x{callable value}, +which is either a function or a value with a @idx{__call} metamethod. You can query the metatable of any value using the @Lid{getmetatable} function. Lua queries metamethods in metatables using a raw access @seeF{rawget}. -So, to retrieve the metamethod for event @id{ev} in object @id{o}, -Lua does the equivalent to the following code: -@verbatim{ -rawget(getmetatable(@rep{o}) or {}, "__@rep{ev}") -} You can replace the metatable of tables using the @Lid{setmetatable} function. -You cannot change the metatable of other types from Lua code -(except by using the @link{debuglib|debug library}); -you should use the @N{C API} for that. +You cannot change the metatable of other types from Lua code, +except by using the @link{debuglib|debug library}. -Tables and full userdata have individual metatables -(although multiple tables and userdata can share their metatables). +Tables and full userdata have individual metatables, +although multiple tables and userdata can share their metatables. Values of all other types share one single metatable per type; that is, there is one single metatable for all numbers, one for all strings, etc. By default, a value has no metatable, but the string library sets a metatable for the string type @see{strlib}. -A metatable controls how an object behaves in -arithmetic operations, bitwise operations, -order comparisons, concatenation, length operation, calls, and indexing. -A metatable also can define a function to be called -when a userdata or a table is @link{GC|garbage collected}. - -For the unary operators (negation, length, and bitwise NOT), -the metamethod is computed and called with a dummy second operand, -equal to the first one. -This extra operand is only to simplify Lua's internals -(by making these operators behave like a binary operation) -and may be removed in future versions. -(For most uses this extra operand is irrelevant.) - -A detailed list of events controlled by metatables is given next. -Each operation is identified by its corresponding key. +A detailed list of operations controlled by metatables is given next. +Each event is identified by its corresponding key. +By convention, all metatable keys used by Lua are composed by +two underscores followed by lowercase Latin letters. @description{ @item{@idx{__add}| the addition (@T{+}) operation. -If any operand for an addition is not a number -(nor a string coercible to a number), +If any operand for an addition is not a number, Lua will try to call a metamethod. -First, Lua will check the first operand (even if it is valid). -If that operand does not define a metamethod for @idx{__add}, +It starts by checking the first operand (even if it is a number); +if that operand does not define a metamethod for @idx{__add}, then Lua will check the second operand. If Lua can find a metamethod, it calls the metamethod with the two operands as arguments, and the result of the call (adjusted to one value) is the result of the operation. -Otherwise, -it raises an error. +Otherwise, if no metamethod is found, +Lua raises an error. } @item{@idx{__sub}| @@ -406,7 +509,7 @@ the bitwise AND (@T{&}) operation. Behavior similar to the addition operation, except that Lua will try a metamethod if any operand is neither an integer -nor a value coercible to an integer @see{coercion}. +nor a float coercible to an integer @see{coercion}. } @item{@idx{__bor}| @@ -470,7 +573,7 @@ the less than (@T{<}) operation. Behavior similar to the addition operation, except that Lua will try a metamethod only when the values being compared are neither both numbers nor both strings. -The result of the call is always converted to a boolean. +Moreover, the result of the call is always converted to a boolean. } @item{@idx{__le}| @@ -482,19 +585,19 @@ Behavior similar to the less than operation. The indexing access operation @T{table[key]}. This event happens when @id{table} is not a table or when @id{key} is not present in @id{table}. -The metamethod is looked up in @id{table}. +The metavalue is looked up in the metatable of @id{table}. -Despite the name, -the metamethod for this event can be either a function or a table. +The metavalue for this event can be either a function, a table, +or any value with an @idx{__index} metavalue. If it is a function, it is called with @id{table} and @id{key} as arguments, and the result of the call (adjusted to one value) is the result of the operation. -If it is a table, -the final result is the result of indexing this table with @id{key}. -(This indexing is regular, not raw, -and therefore can trigger another metamethod.) +Otherwise, +the final result is the result of indexing this metavalue with @id{key}. +This indexing is regular, not raw, +and therefore can trigger another @idx{__index} metavalue. } @item{@idx{__newindex}| @@ -502,22 +605,24 @@ The indexing assignment @T{table[key] = value}. Like the index event, this event happens when @id{table} is not a table or when @id{key} is not present in @id{table}. -The metamethod is looked up in @id{table}. +The metavalue is looked up in the metatable of @id{table}. Like with indexing, -the metamethod for this event can be either a function or a table. +the metavalue for this event can be either a function, a table, +or any value with an @idx{__newindex} metavalue. If it is a function, it is called with @id{table}, @id{key}, and @id{value} as arguments. -If it is a table, -Lua does an indexing assignment to this table with the same key and value. -(This assignment is regular, not raw, -and therefore can trigger another metamethod.) +Otherwise, +Lua repeats the indexing assignment over this metavalue +with the same key and value. +This assignment is regular, not raw, +and therefore can trigger another @idx{__newindex} metavalue. -Whenever there is a @idx{__newindex} metamethod, +Whenever a @idx{__newindex} metavalue is invoked, Lua does not perform the primitive assignment. -(If necessary, +If needed, the metamethod itself can call @Lid{rawset} -to do the assignment.) +to do the assignment. } @item{@idx{__call}| @@ -529,16 +634,29 @@ If present, the metamethod is called with @id{func} as its first argument, followed by the arguments of the original call (@id{args}). All results of the call -are the result of the operation. -(This is the only metamethod that allows multiple results.) +are the results of the operation. +This is the only metamethod that allows multiple results. } } -It is a good practice to add all needed metamethods to a table -before setting it as a metatable of some object. -In particular, the @idx{__gc} metamethod works only when this order -is followed @see{finalizers}. +In addition to the previous list, +the interpreter also respects the following keys in metatables: +@idx{__gc} @see{finalizers}, +@idx{__close} @see{to-be-closed}, +@idx{__mode} @see{weak-table}, +and @idx{__name}. +(The entry @idx{__name}, +when it contains a string, +may be used by @Lid{tostring} and in error messages.) + +For the unary operators (negation, length, and bitwise NOT), +the metamethod is computed and called with a dummy second operand, +equal to the first one. +This extra operand is only to simplify Lua's internals +(by making these operators behave like a binary operation) +and may be removed in future versions. +For most uses this extra operand is irrelevant. Because metatables are regular tables, they can contain arbitrary fields, @@ -547,38 +665,69 @@ Some functions in the standard library (e.g., @Lid{tostring}) use other fields in metatables for their own purposes. +It is a good practice to add all needed metamethods to a table +before setting it as a metatable of some object. +In particular, the @idx{__gc} metamethod works only when this order +is followed @see{finalizers}. +It is also a good practice to set the metatable of an object +right after its creation. + } @sect2{GC| @title{Garbage Collection} +@simplesect{ + Lua performs automatic memory management. This means that you do not have to worry about allocating memory for new objects or freeing it when the objects are no longer needed. Lua manages memory automatically by running -a @def{garbage collector} to collect all @emph{dead objects} -(that is, objects that are no longer accessible from Lua). +a @def{garbage collector} to collect all @emph{dead} objects. All memory used by Lua is subject to automatic management: strings, tables, userdata, functions, threads, internal structures, etc. +An object is considered @def{dead} +as soon as the collector can be sure the object +will not be accessed again in the normal execution of the program. +(@Q{Normal execution} here excludes finalizers, +which resurrect dead objects @see{finalizers}, +and it excludes also some operations using the debug library.) +Note that the time when the collector can be sure that an object +is dead may not coincide with the programmer's expectations. +The only guarantees are that Lua will not collect an object +that may still be accessed in the normal execution of the program, +and it will eventually collect an object +that is inaccessible from Lua. +(Here, +@emph{inaccessible from Lua} means that neither a variable nor +another live object refer to the object.) +Because Lua has no knowledge about @N{C code}, +it never collects objects accessible through the registry @see{registry}, +which includes the global environment @see{globalenv} and +the main thread. + + The garbage collector (GC) in Lua can work in two modes: incremental and generational. The default GC mode with the default parameters are adequate for most uses. -Programs that waste a large proportion of its time +However, programs that waste a large proportion of their time allocating and freeing memory can benefit from other settings. Keep in mind that the GC behavior is non-portable both across platforms and across different Lua releases; therefore, optimal settings are also non-portable. You can change the GC mode and parameters by calling -@Lid{lua_gc} in C +@Lid{lua_gc} @N{in C} or @Lid{collectgarbage} in Lua. -You can also use these functions to control -the collector directly (e.g., stop and restart it). +You can also use these functions to control the collector directly, +for instance to stop or restart it. -@sect3{@title{Incremental Garbage Collection} +} + +@sect3{incmode| @title{Incremental Garbage Collection} In incremental mode, each GC cycle performs a mark-and-sweep collection in small steps @@ -591,72 +740,79 @@ and the @def{garbage-collector step size}. The garbage-collector pause controls how long the collector waits before starting a new cycle. -The collector starts a new cycle when the use of memory -hits @M{n%} of the use after the previous collection. +The collector starts a new cycle when the number of bytes +hits @M{n%} of the total after the previous collection. Larger values make the collector less aggressive. -Values smaller than 100 mean the collector will not wait to +Values equal to or less than 100 mean the collector will not wait to start a new cycle. -A value of 200 means that the collector waits for the total memory in use -to double before starting a new cycle. -The default value is 200; the maximum value is 1000. - -The garbage-collector step multiplier -controls the relative speed of the collector relative to -memory allocation, -that is, -how many elements it marks or sweeps for each -kilobyte of memory allocated. -Larger values make the collector more aggressive but also increase -the size of each incremental step. -You should not use values smaller than 100, -because they make the collector too slow and -can result in the collector never finishing a cycle. -The default value is 100; the maximum value is 1000. +A value of 200 means that the collector waits for +the total number of bytes to double before starting a new cycle. The garbage-collector step size controls the size of each incremental step, specifically how many bytes the interpreter allocates -before performing a step. -This parameter is logarithmic: -A value of @M{n} means the interpreter will allocate @M{2@sp{n}} -bytes between steps and perform equivalent work during the step. -A large value (e.g., 60) makes the collector a stop-the-world -(non-incremental) collector. -The default value is 13, -which makes for steps of approximately @N{8 Kbytes}. +before performing a step: +A value of @M{n} means the interpreter will allocate +approximately @M{n} bytes between steps. + +The garbage-collector step multiplier +controls how much work each incremental step does. +A value of @M{n} means the interpreter will execute +@M{n%} @emphx{units of work} for each word allocated. +A unit of work corresponds roughly to traversing one slot +or sweeping one object. +Larger values make the collector more aggressive. +Beware that values too small can +make the collector too slow to ever finish a cycle. +As a special case, a zero value means unlimited work, +effectively producing a non-incremental, stop-the-world collector. } -@sect3{@title{Generational Garbage Collection} +@sect3{genmode| @title{Generational Garbage Collection} In generational mode, the collector does frequent @emph{minor} collections, which traverses only objects recently created. -If after a minor collection the use of memory is still above a limit, -the collector does a @emph{major} collection, +If after a minor collection the number of bytes is above a limit, +the collector shifts to a @emph{major} collection, which traverses all objects. -The generational mode uses two parameters: -the @def{major multiplier} and the @def{the minor multiplier}. - -The major multiplier controls the frequency of major collections. -For a major multiplier @M{x}, -a new major collection will be done when memory -grows @M{x%} larger than the memory in use after the previous major -collection. -For instance, for a multiplier of 100, -the collector will do a major collection when the use of memory -gets larger than twice the use after the previous collection. -The default value is 100; the maximum value is 1000. +The collector will then stay doing major collections until +it detects that the program is generating enough garbage to justify +going back to minor collections. + +The generational mode uses three parameters: +the @def{minor multiplier}, the @def{minor-major multiplier}, +and the @def{major-minor multiplier}. The minor multiplier controls the frequency of minor collections. For a minor multiplier @M{x}, -a new minor collection will be done when memory -grows @M{x%} larger than the memory in use after the previous major -collection. +a new minor collection will be done when the number of bytes +grows @M{x%} larger than the number in use just +after the last major collection. For instance, for a multiplier of 20, -the collector will do a minor collection when the use of memory -gets 20% larger than the use after the previous major collection. -The default value is 20; the maximum value is 200. +the collector will do a minor collection when the number of bytes +gets 20% larger than the total after the last major collection. + +The minor-major multiplier controls the shift to major collections. +For a multiplier @M{x}, +the collector will shift to a major collection +when the number of bytes from old objects grows @M{x%} larger +than the total after the previous major collection. +For instance, for a multiplier of 100, +the collector will do a major collection when the number of old bytes +gets larger than twice the total after the previous major collection. +As a special case, +a value of 0 stops the collector from doing major collections. + +The major-minor multiplier controls the shift back to minor collections. +For a multiplier @M{x}, +the collector will shift back to minor collections +after a major collection collects at least @M{x%} +of the bytes allocated during the last cycle. +In particular, for a multiplier of 0, +the collector will immediately shift back to minor collections +after doing one major collection. } @@ -667,22 +823,22 @@ and, using the @N{C API}, for full userdata @see{metatable}. These metamethods, called @def{finalizers}, are called when the garbage collector detects that the -corresponding table or userdata is unreachable. +corresponding table or userdata is dead. Finalizers allow you to coordinate Lua's garbage collection -with external resource management -such as closing files, network or database connections, +with external resource management such as closing files, +network or database connections, or freeing your own memory. For an object (table or userdata) to be finalized when collected, you must @emph{mark} it for finalization. @index{mark (for finalization)} You mark an object for finalization when you set its metatable -and the metatable has a field indexed by the string @St{__gc}. +and the metatable has a @idx{__gc} metamethod. Note that if you set a metatable without a @idx{__gc} field and later create that field in the metatable, the object will not be marked for finalization. -When a marked object becomes garbage, +When a marked object becomes dead, it is not collected immediately by the garbage collector. Instead, Lua puts it in a list. After the collection, @@ -693,7 +849,7 @@ If it is present, Lua calls it with the object as its single argument. At the end of each garbage-collection cycle, -the finalizers for objects are called in +the finalizers are called in the reverse order that the objects were marked for finalization, among those collected in that cycle; that is, the first finalizer to be called is the one associated @@ -711,10 +867,10 @@ However, if the finalizer stores the object in some global place then the resurrection is permanent. Moreover, if the finalizer marks a finalizing object for finalization again, its finalizer will be called again in the next cycle where the -object is unreachable. +object is dead. In any case, the object memory is freed only in a GC cycle where -the object is unreachable and not marked for finalization. +the object is dead and not marked for finalization. When you close a state @seeF{lua_close}, Lua calls the finalizers of all objects marked for finalization, @@ -722,10 +878,14 @@ following the reverse order that they were marked. If any finalizer marks objects for collection during that phase, these marks have no effect. -Finalizers cannot yield. +Finalizers cannot yield nor run the garbage collector. +Because they can run in unpredictable times, +it is good practice to restrict each finalizer +to the minimum necessary to properly release +its associated resource. Any error while running a finalizer generates a warning; -it is not propagated. +the error is not propagated. } @@ -747,7 +907,7 @@ In any case, if either the key or the value is collected, the whole pair is removed from the table. The weakness of a table is controlled by the @idx{__mode} field of its metatable. -This field, if present, must be one of the following strings: +This metavalue, if present, must be one of the following strings: @St{k}, for a table with weak keys; @St{v}, for a table with weak values; or @St{kv}, for a table with both weak keys and values. @@ -773,8 +933,10 @@ are not subject to garbage collection, and therefore are not removed from weak tables (unless their associated values are collected). Although strings are subject to garbage collection, -they do not have an explicit construction, -and therefore are not removed from weak tables. +they do not have an explicit construction and +their equality is by value; +they behave more like values than like objects. +Therefore, they are not removed from weak tables. Resurrected objects (that is, objects being finalized @@ -828,7 +990,10 @@ In case of normal termination, @Lid{coroutine.resume} returns @true, plus any values returned by the coroutine main function. In case of errors, @Lid{coroutine.resume} returns @false -plus an error object. +plus the error object. +In this case, the coroutine does not unwind its stack, +so that it is possible to inspect it after the error +with the debug API. A coroutine yields by calling @Lid{coroutine.yield}. When a coroutine yields, @@ -852,8 +1017,10 @@ go as extra arguments to @Lid{coroutine.resume}. @Lid{coroutine.wrap} returns all the values returned by @Lid{coroutine.resume}, except the first one (the boolean error code). Unlike @Lid{coroutine.resume}, -@Lid{coroutine.wrap} does not catch errors; -any error is propagated to the caller. +the function created by @Lid{coroutine.wrap} +propagates any error to the caller. +In this case, +the function also closes the coroutine @seeF{coroutine.close}. As an example of how coroutines work, consider the following code: @@ -901,6 +1068,8 @@ and @Lid{lua_yield}. @C{-------------------------------------------------------------------------} @sect1{language| @title{The Language} +@simplesect{ + This section describes the lexis, the syntax, and the semantics of Lua. In other words, this section describes @@ -918,13 +1087,15 @@ and other terminal symbols are shown like @bnfter{=}. The complete syntax of Lua can be found in @refsec{BNF} at the end of this manual. +} + @sect2{lexical| @title{Lexical Conventions} Lua is a @x{free-form} language. It ignores spaces and comments between lexical elements (@x{tokens}), -except as delimiters between @x{names} and @x{keywords}. +except as delimiters between two tokens. In source code, -Lua recognizes as spaces the standard ASCII white-space +Lua recognizes as spaces the standard ASCII whitespace characters space, form feed, newline, carriage return, horizontal tab, and vertical tab. @@ -941,9 +1112,9 @@ and cannot be used as names: @index{reserved words} @verbatim{ and break do else elseif end -false for function goto if in -local nil not or repeat return -then true until while +false for function global goto if +in local nil not or repeat +return then true until while } Lua is a case-sensitive language: @@ -979,7 +1150,7 @@ and @Char{\'} (apostrophe [single quote]). A backslash followed by a line break results in a newline in the string. The escape sequence @Char{\z} skips the following span -of white-space characters, +of whitespace characters, including line breaks; it is particularly useful to break and indent a long literal string into multiple lines without adding the newlines and spaces @@ -1001,11 +1172,12 @@ it must be expressed using exactly three digits.) The @x{UTF-8} encoding of a @x{Unicode} character can be inserted in a literal string with the escape sequence @T{\u{@rep{XXX}}} -(note the mandatory enclosing brackets), +(with mandatory enclosing braces), where @rep{XXX} is a sequence of one or more hexadecimal digits representing the character code point. -This code point can be any value smaller than @M{2@sp{31}}. -(Lua uses the original UTF-8 specification here.) +This code point can be any value less than @M{2@sp{31}}. +(Lua uses the original UTF-8 specification here, +which is not restricted to valid Unicode code points.) Literal strings can also be defined using a long format enclosed by @def{long brackets}. @@ -1028,9 +1200,9 @@ Any kind of end-of-line sequence (carriage return, newline, carriage return followed by newline, or newline followed by carriage return) is converted to a simple newline. - When the opening long bracket is immediately followed by a newline, the newline is not included in the string. + As an example, in a system using ASCII (in which @Char{a} is coded @N{as 97}, newline is coded @N{as 10}, and @Char{1} is coded @N{as 49}), @@ -1052,7 +1224,7 @@ However, Lua opens files for parsing in text mode, and the system's file functions may have problems with some control characters. So, it is safer to represent -non-text data as a quoted literal with +binary data as a quoted literal with explicit escape sequences for the non-text characters. A @def{numeric constant} (or @def{numeral}) @@ -1063,7 +1235,9 @@ Lua also accepts @x{hexadecimal constants}, which start with @T{0x} or @T{0X}. Hexadecimal constants also accept an optional fractional part plus an optional binary exponent, -marked by a letter @Char{p} or @Char{P}. +marked by a letter @Char{p} or @Char{P} and written in decimal. +(For instance, @T{0x1.fp10} denotes 1984, +which is @M{0x1f / 16} multiplied by @M{2@sp{10}}.) A numeric constant with a radix point or an exponent denotes a float; @@ -1072,8 +1246,11 @@ if its value fits in an integer or it is a hexadecimal constant, it denotes an integer; otherwise (that is, a decimal integer numeral that overflows), it denotes a float. -(Hexadecimal integer numerals that overflow @emph{wrap around}; -they always denote an integer value.) +Hexadecimal numerals with neither a radix point nor an exponent +always denote an integer value; +if the value overflows, it @emph{wraps around} +to fit into a valid integer. + Examples of valid integer constants are @verbatim{ 3 345 0xff 0xBEBADA @@ -1102,17 +1279,15 @@ global variables, local variables, and table fields. A single name can denote a global variable or a local variable (or a function's formal parameter, -which is a particular kind of local variable): +which is a particular kind of local variable) @see{globalenv}: @Produc{ @producname{var}@producbody{@bnfNter{Name}} } -@bnfNter{Name} denotes identifiers, as defined in @See{lexical}. +@bnfNter{Name} denotes identifiers @see{lexical}. -Any variable name is assumed to be global unless explicitly declared -as a local @see{localvar}. -@x{Local variables} are @emph{lexically scoped}: +Because variables are @emph{lexically scoped}, local variables can be freely accessed by functions -defined inside their scope @see{visibility}. +defined inside their scope @see{globalenv}. Before the first assignment to a variable, its value is @nil. @@ -1131,19 +1306,21 @@ The syntax @id{var.Name} is just syntactic sugar for An access to a global variable @id{x} is equivalent to @id{_ENV.x}. -Due to the way that chunks are compiled, -the variable @id{_ENV} itself is never global @see{globalenv}. } @sect2{stats| @title{Statements} +@simplesect{ + Lua supports an almost conventional set of @x{statements}, -similar to those in Pascal or C. +similar to those in other conventional languages. This set includes -assignments, control structures, function calls, +blocks, assignments, control structures, function calls, and variable declarations. +} + @sect3{@title{Blocks} A @x{block} is a list of statements, @@ -1159,7 +1336,7 @@ or write two semicolons in sequence: @producname{stat}@producbody{@bnfter{;}} } -Function calls and assignments +Both function calls and assignments can start with an open parenthesis. This possibility leads to an ambiguity in Lua's grammar. Consider the following fragment: @@ -1167,7 +1344,7 @@ Consider the following fragment: a = b + c (print or io.write)('done') } -The grammar could see it in two ways: +The grammar could see this fragment in two ways: @verbatim{ a = b + c(print or io.write)('done') @@ -1212,7 +1389,7 @@ As such, chunks can define local variables, receive arguments, and return values. Moreover, such anonymous function is compiled as in the scope of an external local variable called @id{_ENV} @see{globalenv}. -The resulting function always has @id{_ENV} as its only upvalue, +The resulting function always has @id{_ENV} as its only external variable, even if it does not use that variable. A chunk can be stored in a file or in a string inside the host program. @@ -1223,9 +1400,11 @@ and then Lua executes the compiled code with an interpreter for the virtual machine. Chunks can also be precompiled into binary form; -see program @idx{luac} and function @Lid{string.dump} for details. +see the program @idx{luac} and the function @Lid{string.dump} for details. Programs in source and compiled forms are interchangeable; Lua automatically detects the file type and acts accordingly @seeF{load}. +Be aware that, unlike source code, +maliciously crafted binary chunks can crash the interpreter. } @@ -1245,18 +1424,12 @@ Expressions are discussed in @See{expressions}. Before the assignment, the list of values is @emph{adjusted} to the length of -the list of variables.@index{adjustment} -If there are more values than needed, -the excess values are thrown away. -If there are fewer values than needed, -the list is extended with as many @nil's as needed. -If the list of expressions ends with a function call, -then all values returned by that call enter the list of values, -before the adjustment -(except when the call is enclosed in parentheses; see @See{expressions}). - -The assignment statement first evaluates all its expressions -and only then the assignments are performed. +the list of variables @see{multires}. + +If a variable is both assigned and read +inside a multiple assignment, +Lua ensures that all reads get the value of the variable +before the assignment. Thus the code @verbatim{ i = 3 @@ -1276,6 +1449,12 @@ x, y, z = y, z, x } cyclically permutes the values of @id{x}, @id{y}, and @id{z}. +Note that this guarantee covers only accesses +syntactically inside the assignment statement. +If a function or a metamethod called during the assignment +changes the value of a variable, +Lua gives no guarantees about the order of that access. + An assignment to a global name @T{x = val} is equivalent to the assignment @T{_ENV.x = val} @see{globalenv}. @@ -1306,7 +1485,7 @@ The @x{condition expression} of a control structure can return any value. Both @false and @nil test false. All values different from @nil and @false test true. -(In particular, the number 0 and the empty string also test true). +In particular, the number 0 and the empty string also test true. In the @Rw{repeat}@En@Rw{until} loop, the inner block does not end at the @Rw{until} keyword, @@ -1327,15 +1506,12 @@ labels in Lua are considered statements too: A label is visible in the entire block where it is defined, except inside nested functions. -A goto may jump to any visible label as long as it does not -enter into the scope of a local variable. +A goto can jump to any visible label as long as it does not +enter into the scope of a variable declaration. A label should not be declared -where a label with the same name is visible, +where a previous label with the same name is visible, even if this other label has been declared in an enclosing block. -Labels and empty statements are called @def{void statements}, -as they perform no actions. - The @Rw{break} statement terminates the execution of a @Rw{while}, @Rw{repeat}, or @Rw{for} loop, skipping to the next statement after the loop: @@ -1347,7 +1523,7 @@ A @Rw{break} ends the innermost enclosing loop. The @Rw{return} statement is used to return values from a function or a chunk -(which is an anonymous function). +(which is handled as an anonymous function). @index{return statement} Functions can return more than one value, so the syntax for the @Rw{return} statement is @@ -1357,7 +1533,7 @@ so the syntax for the @Rw{return} statement is The @Rw{return} statement can only be written as the last statement of a block. -If it is really necessary to @Rw{return} in the middle of a block, +If it is necessary to @Rw{return} in the middle of a block, then an explicit inner block can be used, as in the idiom @T{do return end}, because now @Rw{return} is the last statement in its (inner) block. @@ -1370,73 +1546,53 @@ because now @Rw{return} is the last statement in its (inner) block. The @Rw{for} statement has two forms: one numerical and one generic. +@sect4{@title{The numerical @Rw{for} loop} + The numerical @Rw{for} loop repeats a block of code while a -control variable runs through an arithmetic progression. +control variable goes through an arithmetic progression. It has the following syntax: @Produc{ @producname{stat}@producbody{@Rw{for} @bnfNter{Name} @bnfter{=} exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} @Rw{do} block @Rw{end}} } -The @emph{block} is repeated for @emph{name} starting at the value of -the first @emph{exp}, until it passes the second @emph{exp} by steps of the -third @emph{exp}. -More precisely, a @Rw{for} statement like -@verbatim{ -for v = @rep{e1}, @rep{e2}, @rep{e3} do @rep{block} end -} -is equivalent to the code: -@verbatim{ -do - local @rep{var}, @rep{limit}, @rep{step} = tonumber(@rep{e1}), tonumber(@rep{e2}), tonumber(@rep{e3}) - if not (@rep{var} and @rep{limit} and @rep{step}) then error() end - @rep{var} = @rep{var} - @rep{step} - while true do - @rep{var} = @rep{var} + @rep{step} - if (@rep{step} >= 0 and @rep{var} > @rep{limit}) or (@rep{step} < 0 and @rep{var} < @rep{limit}) then - break - end - local v = @rep{var} - @rep{block} - end -end -} - -Note the following: -@itemize{ +The given identifier (@bnfNter{Name}) defines the control variable, +which is a new read-only (@id{const}) variable local to the loop body +(@emph{block}). -@item{ -All three control expressions are evaluated only once, -before the loop starts. -They must all result in numbers. -} +The loop starts by evaluating once the three control expressions. +Their values are called respectively +the @emph{initial value}, the @emph{limit}, and the @emph{step}. +If the step is absent, it defaults @N{to 1}. -@item{ -@T{@rep{var}}, @T{@rep{limit}}, and @T{@rep{step}} are invisible variables. -The names shown here are for explanatory purposes only. -} +If both the initial value and the step are integers, +the loop is done with integers; +note that the limit may not be an integer. +Otherwise, the three values are converted to +floats and the loop is done with floats. +Beware of floating-point accuracy in this case. -@item{ -If the third expression (the step) is absent, -then a step @N{of 1} is used. -} +After that initialization, +the loop body is repeated with the value of the control variable +going through an arithmetic progression, +starting at the initial value, +with a common difference given by the step. +A negative step makes a decreasing sequence; +a step equal to zero raises an error. +The loop continues while the value is less than +or equal to the limit +(greater than or equal to for a negative step). +If the initial value is already greater than the limit +(or less than, if the step is negative), +the body is not executed. -@item{ -You can use @Rw{break} and @Rw{goto} to exit a @Rw{for} loop. -} +For integer loops, +the control variable never wraps around; +instead, the loop ends in case of an overflow. -@item{ -The loop variable @T{v} is local to the loop body. -If you need its value after the loop, -assign it to another variable before exiting the loop. } -@item{ -The values in @rep{var}, @rep{limit}, and @rep{step} -can be integers or floats. -All operations on them respect the usual rules in Lua. -} +@sect4{@title{The generic @Rw{for} loop} -} The generic @Rw{for} statement works over functions, called @def{iterators}. @@ -1450,50 +1606,35 @@ The generic @Rw{for} loop has the following syntax: } A @Rw{for} statement like @verbatim{ -for @rep{var_1}, @Cdots, @rep{var_n} in @rep{explist} do @rep{block} end -} -is equivalent to the code: -@verbatim{ -do - local @rep{f}, @rep{s}, @rep{var} - local *toclose @rep{tbc} = nil - @rep{f}, @rep{s}, @rep{var}, @rep{tbc} = @rep{explist} - while true do - local @rep{var_1}, @Cdots, @rep{var_n} = @rep{f}(@rep{s}, @rep{var}) - if @rep{var_1} == nil then break end - @rep{var} = @rep{var_1} - @rep{block} - end -end +for @rep{var_1}, @Cdots, @rep{var_n} in @rep{explist} do @rep{body} end } -Note the following: -@itemize{ +works as follows. -@item{ -@T{@rep{explist}} is evaluated only once. -Its results are an @emph{iterator} function, +The names @rep{var_i} declare loop variables local to the loop body. +The first of these variables is the @emph{control variable}, +which is a read-only (@id{const}) variable. + +The loop starts by evaluating @rep{explist} +to produce four values: +an @emph{iterator function}, a @emph{state}, -an initial value for the first @emph{iterator variable}, -and a to-be-closed variable @see{to-be-closed}, +an initial value for the control variable, +and a @emph{closing value}. + +Then, at each iteration, +Lua calls the iterator function with two arguments: +the state and the control variable. +The results from this call are then assigned to the loop variables, +following the rules of multiple assignments @see{assignment}. +If the control variable becomes @nil, +the loop terminates. +Otherwise, the body is executed and the loop goes +to the next iteration. + +The closing value behaves like a +to-be-closed variable @see{to-be-closed}, which can be used to release resources when the loop ends. -} - -@item{ -@T{@rep{f}}, @T{@rep{s}}, @T{@rep{var}}, and @T{@rep{tbc}} -are invisible variables. -The names are here for explanatory purposes only. -} - -@item{ -You can use @Rw{break} to exit a @Rw{for} loop. -} - -@item{ -The loop variables @T{@rep{var_i}} are local to the loop; -you cannot use their values after the @Rw{for} ends. -If you need these values, -then assign them to other variables before breaking or exiting the loop. -} +Otherwise, it does not interfere with the loop. } @@ -1510,32 +1651,97 @@ Function calls are explained in @See{functioncall}. } -@sect3{localvar| @title{Local Declarations} -@x{Local variables} can be declared anywhere inside a block. -The declaration can include an initial assignment: +@sect3{localvar| @title{Variable Declarations} +Local and global variables can be declared anywhere inside a block. +The declaration can include an initialization: @Produc{ -@producname{stat}@producbody{@Rw{local} namelist @bnfopt{@bnfter{=} explist}} -} -If present, an initial assignment has the same semantics +@producname{stat}@producbody{@Rw{local} + attnamelist @bnfopt{@bnfter{=} explist}} +@producname{stat}@producbody{@Rw{global} + attnamelist @bnfopt{@bnfter{=} explist}} +} +If there is no initialization, +local variables are initialized with @nil; +global variables are left unchanged. +Otherwise, the initialization gets the same adjustment of a multiple assignment @see{assignment}. -Otherwise, all variables are initialized with @nil. +Moreover, for global variables, +the initialization will raise a runtime error +if the variable is already defined, +that is, it has a non-nil value. + +The list of names may be prefixed by an attribute +(a name between angle brackets) +and each variable name may be postfixed by an attribute: +@Produc{ +@producname{attnamelist}@producbody{ + @bnfopt{attrib} @bnfNter{Name} @bnfopt{attrib} + @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}} +@producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}} +} +A prefixed attribute applies to all names in the list; +a postfixed attribute applies to its particular name. +There are two possible attributes: +@id{const}, which declares a @emph{constant} or @emph{read-only} variable, +@index{constant variable} +that is, a variable that cannot be used as the left-hand side of an +assignment, +and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. +Only local variables can have the @id{close} attribute. +A list of variables can contain at most one to-be-closed variable. + +Lua offers also a collective declaration for global variables: +@Produc{ +@producname{stat}@producbody{@Rw{global} @bnfopt{attrib} @bnfter{*}} +} +This special form implicitly declares +as globals all names not explicitly declared previously. +In particular, +@T{global *} implicitly declares +as read-only globals all names not explicitly declared previously; +see the following example: +@verbatim{ +global X +global * +print(math.pi) -- Ok, 'print' and 'math' are read-only +X = 1 -- Ok, declared as read-write +Y = 1 -- Error, Y is read-only +} + +As noted in @See{globalenv}, +all chunks start with an implicit declaration @T{global *}, +but this preambular declaration becomes void inside +the scope of any other @Rw{global} declaration. +Therefore, a program that does not use global declarations +or start with @T{global *} +has free read-write access to any global; +a program that starts with @T{global *} +has free read-only access to any global; +and a program that starts with any other global declaration +(e.g., @T{global none}) can only refer to declared variables. + +Note that, for global variables, +the effect of any declaration is only syntactical +(except for the optional assignment): +@verbatim{ +global X , _G +X = 1 -- ERROR +_ENV.X = 1 -- Ok +_G.print(X) -- Ok +foo() -- 'foo' can freely change any global +} A chunk is also a block @see{chunks}, -and so local variables can be declared in a chunk outside any explicit block. +and so variables can be declared in a chunk outside any explicit block. -The visibility rules for local variables are explained in @See{visibility}. +The visibility rules for variable declarations +are explained in @See{globalenv}. } @sect3{to-be-closed| @title{To-be-closed Variables} -A local variable can be declared as a @def{to-be-closed} variable, -with the following syntax: -@Produc{ -@producname{stat}@producbody{ - @Rw{local} @bnfter{*} @bnfter{toclose} Name @bnfter{=} exp -}} -A to-be-closed variable behaves like a normal local variable, +A to-be-closed variable behaves like a constant local variable, except that its value is @emph{closed} whenever the variable goes out of scope, including normal block termination, exiting its block by @Rw{break}/@Rw{goto}/@Rw{return}, @@ -1543,27 +1749,42 @@ or exiting by an error. Here, to @emph{close} a value means to call its @idx{__close} metamethod. -If the value is @nil, it is ignored; -otherwise, -if it does not have a @idx{__close} metamethod, -an error is raised. When calling the metamethod, -the value itself is passed as the first argument -and the error object (if any) is passed as a second argument; -if there was no error, the second argument is @nil. +the value itself is passed as the first argument. +If there was an error, +the error object that caused the exit +is passed as a second argument; +otherwise, there is no second argument. + +The value assigned to a to-be-closed variable +must have a @idx{__close} metamethod +or be a false value. +(@nil and @false are ignored as to-be-closed values.) If several to-be-closed variables go out of scope at the same event, they are closed in the reverse order that they were declared. + If there is any error while running a closing method, that error is handled like an error in the regular code -where the variable was defined; -in particular, +where the variable was defined. +After an error, the other pending closing methods will still be called. -If a coroutine yields inside a block and is never resumed again, -the variables visible at that block will never go out of scope, -and therefore they will not be closed. -(You should use finalizers to handle this case.) +If a coroutine yields and is never resumed again, +some variables may never go out of scope, +and therefore they will never be closed. +(These variables are the ones created inside the coroutine +and in scope at the point where the coroutine yielded.) +Similarly, if a coroutine ends with an error, +it does not unwind its stack, +so it does not close any variable. +In both cases, +you can either use finalizers +or call @Lid{coroutine.close} to close the variables. +However, if the coroutine was created +through @Lid{coroutine.wrap}, +then its corresponding function will close the coroutine +in case of errors. } @@ -1571,6 +1792,8 @@ and therefore they will not be closed. @sect2{expressions| @title{Expressions} +@simplesect{ + The basic expressions in Lua are the following: @Produc{ @producname{exp}@producbody{prefixexp} @@ -1593,9 +1816,10 @@ function calls are explained in @See{functioncall}; table constructors are explained in @See{tableconstructor}. Vararg expressions, denoted by three dots (@Char{...}), can only be used when -directly inside a vararg function; +directly inside a variadic function; they are explained in @See{func-def}. + Binary operators comprise arithmetic operators @see{arith}, bitwise operators @see{bitwise}, relational operators @see{rel-ops}, logical operators @see{logic}, @@ -1605,46 +1829,8 @@ the unary bitwise NOT @see{bitwise}, the unary logical @Rw{not} @see{logic}, and the unary @def{length operator} @see{len-op}. -Both function calls and vararg expressions can result in multiple values. -If a function call is used as a statement @see{funcstat}, -then its return list is adjusted to zero elements, -thus discarding all returned values. -If an expression is used as the last (or the only) element -of a list of expressions, -then no adjustment is made -(unless the expression is enclosed in parentheses). -In all other contexts, -Lua adjusts the result list to one element, -either discarding all values except the first one -or adding a single @nil if there are no values. - -Here are some examples: -@verbatim{ -f() -- adjusted to 0 results -g(f(), x) -- f() is adjusted to 1 result -g(x, f()) -- g gets x plus all results from f() -a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) -a,b = ... -- a gets the first vararg argument, b gets - -- the second (both a and b can get nil if there - -- is no corresponding vararg argument) - -a,b,c = x, f() -- f() is adjusted to 2 results -a,b,c = f() -- f() is adjusted to 3 results -return f() -- returns all results from f() -return ... -- returns all received vararg arguments -return x,y,f() -- returns x, y, and all results from f() -{f()} -- creates a list with all results from f() -{...} -- creates a list with all vararg arguments -{f(), nil} -- f() is adjusted to 1 result } -Any expression enclosed in parentheses always results in only one value. -Thus, -@T{(f(x,y,z))} is always a single value, -even if @id{f} returns several values. -(The value of @T{(f(x,y,z))} is the first value returned by @id{f} -or @nil if @id{f} does not return any values.) - @sect3{arith| @title{Arithmetic Operators} @@ -1681,18 +1867,13 @@ so that it works for non-integer exponents too. Floor division (@T{//}) is a division that rounds the quotient towards minus infinity, -that is, the floor of the division of its operands. +resulting in the floor of the division of its operands. Modulo is defined as the remainder of a division that rounds the quotient towards minus infinity (floor division). In case of overflows in integer arithmetic, -all operations @emphx{wrap around}, -according to the usual rules of two-complement arithmetic. -(In other words, -they return the unique representable integer -that is equal modulo @M{2@sp{n}} to the mathematical result, -where @M{n} is the number of bits of the integer type.) +all operations @emphx{wrap around}. } @sect3{bitwise| @title{Bitwise Operators} @@ -1747,21 +1928,40 @@ it is in the range of integer representation). If it does, that representation is the result. Otherwise, the conversion fails. -The string library uses metamethods that try to coerce +Several places in Lua coerce strings to numbers when necessary. +In particular, +the string library sets metamethods that try to coerce strings to numbers in all arithmetic operations. -Any string operator is converted to an integer or a float, +If the conversion fails, +the library calls the metamethod of the other operand +(if present) or it raises an error. +Note that bitwise operators do not do this coercion. + +It is always a good practice not to rely on the +implicit coercions from strings to numbers, +as they are not always applied; +in particular, @T{"1"==1} is false and @T{"1"<1} raises an error +@see{rel-ops}. +These coercions exist mainly for compatibility and may be removed +in future versions of the language. + +A string is converted to an integer or a float following its syntax and the rules of the Lua lexer. -(The string may have also leading and trailing spaces and a sign.) +The string may have also leading and trailing whitespaces and a sign. All conversions from strings to numbers accept both a dot and the current locale mark as the radix character. (The Lua lexer, however, accepts only a dot.) +If the string is not a valid numeral, +the conversion fails. +If necessary, the result of this first step is then converted +to a specific number subtype following the previous rules +for conversions between floats and integers. The conversion from numbers to strings uses a non-specified human-readable format. -For complete control over how numbers are converted to strings, -use the @id{format} function from the string library -@seeF{string.format}. +To convert numbers to strings in any specific way, +use the function @Lid{string.format}. } @@ -1780,7 +1980,7 @@ These operators always result in @false or @true. Equality (@T{==}) first compares the type of its operands. If the types are different, then the result is @false. Otherwise, the values of the operands are compared. -Strings are equal if they have the same content. +Strings are equal if they have the same byte content. Numbers are equal if they denote the same mathematical value. Tables, userdata, and threads @@ -1809,8 +2009,8 @@ The operator @T{~=} is exactly the negation of equality (@T{==}). The order operators work as follows. If both arguments are numbers, -then they are compared according to their mathematical values -(regardless of their subtypes). +then they are compared according to their mathematical values, +regardless of their subtypes. Otherwise, if both arguments are strings, then their values are compared according to the current locale. Otherwise, Lua tries to call the @idx{__lt} or the @idx{__le} @@ -1819,8 +2019,8 @@ A comparison @T{a > b} is translated to @T{b < a} and @T{a >= b} is translated to @T{b <= a}. Following the @x{IEEE 754} standard, -@x{NaN} is considered neither smaller than, -nor equal to, nor greater than any value (including itself). +the special value @x{NaN} is considered neither less than, +nor equal to, nor greater than any value, including itself. } @@ -1858,8 +2058,9 @@ false or nil --> nil @sect3{concat| @title{Concatenation} The string @x{concatenation} operator in Lua is denoted by two dots (@Char{..}). -If both operands are strings or numbers, then they are converted to -strings according to the rules described in @See{coercion}. +If both operands are strings or numbers, +then the numbers are converted to strings +in a non-specified format @see{coercion}. Otherwise, the @idx{__concat} metamethod is called @see{metatable}. } @@ -1868,33 +2069,37 @@ Otherwise, the @idx{__concat} metamethod is called @see{metatable}. The length operator is denoted by the unary prefix operator @T{#}. -The length of a string is its number of bytes -(that is, the usual meaning of string length when each -character is one byte). +The length of a string is its number of bytes. +(That is the usual meaning of string length when each +character is one byte.) The length operator applied on a table returns a @x{border} in that table. -A @def{border} in a table @id{t} is any natural number +A @def{border} in a table @id{t} is any non-negative integer that satisfies the following condition: @verbatim{ -(border == 0 or t[border] ~= nil) and t[border + 1] == nil +(border == 0 or t[border] ~= nil) and +(t[border + 1] == nil or border == math.maxinteger) } In words, -a border is any (natural) index present in the table -that is followed by an absent index -(or zero, when index 1 is absent). +a border is any positive integer index present in the table +that is followed by an absent index, +plus two limit cases: +zero, when index 1 is absent; +and the maximum value for an integer, when that index is present. +Note that keys that are not positive integers +do not interfere with borders. A table with exactly one border is called a @def{sequence}. -For instance, the table @T{{10, 20, 30, 40, 50}} is a sequence, +For instance, the table @T{{10,20,30,40,50}} is a sequence, as it has only one border (5). -The table @T{{10, 20, 30, nil, 50}} has two borders (3 and 5), +The table @T{{10,20,30,nil,50}} has two borders (3 and 5), and therefore it is not a sequence. -The table @T{{nil, 20, 30, nil, nil, 60, nil}} +(The @nil at index 4 is called a @emphx{hole}.) +The table @T{{nil,20,30,nil,nil,60,nil}} has three borders (0, 3, and 6), so it is not a sequence, too. The table @T{{}} is a sequence with border 0. -Note that non-natural keys do not interfere -with whether a table is a sequence. When @id{t} is a sequence, @T{#t} returns its only border, @@ -1908,7 +2113,7 @@ the memory addresses of its non-numeric keys.) The computation of the length of a table has a guaranteed worst time of @M{O(log n)}, -where @M{n} is the largest natural key in the table. +where @M{n} is the largest integer key in the table. A program can modify the behavior of the length operator for any value but strings through the @idx{__len} metamethod @see{metatable}. @@ -1958,10 +2163,10 @@ Each field of the form @T{[exp1] = exp2} adds to the new table an entry with key @id{exp1} and value @id{exp2}. A field of the form @T{name = exp} is equivalent to @T{["name"] = exp}. -Finally, fields of the form @id{exp} are equivalent to +Fields of the form @id{exp} are equivalent to @T{[i] = exp}, where @id{i} are consecutive integers -starting with 1. -Fields in the other formats do not affect this counting. +starting with 1; +fields in the other formats do not affect this counting. For example, @verbatim{ a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } @@ -1985,9 +2190,9 @@ The order of the assignments in a constructor is undefined. (This order would be relevant only when there are repeated keys.) If the last field in the list has the form @id{exp} -and the expression is a function call or a vararg expression, +and the expression is a multires expression, then all values returned by this expression enter the list consecutively -@see{functioncall}. +@see{multires}. The field list can have an optional trailing separator, as a convenience for machine-generated code. @@ -2004,8 +2209,9 @@ first @bnfNter{prefixexp} and @bnfNter{args} are evaluated. If the value of @bnfNter{prefixexp} has type @emph{function}, then this function is called with the given arguments. -Otherwise, the @bnfNter{prefixexp} @idx{__call} metamethod is called, -having as first argument the value of @bnfNter{prefixexp}, +Otherwise, if present, +the @bnfNter{prefixexp} @idx{__call} metamethod is called: +its first argument is the value of @bnfNter{prefixexp}, followed by the original call arguments @see{metatable}. @@ -2013,9 +2219,9 @@ The form @Produc{ @producname{functioncall}@producbody{prefixexp @bnfter{:} @bnfNter{Name} args} } -can be used to call @Q{methods}. +can be used to emulate methods. A call @T{v:name(@rep{args})} -is syntactic sugar for @T{v.name(v,@rep{args})}, +is syntactic sugar for @T{v.name(v, @rep{args})}, except that @id{v} is evaluated only once. Arguments have the following syntax: @@ -2036,8 +2242,8 @@ that is, the argument list is a single literal string. A call of the form @T{return @rep{functioncall}} not in the scope of a to-be-closed variable is called a @def{tail call}. Lua implements @def{proper tail calls} -(or @emph{proper tail recursion}): -in a tail call, +(or @def{proper tail recursion}): +In a tail call, the called function reuses the stack entry of the calling function. Therefore, there is no limit on the number of nested tail calls that a program can execute. @@ -2062,7 +2268,7 @@ return x or f(x) -- results adjusted to 1 @sect3{func-def| @title{Function Definitions} -The syntax for function definition is +The syntax for a function definition is @Produc{ @producname{functiondef}@producbody{@Rw{function} funcbody} @producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}} @@ -2072,6 +2278,7 @@ The following syntactic sugar simplifies function definitions: @Produc{ @producname{stat}@producbody{@Rw{function} funcname funcbody} @producname{stat}@producbody{@Rw{local} @Rw{function} @bnfNter{Name} funcbody} +@producname{stat}@producbody{@Rw{global} @Rw{function} @bnfNter{Name} funcbody} @producname{funcname}@producbody{@bnfNter{Name} @bnfrep{@bnfter{.} @bnfNter{Name}} @bnfopt{@bnfter{:} @bnfNter{Name}}} } The statement @@ -2090,6 +2297,7 @@ translates to @verbatim{ t.a.b.c.f = function () @rep{body} end } + The statement @verbatim{ local function f () @rep{body} end @@ -2103,42 +2311,71 @@ not to local f = function () @rep{body} end } (This only makes a difference when the body of the function -contains references to @id{f}.) +contains recursive references to @id{f}.) +Similarly, the statement +@verbatim{ +global function f () @rep{body} end +} +translates to +@verbatim{ +global f; global f = function () @rep{body} end +} +The second @Rw{global} makes the assignment an initialization, +which will raise an error if that global is already defined. + +The @emphx{colon} syntax +is used to emulate @def{methods}, +adding an implicit extra parameter @idx{self} to the function. +Thus, the statement +@verbatim{ +function t.a.b.c:f (@rep{params}) @rep{body} end +} +is syntactic sugar for +@verbatim{ +t.a.b.c.f = function (self, @rep{params}) @rep{body} end +} A function definition is an executable expression, whose value has type @emph{function}. When Lua precompiles a chunk, -all its function bodies are precompiled too. +all its function bodies are precompiled too, +but they are not created yet. Then, whenever Lua executes the function definition, the function is @emph{instantiated} (or @emph{closed}). -This function instance (or @emphx{closure}) +This function instance, or @emphx{closure}, is the final value of the expression. +Results are returned using the @Rw{return} statement @see{control}. +If control reaches the end of a function +without encountering a @Rw{return} statement, +then the function returns with no results. + +@index{multiple return} +There is a system-dependent limit on the number of values +that a function may return. +This limit is guaranteed to be at least 1000. + +@sect4{@title{Parameters} + Parameters act as local variables that are initialized with the argument values: @Produc{ -@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} @bnfter{...}} @Or - @bnfter{...}} +@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or + varargparam} +@producname{varargparam}@producbody{@bnfter{...} @bnfopt{@bnfNter{Name}}} } When a Lua function is called, it adjusts its list of @x{arguments} to -the length of its list of parameters, -unless the function is a @def{vararg function}, +the length of its list of parameters @see{multires}, +unless the function is a @def{variadic function}, which is indicated by three dots (@Char{...}) at the end of its parameter list. -A vararg function does not adjust its argument list; +A variadic function does not adjust its argument list; instead, it collects all extra arguments and supplies them -to the function through a @def{vararg expression}, -which is also written as three dots. -The value of this expression is a list of all actual extra arguments, -similar to a function with multiple results. -If a vararg expression is used inside another expression -or in the middle of a list of expressions, -then its return list is adjusted to one element. -If the expression is used as the last element of a list of expressions, -then no adjustment is made -(unless that last expression is enclosed in parentheses). - +to the function through a @def{vararg table}. +In that table, +the values at indices 1, 2, etc. are the extra arguments, +and the value at index @St{n} is the number of extra arguments. As an example, consider the following definitions: @verbatim{ @@ -2147,9 +2384,9 @@ function g(a, b, ...) end function r() return 1,2,3 end } Then, we have the following mapping from arguments to parameters and -to the vararg expression: +to the vararg table: @verbatim{ -CALL PARAMETERS +CALL PARAMETERS f(3) a=3, b=nil f(3, 4) a=3, b=4 @@ -2157,96 +2394,155 @@ f(3, 4, 5) a=3, b=4 f(r(), 10) a=1, b=10 f(r()) a=1, b=2 -g(3) a=3, b=nil, ... --> (nothing) -g(3, 4) a=3, b=4, ... --> (nothing) -g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 -g(5, r()) a=5, b=1, ... --> 2 3 +g(3) a=3, b=nil, va. table -> {n = 0} +g(3, 4) a=3, b=4, va. table -> {n = 0} +g(3, 4, 5, 8) a=3, b=4, va. table -> {5, 8, n = 2} +g(5, r()) a=5, b=1, va. table -> {2, 3, n = 2} } -Results are returned using the @Rw{return} statement @see{control}. -If control reaches the end of a function -without encountering a @Rw{return} statement, -then the function returns with no results. +A vararg table in a variadic function can have an optional name, +given after the three dots. +When present, +that name denotes a read-only local variable that +refers to the vararg table. +If the vararg table does not have a name, +it can only be accessed through a vararg expression. -@index{multiple return} -There is a system-dependent limit on the number of values -that a function may return. -This limit is guaranteed to be larger than 1000. +A vararg expression is also written as three dots, +and its value is a list of the values in the vararg table, +from 1 to the integer value at index @St{n}. +(Therefore, if the code does not modify the vararg table, +this list corresponds to the extra arguments in the function call.) +This list behaves like the results from a +function with multiple results @see{multires}. -The @emphx{colon} syntax -is used for defining @def{methods}, -that is, functions that have an implicit extra parameter @idx{self}. -Thus, the statement -@verbatim{ -function t.a.b.c:f (@rep{params}) @rep{body} end -} -is syntactic sugar for -@verbatim{ -t.a.b.c.f = function (self, @rep{params}) @rep{body} end -} +As an optimization, +if the vararg table satisfies some conditions, +the code does not create an actual table and instead translates +the indexing expressions and the vararg expressions +into accesses to the internal vararg data. +The conditions are as follows: +If the vararg table has a name, +that name is not an upvalue in a nested function +and it is used only as the base table +in the syntactic constructions @T{t[exp]} or @T{t.id}. +Note that an anonymous vararg table always satisfy these conditions. } } -@sect2{visibility| @title{Visibility Rules} +@sect3{multires| @title{Lists of Expressions, Multiple Results, and Adjustment} -@index{visibility} -Lua is a lexically scoped language. -The scope of a local variable begins at the first statement after -its declaration and lasts until the last non-void statement -of the innermost block that includes the declaration. -Consider the following example: -@verbatim{ -x = 10 -- global variable -do -- new block - local x = x -- new 'x', with value 10 - print(x) --> 10 - x = x+1 - do -- another block - local x = x+1 -- another 'x' - print(x) --> 12 - end - print(x) --> 11 -end -print(x) --> 10 (the global one) -} +Both function calls and vararg expressions can result in multiple values. +These expressions are called @def{multires expressions}. -Notice that, in a declaration like @T{local x = x}, -the new @id{x} being declared is not in scope yet, -and so the second @id{x} refers to the outside variable. +When a multires expression is used as the last element +of a list of expressions, +all results from the expression are added to the +list of values produced by the list of expressions. +Note that a single expression +in a place that expects a list of expressions +is the last expression in that (singleton) list. -Because of the @x{lexical scoping} rules, -local variables can be freely accessed by functions -defined inside their scope. -A local variable used by an inner function is called -an @def{upvalue}, or @emphx{external local variable}, -inside the inner function. +These are the places where Lua expects a list of expressions: +@description{ -Notice that each execution of a @Rw{local} statement -defines new local variables. -Consider the following example: +@item{A @rw{return} statement, +for instance @T{return e1,e2,e3} @see{control}.} + +@item{A table constructor, +for instance @T{{e1,e2,e3}} @see{tableconstructor}.} + +@item{The arguments of a function call, +for instance @T{foo(e1,e2,e3)} @see{functioncall}.} + +@item{A multiple assignment, +for instance @T{a,b,c = e1,e2,e3} @see{assignment}.} + +@item{A local or global declaration, +which is similar to a multiple assignment.} + +@item{The initial values in a generic @rw{for} loop, +for instance @T{for k in e1,e2,e3 do ... end} @see{for}.} + +} +In the last four cases, +the list of values from the list of expressions +must be @emph{adjusted} to a specific length: +the number of parameters in a call to a non-variadic function +@see{func-def}, +the number of variables in a multiple assignment or a declaration, +and exactly four values for a generic @rw{for} loop. +The @def{adjustment} follows these rules: +If there are more values than needed, +the extra values are thrown away; +if there are fewer values than needed, +the list is extended with @nil's. +When the list of expressions ends with a multires expression, +all results from that expression enter the list of values +before the adjustment. + +When a multires expression is used +in a list of expressions without being the last element, +or in a place where the syntax expects a single expression, +Lua adjusts the result list of that expression to one element. +As a particular case, +the syntax expects a single expression inside a parenthesized expression; +therefore, adding parentheses around a multires expression +forces it to produce exactly one result. + +We seldom need to use a vararg expression in a place +where the syntax expects a single expression. +(Usually it is simpler to add a regular parameter before +the variadic part and use that parameter.) +When there is such a need, +we recommend assigning the vararg expression +to a single variable and using that variable +in its place. + +Here are some examples of uses of multires expressions. +In all cases, when the construction needs +@Q{the n-th result} and there is no such result, +it uses a @nil. @verbatim{ -a = {} -local x = 20 -for i=1,10 do - local y = 0 - a[i] = function () y=y+1; return x+y end -end +print(x, f()) -- prints x and all results from f(). +print(x, (f())) -- prints x and the first result from f(). +print(f(), x) -- prints the first result from f() and x. +print(1 + f()) -- prints 1 added to the first result from f(). +local x = ... -- x gets the first vararg argument. +x,y = ... -- x gets the first vararg argument, + -- y gets the second vararg argument. +x,y,z = w, f() -- x gets w, y gets the first result from f(), + -- z gets the second result from f(). +x,y,z = f() -- x gets the first result from f(), + -- y gets the second result from f(), + -- z gets the third result from f(). +x,y,z = f(), g() -- x gets the first result from f(), + -- y gets the first result from g(), + -- z gets the second result from g(). +x,y,z = (f()) -- x gets the first result from f(), y and z get nil. +return f() -- returns all results from f(). +return x, ... -- returns x and all received vararg arguments. +return x,y,f() -- returns x, y, and all results from f(). +{f()} -- creates a list with all results from f(). +{...} -- creates a list with all vararg arguments. +{f(), 5} -- creates a list with the first result from f() and 5. } -The loop creates ten closures -(that is, ten instances of the anonymous function). -Each of these closures uses a different @id{y} variable, -while all of them share the same @id{x}. } } +} + + @C{-------------------------------------------------------------------------} @sect1{API| @title{The Application Program Interface} +@simplesect{ + @index{C API} This section describes the @N{C API} for Lua, that is, the set of @N{C functions} available to the host program to communicate @@ -2283,9 +2579,13 @@ every function in the library, except to @Lid{lua_newstate}, which creates a Lua state from scratch and returns a pointer to the @emph{main thread} in the new state. +} + @sect2{@title{The Stack} +@simplesect{ + Lua uses a @emph{virtual stack} to pass values to and from C. Each element in this stack represents a Lua value (@nil, number, string, etc.). @@ -2304,8 +2604,8 @@ For convenience, most query operations in the API do not follow a strict stack discipline. Instead, they can refer to any element in the stack by using an @emph{index}:@index{index (API stack)} -A positive index represents an absolute stack position -(starting @N{at 1}); +A positive index represents an absolute stack position, +starting @N{at 1} as the bottom of the stack; a negative index represents an offset relative to the top of the stack. More specifically, if the stack has @rep{n} elements, then @N{index 1} represents the first element @@ -2318,32 +2618,37 @@ and index @M{-n} represents the first element. } -@sect2{stacksize| @title{Stack Size} +@sect3{stacksize| @title{Stack Size} When you interact with the Lua API, you are responsible for ensuring consistency. In particular, @emph{you are responsible for controlling stack overflow}. -You can use the function @Lid{lua_checkstack} -to ensure that the stack has enough space for pushing new elements. +When you call any API function, +you must ensure the stack has enough room to accommodate the results. + +There is one exception to the above rule: +When you call a Lua function +without a fixed number of results @seeF{lua_call}, +Lua ensures that the stack has enough space for all results. +However, it does not ensure any extra space. +So, before pushing anything on the stack after such a call +you should use @Lid{lua_checkstack}. Whenever Lua calls C, it ensures that the stack has space for -at least @defid{LUA_MINSTACK} extra slots. +at least @defid{LUA_MINSTACK} extra elements; +that is, you can safely push up to @id{LUA_MINSTACK} values into it. @id{LUA_MINSTACK} is defined as 20, so that usually you do not have to worry about stack space unless your code has loops pushing elements onto the stack. - -When you call a Lua function -without a fixed number of results @seeF{lua_call}, -Lua ensures that the stack has enough space for all results, -but it does not ensure any extra space. -So, before pushing anything in the stack after such a call -you should use @Lid{lua_checkstack}. +Whenever necessary, +you can use the function @Lid{lua_checkstack} +to ensure that the stack has enough space for pushing new elements. } -@sect2{@title{Valid and Acceptable Indices} +@sect3{@title{Valid and Acceptable Indices} Any function in the API that receives stack indices works only with @emphx{valid indices} or @emphx{acceptable indices}. @@ -2367,7 +2672,7 @@ but it also can be any positive index after the stack top within the space allocated for the stack, that is, indices up to the stack size. (Note that 0 is never an acceptable index.) -Indices to upvalues @see{c-closure} larger than the real number +Indices to upvalues @see{c-closure} greater than the real number of upvalues in the current @N{C function} are also acceptable (but invalid). Except when noted otherwise, functions in the API work with acceptable indices. @@ -2375,7 +2680,7 @@ functions in the API work with acceptable indices. Acceptable indices serve to avoid extra tests against the stack top when querying the stack. For instance, a @N{C function} can query its third argument -without the need to first check whether there is a third argument, +without the need to check whether there is a third argument, that is, without the need to check whether 3 is a valid index. For functions that can be called with acceptable indices, @@ -2385,6 +2690,42 @@ which behaves like a nil value. } +@sect3{constchar|@title{Pointers to Strings} + +Several functions in the API return pointers (@T{const char*}) +to Lua strings in the stack. +(See @Lid{lua_pushfstring}, @Lid{lua_pushlstring}, +@Lid{lua_pushstring}, and @Lid{lua_tolstring}. +See also @Lid{luaL_checklstring}, @Lid{luaL_checkstring}, +and @Lid{luaL_tolstring} in the auxiliary library.) + +In general, +Lua's garbage collection can free or move memory +and then invalidate pointers to strings handled by a Lua state. +To allow a safe use of these pointers, +the API guarantees that any pointer to a string in a stack index +is valid while the string value at that index is not removed from the stack. +(It can be moved to another index, though.) +When the index is a pseudo-index (referring to an upvalue), +the pointer is valid while the corresponding call is active and +the corresponding upvalue is not modified. + +Some functions in the debug interface +also return pointers to strings, +namely @Lid{lua_getlocal}, @Lid{lua_getupvalue}, +@Lid{lua_setlocal}, and @Lid{lua_setupvalue}. +For these functions, the pointer is guaranteed to +be valid while the caller function is active and +the given closure (if one was given) is in the stack. + +Except for these guarantees, +the garbage collector is free to invalidate +any pointer to internal strings. + +} + +} + @sect2{c-closure| @title{C Closures} When a @N{C function} is created, @@ -2407,7 +2748,8 @@ current function which is one plus the maximum number of upvalues in a closure), produces an acceptable but invalid index. -A @N{C closure} can also change the values of its corresponding upvalues. +A @N{C closure} can also change the values +of its corresponding upvalues. } @@ -2416,7 +2758,7 @@ A @N{C closure} can also change the values of its corresponding upvalues. Lua provides a @def{registry}, a predefined table that can be used by any @N{C code} to store whatever Lua values it needs to store. -The registry table is always located at pseudo-index +The registry table is always accessible at pseudo-index @defid{LUA_REGISTRYINDEX}. Any @N{C library} can store data into this table, but it must take care to choose keys @@ -2430,9 +2772,10 @@ string keys starting with an underscore followed by uppercase letters are reserved for Lua. The integer keys in the registry are used -by the reference mechanism @seeC{luaL_ref} -and by some predefined values. -Therefore, integer keys must not be used for other purposes. +by the reference mechanism @seeC{luaL_ref}, +with some predefined values. +Therefore, integer keys in the registry +must not be used for other purposes. When you create a new Lua state, its registry comes with some predefined values. @@ -2454,18 +2797,21 @@ the @x{global environment}. @sect2{C-error|@title{Error Handling in C} +@simplesect{ + Internally, Lua uses the C @id{longjmp} facility to handle errors. (Lua will use exceptions if you compile it as C++; search for @id{LUAI_THROW} in the source code for details.) -When Lua faces any error -(such as a @x{memory allocation error} or a type error) +When Lua faces any error, +such as a @x{memory allocation error} or a type error, it @emph{raises} an error; that is, it does a long jump. A @emphx{protected environment} uses @id{setjmp} to set a recovery point; any error jumps to the most recent active recovery point. -Inside a @N{C function} you can raise an error by calling @Lid{lua_error}. +Inside a @N{C function} you can raise an error explicitly +by calling @Lid{lua_error}. Most functions in the API can raise an error, for instance due to a @x{memory allocation error}. @@ -2503,6 +2849,48 @@ the panic function must first check the available space @see{stacksize}. } + +@sect3{statuscodes|@title{Status Codes} + +Several functions that report errors in the API use the following +status codes to indicate different kinds of errors or other conditions: +@description{ + +@item{@defid{LUA_OK} (0)| no errors.} + +@item{@defid{LUA_ERRRUN}| a runtime error.} + +@item{@defid{LUA_ERRMEM}| +@x{memory allocation error}. +For such errors, Lua does not call the @x{message handler}. +} + +@item{@defid{LUA_ERRERR}| +stack overflow while running the @x{message handler} +due to another stack overflow. +More often than not, +this error is the result of some other error while running +a message handler. +An error in a message handler will call the handler again, +which will generate the error again, and so on, +until this loop exhausts the stack and cause this error. +} + +@item{@defid{LUA_ERRSYNTAX}| syntax error during precompilation +or format error in a binary chunk.} + +@item{@defid{LUA_YIELD}| the thread (coroutine) yields.} + +@item{@defid{LUA_ERRFILE}| a file-related error; +e.g., it cannot open or read the file.} + +} +These constants are defined in the header file @id{lua.h}. + +} + +} + @sect2{continuations|@title{Handling Yields in C} Internally, Lua uses the C @id{longjmp} facility to yield a coroutine. @@ -2510,7 +2898,7 @@ Therefore, if a @N{C function} @id{foo} calls an API function and this API function yields (directly or indirectly by calling another function that yields), Lua cannot return to @id{foo} any more, -because the @id{longjmp} removes its frame from the C stack. +because the @id{longjmp} removes its frame from the @N{C stack}. To avoid this kind of problem, Lua raises an error whenever it tries to yield across an API call, @@ -2525,16 +2913,16 @@ the @emph{original function}. This original function then calls one of those three functions in the C API, which we will call the @emph{callee function}, that then yields the current thread. -(This can happen when the callee function is @Lid{lua_yieldk}, +This can happen when the callee function is @Lid{lua_yieldk}, or when the callee function is either @Lid{lua_callk} or @Lid{lua_pcallk} -and the function called by them yields.) +and the function called by them yields. Suppose the running thread yields while executing the callee function. After the thread resumes, it eventually will finish running the callee function. However, the callee function cannot return to the original function, -because its frame in the C stack was destroyed by the yield. +because its frame in the @N{C stack} was destroyed by the yield. Instead, Lua calls a @def{continuation function}, which was given as an argument to the callee function. As the name implies, @@ -2588,11 +2976,11 @@ you can do the equivalent work directly inside the original function.) Besides the Lua state, the continuation function has two other parameters: -the final status of the call plus the context value (@id{ctx}) that +the final status of the call and the context value (@id{ctx}) that was passed originally to @Lid{lua_pcallk}. -(Lua does not use this context value; +Lua does not use this context value; it only passes this value from the original function to the -continuation function.) +continuation function. For @Lid{lua_pcallk}, the status is the same value that would be returned by @Lid{lua_pcallk}, except that it is @Lid{LUA_YIELD} when being executed after a yield @@ -2639,8 +3027,8 @@ A field in the form @T{x|y} means the function can push (or pop) depending on the situation; an interrogation mark @Char{?} means that we cannot know how many elements the function pops/pushes -by looking only at its arguments -(e.g., they may depend on what is on the stack). +by looking only at its arguments. +(For instance, they may depend on what is in the stack.) The third field, @T{x}, tells whether the function may raise errors: @Char{-} means the function never raises any error; @@ -2656,7 +3044,7 @@ and therefore may raise any errors. Converts the @x{acceptable index} @id{idx} into an equivalent @x{absolute index} -(that is, one that does not depend on the stack top). +(that is, one that does not depend on the stack size). } @@ -2667,7 +3055,7 @@ typedef void * (*lua_Alloc) (void *ud, size_t osize, size_t nsize);| -The type of the @x{memory-allocation function} used by Lua states. +The type of the @x{memory-allocator function} used by Lua states. The allocator function must provide a functionality similar to @id{realloc}, but not exactly the same. @@ -2695,18 +3083,19 @@ Lua assumes the following behavior from the allocator function: When @id{nsize} is zero, the allocator must behave like @id{free} -and return @id{NULL}. +and then return @id{NULL}. When @id{nsize} is not zero, the allocator must behave like @id{realloc}. -The allocator returns @id{NULL} +In particular, the allocator returns @id{NULL} if and only if it cannot fulfill the request. -Here is a simple implementation for the @x{allocator function}. -It is used in the auxiliary library by @Lid{luaL_newstate}. +Here is a simple implementation for the @x{allocator function}, +corresponding to the function @Lid{luaL_alloc} from the +auxiliary library. @verbatim{ -static void *l_alloc (void *ud, void *ptr, size_t osize, - size_t nsize) { +void *luaL_alloc (void *ud, void *ptr, size_t osize, + size_t nsize) { (void)ud; (void)osize; /* not used */ if (nsize == 0) { free(ptr); @@ -2716,7 +3105,7 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, return realloc(ptr, nsize); } } -Note that @N{Standard C} ensures +Note that @N{ISO C} ensures that @T{free(NULL)} has no effect and that @T{realloc(NULL,size)} is equivalent to @T{malloc(size)}. @@ -2766,30 +3155,38 @@ Sets a new panic function and returns the old one @see{C-error}. @apii{nargs+1,nresults,e} Calls a function. +Like regular Lua calls, +@id{lua_call} respects the @idx{__call} metamethod. +So, here the word @Q{function} +means any callable value. To do a call you must use the following protocol: -first, the value to be called is pushed onto the stack; +first, the function to be called is pushed onto the stack; then, the arguments to the call are pushed in direct order; that is, the first argument is pushed first. Finally you call @Lid{lua_call}; @id{nargs} is the number of arguments that you pushed onto the stack. -All arguments and the function value are popped from the stack -when the function is called. -The function results are pushed onto the stack when the function returns. +When the function returns, +all arguments and the function value are popped +and the call results are pushed onto the stack. The number of results is adjusted to @id{nresults}, -unless @id{nresults} is @defid{LUA_MULTRET}. -In this case, all results from the function are pushed; +unless @id{nresults} is @defid{LUA_MULTRET}, +which makes all results from the function to be pushed. +In the first case, an explicit number of results, +the caller must ensure that the stack has space for the +returned values. +In the second case, all results, Lua takes care that the returned values fit into the stack space, but it does not ensure any extra space in the stack. The function results are pushed onto the stack in direct order (the first result is pushed first), so that after the call the last result is on the top of the stack. +The maximum value for @id{nresults} is 250. + Any error while calling and running the function is propagated upwards (with a @id{longjmp}). -Like regular Lua calls, -this function respects the @idx{__call} metamethod. The following example shows how the host program can do the equivalent to this Lua code: @@ -2841,7 +3238,7 @@ The first argument (if any) is at index 1 and its last argument is at index @T{lua_gettop(L)}. To return values to Lua, a @N{C function} just pushes them onto the stack, in direct order (the first result is pushed first), -and returns the number of results. +and returns in C the number of results. Any other value in the stack below the results will be properly discarded by Lua. Like a Lua function, a @N{C function} called by Lua can also return @@ -2875,15 +3272,15 @@ static int foo (lua_State *L) { @APIEntry{int lua_checkstack (lua_State *L, int n);| @apii{0,0,-} -Ensures that the stack has space for at least @id{n} extra slots -(that is, that you can safely push up to @id{n} values into it). +Ensures that the stack has space for at least @id{n} extra elements, +that is, that you can safely push up to @id{n} values into it. It returns false if it cannot fulfill the request, either because it would cause the stack -to be larger than a fixed maximum size +to be greater than a fixed maximum size (typically at least several thousand elements) or because it cannot allocate memory for the extra space. This function never shrinks the stack; -if the stack already has space for the extra slots, +if the stack already has space for the extra elements, it is left unchanged. } @@ -2891,9 +3288,11 @@ it is left unchanged. @APIEntry{void lua_close (lua_State *L);| @apii{0,0,-} -Destroys all objects in the given Lua state -(calling the corresponding garbage-collection metamethods, if any) +Close all active to-be-closed variables in the main thread, +release all objects in the given Lua state +(calling the corresponding garbage-collection metamethods, if any), and frees all dynamic memory used by this state. + On several platforms, you may not need to call this function, because all resources are naturally released when the host program ends. On the other hand, long-running programs that create multiple states, @@ -2902,6 +3301,45 @@ will probably need to close states as soon as they are not needed. } +@APIEntry{void lua_closeslot (lua_State *L, int index);| +@apii{0,0,e} + +Close the to-be-closed slot at the given index and set its value to @nil. +The index must be the last index previously marked to be closed +@see{lua_toclose} that is still active (that is, not closed yet). + +A @idx{__close} metamethod cannot yield +when called through this function. + +} + +@APIEntry{int lua_closethread (lua_State *L, lua_State *from);| +@apii{0,?,-} + +Resets a thread, cleaning its call stack and closing all pending +to-be-closed variables. +The parameter @id{from} represents the coroutine that is resetting @id{L}. +If there is no such coroutine, +this parameter can be @id{NULL}. + +Unless @id{L} is equal to @id{from}, +the call returns a status code: +@Lid{LUA_OK} for no errors in the thread +(either the original error that stopped the thread or +errors in closing methods), +or an error status otherwise. +In case of error, +the error object is put on the top of the stack. + +If @id{L} is equal to @id{from}, +it corresponds to a thread closing itself. +In that case, +the call does not return; +instead, the resume that (re)started the thread returns. +The thread must be running inside a resume. + +} + @APIEntry{int lua_compare (lua_State *L, int index1, int index2, int op);| @apii{0,0,e} @@ -2947,18 +3385,18 @@ Values at other positions are not affected. } -@APIEntry{void lua_createtable (lua_State *L, int narr, int nrec);| +@APIEntry{void lua_createtable (lua_State *L, int nseq, int nrec);| @apii{0,1,m} Creates a new empty table and pushes it onto the stack. -Parameter @id{narr} is a hint for how many elements the table +Parameter @id{nseq} is a hint for how many elements the table will have as a sequence; parameter @id{nrec} is a hint for how many other elements the table will have. Lua may use these hints to preallocate memory for the new table. -This preallocation is useful for performance when you know in advance +This preallocation may help performance when you know in advance how many elements the table will have. -Otherwise you can use the function @Lid{lua_newtable}. +Otherwise you should use the function @Lid{lua_newtable}. } @@ -2978,6 +3416,13 @@ As it produces parts of the chunk, with the given @id{data} to write them. +The function @Lid{lua_dump} fully preserves the Lua stack +through the calls to the writer function, +except that it may push some values for internal use +before the first call, +and it restores the stack size to its original size +after the last call. + If @id{strip} is true, the binary representation may not include all debug information about the function, @@ -2987,14 +3432,12 @@ The value returned is the error code returned by the last call to the writer; @N{0 means} no errors. -This function does not pop the Lua function from the stack. - } @APIEntry{int lua_error (lua_State *L);| @apii{1,0,v} -Generates a Lua error, +Raises a Lua error, using the value on the top of the stack as the error object. This function does a long jump, and therefore never returns @@ -3002,67 +3445,84 @@ and therefore never returns } -@APIEntry{int lua_gc (lua_State *L, int what, int data);| +@APIEntry{int lua_gc (lua_State *L, int what, ...);| @apii{0,0,-} Controls the garbage collector. This function performs several tasks, -according to the value of the parameter @id{what}: +according to the value of the parameter @id{what}. +For options that need extra arguments, +they are listed after the option. @description{ -@item{@id{LUA_GCSTOP}| -stops the garbage collector. +@item{@defid{LUA_GCCOLLECT}| +Performs a full garbage-collection cycle. } -@item{@id{LUA_GCRESTART}| -restarts the garbage collector. +@item{@defid{LUA_GCSTOP}| +Stops the garbage collector. } -@item{@id{LUA_GCCOLLECT}| -performs a full garbage-collection cycle. +@item{@defid{LUA_GCRESTART}| +Restarts the garbage collector. } -@item{@id{LUA_GCCOUNT}| -returns the current amount of memory (in Kbytes) in use by Lua. +@item{@defid{LUA_GCCOUNT}| +Returns the current amount of memory (in Kbytes) in use by Lua. } -@item{@id{LUA_GCCOUNTB}| -returns the remainder of dividing the current amount of bytes of +@item{@defid{LUA_GCCOUNTB}| +Returns the remainder of dividing the current amount of bytes of memory in use by Lua by 1024. } -@item{@id{LUA_GCSTEP}| -performs an incremental step of garbage collection. +@item{@defid{LUA_GCSTEP} (size_t n)| +Performs a step of garbage collection. } -@item{@id{LUA_GCSETPAUSE}| -sets @id{data} as the new value -for the @emph{pause} of the collector @see{GC} -and returns the previous value of the pause. +@item{@defid{LUA_GCISRUNNING}| +Returns a boolean that tells whether the collector is running +(i.e., not stopped). } -@item{@id{LUA_GCSETSTEPMUL}| -sets @id{data} as the new value for the @emph{step multiplier} of -the collector @see{GC} -and returns the previous value of the step multiplier. +@item{@defid{LUA_GCINC}| +Changes the collector to incremental mode. +Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). } -@item{@id{LUA_GCISRUNNING}| -returns a boolean that tells whether the collector is running -(i.e., not stopped). +@item{@defid{LUA_GCGEN}| +Changes the collector to generational mode. +Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). } +@item{@defid{LUA_GCPARAM} (int param, int val)| +Changes and/or returns the value of a parameter of the collector. +If @id{val} is -1, the call only returns the current value. +The argument @id{param} must have one of the following values: +@description{ +@item{@defid{LUA_GCPMINORMUL}| The minor multiplier. } +@item{@defid{LUA_GCPMAJORMINOR}| The major-minor multiplier. } +@item{@defid{LUA_GCPMINORMAJOR}| The minor-major multiplier. } +@item{@defid{LUA_GCPPAUSE}| The garbage-collector pause. } +@item{@defid{LUA_GCPSTEPMUL}| The step multiplier. } +@item{@defid{LUA_GCPSTEPSIZE}| The step size. } +} } + +} + For more details about these options, see @Lid{collectgarbage}. +This function should not be called by a finalizer. + } @APIEntry{lua_Alloc lua_getallocf (lua_State *L, void **ud);| @apii{0,0,-} -Returns the @x{memory-allocation function} of a given state. +Returns the @x{memory-allocator function} of a given state. If @id{ud} is not @id{NULL}, Lua stores in @T{*ud} the opaque pointer given when the memory-allocator function was set. @@ -3180,9 +3640,9 @@ because a pseudo-index is not an actual stack position. The type of integers in Lua. By default this type is @id{long long}, -(usually a 64-bit two-complement integer), +(usually a 64-bit two's complement integer), but that can be changed to @id{long} or @id{int} -(usually a 32-bit two-complement integer). +(usually a 32-bit two's complement integer). (See @id{LUA_INT_TYPE} in @id{luaconf.h}.) Lua also defines the constants @@ -3349,19 +3809,6 @@ If there are no errors, function on top of the stack. Otherwise, it pushes an error message. -The return values of @id{lua_load} are: -@description{ - -@item{@Lid{LUA_OK}| no errors;} - -@item{@defid{LUA_ERRSYNTAX}| -syntax error during precompilation;} - -@item{@Lid{LUA_ERRMEM}| -@x{memory allocation (out-of-memory) error};} - -} - The @id{lua_load} function uses a user-supplied @id{reader} function to read the chunk @seeC{lua_Reader}. The @id{data} argument is an opaque value passed to the reader function. @@ -3374,10 +3821,33 @@ and loads it accordingly (see program @idx{luac}). The string @id{mode} works as in function @Lid{load}, with the addition that a @id{NULL} value is equivalent to the string @St{bt}. - -@id{lua_load} uses the stack internally, -so the reader function must always leave the stack -unmodified when returning. +Moreover, it may have a @Char{B} instead of a @Char{b}, +meaning a @emphx{fixed buffer} with the binary dump. + +A fixed buffer means that the address returned by the reader function +will contain the chunk until everything created by the chunk has +been collected; +therefore, Lua can avoid copying to internal structures +some parts of the chunk. +(In general, a fixed buffer would keep its contents +until the end of the program, +for instance with the chunk in ROM.) +Moreover, for a fixed buffer, +the reader function should return the entire chunk in the first read. +(As an example, @Lid{luaL_loadbufferx} does that, +which means that you can use it to load fixed buffers.) + +The function @Lid{lua_load} fully preserves the Lua stack +through the calls to the reader function, +except that it may push some values for internal use +before the first call, +and it restores the stack size to its original size plus one +(for the pushed result) after the last call. + +@id{lua_load} can return +@Lid{LUA_OK}, @Lid{LUA_ERRSYNTAX}, or @Lid{LUA_ERRMEM}. +The function may also return other values corresponding to +errors raised by the read function @see{statuscodes}. If the resulting function has upvalues, its first upvalue is set to the value of the @x{global environment} @@ -3388,17 +3858,20 @@ Other upvalues are initialized with @nil. } -@APIEntry{lua_State *lua_newstate (lua_Alloc f, void *ud);| +@APIEntry{lua_State *lua_newstate (lua_Alloc f, void *ud, + unsigned int seed);| @apii{0,0,-} -Creates a new thread running in a new, independent state. -Returns @id{NULL} if it cannot create the thread or the state +Creates a new independent state and returns its main thread. +Returns @id{NULL} if it cannot create the state (due to lack of memory). The argument @id{f} is the @x{allocator function}; -Lua does all memory allocation for this state +Lua will do all memory allocation for this state through this function @seeF{lua_Alloc}. The second argument, @id{ud}, is an opaque pointer that Lua passes to the allocator in every call. +The third argument, @id{seed}, +is a seed for the hashing of strings. } @@ -3406,7 +3879,7 @@ passes to the allocator in every call. @apii{0,1,m} Creates a new empty table and pushes it onto the stack. -It is equivalent to @T{lua_createtable(L, 0, 0)}. +It is equivalent to @T{lua_createtable(L,0,0)}. } @@ -3419,7 +3892,6 @@ The new thread returned by this function shares with the original thread its global environment, but has an independent execution stack. -There is no explicit function to close or to destroy a thread. Threads are subject to garbage collection, like any Lua object. @@ -3429,12 +3901,16 @@ like any Lua object. @apii{0,1,m} This function creates and pushes on the stack a new full userdata, -with @id{nuvalue} associated Lua values (called @id{user values}) +with @id{nuvalue} associated Lua values, called @id{user values}, plus an associated block of raw memory with @id{size} bytes. (The user values can be set and read with the functions @Lid{lua_setiuservalue} and @Lid{lua_getiuservalue}.) The function returns the address of the block of memory. +Lua ensures that this address is valid as long as +the corresponding userdata is alive @see{GC}. +Moreover, if the userdata is marked for finalization @see{finalizers}, +its address is valid at least until the call to its finalizer. } @@ -3442,12 +3918,12 @@ The function returns the address of the block of memory. @apii{1,2|0,v} Pops a key from the stack, -and pushes a key@En{}value pair from the table at the given index -(the @Q{next} pair after the given key). +and pushes a key@En{}value pair from the table at the given index, +the @Q{next} pair after the given key. If there are no more elements in the table, -then @Lid{lua_next} returns 0 (and pushes nothing). +then @Lid{lua_next} @N{returns 0} and pushes nothing. -A typical traversal looks like this: +A typical table traversal looks like this: @verbatim{ /* table is in the stack at index 't' */ lua_pushnil(L); /* first key */ @@ -3462,7 +3938,7 @@ while (lua_next(L, t) != 0) { } While traversing a table, -do not call @Lid{lua_tolstring} directly on a key, +avoid calling @Lid{lua_tolstring} directly on a key, unless you know that the key is actually a string. Recall that @Lid{lua_tolstring} may change the value at the given index; @@ -3487,20 +3963,33 @@ but that can be changed to a single float or a long double. @APIEntry{int lua_numbertointeger (lua_Number n, lua_Integer *p);| -Converts a Lua float to a Lua integer. -This macro assumes that @id{n} has an integral value. +Tries to convert a Lua float to a Lua integer; +the float @id{n} must have an integral value. If that value is within the range of Lua integers, it is converted to an integer and assigned to @T{*p}. The macro results in a boolean indicating whether the conversion was successful. (Note that this range test can be tricky to do -correctly without this macro, -due to roundings.) +correctly without this macro, due to rounding.) This macro may evaluate its arguments more than once. } +@APIEntry{unsigned lua_numbertocstring (lua_State *L, int idx, + char *buff);| +@apii{0,0,-} + +Converts the number at acceptable index @id{idx} to a string +and puts the result in @id{buff}. +The buffer must have a size of at least @defid{LUA_N2SBUFFSZ} bytes. +The conversion follows a non-specified format @see{coercion}. +The function returns the number of bytes written to the buffer +(including the final zero), +or zero if the value at @id{idx} is not a number. + +} + @APIEntry{int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);| @apii{nargs + 1,nresults|1,-} @@ -3525,7 +4014,7 @@ Otherwise, @id{msgh} is the stack index of a @emph{message handler}. (This index cannot be a pseudo-index.) In case of runtime errors, -this function will be called with the error object +this handler will be called with the error object and its return value will be the object returned on the stack by @Lid{lua_pcall}. @@ -3534,27 +4023,8 @@ information to the error object, such as a stack traceback. Such information cannot be gathered after the return of @Lid{lua_pcall}, since by then the stack has unwound. -The @Lid{lua_pcall} function returns one of the following constants -(defined in @id{lua.h}): -@description{ - -@item{@defid{LUA_OK} (0)| -success.} - -@item{@defid{LUA_ERRRUN}| -a runtime error. -} - -@item{@defid{LUA_ERRMEM}| -@x{memory allocation error}. -For such errors, Lua does not call the @x{message handler}. -} - -@item{@defid{LUA_ERRERR}| -error while running the @x{message handler}. -} - -} +The @Lid{lua_pcall} function returns one of the following status codes: +@Lid{LUA_OK}, @Lid{LUA_ERRRUN}, @Lid{LUA_ERRMEM}, or @Lid{LUA_ERRERR}. } @@ -3568,14 +4038,15 @@ int lua_pcallk (lua_State *L, @apii{nargs + 1,nresults|1,-} This function behaves exactly like @Lid{lua_pcall}, -but allows the called function to yield @see{continuations}. +except that it allows the called function to yield @see{continuations}. } @APIEntry{void lua_pop (lua_State *L, int n);| -@apii{n,0,-} +@apii{n,0,e} Pops @id{n} elements from the stack. +It is implemented as a macro over @Lid{lua_settop}. } @@ -3602,11 +4073,12 @@ and return its results @seeC{lua_CFunction}. When a @N{C function} is created, it is possible to associate some values with it, -thus creating a @x{@N{C closure}} @see{c-closure}; -these values are then accessible to the function whenever it is called. -To associate values with a @N{C function}, -first these values must be pushed onto the stack -(when there are multiple values, the first value is pushed first). +the so called upvalues; +these upvalues are then accessible to the function whenever it is called. +This association is called a @x{@N{C closure}} @see{c-closure}. +To create a @N{C closure}, +first the initial values for its upvalues must be pushed onto the stack. +(When there are multiple upvalues, the first value is pushed first.) Then @Lid{lua_pushcclosure} is called to create and push the @N{C function} onto the stack, with the argument @id{n} telling how many values will be @@ -3626,35 +4098,66 @@ In that case, it never raises a memory error. @apii{0,1,-} Pushes a @N{C function} onto the stack. +This function is equivalent to @Lid{lua_pushcclosure} with no upvalues. } +@APIEntry{const char *lua_pushexternalstring (lua_State *L, + const char *s, size_t len, lua_Alloc falloc, void *ud);| +@apii{0,1,m} + +Creates an @emphx{external string}, +that is, a string that uses memory not managed by Lua. +The pointer @id{s} points to the external buffer +holding the string content, +and @id{len} is the length of the string. +The string should have a zero at its end, +that is, the condition @T{s[len] == '\0'} should hold. +As with any string in Lua, +the length must fit in a Lua integer. + +If @id{falloc} is different from @id{NULL}, +that function will be called by Lua +when the external buffer is no longer needed. +The contents of the buffer should not change before this call. +The function will be called with the given @id{ud}, +the string @id{s} as the block, +the length plus one (to account for the ending zero) as the old size, +and 0 as the new size. + +Even when using an external buffer, +Lua still has to allocate a header for the string. +In case of a memory-allocation error, +Lua will call @id{falloc} before raising the error. + +The function returns a pointer to the string (that is, @id{s}). + +} + + @APIEntry{const char *lua_pushfstring (lua_State *L, const char *fmt, ...);| @apii{0,1,v} Pushes onto the stack a formatted string -and returns a pointer to this string. -It is similar to the @ANSI{sprintf}, -but has two important differences. -First, -you do not have to allocate space for the result; -the result is a Lua string and Lua takes care of memory allocation -(and deallocation, through garbage collection). -Second, -the conversion specifiers are quite restricted. -There are no flags, widths, or precisions. -The conversion specifiers can only be +and returns a pointer to this string @see{constchar}. +The result is a copy of @id{fmt} with +each @emph{conversion specifier} replaced by a string representation +of its respective extra argument. +A conversion specifier (and its corresponding extra argument) can be @Char{%%} (inserts the character @Char{%}), @Char{%s} (inserts a zero-terminated string, with no size restrictions), @Char{%f} (inserts a @Lid{lua_Number}), @Char{%I} (inserts a @Lid{lua_Integer}), -@Char{%p} (inserts a pointer as a hexadecimal numeral), +@Char{%p} (inserts a void pointer), @Char{%d} (inserts an @T{int}), @Char{%c} (inserts an @T{int} as a one-byte character), and -@Char{%U} (inserts a @T{long int} as a @x{UTF-8} byte sequence). +@Char{%U} (inserts an @T{unsigned long} as a @x{UTF-8} byte sequence). + +Every occurrence of @Char{%} in the string @id{fmt} +must form a valid conversion specifier. -This function may raise errors due to memory overflow -or an invalid conversion specifier. +Besides memory allocation errors, +this function may raise an error if the resulting string is too large. } @@ -3688,25 +4191,29 @@ light userdata with the same @N{C address}. } @APIEntry{const char *lua_pushliteral (lua_State *L, const char *s);| -@apii{0,1,m} +@apii{0,1,v} This macro is equivalent to @Lid{lua_pushstring}, but should be used only when @id{s} is a literal string. +(Lua may optimize this case.) } @APIEntry{const char *lua_pushlstring (lua_State *L, const char *s, size_t len);| -@apii{0,1,m} +@apii{0,1,v} Pushes the string pointed to by @id{s} with size @id{len} onto the stack. -Lua makes (or reuses) an internal copy of the given string, +Lua will make or reuse an internal copy of the given string, so the memory at @id{s} can be freed or reused immediately after the function returns. The string can contain any binary data, including @x{embedded zeros}. -Returns a pointer to the internal copy of the string. +Returns a pointer to the internal copy of the string @see{constchar}. + +Besides memory allocation errors, +this function may raise an error if the string is too large. } @@ -3729,11 +4236,11 @@ Pushes a float with value @id{n} onto the stack. Pushes the zero-terminated string pointed to by @id{s} onto the stack. -Lua makes (or reuses) an internal copy of the given string, +Lua will make or reuse an internal copy of the given string, so the memory at @id{s} can be freed or reused immediately after the function returns. -Returns a pointer to the internal copy of the string. +Returns a pointer to the internal copy of the string @see{constchar}. If @id{s} is @id{NULL}, pushes @nil and returns @id{NULL}. @@ -3759,10 +4266,14 @@ onto the stack. const char *lua_pushvfstring (lua_State *L, const char *fmt, va_list argp);| -@apii{0,1,v} +@apii{0,1,-} -Equivalent to @Lid{lua_pushfstring}, except that it receives a @id{va_list} -instead of a variable number of arguments. +Equivalent to @Lid{lua_pushfstring}, +except that it receives a @id{va_list} +instead of a variable number of arguments, +and it does not raise errors. +Instead, in case of errors it pushes the error message +and returns @id{NULL}. } @@ -3771,7 +4282,7 @@ instead of a variable number of arguments. Returns 1 if the two values in indices @id{index1} and @id{index2} are primitively equal -(that is, without calling the @idx{__eq} metamethod). +(that is, equal without calling the @idx{__eq} metamethod). Otherwise @N{returns 0}. Also @N{returns 0} if any of the indices are not valid. @@ -3782,6 +4293,7 @@ Also @N{returns 0} if any of the indices are not valid. Similar to @Lid{lua_gettable}, but does a raw access (i.e., without metamethods). +The value at @id{index} must be a table. } @@ -3791,7 +4303,7 @@ Similar to @Lid{lua_gettable}, but does a raw access Pushes onto the stack the value @T{t[n]}, where @id{t} is the table at the given index. The access is raw, -that is, it does not invoke the @idx{__index} metamethod. +that is, it does not use the @idx{__index} metavalue. Returns the type of the pushed value. @@ -3804,7 +4316,7 @@ Pushes onto the stack the value @T{t[k]}, where @id{t} is the table at the given index and @id{k} is the pointer @id{p} represented as a light userdata. The access is raw; -that is, it does not invoke the @idx{__index} metamethod. +that is, it does not use the @idx{__index} metavalue. Returns the type of the pushed value. @@ -3818,8 +4330,8 @@ for strings, this is the string length; for tables, this is the result of the length operator (@Char{#}) with no metamethods; for userdata, this is the size of the block of memory allocated -for the userdata; -for other values, it @N{is 0}. +for the userdata. +For other values, this call @N{returns 0}. } @@ -3828,6 +4340,7 @@ for other values, it @N{is 0}. Similar to @Lid{lua_settable}, but does a raw assignment (i.e., without metamethods). +The value at @id{index} must be a table. } @@ -3840,7 +4353,7 @@ and @id{v} is the value on the top of the stack. This function pops the value from the stack. The assignment is raw, -that is, it does not invoke the @idx{__newindex} metamethod. +that is, it does not use the @idx{__newindex} metavalue. } @@ -3854,7 +4367,7 @@ and @id{v} is the value on the top of the stack. This function pops the value from the stack. The assignment is raw, -that is, it does not invoke @idx{__newindex} metamethod. +that is, it does not use the @idx{__newindex} metavalue. } @@ -3864,8 +4377,8 @@ typedef const char * (*lua_Reader) (lua_State *L, size_t *size);| The reader function used by @Lid{lua_load}. -Every time it needs another piece of the chunk, -@Lid{lua_load} calls the reader, +Every time @Lid{lua_load} needs another piece of the chunk, +it calls the reader, passing along its @id{data} parameter. The reader must return a pointer to a block of memory with a new piece of the chunk @@ -3904,24 +4417,12 @@ because a pseudo-index is not an actual stack position. Moves the top element into the given valid index without shifting any element -(therefore replacing the value at that given index), -and then pops the top element. - -} - -@APIEntry{int lua_resetthread (lua_State *L);| -@apii{0,?,-} - -Resets a thread, cleaning its call stack and closing all pending -to-be-closed variables. -Returns a status code: -@Lid{LUA_OK} for no errors in closing methods, -or an error status otherwise. -In case of error, -leave the error object on the stack, +(therefore replacing the value at that given index), +and then pops the top element. } + @APIEntry{int lua_resume (lua_State *L, lua_State *from, int nargs, int *nresults);| @apii{?,?,-} @@ -3929,27 +4430,30 @@ leave the error object on the stack, Starts and resumes a coroutine in the given thread @id{L}. To start a coroutine, -you push onto the thread stack the main function plus any arguments; +you push the main function plus any arguments +onto the empty stack of the thread. then you call @Lid{lua_resume}, with @id{nargs} being the number of arguments. -This call returns when the coroutine suspends or finishes its execution. -When it returns, -@id{nresults} is updated and +The function returns when the coroutine suspends, +finishes its execution, or raises an unprotected error. +When it returns without errors, +@id{*nresults} is updated and the top of the stack contains -the @id{nresults} values passed to @Lid{lua_yield} +the @id{*nresults} values passed to @Lid{lua_yield} or returned by the body function. @Lid{lua_resume} returns @Lid{LUA_YIELD} if the coroutine yields, @Lid{LUA_OK} if the coroutine finishes its execution without errors, -or an error code in case of errors @seeC{lua_pcall}. +or an error code in case of errors @see{statuscodes}. In case of errors, -the error object is on the top of the stack. +the error object is pushed on the top of the stack. +(In that case, @id{nresults} is not updated, +as its value would have to be 1 for the sole error object.) -To resume a coroutine, -you remove all results from the last @Lid{lua_yield}, -put on its stack only the values to -be passed as results from @id{yield}, +To resume a suspended coroutine, +you remove the @id{*nresults} yielded values from its stack, +push the values to be passed as results from @id{yield}, and then call @Lid{lua_resume}. The parameter @id{from} represents the coroutine that is resuming @id{L}. @@ -4026,11 +4530,15 @@ Returns 0 if the userdata does not have that value. } -@APIEntry{void lua_setmetatable (lua_State *L, int index);| +@APIEntry{int lua_setmetatable (lua_State *L, int index);| @apii{1,0,-} -Pops a table from the stack and -sets it as the new metatable for the value at the given index. +Pops a table or @nil from the stack and +sets that value as the new metatable for the value at the given index. +(@nil means no metatable.) + +(For historical reasons, this function returns an @id{int}, +which now is always 1.) } @@ -4049,14 +4557,17 @@ for the @Q{newindex} event @see{metatable}. } @APIEntry{void lua_settop (lua_State *L, int index);| -@apii{?,?,-} +@apii{?,?,e} -Accepts any index, @N{or 0}, +Receives any acceptable stack index, @N{or 0}, and sets the stack top to this index. -If the new top is larger than the old one, +If the new top is greater than the old one, then the new elements are filled with @nil. If @id{index} @N{is 0}, then all stack elements are removed. +This function can run arbitrary code when removing an index +marked as to-be-closed from the stack. + } @APIEntry{void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud);| @@ -4088,12 +4599,12 @@ which creates a Lua state from scratch. Returns the status of the thread @id{L}. -The status can be 0 (@Lid{LUA_OK}) for a normal thread, +The status can be @Lid{LUA_OK} for a normal thread, an error code if the thread finished the execution of a @Lid{lua_resume} with an error, -or @defid{LUA_YIELD} if the thread is suspended. +or @Lid{LUA_YIELD} if the thread is suspended. -You can only call functions in threads with status @Lid{LUA_OK}. +You can call functions only in threads with status @Lid{LUA_OK}. You can resume threads with status @Lid{LUA_OK} (to start a new coroutine) or @Lid{LUA_YIELD} (to resume a coroutine). @@ -4109,7 +4620,7 @@ and returns the total size of the string, that is, its length plus one. The conversion can result in an integer or a float, according to the lexical conventions of Lua @see{lexical}. -The string may have leading and trailing spaces and a sign. +The string may have leading and trailing whitespaces and a sign. If the string is not a valid numeral, returns 0 and pushes nothing. (Note that the result can be used as a boolean, @@ -4144,24 +4655,31 @@ otherwise, returns @id{NULL}. @apii{0,0,v} Marks the given index in the stack as a -to-be-closed @Q{variable} @see{to-be-closed}. +to-be-closed slot @see{to-be-closed}. Like a to-be-closed variable in Lua, -the value at that index in the stack will be closed +the value at that slot in the stack will be closed when it goes out of scope. Here, in the context of a C function, -to go out of scope means that the running function returns (to Lua), -there is an error, -or the index is removed from the stack through -@Lid{lua_settop} or @Lid{lua_pop}. -An index marked as to-be-closed should not be removed from the stack -by any other function in the API except @Lid{lua_settop} or @Lid{lua_pop}. +to go out of scope means that the running function returns to Lua, +or there is an error, +or the slot is removed from the stack through +@Lid{lua_settop} or @Lid{lua_pop}, +or there is a call to @Lid{lua_closeslot}. +A slot marked as to-be-closed should not be removed from the stack +by any other function in the API except @Lid{lua_settop} or @Lid{lua_pop}, +unless previously deactivated by @Lid{lua_closeslot}. + +This function raises an error if the value at the given slot +neither has a @idx{__close} metamethod nor is a false value. This function should not be called for an index -that is equal to or below an already marked to-be-closed index. +that is equal to or below an active to-be-closed slot. -This function can raise an out-of-memory error. -In that case, the value in the given index is immediately closed, -as if it was already marked. +Note that, both in case of errors and of a regular return, +by the time the @idx{__close} metamethod runs, +the @N{C stack} was already unwound, +so that any automatic @N{C variable} declared in the calling function +(e.g., a buffer) will be out of scope. } @@ -4191,8 +4709,6 @@ indicates whether the operation succeeded. @apii{0,0,m} Converts the Lua value at the given index to a @N{C string}. -If @id{len} is not @id{NULL}, -it sets @T{*len} with the string length. The Lua value must be a string or a number; otherwise, the function returns @id{NULL}. If the value is a number, @@ -4201,15 +4717,19 @@ then @id{lua_tolstring} also (This change confuses @Lid{lua_next} when @id{lua_tolstring} is applied to keys during a table traversal.) -@id{lua_tolstring} returns a pointer -to a string inside the Lua state. -This string always has a zero (@Char{\0}) -after its last character (as @N{in C}), +If @id{len} is not @id{NULL}, +the function sets @T{*len} with the string length. +The returned @N{C string} always has a zero (@Char{\0}) +after its last character, but can contain other zeros in its body. -Because Lua has garbage collection, -there is no guarantee that the pointer returned by @id{lua_tolstring} -will be valid after the corresponding Lua value is removed from the stack. +The pointer returned by @id{lua_tolstring} +may be invalidated by the garbage collector if the +corresponding Lua value is removed from the stack @see{constchar}. + +This function can raise memory errors only +when converting a number to a string +(as then it may create a new string). } @@ -4272,7 +4792,7 @@ otherwise, the function returns @id{NULL}. If the value at the given index is a full userdata, returns its memory-block address. If the value is a light userdata, -returns its pointer. +returns its value (a pointer). Otherwise, returns @id{NULL}. } @@ -4281,7 +4801,7 @@ Otherwise, returns @id{NULL}. @apii{0,0,-} Returns the type of the value in the given valid index, -or @id{LUA_TNONE} for a non-valid (but acceptable) index. +or @id{LUA_TNONE} for a non-valid but acceptable index. The types returned by @Lid{lua_type} are coded by the following constants defined in @id{lua.h}: @defid{LUA_TNIL}, @@ -4338,6 +4858,8 @@ The third parameter is a boolean that indicates whether the message is to be continued by the message in the next call. +See @Lid{warn} for more details about warnings. + } @APIEntry{ @@ -4348,6 +4870,8 @@ Emits a warning with the given message. A message in a call with @id{tocont} true should be continued in another call to this function. +See @Lid{warn} for more details about warnings. + } @APIEntry{ @@ -4357,12 +4881,16 @@ typedef int (*lua_Writer) (lua_State *L, void* ud);| The type of the writer function used by @Lid{lua_dump}. -Every time it produces another piece of chunk, -@Lid{lua_dump} calls the writer, +Every time @Lid{lua_dump} produces another piece of chunk, +it calls the writer, passing along the buffer to be written (@id{p}), its size (@id{sz}), and the @id{ud} parameter supplied to @Lid{lua_dump}. +After @Lid{lua_dump} writes its last piece, +it will signal that by calling the writer function one more time, +with a @id{NULL} buffer (and size 0). + The writer returns an error code: @N{0 means} no errors; any other value means an error and stops @Lid{lua_dump} from @@ -4436,7 +4964,7 @@ of the (Lua) function that triggered the hook. This function can raise an error if it is called from a thread with a pending C call with no continuation function -(what is called a @emphx{C-call boundary}, +(what is called a @emphx{C-call boundary}), or it is called from a thread that is not running inside a resume (typically the main thread). @@ -4461,15 +4989,17 @@ typedef struct lua_Debug { const char *namewhat; /* (n) */ const char *what; /* (S) */ const char *source; /* (S) */ + size_t srclen; /* (S) */ int currentline; /* (l) */ int linedefined; /* (S) */ int lastlinedefined; /* (S) */ unsigned char nups; /* (u) number of upvalues */ unsigned char nparams; /* (u) number of parameters */ char isvararg; /* (u) */ + unsigned char extraargs; /* (t) number of extra arguments */ char istailcall; /* (t) */ - unsigned short ftransfer; /* (r) index of first value transferred */ - unsigned short ntransfer; /* (r) number of transferred values */ + int ftransfer; /* (r) index of first value transferred */ + int ntransfer; /* (r) number of transferred values */ char short_src[LUA_IDSIZE]; /* (S) */ /* private part */ @rep{other fields} @@ -4481,23 +5011,30 @@ information about a function or an activation record. @Lid{lua_getstack} fills only the private part of this structure, for later use. To fill the other fields of @Lid{lua_Debug} with useful information, -call @Lid{lua_getinfo}. +you must call @Lid{lua_getinfo} with an appropriate parameter. +(Specifically, to get a field, +you must add the letter between parentheses in the field's comment +to the parameter @id{what} of @Lid{lua_getinfo}.) The fields of @Lid{lua_Debug} have the following meaning: @description{ @item{@id{source}| -the name of the chunk that created the function. +the source of the chunk that created the function. If @T{source} starts with a @Char{@At}, it means that the function was defined in a file where the file name follows the @Char{@At}. If @T{source} starts with a @Char{=}, -the remainder of its contents describe the source in a user-dependent manner. +the remainder of its contents describes the source in a user-dependent manner. Otherwise, the function was defined in a string where @T{source} is that string. } +@item{@id{srclen}| +The length of the string @id{source}. +} + @item{@id{short_src}| a @Q{printable} version of @T{source}, to be used in error messages. } @@ -4537,8 +5074,8 @@ then @id{name} is set to @id{NULL}. @item{@id{namewhat}| explains the @T{name} field. The value of @T{namewhat} can be -@T{"global"}, @T{"local"}, @T{"method"}, -@T{"field"}, @T{"upvalue"}, or @T{""} (the empty string), +@T{"global"}, @T{"local"}, @T{"upvalue"}, +@T{"field"}, @T{""} (the empty string), plus some other options, according to how the function was called. (Lua uses the empty string when no other option seems to apply.) } @@ -4548,6 +5085,14 @@ true if this function invocation was called by a tail call. In this case, the caller of this level is not in the stack. } +@item{@id{extraargs}| +The number of extra arguments added by the call +to functions called through @idx{__call} metamethods. +(Each @idx{__call} metavalue adds a single extra argument, +the object being called, +but there may be a chain of @idx{__call} metavalues.) +} + @item{@id{nups}| the number of upvalues of the function. } @@ -4558,12 +5103,12 @@ the number of parameters of the function } @item{@id{isvararg}| -true if the function is a vararg function +true if the function is a variadic function (always true for @N{C functions}). } @item{@id{ftransfer}| -the index on the stack of the first value being @Q{transferred}, +the index in the stack of the first value being @Q{transferred}, that is, parameters in a call or return values in a return. (The other values are in consecutive indices.) Using this index, you can access and modify these values @@ -4630,10 +5175,24 @@ printf("%d\n", ar.linedefined); Each character in the string @id{what} selects some fields of the structure @id{ar} to be filled or -a value to be pushed on the stack: +a value to be pushed on the stack. +(These characters are also documented in the declaration of +the structure @Lid{lua_Debug}, +between parentheses in the comments following each field.) @description{ -@item{@Char{n}| fills in the field @id{name} and @id{namewhat}; +@item{@Char{f}| +pushes onto the stack the function that is +running at the given level; +} + +@item{@Char{l}| fills in the field @id{currentline}; +} + +@item{@Char{n}| fills in the fields @id{name} and @id{namewhat}; +} + +@item{@Char{r}| fills in the fields @id{ftransfer} and @id{ntransfer}; } @item{@Char{S}| @@ -4641,31 +5200,20 @@ fills in the fields @id{source}, @id{short_src}, @id{linedefined}, @id{lastlinedefined}, and @id{what}; } -@item{@Char{l}| fills in the field @id{currentline}; -} - -@item{@Char{t}| fills in the field @id{istailcall}; +@item{@Char{t}| fills in the fields @id{istailcall} and @id{extraargs}; } @item{@Char{u}| fills in the fields @id{nups}, @id{nparams}, and @id{isvararg}; } -@item{@Char{f}| -pushes onto the stack the function that is -running at the given level; -} - @item{@Char{L}| -pushes onto the stack a table whose indices are the -numbers of the lines that are valid on the function. -(A @emph{valid line} is a line with some associated code, -that is, a line where you can put a break point. -Non-valid lines include empty lines and comments.) - +pushes onto the stack a table whose indices are +the lines on the function with some associated code, +that is, the lines where you can put a break point. +(Lines with no code include empty lines and comments.) If this option is given together with option @Char{f}, its table is pushed after the function. - This is the only option that can raise a memory error. } @@ -4715,10 +5263,10 @@ an identification of the @emph{activation record} of the function executing at a given level. @N{Level 0} is the current running function, whereas level @M{n+1} is the function that has called level @M{n} -(except for tail calls, which do not count on the stack). -When there are no errors, @Lid{lua_getstack} returns 1; -when called with a level greater than the stack depth, -it returns 0. +(except for tail calls, which do not count in the stack). +When called with a level greater than the stack depth, +@Lid{lua_getstack} returns 0; +otherwise it returns 1. } @@ -4732,15 +5280,7 @@ and returns its name. Returns @id{NULL} (and pushes nothing) when the index @id{n} is greater than the number of upvalues. -For @N{C functions}, this function uses the empty string @T{""} -as a name for all upvalues. -(For Lua functions, -upvalues are the external local variables that the function uses, -and that are consequently included in its closure.) - -Upvalues have no particular order, -as they are active through the whole function. -They are numbered in an arbitrary order. +See @Lid{debug.getupvalue} for more information about upvalues. } @@ -4796,30 +5336,27 @@ For each event, the hook is called as explained below: @description{ @item{The call hook| is called when the interpreter calls a function. -The hook is called just after Lua enters the new function, -before the function gets its arguments. +The hook is called just after Lua enters the new function. } @item{The return hook| is called when the interpreter returns from a function. The hook is called just before Lua leaves the function. -There is no standard way to access the values -to be returned by the function. } @item{The line hook| is called when the interpreter is about to start the execution of a new line of code, or when it jumps back in the code (even to the same line). -(This event only happens while Lua is executing a Lua function.) +This event only happens while Lua is executing a Lua function. } @item{The count hook| is called after the interpreter executes every @T{count} instructions. -(This event only happens while Lua is executing a Lua function.) +This event only happens while Lua is executing a Lua function. } } -A hook is disabled by setting @id{mask} to zero. +Hooks are disabled by setting @id{mask} to zero. } @@ -4835,7 +5372,7 @@ Returns @id{NULL} (and pops nothing) when the index is greater than the number of active local variables. -Parameters @id{ar} and @id{n} are as in function @Lid{lua_getlocal}. +Parameters @id{ar} and @id{n} are as in the function @Lid{lua_getlocal}. } @@ -4850,7 +5387,8 @@ It also pops the value from the stack. Returns @id{NULL} (and pops nothing) when the index @id{n} is greater than the number of upvalues. -Parameters @id{funcindex} and @id{n} are as in function @Lid{lua_getupvalue}. +Parameters @id{funcindex} and @id{n} are as in +the function @Lid{lua_getupvalue}. } @@ -4866,7 +5404,8 @@ Lua closures that share an upvalue (that is, that access a same external local variable) will return identical ids for those upvalue indices. -Parameters @id{funcindex} and @id{n} are as in function @Lid{lua_getupvalue}, +Parameters @id{funcindex} and @id{n} are as in +the function @Lid{lua_getupvalue}, but @id{n} cannot be greater than the number of upvalues. } @@ -4887,7 +5426,9 @@ refer to the @id{n2}-th upvalue of the Lua closure at index @id{funcindex2}. @C{-------------------------------------------------------------------------} -@sect1{@title{The Auxiliary Library} +@sect1{auxlib|@title{The Auxiliary Library} + +@simplesect{ @index{lauxlib.h} The @def{auxiliary library} provides several convenient functions @@ -4898,7 +5439,7 @@ the auxiliary library provides higher-level functions for some common tasks. All functions and types from the auxiliary library -are defined in header file @id{lauxlib.h} and +are defined in the header file @id{lauxlib.h} and have a prefix @id{luaL_}. All functions in the auxiliary library are built on @@ -4923,6 +5464,9 @@ you should not use these functions for other stack values. Functions called @id{luaL_check*} always raise an error if the check is not satisfied. +} + + @sect2{@title{Functions and Types} Here we list all functions and types from the auxiliary library @@ -4937,6 +5481,17 @@ Adds the byte @id{c} to the buffer @id{B} } +@APIEntry{ +const void luaL_addgsub (luaL_Buffer *B, const char *s, + const char *p, const char *r);| +@apii{?,?,m} + +Adds a copy of the string @id{s} to the buffer @id{B} @seeC{luaL_Buffer}, +replacing any occurrence of the string @id{p} +with the string @id{r}. + +} + @APIEntry{void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);| @apii{?,?,m} @@ -4950,7 +5505,7 @@ The string can contain @x{embedded zeros}. @APIEntry{void luaL_addsize (luaL_Buffer *B, size_t n);| @apii{?,?,-} -Adds to the buffer @id{B} @seeC{luaL_Buffer} +Adds to the buffer @id{B} a string of length @id{n} previously copied to the buffer area @seeC{luaL_prepbuffer}. @@ -4966,7 +5521,7 @@ to the buffer @id{B} } @APIEntry{void luaL_addvalue (luaL_Buffer *B);| -@apii{1,?,m} +@apii{?,?,m} Adds the value on the top of the stack to the buffer @id{B} @@ -5028,7 +5583,7 @@ Its pattern of use is as follows: @item{First declare a variable @id{b} of type @Lid{luaL_Buffer}.} -@item{Then initialize it with a call @T{luaL_buffinit(L, &b)}.} +@item{Then initialize it with a call @T{luaL_buffinit(L,&b)}.} @item{ Then add string pieces to the buffer calling any of @@ -5049,14 +5604,14 @@ you can use the buffer like this: @item{First declare a variable @id{b} of type @Lid{luaL_Buffer}.} @item{Then initialize it and preallocate a space of -size @id{sz} with a call @T{luaL_buffinitsize(L, &b, sz)}.} +size @id{sz} with a call @T{luaL_buffinitsize(L,&b,sz)}.} @item{Then produce the string into that space.} @item{ -Finish by calling @T{luaL_pushresultsize(&b, sz)}, +Finish by calling @T{luaL_pushresultsize(&b,sz)}, where @id{sz} is the total size of the resulting string -copied into that space (which may be smaller than or +copied into that space (which may be less than or equal to the preallocated size). } @@ -5079,12 +5634,29 @@ plus the final string on its top. } -@APIEntry{void luaL_buffinit (lua_State *L, luaL_Buffer *B);| +@APIEntry{char *luaL_buffaddr (luaL_Buffer *B);| @apii{0,0,-} -Initializes a buffer @id{B}. +Returns the address of the current content of buffer @id{B} +@seeC{luaL_Buffer}. +Note that any addition to the buffer may invalidate this address. + +} + +@APIEntry{void luaL_buffinit (lua_State *L, luaL_Buffer *B);| +@apii{0,?,-} + +Initializes a buffer @id{B} +@seeC{luaL_Buffer}. This function does not allocate any space; -the buffer must be declared as a variable +the buffer must be declared as a variable. + +} + +@APIEntry{size_t luaL_bufflen (luaL_Buffer *B);| +@apii{0,0,-} + +Returns the length of the current content of buffer @id{B} @seeC{luaL_Buffer}. } @@ -5097,6 +5669,15 @@ Equivalent to the sequence } +@APIEntry{void luaL_buffsub (luaL_Buffer *B, int n);| +@apii{?,?,-} + +Removes @id{n} bytes from the buffer @id{B} +@seeC{luaL_Buffer}. +The buffer must have at least that many bytes. + +} + @APIEntry{int luaL_callmeta (lua_State *L, int obj, const char *e);| @apii{0,0|1,e} @@ -5108,7 +5689,7 @@ this function calls this field passing the object as its only argument. In this case this function returns true and pushes onto the stack the value returned by the call. If there is no metatable or no metamethod, -this function returns false (without pushing any value on the stack). +this function returns false without pushing any value on the stack. } @@ -5125,7 +5706,7 @@ of any type (including @nil) at position @id{arg}. Checks whether the function argument @id{arg} is an integer (or can be converted to an integer) -and returns this integer cast to a @Lid{lua_Integer}. +and returns this integer. } @@ -5134,7 +5715,7 @@ and returns this integer cast to a @Lid{lua_Integer}. Checks whether the function argument @id{arg} is a string and returns this string; -if @id{l} is not @id{NULL} fills @T{*l} +if @id{l} is not @id{NULL} fills its referent with the string's length. This function uses @Lid{lua_tolstring} to get its result, @@ -5146,7 +5727,7 @@ so all conversions and caveats of that function apply here. @apii{0,0,v} Checks whether the function argument @id{arg} is a number -and returns this number. +and returns this number converted to a @id{lua_Number}. } @@ -5228,8 +5809,9 @@ It is defined as the following macro: @verbatim{ (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0)) } -It returns false if there are no errors -or true in case of errors. +It @N{returns 0} (@Lid{LUA_OK}) if there are no errors, +or 1 in case of errors. +(Except for out-of-memory errors, which are raised.) } @@ -5241,8 +5823,8 @@ It is defined as the following macro: @verbatim{ (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0)) } -It returns false if there are no errors -or true in case of errors. +It @N{returns 0} (@Lid{LUA_OK}) if there are no errors, +or 1 in case of errors. } @@ -5322,8 +5904,8 @@ const char *luaL_gsub (lua_State *L, const char *r);| @apii{0,1,m} -Creates a copy of string @id{s} by replacing -any occurrence of the string @id{p} +Creates a copy of string @id{s}, +replacing any occurrence of the string @id{p} with the string @id{r}. Pushes the resulting string on the stack and returns it. @@ -5336,7 +5918,7 @@ Returns the @Q{length} of the value at the given index as a number; it is equivalent to the @Char{#} operator in Lua @see{len-op}. Raises an error if the result of the operation is not an integer. -(This case only can happen through metamethods.) +(This case can only happen through metamethods.) } @@ -5367,7 +5949,9 @@ buffer pointed to by @id{buff} with size @id{sz}. This function returns the same results as @Lid{lua_load}. @id{name} is the chunk name, used for debug information and error messages. -The string @id{mode} works as in function @Lid{lua_load}. +The string @id{mode} works as in the function @Lid{lua_load}. +In particular, this function supports mode @Char{B} for +fixed buffers. } @@ -5390,12 +5974,10 @@ If @id{filename} is @id{NULL}, then it loads from the standard input. The first line in the file is ignored if it starts with a @T{#}. -The string @id{mode} works as in function @Lid{lua_load}. +The string @id{mode} works as in the function @Lid{lua_load}. This function returns the same results as @Lid{lua_load}, -but it has an extra error code @defid{LUA_ERRFILE} -for file-related errors -(e.g., it cannot open or read the file). +or @Lid{LUA_ERRFILE} for file-related errors. As @Lid{lua_load}, this function only loads the chunk; it does not run it. @@ -5416,12 +5998,21 @@ it does not run it. } +@APIEntry{unsigned int luaL_makeseed (lua_State *L);| +@apii{0,0,-} + +Returns a value with a weak attempt for randomness. +The parameter @id{L} can be @id{NULL} +if there is no Lua state available. + +} + @APIEntry{void luaL_newlib (lua_State *L, const luaL_Reg l[]);| @apii{0,1,m} Creates a new table and registers there -the functions in list @id{l}. +the functions in the list @id{l}. It is implemented as the following macro: @verbatim{ @@ -5457,9 +6048,9 @@ creates a new table to be used as a metatable for userdata, adds to this new table the pair @T{__name = tname}, adds to the registry the pair @T{[tname] = new table}, and returns 1. -(The entry @idx{__name} is used by some error-reporting functions.) -In both cases pushes onto the stack the final value associated +In both cases, +the function pushes onto the stack the final value associated with @id{tname} in the registry. } @@ -5468,24 +6059,17 @@ with @id{tname} in the registry. @apii{0,0,-} Creates a new Lua state. -It calls @Lid{lua_newstate} with an -allocator based on the @N{standard C} @id{realloc} function -and then sets a panic function @see{C-error} that prints -an error message to the standard error output in case of fatal -errors. +It calls @Lid{lua_newstate} with @Lid{luaL_alloc} as +the allocator function and the result of @T{luaL_makeseed(NULL)} +as the seed, +and then sets a warning function and a panic function @see{C-error} +that print messages to the standard error output. Returns the new state, or @id{NULL} if there is a @x{memory allocation error}. } -@APIEntry{void luaL_openlibs (lua_State *L);| -@apii{0,0,e} - -Opens all standard Lua libraries into the given state. - -} - @APIEntry{ T luaL_opt (L, func, arg, dflt);| @apii{0,0,-} @@ -5510,7 +6094,7 @@ lua_Integer luaL_optinteger (lua_State *L, @apii{0,0,v} If the function argument @id{arg} is an integer -(or convertible to an integer), +(or it is convertible to an integer), returns this integer. If this argument is absent or is @nil, returns @id{d}. @@ -5532,7 +6116,7 @@ returns @id{d}. Otherwise, raises an error. If @id{l} is not @id{NULL}, -fills the position @T{*l} with the result's length. +fills its referent with the result's length. If the result is @id{NULL} (only possible when returning @id{d} and @T{d == NULL}), its length is considered zero. @@ -5546,7 +6130,7 @@ so all conversions and caveats of that function apply here. @apii{0,0,v} If the function argument @id{arg} is a number, -returns this number. +returns this number as a @id{lua_Number}. If this argument is absent or is @nil, returns @id{d}. Otherwise, raises an error. @@ -5587,6 +6171,13 @@ it to the buffer. } +@APIEntry{void luaL_pushfail (lua_State *L);| +@apii{0,1,-} + +Pushes the @fail value onto the stack @see{libraries}. + +} + @APIEntry{void luaL_pushresult (luaL_Buffer *B);| @apii{?,1,m} @@ -5609,12 +6200,22 @@ Creates and returns a @def{reference}, in the table at index @id{t}, for the object on the top of the stack (and pops the object). -A reference is a unique integer key. -As long as you do not manually add integer keys into table @id{t}, -@Lid{luaL_ref} ensures the uniqueness of the key it returns. -You can retrieve an object referred by reference @id{r} -by calling @T{lua_rawgeti(L, t, r)}. -Function @Lid{luaL_unref} frees a reference and its associated object. +The reference system uses the integer keys of the table. +A reference is a unique integer key; +@Lid{luaL_ref} ensures the uniqueness of the keys it returns. +The entry 1 is reserved for internal use. +Before the first use of @Lid{luaL_ref}, +the integer keys of the table +should form a proper sequence (no holes), +and the value at entry 1 should be false: +@nil if the sequence is empty, +@false otherwise. +You should not manually set integer keys in the table +after the first use of @Lid{luaL_ref}. + +You can retrieve an object referred by the reference @id{r} +by calling @T{lua_rawgeti(L,t,r)} or @T{lua_geti(L,t,r)}. +The function @Lid{luaL_unref} frees a reference. If the object on the top of the stack is @nil, @Lid{luaL_ref} returns the constant @defid{LUA_REFNIL}. @@ -5645,12 +6246,12 @@ void luaL_requiref (lua_State *L, const char *modname, @apii{0,1,e} If @T{package.loaded[modname]} is not true, -calls function @id{openf} with string @id{modname} as an argument +calls the function @id{openf} with the string @id{modname} as an argument and sets the call result to @T{package.loaded[modname]}, as if that function has been called through @Lid{require}. If @id{glb} is true, -also stores the module into global @id{modname}. +also stores the module into the global variable @id{modname}. Leaves a copy of the module on the stack. @@ -5670,6 +6271,9 @@ previously pushed on the stack on top of the library table. These values are popped from the stack after the registration. +A function with a @id{NULL} value represents a placeholder, +which is filled with @false. + } @APIEntry{void luaL_setmetatable (lua_State *L, const char *tname);| @@ -5681,6 +6285,15 @@ in the registry @seeC{luaL_newmetatable}. } +@APIEntry{ +void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize);| + +A standard allocator function for Lua @seeF{lua_Alloc}, +built on top of the C functions @id{realloc} and @id{free}. + +} + + @APIEntry{ typedef struct luaL_Stream { FILE *f; @@ -5688,8 +6301,8 @@ typedef struct luaL_Stream { } luaL_Stream; | -The standard representation for @x{file handles}, -which is used by the standard I/O library. +The standard representation for @x{file handles} +used by the standard I/O library. A file handle is implemented as a full userdata, with a metatable called @id{LUA_FILEHANDLE} @@ -5699,14 +6312,14 @@ The metatable is created by the I/O library This userdata must start with the structure @id{luaL_Stream}; it can contain other data after this initial structure. -Field @id{f} points to the corresponding C stream -(or it can be @id{NULL} to indicate an incompletely created handle). -Field @id{closef} points to a Lua function +The field @id{f} points to the corresponding C stream, +or it is @id{NULL} to indicate an incompletely created handle. +The field @id{closef} points to a Lua function that will be called to close the stream when the handle is closed or collected; this function receives the file handle as its sole argument and -must return either @true (in case of success) -or @nil plus an error message (in case of error). +must return either a true value, in case of success, +or a false value plus an error message, in case of error. Once Lua calls this field, it changes the field value to @id{NULL} to signal that the handle is closed. @@ -5728,7 +6341,7 @@ it returns @id{NULL} instead of raising an error. Converts any Lua value at the given index to a @N{C string} in a reasonable format. The resulting string is pushed onto the stack and also -returned by the function. +returned by the function @see{constchar}. If @id{len} is not @id{NULL}, the function also sets @T{*len} with the string length. @@ -5745,19 +6358,17 @@ void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, @apii{0,1,m} Creates and pushes a traceback of the stack @id{L1}. -If @id{msg} is not @id{NULL} it is appended +If @id{msg} is not @id{NULL}, it is appended at the beginning of the traceback. The @id{level} parameter tells at which level to start the traceback. } -@APIEntry{const char *luaL_typeerror (lua_State *L, - int arg, - const char *tname);| +@APIEntry{int luaL_typeerror (lua_State *L, int arg, const char *tname);| @apii{0,0,v} -Raises a type error for argument @id{arg} +Raises a type error for the argument @id{arg} of the @N{C function} that called it, using a standard message; @id{tname} is a @Q{name} for the expected type. @@ -5775,14 +6386,16 @@ Returns the name of the type of the value at the given index. @APIEntry{void luaL_unref (lua_State *L, int t, int ref);| @apii{0,0,-} -Releases reference @id{ref} from the table at index @id{t} -@seeC{luaL_ref}. -The entry is removed from the table, -so that the referred object can be collected. -The reference @id{ref} is also freed to be used again. - -If @id{ref} is @Lid{LUA_NOREF} or @Lid{LUA_REFNIL}, -@Lid{luaL_unref} does nothing. +Releases a reference @see{luaL_ref}. +The integer @id{ref} must be either +@Lid{LUA_NOREF}, @Lid{LUA_REFNIL}, +or a reference previously returned by @Lid{luaL_ref} +and not already released. +If @id{ref} is either @Lid{LUA_NOREF} or @Lid{LUA_REFNIL} +this function does nothing. +Otherwise, the entry is removed from the table, +so that the referred object can be collected and +the reference @id{ref} can be used again by @Lid{luaL_ref}. } @@ -5809,15 +6422,17 @@ This function is used to build a prefix for error messages. @C{-------------------------------------------------------------------------} -@sect1{libraries| @title{Standard Libraries} +@sect1{libraries| @title{The Standard Libraries} + +@simplesect{ The standard Lua libraries provide useful functions -that are implemented directly through the @N{C API}. +that are implemented @N{in C} through the @N{C API}. Some of these functions provide essential services to the language (e.g., @Lid{type} and @Lid{getmetatable}); -others provide access to @Q{outside} services (e.g., I/O); +others provide access to outside services (e.g., I/O); and others could be implemented in Lua itself, -but are quite useful or have critical performance requirements that +but that for different reasons deserve an implementation in C (e.g., @Lid{table.sort}). All libraries are implemented through the official @N{C API} @@ -5828,6 +6443,14 @@ to its expected parameters. For instance, a function documented as @T{foo(arg)} should not be called without an argument. +The notation @fail means a false value representing +some kind of failure. +(Currently, @fail is equal to @nil, +but that may change in future versions. +The recommendation is to always test the success of these functions +with @T{(not status)}, instead of @T{(status == nil)}.) + + Currently, Lua has the following standard libraries: @itemize{ @@ -5856,23 +6479,64 @@ Except for the basic and the package libraries, each library provides all its functions as fields of a global table or as methods of its objects. -To have access to these libraries, -the @N{C host} program should call the @Lid{luaL_openlibs} function, -which opens all standard libraries. +} + + +@sect2{lualib-h| @title{Loading the Libraries in C code} + +A @N{C host} program must explicitly load +the standard libraries into a state, +if it wants its scripts to use them. +For that, +the host program can call the function @Lid{luaL_openlibs}. Alternatively, -the host program can open them individually by using -@Lid{luaL_requiref} to call -@defid{luaopen_base} (for the basic library), -@defid{luaopen_package} (for the package library), -@defid{luaopen_coroutine} (for the coroutine library), -@defid{luaopen_string} (for the string library), -@defid{luaopen_utf8} (for the UTF8 library), -@defid{luaopen_table} (for the table library), -@defid{luaopen_math} (for the mathematical library), -@defid{luaopen_io} (for the I/O library), -@defid{luaopen_os} (for the operating system library), -and @defid{luaopen_debug} (for the debug library). -These functions are declared in @defid{lualib.h}. +the host can select which libraries to open, +by using @Lid{luaL_openselectedlibs}. +Both functions are declared in the header file @id{lualib.h}. +@index{lualib.h} + +The stand-alone interpreter @id{lua} @see{lua-sa} +already opens all standard libraries. + +@APIEntry{void luaL_openlibs (lua_State *L);| +@apii{0,0,e} + +Opens all standard Lua libraries into the given state. + +} + +@APIEntry{void luaL_openselectedlibs (lua_State *L, int load, int preload);| +@apii{0,0,e} + +Opens (loads) and preloads selected standard libraries into the state @id{L}. +(To @emph{preload} means to add +the library loader into the table @Lid{package.preload}, +so that the library can be required later by the program. +Keep in mind that @Lid{require} itself is provided +by the @emph{package} library. +If a program does not load that library, +it will be unable to require anything.) + +The integer @id{load} selects which libraries to load; +the integer @id{preload} selects which to preload, among those not loaded. +Both are masks formed by a bitwise OR of the following constants: +@description{ +@item{@defid{LUA_GLIBK} | the basic library.} +@item{@defid{LUA_LOADLIBK} | the package library.} +@item{@defid{LUA_COLIBK} | the coroutine library.} +@item{@defid{LUA_STRLIBK} | the string library.} +@item{@defid{LUA_UTF8LIBK} | the UTF-8 library.} +@item{@defid{LUA_TABLIBK} | the table library.} +@item{@defid{LUA_MATHLIBK} | the mathematical library.} +@item{@defid{LUA_IOLIBK} | the I/O library.} +@item{@defid{LUA_OSLIBK} | the operating system library.} +@item{@defid{LUA_DBLIBK} | the debug library.} +} + +} + +} + @sect2{predefined| @title{Basic Functions} @@ -5884,7 +6548,7 @@ implementations for some of its facilities. @LibEntry{assert (v [, message])| -Calls @Lid{error} if +Raises an error if the value of its argument @id{v} is false (i.e., @nil or @false); otherwise, returns all its arguments. In case of error, @@ -5900,83 +6564,108 @@ It performs different functions according to its first argument, @id{opt}: @description{ @item{@St{collect}| -performs a full garbage-collection cycle. +Performs a full garbage-collection cycle. This is the default option. } @item{@St{stop}| -stops automatic execution of the garbage collector. +Stops automatic execution of the garbage collector. The collector will run only when explicitly invoked, until a call to restart it. } @item{@St{restart}| -restarts automatic execution of the garbage collector. +Restarts automatic execution of the garbage collector. } @item{@St{count}| -returns the total memory in use by Lua in Kbytes. +Returns the total memory in use by Lua in Kbytes. The value has a fractional part, so that it multiplied by 1024 -gives the exact number of bytes in use by Lua -(except for overflows). +gives the exact number of bytes in use by Lua. } @item{@St{step}| -performs a garbage-collection step. -The step @Q{size} is controlled by @id{arg}. -With a zero value, -the collector will perform one basic (indivisible) step. -For non-zero values, -the collector will perform as if that amount of memory -(in KBytes) had been allocated by Lua. -Returns @true if the step finished a collection cycle. +Performs a garbage-collection step. +This option may be followed by an extra argument, +an integer with the step size. + +If the size is a positive @id{n}, +the collector acts as if @id{n} new bytes have been allocated. +If the size is zero, +the collector performs a basic step. +In incremental mode, +a basic step corresponds to the current step size. +In generational mode, +a basic step performs a full minor collection or +an incremental step, +if the collector has scheduled one. + +In incremental mode, +the function returns @true if the step finished a collection cycle. +In generational mode, +the function returns @true if the step finished a major collection. } -@item{@St{setpause}| -sets @id{arg} as the new value for the @emph{pause} of -the collector @see{GC}. -Returns the previous value for @emph{pause}. +@item{@St{isrunning}| +Returns a boolean that tells whether the collector is running +(i.e., not stopped). } @item{@St{incremental}| -Change the collector mode to incremental. -This option can be followed by three numbers: -the garbage-collector pause, -the step multiplier, -and the step size. +Changes the collector mode to incremental and returns the previous mode. } @item{@St{generational}| -Change the collector mode to generational. -This option can be followed by two numbers: -the garbage-collector minor multiplier -and the major multiplier. +Changes the collector mode to generational and returns the previous mode. } -@item{@St{isrunning}| -returns a boolean that tells whether the collector is running -(i.e., not stopped). +@item{@St{param}| +Changes and/or retrieves the values of a parameter of the collector. +This option must be followed by one or two extra arguments: +The name of the parameter being changed or retrieved (a string) +and an optional new value for that parameter, +an integer in the range @M{[0,100000]}. +The first argument must have one of the following values: +@description{ +@item{@St{minormul}| The minor multiplier. } +@item{@St{majorminor}| The major-minor multiplier. } +@item{@St{minormajor}| The minor-major multiplier. } +@item{@St{pause}| The garbage-collector pause. } +@item{@St{stepmul}| The step multiplier. } +@item{@St{stepsize}| The step size. } +} +The call always returns the previous value of the parameter. +If the call does not give a new value, +the value is left unchanged. + +Lua stores these values in a compressed format, +so, the value returned as the previous value may not be +exactly the last value set. } } +See @See{GC} for more details about garbage collection +and some of these options. + +This function should not be called by a finalizer. } @LibEntry{dofile ([filename])| -Opens the named file and executes its contents as a Lua chunk. +Opens the named file and executes its content as a Lua chunk, +returning all values returned by the chunk. When called without arguments, -@id{dofile} executes the contents of the standard input (@id{stdin}). -Returns all values returned by the chunk. +@id{dofile} executes the content of the standard input (@id{stdin}). In case of errors, @id{dofile} propagates the error -to its caller (that is, @id{dofile} does not run in protected mode). +to its caller. +(That is, @id{dofile} does not run in protected mode.) } @LibEntry{error (message [, level])| -Terminates the last protected function called -and returns @id{message} as the error object. -Function @id{error} never returns. +Raises an error @see{error} with @id{message} as the error object. +This function never returns. Usually, @id{error} adds some information about the error position at the beginning of the message, if the message is a string. @@ -6011,7 +6700,7 @@ Otherwise, returns the metatable of the given object. @LibEntry{ipairs (t)| -Returns three values (an iterator function, the table @id{t}, and 0) +Returns three values (an iterator function, the value @id{t}, and 0) so that the construction @verbatim{ for i,v in ipairs(t) do @rep{body} end @@ -6034,8 +6723,8 @@ with previous results. A return of an empty string, @nil, or no value signals the end of the chunk. If there are no syntactic errors, -returns the compiled chunk as a function; -otherwise, returns @nil plus the error message. +@id{load} returns the compiled chunk as a function; +otherwise, it returns @fail plus the error message. When you load a main chunk, the resulting function will always have exactly one upvalue, @@ -6071,6 +6760,7 @@ The default is @St{bt}. Lua does not check the consistency of binary chunks. Maliciously crafted binary chunks can crash the interpreter. +You can use the @id{mode} parameter to prevent loading binary chunks. } @@ -6088,7 +6778,7 @@ if no file name is given. Allows a program to traverse all fields of a table. Its first argument is a table and its second argument is an index in this table. -@id{next} returns the next index of the table +A call to @id{next} returns the next index of the table and its associated value. When called with @nil as its second argument, @id{next} returns an initial index @@ -6105,9 +6795,8 @@ The order in which the indices are enumerated is not specified, (To traverse a table in numerical order, use a numerical @Rw{for}.) -The behavior of @id{next} is undefined if, -during the traversal, -you assign any value to a non-existent field in the table. +You should not assign any value to a non-existent field in a table +during its traversal. You may however modify existing fields. In particular, you may set existing fields to nil. @@ -6116,11 +6805,11 @@ In particular, you may set existing fields to nil. @LibEntry{pairs (t)| If @id{t} has a metamethod @idx{__pairs}, -calls it with @id{t} as argument and returns the first three +calls it with @id{t} as argument and returns the first four results from the call. Otherwise, -returns three values: the @Lid{next} function, the table @id{t}, and @nil, +returns the @Lid{next} function, the table @id{t}, plus two @nil values, so that the construction @verbatim{ for k,v in pairs(t) do @rep{body} end @@ -6134,24 +6823,27 @@ the table during its traversal. @LibEntry{pcall (f [, arg1, @Cdots])| -Calls function @id{f} with -the given arguments in @def{protected mode}. +Calls the function @id{f} with +the given arguments in @emphx{protected mode}. This means that any error @N{inside @T{f}} is not propagated; instead, @id{pcall} catches the error and returns a status code. Its first result is the status code (a boolean), -which is true if the call succeeds without errors. +which is @true if the call succeeds without errors. In such case, @id{pcall} also returns all results from the call, after this first result. -In case of any error, @id{pcall} returns @false plus the error message. +In case of any error, @id{pcall} returns @false plus the error object. +Note that errors caught by @id{pcall} do not call a message handler. } @LibEntry{print (@Cdots)| Receives any number of arguments and prints their values to @id{stdout}, -using the @Lid{tostring} function to convert each argument to a string. -@id{print} is not intended for formatted output, +converting each argument to a string +following the same rules of @Lid{tostring}. + +The function @id{print} is not intended for formatted output, but only as a quick way to show a value, for instance for debugging. For complete control over the output, @@ -6168,7 +6860,7 @@ Returns a boolean. @LibEntry{rawget (table, index)| Gets the real value of @T{table[index]}, -without invoking the @idx{__index} metamethod. +without using the @idx{__index} metavalue. @id{table} must be a table; @id{index} may be any value. @@ -6184,7 +6876,7 @@ Returns an integer. @LibEntry{rawset (table, index, value)| Sets the real value of @T{table[index]} to @id{value}, -without invoking the @idx{__newindex} metamethod. +without using the @idx{__newindex} metavalue. @id{table} must be a table, @id{index} any value different from @nil and @x{NaN}, and @id{value} any Lua value. @@ -6206,8 +6898,6 @@ and @id{select} returns the total number of extra arguments it received. @LibEntry{setmetatable (table, metatable)| Sets the metatable for the given table. -(To change the metatable of other types from Lua code, -you must use the @link{debuglib|debug library}.) If @id{metatable} is @nil, removes the metatable of the given table. If the original metatable has a @idx{__metatable} field, @@ -6215,6 +6905,9 @@ raises an error. This function returns @id{table}. +To change the metatable of other types from Lua code, +you must use the @link{debuglib|debug library}. + } @LibEntry{tonumber (e [, base])| @@ -6224,11 +6917,11 @@ When called with no @id{base}, If the argument is already a number or a string convertible to a number, then @id{tonumber} returns this number; -otherwise, it returns @nil. +otherwise, it returns @fail. The conversion of strings can result in integers or floats, according to the lexical conventions of Lua @see{lexical}. -(The string may have leading and trailing spaces and a sign.) +The string may have leading and trailing spaces and a sign. When called with @id{base}, then @id{e} must be a string to be interpreted as @@ -6238,24 +6931,30 @@ In bases @N{above 10}, the letter @Char{A} (in either upper or lower case) @N{represents 10}, @Char{B} @N{represents 11}, and so forth, with @Char{Z} representing 35. If the string @id{e} is not a valid numeral in the given base, -the function returns @nil. +the function returns @fail. } @LibEntry{tostring (v)| + Receives a value of any type and converts it to a string in a human-readable format. -(For complete control of how numbers are converted, -use @Lid{string.format}.) If the metatable of @id{v} has a @idx{__tostring} field, then @id{tostring} calls the corresponding value with @id{v} as argument, and uses the result of the call as its result. +Otherwise, if the metatable of @id{v} has a @idx{__name} field +with a string value, +@id{tostring} may use that string in its final result. + +For complete control of how numbers are converted, +use @Lid{string.format}. } @LibEntry{type (v)| + Returns the type of its only argument, coded as a string. The possible results of this function are @St{nil} (a string, not the value @nil), @@ -6273,16 +6972,24 @@ and @St{userdata}. A global variable (not a function) that holds a string containing the running Lua version. -The current value of this variable is @St{Lua 5.4}. +The current value of this variable is @St{Lua 5.5}. } -@LibEntry{warn (message [, tocont])| +@LibEntry{warn (msg1, @Cdots)| -Emits a warning with the given message. -A message in a call with @id{tocont} true should be -continued in another call to this function. -The default for @id{tocont} is false. +Emits a warning with a message composed by the concatenation +of all its arguments (which should be strings). + +By convention, +a one-piece message starting with @Char{@At} +is intended to be a @emph{control message}, +which is a message to the warning system itself. +In particular, the standard warning function in Lua +recognizes the control messages @St{@At{}off}, +to stop the emission of warnings, +and @St{@At{}on}, to (re)start the emission; +it ignores unknown control messages. } @@ -6302,6 +7009,29 @@ which come inside the table @defid{coroutine}. See @See{coroutine} for a general description of coroutines. +@LibEntry{coroutine.close ([co])| + +Closes coroutine @id{co}, +that is, +closes all its pending to-be-closed variables +and puts the coroutine in a dead state. +The default for @id{co} is the running coroutine. + +The given coroutine must be dead, suspended, +or be the running coroutine. +For the running coroutine, +this function does not return. +Instead, the resume that (re)started the coroutine returns. + +For other coroutines, +in case of error +(either the original error that stopped the coroutine or +errors in closing methods), +this function returns @false plus the error object; +otherwise it returns @true. + +} + @LibEntry{coroutine.create (f)| Creates a new coroutine, with body @id{f}. @@ -6311,26 +7041,16 @@ an object with type @T{"thread"}. } -@LibEntry{coroutine.isyieldable ()| +@LibEntry{coroutine.isyieldable ([co])| -Returns true when the running coroutine can yield. +Returns @true when the coroutine @id{co} can yield. +The default for @id{co} is the running coroutine. -A running coroutine is yieldable if it is not the main thread and +A coroutine is yieldable if it is not the main thread and it is not inside a non-yieldable @N{C function}. } -@LibEntry{coroutine.kill(co)| - -Kills coroutine @id{co}, -closing all its pending to-be-closed variables -and putting the coroutine in a dead state. -In case of error closing some variable, -returns @false plus the error object; -otherwise returns @true. - -} - @LibEntry{coroutine.resume (co [, val1, @Cdots])| Starts or continues the execution of coroutine @id{co}. @@ -6355,15 +7075,16 @@ If there is any error, @LibEntry{coroutine.running ()| Returns the running coroutine plus a boolean, -true when the running coroutine is the main one. +@true when the running coroutine is the main one. } @LibEntry{coroutine.status (co)| -Returns the status of coroutine @id{co}, as a string: +Returns the status of the coroutine @id{co}, as a string: @T{"running"}, -if the coroutine is running (that is, it called @id{status}); +if the coroutine is running +(that is, it is the one that called @id{status}); @T{"suspended"}, if the coroutine is suspended in a call to @id{yield}, or if it has not started running yet; @T{"normal"} if the coroutine is active but not running @@ -6375,14 +7096,15 @@ or if it has stopped with an error. @LibEntry{coroutine.wrap (f)| -Creates a new coroutine, with body @id{f}. +Creates a new coroutine, with body @id{f}; @id{f} must be a function. Returns a function that resumes the coroutine each time it is called. -Any arguments passed to the function behave as the +Any arguments passed to this function behave as the extra arguments to @id{resume}. -Returns the same values returned by @id{resume}, +The function returns the same values returned by @id{resume}, except the first boolean. -In case of error, propagates the error. +In case of error, +the function closes the coroutine and propagates the error. } @@ -6401,7 +7123,7 @@ The package library provides basic facilities for loading modules in Lua. It exports one function directly in the global environment: @Lid{require}. -Everything else is exported in a table @defid{package}. +Everything else is exported in the table @defid{package}. @LibEntry{require (modname)| @@ -6411,11 +7133,15 @@ The function starts by looking into the @Lid{package.loaded} table to determine whether @id{modname} is already loaded. If it is, then @id{require} returns the value stored at @T{package.loaded[modname]}. +(The absence of a second result in this case +signals that this call did not have to load the module.) Otherwise, it tries to find a @emph{loader} for the module. To find a loader, -@id{require} is guided by the @Lid{package.searchers} sequence. -By changing this sequence, +@id{require} is guided by the table @Lid{package.searchers}. +Each item in this table is a search function, +that searches for the module in a particular way. +By changing this table, we can change how @id{require} looks for a module. The following explanation is based on the default configuration for @Lid{package.searchers}. @@ -6432,16 +7158,24 @@ it tries an @emph{all-in-one} loader @seeF{package.searchers}. Once a loader is found, @id{require} calls the loader with two arguments: -@id{modname} and an extra value dependent on how it got the loader. -(If the loader came from a file, -this extra value is the file name.) +@id{modname} and an extra value, +a @emph{loader data}, +also returned by the searcher. +The loader data can be any value useful to the module; +for the default searchers, +it indicates where the loader was found. +(For instance, if the loader came from a file, +this extra value is the file path.) If the loader returns any non-nil value, @id{require} assigns the returned value to @T{package.loaded[modname]}. If the loader does not return a non-nil value and has not assigned any value to @T{package.loaded[modname]}, -then @id{require} assigns @Rw{true} to this entry. +then @id{require} assigns @true to this entry. In any case, @id{require} returns the final value of @T{package.loaded[modname]}. +Besides that value, @id{require} also returns as a second result +the loader data returned by the searcher, +which indicates how @id{require} found the module. If there is any error loading or running the module, or if it cannot find any loader for the module, @@ -6479,11 +7213,12 @@ Default is @Char{-}.} @LibEntry{package.cpath| -The path used by @Lid{require} to search for a @N{C loader}. +A string with the path used by @Lid{require} +to search for a @N{C loader}. Lua initializes the @N{C path} @Lid{package.cpath} in the same way it initializes the Lua path @Lid{package.path}, -using the environment variable @defid{LUA_CPATH_5_4}, +using the environment variable @defid{LUA_CPATH_5_5}, or the environment variable @defid{LUA_CPATH}, or a default path defined in @id{luaconf.h}. @@ -6500,6 +7235,8 @@ When you require a module @id{modname} and This variable is only a reference to the real table; assignments to this variable do not change the table used by @Lid{require}. +The real table is stored in the C registry @see{registry}, +indexed by the key @defid{LUA_LOADED_TABLE}, a string. } @@ -6527,23 +7264,34 @@ including if necessary a path and an extension. @id{funcname} must be the exact name exported by the @N{C library} (which may depend on the @N{C compiler} and linker used). -This function is not supported by @N{Standard C}. -As such, it is only available on some platforms -(Windows, Linux, Mac OS X, Solaris, BSD, -plus other Unix systems that support the @id{dlfcn} standard). +This functionality is not supported by @N{ISO C}. +As such, @id{loadlib} is only available on some platforms: +Linux, Windows, Mac OS X, Solaris, BSD, +plus other Unix systems that support the @id{dlfcn} standard. + +This function is inherently insecure, +as it allows Lua to call any function in any readable dynamic +library in the system. +(Lua calls any function assuming the function +has a proper prototype and respects a proper protocol +@see{lua_CFunction}. +Therefore, +calling an arbitrary function in an arbitrary dynamic library +more often than not results in an access violation.) } @LibEntry{package.path| -The path used by @Lid{require} to search for a Lua loader. +A string with the path used by @Lid{require} +to search for a Lua loader. At start-up, Lua initializes this variable with -the value of the environment variable @defid{LUA_PATH_5_4} or +the value of the environment variable @defid{LUA_PATH_5_5} or the environment variable @defid{LUA_PATH} or with a default path defined in @id{luaconf.h}, if those environment variables are not defined. -Any @St{;;} in the value of the environment variable +A @St{;;} in the value of the environment variable is replaced by the default path. } @@ -6556,21 +7304,27 @@ A table to store loaders for specific modules This variable is only a reference to the real table; assignments to this variable do not change the table used by @Lid{require}. +The real table is stored in the C registry @see{registry}, +indexed by the key @defid{LUA_PRELOAD_TABLE}, a string. } @LibEntry{package.searchers| -A table used by @Lid{require} to control how to load modules. +A table used by @Lid{require} to control how to find modules. Each entry in this table is a @def{searcher function}. When looking for a module, @Lid{require} calls each of these searchers in ascending order, with the module name (the argument given to @Lid{require}) as its sole argument. -The function can return another function (the module @def{loader}) -plus an extra value that will be passed to that loader, -or a string explaining why it did not find that module +If the searcher finds the module, +it returns another function, the module @def{loader}, +plus an extra value, a @emph{loader data}, +that will be passed to that loader and +returned as a second result by @Lid{require}. +If it cannot find the module, +it returns a string explaining why (or @nil if it has nothing to say). Lua initializes this table with four searcher functions. @@ -6620,9 +7374,13 @@ into one single library, with each submodule keeping its original open function. All searchers except the first one (preload) return as the extra value -the file name where the module was found, +the file path where the module was found, as returned by @Lid{package.searchpath}. -The first searcher returns no extra value. +The first searcher always returns the string @St{:preload:}. + +Searchers should raise no errors and have no side effects in Lua. +(They may have side effects in C, +for instance by linking the application with a library.) } @@ -6652,7 +7410,7 @@ will try to open the files Returns the resulting name of the first file that it can open in read mode (after closing the file), -or @nil plus an error message if none succeeds. +or @fail plus an error message if none succeeds. (This error message lists all file names it tried to open.) } @@ -6661,6 +7419,8 @@ or @nil plus an error message if none succeeds. @sect2{strlib| @title{String Manipulation} +@simplesect{ + This library provides generic functions for string manipulation, such as finding and extracting substrings, and pattern matching. When indexing a string in Lua, the first character is at @N{position 1} @@ -6731,15 +7491,14 @@ Looks for the first match of @id{pattern} @see{pm} in the string @id{s}. If it finds a match, then @id{find} returns the indices @N{of @T{s}} where this occurrence starts and ends; -otherwise, it returns @nil. +otherwise, it returns @fail. A third, optional numeric argument @id{init} specifies where to start the search; its default value @N{is 1} and can be negative. -A value of @true as a fourth, optional argument @id{plain} +A @true as a fourth, optional argument @id{plain} turns off the pattern matching facilities, so the function does a plain @Q{find substring} operation, with no characters in @id{pattern} being considered magic. -Note that if @id{plain} is given, then @id{init} must be given as well. If the pattern has captures, then in a successful match @@ -6751,11 +7510,17 @@ after the two indices. @LibEntry{string.format (formatstring, @Cdots)| Returns a formatted version of its variable number of arguments -following the description given in its first argument (which must be a string). +following the description given in its first argument, +which must be a string. The format string follows the same rules as the @ANSI{sprintf}. -The only differences are that the conversion specifiers and modifiers -@T{*}, @id{h}, @id{L}, @id{l}, and @id{n} are not supported -and that there is an extra specifier, @id{q}. +The accepted conversion specifiers are +@id{A}, @id{a}, @id{c}, @id{d}, @id{E}, @id{e}, @id{f}, @id{G}, @id{g}, +@id{i}, @id{o}, @id{p}, @id{s}, @id{u}, @id{X}, @id{x}, and @Char{%}, +plus a non-C specifier @id{q}. +The accepted flags are @Char{-}, @Char{+}, @Char{#}, +@Char{0}, and @Char{ } (space). +Both width and precision, when present, +are limited to two digits. The specifier @id{q} formats booleans, nil, numbers, and strings in a way that the result is a valid constant in Lua source code. @@ -6775,7 +7540,7 @@ may produce the string: "a string with \"quotes\" and \ new line" } -This specifier does not support modifiers (flags, width, length). +This specifier does not support modifiers (flags, width, precision). The conversion specifiers @id{A}, @id{a}, @id{E}, @id{e}, @id{f}, @@ -6852,9 +7617,9 @@ If @id{repl} is a string, then its value is used for replacement. The @N{character @T{%}} works as an escape character: any sequence in @id{repl} of the form @T{%@rep{d}}, with @rep{d} between 1 and 9, -stands for the value of the @rep{d}-th captured substring. -The sequence @T{%0} stands for the whole match. -The sequence @T{%%} stands for a @N{single @T{%}}. +stands for the value of the @rep{d}-th captured substring; +the sequence @T{%0} stands for the whole match; +the sequence @T{%%} stands for a @N{single @T{%}}. If @id{repl} is a table, then the table is queried for every match, using the first capture as the key. @@ -6877,25 +7642,25 @@ then there is no replacement Here are some examples: @verbatim{ x = string.gsub("hello world", "(%w+)", "%1 %1") ---> x="hello hello world world" +-- x="hello hello world world" x = string.gsub("hello world", "%w+", "%0 %0", 1) ---> x="hello hello world" +-- x="hello hello world" x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") ---> x="world hello Lua from" +-- x="world hello Lua from" x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) ---> x="home = /home/roberto, user = roberto" +-- x="home = /home/roberto, user = roberto" x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) return load(s)() end) ---> x="4+5 = 9" +-- x="4+5 = 9" -local t = {name="lua", version="5.4"} +local t = {name="lua", version="5.5"} x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) ---> x="lua-5.4.tar.gz" +-- x="lua-5.5.tar.gz" } } @@ -6921,10 +7686,10 @@ The definition of what an uppercase letter is depends on the current locale. @LibEntry{string.match (s, pattern [, init])| Looks for the first @emph{match} of -@id{pattern} @see{pm} in the string @id{s}. +the @id{pattern} @see{pm} in the string @id{s}. If it finds one, then @id{match} returns the captures from the pattern; -otherwise it returns @nil. +otherwise it returns @fail. If @id{pattern} specifies no captures, then the whole match is returned. A third, optional numeric argument @id{init} specifies @@ -6936,14 +7701,14 @@ its default value @N{is 1} and can be negative. @LibEntry{string.pack (fmt, v1, v2, @Cdots)| Returns a binary string containing the values @id{v1}, @id{v2}, etc. -packed (that is, serialized in binary form) +serialized in binary form (packed) according to the format string @id{fmt} @see{pack}. } @LibEntry{string.packsize (fmt)| -Returns the size of a string resulting from @Lid{string.pack} +Returns the length of a string resulting from @Lid{string.pack} with the given format. The format string cannot have the variable-length options @Char{s} or @Char{z} @see{pack}. @@ -6979,7 +7744,7 @@ If @id{j} is absent, then it is assumed to be equal to @num{-1} In particular, the call @T{string.sub(s,1,j)} returns a prefix of @id{s} with length @id{j}, -and @T{string.sub(s, -i)} (for a positive @id{i}) +and @T{string.sub(s,-i)} (for a positive @id{i}) returns a suffix of @id{s} with length @id{i}. @@ -7014,9 +7779,13 @@ The definition of what a lowercase letter is depends on the current locale. } +} + @sect3{pm| @title{Patterns} +@simplesect{ + Patterns in Lua are described by regular strings, which are interpreted as patterns by the pattern-matching functions @Lid{string.find}, @@ -7026,6 +7795,8 @@ and @Lid{string.match}. This section describes the syntax and the meaning (that is, what they match) of these strings. +} + @sect4{@title{Character Class:} A @def{character class} is used to represent a set of characters. The following combinations are allowed in describing a character class: @@ -7064,8 +7835,7 @@ represents the character @rep{x}. This is the standard way to escape the magic characters. Any non-alphanumeric character (including all punctuation characters, even the non-magical) -can be preceded by a @Char{%} -when used to represent itself in a pattern. +can be preceded by a @Char{%} to represent itself in a pattern. } @item{@T{[@rep{set}]}| @@ -7121,19 +7891,19 @@ which matches any single character in the class; @item{ a single character class followed by @Char{*}, -which matches zero or more repetitions of characters in the class. +which matches sequences of zero or more characters in the class. These repetition items will always match the longest possible sequence; } @item{ a single character class followed by @Char{+}, -which matches one or more repetitions of characters in the class. +which matches sequences of one or more characters in the class. These repetition items will always match the longest possible sequence; } @item{ a single character class followed by @Char{-}, -which also matches zero or more repetitions of characters in the class. +which also matches sequences of zero or more characters in the class. Unlike @Char{*}, these repetition items will always match the shortest possible sequence; } @@ -7194,11 +7964,11 @@ that match captures are stored (@emph{captured}) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern @T{"(a*(.)%w(%s*))"}, the part of the string matching @T{"a*(.)%w(%s*)"} is -stored as the first capture (and therefore has @N{number 1}); +stored as the first capture, and therefore has @N{number 1}; the character matching @St{.} is captured with @N{number 2}, and the part matching @St{%s*} has @N{number 3}. -As a special case, the empty capture @T{()} captures +As a special case, the capture @T{()} captures the current string position (a number). For instance, if we apply the pattern @T{"()aa()"} on the string @T{"flaaap"}, there will be two captures: @N{3 and 5}. @@ -7210,7 +7980,7 @@ The function @Lid{string.gsub} and the iterator @Lid{string.gmatch} match multiple occurrences of the given pattern in the subject. For these functions, a new match is considered valid only -if it ends at least one byte after the previous match. +if it ends at least one byte after the end of the previous match. In other words, the pattern machine never accepts the empty string as a match immediately after another match. As an example, @@ -7270,19 +8040,21 @@ coded as an unsigned integer with @id{n} bytes @item{@T{X@rep{op}}|an empty item that aligns according to option @id{op} (which is otherwise ignored)} -@item{@Char{ }|(empty space) ignored} +@item{@Char{ }|(space) ignored} } (A @St{[@rep{n}]} means an optional integral numeral.) Except for padding, spaces, and configurations (options @St{xX <=>!}), -each option corresponds to an argument (in @Lid{string.pack}) -or a result (in @Lid{string.unpack}). +each option corresponds to an argument in @Lid{string.pack} +or a result in @Lid{string.unpack}. For options @St{!@rep{n}}, @St{s@rep{n}}, @St{i@rep{n}}, and @St{I@rep{n}}, @id{n} can be any integer between 1 and 16. All integral options check overflows; @Lid{string.pack} checks whether the given value fits in the given size; @Lid{string.unpack} checks whether the read value fits in a Lua integer. +For the unsigned options, +Lua integers are treated as unsigned values too. Any format string starts as if prefixed by @St{!1=}, that is, @@ -7305,7 +8077,7 @@ option @St{s} follows the alignment of its starting integer. All padding is filled with zeros by @Lid{string.pack} -(and ignored by @Lid{string.unpack}). +and ignored by @Lid{string.unpack}. } @@ -7336,8 +8108,8 @@ Functions that interpret byte sequences only accept valid sequences (well formed and not overlong). By default, they only accept byte sequences that result in valid Unicode code points, -rejecting values larger than @T{10FFFF} and surrogates. -A boolean argument @id{nonstrict}, when available, +rejecting values greater than @T{10FFFF} and surrogates. +A boolean argument @id{lax}, when available, lifts these checks, so that all values up to @T{0x7FFFFFFF} are accepted. (Not well formed and overlong sequences are still rejected.) @@ -7360,55 +8132,61 @@ assuming that the subject is a valid UTF-8 string. } -@LibEntry{utf8.codes (s [, nonstrict])| +@LibEntry{utf8.codes (s [, lax])| Returns values so that the construction @verbatim{ for p, c in utf8.codes(s) do @rep{body} end } -will iterate over all characters in string @id{s}, +will iterate over all UTF-8 characters in string @id{s}, with @id{p} being the position (in bytes) and @id{c} the code point of each character. It raises an error if it meets any invalid byte sequence. } -@LibEntry{utf8.codepoint (s [, i [, j [, nonstrict]]])| +@LibEntry{utf8.codepoint (s [, i [, j [, lax]]])| -Returns the codepoints (as integers) from all characters in @id{s} +Returns the code points (as integers) from all characters in @id{s} that start between byte position @id{i} and @id{j} (both included). The default for @id{i} is 1 and for @id{j} is @id{i}. It raises an error if it meets any invalid byte sequence. } -@LibEntry{utf8.len (s [, i [, j [, nonstrict]]])| +@LibEntry{utf8.len (s [, i [, j [, lax]]])| Returns the number of UTF-8 characters in string @id{s} that start between positions @id{i} and @id{j} (both inclusive). The default for @id{i} is @num{1} and for @id{j} is @num{-1}. If it finds any invalid byte sequence, -returns a false value plus the position of the first invalid byte. +returns @fail plus the position of the first invalid byte. } @LibEntry{utf8.offset (s, n [, i])| -Returns the position (in bytes) where the encoding of the -@id{n}-th character of @id{s} -(counting from position @id{i}) starts. +Returns the position of the @id{n}-th character of @id{s} +(counting from byte position @id{i}) as two integers: +The index (in bytes) where its encoding starts and the +index (in bytes) where it ends. + +If the specified character is right after the end of @id{s}, +the function behaves as if there was a @Char{\0} there. +If the specified character is neither in the subject +nor right after its end, +the function returns @fail. + A negative @id{n} gets characters before position @id{i}. The default for @id{i} is 1 when @id{n} is non-negative and @T{#s + 1} otherwise, -so that @T{utf8.offset(s, -n)} gets the offset of the +so that @T{utf8.offset(s,-n)} gets the offset of the @id{n}-th character from the end of the string. -If the specified character is neither in the subject -nor right after its end, -the function returns @nil. As a special case, -when @id{n} is 0 the function returns the start of the encoding -of the character that contains the @id{i}-th byte of @id{s}. +when @id{n} is 0 the function returns the start and end +of the encoding of the character that contains the +@id{i}-th byte of @id{s}. This function assumes that @id{s} is a valid UTF-8 string. @@ -7438,26 +8216,41 @@ If @id{i} is greater than @id{j}, returns the empty string. } +@LibEntry{table.create (nseq [, nrec])| + +Creates a new empty table, preallocating memory. +This preallocation may help performance and save memory +when you know in advance how many elements the table will have. + +Parameter @id{nseq} is a hint for how many elements the table +will have as a sequence. +Optional parameter @id{nrec} is a hint for how many other elements +the table will have; its default is zero. + +} + @LibEntry{table.insert (list, [pos,] value)| Inserts element @id{value} at position @id{pos} in @id{list}, shifting up the elements -@T{list[pos], list[pos+1], @Cdots, list[#list]}. +@T{list[pos],list[pos+1],@Cdots,list[#list]}. The default value for @id{pos} is @T{#list+1}, so that a call @T{table.insert(t,x)} inserts @id{x} at the end -of list @id{t}. +of the list @id{t}. } @LibEntry{table.move (a1, f, e, t [,a2])| -Moves elements from table @id{a1} to table @id{a2}, +Moves elements from the table @id{a1} to the table @id{a2}, performing the equivalent to the following multiple assignment: @T{a2[t],@Cdots = a1[f],@Cdots,a1[e]}. The default for @id{a2} is @id{a1}. The destination range can overlap with the source range. The number of elements to be moved must fit in a Lua integer. +If @id{f} is larger than @id{e}, +nothing is moved. Returns the destination table @id{a2}. @@ -7478,37 +8271,37 @@ Removes from @id{list} the element at position @id{pos}, returning the value of the removed element. When @id{pos} is an integer between 1 and @T{#list}, it shifts down the elements -@T{list[pos+1], list[pos+2], @Cdots, list[#list]} +@T{list[pos+1],list[pos+2],@Cdots,list[#list]} and erases element @T{list[#list]}; The index @id{pos} can also be 0 when @T{#list} is 0, or @T{#list + 1}. The default value for @id{pos} is @T{#list}, so that a call @T{table.remove(l)} removes the last element -of list @id{l}. +of the list @id{l}. } @LibEntry{table.sort (list [, comp])| -Sorts list elements in a given order, @emph{in-place}, +Sorts the list elements in a given order, @emph{in-place}, from @T{list[1]} to @T{list[#list]}. If @id{comp} is given, then it must be a function that receives two list elements and returns true when the first element must come -before the second in the final order -(so that, after the sort, -@T{i < j} implies @T{not comp(list[j],list[i])}). +before the second in the final order, +so that, after the sort, +@T{i <= j} implies @T{not comp(list[j],list[i])}. If @id{comp} is not given, then the standard Lua operator @T{<} is used instead. -Note that the @id{comp} function must define -a strict partial order over the elements in the list; -that is, it must be asymmetric and transitive. -Otherwise, no valid sort may be possible. +The @id{comp} function must define a consistent order; +more formally, the function must define a strict weak order. +(A weak order is similar to a total order, +but it can equate different elements for comparison purposes.) The sort algorithm is not stable: -elements considered equal by the given order +Different elements considered equal by the given order may have their relative positions changed by the sort. } @@ -7532,15 +8325,15 @@ This library provides basic mathematical functions. It provides all its functions and constants inside the table @defid{math}. Functions with the annotation @St{integer/float} give integer results for integer arguments -and float results for float (or mixed) arguments. -Rounding functions -(@Lid{math.ceil}, @Lid{math.floor}, and @Lid{math.modf}) +and float results for non-integer arguments. +The rounding functions +@Lid{math.ceil}, @Lid{math.floor}, and @Lid{math.modf} return an integer when the result fits in the range of an integer, or a float otherwise. @LibEntry{math.abs (x)| -Returns the absolute value of @id{x}. (integer/float) +Returns the maximum value between @id{x} and @id{-x}. (integer/float) } @@ -7558,11 +8351,11 @@ Returns the arc sine of @id{x} (in radians). @LibEntry{math.atan (y [, x])| -@index{atan2} +@index{atan} @index{atan2} Returns the arc tangent of @T{y/x} (in radians), -but uses the signs of both arguments to find the +using the signs of both arguments to find the quadrant of the result. -(It also handles correctly the case of @id{x} being zero.) +It also handles correctly the case of @id{x} being zero. The default value for @id{x} is 1, so that the call @T{math.atan(y)} @@ -7572,7 +8365,7 @@ returns the arc tangent of @id{y}. @LibEntry{math.ceil (x)| -Returns the smallest integral value larger than or equal to @id{x}. +Returns the smallest integral value greater than or equal to @id{x}. } @@ -7597,7 +8390,7 @@ Returns the value @M{e@sp{x}} @LibEntry{math.floor (x)| -Returns the largest integral value smaller than or equal to @id{x}. +Returns the largest integral value less than or equal to @id{x}. } @@ -7608,10 +8401,27 @@ that rounds the quotient towards zero. (integer/float) } +@LibEntry{math.frexp (x)| + +Returns two numbers @id{m} and @id{e} such that @M{x = m2@sp{e}}, +where @id{e} is an integer. +When @id{x} is zero, NaN, +inf, or -inf, +@id{m} is equal to @id{x}; +otherwise, the absolute value of @id{m} +is in the range @C{(} @M{[0.5, 1)} @C{]}. + +} + @LibEntry{math.huge| The float value @idx{HUGE_VAL}, -a value larger than any other numeric value. +a value greater than any other numeric value. + +} + +@LibEntry{math.ldexp (m, e)| + +Returns @M{m2@sp{e}}, where @id{e} is an integer. } @@ -7626,7 +8436,7 @@ The default for @id{base} is @M{e} @LibEntry{math.max (x, @Cdots)| Returns the argument with the maximum value, -according to the Lua operator @T{<}. (integer/float) +according to the Lua operator @T{<}. } @@ -7638,7 +8448,7 @@ An integer with the maximum value for an integer. @LibEntry{math.min (x, @Cdots)| Returns the argument with the minimum value, -according to the Lua operator @T{<}. (integer/float) +according to the Lua operator @T{<}. } @@ -7670,7 +8480,7 @@ Converts the angle @id{x} from degrees to radians. When called without arguments, returns a pseudo-random float with uniform distribution -in the range @C{(} @M{[0,1)}. @C{]} +in the range @C{(} @M{[0, 1)}. @C{]} When called with two integers @id{m} and @id{n}, @id{math.random} returns a pseudo-random integer with uniform distribution in the range @M{[m, n]}. @@ -7679,24 +8489,24 @@ is equivalent to @T{math.random(1,n)}. The call @T{math.random(0)} produces an integer with all bits (pseudo)random. +This function uses the @idx{xoshiro256**} algorithm to produce +pseudo-random 64-bit integers, +which are the results of calls with @N{argument 0}. +Other results (ranges and floats) +are unbiased extracted from these integers. + Lua initializes its pseudo-random generator with the equivalent of a call to @Lid{math.randomseed} with no arguments, so that @id{math.random} should generate different sequences of results each time the program runs. -The results from this function have good statistical qualities, -but they are not cryptographically secure. -(For instance, there are no guarantees that it is hard -to predict future results based on the observation of -some number of previous results.) - } @LibEntry{math.randomseed ([x [, y]])| When called with at least one argument, the integer parameters @id{x} and @id{y} are -concatenated into a 128-bit @emphx{seed} that +joined into a @emphx{seed} that is used to reinitialize the pseudo-random generator; equal seeds produce equal sequences of numbers. The default for @id{y} is zero. @@ -7704,6 +8514,11 @@ The default for @id{y} is zero. When called with no arguments, Lua generates a seed with a weak attempt for randomness. + +This function returns the two seed components +that were effectively used, +so that setting them again repeats the sequence. + To ensure a required level of randomness to the initial state (or contrarily, to have a deterministic sequence, for instance when debugging a program), @@ -7734,7 +8549,7 @@ Returns the tangent of @id{x} (assumed to be in radians). If the value @id{x} is convertible to an integer, returns that integer. -Otherwise, returns @nil. +Otherwise, returns @fail. } @@ -7742,28 +8557,27 @@ Otherwise, returns @nil. Returns @St{integer} if @id{x} is an integer, @St{float} if it is a float, -or @nil if @id{x} is not a number. +or @fail if @id{x} is not a number. } @LibEntry{math.ult (m, n)| Returns a boolean, -true if and only if integer @id{m} is below integer @id{n} when +@true if and only if integer @id{m} is below integer @id{n} when they are compared as @x{unsigned integers}. } } - @sect2{iolib| @title{Input and Output Facilities} The I/O library provides two different styles for file manipulation. The first one uses implicit file handles; that is, there are operations to set a default input file and a default output file, -and all input/output operations are over these default files. +and all input/output operations are done over these default files. The second style uses explicit file handles. When using implicit file handles, @@ -7772,19 +8586,20 @@ When using explicit file handles, the operation @Lid{io.open} returns a file handle and then all operations are supplied as methods of the file handle. +The metatable for file handles provides metamethods +for @idx{__gc} and @idx{__close} that try +to close the file when called. + The table @id{io} also provides three predefined file handles with their usual meanings from C: @defid{io.stdin}, @defid{io.stdout}, and @defid{io.stderr}. The I/O library never closes these files. -The metatable for file handles provides metamethods -for @idx{__gc} and @idx{__close} that try -to close the file when called. Unless otherwise stated, -all I/O functions return @nil on failure -(plus an error message as a second result and -a system-dependent error code as a third result) -and some value different from @nil on success. +all I/O functions return @fail on failure, +plus an error message as a second result and +a system-dependent error code as a third result, +and some non-false value on success. On non-POSIX systems, the computation of the error message and error code in case of errors @@ -7823,8 +8638,8 @@ instead of returning an error code. Opens the given file name in read mode and returns an iterator function that works like @T{file:lines(@Cdots)} over the opened file. -When the iterator function detects the end of file, -it returns no values (to finish the loop) and automatically closes the file. +When the iterator function fails to read any value, +it automatically closes the file. Besides the iterator function, @id{io.lines} returns three other values: two @nil values as placeholders, @@ -7838,7 +8653,8 @@ to @T{io.input():lines("l")}; that is, it iterates over the lines of the default input file. In this case, the iterator does not close the file when the loop ends. -In case of errors this function raises the error, +In case of errors opening the file, +this function raises the error, instead of returning an error code. } @@ -7876,7 +8692,7 @@ Similar to @Lid{io.input}, but operates over the default output file. This function is system dependent and is not available on all platforms. -Starts program @id{prog} in a separated process and returns +Starts the program @id{prog} in a separated process and returns a file handle that you can use to read data from this program (if @id{mode} is @T{"r"}, the default) or to write data to this program @@ -7904,7 +8720,7 @@ and it is automatically removed when the program ends. Checks whether @id{obj} is a valid file handle. Returns the string @T{"file"} if @id{obj} is an open file handle, @T{"closed file"} if @id{obj} is a closed file handle, -or @nil if @id{obj} is not a file handle. +or @fail if @id{obj} is not a file handle. } @@ -7950,9 +8766,6 @@ starting at the current position. Unlike @Lid{io.lines}, this function does not close the file when the loop ends. -In case of errors this function raises the error, -instead of returning an error code. - } @LibEntry{file:read (@Cdots)| @@ -7961,7 +8774,7 @@ Reads the file @id{file}, according to the given formats, which specify what to read. For each format, the function returns a string or a number with the characters read, -or @nil if it cannot read data with the specified format. +or @fail if it cannot read data with the specified format. (In this latter case, the function does not read subsequent formats.) When called without arguments, @@ -7974,36 +8787,38 @@ The available formats are @item{@St{n}| reads a numeral and returns it as a float or an integer, following the lexical conventions of Lua. -(The numeral may have leading spaces and a sign.) +(The numeral may have leading whitespaces and a sign.) This format always reads the longest input sequence that is a valid prefix for a numeral; if that prefix does not form a valid numeral -(e.g., an empty string, @St{0x}, or @St{3.4e-}), -it is discarded and the format returns @nil. +(e.g., an empty string, @St{0x}, or @St{3.4e-}) +or it is too long (more than 200 characters), +it is discarded and the format returns @fail. } @item{@St{a}| reads the whole file, starting at the current position. -On end of file, it returns the empty string. +On end of file, it returns the empty string; +this format never fails. } @item{@St{l}| reads the next line skipping the end of line, -returning @nil on end of file. +returning @fail on end of file. This is the default format. } @item{@St{L}| reads the next line keeping the end-of-line character (if present), -returning @nil on end of file. +returning @fail on end of file. } @item{@emph{number}| reads a string with up to this number of bytes, -returning @nil on end of file. +returning @fail on end of file. If @id{number} is zero, it reads nothing and returns an empty string, -or @nil on end of file. +or @fail on end of file. } } @@ -8024,7 +8839,7 @@ specified by the string @id{whence}, as follows: } In case of success, @id{seek} returns the final file position, measured in bytes from the beginning of the file. -If @id{seek} fails, it returns @nil, +If @id{seek} fails, it returns @fail, plus a string describing the error. The default value for @id{whence} is @T{"cur"}, @@ -8040,31 +8855,22 @@ end of the file, and returns its size. @LibEntry{file:setvbuf (mode [, size])| -Sets the buffering mode for an output file. +Sets the buffering mode for a file. There are three available modes: @description{ - -@item{@St{no}| -no buffering; the result of any output operation appears immediately. -} - -@item{@St{full}| -full buffering; output operation is performed only -when the buffer is full or when -you explicitly @T{flush} the file @seeF{io.flush}. -} - -@item{@St{line}| -line buffering; output is buffered until a newline is output -or there is any input from some special files -(such as a terminal device). +@item{@St{no}| no buffering.} +@item{@St{full}| full buffering.} +@item{@St{line}| line buffering.} } -} For the last two cases, @id{size} is a hint for the size of the buffer, in bytes. The default is an appropriate size. +The specific behavior of each mode is non portable; +check the underlying @ANSI{setvbuf} in your platform for +more details. + } @LibEntry{file:write (@Cdots)| @@ -8073,7 +8879,9 @@ Writes the value of each of its arguments to @id{file}. The arguments must be strings or numbers. In case of success, this function returns @id{file}. -Otherwise it returns @nil plus a string describing the error. +Otherwise, it returns four values: +@fail, the error message, the error code, +and the number of bytes it was able to write. } @@ -8087,7 +8895,8 @@ This library is implemented through table @defid{os}. @LibEntry{os.clock ()| Returns an approximation of the amount in seconds of CPU time -used by the program. +used by the program, +as returned by the underlying @ANSI{clock}. } @@ -8119,10 +8928,9 @@ If @id{format} is not @St{*t}, then @id{date} returns the date as a string, formatted according to the same rules as the @ANSI{strftime}. -When called without arguments, -@id{date} returns a reasonable date and time representation that depends on -the host system and on the current locale. -(More specifically, @T{os.date()} is equivalent to @T{os.date("%c")}.) +If @id{format} is absent, it defaults to @St{%c}, +which gives a human-readable date and time representation +using the current locale. On non-POSIX systems, this function may be not @x{thread safe} @@ -8146,7 +8954,7 @@ This function is equivalent to the @ANSI{system}. It passes @id{command} to be executed by an operating system shell. Its first result is @true if the command terminated successfully, -or @nil otherwise. +or @fail otherwise. After this first result the function returns a string plus a number, as follows: @@ -8172,23 +8980,23 @@ When called without a @id{command}, @LibEntry{os.exit ([code [, close]])| Calls the @ANSI{exit} to terminate the host program. -If @id{code} is @Rw{true}, +If @id{code} is @true, the returned status is @idx{EXIT_SUCCESS}; -if @id{code} is @Rw{false}, +if @id{code} is @false, the returned status is @idx{EXIT_FAILURE}; if @id{code} is a number, the returned status is this number. -The default value for @id{code} is @Rw{true}. +The default value for @id{code} is @true. If the optional second argument @id{close} is true, -closes the Lua state before exiting. +the function closes the Lua state before exiting @seeF{lua_close}. } @LibEntry{os.getenv (varname)| -Returns the value of the process environment variable @id{varname}, -or @nil if the variable is not defined. +Returns the value of the process environment variable @id{varname} +or @fail if the variable is not defined. } @@ -8196,7 +9004,7 @@ or @nil if the variable is not defined. Deletes the file (or empty directory, on @x{POSIX} systems) with the given name. -If this function fails, it returns @nil, +If this function fails, it returns @fail plus a string describing the error and the error code. Otherwise, it returns true. @@ -8205,7 +9013,7 @@ Otherwise, it returns true. @LibEntry{os.rename (oldname, newname)| Renames the file or directory named @id{oldname} to @id{newname}. -If this function fails, it returns @nil, +If this function fails, it returns @fail, plus a string describing the error and the error code. Otherwise, it returns true. @@ -8220,7 +9028,7 @@ Sets the current locale of the program. @T{"monetary"}, @T{"numeric"}, or @T{"time"}; the default category is @T{"all"}. The function returns the name of the new locale, -or @nil if the request cannot be honored. +or @fail if the request cannot be honored. If @id{locale} is the empty string, the current locale is set to an implementation-defined native locale. @@ -8238,7 +9046,7 @@ because of its reliance on @CId{setlocale}. @LibEntry{os.time ([table])| -Returns the current time when called without arguments, +Returns the current local time when called without arguments, or a time representing the local date and time specified by the given table. This table must have fields @id{year}, @id{month}, and @id{day}, and may have fields @@ -8336,8 +9144,10 @@ within any function and so have no direct access to local variables. Returns the current hook settings of the thread, as three values: the current hook function, the current hook mask, -and the current hook count -(as set by the @Lid{debug.sethook} function). +and the current hook count, +as set by the @Lid{debug.sethook} function. + +Returns @fail if there is no active hook. } @@ -8350,22 +9160,22 @@ which means the function running at level @id{f} of the call stack of the given thread: @N{level 0} is the current function (@id{getinfo} itself); @N{level 1} is the function that called @id{getinfo} -(except for tail calls, which do not count on the stack); +(except for tail calls, which do not count in the stack); and so on. -If @id{f} is a number larger than the number of active functions, -then @id{getinfo} returns @nil. +If @id{f} is a number greater than the number of active functions, +then @id{getinfo} returns @fail. The returned table can contain all the fields returned by @Lid{lua_getinfo}, with the string @id{what} describing which fields to fill in. The default for @id{what} is to get all information available, except the table of valid lines. -If present, -the option @Char{f} +The option @Char{f} adds a field named @id{func} with the function itself. -If present, -the option @Char{L} -adds a field named @id{activelines} with the table of -valid lines. +The option @Char{L} adds a field named @id{activelines} +with the table of valid lines, +provided the function is a Lua function. +If the function has no debug information, +the table is empty. For instance, the expression @T{debug.getinfo(1,"n").name} returns a name for the current function, @@ -8381,15 +9191,22 @@ about the @Lid{print} function. This function returns the name and the value of the local variable with index @id{local} of the function at level @id{f} of the stack. This function accesses not only explicit local variables, -but also parameters, temporaries, etc. +but also parameters and temporary values. The first parameter or local variable has @N{index 1}, and so on, following the order that they are declared in the code, counting only the variables that are active in the current scope of the function. +Compile-time constants may not appear in this listing, +if they were optimized away by the compiler. Negative indices refer to vararg arguments; @num{-1} is the first vararg argument. -The function returns @nil if there is no variable with the given index, +These negative indices are only available when the vararg table +has been optimized away; +otherwise, the vararg arguments are available in the vararg table. + +The function returns @fail +if there is no variable with the given index, and raises an error when called with a level out of range. (You can call @Lid{debug.getinfo} to check whether the level is valid.) @@ -8420,10 +9237,18 @@ Returns the registry table @see{registry}. This function returns the name and the value of the upvalue with index @id{up} of the function @id{f}. -The function returns @nil if there is no upvalue with the given index. +The function returns @fail +if there is no upvalue with the given index. -Variable names starting with @Char{(} (open parenthesis) @C{)} -represent variables with no known names +(For Lua functions, +upvalues are the external local variables that the function uses, +and that are consequently included in its closure.) + +For @N{C functions}, this function uses the empty string @T{""} +as a name for all upvalues. + +Variable name @Char{?} (interrogation mark) +represents variables with no known names (variables from chunks saved without debug information). } @@ -8438,7 +9263,7 @@ to the userdata @id{u} plus a boolean, @LibEntry{debug.sethook ([thread,] hook, mask [, count])| -Sets the given function as a hook. +Sets the given function as the debug hook. The string @id{mask} and the number @id{count} describe when the hook will be called. The string mask may have any combination of the following characters, @@ -8457,16 +9282,15 @@ When called without arguments, When the hook is called, its first parameter is a string describing the event that has triggered its call: -@T{"call"} (or @T{"tail call"}), -@T{"return"}, +@T{"call"}, @T{"tail call"}, @T{"return"}, @T{"line"}, and @T{"count"}. For line events, the hook also gets the new line number as its second parameter. Inside a hook, you can call @id{getinfo} with @N{level 2} to get more information about -the running function -(@N{level 0} is the @id{getinfo} function, -and @N{level 1} is the hook function). +the running function. +(@N{Level 0} is the @id{getinfo} function, +and @N{level 1} is the hook function.) } @@ -8474,7 +9298,7 @@ and @N{level 1} is the hook function). This function assigns the value @id{value} to the local variable with index @id{local} of the function at level @id{level} of the stack. -The function returns @nil if there is no local +The function returns @fail if there is no local variable with the given index, and raises an error when called with a @id{level} out of range. (You can call @id{getinfo} to check whether the level is valid.) @@ -8497,10 +9321,12 @@ Returns @id{value}. This function assigns the value @id{value} to the upvalue with index @id{up} of the function @id{f}. -The function returns @nil if there is no upvalue +The function returns @fail if there is no upvalue with the given index. Otherwise, it returns the name of the upvalue. +See @Lid{debug.getupvalue} for more information about upvalues. + } @LibEntry{debug.setuservalue (udata, value, n)| @@ -8510,7 +9336,7 @@ the @id{n}-th user value associated to the given @id{udata}. @id{udata} must be a full userdata. Returns @id{udata}, -or @nil if the userdata does not have that value. +or @fail if the userdata does not have that value. } @@ -8564,50 +9390,57 @@ An interpreter for Lua as a standalone language, called simply @id{lua}, is provided with the standard distribution. The @x{standalone interpreter} includes -all standard libraries, including the debug library. +all standard libraries. Its usage is: @verbatim{ lua [options] [script [args]] } The options are: @description{ -@item{@T{-e @rep{stat}}| executes string @rep{stat};} -@item{@T{-l @rep{mod}}| @Q{requires} @rep{mod} and assigns the +@item{@T{-e @rep{stat}}| execute string @rep{stat};} +@item{@T{-i}| enter interactive mode after running @rep{script};} +@item{@T{-l @rep{mod}}| @Q{require} @rep{mod} and assign the result to global @rep{mod};} -@item{@T{-i}| enters interactive mode after running @rep{script};} -@item{@T{-v}| prints version information;} -@item{@T{-E}| ignores environment variables;} -@item{@T{--}| stops handling options;} -@item{@T{-}| executes @id{stdin} as a file and stops handling options.} +@item{@T{-l @rep{g=mod}}| @Q{require} @rep{mod} and assign the + result to global @rep{g};} +@item{@T{-v}| print version information;} +@item{@T{-E}| ignore environment variables;} +@item{@T{-W}| turn warnings on;} +@item{@T{--}| stop handling options;} +@item{@T{-}| execute @id{stdin} as a file and stop handling options.} } + After handling its options, @id{lua} runs the given @emph{script}. When called without arguments, @id{lua} behaves as @T{lua -v -i} when the standard input (@id{stdin}) is a terminal, and as @T{lua -} otherwise. -When called without option @T{-E}, -the interpreter checks for an environment variable @defid{LUA_INIT_5_4} +When called without the option @T{-E}, +the interpreter checks for an environment variable @defid{LUA_INIT_5_5} (or @defid{LUA_INIT} if the versioned name is not defined) before running any argument. If the variable content has the format @T{@At@rep{filename}}, then @id{lua} executes the file. Otherwise, @id{lua} executes the string itself. -When called with option @T{-E}, -besides ignoring @id{LUA_INIT}, -Lua also ignores -the values of @id{LUA_PATH} and @id{LUA_CPATH}, -setting the values of -@Lid{package.path} and @Lid{package.cpath} -with the default paths defined in @id{luaconf.h}. - -All options are handled in order, except @T{-i} and @T{-E}. +When called with the option @T{-E}, +Lua does not consult any environment variables. +In particular, +the values of @Lid{package.path} and @Lid{package.cpath} +are set with the default paths defined in @id{luaconf.h}. +To signal to the libraries that this option is on, +the stand-alone interpreter sets the field +@idx{"LUA_NOENV"} in the registry to a true value. +Other libraries may consult this field for the same purpose. + +The options @T{-e}, @T{-l}, and @T{-W} are handled in +the order they appear. For instance, an invocation like @verbatim{ -$ lua -e'a=1' -e 'print(a)' script.lua +$ lua -e 'a=1' -llib1 script.lua } -will first set @id{a} to 1, then print the value of @id{a}, +will first set @id{a} to 1, then require the library @id{lib1}, and finally run the file @id{script.lua} with no arguments. (Here @T{$} is the shell prompt. Your prompt may be different.) @@ -8641,19 +9474,39 @@ will print @St{-e}. If there is a script, the script is called with arguments @T{arg[1]}, @Cdots, @T{arg[#arg]}. -(Like all chunks in Lua, -the script is compiled as a vararg function.) +Like all chunks in Lua, +the script is compiled as a variadic function. In interactive mode, Lua repeatedly prompts and waits for a line. After reading a line, -Lua first try to interpret the line as an expression. +Lua first tries to interpret the line as an expression. If it succeeds, it prints its value. -Otherwise, it interprets the line as a statement. -If you write an incomplete statement, +Otherwise, it interprets the line as a chunk. +If you write an incomplete chunk, the interpreter waits for its completion by issuing a different prompt. +Note that, as each complete line is read as a new chunk, +local variables do not outlive lines. +To steer clear of confusion, +the interpreter gives a warning if a line starts with the +reserved word @Rw{local}: +@verbatim{ +> x = 20 -- global 'x' +> local x = 10; print(x) + --> warning: locals do not survive across lines in interactive mode + --> 10 +> print(x) -- back to global 'x' + --> 20 +> do -- incomplete chunk +>> local x = 10; print(x) -- '>>' prompts for line completion +>> print(x) +>> end -- chunk completed + --> 10 + --> 10 +} + If the global variable @defid{_PROMPT} contains a string, then its value is used as the prompt. Similarly, if the global variable @defid{_PROMPT2} contains a string, @@ -8667,6 +9520,8 @@ has a metamethod @idx{__tostring}, the interpreter calls this metamethod to produce the final message. Otherwise, the interpreter converts the error object to a string and adds a stack traceback to it. +When warnings are on, +they are simply printed in the standard error output. When finishing normally, the interpreter closes its main Lua state @@ -8676,41 +9531,50 @@ calling @Lid{os.exit} to terminate. To allow the use of Lua as a script interpreter in Unix systems, -the standalone interpreter skips -the first line of a chunk if it starts with @T{#}. +Lua skips the first line of a file chunk if it starts with @T{#}. Therefore, Lua scripts can be made into executable programs by using @T{chmod +x} and @N{the @T{#!}} form, as in @verbatim{ #!/usr/local/bin/lua } -(Of course, +Of course, the location of the Lua interpreter may be different in your machine. If @id{lua} is in your @id{PATH}, then @verbatim{ #!/usr/bin/env lua } -is a more portable solution.) +is a more portable solution. } @sect1{incompat| @title{Incompatibilities with the Previous Version} +@simplesect{ + Here we list the incompatibilities that you may find when moving a program -from @N{Lua 5.3} to @N{Lua 5.4}. +from @N{Lua 5.4} to @N{Lua 5.5}. + You can avoid some incompatibilities by compiling Lua with appropriate options (see file @id{luaconf.h}). However, all these compatibility options will be removed in the future. +More often than not, +compatibility issues arise when these compatibility options +are removed. +So, whenever you have the chance, +you should try to test your code with a version of Lua compiled +with all compatibility options turned off. +That will ease transitions to newer versions of Lua. Lua versions can always change the C API in ways that do not imply source-code changes in a program, such as the numeric values for constants or the implementation of functions as macros. Therefore, -you should not assume that binaries are compatible between +you should never assume that binaries are compatible between different Lua versions. Always recompile clients of the Lua API when using a new version. @@ -8722,117 +9586,96 @@ precompiled chunks are not compatible between different Lua versions. The standard paths in the official distribution may change between versions. -@sect2{@title{Changes in the Language} -@itemize{ - -@item{ -The coercion of strings to numbers in -arithmetic and bitwise operations -has been removed from the core language. -The string library does a similar job -for arithmetic (but not for bitwise) operations -using the string metamethods. -However, unlike in previous versions, -the new implementation preserves the implicit type of the numeral -in the string. -For instance, the result of @T{"1" + "2"} now is an integer, -not a float. } +@sect2{@title{Incompatibilities in the Language} +@itemize{ + @item{ -The use of the @idx{__lt} metamethod to emulate @id{__le} -has been removed. -When needed, this metamethod must be explicitly defined. +The word @Rw{global} is a reserved word. +Do not use it as a regular name. } @item{ -When a coroutine finishes with an error, -its stack is unwound (to run any pending closing methods). +The control variable in @Rw{for} loops is read only. +If you need to change it, +declare a local variable with the same name in the loop body. } @item{ -A label for a @Rw{goto} cannot be declared where a label with the same -name is visible, even if this other label is declared in an enclosing -block. +A chain of @id{__call} metamethods can have at most 15 objects. } @item{ -When finalizing an object, -Lua does not ignore @idx{__gc} metamethods that are not functions. -Any value will be called, if present. -(Non-callable values will generate a warning, -like any other error when calling a finalizer.) +In an error, a @nil as the error object is replaced by a +string message. } } } -@sect2{@title{Changes in the Libraries} +@sect2{@title{Incompatibilities in the Libraries} @itemize{ @item{ -The pseudo-random number generator used by the function @Lid{math.random} -now starts with a somewhat random seed. -Moreover, it uses a different algorithm. +Parameters for the garbage collection are not set +with the options @St{incremental} and @St{generational}; +instead, there is a new option @St{param} to that end. +Moreover, there were some changes in the parameters themselves. } -@item{ -The function @Lid{io.lines} now returns three extra values, -besides the iterator function. -You can enclose the call in parentheses if you need to -discard these extra results. } -@item{ -By default, the decoding functions in the @Lid{utf8} library -do not accept surrogates as valid code points. -An extra parameter in these functions makes them more permissive. } -} +@sect2{@title{Incompatibilities in the API} -} +@itemize{ -@sect2{@title{Changes in the API} +@item{ +In @Lid{lua_call} and related functions, +the maximum value for the number of required results +(@id{nresults}) is 250. +If you really need a larger value, +use @Lid{LUA_MULTRET} and then adjust the stack size. +Previously, this limit was unspecified. +} -@itemize{ +@item{ +@Lid{lua_newstate} has a third parameter, +a seed for the hashing of strings. +} @item{ -Full userdata now has an arbitrary number of associated user values. -Therefore, the functions @id{lua_newuserdata}, -@id{lua_setuservalue}, and @id{lua_getuservalue} were -replaced by @Lid{lua_newuserdatauv}, -@Lid{lua_setiuservalue}, and @Lid{lua_getiuservalue}, -which have an extra argument. +The function @id{lua_resetthread} is deprecated; +it is equivalent to @Lid{lua_closethread} with +@id{from} being @id{NULL}. +} -For compatibility, the old names still work as macros assuming -one single user value. -Note, however, that the call @T{lua_newuserdatauv(L,size,0)} -produces a smaller userdata. +@item{ +The function @id{lua_setcstacklimit} is deprecated. +Calls to it can simply be removed. } @item{ -The function @Lid{lua_resume} has an extra parameter. -This out parameter returns the number of values on -the top of the stack that were yielded or returned by the coroutine. -(In previous versions, -those values were the entire stack.) +The function @Lid{lua_dump} changed the way it keeps the stack +through the calls to the writer function. +(That was not specified in previous versions.) +Also, it calls the writer function one extra time, +to signal the end of the dump. } @item{ -The function @Lid{lua_version} returns the version number, -instead of an address of the version number. -(The Lua core should work correctly with libraries using their -own static copies of the same core, -so there is no need to check whether they are using the same -address space.) +Parameters for the garbage collection are not set +with the options @Lid{LUA_GCINC} and @Lid{LUA_GCGEN}; +instead, there is a new option @Lid{LUA_GCPARAM} to that end. +Moreover, there were some changes in the parameters themselves. } @item{ -The constant @id{LUA_ERRGCMM} was removed. -Errors in finalizers are never propagated; -instead, they generate a warning. +The function @Lid{lua_pushvfstring} now reports errors, +instead of raising them. } } @@ -8880,10 +9723,18 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @OrNL @Rw{for} namelist @Rw{in} explist @Rw{do} block @Rw{end} @OrNL @Rw{function} funcname funcbody @OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody -@OrNL @Rw{local} namelist @bnfopt{@bnfter{=} explist} -@OrNL @Rw{local} @bnfter{*} @bnfter{toclose} Name @bnfter{=} exp +@OrNL @Rw{global} @Rw{function} @bnfNter{Name} funcbody +@OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} +@OrNL @Rw{global} attnamelist +@OrNL @Rw{global} @bnfopt{attrib} @bnfter{*} } +@producname{attnamelist}@producbody{ + @bnfopt{attrib} @bnfNter{Name} @bnfopt{attrib} + @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}} + +@producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}} + @producname{retstat}@producbody{@Rw{return} @bnfopt{explist} @bnfopt{@bnfter{;}}} @@ -8936,8 +9787,10 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}} -@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} @bnfter{...}} - @Or @bnfter{...}} +@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or + varargparam} + +@producname{varargparam}@producbody{@bnfter{...} @bnfopt{@bnfNter{Name}}} @producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}} diff --git a/onelua.c b/onelua.c new file mode 100644 index 0000000000..e717121391 --- /dev/null +++ b/onelua.c @@ -0,0 +1,136 @@ +/* +** Lua core, libraries, and interpreter in a single file. +** Compiling just this file generates a complete Lua stand-alone +** program: +** +** $ gcc -O2 -std=c99 -o lua onelua.c -lm +** +** or (for C89) +** +** $ gcc -O2 -std=c89 -DLUA_USE_C89 -o lua onelua.c -lm +** +** or (for Linux) +** +** gcc -O2 -o lua -DLUA_USE_LINUX -Wl,-E onelua.c -lm -ldl +** +*/ + +/* default is to build the full interpreter */ +#ifndef MAKE_LIB +#ifndef MAKE_LUAC +#ifndef MAKE_LUA +#define MAKE_LUA +#endif +#endif +#endif + + +/* +** Choose suitable platform-specific features. Default is no +** platform-specific features. Some of these options may need extra +** libraries such as -ldl -lreadline -lncurses +*/ +#if 0 +#define LUA_USE_LINUX +#define LUA_USE_MACOSX +#define LUA_USE_POSIX +#endif + + +/* +** Other specific features +*/ +#if 0 +#define LUA_32BITS +#define LUA_USE_C89 +#endif + + +/* no need to change anything below this line ----------------------------- */ + +#include "lprefix.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* setup for luaconf.h */ +#define LUA_CORE +#define LUA_LIB + +#include "luaconf.h" + +/* do not export internal symbols */ +#undef LUAI_FUNC +#undef LUAI_DDEC +#undef LUAI_DDEF +#define LUAI_FUNC static +#define LUAI_DDEC(def) /* empty */ +#define LUAI_DDEF static + +/* core -- used by all */ +#include "lzio.c" +#include "lctype.c" +#include "lopcodes.c" +#include "lmem.c" +#include "lundump.c" +#include "ldump.c" +#include "lstate.c" +#include "lgc.c" +#include "llex.c" +#include "lcode.c" +#include "lparser.c" +#include "ldebug.c" +#include "lfunc.c" +#include "lobject.c" +#include "ltm.c" +#include "lstring.c" +#include "ltable.c" +#include "ldo.c" +#include "lvm.c" +#include "lapi.c" + +/* auxiliary library -- used by all */ +#include "lauxlib.c" + +/* standard library -- not used by luac */ +#ifndef MAKE_LUAC +#include "lbaselib.c" +#include "lcorolib.c" +#include "ldblib.c" +#include "liolib.c" +#include "lmathlib.c" +#include "loadlib.c" +#include "loslib.c" +#include "lstrlib.c" +#include "ltablib.c" +#include "lutf8lib.c" +#include "linit.c" +#endif + +/* test library -- used only for internal development */ +#if defined(LUA_DEBUG) +#include "ltests.c" +#endif + +/* lua */ +#ifdef MAKE_LUA +#include "lua.c" +#endif + +/* luac */ +#ifdef MAKE_LUAC +#include "luac.c" +#endif diff --git a/testes/all.lua b/testes/all.lua old mode 100644 new mode 100755 index 8d727b6b21..d3e2f12368 --- a/testes/all.lua +++ b/testes/all.lua @@ -1,12 +1,16 @@ #!../lua -- $Id: testes/all.lua $ --- See Copyright Notice at the end of this file +-- See Copyright Notice in file lua.h +global * -local version = "Lua 5.4" +global _soft, _port, _nomsg +global T + +local version = "Lua 5.5" if _VERSION ~= version then - warn(string.format( - "This test suite is for %s, not for %s\nExiting tests", version, _VERSION)) + io.stderr:write("This test suite is for ", version, + ", not for ", _VERSION, "\nExiting tests") return end @@ -28,16 +32,13 @@ _nomsg = rawget(_G, "_nomsg") or false local usertests = rawget(_G, "_U") if usertests then - -- tests for sissies ;) Avoid problems - _soft = true - _port = true - _nomsg = true + _soft = true -- avoid tests that take too long + _port = true -- avoid non-portable tests + _nomsg = true -- avoid messages about tests not performed end -- tests should require debug when needed -debug = nil - -require"bwcoercion" +global debug; debug = nil if usertests then @@ -46,7 +47,6 @@ else T = rawget(_G, "T") -- avoid problems with 'strict' module end -math.randomseed(0) --[=[ example of a long [comment], @@ -54,6 +54,14 @@ math.randomseed(0) ]=] +print("\n\tStarting Tests") + +do + -- set random seed + local random_x, random_y = math.randomseed() + print(string.format("random seeds: %d, %d", random_x, random_y)) +end + print("current path:\n****" .. package.path .. "****\n") @@ -67,7 +75,7 @@ do -- ( -- track messages for tests not performed local msgs = {} -function Message (m) +global function Message (m) if not _nomsg then print(m) msgs[#msgs+1] = string.sub(m, 3, -3) @@ -95,6 +103,8 @@ local function F (m) end end +local Cstacklevel + local showmem if not T then local max = 0 @@ -104,6 +114,7 @@ if not T then print(format(" ---- total memory: %s, max memory: %s ----\n", F(m), F(max))) end + Cstacklevel = function () return 0 end -- no info about stack level else showmem = function () T.checkmemory() @@ -117,9 +128,16 @@ else T.totalmem"string", T.totalmem"table", T.totalmem"function", T.totalmem"userdata", T.totalmem"thread")) end + + Cstacklevel = function () + local _, _, ncalls = T.stacklevel() + return ncalls -- number of C calls + end end +local Cstack = Cstacklevel() + -- -- redefine dofile to run files through dump/undump -- @@ -139,18 +157,8 @@ end dofile('main.lua') -do - local next, setmetatable, stderr = next, setmetatable, io.stderr - -- track collections - local mt = {} - -- each time a table is collected, remark it for finalization - -- on next cycle - mt.__gc = function (o) - stderr:write'.' -- mark progress - local n = setmetatable(o, mt) -- remark it - end - local n = setmetatable({}, mt) -- create object -end +-- trace GC cycles +require"tracegc".start() report"gc.lua" local f = assert(loadfile('gc.lua')) @@ -158,6 +166,7 @@ f() dofile('db.lua') assert(dofile('calls.lua') == deep and deep) +_G.deep = nil olddofile('strings.lua') olddofile('literals.lua') dofile('tpack.lua') @@ -177,6 +186,7 @@ dofile('nextvar.lua') dofile('pm.lua') dofile('utf8.lua') dofile('api.lua') +dofile('memerr.lua') assert(dofile('events.lua') == 12) dofile('vararg.lua') dofile('closure.lua') @@ -190,16 +200,18 @@ assert(dofile('verybig.lua', true) == 10); collectgarbage() dofile('files.lua') if #msgs > 0 then - warn("#tests not performed:", true) - for i=1,#msgs do - warn("\n ", true); warn(msgs[i], true) - end - warn("\n") + local m = table.concat(msgs, "\n ") + warn("#tests not performed:\n ", m, "\n") end print("(there should be two warnings now)") -warn("#This is ", true); warn("an expected", true); warn(" warning") -warn("#This is", true); warn(" another one") +warn("@on") +warn("#This is ", "an expected", " warning") +warn("@off") +warn("******** THIS WARNING SHOULD NOT APPEAR **********") +warn("******** THIS WARNING ALSO SHOULD NOT APPEAR **********") +warn("@on") +warn("#This is", " another one") -- no test module should define 'debug' assert(debug == nil) @@ -214,11 +226,16 @@ debug.sethook(function (a) assert(type(a) == 'string') end, "cr") -- to survive outside block _G.showmem = showmem + +assert(Cstack == Cstacklevel(), + "should be at the same C-stack level it was when started the tests") + end --) -local _G, showmem, print, format, clock, time, difftime, assert, open = +local _G, showmem, print, format, clock, time, difftime, + assert, open, warn = _G, showmem, print, string.format, os.clock, os.time, os.difftime, - assert, io.open + assert, io.open, warn -- file with time of last performed test local fname = T and "time-debug.txt" or "time.txt" @@ -262,7 +279,7 @@ if not usertests then local diff = (clocktime - lasttime) / lasttime local tolerance = 0.05 -- 5% if (diff >= tolerance or diff <= -tolerance) then - print(format("WARNING: time difference from previous test: %+.1f%%", + warn(format("#time difference from previous test: %+.1f%%", diff * 100)) end assert(open(fname, "w")):write(clocktime):close() @@ -270,30 +287,3 @@ end print("final OK !!!") - - ---[[ -***************************************************************************** -* Copyright (C) 1994-2016 Lua.org, PUC-Rio. -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be -* included in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -***************************************************************************** -]] - diff --git a/testes/api.lua b/testes/api.lua index 08672e8abe..9855f5411d 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -1,5 +1,5 @@ -- $Id: testes/api.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h if T==nil then (Message or print)('\n >>> testC not active: skipping API tests <<<\n') @@ -11,7 +11,7 @@ local debug = require "debug" local pack = table.pack -function tcheck (t1, t2) +local function tcheck (t1, t2) assert(t1.n == (t2.n or #t2) + 1) for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end end @@ -25,7 +25,7 @@ end print('testing C API') -a = T.testC("pushvalue R; return 1") +local a = T.testC("pushvalue R; return 1") assert(a == debug.getregistry()) @@ -40,10 +40,10 @@ a = T.d2s(12458954321123.0) assert(a == string.pack("d", 12458954321123.0)) assert(T.s2d(a) == 12458954321123.0) -a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") +local a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") assert(a == 2 and b == 3 and not c) -f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") +local f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") a,b,c = f() assert(a == 2 and b == 3 and not c) @@ -58,7 +58,7 @@ assert(a==false and b==true and c==false) a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40) assert(a == 40 and b == 5 and not c) -t = pack(T.testC("settop 5; return *", 2, 3)) +local t = pack(T.testC("settop 5; return *", 2, 3)) tcheck(t, {n=4,2,3}) t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23)) @@ -114,7 +114,7 @@ end -- testing warnings T.testC([[ - warningC "#This shold be a" + warningC "#This should be a" warningC " single " warning "warning" warningC "#This should be " @@ -162,17 +162,35 @@ do -- test returning more results than fit in the caller stack end +do -- testing multiple returns + local function foo (n) + if n > 0 then return n, foo(n - 1) end + end + + local t = {T.testC("call 1 10; return 10", foo, 20)} + assert(t[1] == 20 and t[10] == 11 and t[11] == nil) + + local t = table.pack(T.testC("call 1 10; return 10", foo, 2)) + assert(t[1] == 2 and t[2] == 1 and t[3] == nil and t.n == 10) + + local t = {T.testC([[ + checkstack 300 "error"; call 1 250; return 250]], foo, 250)} + assert(t[1] == 250 and t[250] == 1 and t[251] == nil) +end + + -- testing globals -_G.a = 14; _G.b = "a31" +_G.AA = 14; _G.BB = "a31" local a = {T.testC[[ - getglobal a; - getglobal b; - getglobal b; - setglobal a; + getglobal AA; + getglobal BB; + getglobal BB; + setglobal AA; return * ]]} -assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31") +assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.AA == "a31") +_G.AA, _G.BB = nil -- testing arith assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5) @@ -196,13 +214,14 @@ a,b,c = T.testC([[pushnum 1; pushstring 10; arith _; pushstring 5; return 3]]) assert(a == 1 and b == -10 and c == "5") -mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, +local mt = { + __add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end, __unm = function (a) return setmetatable({a[1]* 2}, mt) end} a,b,c = setmetatable({4}, mt), setmetatable({8}, mt), setmetatable({-3}, mt) -x,y,z = T.testC("arith +; return 2", 10, a, b) +local x,y,z = T.testC("arith +; return 2", 10, a, b) assert(x == 10 and y[1] == 12 and z == nil) assert(T.testC("arith %; return 1", a, c)[1] == 4%-3) assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] == @@ -227,7 +246,8 @@ assert(not T.testC("compare LT 1 4, return 1")) assert(not T.testC("compare LE 9 1, return 1")) assert(not T.testC("compare EQ 9 9, return 1")) -local b = {__lt = function (a,b) return a[1] < b[1] end} +local b = {__lt = function (a,b) return a[1] < b[1] end, + __le = function (a,b) return a[1] <= b[1] end} local a1,a3,a4 = setmetatable({1}, b), setmetatable({3}, b), setmetatable({4}, b) @@ -241,6 +261,23 @@ assert(a == 20 and b == false) a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20) assert(a == 20 and b == true) + +do -- testing lessthan and lessequal with metamethods + local mt = {__lt = function (a,b) return a[1] < b[1] end, + __le = function (a,b) return a[1] <= b[1] end, + __eq = function (a,b) return a[1] == b[1] end} + local function O (x) + return setmetatable({x}, mt) + end + + local a, b = T.testC("compare LT 2 3; pushint 10; return 2", O(1), O(2)) + assert(a == true and b == 10) + local a, b = T.testC("compare LE 2 3; pushint 10; return 2", O(3), O(2)) + assert(a == false and b == 10) + local a, b = T.testC("compare EQ 2 3; pushint 10; return 2", O(3), O(3)) + assert(a == true and b == 10) +end + -- testing length local t = setmetatable({x = 20}, {__len = function (t) return t.x end}) a,b,c = T.testC([[ @@ -292,9 +329,9 @@ assert(T.testC("concat 1; return 1", "xuxu") == "xuxu") -- testing lua_is -function B(x) return x and 1 or 0 end +local function B (x) return x and 1 or 0 end -function count (x, n) +local function count (x, n) n = n or 2 local prog = [[ isnumber %d; @@ -325,7 +362,7 @@ assert(count(nil, 15) == 100) -- testing lua_to... -function to (s, x, n) +local function to (s, x, n) n = n or 2 return T.testC(string.format("%s %d; return 1", s, n), x) end @@ -354,8 +391,11 @@ assert(to("topointer", nil) == null) assert(to("topointer", "abc") ~= null) assert(to("topointer", string.rep("x", 10)) == to("topointer", string.rep("x", 10))) -- short strings -assert(to("topointer", string.rep("x", 300)) ~= - to("topointer", string.rep("x", 300))) -- long strings +do -- long strings + local s1 = string.rep("x", 300) + local s2 = string.rep("x", 300) + assert(to("topointer", s1) ~= to("topointer", s2)) +end assert(to("topointer", T.pushuserdata(20)) ~= null) assert(to("topointer", io.read) ~= null) -- light C function assert(to("topointer", hfunc) ~= null) -- "heavy" C function @@ -374,6 +414,10 @@ do -- trivial error assert(T.checkpanic("pushstring hi; error") == "hi") + -- thread status inside panic (bug in 5.4.4) + assert(T.checkpanic("pushstring hi; error", "threadstatus; return 2") == + "ERRRUN") + -- using the stack inside panic assert(T.checkpanic("pushstring hi; error;", [[checkstack 5 XX @@ -382,20 +426,30 @@ do concat 3]]) == "hi alo mundo") -- "argerror" without frames - assert(T.checkpanic("loadstring 4") == + assert(T.checkpanic("loadstring 4 name bt") == "bad argument #4 (string expected, got no value)") - -- memory error - T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k) - assert(T.checkpanic("newuserdata 20000") == "not enough memory") - T.totalmem(0) -- restore high limit + -- memory error + thread status + local x = T.checkpanic( + [[ alloccount 0 # force a memory error in next line + newtable + ]], + [[ + alloccount -1 # allow free allocations again + pushstring XX + threadstatus + concat 2 # to make sure message came from here + return 1 + ]]) + T.alloccount() + assert(x == "XX" .. "not enough memory") -- stack error if not _soft then local msg = T.checkpanic[[ pushstring "function f() f() end" - loadstring -1; call 0 0 + loadstring -1 name t; call 0 0 getglobal f; call 0 0 ]] assert(string.find(msg, "stack overflow")) @@ -405,7 +459,7 @@ do assert(T.checkpanic([[ pushstring "return {__close = function () Y = 'ho'; end}" newtable - loadstring -2 + loadstring -2 name t call 0 1 setmetatable -2 toclose -1 @@ -433,6 +487,8 @@ if not _soft then print'+' end + + local lim = _soft and 500 or 12000 local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"} for i = 1,lim do @@ -440,7 +496,7 @@ for i = 1,lim do prog[#prog + 1] = "pushnum " .. i * 10 end -prog[#prog + 1] = "rawgeti R 2" -- get global table in registry +prog[#prog + 1] = "rawgeti R !G" -- get global table in registry prog[#prog + 1] = "insert " .. -(2*lim + 2) for i = 1,lim do @@ -456,38 +512,99 @@ for i = 1,lim do assert(t[i] == i*10); t[i] = undef end assert(next(t) == nil) prog, g, t = nil +do -- shrink stack + local m1, m2 = 0, collectgarbage"count" * 1024 + while m1 ~= m2 do -- repeat until stable + collectgarbage() + m1 = m2 + m2 = collectgarbage"count" * 1024 + end +end + + -- testing errors a = T.testC([[ - loadstring 2; pcall 0 1 0; + loadstring 2 name t; pcall 0 1 0; pushvalue 3; insert -2; pcall 1 1 0; pcall 0 0 0; return 1 -]], "x=150", function (a) assert(a==nil); return 3 end) +]], "XX=150", function (a) assert(a==nil); return 3 end) -assert(type(a) == 'string' and x == 150) +assert(type(a) == 'string' and XX == 150) +_G.XX = nil -function check3(p, ...) +local function check3(p, ...) local arg = {...} assert(#arg == 3) assert(string.find(arg[3], p)) end -check3(":1:", T.testC("loadstring 2; return *", "x=")) +check3(":1:", T.testC("loadstring 2 name t; return *", "x=")) check3("%.", T.testC("loadfile 2; return *", ".")) check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) -- test errors in non protected threads -function checkerrnopro (code, msg) +local function checkerrnopro (code, msg) local th = coroutine.create(function () end) -- create new thread local stt, err = pcall(T.testC, th, code) -- run code there assert(not stt and string.find(err, msg)) end + +do + print("testing load of binaries in fixed buffers") + local source = {} + local N = 1000 + -- create a somewhat "large" source + for i = 1, N do source[i] = "X = X + 1; " end + -- add a long string to the source + source[#source + 1] = string.format("Y = '%s'", string.rep("a", N)); + source = table.concat(source) + -- give chunk an explicit name to avoid using source as name + source = load(source, "name1") + -- dump without debug information + source = string.dump(source, true) + -- each "X=X+1" generates 4 opcodes with 4 bytes each, plus the string + assert(#source > N * 4 * 4 + N) + collectgarbage(); collectgarbage() + local m1 = collectgarbage"count" * 1024 + -- load dump using fixed buffer + local code = T.testC([[ + loadstring 2 name B; + return 1 + ]], source) + collectgarbage() + local m2 = collectgarbage"count" * 1024 + -- load used fewer than 400 bytes. Code alone has more than 3*N bytes, + -- and string literal has N bytes. Both were not loaded. + assert(m2 > m1 and m2 - m1 < 400) + X = 0; code(); assert(X == N and Y == string.rep("a", N)) + X = nil; Y = nil + + -- testing debug info in fixed buffers + source = {"X = 0"} + for i = 2, 300 do source[i] = "X = X + 1" end + source[#source + 1] = "X = X + {}" -- error in last line + source = table.concat(source, "\n") + source = load(source, "name1") + source = string.dump(source) + -- load dump using fixed buffer + local code = T.testC([[ + loadstring 2 name B; + return 1 + ]], source) + checkerr(":301:", code) -- correct line information +end + + if not _soft then + collectgarbage("stop") -- avoid __gc with full stack checkerrnopro("pushnum 3; call 0 0", "attempt to call") print"testing stack overflow in unprotected thread" - function f () f() end - checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow") + function F () F() end + checkerrnopro("getglobal 'F'; call 0 0;", "stack overflow") + F = nil + collectgarbage("restart") end print"+" @@ -496,9 +613,57 @@ print"+" do -- getp/setp local a = {} - T.testC("rawsetp 2 1", a, 20) + local a1 = T.testC("rawsetp 2 1; return 1", a, 20) + assert(a == a1) assert(a[T.pushuserdata(1)] == 20) - assert(T.testC("rawgetp 2 1; return 1", a) == 20) + local a1, res = T.testC("rawgetp -1 1; return 2", a) + assert(a == a1 and res == 20) +end + + +do -- using the table itself as index + local a = {} + a[a] = 10 + local prog = "gettable -1; return *" + local res = {T.testC(prog, a)} + assert(#res == 2 and res[1] == prog and res[2] == 10) + + local prog = "settable -2; return *" + local res = {T.testC(prog, a, 20)} + assert(a[a] == 20) + assert(#res == 1 and res[1] == prog) + + -- raw + a[a] = 10 + local prog = "rawget -1; return *" + local res = {T.testC(prog, a)} + assert(#res == 2 and res[1] == prog and res[2] == 10) + + local prog = "rawset -2; return *" + local res = {T.testC(prog, a, 20)} + assert(a[a] == 20) + assert(#res == 1 and res[1] == prog) + + -- using the table as the value to set + local prog = "rawset -1; return *" + local res = {T.testC(prog, 30, a)} + assert(a[30] == a) + assert(#res == 1 and res[1] == prog) + + local prog = "settable -1; return *" + local res = {T.testC(prog, 40, a)} + assert(a[40] == a) + assert(#res == 1 and res[1] == prog) + + local prog = "rawseti -1 100; return *" + local res = {T.testC(prog, a)} + assert(a[100] == a) + assert(#res == 1 and res[1] == prog) + + local prog = "seti -1 200; return *" + local res = {T.testC(prog, a)} + assert(a[200] == a) + assert(#res == 1 and res[1] == prog) end a = {x=0, y=12} @@ -515,7 +680,7 @@ assert(a[a] == "x") b = setmetatable({p = a}, {}) getmetatable(b).__index = function (t, i) return t.p[i] end -k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") +local k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") assert(x == 15 and k == 35) k = T.testC("getfield 2 y, return 1", b) assert(k == 12) @@ -632,7 +797,7 @@ for k, v in ipairs(t) do assert(v1 == v and p) end -assert(debug.getuservalue(4) == nil) +assert(not debug.getuservalue(4)) debug.setuservalue(b, function () return 10 end, 10) collectgarbage() -- function should not be collected @@ -646,7 +811,7 @@ assert(debug.getuservalue(b) == 134) -- test barrier for uservalues do local oldmode = collectgarbage("incremental") - T.gcstate("atomic") + T.gcstate("enteratomic") assert(T.gccolor(b) == "black") debug.setuservalue(b, {x = 100}) T.gcstate("pause") -- complete collection @@ -675,8 +840,8 @@ local i = T.ref{} T.unref(i) assert(T.ref{} == i) -Arr = {} -Lim = 100 +local Arr = {} +local Lim = 100 for i=1,Lim do -- lock many objects Arr[i] = T.ref({}) end @@ -688,7 +853,7 @@ for i=1,Lim do -- unlock all them T.unref(Arr[i]) end -function printlocks () +local function printlocks () local f = T.makeCfunc("gettable R; return 1") local n = f("n") print("n", n) @@ -720,8 +885,8 @@ assert(type(T.getref(a)) == 'table') -- colect in cl the `val' of all collected userdata -tt = {} -cl = {n=0} +local tt = {} +local cl = {n=0} A = nil; B = nil local F F = function (x) @@ -731,20 +896,20 @@ F = function (x) d = nil assert(debug.getmetatable(x).__gc == F) assert(load("table.insert({}, {})"))() -- create more garbage - collectgarbage() -- force a GC during GC - assert(debug.getmetatable(x).__gc == F) -- previous GC did not mess this? + assert(not collectgarbage()) -- GC during GC (no op) local dummy = {} -- create more garbage during GC if A ~= nil then assert(type(A) == "userdata") assert(T.udataval(A) == B) - debug.getmetatable(A) -- just acess it + debug.getmetatable(A) -- just access it end - A = x -- ressucita userdata + A = x -- resurrect userdata B = udval return 1,2,3 end tt.__gc = F + -- test whether udate collection frees memory in the right time do collectgarbage(); @@ -781,9 +946,9 @@ end collectgarbage("stop") -- create 3 userdatas with tag `tt' -a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a) -b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b) -c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c) +a = T.newuserdata(0); debug.setmetatable(a, tt); local na = T.udataval(a) +b = T.newuserdata(0); debug.setmetatable(b, tt); local nb = T.udataval(b) +c = T.newuserdata(0); debug.setmetatable(c, tt); local nc = T.udataval(c) -- create userdata without meta table x = T.newuserdata(4) @@ -794,29 +959,31 @@ checkerr("FILE%* expected, got userdata", io.input, x) assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) -d=T.ref(a); -e=T.ref(b); -f=T.ref(c); -t = {T.getref(d), T.getref(e), T.getref(f)} +-- Test references in an arbitrary table +local reftable = {} +local d = T.ref(a, reftable); +local e = T.ref(b, reftable); +local f = T.ref(c, reftable); +t = {T.getref(d, reftable), T.getref(e, reftable), T.getref(f, reftable)} assert(t[1] == a and t[2] == b and t[3] == c) t=nil; a=nil; c=nil; -T.unref(e); T.unref(f) +T.unref(e, reftable); T.unref(f, reftable) collectgarbage() -- check that unref objects have been collected assert(#cl == 1 and cl[1] == nc) -x = T.getref(d) +x = T.getref(d, reftable) assert(type(x) == 'userdata' and debug.getmetatable(x) == tt) x =nil tt.b = b -- create cycle tt=nil -- frees tt for GC A = nil b = nil -T.unref(d); -n5 = T.newuserdata(0) +T.unref(d, reftable); +local n5 = T.newuserdata(0) debug.setmetatable(n5, {__gc=F}) n5 = T.udataval(n5) collectgarbage() @@ -824,6 +991,21 @@ assert(#cl == 4) -- check order of collection assert(cl[2] == n5 and cl[3] == nb and cl[4] == na) +-- reuse a reference in 'reftable' +T.unref(T.ref(23, reftable), reftable) + +do -- check reftable + local count = 0 + local i = 1 + while reftable[i] ~= 0 do + i = reftable[i] -- traverse linked list of free references + count = count + 1 + end + -- maximum number of simultaneously locked objects was 3 + assert(count == 3 and #reftable == 3 + 1) -- +1 for reserved [1] +end + + collectgarbage"restart" @@ -887,11 +1069,11 @@ print'+' -- testing changing hooks during hooks -_G.t = {} +_G.TT = {} T.sethook([[ # set a line hook after 3 count hooks sethook 4 0 ' - getglobal t; + getglobal TT; pushvalue -3; append -2 pushvalue -2; append -2 ']], "c", 3) @@ -901,16 +1083,18 @@ a = 1 -- count hook (set line hook) a = 1 -- line hook a = 1 -- line hook debug.sethook() -t = _G.t +local t = _G.TT assert(t[1] == "line") -line = t[2] +local line = t[2] assert(t[3] == "line" and t[4] == line + 1) assert(t[5] == "line" and t[6] == line + 2) assert(t[7] == nil) +_G.TT = nil ------------------------------------------------------------------------- do -- testing errors during GC + warn("@off") collectgarbage("stop") local a = {} for i=1,20 do @@ -928,7 +1112,9 @@ do -- testing errors during GC collectgarbage() assert(A == 10) -- number of normal collections collectgarbage("restart") + warn("@on") end +_G.A = nil ------------------------------------------------------------------------- -- test for userdata vals do @@ -958,17 +1144,19 @@ assert(a == 'alo' and b == '3') T.doremote(L1, "_ERRORMESSAGE = nil") -- error: `sin' is not defined -a, _, b = T.doremote(L1, "return sin(1)") -assert(a == nil and b == 2) -- 2 == run-time error +a, b, c = T.doremote(L1, "return sin(1)") +assert(a == nil and c == 2) -- 2 == run-time error -- error: syntax error a, b, c = T.doremote(L1, "return a+") assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error -T.loadlib(L1) +T.loadlib(L1, 2, ~2) -- load only 'package', preload all others a, b, c = T.doremote(L1, [[ string = require'string' - a = require'_G'; assert(a == _G and require("_G") == a) + local initialG = _G -- not loaded yet + local a = require'_G'; assert(a == _G and require("_G") == a) + assert(initialG == nil and io == nil) -- now we have 'assert' io = require'io'; assert(type(io.read) == "function") assert(require("io") == io) a = require'table'; assert(type(a.insert) == "function") @@ -982,7 +1170,7 @@ T.closestate(L1); L1 = T.newstate() -T.loadlib(L1) +T.loadlib(L1, 0, 0) T.doremote(L1, "a = {}") T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1; settable -3]]) @@ -1030,35 +1218,72 @@ do assert(type(a[1]) == "string" and a[2][1] == 11) assert(#openresource == 0) -- was closed - -- error + -- closing by error local a, b = pcall(T.makeCfunc[[ call 0 1 # create resource toclose -1 # mark it to be closed - error # resource is the error object + pushvalue -1 # replicate it as error object + error # resource right after error object ]], newresource) assert(a == false and b[1] == 11) assert(#openresource == 0) -- was closed + -- non-closable value + local a, b = pcall(T.makeCfunc[[ + newtable # create non-closable object + toclose -1 # mark it to be closed (should raise an error) + abort # will not be executed + ]]) + assert(a == false and + string.find(b, "non%-closable value")) + local function check (n) assert(#openresource == n) end - -- closing resources with 'settop' + -- closing resources with 'closeslot' + _ENV.xxx = true local a = T.testC([[ - pushvalue 2 - call 0 1 # create resource + pushvalue 2 # stack: S, NR, CH, NR + call 0 1 # create resource; stack: S, NR, CH, R + toclose -1 # mark it to be closed + pushvalue 2 # stack: S, NR, CH, R, NR + call 0 1 # create another resource; stack: S, NR, CH, R, R + toclose -1 # mark it to be closed + pushvalue 3 # stack: S, NR, CH, R, R, CH + pushint 2 # there should be two open resources + call 1 0 # stack: S, NR, CH, R, R + closeslot -1 # close second resource + pushvalue 3 # stack: S, NR, CH, R, R, CH + pushint 1 # there should be one open resource + call 1 0 # stack: S, NR, CH, R, R + closeslot 4 + setglobal "xxx" # previous op. erased the slot + pop 1 # pop other resource from the stack + pushint * + return 1 # return stack size + ]], newresource, check) + assert(a == 3 and _ENV.xxx == nil) -- no extra items left in the stack + + -- closing resources with 'pop' + local a = T.testC([[ + pushvalue 2 # stack: S, NR, CH, NR + call 0 1 # create resource; stack: S, NR, CH, R toclose -1 # mark it to be closed - pushvalue 2 - call 0 1 # create another resource + pushvalue 2 # stack: S, NR, CH, R, NR + call 0 1 # create another resource; stack: S, NR, CH, R, R toclose -1 # mark it to be closed - pushvalue 3 + pushvalue 3 # stack: S, NR, CH, R, R, CH pushint 2 # there should be two open resources - call 1 0 - pop 1 # pop second resource from the stack - pushvalue 3 + call 1 0 # stack: S, NR, CH, R, R + pop 1 # pop second resource + pushvalue 3 # stack: S, NR, CH, R, CH pushint 1 # there should be one open resource - call 1 0 - pop 1 # pop second resource from the stack + call 1 0 # stack: S, NR, CH, R + pop 1 # pop other resource from the stack + pushvalue 3 # stack: S, NR, CH, CH + pushint 0 # there should be no open resources + call 1 0 # stack: S, NR, CH pushint * return 1 # return stack size ]], newresource, check) @@ -1074,197 +1299,6 @@ do end -------------------------------------------------------------------------- --- testing memory limits -------------------------------------------------------------------------- -print("memory-allocation errors") - -checkerr("block too big", T.newuserdata, math.maxinteger) -collectgarbage() -local f = load"local a={}; for i=1,100000 do a[i]=i end" -T.alloccount(10) -checkerr("not enough memory", f) -T.alloccount() -- remove limit - --- test memory errors; increase limit for number of allocations one --- by one, so that we get memory errors in all allocations of a given --- task, until there is enough allocations to complete the task without --- errors. - -function testamem (s, f) - collectgarbage(); collectgarbage() - local M = 0 - local a,b = nil - while true do - T.alloccount(M) - a, b = pcall(f) - T.alloccount() -- remove limit - if a and b then break end -- stop when no more errors - if not a and not -- `real' error? - (string.find(b, "memory") or string.find(b, "overflow")) then - error(b, 0) -- propagate it - end - M = M + 1 -- increase allocation limit - end - print(string.format("limit for %s: %d allocations", s, M)) - return b -end - - --- doing nothing -b = testamem("doing nothing", function () return 10 end) -assert(b == 10) - --- testing memory errors when creating a new state - -b = testamem("state creation", T.newstate) -T.closestate(b); -- close new state - -testamem("empty-table creation", function () - return {} -end) - -testamem("string creation", function () - return "XXX" .. "YYY" -end) - -testamem("coroutine creation", function() - return coroutine.create(print) -end) - - --- testing to-be-closed variables -testamem("to-be-closed variables", function() - local flag - do - local *toclose x = setmetatable({}, {__close = function () flag = true end}) - flag = false - local x = {} - end - return flag -end) - - --- testing threads - --- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) -mt = T.testC("rawgeti R 1; return 1") -assert(type(mt) == "thread" and coroutine.running() == mt) - - - -function expand (n,s) - if n==0 then return "" end - local e = string.rep("=", n) - return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", - e, s, expand(n-1,s), e) -end - -G=0; collectgarbage(); a =collectgarbage("count") -load(expand(20,"G=G+1"))() -assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) - -testamem("running code on new thread", function () - return T.doonnewstack("x=1") == 0 -- try to create thread -end) - - --- testing memory x compiler - -testamem("loadstring", function () - return load("x=1") -- try to do load a string -end) - - -local testprog = [[ -local function foo () return end -local t = {"x"} -a = "aaa" -for i = 1, #t do a=a..t[i] end -return true -]] - --- testing memory x dofile -_G.a = nil -local t =os.tmpname() -local f = assert(io.open(t, "w")) -f:write(testprog) -f:close() -testamem("dofile", function () - local a = loadfile(t) - return a and a() -end) -assert(os.remove(t)) -assert(_G.a == "aaax") - - --- other generic tests - -testamem("gsub", function () - local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end) - return (a == 'ablo ablo') -end) - -testamem("dump/undump", function () - local a = load(testprog) - local b = a and string.dump(a) - a = b and load(b) - return a and a() -end) - -local t = os.tmpname() -testamem("file creation", function () - local f = assert(io.open(t, 'w')) - assert (not io.open"nomenaoexistente") - io.close(f); - return not loadfile'nomenaoexistente' -end) -assert(os.remove(t)) - -testamem("table creation", function () - local a, lim = {}, 10 - for i=1,lim do a[i] = i; a[i..'a'] = {} end - return (type(a[lim..'a']) == 'table' and a[lim] == lim) -end) - -testamem("constructors", function () - local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5} - return (type(a) == 'table' and a.e == 5) -end) - -local a = 1 -close = nil -testamem("closure creation", function () - function close (b) - return function (x) return b + x end - end - return (close(2)(4) == 6) -end) - -testamem("using coroutines", function () - local a = coroutine.wrap(function () - coroutine.yield(string.rep("a", 10)) - return {} - end) - assert(string.len(a()) == 10) - return a() -end) - -do -- auxiliary buffer - local lim = 100 - local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end - testamem("auxiliary buffer", function () - return (#table.concat(a, ",") == 20*lim + lim - 1) - end) -end - -testamem("growing stack", function () - local function foo (n) - if n == 0 then return 1 else return 1 + foo(n - 1) end - end - return foo(100) -end) - do -- testing failing in 'lua_checkstack' local res = T.testC([[rawcheckstack 500000; return 1]]) assert(res == false) @@ -1285,10 +1319,10 @@ end do -- garbage collection with no extra memory local L = T.newstate() - T.loadlib(L) + T.loadlib(L, 1 | 2, 0) -- load _G and 'package' local res = (T.doremote(L, [[ - _ENV = require"_G" - local T = require"T" + _ENV = _G + assert(string == nil) local a = {} for i = 1, 1000 do a[i] = 'i' .. i end -- grow string table local stsize, stuse = T.querystr() diff --git a/testes/attrib.lua b/testes/attrib.lua index dcafd6345d..f415608699 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -1,5 +1,5 @@ -- $Id: testes/attrib.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print "testing require" @@ -28,7 +28,7 @@ do local path = table.concat(t, ";") -- use that path in a search local s, err = package.searchpath("xuxu", path) - -- search fails; check that message has an occurence of + -- search fails; check that message has an occurrence of -- '??????????' with ? replaced by xuxu and at least 'max' lines assert(not s and string.find(err, string.rep("xuxu", 10)) and @@ -47,6 +47,29 @@ do package.path = oldpath end + +do print"testing 'require' message" + local oldpath = package.path + local oldcpath = package.cpath + + package.path = "?.lua;?/?" + package.cpath = "?.so;?/init" + + local st, msg = pcall(require, 'XXX') + + local expected = [[module 'XXX' not found: + no field package.preload['XXX'] + no file 'XXX.lua' + no file 'XXX/XXX' + no file 'XXX.so' + no file 'XXX/init']] + + assert(msg == expected) + + package.path = oldpath + package.cpath = oldcpath +end + print('+') @@ -62,7 +85,7 @@ local DIR = "libs" .. dirsep -- prepend DIR to a name and correct directory separators local function D (x) - x = string.gsub(x, "/", dirsep) + local x = string.gsub(x, "/", dirsep) return DIR .. x end @@ -83,7 +106,7 @@ local function createfiles (files, preextras, posextras) end end -function removefiles (files) +local function removefiles (files) for n in pairs(files) do os.remove(D(n)) end @@ -122,18 +145,18 @@ local oldpath = package.path package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR) -local try = function (p, n, r) +local try = function (p, n, r, ext) NAME = nil - local rr = require(p) + local rr, x = require(p) assert(NAME == n) assert(REQUIRED == p) assert(rr == r) + assert(ext == x) end -a = require"names" +local a = require"names" assert(a[1] == "names" and a[2] == D"names.lua") -_G.a = nil local st, msg = pcall(require, "err") assert(not st and string.find(msg, "arithmetic") and B == 15) st, msg = pcall(require, "synerr") @@ -143,30 +166,31 @@ assert(package.searchpath("C", package.path) == D"C.lua") assert(require"C" == 25) assert(require"C" == 25) AA = nil -try('B', 'B.lua', true) +try('B', 'B.lua', true, "libs/B.lua") assert(package.loaded.B) assert(require"B" == true) assert(package.loaded.A) assert(require"C" == 25) package.loaded.A = nil -try('B', nil, true) -- should not reload package -try('A', 'A.lua', true) +try('B', nil, true, nil) -- should not reload package +try('A', 'A.lua', true, "libs/A.lua") package.loaded.A = nil os.remove(D'A.lua') AA = {} -try('A', 'A.lc', AA) -- now must find second option +try('A', 'A.lc', AA, "libs/A.lc") -- now must find second option assert(package.searchpath("A", package.path) == D"A.lc") assert(require("A") == AA) AA = false -try('K', 'L', false) -- default option -try('K', 'L', false) -- default option (should reload it) +try('K', 'L', false, "libs/L") -- default option +try('K', 'L', false, "libs/L") -- default option (should reload it) assert(rawget(_G, "_REQUIREDNAME") == nil) AA = "x" -try("X", "XXxX", AA) +try("X", "XXxX", AA, "libs/XXxX") removefiles(files) +NAME, REQUIRED, AA, B = nil -- testing require of sub-packages @@ -183,21 +207,23 @@ files = { createfiles(files, "_ENV = {}\n", "\nreturn _ENV\n") AA = 0 -local m = assert(require"P1") +local m, ext = assert(require"P1") +assert(ext == "libs/P1/init.lua") assert(AA == 0 and m.AA == 10) assert(require"P1" == m) assert(require"P1" == m) assert(package.searchpath("P1.xuxu", package.path) == D"P1/xuxu.lua") -m.xuxu = assert(require"P1.xuxu") +m.xuxu, ext = assert(require"P1.xuxu") assert(AA == 0 and m.xuxu.AA == 20) +assert(ext == "libs/P1/xuxu.lua") assert(require"P1.xuxu" == m.xuxu) assert(require"P1.xuxu" == m.xuxu) assert(require"P1" == m and m.AA == 10) removefiles(files) - +AA = nil package.path = "" assert(not pcall(require, "file_does_not_exist")) @@ -210,7 +236,7 @@ package.path = oldpath local fname = "file_does_not_exist2" local m, err = pcall(require, fname) for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do - t = string.gsub(t, "?", fname) + local t = string.gsub(t, "?", fname) assert(string.find(err, t, 1, true)) end @@ -267,23 +293,26 @@ else -- test C modules with prefixes in names package.cpath = DC"?" - local lib2 = require"lib2-v2" + local lib2, ext = require"lib2-v2" + assert(string.find(ext, "libs/lib2-v2", 1, true)) -- check correct access to global environment and correct -- parameters assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") - assert(lib2.id("x") == "x") + assert(lib2.id("x") == true) -- a different "id" implementation -- test C submodules - local fs = require"lib1.sub" + local fs, ext = require"lib1.sub" assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") + assert(string.find(ext, "libs/lib1", 1, true)) assert(fs.id(45) == 45) + _ENV.x, _ENV.y = nil end + _ENV = _G -- testing preload - do local p = package package = {} @@ -293,15 +322,35 @@ do return _ENV end - local pl = require"pl" + local pl, ext = require"pl" assert(require"pl" == pl) assert(pl.xuxu(10) == 30) - assert(pl[1] == "pl" and pl[2] == nil) + assert(pl[1] == "pl" and pl[2] == ":preload:" and ext == ":preload:") package = p assert(type(package.path) == "string") end + +do print("testing external strings") + package.cpath = DC"?" + local lib2 = require"lib2-v2" + local t = {} + for _, len in ipairs{0, 10, 39, 40, 41, 1000} do + local str = string.rep("a", len) + local str1 = lib2.newstr(str) + assert(str == str1) + assert(not T or T.hash(str) == T.hash(str1)) + t[str1] = 20; assert(t[str] == 20 and t[str1] == 20) + t[str] = 10; assert(t[str1] == 10) + local tt = {[str1] = str1} + assert(next(tt) == str1 and next(tt, str1) == nil) + assert(tt[str] == str) + local str2 = lib2.newstr(str1) + assert(str == str2 and t[str2] == 10 and tt[str2] == str) + end +end + print('+') end --] @@ -310,10 +359,10 @@ print("testing assignments, logical operators, and constructors") local res, res2 = 27 -a, b = 1, 2+3 +local a, b = 1, 2+3 assert(a==1 and b==5) a={} -function f() return 10, 11, 12 end +local function f() return 10, 11, 12 end a.x, b, a[1] = 1, 2, f() assert(a.x==1 and b==2 and a[1]==10) a[f()], b, a[f()+3] = f(), a, 'x' @@ -325,15 +374,15 @@ do local a,b,c a,b = 0, f(1) assert(a == 0 and b == 1) - A,b = 0, f(1) - assert(A == 0 and b == 1) + a,b = 0, f(1) + assert(a == 0 and b == 1) a,b,c = 0,5,f(4) assert(a==0 and b==5 and c==1) a,b,c = 0,5,f(0) assert(a==0 and b==5 and c==nil) end -a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 +local a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 assert(not a and b and c and d==6) d = 20 @@ -391,6 +440,7 @@ assert(not pcall(function () local a = {[nil] = 10} end)) assert(a[nil] == undef) a = nil +local a, b, c a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} a, a.x, a.y = a, a[-3] assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y) @@ -406,8 +456,18 @@ a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 == 10) +do + -- _ENV constant + local function foo () + local _ENV = 11 + X = "hi" + end + local st, msg = pcall(foo) + assert(not st and string.find(msg, "number")) +end + --- test of large float/integer indices +-- test of large float/integer indices -- compute maximum integer where all bits fit in a float local maxint = math.maxinteger @@ -417,7 +477,7 @@ while maxint ~= (maxint + 0.0) or (maxint - 1) ~= (maxint - 1.0) do maxint = maxint // 2 end -maxintF = maxint + 0.0 -- float version +local maxintF = maxint + 0.0 -- float version assert(maxintF == maxint and math.type(maxintF) == "float" and maxintF >= 2.0^14) diff --git a/testes/big.lua b/testes/big.lua index 150d15dc06..119caa6c32 100644 --- a/testes/big.lua +++ b/testes/big.lua @@ -1,5 +1,5 @@ -- $Id: testes/big.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h if _soft then return 'a' @@ -32,7 +32,7 @@ setmetatable(env, { }) X = nil -co = coroutine.wrap(f) +local co = coroutine.wrap(f) assert(co() == 's') assert(co() == 'g') assert(co() == 'g') @@ -66,7 +66,7 @@ assert(repstrings * ssize > 2.0^32) -- it should be larger than maximum size local longs = string.rep("\0", ssize) -- create one long string --- create function to concatentate 'repstrings' copies of its argument +-- create function to concatenate 'repstrings' copies of its argument local rep = assert(load( "local a = ...; return " .. string.rep("a", repstrings, ".."))) diff --git a/testes/bitwise.lua b/testes/bitwise.lua index af542e7f68..10afff432f 100644 --- a/testes/bitwise.lua +++ b/testes/bitwise.lua @@ -1,5 +1,5 @@ -- $Id: testes/bitwise.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print("testing bitwise operations") @@ -38,6 +38,18 @@ d = d << 32 assert(a | b ~ c & d == 0xF4000000 << 32) assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1) + +do -- constant folding + local code = string.format("return -1 >> %d", math.maxinteger) + assert(load(code)() == 0) + local code = string.format("return -1 >> %d", math.mininteger) + assert(load(code)() == 0) + local code = string.format("return -1 << %d", math.maxinteger) + assert(load(code)() == 0) + local code = string.format("return -1 << %d", math.mininteger) + assert(load(code)() == 0) +end + assert(-1 >> 1 == (1 << (numbits - 1)) - 1 and 1 << 31 == 0x80000000) assert(-1 >> (numbits - 1) == 1) assert(-1 >> numbits == 0 and @@ -45,6 +57,11 @@ assert(-1 >> numbits == 0 and -1 << numbits == 0 and -1 << -numbits == 0) +assert(1 >> math.mininteger == 0) +assert(1 >> math.maxinteger == 0) +assert(1 << math.mininteger == 0) +assert(1 << math.maxinteger == 0) + assert((2^30 - 1) << 2^30 == 0) assert((2^30 - 1) >> 2^30 == 0) @@ -60,9 +77,9 @@ assert("1234.0" << "5.0" == 1234 * 32) assert("0xffff.0" ~ "0xAAAA" == 0x5555) assert(~"0x0.000p4" == -1) -assert("7" .. 3 << 1 == 146) -assert(10 >> 1 .. "9" == 0) -assert(10 | 1 .. "9" == 27) +assert(("7" .. 3) << 1 == 146) +assert(0xffffffff >> (1 .. "9") == 0x1fff) +assert(10 | (1 .. "9") == 27) do local st, msg = pcall(function () return 4 & "a" end) diff --git a/testes/bwcoercion.lua b/testes/bwcoercion.lua index cd735ab0b6..0544944d84 100644 --- a/testes/bwcoercion.lua +++ b/testes/bwcoercion.lua @@ -4,7 +4,7 @@ local strsub = string.sub local print = print -_ENV = nil +global none -- Try to convert a value to an integer, without assuming any coercion. local function toint (x) diff --git a/testes/calls.lua b/testes/calls.lua index f5108a470e..0dacb85ab7 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -1,5 +1,7 @@ -- $Id: testes/calls.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h + +global * print("testing functions and calls") @@ -16,28 +18,13 @@ assert(type(nil) == 'nil' and type(type) == 'function') assert(type(assert) == type(print)) -function f (x) return a:x (x) end +local function f (x) return a:x (x) end assert(type(f) == 'function') assert(not pcall(type)) -do -- test error in 'print' too... - local tostring = _ENV.tostring - - _ENV.tostring = nil - local st, msg = pcall(print, 1) - assert(st == false and string.find(msg, "attempt to call a nil value")) - - _ENV.tostring = function () return {} end - local st, msg = pcall(print, 1) - assert(st == false and string.find(msg, "must return a string")) - - _ENV.tostring = tostring -end - - -- testing local-function recursion -fact = false +global fact = false do local res = 1 local function fact (n) @@ -48,10 +35,11 @@ do assert(fact(5) == 120) end assert(fact == false) +fact = nil -- testing declarations -a = {i = 10} -self = 20 +local a = {i = 10} +local self = 20 function a:x (x) return x+self.i end function a.y (x) return x+self end @@ -77,7 +65,7 @@ a.b.c:f2('k', 12); assert(a.b.c.k == 12) print('+') -t = nil -- 'declare' t +global t = nil -- 'declare' t function f(a,b,c) local d = 'a'; t={a,b,c,d} end f( -- this line change must be valid @@ -87,7 +75,9 @@ f(1,2, -- this one too 3,4) assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') -function fat(x) +t = nil -- delete 't' + +global function fat(x) if x <= 1 then return 1 else return x*load("return fat(" .. x-1 .. ")", "")() end @@ -95,34 +85,39 @@ end assert(load "load 'assert(fat(6)==720)' () ")() a = load('return fat(5), 3') -a,b = a() +local a,b = a() assert(a == 120 and b == 3) +fat = nil print('+') -function err_on_n (n) +local function err_on_n (n) if n==0 then error(); exit(1); else err_on_n (n-1); exit(1); end end do - function dummy (n) + local function dummy (n) if n > 0 then assert(not pcall(err_on_n, n)) dummy(n-1) end end + + dummy(10) end -dummy(10) +_G.deep = nil -- "declaration" (used by 'all.lua') -function deep (n) +global function deep (n) if n>0 then deep(n-1) end end deep(10) deep(180) --- testing tail calls + +print"testing tail calls" + function deep (n) if n>0 then return deep(n-1) else return 101 end end assert(deep(30000) == 101) a = {} @@ -163,9 +158,85 @@ do -- tail calls x varargs assert(X == 10 and Y == 20 and #A == 1 and A[1] == 30) end + +do -- C-stack overflow while handling C-stack overflow + local function loop () + assert(pcall(loop)) + end + + local err, msg = xpcall(loop, loop) + assert(not err and string.find(msg, "error")) +end + + + +do -- tail calls x chain of __call + local n = 10000 -- depth + + local function foo () + if n == 0 then return 1023 + else n = n - 1; return foo() + end + end + + -- build a chain of __call metamethods ending in function 'foo' + for i = 1, 15 do + foo = setmetatable({}, {__call = foo}) + end + + -- call the first one as a tail call in a new coroutine + -- (to ensure stack is not preallocated) + assert(coroutine.wrap(function() return foo() end)() == 1023) +end + print('+') +do print"testing chains of '__call'" + local N = 15 + local u = table.pack + for i = 1, N do + u = setmetatable({i}, {__call = u}) + end + + local Res = u("a", "b", "c") + + assert(Res.n == N + 3) + for i = 1, N do + assert(Res[i][1] == i) + end + assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c") + + local function u (...) + local n = debug.getinfo(1, 't').extraargs + assert(select("#", ...) == n) + return n + end + + for i = 0, N do + assert(u() == i) + u = setmetatable({}, {__call = u}) + end +end + + +do -- testing chains too long + local a = {} + for i = 1, 16 do -- one too many + a = setmetatable({}, {__call = a}) + end + local status, msg = pcall(a) + assert(not status and string.find(msg, "too long")) + + setmetatable(a, {__call = a}) -- infinite chain + local status, msg = pcall(a) + assert(not status and string.find(msg, "too long")) + + -- again, with a tail call + local status, msg = pcall(function () return a() end) + assert(not status and string.find(msg, "too long")) +end + a = nil (function (x) a=x end)(23) assert(a == 23 and (function (x) return x*2 end)(20) == 40) @@ -174,7 +245,7 @@ assert(a == 23 and (function (x) return x*2 end)(20) == 40) -- testing closures -- fixed-point operator -Z = function (le) +local Z = function (le) local function a (f) return le(function (x) return f(f)(x) end) end @@ -184,14 +255,14 @@ Z = function (le) -- non-recursive factorial -F = function (f) +local F = function (f) return function (n) if n == 0 then return 1 else return n*f(n-1) end end end -fat = Z(F) +local fat = Z(F) assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) @@ -202,22 +273,21 @@ local function g (z) return f(z,z+1,z+2,z+3) end -f = g(10) +local f = g(10) assert(f(9, 16) == 10+11+12+13+10+9+16+10) -Z, F, f = nil print('+') -- testing multiple returns -function unlpack (t, i) +local function unlpack (t, i) i = i or 1 if (i <= #t) then return t[i], unlpack(t, i+1) end end -function equaltab (t1, t2) +local function equaltab (t1, t2) assert(#t1 == #t2) for i = 1, #t1 do assert(t1[i] == t2[i]) @@ -226,8 +296,8 @@ end local pack = function (...) return (table.pack(...)) end -function f() return 1,2,30,4 end -function ret2 (a,b) return a,b end +local function f() return 1,2,30,4 end +local function ret2 (a,b) return a,b end local a,b,c,d = unlpack{1,2,3} assert(a==1 and b==2 and c==3 and d==nil) @@ -256,7 +326,7 @@ table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a = "%s" + return { + function () return str end, + function () return str end, + function () return str end + } + ]], str)) + -- count occurrences of 'str' inside the dump + local dump = string.dump(foo) + local _, count = string.gsub(dump, str, {}) + -- there should be only two occurrences: + -- one inside the source, other the string itself. + assert(count == 2) + + if T then -- check reuse of strings in undump + local funcs = load(dump)() + assert(string.format("%p", T.listk(funcs[1])[1]) == + string.format("%p", T.listk(funcs[3])[1])) + end +end + + +do -- test limit of multiple returns (254 values) + local code = "return 10" .. string.rep(",10", 253) + local res = {assert(load(code))()} + assert(#res == 254 and res[254] == 10) + + code = code .. ",10" + local status, msg = load(code) + assert(not status and string.find(msg, "too many returns")) end print('OK') diff --git a/testes/closure.lua b/testes/closure.lua index cdeaebaa02..0c2e96c0f1 100644 --- a/testes/closure.lua +++ b/testes/closure.lua @@ -1,10 +1,20 @@ -- $Id: testes/closure.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h + +global * print "testing closures" +do -- bug in 5.4.7 + _ENV[true] = 10 + local function aux () return _ENV[1 < 2] end + assert(aux() == 10) + _ENV[true] = nil +end + + local A,B = 0,{g=10} -function f(x) +local function f(x) local a = {} for i=1,1000 do local y = 0 @@ -60,35 +70,33 @@ end -- testing closures with 'for' control variable a = {} for i=1,10 do - a[i] = {set = function(x) i=x end, get = function () return i end} + a[i] = function () return i end if i == 3 then break end end assert(a[4] == undef) -a[1].set(10) -assert(a[2].get() == 2) -a[2].set('a') -assert(a[3].get() == 3) -assert(a[2].get() == 'a') +assert(a[2]() == 2) +assert(a[3]() == 3) a = {} local t = {"a", "b"} for i = 1, #t do local k = t[i] - a[i] = {set = function(x, y) i=x; k=y end, + a[i] = {set = function(x) k=x end, get = function () return i, k end} if i == 2 then break end end -a[1].set(10, 20) +a[1].set(10) local r,s = a[2].get() assert(r == 2 and s == 'b') r,s = a[1].get() -assert(r == 10 and s == 20) -a[2].set('a', 'b') +assert(r == 1 and s == 10) +a[2].set('a') r,s = a[2].get() -assert(r == "a" and s == "b") +assert(r == 2 and s == "a") -- testing closures with 'for' control variable x break +local f for i=1,3 do f = function () return i end break @@ -139,7 +147,7 @@ assert(b('get') == 'xuxu') b('set', 10); assert(b('get') == 14) -local w +local y, w -- testing multi-level closure function f(x) return function (y) @@ -230,6 +238,7 @@ t() -- test for debug manipulation of upvalues local debug = require'debug' +local foo1, foo2, foo3 do local a , b, c = 3, 5, 7 foo1 = function () return a+b end; @@ -242,7 +251,7 @@ end assert(debug.upvalueid(foo1, 1)) assert(debug.upvalueid(foo1, 2)) -assert(not pcall(debug.upvalueid, foo1, 3)) +assert(not debug.upvalueid(foo1, 3)) assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2)) assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1)) assert(debug.upvalueid(foo3, 1)) diff --git a/testes/code.lua b/testes/code.lua index 834ff5e23d..380ff70c1b 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -1,5 +1,7 @@ -- $Id: testes/code.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h + +global * if T==nil then (Message or print)('\n >>> testC not active: skipping opcode tests <<<\n') @@ -7,6 +9,23 @@ if T==nil then end print "testing code generation and optimizations" +-- to test constant propagation +local k0aux = 0 +local k0 = k0aux +local k1 = 1 +local k3 = 3 +local k6 = k3 + (k3 << k0) +local kFF0 = 0xFF0 +local k3_78 = 3.78 +local x, k3_78_4 = 10, k3_78 / 4 +assert(x == 10) + +local kx = "x" + +local kTrue = true +local kFalse = false + +local kNil = nil -- this code gave an error for the code checker do @@ -27,21 +46,49 @@ end local function foo () local a - a = 3; + a = k3; a = 0; a = 0.0; a = -7 + 7 - a = 3.78/4; a = 3.78/4 - a = -3.78/4; a = 3.78/4; a = -3.78/4 + a = k3_78/4; a = k3_78_4 + a = -k3_78/4; a = k3_78/4; a = -3.78/4 a = -3.79/4; a = 0.0; a = -0; - a = 3; a = 3.0; a = 3; a = 3.0 + a = k3; a = 3.0; a = 3; a = 3.0 end checkKlist(foo, {3.78/4, -3.78/4, -3.79/4}) +foo = function (f, a) + f(100 * 1000) + f(100.0 * 1000) + f(-100 * 1000) + f(-100 * 1000.0) + f(100000) + f(100000.0) + f(-100000) + f(-100000.0) + end + +checkKlist(foo, {100000, 100000.0, -100000, -100000.0}) + + +-- floats x integers +foo = function (t, a) + t[a] = 1; t[a] = 1.0 + t[a] = 1; t[a] = 1.0 + t[a] = 2; t[a] = 2.0 + t[a] = 0; t[a] = 0.0 + t[a] = 1; t[a] = 1.0 + t[a] = 2; t[a] = 2.0 + t[a] = 0; t[a] = 0.0 +end + +checkKlist(foo, {1, 1.0, 2, 2.0, 0, 0.0}) + + -- testing opcodes -- check that 'f' opcodes match '...' -function check (f, ...) +local function check (f, ...) local arg = {...} local c = T.listcode(f) for i=1, #arg do @@ -54,7 +101,7 @@ end -- check that 'f' opcodes match '...' and that 'f(p) == r'. -function checkR (f, p, r, ...) +local function checkR (f, p, r, ...) local r1 = f(p) assert(r == r1 and math.type(r) == math.type(r1)) check(f, ...) @@ -62,7 +109,7 @@ end -- check that 'a' and 'b' has the same opcodes -function checkequal (a, b) +local function checkequal (a, b) a = T.listcode(a) b = T.listcode(b) assert(#a == #b) @@ -77,19 +124,22 @@ end -- some basic instructions check(function () -- function does not create upvalues (function () end){f()} -end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN0') +end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL', + 'SETLIST', 'CALL', 'RETURN0') check(function (x) -- function creates upvalues (function () return x end){f()} -end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN') +end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL', + 'SETLIST', 'CALL', 'RETURN') -- sequence of LOADNILs check(function () + local kNil = nil local a,b,c local d; local e; local f,g,h; - d = nil; d=nil; b=nil; a=nil; c=nil; + d = nil; d=nil; b=nil; a=kNil; c=nil; end, 'LOADNIL', 'RETURN0') check(function () @@ -109,7 +159,7 @@ check (function (a,b,c) return a end, 'RETURN1') -- infinite loops -check(function () while true do local a = -1 end end, +check(function () while kTrue do local a = -1 end end, 'LOADI', 'JMP', 'RETURN0') check(function () while 1 do local a = -1 end end, @@ -124,10 +174,10 @@ check(function (a,b,c,d) return a..b..c..d end, 'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN1') -- not -check(function () return not not nil end, 'LOADBOOL', 'RETURN1') -check(function () return not not false end, 'LOADBOOL', 'RETURN1') -check(function () return not not true end, 'LOADBOOL', 'RETURN1') -check(function () return not not 1 end, 'LOADBOOL', 'RETURN1') +check(function () return not not nil end, 'LOADFALSE', 'RETURN1') +check(function () return not not kFalse end, 'LOADFALSE', 'RETURN1') +check(function () return not not true end, 'LOADTRUE', 'RETURN1') +check(function () return not not k3 end, 'LOADTRUE', 'RETURN1') -- direct access to locals check(function () @@ -136,15 +186,16 @@ check(function () c.x, a[b] = -((a + d/b - a[b]) ^ a.x), b end, 'LOADNIL', - 'MUL', - 'DIV', 'ADD', 'GETTABLE', 'SUB', 'GETFIELD', 'POW', - 'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0') + 'MUL', 'MMBIN', + 'DIV', 'MMBIN', 'ADD', 'MMBIN', 'GETTABLE', 'SUB', 'MMBIN', + 'GETFIELD', 'POW', 'MMBIN', 'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0') -- direct access to constants check(function () local a,b - a.x = 3.2 + local c = kNil + a[kx] = 3.2 a.x = b a[b] = 'x' end, @@ -152,8 +203,9 @@ end, -- "get/set table" with numeric indices check(function (a) + local k255 = 255 a[1] = a[100] - a[255] = a[256] + a[k255] = a[256] a[256] = 5 end, 'GETI', 'SETI', @@ -166,13 +218,13 @@ check(function () b = a/a b = 5-4 end, - 'LOADNIL', 'SUB', 'DIV', 'LOADI', 'RETURN0') + 'LOADNIL', 'SUB', 'MMBIN', 'DIV', 'MMBIN', 'LOADI', 'RETURN0') check(function () local a,b - a[true] = false + a[kTrue] = false end, - 'LOADNIL', 'LOADBOOL', 'SETTABLE', 'RETURN0') + 'LOADNIL', 'LOADTRUE', 'SETTABLE', 'RETURN0') -- equalities @@ -238,74 +290,87 @@ local function checkF (func, val) end checkF(function () return 0.0 end, 0.0) -checkI(function () return 0 end, 0) -checkI(function () return -0//1 end, 0) +checkI(function () return k0 end, 0) +checkI(function () return -k0//1 end, 0) checkK(function () return 3^-1 end, 1/3) checkK(function () return (1 + 1)^(50 + 50) end, 2^100) checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0) -checkF(function () return (-3^0 + 5) // 3.0 end, 1.0) -checkI(function () return -3 % 5 end, 2) +checkF(function () return (-k3^0 + 5) // 3.0 end, 1.0) +checkI(function () return -k3 % 5 end, 2) checkF(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0) checkF(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0) checkI(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4) -checkI(function () return ~(~0xFF0 | 0xFF0) end, 0) +checkI(function () return ~(~kFF0 | kFF0) end, 0) checkI(function () return ~~-1024.0 end, -1024) -checkI(function () return ((100 << 6) << -4) >> 2 end, 100) +checkI(function () return ((100 << k6) << -4) >> 2 end, 100) -- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535) local a = 17; local sbx = ((1 << a) - 1) >> 1 -- avoid folding -checkI(function () return 65535 end, sbx) -checkI(function () return -65535 end, -sbx) -checkI(function () return 65536 end, sbx + 1) -checkK(function () return 65537 end, sbx + 2) -checkK(function () return -65536 end, -(sbx + 1)) +local border = 65535 +checkI(function () return border end, sbx) +checkI(function () return -border end, -sbx) +checkI(function () return border + 1 end, sbx + 1) +checkK(function () return border + 2 end, sbx + 2) +checkK(function () return -(border + 1) end, -(sbx + 1)) -checkF(function () return 65535.0 end, sbx + 0.0) -checkF(function () return -65535.0 end, -sbx + 0.0) -checkF(function () return 65536.0 end, (sbx + 1.0)) -checkK(function () return 65537.0 end, (sbx + 2.0)) -checkK(function () return -65536.0 end, -(sbx + 1.0)) +local border = 65535.0 +checkF(function () return border end, sbx + 0.0) +checkF(function () return -border end, -sbx + 0.0) +checkF(function () return border + 1 end, (sbx + 1.0)) +checkK(function () return border + 2 end, (sbx + 2.0)) +checkK(function () return -(border + 1) end, -(sbx + 1.0)) -- immediate operands -checkR(function (x) return x + 1 end, 10, 11, 'ADDI', 'RETURN1') -checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'RETURN1') -checkR(function (x) return x * -127 end, -1.0, 127.0, 'MULI', 'RETURN1') -checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'RETURN1') -checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'RETURN1') -checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'RETURN1') -checkR(function (x) return x // 1 end, 10.0, 10.0, 'IDIVI', 'RETURN1') -checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODI', 'RETURN1') -checkR(function (x) return 1 << x end, 3, 8, 'SHLI', 'RETURN1') -checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1') -checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1') -checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'RETURN1') -checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'RETURN1') -checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'RETURN1') +checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'MMBINI', 'RETURN1') +checkR(function (x) return x - 127 end, 10, -117, 'ADDI', 'MMBINI', 'RETURN1') +checkR(function (x) return 128 + x end, 0.0, 128.0, + 'ADDI', 'MMBINI', 'RETURN1') +checkR(function (x) return x * -127 end, -1.0, 127.0, + 'MULK', 'MMBINK', 'RETURN1') +checkR(function (x) return 20 * x end, 2, 40, 'MULK', 'MMBINK', 'RETURN1') +checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWK', 'MMBINK', 'RETURN1') +checkR(function (x) return x / 40 end, 40, 1.0, 'DIVK', 'MMBINK', 'RETURN1') +checkR(function (x) return x // 1 end, 10.0, 10.0, + 'IDIVK', 'MMBINK', 'RETURN1') +checkR(function (x) return x % (100 - 10) end, 91, 1, + 'MODK', 'MMBINK', 'RETURN1') +checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'MMBINI', 'RETURN1') +checkR(function (x) return x << 127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1') +checkR(function (x) return x << -127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1') +checkR(function (x) return x >> 128 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1') +checkR(function (x) return x >> -127 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1') +checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'MMBINK', 'RETURN1') +checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'MMBINK', 'RETURN1') +checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'MMBINK', 'RETURN1') -- K operands in arithmetic operations -checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'RETURN1') --- check(function (x) return 128 + x end, 'ADDK', 'RETURN1') -checkR(function (x) return x * -10000 end, 2, -20000, 'MULK', 'RETURN1') --- check(function (x) return 20 * x end, 'MULK', 'RETURN1') -checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'RETURN1') -checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'RETURN1') -checkR(function (x) return x // 10000 end, 10000, 1, 'IDIVK', 'RETURN1') -checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, 'MODK', 'RETURN1') +checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'MMBINK', 'RETURN1') +-- check(function (x) return 128 + x end, 'ADDK', 'MMBINK', 'RETURN1') +checkR(function (x) return x * -10000 end, 2, -20000, + 'MULK', 'MMBINK', 'RETURN1') +-- check(function (x) return 20 * x end, 'MULK', 'MMBINK', 'RETURN1') +checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'MMBINK', 'RETURN1') +checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'MMBINK', 'RETURN1') +checkR(function (x) return x // 10000 end, 10000, 1, + 'IDIVK', 'MMBINK', 'RETURN1') +checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, + 'MODK', 'MMBINK', 'RETURN1') -- no foldings (and immediate operands) check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') -check(function () return 3/0 end, 'LOADI', 'DIVI', 'RETURN1') -check(function () return 0%0 end, 'LOADI', 'MODI', 'RETURN1') -check(function () return -4//0 end, 'LOADI', 'IDIVI', 'RETURN1') -check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'RETURN1') -check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'RETURN1') +check(function () return k3/0 end, 'LOADI', 'DIVK', 'MMBINK', 'RETURN1') +check(function () return 0%0 end, 'LOADI', 'MODK', 'MMBINK', 'RETURN1') +check(function () return -4//0 end, 'LOADI', 'IDIVK', 'MMBINK', 'RETURN1') +check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'MMBIN', 'RETURN1') +check(function (x) return x << 128 end, 'LOADI', 'SHL', 'MMBIN', 'RETURN1') +check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'MMBIN', 'RETURN1') -- basic 'for' loops check(function () for i = -10, 10.5 do end end, -'LOADI', 'LOADK', 'LOADI', 'FORPREP1', 'FORLOOP1', 'RETURN0') +'LOADI', 'LOADK', 'LOADI', 'FORPREP', 'FORLOOP', 'RETURN0') check(function () for i = 0xfffffff, 10.0, 1 do end end, -'LOADK', 'LOADF', 'LOADI', 'FORPREP1', 'FORLOOP1', 'RETURN0') +'LOADK', 'LOADF', 'LOADI', 'FORPREP', 'FORLOOP', 'RETURN0') -- bug in constant folding for 5.1 check(function () return -nil end, 'LOADNIL', 'UNM', 'RETURN1') @@ -335,50 +400,105 @@ end, do -- tests for table access in upvalues local t - check(function () t.x = t.y end, 'GETTABUP', 'SETTABUP') + check(function () t[kx] = t.y end, 'GETTABUP', 'SETTABUP') check(function (a) t[a()] = t[a()] end, 'MOVE', 'CALL', 'GETUPVAL', 'MOVE', 'CALL', 'GETUPVAL', 'GETTABLE', 'SETTABLE') end -- de morgan -checkequal(function () local a; if not (a or b) then b=a end end, - function () local a; if (not a and not b) then b=a end end) +checkequal(function () local a, b; if not (a or b) then b=a end end, + function () local a, b; if (not a and not b) then b=a end end) checkequal(function (l) local a; return 0 <= a and a <= l end, function (l) local a; return not (not(a >= 0) or not(a <= l)) end) --- if-break optimizations check(function (a, b) while a do if b then break else a = a + 1 end end end, -'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'JMP', 'RETURN0') - -checkequal( -function (a) while a < 10 do a = a + 1 end end, -function (a) - ::loop:: - if not (a < 10) then goto exit end - a = a + 1 - goto loop -::exit:: +'TEST', 'JMP', 'TEST', 'JMP', 'JMP', 'CLOSE', 'JMP', 'ADDI', 'MMBINI', 'JMP', 'RETURN0') + +check(function () + do + goto exit -- don't need to close + local x = nil + goto exit -- must close + end + ::exit:: + end, 'JMP', 'CLOSE', 'LOADNIL', 'TBC', + 'CLOSE', 'JMP', 'CLOSE', 'RETURN') + +checkequal(function () return 6 or true or nil end, + function () return k6 or kTrue or kNil end) + +checkequal(function () return 6 and true or nil end, + function () return k6 and kTrue or kNil end) + + +do -- string constants + local k0 = "00000000000000000000000000000000000000000000000000" + local function f1 () + local k = k0 + return function () + return function () return k end + end + end + + local f2 = f1() + local f3 = f2() + assert(f3() == k0) + checkK(f3, k0) + -- string is not needed by other functions + assert(T.listk(f1)[1] == nil) + assert(T.listk(f2)[1] == nil) end -) - -checkequal( -function (a) repeat local x = a + 1; a = x until a > 0 end, -function (a) - ::loop:: do - local x = a + 1 - a = x + + +do -- check number of available registers + -- 1 register for local + 1 for function + 252 arguments + local source = "local a; return a(" .. string.rep("a, ", 252) .. "a)" + local prog = T.listcode(assert(load(source))) + -- maximum valid register is 254 + for i = 1, 254 do + assert(string.find(prog[2 + i], "MOVE%s*" .. i)) end - if not (a > 0) then goto loop end + -- one more argument would need register #255 (but that is reserved) + source = "local a; return a(" .. string.rep("a, ", 253) .. "a)" + local _, msg = load(source) + assert(string.find(msg, "too many registers")) end -) +do -- basic check for SETLIST + -- create a list constructor with 50 elements + local source = "local a; return {" .. string.rep("a, ", 50) .. "}" + local func = assert(load(source)) + local code = table.concat(T.listcode(func), "\n") + local _, count = string.gsub(code, "SETLIST", "") + -- code uses only 1 SETLIST for the constructor + assert(count == 1) +end + + +do print("testing code for integer limits") + local function checkints (n) + local source = string.format( + "local a = {[true] = 0X%x}; return a[true]", n) + local f = assert(load(source)) + checkKlist(f, {n}) + assert(f() == n) + f = load(string.dump(f)) + assert(f() == n) + end + + checkints(math.maxinteger) + checkints(math.mininteger) + checkints(-1) + +end + print 'OK' diff --git a/testes/constructs.lua b/testes/constructs.lua index a83df79eaf..94f670c7a5 100644 --- a/testes/constructs.lua +++ b/testes/constructs.lua @@ -1,5 +1,5 @@ -- $Id: testes/constructs.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h ;;print "testing syntax";; @@ -11,6 +11,7 @@ local function checkload (s, msg) end -- testing semicollons +local a do ;;; end ; do ; a = 3; assert(a == 3) end; ; @@ -49,26 +50,87 @@ assert((((nil and true) or false) and true) == false) local a,b = 1,nil; assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75); -x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); +local x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x); -x,y=1,2; +local x, y = 1, 2; assert((x>y) and x or y == 2); x,y=2,1; assert((x>y) and x or y == 2); assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) +do -- testing operators with different kinds of constants + -- operands to consider: + -- * fit in register + -- * constant doesn't fit in register + -- * floats with integral values + local operand = {3, 100, 5.0, -10, -5.0, 10000, -10000} + local operator = {"+", "-", "*", "/", "//", "%", "^", + "&", "|", "^", "<<", ">>", + "==", "~=", "<", ">", "<=", ">=",} + for _, op in ipairs(operator) do + local f = assert(load(string.format([[return function (x,y) + return x %s y + end]], op)))(); + for _, o1 in ipairs(operand) do + for _, o2 in ipairs(operand) do + local gab = f(o1, o2) + + _ENV.XX = o1 + local code = string.format("return XX %s %s", op, o2) + local res = assert(load(code))() + assert(res == gab) + + _ENV.XX = o2 + code = string.format("return (%s) %s XX", o1, op) + res = assert(load(code))() + assert(res == gab) + + code = string.format("return (%s) %s %s", o1, op, o2) + res = assert(load(code))() + assert(res == gab) + end + end + end + _ENV.XX = nil +end + -- silly loops repeat until 1; repeat until true; while false do end; while nil do end; do -- test old bug (first name could not be an `upvalue') - local a; function f(x) x={a=1}; x={x=1}; x={G=1} end + local a; local function f(x) x={a=1}; x={x=1}; x={G=1} end +end + + +do -- bug since 5.4.0 + -- create code with a table using more than 256 constants + local code = {"local x = {"} + for i = 1, 257 do + code[#code + 1] = i .. ".1," + end + code[#code + 1] = "};" + code = table.concat(code) + + -- add "ret" to the end of that code and checks that + -- it produces the expected value "val" + local function check (ret, val) + local code = code .. ret + code = load(code) + assert(code() == val) + end + + check("return (1 ~ (2 or 3))", 1 ~ 2) + check("return (1 | (2 or 3))", 1 | 2) + check("return (1 + (2 or 3))", 1 + 2) + check("return (1 << (2 or 3))", 1 << 2) end -function f (i) + +local function f (i) if type(i) ~= 'number' then return i,'jojo'; end; if i > 0 then return i, f(i-1); end; end @@ -94,10 +156,10 @@ end assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) for i=1,1000 do break; end; -n=100; -i=3; -t = {}; -a=nil +local n=100; +local i=3; +local t = {}; +local a=nil while not a do a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end; end @@ -140,14 +202,14 @@ a={y=1} x = {a.y} assert(x[1] == 1) -function f(i) +local function f (i) while 1 do if i>0 then i=i-1; else return; end; end; end; -function g(i) +local function g(i) while 1 do if i>0 then i=i-1 else return end @@ -175,6 +237,28 @@ assert(a==1 and b==nil) print'+'; +do -- testing constants + local prog = [[local x = 10]] + checkload(prog, "unknown attribute 'XXX'") + + checkload([[local xxx = 20; xxx = 10]], + ":1: attempt to assign to const variable 'xxx'") + + checkload([[ + local xx; + local xxx = 20; + local yyy; + local function foo () + local abc = xx + yyy + xxx; + return function () return function () xxx = yyy end end + end + ]], ":6: attempt to assign to const variable 'xxx'") + + checkload([[ + local x = nil + x = io.open() + ]], ":2: attempt to assign to const variable 'x'") +end f = [[ return function ( a , b , c , d , e ) @@ -190,7 +274,7 @@ function g (a,b,c,d,e) if not (a>=b or c or d and e or nil) then return 0; else return 1; end; end -function h (a,b,c,d,e) +local function h (a,b,c,d,e) while (a>=b or c or (d and e) or nil) do return 1; end; return 0; end; @@ -218,7 +302,7 @@ do assert(a==2) end -function F(a) +local function F (a) assert(debug.getinfo(1, "n").name == 'F') return a,2,3 end @@ -230,7 +314,7 @@ a,b = F(nil)==nil; assert(a == true and b == nil) ------------------------------------------------------------------ -- sometimes will be 0, sometimes will not... -_ENV.GLOB1 = math.floor(os.time()) % 2 +_ENV.GLOB1 = math.random(0, 1) -- basic expressions with their respective values local basiccases = { @@ -241,16 +325,36 @@ local basiccases = { {"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1}, } +local prog + +if _ENV.GLOB1 == 0 then + basiccases[2][1] = "F" -- constant false + + prog = [[ + local F = false + if %s then IX = true end + return %s +]] +else + basiccases[4][1] = "k10" -- constant 10 + + prog = [[ + local k10 = 10 + if %s then IX = true end + return %s + ]] +end + print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')') -- operators with their respective values -local binops = { +local binops = { {" and ", function (a,b) if not a then return a else return b end end}, {" or ", function (a,b) if a then return a else return b end end}, } -local cases = {} +local cases = {} -- creates all combinations of '(cases[i] op cases[n-i])' plus -- 'not(cases[i] op cases[n-i])' (syntax + value) @@ -280,8 +384,6 @@ cases[1] = basiccases for i = 2, level do cases[i] = createcases(i) end print("+") -local prog = [[if %s then IX = true end; return %s]] - local i = 0 for n = 1, level do for _, v in pairs(cases[n]) do @@ -293,6 +395,8 @@ for n = 1, level do if i % 60000 == 0 then print('+') end end end +IX = nil +_G.GLOB1 = nil ------------------------------------------------------------------ -- testing some syntax errors (chosen through 'gcov') diff --git a/testes/coroutine.lua b/testes/coroutine.lua index a4321bedaf..ba394e0c46 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -1,5 +1,5 @@ -- $Id: testes/coroutine.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print "testing coroutines" @@ -10,7 +10,7 @@ local f local main, ismain = coroutine.running() assert(type(main) == "thread" and ismain) assert(not coroutine.resume(main)) -assert(not coroutine.isyieldable()) +assert(not coroutine.isyieldable(main) and not coroutine.isyieldable()) assert(not pcall(coroutine.yield)) @@ -30,7 +30,8 @@ local function eqtab (t1, t2) end _G.x = nil -- declare x -function foo (a, ...) +_G.f = nil -- declare f +local function foo (a, ...) local x, y = coroutine.running() assert(x == f and y == false) -- next call should not corrupt coroutine (but must fail, @@ -38,7 +39,7 @@ function foo (a, ...) assert(coroutine.resume(f) == false) assert(coroutine.status(f) == "running") local arg = {...} - assert(coroutine.isyieldable()) + assert(coroutine.isyieldable(x)) for i=1,#arg do _G.x = {coroutine.yield(table.unpack(arg[i]))} end @@ -46,14 +47,17 @@ function foo (a, ...) end f = coroutine.create(foo) +assert(coroutine.isyieldable(f)) assert(type(f) == "thread" and coroutine.status(f) == "suspended") assert(string.find(tostring(f), "thread")) local s,a,b,c,d s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'}) +assert(coroutine.isyieldable(f)) assert(s and a == nil and coroutine.status(f) == "suspended") s,a,b,c,d = coroutine.resume(f) eqtab(_G.x, {}) assert(s and a == 1 and b == nil) +assert(coroutine.isyieldable(f)) s,a,b,c,d = coroutine.resume(f, 1, 2, 3) eqtab(_G.x, {1, 2, 3}) assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil) @@ -64,10 +68,11 @@ assert(coroutine.status(f) == "dead") s, a = coroutine.resume(f, "xuxu") assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead") +_G.f = nil -- yields in tail calls local function foo (i) return coroutine.yield(i) end -f = coroutine.wrap(function () +local f = coroutine.wrap(function () for i=1,10 do assert(foo(i) == _G.x) end @@ -76,8 +81,10 @@ end) for i=1,10 do _G.x = i; assert(f(i) == i) end _G.x = 'xuxu'; assert(f('xuxu') == 'a') +_G.x = nil + -- recursive -function pf (n, i) +local function pf (n, i) coroutine.yield(n) pf(n*i, i+1) end @@ -90,14 +97,14 @@ for i=1,10 do end -- sieve -function gen (n) +local function gen (n) return coroutine.wrap(function () for i=2,n do coroutine.yield(i) end end) end -function filter (p, g) +local function filter (p, g) return coroutine.wrap(function () while 1 do local n = g() @@ -120,35 +127,78 @@ assert(#a == 22 and a[#a] == 79) x, a = nil --- coroutine kill +do -- "bug" in 5.4.2 + local function foo () foo () end -- just create a stack overflow + local co = coroutine.create(foo) + -- running this coroutine would overflow the unsigned short 'nci', the + -- counter of CallInfo structures available to the thread. + -- (The issue only manifests in an 'assert'.) + local st, msg = coroutine.resume(co) + assert(string.find(msg, "stack overflow")) + assert(coroutine.status(co) == "dead") +end + + +print("to-be-closed variables in coroutines") + +local function func2close (f) + return setmetatable({}, {__close = f}) +end + do - -- ok to kill a dead coroutine + -- ok to close a dead coroutine local co = coroutine.create(print) - assert(coroutine.resume(co, "testing 'coroutine.kill'")) + assert(coroutine.resume(co, "testing 'coroutine.close'")) assert(coroutine.status(co) == "dead") - assert(coroutine.kill(co)) - - -- cannot kill the running coroutine - local st, msg = pcall(coroutine.kill, coroutine.running()) - assert(not st and string.find(msg, "running")) + local st, msg = coroutine.close(co) + assert(st and msg == nil) + -- also ok to close it again + st, msg = coroutine.close(co) + assert(st and msg == nil) local main = coroutine.running() - -- cannot kill a "normal" coroutine + -- cannot close 'main' + local st, msg = pcall(coroutine.close, main); + assert(not st and string.find(msg, "main")) + + + -- cannot close a "normal" coroutine ;(coroutine.wrap(function () - local st, msg = pcall(coroutine.kill, main) + local st, msg = pcall(coroutine.close, main) assert(not st and string.find(msg, "normal")) end))() + do -- close a coroutine while closing it + local co + co = coroutine.create( + function() + local x = func2close(function() + coroutine.close(co) -- close it again + end) + coroutine.yield(20) + end) + local st, msg = coroutine.resume(co) + assert(st and msg == 20) + st, msg = coroutine.close(co) + assert(st and msg == nil) + end + -- to-be-closed variables in coroutines local X - local function func2close (f) - return setmetatable({}, {__close = f}) - end + -- closing a coroutine after an error + local co = coroutine.create(error) + local st, msg = coroutine.resume(co, 100) + assert(not st and msg == 100) + st, msg = coroutine.close(co) + assert(not st and msg == 100) + -- after closing, no more errors + st, msg = coroutine.close(co) + assert(st and msg == nil) co = coroutine.create(function () - local *toclose x = func2close(function (self, err) + local x = func2close(function (self, err) assert(err == nil); X = false end) X = true @@ -156,26 +206,141 @@ do end) coroutine.resume(co) assert(X) - assert(coroutine.kill(co)) + assert(coroutine.close(co)) assert(not X and coroutine.status(co) == "dead") - -- error killing a coroutine + -- error closing a coroutine + local x = 0 co = coroutine.create(function() - local *toclose x = func2close(function (self, err) + local y = func2close(function (self,err) + assert(err == 111) + x = 200 + error(200) + end) + local x = func2close(function (self, err) assert(err == nil); error(111) end) coroutine.yield() end) coroutine.resume(co) - local st, msg = coroutine.kill(co) - assert(not st and coroutine.status(co) == "dead" and msg == 111) + assert(x == 0) + local st, msg = coroutine.close(co) + assert(st == false and coroutine.status(co) == "dead" and msg == 200) + assert(x == 200) + -- after closing, no more errors + st, msg = coroutine.close(co) + assert(st and msg == nil) +end + +do + -- versus pcall in coroutines + local X = false + local Y = false + local function foo () + local x = func2close(function (self, err) + Y = debug.getinfo(2) + X = err + end) + error(43) + end + local co = coroutine.create(function () return pcall(foo) end) + local st1, st2, err = coroutine.resume(co) + assert(st1 and not st2 and err == 43) + assert(X == 43 and Y.what == "C") + + -- recovering from errors in __close metamethods + local track = {} + + local function h (o) + local hv = o + return 1 + end + + local function foo () + local x = func2close(function(_,msg) + track[#track + 1] = msg or false + error(20) + end) + local y = func2close(function(_,msg) + track[#track + 1] = msg or false + return 1000 + end) + local z = func2close(function(_,msg) + track[#track + 1] = msg or false + error(10) + end) + coroutine.yield(1) + h(func2close(function(_,msg) + track[#track + 1] = msg or false + error(2) + end)) + end + + local co = coroutine.create(pcall) + + local st, res = coroutine.resume(co, foo) -- call 'foo' protected + assert(st and res == 1) -- yield 1 + local st, res1, res2 = coroutine.resume(co) -- continue + assert(coroutine.status(co) == "dead") + assert(st and not res1 and res2 == 20) -- last error (20) + assert(track[1] == false and track[2] == 2 and track[3] == 10 and + track[4] == 10) +end + + +do print("coroutines closing itself") + global coroutine, string, os + global assert, error, pcall + + local X = nil + + local function new () + return coroutine.create(function (what) + + local var = func2close(function (t, err) + if what == "yield" then + coroutine.yield() + elseif what == "error" then + error(200) + else + X = "Ok" + return X + end + end) + + -- do an unprotected call so that coroutine becomes non-yieldable + string.gsub("a", "a", function () + assert(not coroutine.isyieldable()) + -- do protected calls while non-yieldable, to add recovery + -- entries (setjmp) to the stack + assert(pcall(pcall, function () + -- 'close' works even while non-yieldable + coroutine.close() -- close itself + os.exit(false) -- not reacheable + end)) + end) + end) + end + + local co = new() + local st, msg = coroutine.resume(co, "ret") + assert(st and msg == nil) + assert(X == "Ok") + + local co = new() + local st, msg = coroutine.resume(co, "error") + assert(not st and msg == 200) + + local co = new() + local st, msg = coroutine.resume(co, "yield") + assert(not st and string.find(msg, "attempt to yield")) end -- yielding across C boundaries -co = coroutine.wrap(function() +local co = coroutine.wrap(function() assert(not pcall(table.sort,{1,2,3}, coroutine.yield)) assert(coroutine.isyieldable()) coroutine.yield(20) @@ -203,15 +368,15 @@ local r1, r2, v = f1(nil) assert(r1 and not r2 and v[1] == (10 + 1)*10/2) -function f (a, b) a = coroutine.yield(a); error{a + b} end -function g(x) return x[1]*2 end +local function f (a, b) a = coroutine.yield(a); error{a + b} end +local function g(x) return x[1]*2 end co = coroutine.wrap(function () coroutine.yield(xpcall(f, g, 10, 20)) end) assert(co() == 10) -r, msg = co(100) +local r, msg = co(100) assert(not r and msg == 240) @@ -273,9 +438,10 @@ assert(not a and b == foo and coroutine.status(x) == "dead") a,b = coroutine.resume(x) assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead") +goo = nil -- co-routines x for loop -function all (a, n, k) +local function all (a, n, k) if k == 0 then coroutine.yield(a) else for i=1,n do @@ -315,7 +481,7 @@ assert(f() == 43 and f() == 53) -- old bug: attempt to resume itself -function co_func (current_co) +local function co_func (current_co) assert(coroutine.running() == current_co) assert(coroutine.resume(current_co) == false) coroutine.yield(10, 20) @@ -343,9 +509,29 @@ do local st, res = coroutine.resume(B) assert(st == true and res == false) - A = coroutine.wrap(function() return pcall(A, 1) end) + local X = false + A = coroutine.wrap(function() + local _ = func2close(function () X = true end) + return pcall(A, 1) + end) st, res = A() - assert(not st and string.find(res, "non%-suspended")) + assert(not st and string.find(res, "non%-suspended") and X == true) +end + + +-- bug in 5.4.1 +do + -- coroutine ran close metamethods with invalid status during a + -- reset. + local co + co = coroutine.wrap(function() + local x = func2close(function() return pcall(co) end) + error(111) + end) + local st, errobj = pcall(co) + assert(not st and errobj == 111) + st, errobj = pcall(co) + assert(not st and string.find(errobj, "dead coroutine")) end @@ -368,28 +554,49 @@ assert(not pcall(a, a)) a = nil +do + -- bug in 5.4: thread can use message handler higher in the stack + -- than the variable being closed + local c = coroutine.create(function() + local clo = setmetatable({}, {__close=function() + local x = 134 -- will overwrite message handler + error(x) + end}) + -- yields coroutine but leaves a new message handler for it, + -- that would be used when closing the coroutine (except that it + -- will be overwritten) + xpcall(coroutine.yield, function() return "XXX" end) + end) + + assert(coroutine.resume(c)) -- start coroutine + local st, msg = coroutine.close(c) + assert(not st and msg == 134) +end + -- access to locals of erroneous coroutines local x = coroutine.create (function () local a = 10 - _G.f = function () a=a+1; return a end + _G.F = function () a=a+1; return a end error('x') end) assert(not coroutine.resume(x)) -- overwrite previous position of local `a' assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1)) -assert(_G.f() == 11) -assert(_G.f() == 12) +assert(_G.F() == 11) +assert(_G.F() == 12) +_G.F = nil if not T then - (Message or print)('\n >>> testC not active: skipping yield/hook tests <<<\n') + (Message or print) + ('\n >>> testC not active: skipping coroutine API tests <<<\n') else print "testing yields inside hooks" local turn - - function fact (t, x) + + local function fact (t, x) assert(turn == t) if x == 0 then return 1 else return x*fact(t, x-1) @@ -411,10 +618,36 @@ else while A==0 or B==0 do -- A ~= 0 when 'x' finishes (similar for 'B','y') if A==0 then turn = "A"; assert(T.resume(x)) end if B==0 then turn = "B"; assert(T.resume(y)) end + + -- check that traceback works correctly after yields inside hooks + debug.traceback(x) + debug.traceback(y) end assert(B // A == 7) -- fact(7) // fact(6) + do -- hooks vs. multiple values + local done + local function test (n) + done = false + return coroutine.wrap(function () + local a = {} + for i = 1, n do a[i] = i end + -- 'pushint' just to perturb the stack + T.sethook("pushint 10; yield 0", "", 1) -- yield at each op. + local a1 = {table.unpack(a)} -- must keep top between ops. + assert(#a1 == n) + for i = 1, n do assert(a[i] == i) end + done = true + end) + end + -- arguments to the coroutine are just to perturb its stack + local co = test(0); while not done do co(30) end + co = test(1); while not done do co(20, 10) end + co = test(3); while not done do co() end + co = test(100); while not done do co() end + end + local line = debug.getinfo(1, "l").currentline + 2 -- get line number local function foo () local x = 10 --<< this line is 'line' @@ -432,6 +665,7 @@ else _G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil) _G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20) assert(co() == 10) + _G.X = nil -- testing yields in count hook co = coroutine.wrap(function () @@ -456,18 +690,22 @@ else -- (bug in 5.2/5.3) c = coroutine.create(function (a, ...) T.sethook("yield 0", "l") -- will yield on next two lines - assert(a == 10) + local b = a return ... end) assert(coroutine.resume(c, 1, 2, 3)) -- start coroutine local n,v = debug.getlocal(c, 0, 1) -- check its local - assert(n == "a" and v == 1) + assert(n == "a" and v == 1 and debug.getlocal(c, 0, 2) ~= "b") assert(debug.setlocal(c, 0, 1, 10)) -- test 'setlocal' local t = debug.getinfo(c, 0) -- test 'getinfo' - assert(t.currentline == t.linedefined + 1) + assert(t.currentline == t.linedefined + 2) assert(not debug.getinfo(c, 1)) -- no other level assert(coroutine.resume(c)) -- run next line + local n,v = debug.getlocal(c, 0, 2) -- check vararg table + assert(n == "(vararg table)" and v == nil) + local n,v = debug.getlocal(c, 0, 3) -- check next local + assert(n == "b" and v == 10) v = {coroutine.resume(c)} -- finish coroutine assert(v[1] == true and v[2] == 2 and v[3] == 3 and v[4] == undef) assert(not coroutine.resume(c)) @@ -486,7 +724,7 @@ else print "testing coroutine API" - + -- reusing a thread assert(T.testC([[ newthread # create thread @@ -509,6 +747,8 @@ else assert(X == 'a a a' and Y == 'OK') + X, Y = nil + -- resuming running coroutine C = coroutine.create(function () @@ -525,7 +765,7 @@ else c == "ERRRUN" and d == 4) a, b, c, d = T.testC([[ - rawgeti R 1 # get main thread + rawgeti R !M # get main thread pushnum 10; pushnum 20; resume -3 2; @@ -536,17 +776,26 @@ else c == "ERRRUN" and d == 4) - -- using a main thread as a coroutine + -- using a main thread as a coroutine (dubious use!) local state = T.newstate() - T.loadlib(state) + + -- check that yielddable is working correctly + assert(T.testC(state, "newthread; isyieldable -1; remove 1; return 1")) + + -- main thread is not yieldable + assert(not T.testC(state, "rawgeti R !M; isyieldable -1; remove 1; return 1")) + + T.testC(state, "settop 0") + + T.loadlib(state, 1 | 2, 4) -- load _G and 'package', preload 'coroutine' assert(T.doremote(state, [[ coroutine = require'coroutine'; X = function (x) coroutine.yield(x, 'BB'); return 'CC' end; return 'ok']])) - t = table.pack(T.testC(state, [[ - rawgeti R 1 # get main thread + local t = table.pack(T.testC(state, [[ + rawgeti R !M # get main thread pushstring 'XX' getglobal X # get function for body pushstring AA # arg @@ -555,7 +804,7 @@ else setglobal T # top setglobal B # second yielded value setglobal A # fist yielded value - rawgeti R 1 # get main thread + rawgeti R !M # get main thread pushnum 5 # arg (noise) resume 1 1 # after coroutine ends, previous stack is back pushstatus @@ -574,31 +823,28 @@ end -- leaving a pending coroutine open -_X = coroutine.wrap(function () +_G.TO_SURVIVE = coroutine.wrap(function () local a = 10 local x = function () a = a+1 end coroutine.yield() end) -_X() +_G.TO_SURVIVE() if not _soft then -- bug (stack overflow) - local j = 2^9 - local lim = 1000000 -- (C stack limit; assume 32-bit machine) - local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1} + local lim = 1000000 -- stack limit; assume 32-bit machine + local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1, lim + 5} for i = 1, #t do local j = t[i] - co = coroutine.create(function() - local t = {} - for i = 1, j do t[i] = i end - return table.unpack(t) + local co = coroutine.create(function() + return table.unpack({}, 1, j) end) local r, msg = coroutine.resume(co) - assert(not r) + -- must fail for unpacking larger than stack limit + assert(j < lim or not r) end - co = nil end @@ -696,6 +942,17 @@ assert(run(function () return a / b end, {"div"}) == 10/12) assert(run(function () return a % b end, {"mod"}) == 10) assert(run(function () return a // b end, {"idiv"}) == 0) +-- repeat tests with larger constants (to use 'K' opcodes) +local a1000 = new(1000) + +assert(run(function () return a1000 + 1000 end, {"add"}) == 2000) +assert(run(function () return a1000 - 25000 end, {"sub"}) == -24000) +assert(run(function () return 2000 * a end, {"mul"}) == 20000) +assert(run(function () return a1000 / 1000 end, {"div"}) == 1) +assert(run(function () return a1000 % 600 end, {"mod"}) == 400) +assert(run(function () return a1000 // 500 end, {"idiv"}) == 2) + + assert(run(function () return a % b end, {"mod"}) == 10) @@ -708,6 +965,12 @@ assert(run(function () return a >> b end, {"shr"}) == 10 >> 12) assert(run(function () return 10 & b end, {"band"}) == 10 & 12) assert(run(function () return a | 2 end, {"bor"}) == 10 | 2) assert(run(function () return a ~ 2 end, {"bxor"}) == 10 ~ 2) +assert(run(function () return a >> 2 end, {"shr"}) == 10 >> 2) +assert(run(function () return 1 >> a end, {"shr"}) == 1 >> 10) +assert(run(function () return a << 2 end, {"shl"}) == 10 << 2) +assert(run(function () return 1 << a end, {"shl"}) == 1 << 10) +assert(run(function () return 2 ~ a end, {"bxor"}) == 2 ~ 10) + assert(run(function () return a..b end, {"concat"}) == "1012") @@ -718,7 +981,7 @@ assert(run(function() return "a" .. "b" .. a .. "c" .. c .. b .. "x" end, {"concat", "concat", "concat"}) == "ab10chello12x") -do -- a few more tests for comparsion operators +do -- a few more tests for comparison operators local mt1 = { __le = function (a,b) coroutine.yield(10) @@ -739,7 +1002,7 @@ do -- a few more tests for comparsion operators until res ~= 10 return res end - + local function test () local a1 = setmetatable({x=1}, mt1) local a2 = setmetatable({x=2}, mt2) @@ -751,7 +1014,7 @@ do -- a few more tests for comparsion operators assert(2 >= a2) return true end - + run(test) end @@ -765,7 +1028,7 @@ assert(run(function () do local _ENV = _ENV f = function () AAA = BBB + 1; return AAA end end -g = new(10); g.k.BBB = 10; +local g = new(10); g.k.BBB = 10; debug.setupvalue(f, 1, g) assert(run(f, {"idx", "nidx", "idx"}) == 11) assert(g.k.AAA == 11) @@ -790,7 +1053,7 @@ assert(run(function () -- tests for coroutine API if T==nil then (Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n') - return + print "OK"; return end print('testing coroutine API') @@ -856,6 +1119,31 @@ f = T.makeCfunc([[ return * ]], 23, "huu") + +do -- testing bug introduced in commit f407b3c4a + local X = false -- flag "to be closed" + local coro = coroutine.wrap(T.testC) + -- runs it until 'pcallk' (that yields) + -- 4th argument (at index 4): object to be closed + local res1, res2 = coro( + [[ + toclose 3 # this could break the next 'pcallk' + pushvalue 2 # push function 'yield' to call it + pushint 22; pushint 33 # arguments to yield + # calls 'yield' (2 args; 2 results; continuation function at index 4) + pcallk 2 2 4 + invalid command (should not arrive here) + ]], -- 1st argument (at index 1): code; + coroutine.yield, -- (at index 2): function to be called + func2close(function () X = true end), -- (index 3): TBC slot + "pushint 43; return 3" -- (index 4): code for continuation function + ) + + assert(res1 == 22 and res2 == 33 and not X) + local res1, res2, res3 = coro(34, "hi") -- runs continuation function + assert(res1 == 34 and res2 == "hi" and res3 == 43 and X) +end + x = coroutine.wrap(f) assert(x() == 102) eqtab({x()}, {23, "huu"}) @@ -905,17 +1193,19 @@ assert(#a == 3 and a[1] == a[2] and a[2] == a[3] and a[3] == 34) -- testing yields with continuations +local y + co = coroutine.wrap(function (...) return T.testC([[ # initial function yieldk 1 2 cannot be here! ]], [[ # 1st continuation - yieldk 0 3 + yieldk 0 3 cannot be here! ]], [[ # 2nd continuation - yieldk 0 4 + yieldk 0 4 cannot be here! ]], [[ # 3th continuation @@ -957,6 +1247,9 @@ assert(x == "YIELD" and y == 4) assert(not pcall(co)) -- coroutine should be dead +_G.ctx = nil +_G.status = nil + -- bug in nCcalls local co = coroutine.wrap(function () diff --git a/testes/cstack.lua b/testes/cstack.lua index 9e5bbaefd7..0a68a30ac2 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -1,32 +1,38 @@ -- $Id: testes/cstack.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h -print"testing C-stack overflow detection" + +local tracegc = require"tracegc" + +print"testing stack overflow detection" -- Segmentation faults in these tests probably result from a C-stack --- overflow. To avoid these errors, recompile Lua with a smaller --- value for the constant 'LUAI_MAXCCALLS' or else ensure a larger --- stack for the program. +-- overflow. To avoid these errors, you should set a smaller limit for +-- the use of C stack by Lua, by changing the constant 'LUAI_MAXCCALLS'. +-- Alternatively, you can ensure a larger stack for the program. + local function checkerror (msg, f, ...) local s, err = pcall(f, ...) assert(not s and string.find(err, msg)) end - -do -- simple recursion +do print("testing stack overflow in message handling") local count = 0 - local function foo () + local function loop (x, y, z) count = count + 1 - foo() + return 1 + loop(x, y, z) end - checkerror("stack overflow", foo) - print(" maximum recursion: " .. count) + tracegc.stop() -- __gc should not be called with a full stack + local res, msg = xpcall(loop, loop) + tracegc.start() + assert(msg == "error in error handling") + print("final count: ", count) end -- bug since 2.5 (C-stack overflow in recursion inside pattern matching) -do +do print("testing recursion inside pattern matching") local function f (size) local s = string.rep("a", size) local p = string.rep(".?", size) @@ -34,29 +40,158 @@ do end local m = f(80) assert(#m == 80) - checkerror("too complex", f, 200000) + checkerror("too complex", f, 2000) end --- testing stack-overflow in recursive 'gsub' -do +do print("testing stack-overflow in recursive 'gsub'") local count = 0 local function foo () count = count + 1 string.gsub("a", ".", foo) end checkerror("stack overflow", foo) - print(" maximum 'gsub' nest (calls): " .. count) + print("final count: ", count) - -- can be done with metamethods, too - count = 0 + print("testing stack-overflow in recursive 'gsub' with metatables") + local count = 0 local t = setmetatable({}, {__index = foo}) foo = function () count = count + 1 string.gsub("a", ".", t) end checkerror("stack overflow", foo) - print(" maximum 'gsub' nest (metamethods): " .. count) + print("final count: ", count) +end + + +do -- bug in 5.4.0 + print("testing limits in coroutines inside deep calls") + local count = 0 + local lim = 1000 + local function stack (n) + if n > 0 then return stack(n - 1) + 1 + else coroutine.wrap(function () + count = count + 1 + stack(lim) + end)() + end + end + + local st, msg = xpcall(stack, function () return "ok" end, lim) + assert(not st and msg == "ok") + print("final count: ", count) +end + + +do -- bug since 5.4.0 + local count = 0 + print("chain of 'coroutine.close'") + -- create N coroutines forming a list so that each one, when closed, + -- closes the previous one. (With a large enough N, previous Lua + -- versions crash in this test.) + local coro = false + for i = 1, 1000 do + local previous = coro + coro = coroutine.create(function() + local cc = setmetatable({}, {__close=function() + count = count + 1 + if previous then + assert(coroutine.close(previous)) + end + end}) + coroutine.yield() -- leaves 'cc' pending to be closed + end) + assert(coroutine.resume(coro)) -- start it and run until it yields + end + local st, msg = coroutine.close(coro) + assert(not st and string.find(msg, "C stack overflow")) + print("final count: ", count) +end + + +do + print("nesting of resuming yielded coroutines") + local count = 0 + + local function body () + coroutine.yield() + local f = coroutine.wrap(body) + f(); -- start new coroutine (will stop in previous yield) + count = count + 1 + f() -- call it recursively + end + + local f = coroutine.wrap(body) + f() + assert(not pcall(f)) + print("final count: ", count) +end + + +do -- bug in 5.4.2 + print("nesting coroutines running after recoverable errors") + local count = 0 + local function foo() + count = count + 1 + pcall(1) -- create an error + -- running now inside 'precover' ("protected recover") + coroutine.wrap(foo)() -- call another coroutine + end + checkerror("C stack overflow", foo) + print("final count: ", count) +end + + +if T then + print("testing stack recovery") + local N = 0 -- trace number of calls + local LIM = -1 -- will store N just before stack overflow + + -- trace stack size; after stack overflow, it should be + -- the maximum allowed stack size. + local stack1 + local dummy + + local function err(msg) + assert(string.find(msg, "stack overflow")) + local _, stacknow = T.stacklevel() + assert(stacknow == stack1 + 200) + end + + -- When LIM==-1, the 'if' is not executed, so this function only + -- counts and stores the stack limits up to overflow. Then, LIM + -- becomes N, and then the 'if' code is run when the stack is + -- full. Then, there is a stack overflow inside 'xpcall', after which + -- the stack must have been restored back to its maximum normal size. + local function f() + dummy, stack1 = T.stacklevel() + if N == LIM then + xpcall(f, err) + local _, stacknow = T.stacklevel() + assert(stacknow == stack1) + return + end + N = N + 1 + f() + end + + local topB, sizeB -- top and size Before overflow + local topA, sizeA -- top and size After overflow + topB, sizeB = T.stacklevel() + tracegc.stop() -- __gc should not be called with a full stack + xpcall(f, err) + tracegc.start() + topA, sizeA = T.stacklevel() + -- sizes should be comparable + assert(topA == topB and sizeA < sizeB * 2) + print(string.format("maximum stack size: %d", stack1)) + LIM = N -- will stop recursion at maximum level + N = 0 -- to count again + tracegc.stop() -- __gc should not be called with a full stack + f() + tracegc.start() + print"+" end print'OK' diff --git a/testes/db.lua b/testes/db.lua index 976962b02f..e15a5be6bd 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -1,5 +1,5 @@ -- $Id: testes/db.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h -- testing debug library @@ -16,7 +16,7 @@ end assert(not debug.gethook()) local testline = 19 -- line where 'test' is defined -function test (s, l, p) -- this must be line 19 +local function test (s, l, p) -- this must be line 19 collectgarbage() -- avoid gc during trace local function f (event, line) assert(event == 'line') @@ -31,6 +31,7 @@ end do assert(not pcall(debug.getinfo, print, "X")) -- invalid option + assert(not pcall(debug.getinfo, 0, ">")) -- invalid option assert(not debug.getinfo(1000)) -- out of range level assert(not debug.getinfo(-1)) -- out of range level local a = debug.getinfo(print) @@ -48,8 +49,17 @@ do end +-- bug in 5.4.4-5.4.6: activelines in vararg functions +-- without debug information +do + local func = load(string.dump(load("print(10)"), true)) + local actl = debug.getinfo(func, "L").activelines + assert(#actl == 0) -- no line info +end + + -- test file and string names truncation -a = "function f () end" +local a = "function f () end" local function dostring (s, x) return load(s, x)() end dostring(a) assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a)) @@ -71,7 +81,8 @@ dostring(a, string.format("=%s", string.rep('x', 500))) assert(string.find(debug.getinfo(f).short_src, "^x*$")) dostring(a, "=") assert(debug.getinfo(f).short_src == "") -a = nil; f = nil; +_G.a = nil; _G.f = nil; +_G[string.rep("p", 400)] = nil repeat @@ -117,7 +128,19 @@ then else a=2 end -]], {2,3,4,7}) +]], {2,4,7}) + + +test([[ +local function foo() +end +foo() +A = 1 +A = 2 +A = 3 +]], {2, 3, 2, 4, 5, 6}) +_G.A = nil + test([[-- if nil then @@ -162,7 +185,9 @@ test([[for i,v in pairs{'a','b'} do end ]], {1,2,1,2,1,3}) -test([[for i=1,4 do a=1 end]], {1,1,1,1,1}) +test([[for i=1,4 do a=1 end]], {1,1,1,1}) + +_G.a = nil do -- testing line info/trace with large gaps in source @@ -183,6 +208,50 @@ do -- testing line info/trace with large gaps in source end end end +_G.a = nil + + +do -- testing active lines + local function checkactivelines (f, lines) + local t = debug.getinfo(f, "SL") + for _, l in pairs(lines) do + l = l + t.linedefined + assert(t.activelines[l]) + t.activelines[l] = undef + end + assert(next(t.activelines) == nil) -- no extra lines + end + + checkactivelines(function (...) -- vararg function + -- 1st line is empty + -- 2nd line is empty + -- 3th line is empty + local a = 20 + -- 5th line is empty + local b = 30 + -- 7th line is empty + end, {4, 6, 8}) + + checkactivelines(function (a) + -- 1st line is empty + -- 2nd line is empty + local a = 20 + local b = 30 + -- 5th line is empty + end, {3, 4, 6}) + + checkactivelines(function (a, b, ...) end, {0}) + + checkactivelines(function (a, b) + end, {1}) + + for _, n in pairs{0, 1, 2, 10, 50, 100, 1000, 10000} do + checkactivelines( + load(string.format("%s return 1", string.rep("\n", n))), + {n + 1}) + end + +end print'+' @@ -233,7 +302,6 @@ foo(200, 3, 4) local a = {} for i = 1, (_soft and 100 or 1000) do a[i] = i end foo(table.unpack(a)) -a = nil @@ -253,9 +321,14 @@ do -- test hook presence in debug info debug.sethook() assert(count == 4) end +_ENV.a = nil + +-- hook table has weak keys +assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k') -a = {}; L = nil + +a = {}; local L = nil local glob = 1 local oldglob = glob debug.sethook(function (e,l) @@ -276,12 +349,15 @@ end, "crl") function f(a,b) + -- declare some globals to check that they don't interfere with 'getlocal' + global collectgarbage collectgarbage() local _, x = debug.getlocal(1, 1) + global assert, g, string local _, y = debug.getlocal(1, 2) assert(x == a and y == b) - assert(debug.setlocal(2, 3, "pera") == "AA".."AA") - assert(debug.setlocal(2, 4, "maçã") == "B") + assert(debug.setlocal(2, 4, "pera") == "AA".."AA") + assert(debug.setlocal(2, 5, "manga") == "B") x = debug.getinfo(2) assert(x.func == g and x.what == "Lua" and x.name == 'g' and x.nups == 2 and string.find(x.source, "^@.*db%.lua$")) @@ -296,7 +372,7 @@ function foo() end; foo() -- set L -- check line counting inside strings and empty lines -_ = 'alo\ +local _ = 'alo\ alo' .. [[ ]] @@ -309,12 +385,14 @@ function g (...) local arg = {...} do local a,b,c; a=math.sin(40); end local feijao - local AAAA,B = "xuxu", "mamão" + local AAAA,B = "xuxu", "abacate" f(AAAA,B) - assert(AAAA == "pera" and B == "maçã") + assert(AAAA == "pera" and B == "manga") do + global * local B = 13 - local x,y = debug.getlocal(1,5) + global assert + local x,y = debug.getlocal(1,6) assert(x == 'B' and y == 13) end end @@ -345,19 +423,20 @@ function g(a,b) return (a+1) + f() end assert(g(0,0) == 30) +_G.f, _G.g = nil debug.sethook(nil); -assert(debug.gethook() == nil) +assert(not debug.gethook()) -- minimal tests for setuservalue/getuservalue do - assert(debug.setuservalue(io.stdin, 10) == nil) + assert(not debug.setuservalue(io.stdin, 10)) local a, b = debug.getuservalue(io.stdin, 10) assert(a == nil and not b) end --- testing iteraction between multiple values x hooks +-- testing interaction between multiple values x hooks do local function f(...) return 3, ... end local count = 0 @@ -379,7 +458,8 @@ local function collectlocals (level) local tab = {} for i = 1, math.huge do local n, v = debug.getlocal(level + 1, i) - if not (n and string.find(n, "^[a-zA-Z0-9_]+$")) then + if not (n and string.find(n, "^[a-zA-Z0-9_]+$") or + n == "(vararg table)") then break -- consider only real variables end tab[n] = v @@ -388,7 +468,7 @@ local function collectlocals (level) end -X = nil +local X = nil a = {} function a:f (a, b, ...) local arg = {...}; local c = 13 end debug.sethook(function (e) @@ -410,7 +490,8 @@ end, "c") a:f(1,2,3,4,5) assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) assert(XX == 12) -assert(debug.gethook() == nil) +assert(not debug.gethook()) +_G.XX = nil -- testing access to local variables in return hook (bug in 5.2) @@ -512,7 +593,7 @@ t = getupvalues(foo2) assert(t.a == 1 and t.b == 2 and t.c == 3) assert(debug.setupvalue(foo1, 1, "xuxu") == "b") assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu") --- upvalues of C functions are allways "called" "" (the empty string) +-- upvalues of C functions are always named "" (the empty string) assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "") @@ -535,6 +616,7 @@ end debug.sethook() +local g, g1 -- tests for tail calls local function f (x) @@ -548,6 +630,9 @@ local function f (x) end end +assert(debug.getinfo(print, 't').istailcall == false) +assert(debug.getinfo(print, 't').extraargs == 0) + function g(x) return f(x) end function g1(x) g(x) end @@ -580,7 +665,7 @@ h(false) debug.sethook() assert(b == 2) -- two tail calls -lim = _soft and 3000 or 30000 +local lim = _soft and 3000 or 30000 local function foo (x) if x==0 then assert(debug.getinfo(2).what == "main") @@ -622,7 +707,7 @@ assert(debug.traceback(print, 4) == print) assert(string.find(debug.traceback("hi", 4), "^hi\n")) assert(string.find(debug.traceback("hi"), "^hi\n")) assert(not string.find(debug.traceback("hi"), "'debug.traceback'")) -assert(string.find(debug.traceback("hi", 0), "'debug.traceback'")) +assert(string.find(debug.traceback("hi", 0), "'traceback'")) assert(string.find(debug.traceback(), "^stack traceback:\n")) do -- C-function names in traceback @@ -641,10 +726,18 @@ assert(t.isvararg == false and t.nparams == 3 and t.nups == 0) t = debug.getinfo(function (a,b,...) return t[a] end, "u") assert(t.isvararg == true and t.nparams == 2 and t.nups == 1) +t = debug.getinfo(function (a,b,...t) t.n = 2; return t[a] end, "u") +assert(t.isvararg == true and t.nparams == 2 and t.nups == 0) + t = debug.getinfo(1) -- main assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and debug.getupvalue(t.func, 1) == "_ENV") +t = debug.getinfo(math.sin) -- C function +assert(t.isvararg == true and t.nparams == 0 and t.nups == 0) + +t = debug.getinfo(string.gmatch("abc", "a")) -- C closure +assert(t.isvararg == true and t.nparams == 0 and t.nups > 0) @@ -734,22 +827,28 @@ a, b = coroutine.resume(co, 100) assert(a and b == 30) --- check traceback of suspended coroutines +-- check traceback of suspended (or dead with error) coroutines + +function f(i) + if i == 0 then error(i) + else coroutine.yield(); f(i-1) + end +end -function f(i) coroutine.yield(i == 0); f(i - 1) end co = coroutine.create(function (x) f(x) end) a, b = coroutine.resume(co, 3) -t = {"'coroutine.yield'", "'f'", "in function <"} -repeat +t = {"'yield'", "'f'", "in function <"} +while coroutine.status(co) == "suspended" do checktraceback(co, t) a, b = coroutine.resume(co) table.insert(t, 2, "'f'") -- one more recursive call to 'f' -until b +end +t[1] = "'error'" checktraceback(co, t) --- test acessing line numbers of a coroutine from a resume inside +-- test accessing line numbers of a coroutine from a resume inside -- a C function (this is a known bug in Lua 5.0) local function g(x) @@ -796,11 +895,12 @@ assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and -a == "unm" and #a == "len" and a&3 == "band") assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and -a == "unm" and #a == "len" and a & 3 == "band") -assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shift" and - a>>1 == "shift") +assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shl" and a>>1 == "shr") assert (a==b and a.op == "eq") -assert (a>=b and a.op == "order") -assert (a>b and a.op == "order") +assert (a>=b and a.op == "le") +assert ("x">=a and a.op == "le") +assert (a>b and a.op == "lt") +assert (a>10 and a.op == "lt") assert(~a == "bnot") do -- testing for-iterator name @@ -817,7 +917,7 @@ do -- testing debug info for finalizers -- create a piece of garbage with a finalizer setmetatable({}, {__gc = function () - local t = debug.getinfo(2) -- get callee information + local t = debug.getinfo(1) -- get function information assert(t.namewhat == "metamethod") name = t.name end}) @@ -849,7 +949,7 @@ do local cl = countlines(rest) -- at most 10 lines in first part, 11 in second, plus '...' assert(cl <= 10 + 11 + 1) - local brk = string.find(rest, "%.%.%.") + local brk = string.find(rest, "%.%.%.\t%(skip") if brk then -- does message have '...'? local rest1 = string.sub(rest, 1, brk) local rest2 = string.sub(rest, brk, #rest) @@ -870,14 +970,14 @@ end print("testing debug functions on chunk without debug info") -prog = [[-- program to be loaded without debug information +local prog = [[-- program to be loaded without debug information (strip) local debug = require'debug' local a = 12 -- a local variable local n, v = debug.getlocal(1, 1) -assert(n == "(temporary)" and v == debug) -- unkown name but known value +assert(n == "(temporary)" and v == debug) -- unknown name but known value n, v = debug.getlocal(1, 2) -assert(n == "(temporary)" and v == 12) -- unkown name but known value +assert(n == "(temporary)" and v == 12) -- unknown name but known value -- a function with an upvalue local f = function () local x; return a end @@ -913,6 +1013,23 @@ local f = assert(load(string.dump(load(prog), true))) assert(f() == 13) +do -- bug in 5.4.0: line hooks in stripped code + local function foo () + local a = 1 + local b = 2 + return b + end + + local s = load(string.dump(foo, true)) + local line = true + debug.sethook(function (e, l) + assert(e == "line") + line = l + end, "l") + assert(s() == 2); debug.sethook(nil) + assert(line == nil) -- hook called without debug info for 1st instruction +end + do -- tests for 'source' in binary dumps local prog = [[ return function (x) diff --git a/testes/errors.lua b/testes/errors.lua index 74975e3143..c9d850994b 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -1,5 +1,5 @@ -- $Id: testes/errors.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print("testing errors") @@ -18,14 +18,15 @@ end local function doit (s) local f, msg = load(s) - if f == nil then return msg end + if not f then return msg end local cond, msg = pcall(f) return (not cond) and msg end -local function checkmessage (prog, msg) +local function checkmessage (prog, msg, debug) local m = doit(prog) + if debug then print(m, msg) end assert(string.find(m, msg, 1, true)) end @@ -44,8 +45,8 @@ end -- test error message with no extra info assert(doit("error('hi', 0)") == 'hi') --- test error message with no info -assert(doit("error()") == nil) +-- test nil error message +assert(doit("error()") == "") -- test common errors/errors that crashed in the past @@ -67,9 +68,30 @@ checksyntax([[ ]], "'}' expected (to close '{' at line 1)", "", 3) +do -- testing errors in goto/break + local function checksyntax (prog, msg, line) + local st, err = load(prog) + assert(string.find(err, "line " .. line)) + assert(string.find(err, msg, 1, true)) + end + + checksyntax([[ + ::A:: a = 1 + ::A:: + ]], "label 'A' already defined", 1) + + checksyntax([[ + a = 1 + goto A + do ::A:: end + ]], "no visible label 'A'", 2) + +end + + if not T then (Message or print) - ('\n >>> testC not active: skipping memory message test <<<\n') + ('\n >>> testC not active: skipping tests for messages in C <<<\n') else print "testing memory error message" local a = {} @@ -82,6 +104,44 @@ else end) T.totalmem(0) assert(not st and msg == "not enough" .. " memory") + + -- stack space for luaL_traceback (bug in 5.4.6) + local res = T.testC[[ + # push 16 elements on the stack + pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1; + pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1; + pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1; + pushnum 1; + # traceback should work with 4 remaining slots + traceback xuxu 1; + return 1 + ]] + assert(string.find(res, "xuxu.-main chunk")) + + do -- tests for error messages about extra arguments from __call + local function createobj (n) + -- function that raises an error on its n-th argument + local code = string.format("argerror %d 'msg'", n) + local func = T.makeCfunc(code) + -- create a chain of 2 __call objects + local M = setmetatable({}, {__call = func}) + M = setmetatable({}, {__call = M}) + -- put it as a method for a new object + return {foo = M} + end + + _G.a = createobj(1) -- error in first (extra) argument + checkmessage("a:foo()", "bad extra argument #1") + + _G.a = createobj(2) -- error in second (extra) argument + checkmessage("a:foo()", "bad extra argument #2") + + _G.a = createobj(3) -- error in self (after two extra arguments) + checkmessage("a:foo()", "bad self") + + _G.a = createobj(4) -- error in first regular argument (after self) + checkmessage("a:foo()", "bad argument #1") + end end @@ -92,17 +152,41 @@ checkmessage("a = {} | 1", "bitwise operation") checkmessage("a = {} < 1", "attempt to compare") checkmessage("a = {} <= 1", "attempt to compare") -checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'") -checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'") +checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'") +checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'") checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") -assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'")) -checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number") -checkmessage("a=(1)..{}", "a table value") +assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'")) +checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number") +checkmessage("aaa=(1)..{}", "a table value") + +-- bug in 5.4.6 +checkmessage("a = {_ENV = {}}; print(a._ENV.x + 1)", "field 'x'") -checkmessage("a = #print", "length of a function value") -checkmessage("a = #3", "length of a number value") +-- a similar bug, since 5.4.0 +checkmessage("print(('_ENV').x + 1)", "field 'x'") + +_G.aaa, _G.bbbb = nil + +-- calls +checkmessage("local a; a(13)", "local 'a'") +checkmessage([[ + local a = setmetatable({}, {__add = 34}) + a = a + 1 +]], "metamethod 'add'") +checkmessage([[ + local a = setmetatable({}, {__lt = {}}) + a = a > a +]], "metamethod 'lt'") + +-- tail calls +checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'") +checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'") + +checkmessage("aaa = #print", "length of a function value") +checkmessage("aaa = #3", "length of a number value") + +_G.aaa = nil -aaa = nil checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'") @@ -115,15 +199,16 @@ checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'") checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") -checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'") +checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'") checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'") -checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'") +checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'") checkmessage("aaa={}; x=-aaa", "global 'aaa'") -- short circuit -checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)", +checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)", "local 'bbbb'") -checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'") +checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)", + "local 'bbbb'") checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'")) @@ -150,8 +235,15 @@ checkmessage("return ~-3e40", "has no integer representation") checkmessage("return ~-3.009", "has no integer representation") checkmessage("return 3.009 & 1", "has no integer representation") checkmessage("return 34 >> {}", "table value") -checkmessage("a = 24 // 0", "divide by zero") -checkmessage("a = 1 % 0", "'n%0'") +checkmessage("aaa = 24 // 0", "divide by zero") +checkmessage("aaa = 1 % 0", "'n%0'") + + +-- type error for an object which is neither in an upvalue nor a register. +-- The following code will try to index the value 10 that is stored in +-- the metatable, without moving it to a register. +checkmessage("local a = setmetatable({}, {__index = 10}).x", + "attempt to index a number value") -- numeric for loops @@ -184,6 +276,22 @@ do -- named objects (field '__name') checkmessage("return {} < XX", "table with My Type") checkmessage("return XX < io.stdin", "My Type with FILE*") _G.XX = nil + + if T then -- extra tests for 'luaL_tolstring' + -- bug in 5.4.3; 'luaL_tolstring' with negative indices + local x = setmetatable({}, {__name="TABLE"}) + assert(T.testC("Ltolstring -1; return 1", x) == tostring(x)) + + local a, b = T.testC("pushint 10; Ltolstring -2; return 2", x) + assert(a == 10 and b == tostring(x)) + + setmetatable(x, {__tostring=function (o) + assert(o == x) + return "ABC" + end}) + local a, b, c = T.testC("pushint 10; Ltolstring -2; return 3", x) + assert(a == x and b == 10 and c == "ABC") + end end -- global functions @@ -195,28 +303,29 @@ do local f = function (a) return a + 1 end f = assert(load(string.dump(f, true))) assert(f(3) == 4) - checkerr("^%?:%-1:", f, {}) + checkerr("^%?:%?:", f, {}) -- code with a move to a local var ('OP_MOV A B' with A a, b +]], 1, "multiple") if not _soft then - -- several tests that exaust the Lua stack + -- several tests that exhaust the Lua stack collectgarbage() print"testing stack overflow" - C = 0 - local l = debug.getinfo(1, "l").currentline; function y () C=C+1; y() end + local C = 0 + -- get line where stack overflow will happen + local l = debug.getinfo(1, "l").currentline + 1 + local function auxy () C=C+1; auxy() end -- produce a stack overflow + function YY () + collectgarbage("stop") -- avoid running finalizers without stack space + auxy() + collectgarbage("restart") + end local function checkstackmessage (m) + print("(expected stack overflow after " .. C .. " calls)") + C = 0 -- prepare next count return (string.find(m, "stack overflow")) end -- repeated stack overflows (to check stack recovery) - assert(checkstackmessage(doit('y()'))) - print('+') - assert(checkstackmessage(doit('y()'))) - print('+') - assert(checkstackmessage(doit('y()'))) - print('+') + assert(checkstackmessage(doit('YY()'))) + assert(checkstackmessage(doit('YY()'))) + assert(checkstackmessage(doit('YY()'))) + + _G.YY = nil -- error lines in stack overflow - C = 0 local l1 local function g(x) - l1 = debug.getinfo(x, "l").currentline; y() + l1 = debug.getinfo(x, "l").currentline + 2 + collectgarbage("stop") -- avoid running finalizers without stack space + auxy() + collectgarbage("restart") end local _, stackmsg = xpcall(g, debug.traceback, 1) print('+') @@ -419,7 +586,7 @@ if not _soft then -- error in error handling local res, msg = xpcall(error, error) - assert(not res and type(msg) == 'string') + assert(not res and msg == 'error in error handling') print('+') local function f (x) @@ -450,6 +617,27 @@ if not _soft then end +do -- errors in error handle that not necessarily go forever + local function err (n) -- function to be used as message handler + -- generate an error unless n is zero, so that there is a limited + -- loop of errors + if type(n) ~= "number" then -- some other error? + return n -- report it + elseif n == 0 then + return "END" -- that will be the final message + else error(n - 1) -- does the loop + end + end + + local res, msg = xpcall(error, err, 170) + assert(not res and msg == "END") + + -- too many levels + local res, msg = xpcall(error, err, 300) + assert(not res and msg == "C stack overflow") +end + + do -- non string messages local t = {} @@ -457,7 +645,7 @@ do assert(not res and msg == t) res, msg = pcall(function () error(nil) end) - assert(not res and msg == nil) + assert(not res and msg == "") local function f() error{msg='x'} end res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end) @@ -477,7 +665,7 @@ do assert(not res and msg == t) res, msg = pcall(assert, nil, nil) - assert(not res and msg == nil) + assert(not res and type(msg) == "string") -- 'assert' without arguments res, msg = pcall(assert) @@ -485,7 +673,7 @@ do end -- xpcall with arguments -a, b, c = xpcall(string.find, error, "alo", "al") +local a, b, c = xpcall(string.find, error, "alo", "al") assert(a and b == 1 and c == 2) a, b, c = xpcall(string.find, function (x) return {} end, true, "al") assert(not a and type(b) == "table" and c == nil) @@ -505,11 +693,12 @@ checksyntax("a\1a = 1", "", "<\\1>", 1) -- test 255 as first char in a chunk checksyntax("\255a = 1", "", "<\\255>", 1) -doit('I = load("a=9+"); a=3') -assert(a==3 and I == nil) +doit('I = load("a=9+"); aaa=3') +assert(_G.aaa==3 and not _G.I) +_G.I,_G.aaa = nil print('+') -lim = 1000 +local lim = 1000 if _soft then lim = 100 end for i=1,lim do doit('a = ') @@ -519,25 +708,34 @@ end -- testing syntax limits -local function testrep (init, rep, close, repc) - local s = init .. string.rep(rep, 100) .. close .. string.rep(repc, 100) - assert(load(s)) -- 100 levels is OK - s = init .. string.rep(rep, 10000) - local res, msg = load(s) -- 10000 levels not ok - assert(not res and (string.find(msg, "too many registers") or - string.find(msg, "stack overflow"))) +local function testrep (init, rep, close, repc, finalresult) + local function gencode (n) + return init .. string.rep(rep, n) .. close .. string.rep(repc, n) + end + local res, msg = load(gencode(100)) -- 100 levels is OK + assert(res) + if (finalresult) then + assert(res() == finalresult) + end + local res, msg = load(gencode(500)) -- 500 levels not ok + assert(not res and (string.find(msg, "too many") or + string.find(msg, "overflow"))) end +testrep("local a", ",a", ";", "") -- local variables +testrep("local a", ",a", "= 1", ",1") -- local variables initialized +testrep("local a", ",a", "= f()", "") -- local variables initialized testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment -testrep("local a; a=", "{", "0", "}") -testrep("local a; a=", "(", "2", ")") -testrep("local a; ", "a(", "2", ")") +testrep("local a; a=", "{", "0", "}") -- constructors +testrep("return ", "(", "2", ")", 2) -- parentheses +-- nested calls (a(a(a(a(...))))) +testrep("local function a (x) return x end; return ", "a(", "2.2", ")", 2.2) testrep("", "do ", "", " end") testrep("", "while a do ", "", " end") testrep("local a; ", "if a then else ", "", " end") testrep("", "function foo () ", "", " end") -testrep("local a; a=", "a..", "a", "") -testrep("local a; a=", "a^", "a", "") +testrep("local a = ''; return ", "a..", "'a'", "", "a") +testrep("local a = 1; return ", "a^", "a", "", 1) checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers") @@ -569,7 +767,7 @@ assert(c > 255 and string.find(b, "too many upvalues") and -- local variables s = "\nfunction foo ()\n local " -for j = 1,300 do +for j = 1,200 do s = s.."a"..j..", " end s = s.."b\n" diff --git a/testes/events.lua b/testes/events.lua index ac630d895c..7e434b1f6f 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -1,5 +1,5 @@ -- $Id: testes/events.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print('testing metatables') @@ -138,64 +138,55 @@ t.__bxor = f("bxor") t.__shl = f("shl") t.__shr = f("shr") t.__bnot = f("bnot") +t.__lt = f("lt") +t.__le = f("le") + + +local function checkcap (t) + assert(#cap + 1 == #t) + for i = 1, #t do + assert(cap[i - 1] == t[i]) + assert(math.type(cap[i - 1]) == math.type(t[i])) + end +end -- Some tests are done inside small anonymous functions to ensure -- that constants go to constant table even in debug compilation, -- when the constant table is very small. -assert(b+5 == b) -assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==undef) -assert(5.2 + b == 5.2) -assert(cap[0] == "add" and cap[1] == 5.2 and cap[2] == b and cap[3]==undef) -assert(b+'5' == b) -assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==undef) -assert(5+b == 5) -assert(cap[0] == "add" and cap[1] == 5 and cap[2] == b and cap[3]==undef) -assert('5'+b == '5') -assert(cap[0] == "add" and cap[1] == '5' and cap[2] == b and cap[3]==undef) -b=b-3; assert(getmetatable(b) == t) -assert(cap[0] == "sub" and cap[1] == b and cap[2] == 3 and cap[3]==undef) -assert(5-a == 5) -assert(cap[0] == "sub" and cap[1] == 5 and cap[2] == a and cap[3]==undef) -assert('5'-a == '5') -assert(cap[0] == "sub" and cap[1] == '5' and cap[2] == a and cap[3]==undef) -assert(a*a == a) -assert(cap[0] == "mul" and cap[1] == a and cap[2] == a and cap[3]==undef) -assert(a/0 == a) -assert(cap[0] == "div" and cap[1] == a and cap[2] == 0 and cap[3]==undef) -assert(a%2 == a) -assert(cap[0] == "mod" and cap[1] == a and cap[2] == 2 and cap[3]==undef) -assert(a // (1/0) == a) -assert(cap[0] == "idiv" and cap[1] == a and cap[2] == 1/0 and cap[3]==undef) -;(function () assert(a & "hi" == a) end)() -assert(cap[0] == "band" and cap[1] == a and cap[2] == "hi" and cap[3]==undef) -;(function () assert(10 & a == 10) end)() -assert(cap[0] == "band" and cap[1] == 10 and cap[2] == a and cap[3]==undef) -;(function () assert(a | 10 == a) end)() -assert(cap[0] == "bor" and cap[1] == a and cap[2] == 10 and cap[3]==undef) -assert(a | "hi" == a) -assert(cap[0] == "bor" and cap[1] == a and cap[2] == "hi" and cap[3]==undef) -assert("hi" ~ a == "hi") -assert(cap[0] == "bxor" and cap[1] == "hi" and cap[2] == a and cap[3]==undef) -;(function () assert(10 ~ a == 10) end)() -assert(cap[0] == "bxor" and cap[1] == 10 and cap[2] == a and cap[3]==undef) -assert(-a == a) -assert(cap[0] == "unm" and cap[1] == a) -assert(a^4 == a) -assert(cap[0] == "pow" and cap[1] == a and cap[2] == 4 and cap[3]==undef) -assert(a^'4' == a) -assert(cap[0] == "pow" and cap[1] == a and cap[2] == '4' and cap[3]==undef) -assert(4^a == 4) -assert(cap[0] == "pow" and cap[1] == 4 and cap[2] == a and cap[3]==undef) -assert('4'^a == '4') -assert(cap[0] == "pow" and cap[1] == '4' and cap[2] == a and cap[3]==undef) -assert(#a == a) -assert(cap[0] == "len" and cap[1] == a) -assert(~a == a) -assert(cap[0] == "bnot" and cap[1] == a) -assert(a << 3 == a) -assert(cap[0] == "shl" and cap[1] == a and cap[2] == 3) -assert(1.5 >> a == 1.5) -assert(cap[0] == "shr" and cap[1] == 1.5 and cap[2] == a) +assert(b+5 == b); checkcap{"add", b, 5} +assert(5.2 + b == 5.2); checkcap{"add", 5.2, b} +assert(b+'5' == b); checkcap{"add", b, '5'} +assert(5+b == 5); checkcap{"add", 5, b} +assert('5'+b == '5'); checkcap{"add", '5', b} +b=b-3; assert(getmetatable(b) == t); checkcap{"sub", b, 3} +assert(5-a == 5); checkcap{"sub", 5, a} +assert('5'-a == '5'); checkcap{"sub", '5', a} +assert(a*a == a); checkcap{"mul", a, a} +assert(a/0 == a); checkcap{"div", a, 0} +assert(a/0.0 == a); checkcap{"div", a, 0.0} +assert(a%2 == a); checkcap{"mod", a, 2} +assert(a // (1/0) == a); checkcap{"idiv", a, 1/0} +;(function () assert(a & "hi" == a) end)(); checkcap{"band", a, "hi"} +;(function () assert(10 & a == 10) end)(); checkcap{"band", 10, a} +;(function () assert(a | 10 == a) end)(); checkcap{"bor", a, 10} +assert(a | "hi" == a); checkcap{"bor", a, "hi"} +assert("hi" ~ a == "hi"); checkcap{"bxor", "hi", a} +;(function () assert(10 ~ a == 10) end)(); checkcap{"bxor", 10, a} +assert(-a == a); checkcap{"unm", a, a} +assert(a^4.0 == a); checkcap{"pow", a, 4.0} +assert(a^'4' == a); checkcap{"pow", a, '4'} +assert(4^a == 4); checkcap{"pow", 4, a} +assert('4'^a == '4'); checkcap{"pow", '4', a} +assert(#a == a); checkcap{"len", a, a} +assert(~a == a); checkcap{"bnot", a, a} +assert(a << 3 == a); checkcap{"shl", a, 3} +assert(1.5 >> a == 1.5); checkcap{"shr", 1.5, a} + +-- for comparison operators, all results are true +assert(5.0 > a); checkcap{"lt", a, 5.0} +assert(a >= 10); checkcap{"le", 10, a} +assert(a <= -10.0); checkcap{"le", a, -10.0} +assert(a < -10); checkcap{"lt", a, -10} -- test for rawlen @@ -226,9 +217,16 @@ t.__le = function (a,b,c) return a<=b, "dummy" end +t.__eq = function (a,b,c) + assert(c == nil) + if type(a) == 'table' then a = a.x end + if type(b) == 'table' then b = b.x end + return a == b, "dummy" +end + function Op(x) return setmetatable({x=x}, t) end -local function test () +local function test (a, b, c) assert(not(Op(1)= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1)) assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a'))) assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a'))) + assert(Op(1) == Op(1) and Op(1) ~= Op(2)) + assert(Op('a') == Op('a') and Op('a') ~= Op('b')) + assert(a == a and a ~= b) + assert(Op(3) == c) end -test() +test(Op(1), Op(2), Op(3)) + + +do -- test nil as false + local x = setmetatable({12}, {__eq= function (a,b) + return a[1] == b[1] or nil + end}) + assert(not (x == {20})) + assert(x == {12}) +end -- test `partial order' @@ -303,6 +314,17 @@ t[Set{1,3,5}] = 1 assert(t[Set{1,3,5}] == undef) +do -- test invalidating flags + local mt = {__eq = true} + local a = setmetatable({10}, mt) + local b = setmetatable({10}, mt) + mt.__eq = nil + assert(a ~= b) -- no metamethod + mt.__eq = function (x,y) return x[1] == y[1] end + assert(a == b) -- must use metamethod now +end + + if not T then (Message or print)('\n >>> testC not active: skipping tests for \z userdata <<<\n') @@ -323,6 +345,7 @@ else assert(u1 == u3 and u3 == u1 and u1 ~= u2) assert(u2 == u1 and u2 == u3 and u3 == u2) assert(u2 ~= {}) -- different types cannot be equal + assert(rawequal(u1, u1) and not rawequal(u1, u3)) local mirror = {} debug.setmetatable(u3, {__index = mirror, __newindex = mirror}) @@ -356,6 +379,17 @@ x = 0 .."a".."b"..c..d.."e".."f".."g" assert(x.val == "0abcdefg") +do + -- bug since 5.4.1 (test needs T) + local mt = setmetatable({__newindex={}}, {__mode='v'}) + local t = setmetatable({}, mt) + + if T then T.allocfailnext() end + + -- seg. fault + for i=1, 10 do t[i] = 1 end +end + -- concat metamethod x numbers (bug in 5.1.1) c = {} local x @@ -406,6 +440,9 @@ assert(i == 3 and x[1] == 3 and x[3] == 5) assert(_G.X == 20) +_G.X, _G.B = nil + + print'+' local _g = _G @@ -455,7 +492,7 @@ assert(not pcall(function (a,b) return a[b] end, a, 10)) assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true)) -- bug in 5.1 -T, K, V = nil +local T, K, V = nil grandparent = {} grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end diff --git a/testes/files.lua b/testes/files.lua index 34fcf85134..7146ac7ca2 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -1,5 +1,7 @@ -- $Id: testes/files.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h + +global * local debug = require "debug" @@ -74,6 +76,8 @@ io.input(io.stdin); io.output(io.stdout); os.remove(file) assert(not loadfile(file)) +-- Lua code cannot use chunks with fixed buffers +checkerr("invalid mode", load, "", "", "B") checkerr("", dofile, file) assert(not io.open(file)) io.output(file) @@ -92,8 +96,8 @@ assert(io.output():seek("end") == string.len("alo joao")) assert(io.output():seek("set") == 0) -assert(io.write('"álo"', "{a}\n", "second line\n", "third line \n")) -assert(io.write('çfourth_line')) +assert(io.write('"alo"', "{a}\n", "second line\n", "third line \n")) +assert(io.write('Xfourth_line')) io.output(io.stdout) collectgarbage() -- file should be closed by GC assert(io.input() == io.stdin and rawequal(io.output(), io.stdout)) @@ -125,7 +129,7 @@ do -- closing file by scope local F = nil do - local *toclose f = assert(io.open(file, "w")) + local f = assert(io.open(file, "w")) F = f end assert(tostring(F) == "file (closed)") @@ -135,7 +139,7 @@ assert(os.remove(file)) do -- test writing/reading numbers - local *toclose f = assert(io.open(file, "w")) + local f = assert(io.open(file, "w")) f:write(maxint, '\n') f:write(string.format("0X%x\n", maxint)) f:write("0xABCp-3", '\n') @@ -144,7 +148,7 @@ do f:write(string.format("0x%X\n", -maxint)) f:write("-0xABCp-3", '\n') assert(f:close()) - f = assert(io.open(file, "r")) + local f = assert(io.open(file, "r")) assert(f:read("n") == maxint) assert(f:read("n") == maxint) assert(f:read("n") == 0xABCp-3) @@ -158,7 +162,7 @@ assert(os.remove(file)) -- testing multiple arguments to io.read do - local *toclose f = assert(io.open(file, "w")) + local f = assert(io.open(file, "w")) f:write[[ a line another line @@ -170,21 +174,21 @@ three ]] local l1, l2, l3, l4, n1, n2, c, dummy assert(f:close()) - f = assert(io.open(file, "r")) + local f = assert(io.open(file, "r")) l1, l2, n1, n2, dummy = f:read("l", "L", "n", "n") assert(l1 == "a line" and l2 == "another line\n" and n1 == 1234 and n2 == 3.45 and dummy == nil) assert(f:close()) - f = assert(io.open(file, "r")) + local f = assert(io.open(file, "r")) l1, l2, n1, n2, c, l3, l4, dummy = f:read(7, "l", "n", "n", 1, "l", "l") assert(l1 == "a line\n" and l2 == "another line" and c == '\n' and n1 == 1234 and n2 == 3.45 and l3 == "one" and l4 == "two" and dummy == nil) assert(f:close()) - f = assert(io.open(file, "r")) + local f = assert(io.open(file, "r")) -- second item failing l1, n1, n2, dummy = f:read("l", "n", "n", "l") - assert(l1 == "a line" and n1 == nil) + assert(l1 == "a line" and not n1) end assert(os.remove(file)) @@ -228,7 +232,7 @@ assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n") assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e") do -- attempt to read too long number - assert(f:read("n") == nil) -- fails + assert(not f:read("n")) -- fails local s = f:read("L") -- read rest of line assert(string.find(s, "^00*\n$")) -- lots of 0's left end @@ -300,27 +304,27 @@ do -- test error returns end checkerr("invalid format", io.read, "x") assert(io.read(0) == "") -- not eof -assert(io.read(5, 'l') == '"álo"') +assert(io.read(5, 'l') == '"alo"') assert(io.read(0) == "") assert(io.read() == "second line") local x = io.input():seek() assert(io.read() == "third line ") assert(io.input():seek("set", x)) assert(io.read('L') == "third line \n") -assert(io.read(1) == "ç") +assert(io.read(1) == "X") assert(io.read(string.len"fourth_line") == "fourth_line") assert(io.input():seek("cur", -string.len"fourth_line")) assert(io.read() == "fourth_line") assert(io.read() == "") -- empty line assert(io.read('n') == 3450) assert(io.read(1) == '\n') -assert(io.read(0) == nil) -- end of file -assert(io.read(1) == nil) -- end of file -assert(io.read(30000) == nil) -- end of file +assert(not io.read(0)) -- end of file +assert(not io.read(1)) -- end of file +assert(not io.read(30000)) -- end of file assert(({io.read(1)})[2] == undef) -assert(io.read() == nil) -- end of file +assert(not io.read()) -- end of file assert(({io.read()})[2] == undef) -assert(io.read('n') == nil) -- end of file +assert(not io.read('n')) -- end of file assert(({io.read('n')})[2] == undef) assert(io.read('a') == '') -- end of file (OK for 'a') assert(io.read('a') == '') -- end of file (OK for 'a') @@ -345,7 +349,7 @@ collectgarbage() assert(io.write(' ' .. t .. ' ')) assert(io.write(';', 'end of file\n')) -f:flush(); io.flush() +assert(f:flush()); assert(io.flush()) f:close() print('+') @@ -356,7 +360,7 @@ assert(io.read(string.len(t)) == t) assert(io.read(1) == ' ') assert(io.read(0)) assert(io.read('a') == ';end of file\n') -assert(io.read(0) == nil) +assert(not io.read(0)) assert(io.close(io.input())) @@ -364,7 +368,7 @@ assert(io.close(io.input())) do local function ismsg (m) -- error message is not a code number - return (type(m) == "string" and tonumber(m) == nil) + return (type(m) == "string" and not tonumber(m)) end -- read @@ -393,7 +397,7 @@ assert(io.read"L" == "\n") assert(io.read"L" == "\n") assert(io.read"L" == "line\n") assert(io.read"L" == "other") -assert(io.read"L" == nil) +assert(not io.read"L") io.input():close() local f = assert(io.open(file)) @@ -427,10 +431,12 @@ do -- testing closing file in line iteration -- get the to-be-closed variable from a loop local function gettoclose (lv) lv = lv + 1 - for i = 1, math.maxinteger do + local stvar = 0 -- to-be-closed is 3th state variable in the loop + for i = 1, 1000 do local n, v = debug.getlocal(lv, i) - if n == "(for toclose)" then - return v + if n == "(for state)" then + stvar = stvar + 1 + if stvar == 3 then return v end end end end @@ -457,10 +463,27 @@ do -- testing closing file in line iteration end --- test for multipe arguments in 'lines' +do print("testing flush") + local f = io.output("/dev/null") + assert(f:write("abcd")) -- write to buffer + assert(f:flush()) -- write to device + assert(f:write("abcd")) -- write to buffer + assert(io.flush()) -- write to device + assert(f:close()) + + local f = io.output("/dev/full") + assert(f:write("abcd")) -- write to buffer + assert(not f:flush()) -- cannot write to device + assert(f:write("abcd")) -- write to buffer + assert(not io.flush()) -- cannot write to device + assert(f:close()) +end + + +-- test for multiple arguments in 'lines' io.output(file); io.write"0123456789\n":close() for a,b in io.lines(file, 1, 1) do - if a == "\n" then assert(b == nil) + if a == "\n" then assert(not b) else assert(tonumber(a) == tonumber(b) - 1) end end @@ -471,13 +494,13 @@ end for a,b,c in io.lines(file, "a", 0, 1) do if a == "" then break end - assert(a == "0123456789\n" and b == nil and c == nil) + assert(a == "0123456789\n" and not b and not c) end collectgarbage() -- to close file in previous iteration io.output(file); io.write"00\n10\n20\n30\n40\n":close() for a, b in io.lines(file, "n", "n") do - if a == 40 then assert(b == nil) + if a == 40 then assert(not b) else assert(a == b - 10) end end @@ -505,15 +528,17 @@ load((io.lines(file, 1)))() assert(_G.X == 4) load((io.lines(file, 3)))() assert(_G.X == 8) +_G.X = nil print('+') local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'" io.output(file) -assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1))) +assert(io.write(string.format("X2 = %q\n-- comment without ending EOS", x1))) io.close() assert(loadfile(file))() -assert(x1 == x2) +assert(x1 == _G.X2) +_G.X2 = nil print('+') assert(os.remove(file)) assert(not os.remove(file)) @@ -652,7 +677,7 @@ and the rest of the file io.input(file) local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10) assert(io.close(io.input())) -assert(_ == ' ' and __ == nil) +assert(_ == ' ' and not __) assert(type(a) == 'number' and a==123.4 and b==-56e-2) assert(d=='second line' and e=='third line') assert(h==[[ @@ -690,6 +715,37 @@ do end +if T and T.nonblock and not _port then + print("testing failed write") + + -- unable to write anything to /dev/full + local f = io.open("/dev/full", "w") + assert(f:setvbuf("no")) + local _, _, err, count = f:write("abcd") + assert(err > 0 and count == 0) + assert(f:close()) + + -- receiver will read a "few" bytes (enough to empty a large buffer) + local receiver = [[ + lua -e 'assert(io.stdin:setvbuf("no")); assert(#io.read(1e4) == 1e4)' ]] + + local f = io.popen(receiver, "w") + assert(f:setvbuf("no")) + T.nonblock(f) + + -- able to write a few bytes + assert(f:write(string.rep("a", 1e2))) + + -- Unable to write more bytes than the pipe buffer supports. + -- (In Linux, the pipe buffer size is 64K (2^16). Posix requires at + -- least 512 bytes.) + local _, _, err, count = f:write("abcd", string.rep("a", 2^17)) + assert(err > 0 and count >= 512 and count < 2^17) + + assert(f:close()) +end + + if not _soft then print("testing large files (> BUFSIZ)") io.output(file) @@ -704,7 +760,7 @@ if not _soft then io.input():seek('set', 0) y = io.read() -- huge line assert(x == y..'\n'..io.read()) - assert(io.read() == nil) + assert(not io.read()) io.close(io.input()) assert(os.remove(file)) x = nil; y = nil @@ -719,6 +775,21 @@ if not _port then progname = '"' .. arg[i + 1] .. '"' end print("testing popen/pclose and execute") + -- invalid mode for popen + checkerr("invalid mode", io.popen, "cat", "") + checkerr("invalid mode", io.popen, "cat", "r+") + checkerr("invalid mode", io.popen, "cat", "rw") + do -- basic tests for popen + local file = os.tmpname() + local f = assert(io.popen("cat - > " .. file, "w")) + f:write("a line") + assert(f:close()) + local f = assert(io.popen("cat - < " .. file, "r")) + assert(f:read("a") == "a line") + assert(f:close()) + assert(os.remove(file)) + end + local tests = { -- command, what, code {"ls > /dev/null", "ok"}, @@ -745,6 +816,7 @@ if not _port then assert((v[3] == nil and z > 0) or v[3] == z) end end + print("(done)") end @@ -768,16 +840,29 @@ assert(os.date("!\0\0") == "\0\0") local x = string.rep("a", 10000) assert(os.date(x) == x) local t = os.time() -D = os.date("*t", t) +global D = os.date("*t", t) assert(os.date(string.rep("%d", 1000), t) == string.rep(os.date("%d", t), 1000)) assert(os.date(string.rep("%", 200)) == string.rep("%", 100)) -local t = os.time() -D = os.date("*t", t) -load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and - D.hour==%H and D.min==%M and D.sec==%S and - D.wday==%w+1 and D.yday==%j and type(D.isdst) == 'boolean')]], t))() +local function checkDateTable (t) + D = os.date("*t", t) + assert(os.time(D) == t) + load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and + D.hour==%H and D.min==%M and D.sec==%S and + D.wday==%w+1 and D.yday==%j)]], t))() + _G.D = nil +end + +checkDateTable(os.time()) +if not _port then + -- assume that time_t can represent these values + checkDateTable(0) + checkDateTable(1) + checkDateTable(1000) + checkDateTable(0x7fffffff) + checkDateTable(0x80000000) +end checkerr("invalid conversion specifier", os.date, "%") checkerr("invalid conversion specifier", os.date, "%9") @@ -791,11 +876,33 @@ checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour=1.5}) checkerr("missing", os.time, {hour = 12}) -- missing date + +if string.packsize("i") == 4 then -- 4-byte ints + checkerr("field 'year' is out-of-bound", os.time, + {year = -(1 << 31) + 1899, month = 1, day = 1}) + + checkerr("field 'year' is out-of-bound", os.time, + {year = -(1 << 31), month = 1, day = 1}) + + if math.maxinteger > 2^31 then -- larger lua_integer? + checkerr("field 'year' is out-of-bound", os.time, + {year = (1 << 31) + 1900, month = 1, day = 1}) + end +end + + if not _port then -- test Posix-specific modifiers assert(type(os.date("%Ex")) == 'string') assert(type(os.date("%Oy")) == 'string') + -- test large dates (assume at least 4-byte ints and time_t) + local t0 = os.time{year = 1970, month = 1, day = 0} + local t1 = os.time{year = 1970, month = 1, day = 0, sec = (1 << 31) - 1} + assert(t1 - t0 == (1 << 31) - 1) + t0 = os.time{year = 1970, month = 1, day = 1} + t1 = os.time{year = 1970, month = 1, day = 1, sec = -(1 << 31)} + assert(t1 - t0 == -(1 << 31)) -- test out-of-range dates (at least for Unix) if maxint >= 2^62 then -- cannot do these tests in Small Lua @@ -810,34 +917,51 @@ if not _port then -- time_t has 8 bytes; an int year cannot represent a huge time print(" 8-byte time_t") checkerr("cannot be represented", os.date, "%Y", 2^60) - -- it should have no problems with year 4000 - assert(tonumber(os.time{year=4000, month=1, day=1})) + + -- this is the maximum year + assert(tonumber(os.time + {year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=59})) + + -- this is too much + checkerr("represented", os.time, + {year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=60}) end + + -- internal 'int' fields cannot hold these values + checkerr("field 'day' is out-of-bound", os.time, + {year = 0, month = 1, day = 2^32}) + + checkerr("field 'month' is out-of-bound", os.time, + {year = 0, month = -((1 << 31) + 1), day = 1}) + + checkerr("field 'year' is out-of-bound", os.time, + {year = (1 << 31) + 1900, month = 1, day = 1}) + else -- 8-byte ints -- assume time_t has 8 bytes too print(" 8-byte time_t") assert(tonumber(os.date("%Y", 2^60))) + -- but still cannot represent a huge year checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1}) end end end - -D = os.date("!*t", t) -load(os.date([[!assert(D.year==%Y and D.month==%m and D.day==%d and - D.hour==%H and D.min==%M and D.sec==%S and - D.wday==%w+1 and D.yday==%j and type(D.isdst) == 'boolean')]], t))() - do local D = os.date("*t") local t = os.time(D) - assert(type(D.isdst) == 'boolean') + if D.isdst == nil then + print("no daylight saving information") + else + assert(type(D.isdst) == 'boolean') + end D.isdst = nil local t1 = os.time(D) assert(t == t1) -- if isdst is absent uses correct default end +local D = os.date("*t") t = os.time(D) D.year = D.year-1; local t1 = os.time(D) diff --git a/testes/gc.lua b/testes/gc.lua index 91e78a4822..62713dac64 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -1,5 +1,5 @@ -- $Id: testes/gc.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print('testing incremental garbage collection') @@ -27,27 +27,54 @@ end -- test weird parameters to 'collectgarbage' do - -- save original parameters - local a = collectgarbage("setpause", 200) - local b = collectgarbage("setstepmul", 200) + collectgarbage("incremental") + local opause = collectgarbage("param", "pause", 100) + local ostepmul = collectgarbage("param", "stepmul", 100) + assert(collectgarbage("param", "pause") == 100) + assert(collectgarbage("param", "stepmul") == 100) local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe} for i = 1, #t do - local p = t[i] + collectgarbage("param", "pause", t[i]) for j = 1, #t do - local m = t[j] - collectgarbage("setpause", p) - collectgarbage("setstepmul", m) - collectgarbage("step", 0) - collectgarbage("step", 10000) + collectgarbage("param", "stepmul", t[j]) + collectgarbage("step", t[j]) end end -- restore original parameters - collectgarbage("setpause", a) - collectgarbage("setstepmul", b) + collectgarbage("param", "pause", opause) + collectgarbage("param", "stepmul", ostepmul) collectgarbage() end +-- +-- test the "size" of basic GC steps (whatever they mean...) +-- +do print("steps") + + local function dosteps (siz) + collectgarbage() + local a = {} + for i=1,100 do a[i] = {{}}; local b = {} end + local x = gcinfo() + local i = 0 + repeat -- do steps until it completes a collection cycle + i = i+1 + until collectgarbage("step", siz) + assert(gcinfo() < x) + return i -- number of steps + end + + + if not _port then + collectgarbage"stop" + assert(dosteps(10) < dosteps(2)) + collectgarbage"restart" + end + +end + + _G["while"] = 234 @@ -99,40 +126,33 @@ local function GC() GC1(); GC2() end do print("creating many objects") - local contCreate = 0 - local limit = 5000 - while contCreate <= limit do + for i = 1, limit do local a = {}; a = nil - contCreate = contCreate+1 end local a = "a" - contCreate = 0 - while contCreate <= limit do - a = contCreate .. "b"; - a = string.gsub(a, '(%d%d*)', string.upper) + for i = 1, limit do + a = i .. "b"; + a = string.gsub(a, '(%d%d*)', "%1 %1") a = "a" - contCreate = contCreate+1 end - contCreate = 0 a = {} function a:test () - while contCreate <= limit do - load(string.format("function temp(a) return 'a%d' end", contCreate), "")() - assert(temp() == string.format('a%d', contCreate)) - contCreate = contCreate+1 + for i = 1, limit do + load(string.format("function temp(a) return 'a%d' end", i), "")() + assert(temp() == string.format('a%d', i)) end end a:test() - + _G.temp = nil end @@ -141,7 +161,7 @@ do local f = function () end end print("functions with errors") -prog = [[ +local prog = [[ do a = 10; function foo(x,y) @@ -160,61 +180,24 @@ do end end end +rawset(_G, "a", nil) +_G.x = nil -foo = nil -print('long strings') -x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" -assert(string.len(x)==80) -s = '' -n = 0 -k = math.min(300, (math.maxinteger // 80) // 2) -while n < k do s = s..x; n=n+1; j=tostring(n) end -assert(string.len(s) == k*80) -s = string.sub(s, 1, 10000) -s, i = string.gsub(s, '(%d%d%d%d)', '') -assert(i==10000 // 4) -s = nil -x = nil - -assert(_G["while"] == 234) - - --- --- test the "size" of basic GC steps (whatever they mean...) --- do -print("steps") - - print("steps (2)") - - local function dosteps (siz) - collectgarbage() - local a = {} - for i=1,100 do a[i] = {{}}; local b = {} end - local x = gcinfo() - local i = 0 - repeat -- do steps until it completes a collection cycle - i = i+1 - until collectgarbage("step", siz) - assert(gcinfo() < x) - return i -- number of steps - end - - collectgarbage"stop" - - if not _port then - assert(dosteps(10) < dosteps(2)) - end - - -- collector should do a full collection with so many steps - assert(dosteps(20000) == 1) - assert(collectgarbage("step", 20000) == true) - assert(collectgarbage("step", 20000) == true) - - assert(not collectgarbage("isrunning")) - collectgarbage"restart" - assert(collectgarbage("isrunning")) - + foo = nil + print('long strings') + local x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" + assert(string.len(x)==80) + local s = '' + local k = math.min(300, (math.maxinteger // 80) // 2) + for n = 1, k do s = s..x; local j=tostring(n) end + assert(string.len(s) == k*80) + s = string.sub(s, 1, 10000) + local s, i = string.gsub(s, '(%d%d%d%d)', '') + assert(i==10000 // 4) + + assert(_G["while"] == 234) + _G["while"] = nil end @@ -235,8 +218,8 @@ end print("clearing tables") -lim = 15 -a = {} +local lim = 15 +local a = {} -- fill a with `collectable' indices for i=1,lim do a[{}] = i end b = {} @@ -305,6 +288,21 @@ x,y,z=nil collectgarbage() assert(next(a) == string.rep('$', 11)) +do -- invalid mode + local a = setmetatable({}, {__mode = 34}) + collectgarbage() +end + + +if T then -- bug since 5.3: all-weak tables are not being revisited + T.gcstate("propagate") + local t = setmetatable({}, {__mode = "kv"}) + T.gcstate("enteratomic") -- 't' was visited + setmetatable(t, {__mode = "kv"}) + T.gcstate("pause") -- its new metatable is not being visited + assert(getmetatable(t).__mode == "kv") +end + -- 'bug' in 5.1 a = {} @@ -377,9 +375,10 @@ if T then s[n] = i end + warn("@on"); warn("@store") collectgarbage() - assert(string.find(_WARN, "error in __gc metamethod")) - assert(string.match(_WARN, "@(.-)@") == "expected") + assert(string.find(_WARN, "error in __gc")) + assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = false for i = 8, 10 do assert(s[i]) end for i = 1, 5 do @@ -391,6 +390,7 @@ if T then for i = 1, 10 do assert(s[i]) end getmetatable(u).__gc = nil + warn("@normal") end print '+' @@ -461,12 +461,9 @@ do -- tests for string keys in weak tables local m = collectgarbage("count") -- current memory local a = setmetatable({}, {__mode = "kv"}) a[string.rep("a", 2^22)] = 25 -- long string key -> number value - a[string.rep("b", 2^22)] = {} -- long string key -> colectable value - a[{}] = 14 -- colectable key - assert(collectgarbage("count") > m + 2^13) -- 2^13 == 2 * 2^22 in KB + a[string.rep("b", 2^22)] = {} -- long string key -> collectable value + a[{}] = 14 -- collectable key collectgarbage() - assert(collectgarbage("count") >= m + 2^12 and - collectgarbage("count") < m + 2^13) -- one key was collected local k, v = next(a) -- string key with number value preserved assert(k == string.rep("a", 2^22) and v == 25) assert(next(a, k) == nil) -- everything else cleared @@ -477,15 +474,18 @@ do -- tests for string keys in weak tables assert(next(a) == nil) -- make sure will not try to compare with dead key assert(a[string.rep("b", 100)] == undef) - assert(collectgarbage("count") <= m + 1) -- eveything collected + assert(collectgarbage("count") <= m + 1) -- everything collected end -- errors during collection if T then + warn("@store") u = setmetatable({}, {__gc = function () error "@expected error" end}) u = nil collectgarbage() + assert(string.find(_WARN, "@expected error")); _WARN = false + warn("@normal") end @@ -539,7 +539,7 @@ do local co = coroutine.create(f) assert(coroutine.resume(co, co)) end - -- Now, thread and closure are not reacheable any more. + -- Now, thread and closure are not reachable any more. collectgarbage() assert(collected) collectgarbage("restart") @@ -549,12 +549,13 @@ end do collectgarbage() collectgarbage"stop" - collectgarbage("step", 0) -- steps should not unblock the collector + collectgarbage("step") -- steps should not unblock the collector local x = gcinfo() repeat for i=1,1000 do _ENV.a = {} end -- no collection during the loop until gcinfo() > 2 * x collectgarbage"restart" + _ENV.a = nil end @@ -574,8 +575,8 @@ if T then -- tests for weird cases collecting upvalues -- create coroutine in a weak table, so it will never be marked t.co = coroutine.wrap(foo) local f = t.co() -- create function to access local 'a' - T.gcstate("atomic") -- ensure all objects are traversed - assert(T.gcstate() == "atomic") + T.gcstate("enteratomic") -- ensure all objects are traversed + assert(T.gcstate() == "enteratomic") assert(t.co() == 100) -- resume coroutine, creating new table for 'a' assert(T.gccolor(t.co) == "white") -- thread was not traversed T.gcstate("pause") -- collect thread, but should mark 'a' before that @@ -588,7 +589,7 @@ if T then -- tests for weird cases collecting upvalues collectgarbage() collectgarbage"stop" local a = {} -- avoid 'u' as first element in 'allgc' - T.gcstate"atomic" + T.gcstate"enteratomic" T.gcstate"sweepallgc" local x = {} assert(T.gccolor(u) == "black") -- userdata is "old" (black) @@ -614,6 +615,21 @@ if T then end +if T then + collectgarbage("stop") + T.gcstate("pause") + local sup = {x = 0} + local a = setmetatable({}, {__newindex = sup}) + T.gcstate("enteratomic") + assert(T.gccolor(sup) == "black") + a.x = {} -- should not break the invariant + assert(not (T.gccolor(sup) == "black" and T.gccolor(sup.x) == "white")) + T.gcstate("pause") -- complete the GC cycle + sup.x.y = 10 + collectgarbage("restart") +end + + if T then print("emergency collections") collectgarbage() @@ -643,7 +659,7 @@ do assert(getmetatable(o) == tt) -- create new objects during GC local a = 'xuxu'..(10+3)..'joao', {} - ___Glob = o -- ressurect object! + ___Glob = o -- resurrect object! setmetatable({}, tt) -- creates a new one with same metatable print(">>> closing state " .. "<<<\n") end @@ -653,20 +669,21 @@ end -- create several objects to raise errors when collected while closing state if T then - local error, assert, find = error, assert, string.find + local error, assert, find, warn = error, assert, string.find, warn local n = 0 local lastmsg local mt = {__gc = function (o) n = n + 1 assert(n == o[1]) if n == 1 then - _WARN = nil + _WARN = false elseif n == 2 then assert(find(_WARN, "@expected warning")) lastmsg = _WARN -- get message from previous error (first 'o') else assert(lastmsg == _WARN) -- subsequent error messages are equal end + warn("@store"); _WARN = false error"@expected warning" end} for i = 10, 1, -1 do @@ -678,6 +695,16 @@ end -- just to make sure assert(collectgarbage'isrunning') +do -- check that the collector is not reentrant in incremental mode + local res = true + setmetatable({}, {__gc = function () + res = collectgarbage() + end}) + collectgarbage() + assert(not res) +end + + collectgarbage(oldmode) print('OK') diff --git a/testes/gengc.lua b/testes/gengc.lua index b02f471b36..6509e39d8a 100644 --- a/testes/gengc.lua +++ b/testes/gengc.lua @@ -1,5 +1,5 @@ -- $Id: testes/gengc.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print('testing generational garbage collection') @@ -24,12 +24,12 @@ do assert(not T or (T.gcage(U) == "touched1" and T.gcage(U[1]) == "new")) -- both U and the table survive one more collection - collectgarbage("step", 0) + collectgarbage("step") assert(not T or (T.gcage(U) == "touched2" and T.gcage(U[1]) == "survival")) -- both U and the table survive yet another collection -- now everything is old - collectgarbage("step", 0) + collectgarbage("step") assert(not T or (T.gcage(U) == "old" and T.gcage(U[1]) == "old1")) -- data was not corrupted @@ -37,6 +37,92 @@ do end +do + -- ensure that 'firstold1' is corrected when object is removed from + -- the 'allgc' list + local function foo () end + local old = {10} + collectgarbage() -- make 'old' old + assert(not T or T.gcage(old) == "old") + setmetatable(old, {}) -- new table becomes OLD0 (barrier) + assert(not T or T.gcage(getmetatable(old)) == "old0") + collectgarbage("step") -- new table becomes OLD1 and firstold1 + assert(not T or T.gcage(getmetatable(old)) == "old1") + setmetatable(getmetatable(old), {__gc = foo}) -- get it out of allgc list + collectgarbage("step") -- should not seg. fault +end + + +do -- bug in 5.4.0 +-- When an object aged OLD1 is finalized, it is moved from the list +-- 'finobj' to the *beginning* of the list 'allgc', but that part of the +-- list was not being visited by 'markold'. + local A = {} + A[1] = false -- old anchor for object + + -- obj finalizer + local function gcf (obj) + A[1] = obj -- anchor object + assert(not T or T.gcage(obj) == "old1") + obj = nil -- remove it from the stack + collectgarbage("step") -- do a young collection + print(getmetatable(A[1]).x) -- metatable was collected + end + + collectgarbage() -- make A old + local obj = {} -- create a new object + collectgarbage("step") -- make it a survival + assert(not T or T.gcage(obj) == "survival") + setmetatable(obj, {__gc = gcf, x = "+"}) -- create its metatable + assert(not T or T.gcage(getmetatable(obj)) == "new") + obj = nil -- clear object + collectgarbage("step") -- will call obj's finalizer +end + + +do -- another bug in 5.4.0 + local old = {10} + collectgarbage() -- make 'old' old + local co = coroutine.create( + function () + local x = nil + local f = function () + return x[1] + end + x = coroutine.yield(f) + coroutine.yield() + end + ) + local _, f = coroutine.resume(co) -- create closure over 'x' in coroutine + collectgarbage("step") -- make upvalue a survival + old[1] = {"hello"} -- 'old' go to grayagain as 'touched1' + coroutine.resume(co, {123}) -- its value will be new + co = nil + collectgarbage("step") -- hit the barrier + assert(f() == 123 and old[1][1] == "hello") + collectgarbage("step") -- run the collector once more + -- make sure old[1] was not collected + assert(f() == 123 and old[1][1] == "hello") +end + + +do -- bug introduced in commit 9cf3299fa + local t = setmetatable({}, {__mode = "kv"}) -- all-weak table + collectgarbage() -- full collection + assert(not T or T.gcage(t) == "old") + t[1] = {10} + assert(not T or (T.gcage(t) == "touched1" and T.gccolor(t) == "gray")) + collectgarbage("step") -- minor collection + assert(not T or (T.gcage(t) == "touched2" and T.gccolor(t) == "black")) + collectgarbage("step") -- minor collection + assert(not T or T.gcage(t) == "old") -- t should be black, but it was gray + t[1] = {10} -- no barrier here, so t was still old + collectgarbage("step") -- minor collection + -- t, being old, is ignored by the collection, so it is not cleared + assert(t[1] == nil) -- fails with the bug +end + + if T == nil then (Message or print)('\n >>> testC not active: \z skipping some generational tests <<<\n') @@ -58,13 +144,13 @@ do T.gcage(debug.getuservalue(U)) == "new") -- both U and the table survive one more collection - collectgarbage("step", 0) + collectgarbage("step") assert(T.gcage(U) == "touched2" and T.gcage(debug.getuservalue(U)) == "survival") -- both U and the table survive yet another collection -- now everything is old - collectgarbage("step", 0) + collectgarbage("step") assert(T.gcage(U) == "old" and T.gcage(debug.getuservalue(U)) == "old1") @@ -72,11 +158,38 @@ do assert(debug.getuservalue(U).x[1] == 234) end - - -- just to make sure assert(collectgarbage'isrunning') + +do print"testing stop-the-world collection" + local step = collectgarbage("param", "stepsize", 0); + collectgarbage("incremental") + assert(collectgarbage("param", "stepsize") == 0) + + -- each step does a complete cycle + assert(collectgarbage("step")) + assert(collectgarbage("step")) + + -- back to default value + collectgarbage("param", "stepsize", step); + assert(collectgarbage("param", "stepsize") == step) +end + + +if T then -- test GC parameter codification + for _, percentage in ipairs{5, 10, 12, 20, 50, 100, 200, 500} do + local param = T.codeparam(percentage) -- codify percentage + for _, value in ipairs{1, 2, 10, 100, 257, 1023, 6500, 100000} do + local exact = value*percentage // 100 + local aprox = T.applyparam(param, value) -- apply percentage + -- difference is at most 10% (+1 compensates difference due to + -- rounding to integers) + assert(math.abs(aprox - exact) <= exact/10 + 1) + end + end +end + collectgarbage(oldmode) print('OK') diff --git a/testes/goto.lua b/testes/goto.lua index f3dcfd4a3e..906208b553 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -1,5 +1,11 @@ -- $Id: testes/goto.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h + +global require +global print, load, assert, string, setmetatable +global collectgarbage, error + +print("testing goto and global declarations") collectgarbage() @@ -17,15 +23,18 @@ errmsg([[ ::l1:: ::l1:: ]], "label 'l1'") errmsg([[ ::l1:: do ::l1:: end]], "label 'l1'") --- undefined label -errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "local 'aa'") --- jumping over variable definition +-- jumping over variable declaration +errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "scope of 'aa'") + +errmsg([[ goto l2; global *; ::l1:: ::l2:: print(3) ]], "scope of '*'") + errmsg([[ do local bb, cc; goto l1; end local aa ::l1:: print(3) -]], "local 'aa'") +]], "scope of 'aa'") + -- jumping into a block errmsg([[ do ::l1:: end goto l1 ]], "label 'l1'") @@ -38,7 +47,7 @@ errmsg([[ local xuxu = 10 ::cont:: until xuxu < x -]], "local 'xuxu'") +]], "scope of 'xuxu'") -- simple gotos local x @@ -250,22 +259,219 @@ assert(testG(3) == "3") assert(testG(4) == 5) assert(testG(5) == 10) -do - -- if x back goto out of scope of upvalue - local X +do -- test goto's around to-be-closed variable + + global * + + -- set 'var' and return an object that will reset 'var' when + -- it goes out of scope + local function newobj (var) + _ENV[var] = true + return setmetatable({}, {__close = function () + _ENV[var] = nil + end}) + end + goto L1 - ::L2:: goto L3 + ::L4:: assert(not varX); goto L5 -- varX dead here + + ::L1:: + local varX = newobj("X") + assert(varX); goto L2 -- varX alive here + + ::L3:: + assert(varX); goto L4 -- varX alive here + + ::L2:: assert(varX); goto L3 -- varX alive here + + ::L5:: -- return +end + + + +foo() +-------------------------------------------------------------------------- + +-- check for compilation errors +local function checkerr (code, err) + local st, msg = load(code) + assert(not st and string.find(msg, err)) +end + +do + global T + + -- globals must be declared, after a global declaration + checkerr("global none; X = 1", "variable 'X'") + checkerr("global none; function XX() end", "variable 'XX'") + + -- global variables cannot be to-be-closed + checkerr("global X", "cannot be") + checkerr("global *", "cannot be") + + do + local X = 10 + do global X; X = 20 end + assert(X == 10) -- local X + end + assert(_ENV.X == 20) -- global X + + -- '_ENV' cannot be global + checkerr("global _ENV, a; a = 10", "variable 'a'") + + -- global declarations inside functions + checkerr([[ + global none + local function foo () XXX = 1 end --< ERROR]], "variable 'XXX'") + + if not T then -- when not in "test mode", "global" isn't reserved + assert(load("global = 1; return global")() == 1) + print " ('global' is not a reserved word)" + else + -- "global" reserved, cannot be used as a variable + assert(not load("global = 1; return global")) + end + + local foo = 20 + do + global function foo (x) + if x == 0 then return 1 else return 2 * foo(x - 1) end + end + assert(foo == _ENV.foo and foo(4) == 16) + end + assert(_ENV.foo(4) == 16) + assert(foo == 20) -- local one is in context here - ::L1:: do - local *toclose a = setmetatable({}, {__close = function () X = true end}) - assert(X == nil) - if a then goto L2 end -- jumping back out of scope of 'a' + do + global foo; + function foo (x) return end -- Ok after declaration end - ::L3:: assert(X == true) -- checks that 'a' was correctly closed + checkerr([[ + global foo; + function foo (x) return end -- ERROR: foo is read-only + ]], "assign to const variable 'foo'") + + checkerr([[ + global foo ; + function foo (x) -- ERROR: foo is read-only + return + end + ]], "%:2%:") -- correct line in error message + + checkerr([[ + global *; + print(X) -- Ok to use + Y = 1 -- ERROR + ]], "assign to const variable 'Y'") + + checkerr([[ + global *; + Y = X -- Ok to use + global *; + Y = 1 -- ERROR + ]], "assign to const variable 'Y'") + + global * + Y = 10 + assert(_ENV.Y == 10) + global * + local x = Y + global * + Y = x + Y + assert(_ENV.Y == 20) + Y = nil end --------------------------------------------------------------------------------- +do -- Ok to declare hundreds of globals + global table + local code = {} + for i = 1, 1000 do + code[#code + 1] = ";global x" .. i + end + code[#code + 1] = "; return x990" + code = table.concat(code) + _ENV.x990 = 11 + assert(load(code)() == 11) + _ENV.x990 = nil +end + +do -- mixing lots of global/local declarations + global table + local code = {} + for i = 1, 200 do + code[#code + 1] = ";global x" .. i + code[#code + 1] = ";local y" .. i .. "=" .. (2*i) + end + code[#code + 1] = "; return x200 + y200" + code = table.concat(code) + _ENV.x200 = 11 + assert(assert(load(code))() == 2*200 + 11) + _ENV.x200 = nil +end + +do print "testing initialization in global declarations" + global a, b, c = 10, 20, 30 + assert(_ENV.a == 10 and b == 20 and c == 30) + _ENV.a = nil; _ENV.b = nil; _ENV.c = nil; + + global a, b, c = 10 + assert(_ENV.a == 10 and b == nil and c == nil) + _ENV.a = nil; _ENV.b = nil; _ENV.c = nil; + + global table + global a, b, c, d = table.unpack{1, 2, 3, 6, 5} + assert(_ENV.a == 1 and b == 2 and c == 3 and d == 6) + a = nil; b = nil; c = nil; d = nil + + local a, b = 100, 200 + do + global a, b = a, b + end + assert(_ENV.a == 100 and _ENV.b == 200) + _ENV.a = nil; _ENV.b = nil + + + assert(_ENV.a == nil and _ENV.b == nil and _ENV.c == nil and _ENV.d == nil) +end + +do + global table, string + -- global initialization when names don't fit in K + + -- to fill constant table + local code = {} + for i = 1, 300 do code[i] = "'" .. i .. "'" end + code = table.concat(code, ",") + code = string.format([[ + return function (_ENV) + local dummy = {%s} -- fill initial positions in constant table, + -- so that initialization must use registers for global names + global a, b, c = 10, 20, 30 + end]], code) + + local fun = assert(load(code))() + + local env = {} + fun(env) + assert(env.a == 10 and env.b == 20 and env.c == 30) +end + + +do -- testing global redefinitions + -- cannot use 'checkerr' as errors are not compile time + global pcall + local f = assert(load("global print = 10")) + local st, msg = pcall(f) + assert(string.find(msg, "global 'print' already defined")) + + local f = assert(load("local _ENV = {AA = false}; global AA = 10")) + local st, msg = pcall(f) + assert(string.find(msg, "global 'AA' already defined")) + +end + print'OK' + diff --git a/testes/heavy.lua b/testes/heavy.lua index 4731c7472f..e7219a91ae 100644 --- a/testes/heavy.lua +++ b/testes/heavy.lua @@ -1,5 +1,7 @@ --- $Id: heavy.lua,v 1.7 2017/12/29 15:42:15 roberto Exp $ --- See Copyright Notice in file all.lua +-- $Id: testes/heavy.lua,v $ +-- See Copyright Notice in file lua.h + +global * local function teststring () print("creating a string too long") @@ -47,9 +49,9 @@ local function loadrep (x, what) end -function controlstruct () +local function controlstruct () print("control structure too long") - local lim = ((1 << 24) - 2) // 3 + local lim = ((1 << 24) - 2) // 4 local s = string.rep("a = a + 1\n", lim) s = "while true do " .. s .. "end" assert(load(s)) @@ -63,7 +65,7 @@ function controlstruct () end -function manylines () +local function manylines () print("loading chunk with too many lines") local st, msg = loadrep("\n", "lines") assert(not st and string.find(msg, "too many lines")) @@ -71,7 +73,7 @@ function manylines () end -function hugeid () +local function hugeid () print("loading chunk with huge identifier") local st, msg = loadrep("a", "chars") assert(not st and @@ -80,7 +82,7 @@ function hugeid () print('+') end -function toomanyinst () +local function toomanyinst () print("loading chunk with too many instructions") local st, msg = loadrep("a = 10; ", "instructions") print('+') @@ -107,7 +109,7 @@ local function loadrepfunc (prefix, f) end -function toomanyconst () +local function toomanyconst () print("loading function with too many constants") loadrepfunc("function foo () return {0,", function (n) @@ -126,7 +128,7 @@ function toomanyconst () end -function toomanystr () +local function toomanystr () local a = {} local st, msg = pcall(function () for i = 1, math.huge do @@ -144,7 +146,7 @@ function toomanystr () end -function toomanyidx () +local function toomanyidx () local a = {} local st, msg = pcall(function () for i = 1, math.huge do diff --git a/testes/libs/lib11.c b/testes/libs/lib11.c index 377d0c484f..6a85f4d621 100644 --- a/testes/libs/lib11.c +++ b/testes/libs/lib11.c @@ -1,7 +1,7 @@ #include "lua.h" /* function from lib1.c */ -int lib1_export (lua_State *L); +LUAMOD_API int lib1_export (lua_State *L); LUAMOD_API int luaopen_lib11 (lua_State *L) { return lib1_export(L); diff --git a/testes/libs/lib22.c b/testes/libs/lib22.c new file mode 100644 index 0000000000..b377cce520 --- /dev/null +++ b/testes/libs/lib22.c @@ -0,0 +1,76 @@ +/* implementation for lib2-v2 */ + +#include + +#include "lua.h" +#include "lauxlib.h" + +static int id (lua_State *L) { + lua_pushboolean(L, 1); + lua_insert(L, 1); + return lua_gettop(L); +} + + +struct STR { + void *ud; + lua_Alloc allocf; +}; + + +static void *t_freestr (void *ud, void *ptr, size_t osize, size_t nsize) { + struct STR *blk = (struct STR*)ptr - 1; + blk->allocf(blk->ud, blk, sizeof(struct STR) + osize, 0); + return NULL; +} + + +static int newstr (lua_State *L) { + size_t len; + const char *str = luaL_checklstring(L, 1, &len); + void *ud; + lua_Alloc allocf = lua_getallocf(L, &ud); + struct STR *blk = (struct STR*)allocf(ud, NULL, 0, + len + 1 + sizeof(struct STR)); + if (blk == NULL) { /* allocation error? */ + lua_pushliteral(L, "not enough memory"); + lua_error(L); /* raise a memory error */ + } + blk->ud = ud; blk->allocf = allocf; + memcpy(blk + 1, str, len + 1); + lua_pushexternalstring(L, (char *)(blk + 1), len, t_freestr, L); + return 1; +} + + +/* +** Create an external string and keep it in the registry, so that it +** will test that the library code is still available (to deallocate +** this string) when closing the state. +*/ +static void initstr (lua_State *L) { + lua_pushcfunction(L, newstr); + lua_pushstring(L, + "012345678901234567890123456789012345678901234567890123456789"); + lua_call(L, 1, 1); /* call newstr("0123...") */ + luaL_ref(L, LUA_REGISTRYINDEX); /* keep string in the registry */ +} + + +static const struct luaL_Reg funcs[] = { + {"id", id}, + {"newstr", newstr}, + {NULL, NULL} +}; + + +LUAMOD_API int luaopen_lib2 (lua_State *L) { + lua_settop(L, 2); + lua_setglobal(L, "y"); /* y gets 2nd parameter */ + lua_setglobal(L, "x"); /* x gets 1st parameter */ + initstr(L); + luaL_newlib(L, funcs); + return 1; +} + + diff --git a/testes/libs/makefile b/testes/libs/makefile index acff4848c2..cf4c688152 100644 --- a/testes/libs/makefile +++ b/testes/libs/makefile @@ -5,23 +5,23 @@ LUA_DIR = ../../ CC = gcc # compilation should generate Dynamic-Link Libraries -CFLAGS = -Wall -std=gnu99 -O2 -I$(LUA_DIR) -fPIC -shared +CFLAGS = -Wall -O2 -I$(LUA_DIR) -fPIC -shared # libraries used by the tests all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so touch all -lib1.so: lib1.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h +lib1.so: lib1.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h $(CC) $(CFLAGS) -o lib1.so lib1.c -lib11.so: lib11.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h +lib11.so: lib11.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h $(CC) $(CFLAGS) -o lib11.so lib11.c -lib2.so: lib2.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h +lib2.so: lib2.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h $(CC) $(CFLAGS) -o lib2.so lib2.c -lib21.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h +lib21.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h $(CC) $(CFLAGS) -o lib21.so lib21.c -lib2-v2.so: lib2.so - cp lib2.so ./lib2-v2.so +lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h + $(CC) $(CFLAGS) -o lib2-v2.so lib22.c diff --git a/testes/literals.lua b/testes/literals.lua index fc45d4adf4..336ef585c5 100644 --- a/testes/literals.lua +++ b/testes/literals.lua @@ -1,8 +1,10 @@ -- $Id: testes/literals.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print('testing scanner') +global * + local debug = require "debug" @@ -10,6 +12,7 @@ local function dostring (x) return assert(load(x), "")() end dostring("x \v\f = \t\r 'a\0a' \v\f\f") assert(x == 'a\0a' and string.len(x) == 3) +_G.x = nil -- escape sequences assert('\n\"\'\\' == [[ @@ -129,16 +132,16 @@ end -- long variable names -var1 = string.rep('a', 15000) .. '1' -var2 = string.rep('a', 15000) .. '2' -prog = string.format([[ +local var1 = string.rep('a', 15000) .. '1' +local var2 = string.rep('a', 15000) .. '2' +local prog = string.format([[ %s = 5 %s = %s + 1 return function () return %s - %s end ]], var1, var2, var1, var1, var2) local f = dostring(prog) assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1) -var1, var2, f = nil +_G[var1], _G[var2] = nil print('+') -- escapes -- @@ -150,13 +153,13 @@ assert([[ $debug]] == "\n $debug") assert([[ [ ]] ~= [[ ] ]]) -- long strings -- -b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" +local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" assert(string.len(b) == 960) prog = [=[ print('+') -a1 = [["this is a 'string' with several 'quotes'"]] -a2 = "'quotes'" +local a1 = [["this is a 'string' with several 'quotes'"]] +local a2 = "'quotes'" assert(string.find(a1, a2) == 34) print('+') @@ -164,12 +167,13 @@ print('+') a1 = [==[temp = [[an arbitrary value]]; ]==] assert(load(a1))() assert(temp == 'an arbitrary value') +_G.temp = nil -- long strings -- -b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" +local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" assert(string.len(b) == 960) print('+') -a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 +local a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 00123456789012345678901234567890123456789123456789012345678901234567890123456789 00123456789012345678901234567890123456789123456789012345678901234567890123456789 00123456789012345678901234567890123456789123456789012345678901234567890123456789 @@ -199,19 +203,41 @@ x = 1 ]=] print('+') -x = nil +_G.x = nil dostring(prog) assert(x) +_G.x = nil + + + +do -- reuse of long strings + + -- get the address of a string + local function getadd (s) return string.format("%p", s) end -prog = nil -a = nil -b = nil + local s1 = "01234567890123456789012345678901234567890123456789" + local s2 = "01234567890123456789012345678901234567890123456789" + local s3 = "01234567890123456789012345678901234567890123456789" + local function foo() return s1 end + local function foo1() return s3 end + local function foo2() + return "01234567890123456789012345678901234567890123456789" + end + local a1 = getadd(s1) + assert(a1 == getadd(s2)) + assert(a1 == getadd(foo())) + assert(a1 == getadd(foo1())) + assert(a1 == getadd(foo2())) + + local sd = "0123456789" .. "0123456789012345678901234567890123456789" + assert(sd == s1 and getadd(sd) ~= a1) +end -- testing line ends prog = [[ -a = 1 -- a comment -b = 2 +local a = 1 -- a comment +local b = 2 x = [=[ @@ -228,10 +254,11 @@ for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do assert(dostring(prog) == nn) assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n") end +_G.x, _G.y = nil -- testing comments and strings with long brackets -a = [==[]=]==] +local a = [==[]=]==] assert(a == "]=") a = [==[[===[[=[]]=][====[]]===]===]==] @@ -281,7 +308,7 @@ if os.setlocale("pt_BR") or os.setlocale("ptb") then assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1) - assert(tonumber"inf" == nil and tonumber"NAN" == nil) + assert(not tonumber"inf" and not tonumber"NAN") assert(assert(load(string.format("return %q", 4.51)))() == 4.51) @@ -306,4 +333,13 @@ assert(not load"a = 'non-ending string\n'") assert(not load"a = '\\345'") assert(not load"a = [=x]") +local function malformednum (n, exp) + local s, msg = load("return " .. n) + assert(not s and string.find(msg, exp)) +end + +malformednum("0xe-", "near ") +malformednum("0xep-p", "malformed number") +malformednum("1print()", "malformed number") + print('OK') diff --git a/testes/locals.lua b/testes/locals.lua index de47ae3182..6cd1054764 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -1,10 +1,14 @@ -- $Id: testes/locals.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h + +global * print('testing local variables and environments') local debug = require"debug" +local tracegc = require"tracegc" + -- bug in 5.1: @@ -35,11 +39,13 @@ end f = nil local f -x = 1 +local x = 1 -a = nil -load('local a = {}')() -assert(a == nil) +do + global a; a = nil + load('local a = {}')() + assert(a == nil) +end function f (a) local _1, _2, _3, _4, _5 @@ -82,7 +88,7 @@ assert(c.a == nil) f() assert(c.a == 3) --- old test for limits for special instructions (now just a generic test) +-- old test for limits for special instructions do local i = 2 local p = 4 -- p == 2^i @@ -114,7 +120,7 @@ if rawget(_G, "T") then local t = T.querytab(a) for k,_ in pairs(a) do a[k] = undef end - collectgarbage() -- restore GC and collect dead fiels in `a' + collectgarbage() -- restore GC and collect dead fields in 'a' for i=0,t-1 do local k = querytab(a, i) assert(k == nil or type(k) == 'number' or k == 'alo') @@ -150,9 +156,9 @@ local dummy local _ENV = (function (...) return ... end)(_G, dummy) -- { do local _ENV = {assert=assert}; assert(true) end -mt = {_G = _G} +local mt = {_G = _G} local foo,x -A = false -- "declare" A +global A; A = false -- "declare" A do local _ENV = mt function foo (x) A = x @@ -172,25 +178,84 @@ do local _ENV = {assert=assert, A=10}; end assert(x==20) +A = nil + + +do print("testing local constants") + global assert, load, string, X + X = 1 -- not a constant + local a, b, c = 10, 20, 30 + b = a + c + b -- 'b' is not constant + assert(a == 10 and b == 60 and c == 30) + + local function checkro (name, code) + local st, msg = load(code) + local gab = string.format("attempt to assign to const variable '%s'", name) + assert(not st and string.find(msg, gab)) + end + + checkro("y", "local x, y , z = 10, 20, 30; x = 11; y = 12") + checkro("x", "local x , y, z = 10, 20, 30; x = 11") + checkro("z", "local x , y, z = 10, 20, 30; y = 10; z = 11") + checkro("foo", "local foo = 10; function foo() end") + checkro("foo", "local foo = {}; function foo() end") + checkro("foo", "global foo ; function foo() end") + checkro("XX", "global XX ; XX = 10") + checkro("XX", "local _ENV; global XX ; XX = 10") + + checkro("z", [[ + local a, z , b = 10; + function foo() a = 20; z = 32; end + ]]) + + checkro("var1", [[ + local a, var1 = 10; + function foo() a = 20; z = function () var1 = 12; end end + ]]) + + checkro("var1", [[ + global a, var1 , z; + local function foo() a = 20; z = function () var1 = 12; end end + ]]) +end + + print"testing to-be-closed variables" + +do + local st, msg = load("local a, b") + assert(not st and string.find(msg, "multiple")) + + local st, msg = load("local a, b") + assert(not st and string.find(msg, "multiple")) +end + local function stack(n) n = ((n == 0) or stack(n - 1)) end -local function func2close (f) - return setmetatable({}, {__close = f}) +local function func2close (f, x, y) + local obj = setmetatable({}, {__close = f}) + if x then + return x, obj, y + else + return obj + end end do local a = {} do - local *toclose x = setmetatable({"x"}, {__close = function (self) + local b = false -- not to be closed + local x = setmetatable({"x"}, {__close = function (self) a[#a + 1] = self[1] end}) - local *toclose y = func2close(function (self, err) - assert(err == nil); a[#a + 1] = "y" - end) + local w, y , z = func2close(function (self, err) + assert(err == nil); a[#a + 1] = "y" + end, 10, 20) + local c = nil -- not to be closed a[#a + 1] = "in" + assert(w == 10 and z == 20) end a[#a + 1] = "out" assert(a[1] == "in" and a[2] == "y" and a[3] == "x" and a[4] == "out") @@ -199,11 +264,16 @@ end do local X = false - local closescope = func2close(function () stack(10); X = true end) + local x, closescope = func2close(function (_, msg) + stack(10); + assert(msg == nil) + X = true + end, 100) + assert(x == 100); x = 101; -- 'x' is not read-only -- closing functions do not corrupt returning values local function foo (x) - local *toclose _ = closescope + local _ = closescope return x, X, 23 end @@ -212,7 +282,13 @@ do X = false foo = function (x) - local *toclose _ = closescope + local _ = func2close(function (_, msg) + -- without errors, enclosing function should be still active when + -- __close is called + assert(debug.getinfo(2).name == "foo") + assert(msg == nil) + end) + local _ = closescope local y = 15 return y end @@ -221,7 +297,7 @@ do X = false foo = function () - local *toclose x = closescope + local x = closescope return x end @@ -230,17 +306,79 @@ do end +do -- testing presence of second argument + local function foo (howtoclose, obj, n) + local ca -- copy of 'a' visible inside its close metamethod + do + local a = func2close(function (...t) + assert(select("#", ...) == n) + assert(t.n == n and t[1] == ca and (t.n < 2 or t[2] == obj)) + ca = 15 -- final value to be returned if howtoclose=="scope" + end) + ca = a + if howtoclose == "ret" then return obj -- 'a' closed by return + elseif howtoclose == "err" then error(obj) -- 'a' closed by error + end + end -- 'a' closed by end of scope + return ca -- ca now should be 15 + end + -- with no errors, closing methods receive no extra argument + assert(foo("scope", nil, 1) == 15) -- close by end of scope + assert(foo("ret", 32, 1) == 32) -- close by return + -- with errors, they do + local st, msg = pcall(foo, "err", 23, 2) -- close by error + assert(not st and msg == 23) +end + + +-- testing to-be-closed x compile-time constants +-- (there were some bugs here in Lua 5.4-rc3, due to a confusion +-- between compile levels and stack levels of variables) +do + local flag = false + local x = setmetatable({}, + {__close = function() assert(flag == false); flag = true end}) + local y = nil + local z = nil + do + local a = x + end + assert(flag) -- 'x' must be closed here +end + +do + -- similar problem, but with implicit close in for loops + local flag = false + local x = setmetatable({}, + {__close = function () assert(flag == false); flag = true end}) + -- return an empty iterator, nil, nil, and 'x' to be closed + local function a () + return (function () return nil end), nil, nil, x + end + local v = 1 + local w = 1 + local x = 1 + local y = 1 + local z = 1 + for k in a() do + a = k + end -- ending the loop must close 'x' + assert(flag) -- 'x' must be closed here +end + + + do -- calls cannot be tail in the scope of to-be-closed variables local X, Y local function foo () - local *toclose _ = func2close(function () Y = 10 end) + local _ = func2close(function () Y = 10 end) assert(X == true and Y == nil) -- 'X' not closed yet return 1,2,3 end local function bar () - local *toclose _ = func2close(function () X = false end) + local _ = func2close(function () X = false end) X = true do return foo() -- not a tail call! @@ -252,63 +390,310 @@ do end -do -- errors in __close - local log = {} - local function foo (err) - local *toclose x = - func2close(function (self, msg) log[#log + 1] = msg; error(1) end) - local *toclose x1 = - func2close(function (self, msg) log[#log + 1] = msg; end) - local *toclose gc = func2close(function () collectgarbage() end) - local *toclose y = - func2close(function (self, msg) log[#log + 1] = msg; error(2) end) - local *toclose z = - func2close(function (self, msg) log[#log + 1] = msg or 10; error(3) end) - if err then error(4) end +do + -- bug in 5.4.3: previous condition (calls cannot be tail in the + -- scope of to-be-closed variables) must be valid for tbc variables + -- created by 'for' loops. + + local closed = false + + local function foo () + return function () return true end, 0, 0, + func2close(function () closed = true end) end - local stat, msg = pcall(foo, false) - assert(msg == 1) - assert(log[1] == 10 and log[2] == 3 and log[3] == 2 and log[4] == 2 - and #log == 4) - log = {} - local stat, msg = pcall(foo, true) - assert(msg == 1) - assert(log[1] == 4 and log[2] == 3 and log[3] == 2 and log[4] == 2 - and #log == 4) + local function tail() return closed end + + local function foo1 () + for k in foo() do return tail() end + end + + assert(foo1() == false) + assert(closed == true) end do + -- bug in 5.4.4: 'break' may generate wrong 'close' instruction when + -- leaving a loop block. + + local closed = false + + local o1 = setmetatable({}, {__close=function() closed = true end}) + + local function test() + for k, v in next, {}, nil, o1 do + local function f() return k end -- create an upvalue + break + end + assert(closed) + end + + test() +end + + +do print("testing errors in __close") + + -- original error is in __close + local function foo () + + local x = + func2close(function (self, msg) + assert(string.find(msg, "@y")) + error("@x") + end) + + local x1 = + func2close(function (self, msg) + assert(string.find(msg, "@y")) + end) + + local gc = func2close(function () collectgarbage() end) + + local y = + func2close(function (self, msg) + assert(string.find(msg, "@z")) -- error in 'z' + error("@y") + end) + + local z = + func2close(function (self, msg) + assert(msg == nil) + error("@z") + end) + + return 200 + end + + local stat, msg = pcall(foo, false) + assert(string.find(msg, "@x")) + + + -- original error not in __close + local function foo () + + local x = + func2close(function (self, msg) + -- after error, 'foo' was discarded, so caller now + -- must be 'pcall' + assert(debug.getinfo(2).name == "pcall") + assert(string.find(msg, "@x1")) + end) + + local x1 = + func2close(function (self, msg) + assert(debug.getinfo(2).name == "pcall") + assert(string.find(msg, "@y")) + error("@x1") + end) + + local gc = func2close(function () collectgarbage() end) + + local y = + func2close(function (self, msg) + assert(debug.getinfo(2).name == "pcall") + assert(string.find(msg, "@z")) + error("@y") + end) + + local first = true + local z = + func2close(function (self, msg) + assert(debug.getinfo(2).name == "pcall") + -- 'z' close is called once + assert(first and msg == 4) + first = false + error("@z") + end) + + error(4) -- original error + end + + local stat, msg = pcall(foo, true) + assert(string.find(msg, "@x1")) + + -- error leaving a block + local function foo (...) + do + local x1 = + func2close(function (self, msg) + assert(string.find(msg, "@X")) + error("@Y") + end) + + local x123 = + func2close(function (_, msg) + assert(msg == nil) + error("@X") + end) + end + os.exit(false) -- should not run + end + + local st, msg = xpcall(foo, debug.traceback) + assert(string.match(msg, "^[^ ]* @Y")) - -- errors due to non-closable values + -- error in toclose in vararg function + local function foo (...) + local x123 = func2close(function () error("@x123") end) + end + + local st, msg = xpcall(foo, debug.traceback) + assert(string.match(msg, "^[^ ]* @x123")) + assert(string.find(msg, "in metamethod 'close'")) +end + + +do -- errors due to non-closable values local function foo () - local *toclose x = 34 + local x = {} + os.exit(false) -- should not run end local stat, msg = pcall(foo) - assert(not stat and string.find(msg, "variable 'x'")) + assert(not stat and + string.find(msg, "variable 'x' got a non%-closable value")) + local function foo () + local xyz = setmetatable({}, {__close = print}) + getmetatable(xyz).__close = nil -- remove metamethod + end + local stat, msg = pcall(foo) + assert(not stat and string.find(msg, "metamethod 'close'")) - -- with other errors, non-closable values are ignored local function foo () - local *toclose x = 34 - local *toclose y = func2close(function () error(32) end) + local a1 = func2close(function (_, msg) + assert(string.find(msg, "number value")) + error(12) + end) + local a2 = setmetatable({}, {__close = print}) + local a3 = func2close(function (_, msg) + assert(msg == nil) + error(123) + end) + getmetatable(a2).__close = 4 -- invalidate metamethod end local stat, msg = pcall(foo) - assert(not stat and msg == 32) + assert(not stat and msg == 12) +end + +do -- tbc inside close methods + local track = {} + local function foo () + local x = func2close(function () + local xx = func2close(function (_, msg) + assert(msg == nil) + track[#track + 1] = "xx" + end) + track[#track + 1] = "x" + end) + track[#track + 1] = "foo" + return 20, 30, 40 + end + local a, b, c, d = foo() + assert(a == 20 and b == 30 and c == 40 and d == nil) + assert(track[1] == "foo" and track[2] == "x" and track[3] == "xx") + + -- again, with errors + local track = {} + local function foo () + local x0 = func2close(function (_, msg) + assert(msg == 202) + track[#track + 1] = "x0" + end) + local x = func2close(function () + local xx = func2close(function (_, msg) + assert(msg == 101) + track[#track + 1] = "xx" + error(202) + end) + track[#track + 1] = "x" + error(101) + end) + track[#track + 1] = "foo" + return 20, 30, 40 + end + local st, msg = pcall(foo) + assert(not st and msg == 202) + assert(track[1] == "foo" and track[2] == "x" and track[3] == "xx" and + track[4] == "x0") +end + + +local function checktable (t1, t2) + assert(#t1 == #t2) + for i = 1, #t1 do + assert(t1[i] == t2[i]) + end +end + + +do -- test for tbc variable high in the stack + + -- function to force a stack overflow + local function overflow (n) + overflow(n + 1) + end + + -- error handler will create tbc variable handling a stack overflow, + -- high in the stack + local function errorh (m) + assert(string.find(m, "stack overflow")) + local x = func2close(function (o) o[1] = 10 end) + return x + end + + local flag + local st, obj + -- run test in a coroutine so as not to swell the main stack + local co = coroutine.wrap(function () + -- tbc variable down the stack + local y = func2close(function (obj, msg) + assert(msg == nil) + obj[1] = 100 + flag = obj + end) + tracegc.stop() + st, obj = xpcall(overflow, errorh, 0) + tracegc.start() + end) + co() + assert(not st and obj[1] == 10 and flag[1] == 100) end if rawget(_G, "T") then + do + -- bug in 5.4.3 + -- 'lua_settop' may use a pointer to stack invalidated by 'luaF_close' + + -- reduce stack size + collectgarbage(); collectgarbage(); collectgarbage() + + -- force a stack reallocation + local function loop (n) + if n < 400 then loop(n + 1) end + end + + -- close metamethod will reallocate the stack + local o = setmetatable({}, {__close = function () loop(0) end}) + + local script = [[toclose 2; settop 1; return 1]] + + assert(T.testC(script, o) == script) + + end + + -- memory error inside closing function local function foo () - local *toclose y = func2close(function () T.alloccount() end) - local *toclose x = setmetatable({}, {__close = function () + local y = func2close(function () T.alloccount() end) + local x = setmetatable({}, {__close = function () T.alloccount(0); local x = {} -- force a memory error end}) - error("a") -- common error inside the function's body + error(1000) -- common error inside the function's body end stack(5) -- ensure a minimal number of CI structures @@ -318,49 +703,44 @@ if rawget(_G, "T") then local _, msg = pcall(foo) assert(msg == "not enough memory") + local closemsg local close = func2close(function (self, msg) T.alloccount() - assert(msg == "not enough memory") + closemsg = msg end) -- set a memory limit and return a closing object to remove the limit local function enter (count) stack(10) -- reserve some stack space T.alloccount(count) + closemsg = nil return close end local function test () - local *toclose x = enter(0) -- set a memory limit - -- creation of previous upvalue will raise a memory error - assert(false) -- should not run + local x = enter(0) -- set a memory limit + local y = {} -- raise a memory error end local _, msg = pcall(test) - assert(msg == "not enough memory") + assert(msg == "not enough memory" and closemsg == "not enough memory") - -- now use metamethod for closing - close = setmetatable({}, {__close = function () - T.alloccount() - end}) -- repeat test with extra closing upvalues local function test () - local *toclose xxx = func2close(function (self, msg) + local xxx = func2close(function (self, msg) assert(msg == "not enough memory"); error(1000) -- raise another error end) - local *toclose xx = func2close(function (self, msg) + local xx = func2close(function (self, msg) assert(msg == "not enough memory"); end) - local *toclose x = enter(0) -- set a memory limit - -- creation of previous upvalue will raise a memory error - os.exit(false) -- should not run + local x = enter(0) -- set a memory limit + local y = {} -- raise a memory error end local _, msg = pcall(test) - assert(msg == 1000) - + assert(msg == 1000 and closemsg == "not enough memory") do -- testing 'toclose' in C string buffer collectgarbage() @@ -379,12 +759,12 @@ if rawget(_G, "T") then local s = string.rep("a", lim) - -- concat this table needs two buffer resizes (one for each 's') + -- concat this table needs two buffer resizes (one for each 's') local a = {s, s} - collectgarbage() + collectgarbage(); collectgarbage() - m = T.totalmem() + local m = T.totalmem() collectgarbage("stop") -- error in the first buffer allocation @@ -399,14 +779,8 @@ if rawget(_G, "T") then -- first buffer was released by 'toclose' assert(T.totalmem() - m <= extra) - -- error in creation of final string - T.totalmem(m + 2 * lim + extra) - assert(not pcall(table.concat, a)) - -- second buffer was released by 'toclose' - assert(T.totalmem() - m <= extra) - - -- userdata, upvalue, buffer, buffer, final string - T.totalmem(m + 4*lim + extra) + -- userdata, buffer, final string + T.totalmem(m + 2*lim + extra) assert(#table.concat(a) == 2*lim) T.totalmem(0) -- remove memory limit @@ -414,41 +788,396 @@ if rawget(_G, "T") then print'+' end + + + do + -- '__close' vs. return hooks in C functions + local trace = {} + + local function hook (event) + trace[#trace + 1] = event .. " " .. (debug.getinfo(2).name or "?") + end + + -- create tbc variables to be used by C function + local x = func2close(function (_,msg) + trace[#trace + 1] = "x" + end) + + local y = func2close(function (_,msg) + trace[#trace + 1] = "y" + end) + + debug.sethook(hook, "r") + local t = {T.testC([[ + toclose 2 # x + pushnum 10 + pushint 20 + toclose 3 # y + return 2 + ]], x, y)} + debug.sethook() + + -- hooks ran before return hook from 'testC' + checktable(trace, + {"return sethook", "y", "return ?", "x", "return ?", "return testC"}) + -- results are correct + checktable(t, {10, 20}) + end +end + + +do -- '__close' vs. return hooks in Lua functions + local trace = {} + + local function hook (event) + trace[#trace + 1] = event .. " " .. debug.getinfo(2).name + end + + local function foo (...) + local x = func2close(function (_,msg) + trace[#trace + 1] = "x" + end) + + local y = func2close(function (_,msg) + debug.sethook(hook, "r") + end) + + return ... + end + + local t = {foo(10,20,30)} + debug.sethook() + checktable(t, {10, 20, 30}) + checktable(trace, + {"return sethook", "return close", "x", "return close", "return foo"}) +end + + +print "to-be-closed variables in coroutines" + +do + -- yielding inside closing metamethods + + local trace = {} + local co = coroutine.wrap(function () + + trace[#trace + 1] = "nowX" + + -- will be closed after 'y' + local x = func2close(function (_, msg) + assert(msg == nil) + trace[#trace + 1] = "x1" + coroutine.yield("x") + trace[#trace + 1] = "x2" + end) + + return pcall(function () + do -- 'z' will be closed first + local z = func2close(function (_, msg) + assert(msg == nil) + trace[#trace + 1] = "z1" + coroutine.yield("z") + trace[#trace + 1] = "z2" + end) + end + + trace[#trace + 1] = "nowY" + + -- will be closed after 'z' + local y = func2close(function(_, msg) + assert(msg == nil) + trace[#trace + 1] = "y1" + coroutine.yield("y") + trace[#trace + 1] = "y2" + end) + + return 10, 20, 30 + end) + end) + + assert(co() == "z") + assert(co() == "y") + assert(co() == "x") + checktable({co()}, {true, 10, 20, 30}) + checktable(trace, {"nowX", "z1", "z2", "nowY", "y1", "y2", "x1", "x2"}) + +end + + +do + -- yielding inside closing metamethods while returning + -- (bug in 5.4.3) + + local extrares -- result from extra yield (if any) + + local function check (body, extra, ...t) + local co = coroutine.wrap(body) + if extra then + extrares = co() -- runs until first (extra) yield + end + local res = table.pack(co()) -- runs until "regular" yield + -- regular yield will yield all values passed to the close function; + -- without errors, that is only the object being closed. + assert(res.n == 1 and type(res[1]) == "table") + local res2 = table.pack(co()) -- runs until end of function + assert(res2.n == t.n) + for i = 1, #t do + if t[i] == "x" then + assert(res2[i] == res[1]) -- value that was closed + else + assert(res2[i] == t[i]) + end + end + end + + local function foo () + local x = func2close(coroutine.yield) -- "regular" yield + local extra = func2close(function (self) + assert(self == extrares) + coroutine.yield(100) -- first (extra) yield + end) + extrares = extra + return table.unpack{10, x, 30} + end + check(foo, true, 10, "x", 30) + assert(extrares == 100) + + local function foo () + local x = func2close(coroutine.yield) -- "regular" yield + return + end + check(foo, false) + + local function foo () + local x = func2close(coroutine.yield) -- "regular" yield + local y, z = 20, 30 + return x + end + check(foo, false, "x") + + local function foo () + local x = func2close(coroutine.yield) -- "regular" yield + local extra = func2close(coroutine.yield) -- extra yield + return table.unpack({}, 1, 100) -- 100 nils + end + check(foo, true, table.unpack({}, 1, 100)) + +end + +do + -- yielding inside closing metamethods after an error + + local co = coroutine.wrap(function () + + local function foo (err) + + local z = func2close(function(_, msg) + assert(msg == nil or msg == err + 20) + coroutine.yield("z") + return 100, 200 + end) + + local y = func2close(function(_, msg) + -- still gets the original error (if any) + assert(msg == err or (msg == nil and err == 1)) + coroutine.yield("y") + if err then error(err + 20) end -- creates or changes the error + end) + + local x = func2close(function(_, msg) + assert(msg == err or (msg == nil and err == 1)) + coroutine.yield("x") + return 100, 200 + end) + + if err == 10 then error(err) else return 10, 20 end + end + + coroutine.yield(pcall(foo, nil)) -- no error + coroutine.yield(pcall(foo, 1)) -- error in __close + return pcall(foo, 10) -- 'foo' will raise an error + end) + + local a, b = co() -- first foo: no error + assert(a == "x" and b == nil) -- yields inside 'x'; Ok + a, b = co() + assert(a == "y" and b == nil) -- yields inside 'y'; Ok + a, b = co() + assert(a == "z" and b == nil) -- yields inside 'z'; Ok + local a, b, c = co() + assert(a and b == 10 and c == 20) -- returns from 'pcall(foo, nil)' + + local a, b = co() -- second foo: error in __close + assert(a == "x" and b == nil) -- yields inside 'x'; Ok + a, b = co() + assert(a == "y" and b == nil) -- yields inside 'y'; Ok + a, b = co() + assert(a == "z" and b == nil) -- yields inside 'z'; Ok + local st, msg = co() -- reports the error in 'y' + assert(not st and msg == 21) + + local a, b = co() -- third foo: error in function body + assert(a == "x" and b == nil) -- yields inside 'x'; Ok + a, b = co() + assert(a == "y" and b == nil) -- yields inside 'y'; Ok + a, b = co() + assert(a == "z" and b == nil) -- yields inside 'z'; Ok + local st, msg = co() -- gets final error + assert(not st and msg == 10 + 20) + end --- to-be-closed variables in coroutines do - -- an error in a coroutine closes variables + -- an error in a wrapped coroutine closes variables local x = false local y = false - local co = coroutine.create(function () - local *toclose xv = func2close(function () x = true end) + local co = coroutine.wrap(function () + local xv = func2close(function () x = true end) do - local *toclose yv = func2close(function () y = true end) + local yv = func2close(function () y = true end) coroutine.yield(100) -- yield doesn't close variable end coroutine.yield(200) -- yield doesn't close variable error(23) -- error does end) - local a, b = coroutine.resume(co) - assert(a and b == 100 and not x and not y) - a, b = coroutine.resume(co) - assert(a and b == 200 and not x and y) - a, b = coroutine.resume(co) + local b = co() + assert(b == 100 and not x and not y) + b = co() + assert(b == 200 and not x and y) + local a, b = pcall(co) assert(not a and b == 23 and x and y) end + +do + + -- error in a wrapped coroutine raising errors when closing a variable + local x = 0 + local co = coroutine.wrap(function () + local xx = func2close(function (_, msg) + x = x + 1; + assert(string.find(msg, "@XXX")) + error("@YYY") + end) + local xv = func2close(function () x = x + 1; error("@XXX") end) + coroutine.yield(100) + error(200) + end) + assert(co() == 100); assert(x == 0) + local st, msg = pcall(co); assert(x == 2) + assert(not st and string.find(msg, "@YYY")) -- should get error raised + + local x = 0 + local y = 0 + co = coroutine.wrap(function () + local xx = func2close(function (_, err) + y = y + 1; + assert(string.find(err, "XXX")) + error("YYY") + end) + local xv = func2close(function () + x = x + 1; error("XXX") + end) + coroutine.yield(100) + return 200 + end) + assert(co() == 100); assert(x == 0) + local st, msg = pcall(co) + assert(x == 1 and y == 1) + -- should get first error raised + assert(not st and string.find(msg, "%w+%.%w+:%d+: YYY")) + +end + + -- a suspended coroutine should not close its variables when collected local co co = coroutine.wrap(function() - local *toclose x = function () os.exit(false) end -- should not run + -- should not run + local x = func2close(function () os.exit(false) end) co = nil coroutine.yield() end) co() -- start coroutine assert(co == nil) -- eventually it will be collected +collectgarbage() + + +if rawget(_G, "T") then + print("to-be-closed variables x coroutines in C") + do + local token = 0 + local count = 0 + local f = T.makeCfunc[[ + toclose 1 + toclose 2 + return . + ]] + + local obj = func2close(function (_, msg) + count = count + 1 + token = coroutine.yield(count, token) + end) + + local co = coroutine.wrap(f) + local ct, res = co(obj, obj, 10, 20, 30, 3) -- will return 10, 20, 30 + -- initial token value, after closing 2nd obj + assert(ct == 1 and res == 0) + -- run until yield when closing 1st obj + ct, res = co(100) + assert(ct == 2 and res == 100) + res = {co(200)} -- run until end + assert(res[1] == 10 and res[2] == 20 and res[3] == 30 and res[4] == nil) + assert(token == 200) + end + + do + local f = T.makeCfunc[[ + toclose 1 + return . + ]] + + local obj = func2close(function () + local temp + local x = func2close(function () + coroutine.yield(temp) + return 1,2,3 -- to be ignored + end) + temp = coroutine.yield("closing obj") + return 1,2,3 -- to be ignored + end) + + local co = coroutine.wrap(f) + local res = co(obj, 10, 30, 1) -- will return only 30 + assert(res == "closing obj") + res = co("closing x") + assert(res == "closing x") + res = {co()} + assert(res[1] == 30 and res[2] == nil) + end + + do + -- still cannot yield inside 'closeslot' + local f = T.makeCfunc[[ + toclose 1 + closeslot 1 + ]] + local obj = func2close(coroutine.yield) + local co = coroutine.create(f) + local st, msg = coroutine.resume(co, obj) + assert(not st and string.find(msg, "attempt to yield across")) + + -- nor outside a coroutine + local f = T.makeCfunc[[ + toclose 1 + ]] + local st, msg = pcall(f, obj) + assert(not st and string.find(msg, "attempt to yield from outside")) + end +end + -- to-be-closed variables in generic for loops @@ -457,7 +1186,7 @@ do local function open (x) numopen = numopen + 1 return - function () -- iteraction function + function () -- iteration function x = x - 1 if x > 0 then return x end end, @@ -479,27 +1208,15 @@ do end assert(s == 35 and numopen == 0) - -- repeat test with '__open' metamethod instead of a function - local function open (x) - numopen = numopen + 1 - local state = setmetatable({x}, - {__close = function () numopen = numopen - 1 end}) - return - function (t) -- iteraction function - t[1] = t[1] - 1 - if t[1] > 0 then return t[1] end - end, - state, - nil, - state -- to-be-closed - end - local s = 0 for i in open(10) do - if (i < 5) then break end - s = s + i + for j in open(10) do + if i + j < 5 then goto endloop end + s = s + i + end end - assert(s == 35 and numopen == 0) + ::endloop:: + assert(s == 375 and numopen == 0) end print('OK') diff --git a/testes/main.lua b/testes/main.lua index b9dcab1c12..dc48dc485f 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -1,6 +1,6 @@ # testing special comment on first line -- $Id: testes/main.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h -- most (all?) tests here assume a reasonable "Unix-like" shell if _port then return end @@ -27,22 +27,26 @@ do end print("progname: "..progname) -local prepfile = function (s, p) - p = p or prog - io.output(p) - io.write(s) - assert(io.close()) + +local prepfile = function (s, mod, p) + mod = mod and "wb" or "w" -- mod true means binary files + p = p or prog -- file to write the program + local f = io.open(p, mod) + f:write(s) + assert(f:close()) end local function getoutput () - io.input(out) - local t = io.read("a") - io.input():close() + local f = io.open(out) + local t = f:read("a") + f:close() assert(os.remove(out)) return t end local function checkprogout (s) + -- expected result must end with new line + assert(string.sub(s, -1) == "\n") local t = getoutput() for line in string.gmatch(s, ".-\n") do assert(string.find(t, line, 1, true)) @@ -63,10 +67,11 @@ local function RUN (p, ...) assert(os.execute(s)) end + local function NoRun (msg, p, ...) p = string.gsub(p, "lua", '"'..progname..'"', 1) local s = string.format(p, ...) - s = string.format("%s 2> %s", s, out) -- will send error to 'out' + s = string.format("%s >%s 2>&1", s, out) -- send output and error to 'out' assert(not os.execute(s)) assert(string.find(getoutput(), msg, 1, true)) -- check error message end @@ -85,13 +90,40 @@ prepfile[[ 1, a ) ]] -RUN('lua - < %s > %s', prog, out) +RUN('lua - -- < %s > %s', prog, out) checkout("1\tnil\n") RUN('echo "print(10)\nprint(2)\n" | lua > %s', out) checkout("10\n2\n") +-- testing BOM +prepfile("\xEF\xBB\xBF") +RUN('lua %s > %s', prog, out) +checkout("") + +prepfile("\xEF\xBB\xBFprint(3)") +RUN('lua %s > %s', prog, out) +checkout("3\n") + +prepfile("\xEF\xBB\xBF# comment!!\nprint(3)") +RUN('lua %s > %s', prog, out) +checkout("3\n") + +-- bad BOMs +prepfile("\xEF", true) +NoRun("unexpected symbol", 'lua %s', prog) + +prepfile("\xEF\xBB", true) +NoRun("unexpected symbol", 'lua %s', prog) + +prepfile("\xEFprint(3)", true) +NoRun("unexpected symbol", 'lua %s', prog) + +prepfile("\xEF\xBBprint(3)", true) +NoRun("unexpected symbol", 'lua %s', prog) + + -- test option '-' RUN('echo "print(arg[1])" | lua - -h > %s', out) checkout("-h\n") @@ -101,11 +133,11 @@ checkout("-h\n") prepfile("print(package.path)") -- test LUA_PATH -RUN('env LUA_INIT= LUA_PATH=x lua %s > %s', prog, out) +RUN('env LUA_INIT= LUA_PATH=x lua -- %s > %s', prog, out) checkout("x\n") -- test LUA_PATH_version -RUN('env LUA_INIT= LUA_PATH_5_4=y LUA_PATH=x lua %s > %s', prog, out) +RUN('env LUA_INIT= LUA_PATH_5_5=y LUA_PATH=x lua %s > %s', prog, out) checkout("y\n") -- test LUA_CPATH @@ -114,7 +146,7 @@ RUN('env LUA_INIT= LUA_CPATH=xuxu lua %s > %s', prog, out) checkout("xuxu\n") -- test LUA_CPATH_version -RUN('env LUA_INIT= LUA_CPATH_5_4=yacc LUA_CPATH=x lua %s > %s', prog, out) +RUN('env LUA_INIT= LUA_CPATH_5_5=yacc LUA_CPATH=x lua %s > %s', prog, out) checkout("yacc\n") -- test LUA_INIT (and its access to 'arg' table) @@ -124,7 +156,7 @@ checkout("3.2\n") -- test LUA_INIT_version prepfile("print(X)") -RUN('env LUA_INIT_5_4="X=10" LUA_INIT="X=3" lua %s > %s', prog, out) +RUN('env LUA_INIT_5_5="X=10" LUA_INIT="X=3" lua %s > %s', prog, out) checkout("10\n") -- test LUA_INIT for files @@ -142,12 +174,18 @@ do prepfile("print(package.path, package.cpath)") RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s', prog, out) + local output = getoutput() + defaultpath = string.match(output, "^(.-)\t") + defaultCpath = string.match(output, "\t(.-)$") + + -- running with an empty environment + RUN('env -i lua %s > %s', prog, out) local out = getoutput() - defaultpath = string.match(out, "^(.-)\t") - defaultCpath = string.match(out, "\t(.-)$") + assert(defaultpath == string.match(output, "^(.-)\t")) + assert(defaultCpath == string.match(output, "\t(.-)$")) end --- paths did not changed +-- paths did not change assert(not string.find(defaultpath, "xxx") and string.find(defaultpath, "lua") and not string.find(defaultCpath, "xxx") and @@ -160,23 +198,40 @@ local function convert (p) RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out) local expected = getoutput() expected = string.sub(expected, 1, -2) -- cut final end of line - assert(string.gsub(p, ";;", ";"..defaultpath..";") == expected) + if string.find(p, ";;") then + p = string.gsub(p, ";;", ";"..defaultpath..";") + p = string.gsub(p, "^;", "") -- remove ';' at the beginning + p = string.gsub(p, ";$", "") -- remove ';' at the end + end + assert(p == expected) end convert(";") convert(";;") -convert(";;;") -convert(";;;;") -convert(";;;;;") -convert(";;a;;;bc") +convert("a;;b") +convert(";;b") +convert("a;;") +convert("a;b;;c") -- test -l over multiple libraries prepfile("print(1); a=2; return {x=15}") -prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog) +prepfile(("print(a); print(_G['%s'].x)"):format(prog), false, otherprog) RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out) checkout("1\n2\n15\n2\n15\n") +-- test explicit global names in -l +prepfile("print(str.upper'alo alo', m.max(10, 20))") +RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out) +checkout("0.0\nALO ALO\t20\n") + + +-- test module names with version suffix ("libs/lib2-v2") +RUN("env LUA_CPATH='./libs/?.so' lua -l lib2-v2 -e 'print(lib2.id())' > %s", + out) +checkout("true\n") + + -- test 'arg' table local a = [[ assert(#arg == 3 and arg[1] == 'a' and @@ -192,7 +247,7 @@ RUN('lua "-e " -- %s a b c', prog) -- "-e " runs an empty command -- test 'arg' availability in libraries prepfile"assert(arg)" -prepfile("assert(arg)", otherprog) +prepfile("assert(arg)", false, otherprog) RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog) -- test messing up the 'arg' table @@ -208,6 +263,82 @@ assert(string.find(getoutput(), "error calling 'print'")) RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out) checkout("lua_debug> 1000lua_debug> ") +do -- test warning for locals + RUN('echo " local x" | lua -i > %s 2>&1', out) + assert(string.find(getoutput(), "warning: ")) + + RUN('echo "local1 = 10\nlocal1 + 3" | lua -i > %s 2>&1', out) + local t = getoutput() + assert(not string.find(t, "warning")) + assert(string.find(t, "13")) +end + +print("testing warnings") + +-- no warnings by default +RUN('echo "io.stderr:write(1); warn[[XXX]]" | lua 2> %s', out) +checkout("1") + +prepfile[[ +warn("@allow") -- unknown control, ignored +warn("@off", "XXX", "@off") -- these are not control messages +warn("@off") -- this one is +warn("@on", "YYY", "@on") -- not control, but warn is off +warn("@off") -- keep it off +warn("@on") -- restart warnings +warn("", "@on") -- again, no control, real warning +warn("@on") -- keep it "started" +warn("Z", "Z", "Z") -- common warning +]] +RUN('lua -W %s 2> %s', prog, out) +checkout[[ +Lua warning: @offXXX@off +Lua warning: @on +Lua warning: ZZZ +]] + +prepfile[[ +warn("@allow") +-- create two objects to be finalized when closing state +-- the errors in the finalizers must generate warnings +u1 = setmetatable({}, {__gc = function () error("XYZ") end}) +u2 = setmetatable({}, {__gc = function () error("ZYX") end}) +]] +RUN('lua -W %s 2> %s', prog, out) +checkprogout("ZYX)\nXYZ)\n") + +-- bug since 5.2: finalizer called when closing a state could +-- subvert finalization order +prepfile[[ +-- ensure tables will be collected only at the end of the program +collectgarbage"stop" + +print("creating 1") +-- this finalizer should be called last +setmetatable({}, {__gc = function () print(1) end}) + +print("creating 2") +setmetatable({}, {__gc = function () + print("2") + print("creating 3") + -- this finalizer should not be called, as object will be + -- created after 'lua_close' has been called + setmetatable({}, {__gc = function () print(3) end}) + print(collectgarbage() or false) -- cannot call collector here + os.exit(0, true) +end}) +]] +RUN('lua -W %s > %s', prog, out) +checkout[[ +creating 1 +creating 2 +2 +creating 3 +false +1 +]] + + -- test many arguments prepfile[[print(({...})[30])]] RUN('lua %s %s > %s', prog, string.rep(" a", 30), out) @@ -216,7 +347,7 @@ checkout("a\n") RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out) checkout("1\n3\n") --- test iteractive mode +-- test interactive mode prepfile[[ (6*2-6) -- === a = @@ -226,11 +357,16 @@ a]] RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) checkprogout("6\n10\n10\n\n") -prepfile("a = [[b\nc\nd\ne]]\n=a") -RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) +prepfile("a = [[b\nc\nd\ne]]\na") +RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i -- < %s > %s]], prog, out) checkprogout("b\nc\nd\ne\n\n") -prompt = "alo" +-- input interrupted in continuation line +prepfile("a.\n") +RUN([[lua -i < %s > /dev/null 2> %s]], prog, out) +checkprogout("near \n") + +local prompt = "alo" prepfile[[ -- a = 2 ]] @@ -238,6 +374,31 @@ RUN([[lua "-e_PROMPT='%s'" -i < %s > %s]], prompt, prog, out) local t = getoutput() assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt)) +-- using the prompt default +prepfile[[ -- +a = 2 +]] +RUN([[lua -i < %s > %s]], prog, out) +local t = getoutput() +prompt = "> " -- the default +assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt)) + + +-- non-string prompt +prompt = [[ + local C = 'X'; + _PROMPT=setmetatable({},{__tostring = function () + C = C .. 'X'; return C end}) +]] +prepfile[[ -- +a = 2 +]] +RUN([[lua -e "%s" -i < %s > %s]], prompt, prog, out) +local t = getoutput() +-- skip version line and then check the presence of the three prompts +assert(string.find(t, "^.-\nXX[^\nX]*\n?XXX[^\nX]*\n?XXXX\n?$")) + + -- test for error objects prepfile[[ debug = require "debug" @@ -254,7 +415,7 @@ NoRun("error object is a table value", [[lua %s]], prog) -- chunk broken in many lines -s = [=[ -- +local s = [=[ -- function f ( x ) local a = [[ xuxu @@ -276,12 +437,10 @@ checkprogout("101\n13\t22\n\n") prepfile[[#comment in 1st line without \n at the end]] RUN('lua %s', prog) -prepfile[[#test line number when file starts with comment line -debug = require"debug" -print(debug.getinfo(1).currentline) -]] +-- first-line comment with binary file +prepfile("#comment\n" .. string.dump(load("print(3)")), true) RUN('lua %s > %s', prog, out) -checkprogout('3') +checkout('3\n') -- close Lua with an open file prepfile(string.format([[io.output(%q); io.write('alo')]], out)) @@ -309,15 +468,16 @@ NoRun("", "lua %s", prog) -- no message -- to-be-closed variables in main chunk prepfile[[ - local *toclose x = function (err) - assert(err == 120) - print("Ok") - end - local *toclose e1 = function () error(120) end + local x = setmetatable({}, + {__close = function (self, err) + assert(err == nil) + print("Ok") + end}) + local e1 = setmetatable({}, {__close = function () print(120) end}) os.exit(true, true) ]] RUN('lua %s > %s', prog, out) -checkprogout("Ok") +checkprogout("120\nOk\n") -- remove temporary files @@ -328,18 +488,42 @@ assert(not os.remove(out)) -- invalid options NoRun("unrecognized option '-h'", "lua -h") NoRun("unrecognized option '---'", "lua ---") -NoRun("unrecognized option '-Ex'", "lua -Ex") +NoRun("unrecognized option '-Ex'", "lua -Ex --") NoRun("unrecognized option '-vv'", "lua -vv") NoRun("unrecognized option '-iv'", "lua -iv") NoRun("'-e' needs argument", "lua -e") NoRun("syntax error", "lua -e a") NoRun("'-l' needs argument", "lua -l") +NoRun("-i", "lua -- -i") -- handles -i as a script name -if T then -- auxiliary library? +if T then -- test library? print("testing 'not enough memory' to create a state") NoRun("not enough memory", "env MEMLIMIT=100 lua") + + -- testing 'warn' + warn("@store") + warn("@123", "456", "789") + assert(_WARN == "@123456789"); _WARN = false + + warn("zip", "", " ", "zap") + assert(_WARN == "zip zap"); _WARN = false + warn("ZIP", "", " ", "ZAP") + assert(_WARN == "ZIP ZAP"); _WARN = false + warn("@normal") end + +do + -- 'warn' must get at least one argument + local st, msg = pcall(warn) + assert(string.find(msg, "string expected")) + + -- 'warn' does not leave unfinished warning in case of errors + -- (message would appear in next warning) + st, msg = pcall(warn, "SHOULD NOT APPEAR", {}) + assert(string.find(msg, "string expected")) +end + print('+') print('testing Ctrl C') diff --git a/testes/math.lua b/testes/math.lua index b010ff6c3a..54d19c4075 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -1,12 +1,19 @@ -- $Id: testes/math.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print("testing numbers and math lib") -local minint = math.mininteger -local maxint = math.maxinteger +local math = require "math" +local string = require "string" -local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 +global none + +global print, assert, pcall, type, pairs, load +global tonumber, tostring, select + +local minint, maxint = math.mininteger, math.maxinteger + +local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 assert((1 << intbits) == 0) assert(minint == 1 << (intbits - 1)) @@ -22,6 +29,18 @@ do end end + +-- maximum exponent for a floating-point number +local maxexp = 0 +do + local p = 2.0 + while p < math.huge do + maxexp = maxexp + 1 + p = p + p + end +end + + local function isNaN (x) return (x ~= x) end @@ -34,12 +53,12 @@ do local x = 2.0^floatbits assert(x > x - 1.0 and x == x + 1.0) - print(string.format("%d-bit integers, %d-bit (mantissa) floats", - intbits, floatbits)) + local msg = " %d-bit integers, %d-bit*2^%d floats" + print(string.format(msg, intbits, floatbits, maxexp)) end assert(math.type(0) == "integer" and math.type(0.0) == "float" - and math.type("10") == nil) + and not math.type("10")) local function checkerror (msg, f, ...) @@ -50,7 +69,7 @@ end local msgf2i = "number.* has no integer representation" -- float equality -function eq (a,b,limit) +local function eq (a,b,limit) if not limit then if floatbits >= 50 then limit = 1E-11 else limit = 1E-5 @@ -62,7 +81,7 @@ end -- equality with types -function eqT (a,b) +local function eqT (a,b) return a == b and math.type(a) == math.type(b) end @@ -83,7 +102,7 @@ end do local x = -1 local mz = 0/x -- minus zero - t = {[0] = 10, 20, 30, 40, 50} + local t = {[0] = 10, 20, 30, 40, 50} assert(t[mz] == t[0] and t[-0] == t[0]) end @@ -172,7 +191,7 @@ do for i = -3, 3 do -- variables avoid constant folding for j = -3, 3 do -- domain errors (0^(-n)) are not portable - if not _port or i ~= 0 or j > 0 then + if not _ENV._port or i ~= 0 or j > 0 then assert(eq(i^j, 1 / i^(-j))) end end @@ -270,7 +289,7 @@ else end do - local NaN = 0/0 + local NaN = 0/0 assert(not (NaN < 0)) assert(not (NaN > minint)) assert(not (NaN <= -9)) @@ -381,17 +400,17 @@ assert(tonumber(1/0) == 1/0) -- 'tonumber' with strings assert(tonumber("0") == 0) -assert(tonumber("") == nil) -assert(tonumber(" ") == nil) -assert(tonumber("-") == nil) -assert(tonumber(" -0x ") == nil) -assert(tonumber{} == nil) +assert(not tonumber("")) +assert(not tonumber(" ")) +assert(not tonumber("-")) +assert(not tonumber(" -0x ")) +assert(not tonumber{}) assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and tonumber'.01' == 0.01 and tonumber'-1.' == -1 and tonumber'+1.' == 1) -assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and - tonumber'1e' == nil and tonumber'1.0e+' == nil and - tonumber'.' == nil) +assert(not tonumber'+ 0.01' and not tonumber'+.e1' and + not tonumber'1e' and not tonumber'1.0e+' and + not tonumber'.') assert(tonumber('-012') == -010-2) assert(tonumber('-1.2e2') == - - -120) @@ -418,7 +437,7 @@ for i = 2,36 do assert(tonumber('\t10000000000\t', i) == i10) end -if not _soft then +if not _ENV._soft then -- tests with very long numerals assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1) assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1) @@ -445,45 +464,45 @@ local function f (...) end end -assert(f(tonumber('fFfa', 15)) == nil) -assert(f(tonumber('099', 8)) == nil) -assert(f(tonumber('1\0', 2)) == nil) -assert(f(tonumber('', 8)) == nil) -assert(f(tonumber(' ', 9)) == nil) -assert(f(tonumber(' ', 9)) == nil) -assert(f(tonumber('0xf', 10)) == nil) - -assert(f(tonumber('inf')) == nil) -assert(f(tonumber(' INF ')) == nil) -assert(f(tonumber('Nan')) == nil) -assert(f(tonumber('nan')) == nil) - -assert(f(tonumber(' ')) == nil) -assert(f(tonumber('')) == nil) -assert(f(tonumber('1 a')) == nil) -assert(f(tonumber('1 a', 2)) == nil) -assert(f(tonumber('1\0')) == nil) -assert(f(tonumber('1 \0')) == nil) -assert(f(tonumber('1\0 ')) == nil) -assert(f(tonumber('e1')) == nil) -assert(f(tonumber('e 1')) == nil) -assert(f(tonumber(' 3.4.5 ')) == nil) +assert(not f(tonumber('fFfa', 15))) +assert(not f(tonumber('099', 8))) +assert(not f(tonumber('1\0', 2))) +assert(not f(tonumber('', 8))) +assert(not f(tonumber(' ', 9))) +assert(not f(tonumber(' ', 9))) +assert(not f(tonumber('0xf', 10))) + +assert(not f(tonumber('inf'))) +assert(not f(tonumber(' INF '))) +assert(not f(tonumber('Nan'))) +assert(not f(tonumber('nan'))) + +assert(not f(tonumber(' '))) +assert(not f(tonumber(''))) +assert(not f(tonumber('1 a'))) +assert(not f(tonumber('1 a', 2))) +assert(not f(tonumber('1\0'))) +assert(not f(tonumber('1 \0'))) +assert(not f(tonumber('1\0 '))) +assert(not f(tonumber('e1'))) +assert(not f(tonumber('e 1'))) +assert(not f(tonumber(' 3.4.5 '))) -- testing 'tonumber' for invalid hexadecimal formats -assert(tonumber('0x') == nil) -assert(tonumber('x') == nil) -assert(tonumber('x3') == nil) -assert(tonumber('0x3.3.3') == nil) -- two decimal points -assert(tonumber('00x2') == nil) -assert(tonumber('0x 2') == nil) -assert(tonumber('0 x2') == nil) -assert(tonumber('23x') == nil) -assert(tonumber('- 0xaa') == nil) -assert(tonumber('-0xaaP ') == nil) -- no exponent -assert(tonumber('0x0.51p') == nil) -assert(tonumber('0x5p+-2') == nil) +assert(not tonumber('0x')) +assert(not tonumber('x')) +assert(not tonumber('x3')) +assert(not tonumber('0x3.3.3')) -- two decimal points +assert(not tonumber('00x2')) +assert(not tonumber('0x 2')) +assert(not tonumber('0 x2')) +assert(not tonumber('23x')) +assert(not tonumber('- 0xaa')) +assert(not tonumber('-0xaaP ')) -- no exponent +assert(not tonumber('0x0.51p')) +assert(not tonumber('0x5p+-2')) -- testing hexadecimal numerals @@ -620,7 +639,7 @@ assert(maxint % -2 == -1) -- non-portable tests because Windows C library cannot compute -- fmod(1, huge) correctly -if not _port then +if not _ENV._port then local function anan (x) assert(isNaN(x)) end -- assert Not a Number anan(0.0 % 0) anan(1.3 % 0) @@ -666,6 +685,18 @@ assert(eq(math.exp(0), 1)) assert(eq(math.sin(10), math.sin(10%(2*math.pi)))) +do print("testing ldexp/frexp") + global ipairs + for _, x in ipairs{0, 10, 32, -math.pi, 1e10, 1e-10, math.huge, -math.huge} do + local m, p = math.frexp(x) + assert(math.ldexp(m, p) == x) + local am = math.abs(m) + assert(m == x or (0.5 <= am and am < 1)) + end + +end + + assert(tonumber(' 1.3e-2 ') == 1.3e-2) assert(tonumber(' -1.00000000000001 ') == -1.00000000000001) @@ -705,19 +736,19 @@ do -- testing floor & ceil assert(eqT(math.tointeger(maxint), maxint)) assert(eqT(math.tointeger(maxint .. ""), maxint)) assert(eqT(math.tointeger(minint + 0.0), minint)) - assert(math.tointeger(0.0 - minint) == nil) - assert(math.tointeger(math.pi) == nil) - assert(math.tointeger(-math.pi) == nil) + assert(not math.tointeger(0.0 - minint)) + assert(not math.tointeger(math.pi)) + assert(not math.tointeger(-math.pi)) assert(math.floor(math.huge) == math.huge) assert(math.ceil(math.huge) == math.huge) - assert(math.tointeger(math.huge) == nil) + assert(not math.tointeger(math.huge)) assert(math.floor(-math.huge) == -math.huge) assert(math.ceil(-math.huge) == -math.huge) - assert(math.tointeger(-math.huge) == nil) + assert(not math.tointeger(-math.huge)) assert(math.tointeger("34.0") == 34) - assert(math.tointeger("34.3") == nil) - assert(math.tointeger({}) == nil) - assert(math.tointeger(0/0) == nil) -- NaN + assert(not math.tointeger("34.3")) + assert(not math.tointeger({})) + assert(not math.tointeger(0/0)) -- NaN end @@ -758,7 +789,7 @@ do -- testing max/min assert(eqT(math.min(maxint, maxint - 1), maxint - 1)) assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2)) end --- testing implicit convertions +-- testing implicit conversions local a,b = '10', '20' assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20) @@ -767,7 +798,9 @@ assert(a == '10' and b == '20') do print("testing -0 and NaN") - local mz, z = -0.0, 0.0 + global rawset, undef + local mz = -0.0 + local z = 0.0 assert(mz == z) assert(1/mz < 0 and 0 < 1/z) local a = {[mz] = 1} @@ -775,17 +808,18 @@ do a[z] = 2 assert(a[z] == 2 and a[mz] == 2) local inf = math.huge * 2 + 1 - mz, z = -1/inf, 1/inf + local mz = -1/inf + local z = 1/inf assert(mz == z) assert(1/mz < 0 and 0 < 1/z) - local NaN = inf - inf + local NaN = inf - inf assert(NaN ~= NaN) assert(not (NaN < NaN)) assert(not (NaN <= NaN)) assert(not (NaN > NaN)) assert(not (NaN >= NaN)) assert(not (0 < NaN) and not (NaN < 0)) - local NaN1 = 0/0 + local NaN1 = 0/0 assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN)) local a = {} assert(not pcall(rawset, a, NaN, 1)) @@ -801,7 +835,11 @@ do end -print("testing 'math.random'") +-- +-- [[================================================================== + print("testing 'math.random'") +-- -=================================================================== +-- local random, max, min = math.random, math.max, math.min @@ -813,9 +851,9 @@ end -- low-level!! For the current implementation of random in Lua, -- the first call after seed 1007 should return 0x7a7040a5a323c9d6 do - -- all computations assume at most 32-bit integers - local h = 0x7a7040a5 -- higher half - local l = 0xa323c9d6 -- lower half + -- all computations should work with 32-bit integers + local h = 0x7a7040a5 -- higher half + local l = 0xa323c9d6 -- lower half math.randomseed(1007) -- get the low 'intbits' of the 64-bit expected result @@ -838,7 +876,17 @@ do assert(rand * 2^floatbits == res) end -math.randomseed() +do + -- testing return of 'randomseed' + local x, y = math.randomseed() + local res = math.random(0) + x, y = math.randomseed(x, y) -- should repeat the state + assert(math.random(0) == res) + math.randomseed(x, y) -- again should repeat the state + assert(math.random(0) == res) + -- keep the random seed for following tests + print(string.format("random seeds: %d, %d", x, y)) +end do -- test random for floats local randbits = math.min(floatbits, 64) -- at most 64 random bits @@ -949,7 +997,10 @@ do aux(-10,0) aux(1, 6) aux(1, 2) + aux(1, 13) + aux(1, 31) aux(1, 32) + aux(1, 33) aux(-10, 10) aux(-10,-10) -- unit set aux(minint, minint) -- unit set @@ -987,6 +1038,7 @@ do end aux(0, maxint) aux(1, maxint) + aux(3, maxint // 3) aux(minint, -1) aux(minint // 2, maxint // 2) aux(minint, maxint) @@ -1003,6 +1055,91 @@ assert(not pcall(random, minint + 1, minint)) assert(not pcall(random, maxint, maxint - 1)) assert(not pcall(random, maxint, minint)) +-- ]]================================================================== + + +-- +-- [[================================================================== + print("testing precision of 'tostring'") +-- -=================================================================== +-- + +-- number of decimal digits supported by float precision +local decdig = math.floor(floatbits * math.log(2, 10)) +print(string.format(" %d-digit float numbers with full precision", + decdig)) +-- number of decimal digits supported by integer precision +local Idecdig = math.floor(math.log(maxint, 10)) +print(string.format(" %d-digit integer numbers with full precision", + Idecdig)) + +do + -- Any number should print so that reading it back gives itself: + -- tonumber(tostring(x)) == x + + -- Mersenne fractions + local p = 1.0 + for i = 1, maxexp do + p = p + p + local x = 1 / (p - 1) + assert(x == tonumber(tostring(x))) + end + + -- some random numbers in [0,1) + for i = 1, 100 do + local x = math.random() + assert(x == tonumber(tostring(x))) + end + + -- different numbers should print differently. + -- check pairs of floats with minimum detectable difference + local p = floatbits - 1 + global ipairs + for i = 1, maxexp - 1 do + for _, i in ipairs{-i, i} do + local x = 2^i + local diff = 2^(i - p) -- least significant bit for 'x' + local y = x + diff + local fy = tostring(y) + assert(x ~= y and tostring(x) ~= fy) + assert(tonumber(fy) == y) + end + end + + + -- "reasonable" numerals should be printed like themselves + + -- create random float numerals with 5 digits, with a decimal point + -- inserted in all places. (With more than 5, things like "0.00001" + -- reformats like "1e-5".) + for i = 1, 1000 do + -- random numeral with 5 digits + local x = string.format("%.5d", math.random(0, 99999)) + for i = 2, #x do + -- insert decimal point at position 'i' + local y = string.sub(x, 1, i - 1) .. "." .. string.sub(x, i, -1) + y = string.gsub(y, "^0*(%d.-%d)0*$", "%1") -- trim extra zeros + assert(y == tostring(tonumber(y))) + end + end + + -- all-random floats + local Fsz = string.packsize("n") -- size of floats in bytes + + for i = 1, 400 do + local s = string.pack("j", math.random(0)) -- a random string of bits + while #s < Fsz do -- make 's' long enough + s = s .. string.pack("j", math.random(0)) + end + local n = string.unpack("n", s) -- read 's' as a float + s = tostring(n) + if string.find(s, "^%-?%d") then -- avoid NaN, inf, -inf + assert(tonumber(s) == n) + end + end + +end +-- ]]================================================================== print('OK') diff --git a/testes/memerr.lua b/testes/memerr.lua new file mode 100644 index 0000000000..9c940ca79a --- /dev/null +++ b/testes/memerr.lua @@ -0,0 +1,290 @@ +-- $Id: testes/memerr.lua $ +-- See Copyright Notice in file lua.h + + +local function checkerr (msg, f, ...) + local stat, err = pcall(f, ...) + assert(not stat and string.find(err, msg)) +end + +if T==nil then + (Message or print) + ('\n >>> testC not active: skipping memory error tests <<<\n') + return +end + +print("testing memory-allocation errors") + +local debug = require "debug" + +local pack = table.pack + +-- standard error message for memory errors +local MEMERRMSG = "not enough memory" + + +-- memory error in panic function +T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k) +assert(T.checkpanic("newuserdata 20000") == MEMERRMSG) +T.totalmem(0) -- restore high limit + + + +-- {================================================================== +-- Testing memory limits +-- =================================================================== + +checkerr("block too big", T.newuserdata, math.maxinteger) +collectgarbage() +local f = load"local a={}; for i=1,100000 do a[i]=i end" +T.alloccount(10) +checkerr(MEMERRMSG, f) +T.alloccount() -- remove limit + + +-- preallocate stack space +local function deep (n) if n > 0 then deep(n - 1) end end + + +-- test memory errors; increase limit for maximum memory by steps, +-- so that we get memory errors in all allocations of a given +-- task, until there is enough memory to complete the task without +-- errors. +local function testbytes (s, f) + collectgarbage() + local M = T.totalmem() + local oldM = M + local a,b = nil + while true do + collectgarbage(); collectgarbage() + deep(4) + T.totalmem(M) + a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) + T.totalmem(0) -- remove limit + if a and b == "OK" then break end -- stop when no more errors + if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? + error(a, 0) -- propagate it + end + M = M + 7 -- increase memory limit + end + print(string.format("minimum memory for %s: %d bytes", s, M - oldM)) + return a +end + +-- test memory errors; increase limit for number of allocations one +-- by one, so that we get memory errors in all allocations of a given +-- task, until there is enough allocations to complete the task without +-- errors. + +local function testalloc (s, f) + collectgarbage() + local M = 0 + local a,b = nil + while true do + collectgarbage(); collectgarbage() + deep(4) + T.alloccount(M) + a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) + T.alloccount() -- remove limit + if a and b == "OK" then break end -- stop when no more errors + if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? + error(a, 0) -- propagate it + end + M = M + 1 -- increase allocation limit + end + print(string.format("minimum allocations for %s: %d allocations", s, M)) + return M +end + + +local function testamem (s, f) + local aloc = testalloc(s, f) + local res = testbytes(s, f) + return {aloc = aloc, res = res} +end + + +local b = testamem("function call", function () return 10 end) +assert(b.res == 10 and b.aloc == 0) + +testamem("state creation", function () + local st = T.newstate() + if st then T.closestate(st) end -- close new state + return st +end) + +testamem("empty-table creation", function () + return {} +end) + +testamem("string creation", function () + return "XXX" .. "YYY" +end) + +testamem("coroutine creation", function() + return coroutine.create(print) +end) + +do -- vararg tables + local function pack (...t) return t end + local b = testamem("vararg table", function () + return pack(10, 20, 30, 40, "hello") + end) + assert(b.aloc == 3) -- new table uses three memory blocks + -- table optimized away + local function sel (n, ...arg) return arg[n] + arg.n end + local b = testamem("optimized vararg table", + function () return sel(2.0, 20, 30) end) + assert(b.res == 32 and b.aloc == 0) -- no memory needed for this case +end + +-- testing to-be-closed variables +testamem("to-be-closed variables", function() + local flag + do + local x = + setmetatable({}, {__close = function () flag = true end}) + flag = false + local x = {} + end + return flag +end) + + +-- testing threads + +-- get main thread from registry +local mt = T.testC("rawgeti R !M; return 1") +assert(type(mt) == "thread" and coroutine.running() == mt) + + + +local function expand (n,s) + if n==0 then return "" end + local e = string.rep("=", n) + return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", + e, s, expand(n-1,s), e) +end + +G=0; collectgarbage() +load(expand(20,"G=G+1"))() +assert(G==20); collectgarbage() +G = nil + +testamem("running code on new thread", function () + return T.doonnewstack("local x=1") == 0 -- try to create thread +end) + + +do -- external strings + local str = string.rep("a", 100) + testamem("creating external strings", function () + return T.externstr(str) + end) +end + + +-- testing memory x compiler + +testamem("loadstring", function () + return load("x=1") -- try to do load a string +end) + + +local testprog = [[ +local function foo () return end +local t = {"x"} +AA = "aaa" +for i = 1, #t do AA = AA .. t[i] end +return true +]] + +-- testing memory x dofile +_G.AA = nil +local t =os.tmpname() +local f = assert(io.open(t, "w")) +f:write(testprog) +f:close() +testamem("dofile", function () + local a = loadfile(t) + return a and a() +end) +assert(os.remove(t)) +assert(_G.AA == "aaax") + + +-- other generic tests + +testamem("gsub", function () + local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end) + return (a == 'ablo ablo') +end) + +testamem("dump/undump", function () + local a = load(testprog) + local b = a and string.dump(a) + a = b and load(b) + return a and a() +end) + +_G.AA = nil + +local t = os.tmpname() +testamem("file creation", function () + local f = assert(io.open(t, 'w')) + assert (not io.open"nomenaoexistente") + io.close(f); + return not loadfile'nomenaoexistente' +end) +assert(os.remove(t)) + +testamem("table creation", function () + local a, lim = {}, 10 + for i=1,lim do a[i] = i; a[i..'a'] = {} end + return (type(a[lim..'a']) == 'table' and a[lim] == lim) +end) + +testamem("constructors", function () + local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5} + return (type(a) == 'table' and a.e == 5) +end) + +local a = 1 +local close = nil +testamem("closure creation", function () + function close (b) + return function (x) return b + x end + end + return (close(2)(4) == 6) +end) + +testamem("using coroutines", function () + local a = coroutine.wrap(function () + coroutine.yield(string.rep("a", 10)) + return {} + end) + assert(string.len(a()) == 10) + return a() +end) + +do -- auxiliary buffer + local lim = 100 + local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end + testamem("auxiliary buffer", function () + return (#table.concat(a, ",") == 20*lim + lim - 1) + end) +end + +testamem("growing stack", function () + local function foo (n) + if n == 0 then return 1 else return 1 + foo(n - 1) end + end + return foo(100) +end) + +-- }================================================================== + + +print "Ok" + + diff --git a/testes/nextvar.lua b/testes/nextvar.lua index d2306ed1ff..098e7891c9 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -1,5 +1,7 @@ -- $Id: testes/nextvar.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h + +global * print('testing tables, next, and for') @@ -9,6 +11,38 @@ local function checkerror (msg, f, ...) end + +---------------------------------------------------------------- +local function printTable (t) + local a, h = T.querytab(t) + print("array:") + for i = 1, a do + print("", T.querytab(t, i - 1)) + end + print("hash:") + for i = 1, h do + print("", T.querytab(t, a + i - 1)) + end +end +---------------------------------------------------------------- +local function countentries (t) + local e = 0 + for _ in pairs(t) do e = e + 1 end + return e +end +---------------------------------------------------------------- + + +local function check (t, na, nh) + if not T then return end + local a, h = T.querytab(t) + if a ~= na or h ~= nh then + print(na, nh, a, h) + assert(nil) + end +end + + local a = {} -- make sure table has lots of space in hash part @@ -20,6 +54,44 @@ for i=1,100 do assert(#a == i) end + +do -- rehash moving elements from array to hash + local a = {} + for i = 1, 100 do a[i] = i end + check(a, 128, 0) + + for i = 5, 95 do a[i] = nil end + check(a, 128, 0) + + a[129] = 1 -- force a re-hash + check(a, 4, 8) -- keys larger than 4 go to the hash part + + for i = 1, 4 do assert(a[i] == i) end + for i = 5, 95 do assert(a[i] == nil) end + for i = 96, 100 do assert(a[i] == i) end + assert(a[129] == 1) +end + + +do -- growing hash part keeping array part + local a = table.create(1000) + check(a, 1000, 0) + a.x = 10 + check(a, 1000, 1) -- array part keeps its elements +end + + +do -- "growing" length of a prebuilt table + local N = 100 + local a = table.create(N) + for i = 1, N do + a[#a + 1] = true + assert(#a == i) + end + check(a, N, 0) +end + + -- testing ipairs local x = 0 for k,v in ipairs{10,20,30;x=12} do @@ -43,48 +115,46 @@ assert(i == 4) assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{}) +do -- overflow (must wrap-around) + local f = ipairs{} + local k, v = f({[math.mininteger] = 10}, math.maxinteger) + assert(k == math.mininteger and v == 10) + k, v = f({[math.mininteger] = 10}, k) + assert(k == nil) +end + + +do + -- alternate insertions and deletions in an almost full hash. + -- In versions pre-5.5, that causes constant rehashings and + -- takes a long time to complete. + local a = {} + for i = 1, 2^11 - 1 do + a[i .. ""] = true + end + + for i = 1, 1e5 do + local key = i .. "." + a[key] = true + a[key] = nil + end + assert(countentries(a) == 2^11 - 1) +end + if not T then (Message or print) ('\n >>> testC not active: skipping tests for table sizes <<<\n') else --[ -- testing table sizes -local function log2 (x) return math.log(x, 2) end -local function mp2 (n) -- minimum power of 2 >= n - local mp = 2^math.ceil(log2(n)) - assert(n == 0 or (mp/2 < n and n <= mp)) +-- minimum power of 2 (or zero) >= n +local function mp2 (n) + local mp = 2^math.ceil(math.log(n, 2)) + assert((mp == 0 or mp/2 < n) and n <= mp) return mp end -local function fb (n) - local r, nn = T.int2fb(n) - assert(r < 256) - return nn -end - --- test fb function -for a = 1, 10000 do -- all numbers up to 10^4 - local n = fb(a) - assert(a <= n and n <= a*1.125) -end -local a = 1024 -- plus a few up to 2 ^30 -local lim = 2^30 -while a < lim do - local n = fb(a) - assert(a <= n and n <= a*1.125) - a = math.ceil(a*1.3) -end - - -local function check (t, na, nh) - local a, h = T.querytab(t) - if a ~= na or h ~= nh then - print(na, nh, a, h) - assert(nil) - end -end - -- testing C library sizes do @@ -95,37 +165,54 @@ end -- testing constructor sizes -local lim = 40 -local s = 'return {' -for i=1,lim do - s = s..i..',' - local s = s - for k=0,lim do - local t = load(s..'}', '')() - assert(#t == i) - check(t, fb(i), mp2(k)) - s = string.format('%sa%d=%d,', s, k, k) +local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, + 30, 31, 32, 33, 34, 254, 255, 256, 257, 500, 1001} + +for _, sa in ipairs(sizes) do -- 'sa' is size of the array part + local arr = {"return {"} + for i = 1, sa do arr[1 + i] = "1," end -- build array part + for _, sh in ipairs(sizes) do -- 'sh' is size of the hash part + for j = 1, sh do -- build hash part + arr[1 + sa + j] = string.format('k%x=%d,', j, j) + end + arr[1 + sa + sh + 1] = "}" + local prog = table.concat(arr) + local f = assert(load(prog)) + collectgarbage("stop") + f() -- call once to ensure stack space + -- make sure table is not resized after being created + if sa == 0 or sh == 0 then + T.alloccount(2); -- header + array or hash part + else + T.alloccount(3); -- header + array part + hash part + end + local t = f() + T.alloccount(); + collectgarbage("restart") + assert(#t == sa) + check(t, sa, mp2(sh)) end end -- tests with unknown number of elements local a = {} -for i=1,lim do a[i] = i end -- build auxiliary table -for k=0,lim do - local a = {table.unpack(a,1,k)} - assert(#a == k) - check(a, k, 0) - a = {1,2,3,table.unpack(a,1,k)} - check(a, k+3, 0) - assert(#a == k + 3) +for i=1,sizes[#sizes] do a[i] = i end -- build auxiliary table +for k in ipairs(sizes) do + local t = {table.unpack(a,1,k)} + assert(#t == k) + check(t, k, 0) + t = {1,2,3,table.unpack(a,1,k)} + check(t, k+3, 0) + assert(#t == k + 3) end -- testing tables dynamically built local lim = 130 -local a = {}; a[2] = 1; check(a, 0, 1) -a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2) +local a = {}; a[2] = 1; check(a, 2, 0) +a = {}; a[0] = 1; check(a, 0, 1); +a[2] = 1; check(a, 2, 1) a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1) a = {} for i = 1,lim do @@ -141,31 +228,85 @@ for i = 1,lim do check(a, 0, mp2(i)) end -a = {} -for i=1,16 do a[i] = i end -check(a, 16, 0) + +-- insert and delete elements until a rehash occur. Caller must ensure +-- that a rehash will change the shape of the table. Must repeat because +-- the insertion may collide with the deleted element, and then there is +-- no rehash. +local function forcerehash (t) + local na, nh = T.querytab(t) + local i = 10000 + repeat + i = i + 1 + t[i] = true + t[i] = undef + local nna, nnh = T.querytab(t) + until nna ~= na or nnh ~= nh +end + + do + local a = {} + for i=1,16 do a[i] = i end + check(a, 16, 0) for i=1,11 do a[i] = undef end - for i=30,50 do a[i] = true; a[i] = undef end -- force a rehash (?) - check(a, 0, 8) -- 5 elements in the table + check(a, 16, 0) + a[30] = true -- force a rehash + a[30] = undef + check(a, 0, 8) -- 5 elements in the hash part: [12]-[16] a[10] = 1 - for i=30,50 do a[i] = true; a[i] = undef end -- force a rehash (?) - check(a, 0, 8) -- only 6 elements in the table + forcerehash(a) + check(a, 16, 1) for i=1,14 do a[i] = true; a[i] = undef end - for i=18,50 do a[i] = true; a[i] = undef end -- force a rehash (?) - check(a, 0, 4) -- only 2 elements ([15] and [16]) + check(a, 16, 1) -- no rehash... + a[31] = true; a[32] = true -- force a rehash + check(a, 0, 4) -- [15], [16], [31], [32] end -- reverse filling -for i=1,lim do +do + local N = 2^10 local a = {} - for i=i,1,-1 do a[i] = i end -- fill in reverse - check(a, mp2(i), 0) + for i = N, 1, -1 do a[i] = i end -- fill in reverse + check(a, mp2(N), 0) +end + + +do -- "almost sparse" arrays + -- create table with holes in 1/3 of its entries; all its + -- elements are always in the array part + local a = {} + for i = 1, 257 do + if i % 3 ~= 1 then + a[i] = true + check(a, mp2(i), 0) + end + end end + +do + -- alternate insertions and deletions should give some extra + -- space for the hash part. Otherwise, a mix of insertions/deletions + -- could cause too many rehashes. (See the other test for "alternate + -- insertions and deletions" in this file.) + local a = {} + for i = 1, 256 do + a[i .. ""] = true + end + check(a, 0, 256) -- hash part is full + a["256"] = nil -- delete a key + forcerehash(a) + -- table has only 255 elements, but it got some extra space; + -- otherwise, almost each delete-insert would rehash the table again. + assert(countentries(a) == 255) + check(a, 0, 512) +end + + -- size tests for vararg lim = 35 -function foo (n, ...) +local function foo (n, ...) local arg = {...} check(arg, n, 0) assert(select('#', ...) == n) @@ -177,21 +318,6 @@ end local a = {} for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end - --- Table length with limit smaller than maximum value at array -local a = {} -for i = 1,64 do a[i] = true end -- make its array size 64 -for i = 1,64 do a[i] = nil end -- erase all elements -assert(T.querytab(a) == 64) -- array part has 64 elements -a[32] = true; a[48] = true; -- binary search will find these ones -a[51] = true -- binary search will miss this one -assert(#a == 48) -- this will set the limit -assert(select(4, T.querytab(a)) == 48) -- this is the limit now -a[50] = true -- this will set a new limit -assert(select(4, T.querytab(a)) == 50) -- this is the limit now --- but the size is larger (and still inside the array part) -assert(#a == 51) - end --] @@ -205,13 +331,36 @@ assert(#{1, 2, 3, nil, nil} == 3) print'+' -local nofind = {} +do + local s1, s2 = math.randomseed() + print(string.format( + "testing length for some random tables (seeds 0X%x:%x)", s1, s2)) + local N = 130 + for i = 1, 1e3 do -- create that many random tables + local a = table.create(math.random(N)) -- initiate with random size + for j = 1, math.random(N) do -- add random number of random entries + a[math.random(N)] = true + end + assert(#a == 0 or a[#a] and not a[#a + 1]) + end +end -a,b,c = 1,2,3 -a,b,c = nil +do print("testing attack on table length") + local t = {} + local lim = math.floor(math.log(math.maxinteger, 2)) - 1 + for i = lim, 0, -1 do + t[2^i] = true + end + assert(t[1 << lim]) + -- next loop should not take forever + for i = 1, #t do end +end --- next uses always the same iteraction function +local nofind = {} + + +-- next uses always the same iteration function assert(next{} == next{}) local function find (name) @@ -258,7 +407,7 @@ for i=0,10000 do end end -n = {n=0} +local n = {n=0} for i,v in pairs(a) do n.n = n.n+1 assert(i and v and a[i] == v) @@ -279,7 +428,7 @@ do -- clear global table end --- +-- local function checknext (a) local b = {} @@ -363,6 +512,38 @@ end assert(n == 5) +do + print("testing next x GC of deleted keys") + -- bug in 5.4.1 + local co = coroutine.wrap(function (t) + for k, v in pairs(t) do + local k1 = next(t) -- all previous keys were deleted + assert(k == k1) -- current key is the first in the table + t[k] = nil + local expected = (type(k) == "table" and k[1] or + type(k) == "function" and k() or + string.sub(k, 1, 1)) + assert(expected == v) + coroutine.yield(v) + end + end) + local t = {} + t[{1}] = 1 -- add several unanchored, collectable keys + t[{2}] = 2 + t[string.rep("a", 50)] = "a" -- long string + t[string.rep("b", 50)] = "b" + t[{3}] = 3 + t[string.rep("c", 10)] = "c" -- short string + t[function () return 10 end] = 10 + local count = 7 + while co(t) do + collectgarbage("collect") -- collect dead keys + count = count - 1 + end + assert(count == 0 and next(t) == nil) -- traversed the whole table +end + + local function test (a) assert(not pcall(table.insert, a, 2, 20)); table.insert(a, 10); table.insert(a, 2, 20); @@ -471,6 +652,15 @@ do -- testing table library with metamethods end +do -- testing overflow in table.insert (must wrap-around) + + local t = setmetatable({}, + {__len = function () return math.maxinteger end}) + table.insert(t, 20) + local k, v = next(t) + assert(k == math.mininteger and v == 20) +end + if not T then (Message or print) ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n') @@ -500,7 +690,7 @@ else --[ mt.__newindex = nil mt.__len = nil local tab2 = {} - local u2 = T.newuserdata(0) + local u2 = T.newuserdata(0) debug.setmetatable(u2, {__newindex = function (_, k, v) tab2[k] = v end}) table.move(u, 1, 4, 1, u2) assert(#tab2 == 4 and tab2[1] == tab[1] and tab2[4] == tab[4]) @@ -544,6 +734,14 @@ do a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1) end +do -- attempt to change the control variable + local st, msg = load "for i = 1, 10 do i = 10 end" + assert(not st and string.find(msg, "assign to const variable 'i'")) + + local st, msg = load "for v, k in pairs{} do v = 10 end" + assert(not st and string.find(msg, "assign to const variable 'v'")) +end + -- conversion a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5) @@ -601,6 +799,69 @@ do -- checking types end + +do -- testing other strange cases for numeric 'for' + + local function checkfor (from, to, step, t) + local c = 0 + for i = from, to, step do + c = c + 1 + assert(i == t[c]) + end + assert(c == #t) + end + + local maxi = math.maxinteger + local mini = math.mininteger + + checkfor(mini, maxi, maxi, {mini, -1, maxi - 1}) + + checkfor(mini, math.huge, maxi, {mini, -1, maxi - 1}) + + checkfor(maxi, mini, mini, {maxi, -1}) + + checkfor(maxi, mini, -maxi, {maxi, 0, -maxi}) + + checkfor(maxi, -math.huge, mini, {maxi, -1}) + + checkfor(maxi, mini, 1, {}) + checkfor(mini, maxi, -1, {}) + + checkfor(maxi - 6, maxi, 3, {maxi - 6, maxi - 3, maxi}) + checkfor(mini + 4, mini, -2, {mini + 4, mini + 2, mini}) + + local step = maxi // 10 + local c = mini + for i = mini, maxi, step do + assert(i == c) + c = c + step + end + + c = maxi + for i = maxi, mini, -step do + assert(i == c) + c = c - step + end + + checkfor(maxi, maxi, maxi, {maxi}) + checkfor(maxi, maxi, mini, {maxi}) + checkfor(mini, mini, maxi, {mini}) + checkfor(mini, mini, mini, {mini}) +end + + +checkerror("'for' step is zero", function () + for i = 1, 10, 0 do end +end) + +checkerror("'for' step is zero", function () + for i = 1, -10, 0 do end +end) + +checkerror("'for' step is zero", function () + for i = 1.0, -10, 0.0 do end +end) + collectgarbage() @@ -608,7 +869,8 @@ collectgarbage() local function f (n, p) local t = {}; for i=1,p do t[i] = i*10 end - return function (_,n) + return function (_, n, ...) + assert(select("#", ...) == 0) -- no extra arguments if n > 0 then n = n-1 return n, table.unpack(t) @@ -643,13 +905,18 @@ local function foo1 (e,i) if i <= e.n then return i,a[i] end end -setmetatable(a, {__pairs = function (x) return foo, x, 0 end}) +local closed = false +setmetatable(a, {__pairs = function (x) + local tbc = setmetatable({}, {__close = function () closed = true end}) + return foo, x, 0, tbc + end}) local i = 0 for k,v in pairs(a) do i = i + 1 assert(k == i and v == k+1) end +assert(closed) -- 'tbc' has been closed a.n = 5 a[3] = 30 @@ -657,7 +924,7 @@ a[3] = 30 -- testing ipairs with metamethods a = {n=10} setmetatable(a, { __index = function (t,k) - if k <= t.n then return k * 10 end + if k <= t.n then return k * 10 end end}) i = 0 for k,v in ipairs(a) do @@ -666,4 +933,25 @@ for k,v in ipairs(a) do end assert(i == a.n) + +-- testing yield inside __pairs +do + local t = setmetatable({10, 20, 30}, {__pairs = function (t) + local inc = coroutine.yield() + return function (t, i) + if i > 1 then return i - inc, t[i - inc] else return nil end + end, t, #t + 1 + end}) + + local res = {} + local co = coroutine.wrap(function () + for i,p in pairs(t) do res[#res + 1] = p end + end) + + co() -- start coroutine + co(1) -- continue after yield + assert(res[1] == 30 and res[2] == 20 and res[3] == 10 and #res == 3) + +end + print"OK" diff --git a/testes/packtests b/testes/packtests new file mode 100755 index 0000000000..855c054a0c --- /dev/null +++ b/testes/packtests @@ -0,0 +1,56 @@ +NAME=$1"-tests" + +ln -s . $NAME +ln -s .. ltests + +tar --create --gzip --no-recursion --file=$NAME.tar.gz \ +$NAME/all.lua \ +$NAME/api.lua \ +$NAME/attrib.lua \ +$NAME/big.lua \ +$NAME/bitwise.lua \ +$NAME/bwcoercion.lua \ +$NAME/calls.lua \ +$NAME/closure.lua \ +$NAME/code.lua \ +$NAME/constructs.lua \ +$NAME/coroutine.lua \ +$NAME/cstack.lua \ +$NAME/db.lua \ +$NAME/errors.lua \ +$NAME/events.lua \ +$NAME/files.lua \ +$NAME/gc.lua \ +$NAME/gengc.lua \ +$NAME/goto.lua \ +$NAME/heavy.lua \ +$NAME/literals.lua \ +$NAME/locals.lua \ +$NAME/main.lua \ +$NAME/math.lua \ +$NAME/memerr.lua \ +$NAME/nextvar.lua \ +$NAME/pm.lua \ +$NAME/sort.lua \ +$NAME/strings.lua \ +$NAME/tpack.lua \ +$NAME/tracegc.lua \ +$NAME/utf8.lua \ +$NAME/vararg.lua \ +$NAME/verybig.lua \ +$NAME/libs/makefile \ +$NAME/libs/P1 \ +$NAME/libs/lib1.c \ +$NAME/libs/lib11.c \ +$NAME/libs/lib2.c \ +$NAME/libs/lib21.c \ +$NAME/libs/lib22.c \ +$NAME/ltests/ltests.h \ +$NAME/ltests/ltests.c + +\rm -I $NAME +\rm -I ltests + +echo ${NAME}.tar.gz" created" + + diff --git a/testes/pm.lua b/testes/pm.lua index 8cc8772e87..720d2a3562 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -1,37 +1,42 @@ -- $Id: testes/pm.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h + +-- UTF-8 file + print('testing pattern matching') +global * + local function checkerror (msg, f, ...) local s, err = pcall(f, ...) assert(not s and string.find(err, msg)) end -function f(s, p) +local function f (s, p) local i,e = string.find(s, p) if i then return string.sub(s, i, e) end end -a,b = string.find('', '') -- empty patterns are tricky +local a,b = string.find('', '') -- empty patterns are tricky assert(a == 1 and b == 0); a,b = string.find('alo', '') assert(a == 1 and b == 0) a,b = string.find('a\0o a\0o a\0o', 'a', 1) -- first position assert(a == 1 and b == 1) -a,b = string.find('a\0o a\0o a\0o', 'a\0o', 2) -- starts in the midle +a,b = string.find('a\0o a\0o a\0o', 'a\0o', 2) -- starts in the middle assert(a == 5 and b == 7) -a,b = string.find('a\0o a\0o a\0o', 'a\0o', 9) -- starts in the midle +a,b = string.find('a\0o a\0o a\0o', 'a\0o', 9) -- starts in the middle assert(a == 9 and b == 11) a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end assert(a == 9 and b == 11); a,b = string.find('a\0a\0a\0a\0\0ab', 'b') -- last position assert(a == 11 and b == 11) -assert(string.find('a\0a\0a\0a\0\0ab', 'b\0') == nil) -- check ending -assert(string.find('', '\0') == nil) +assert(not string.find('a\0a\0a\0a\0\0ab', 'b\0')) -- check ending +assert(not string.find('', '\0')) assert(string.find('alo123alo', '12') == 4) -assert(string.find('alo123alo', '^12') == nil) +assert(not string.find('alo123alo', '^12')) assert(string.match("aaab", ".*b") == "aaab") assert(string.match("aaa", ".*a") == "aaa") @@ -50,6 +55,20 @@ assert(f('aLo_ALO', '%a*') == 'aLo') assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu") + +-- Adapt a pattern to UTF-8 +local function PU (p) + -- distribute '?' into each individual byte of a character. + -- (For instance, "á?" becomes "\195?\161?".) + p = string.gsub(p, "(" .. utf8.charpattern .. ")%?", function (c) + return string.gsub(c, ".", "%0?") + end) + -- change '.' to utf-8 character patterns + p = string.gsub(p, "%.", utf8.charpattern) + return p +end + + assert(f('aaab', 'a*') == 'aaa'); assert(f('aaa', '^.*$') == 'aaa'); assert(f('aaa', 'b*') == ''); @@ -57,38 +76,38 @@ assert(f('aaa', 'ab*a') == 'aa') assert(f('aba', 'ab*a') == 'aba') assert(f('aaab', 'a+') == 'aaa') assert(f('aaa', '^.+$') == 'aaa') -assert(f('aaa', 'b+') == nil) -assert(f('aaa', 'ab+a') == nil) +assert(not f('aaa', 'b+')) +assert(not f('aaa', 'ab+a')) assert(f('aba', 'ab+a') == 'aba') assert(f('a$a', '.$') == 'a') assert(f('a$a', '.%$') == 'a$') assert(f('a$a', '.$.') == 'a$a') -assert(f('a$a', '$$') == nil) -assert(f('a$b', 'a$') == nil) +assert(not f('a$a', '$$')) +assert(not f('a$b', 'a$')) assert(f('a$a', '$') == '') assert(f('', 'b*') == '') -assert(f('aaa', 'bb*') == nil) +assert(not f('aaa', 'bb*')) assert(f('aaab', 'a-') == '') assert(f('aaa', '^.-$') == 'aaa') assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab') assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab') assert(f('alo xo', '.o$') == 'xo') -assert(f(' \n isto é assim', '%S%S*') == 'isto') -assert(f(' \n isto é assim', '%S*$') == 'assim') -assert(f(' \n isto é assim', '[a-z]*$') == 'assim') +assert(f(' \n isto é assim', '%S%S*') == 'isto') +assert(f(' \n isto é assim', '%S*$') == 'assim') +assert(f(' \n isto é assim', '[a-z]*$') == 'assim') assert(f('um caracter ? extra', '[^%sa-z]') == '?') assert(f('', 'a?') == '') -assert(f('á', 'á?') == 'á') -assert(f('ábl', 'á?b?l?') == 'ábl') -assert(f(' ábl', 'á?b?l?') == '') +assert(f('á', PU'á?') == 'á') +assert(f('ábl', PU'á?b?l?') == 'ábl') +assert(f(' ábl', PU'á?b?l?') == '') assert(f('aa', '^aa?a?a') == 'aa') -assert(f(']]]áb', '[^]]') == 'á') +assert(f(']]]áb', '[^]]+') == 'áb') assert(f("0alo alo", "%x*") == "0a") assert(f("alo alo", "%C+") == "alo alo") print('+') -function f1(s, p) +local function f1 (s, p) p = string.gsub(p, "%%([0-9])", function (s) return "%" .. (tonumber(s)+1) end) @@ -101,7 +120,7 @@ end assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o") assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3') assert(f1('=======', '^(=*)=%1$') == '=======') -assert(string.match('==========', '^([=]*)=%1$') == nil) +assert(not string.match('==========', '^([=]*)=%1$')) local function range (i, j) if i <= j then @@ -113,7 +132,7 @@ local abc = string.char(range(0, 127)) .. string.char(range(128, 255)); assert(string.len(abc) == 256) -function strset (p) +local function strset (p) local res = {s=''} string.gsub(abc, p, function (c) res.s = res.s .. c end) return res.s @@ -135,29 +154,29 @@ print('+'); assert(string.match("alo xyzK", "(%w+)K") == "xyz") assert(string.match("254 K", "(%d*)K") == "") assert(string.match("alo ", "(%w*)$") == "") -assert(string.match("alo ", "(%w+)$") == nil) -assert(string.find("(álo)", "%(á") == 1) -local a, b, c, d, e = string.match("âlo alo", "^(((.).).* (%w*))$") -assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil) +assert(not string.match("alo ", "(%w+)$")) +assert(string.find("(álo)", "%(á") == 1) +local a, b, c, d, e = string.match("âlo alo", PU"^(((.).). (%w*))$") +assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil) a, b, c, d = string.match('0123456789', '(.+(.?)())') assert(a == '0123456789' and b == '' and c == 11 and d == nil) print('+') -assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo') -assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim +assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo') +assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') -t = "abç d" -a, b = string.gsub(t, '(.)', '%1@') -assert('@'..a == string.gsub(t, '', '@') and b == 5) -a, b = string.gsub('abçd', '(.)', '%0@', 2) -assert(a == 'a@b@çd' and b == 2) +local t = "abç d" +a, b = string.gsub(t, PU'(.)', '%1@') +assert(a == "a@b@ç@ @d@" and b == 5) +a, b = string.gsub('abçd', PU'(.)', '%0@', 2) +assert(a == 'a@b@çd' and b == 2) assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o') assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") == "xyz=abc-abc=xyz") assert(string.gsub("abc", "%w", "%1%0") == "aabbcc") assert(string.gsub("abc", "%w+", "%0%1") == "abcabc") -assert(string.gsub('áéí', '$', '\0óú') == 'áéí\0óú') +assert(string.gsub('áéí', '$', '\0óú') == 'áéí\0óú') assert(string.gsub('', '^', 'r') == 'r') assert(string.gsub('', '$', 'r') == 'r') print('+') @@ -184,32 +203,34 @@ do local function setglobal (n,v) rawset(_G, n, v) end string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal) assert(_G.a=="roberto" and _G.roberto=="a") + _G.a = nil; _G.roberto = nil end function f(a,b) return string.gsub(a,'.',b) end -assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) == - "trocar tudo em bbbbb é alalalalalal") +assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) == + "trocar tudo em bbbbb é alalalalalal") local function dostring (s) return load(s, "")() or "" end assert(string.gsub("alo $a='x'$ novamente $return a$", "$([^$]*)%$", dostring) == "alo novamente x") -x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", +local x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", "$([^$]*)%$", dostring) assert(x == ' assim vai para ALO') - -t = {} -s = 'a alo jose joao' -r = string.gsub(s, '()(%w+)()', function (a,w,b) - assert(string.len(w) == b-a); - t[a] = b-a; - end) +_G.a, _G.x = nil + +local t = {} +local s = 'a alo jose joao' +local r = string.gsub(s, '()(%w+)()', function (a,w,b) + assert(string.len(w) == b-a); + t[a] = b-a; + end) assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) -function isbalanced (s) - return string.find(string.gsub(s, "%b()", ""), "[()]") == nil +local function isbalanced (s) + return not string.find(string.gsub(s, "%b()", ""), "[()]") end assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a")) @@ -251,7 +272,7 @@ if not _soft then end -- recursive nest of gsubs -function rev (s) +local function rev (s) return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end) end @@ -387,5 +408,35 @@ assert(string.match("abc\0\0\0", "%\0%\0?") == "\0\0") assert(string.find("abc\0\0","\0.") == 4) assert(string.find("abcx\0\0abc\0abc","x\0\0abc\0a.") == 4) + +do -- test reuse of original string in gsub + local s = string.rep("a", 100) + local r = string.gsub(s, "b", "c") -- no match + assert(string.format("%p", s) == string.format("%p", r)) + + r = string.gsub(s, ".", {x = "y"}) -- no substitutions + assert(string.format("%p", s) == string.format("%p", r)) + + local count = 0 + r = string.gsub(s, ".", function (x) + assert(x == "a") + count = count + 1 + return nil -- no substitution + end) + r = string.gsub(r, ".", {b = 'x'}) -- "a" is not a key; no subst. + assert(count == 100) + assert(string.format("%p", s) == string.format("%p", r)) + + count = 0 + r = string.gsub(s, ".", function (x) + assert(x == "a") + count = count + 1 + return x -- substitution... + end) + assert(count == 100) + -- no reuse in this case + assert(r == s and string.format("%p", s) ~= string.format("%p", r)) +end + print('OK') diff --git a/testes/sort.lua b/testes/sort.lua index ef405d9219..b012766057 100644 --- a/testes/sort.lua +++ b/testes/sort.lua @@ -1,12 +1,8 @@ -- $Id: testes/sort.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print "testing (parts of) table library" -print "testing unpack" - -local unpack = table.unpack - local maxI = math.maxinteger local minI = math.mininteger @@ -17,10 +13,44 @@ local function checkerror (msg, f, ...) end +do print "testing 'table.create'" + local N = 10000 + collectgarbage() + local m = collectgarbage("count") * 1024 + local t = table.create(N) + local memdiff = collectgarbage("count") * 1024 - m + assert(memdiff > N * 4) + for i = 1, 20 do + assert(#t == i - 1) + t[i] = 0 + end + for i = 1, 20 do t[#t + 1] = i * 10 end + assert(#t == 40 and t[39] == 190) + assert(not T or T.querytab(t) == N) + t = nil + collectgarbage() + m = collectgarbage("count") * 1024 + t = table.create(0, 1024) + memdiff = collectgarbage("count") * 1024 - m + assert(memdiff > 1024 * 12) + assert(not T or select(2, T.querytab(t)) == 1024) + + local maxint1 = 1 << (string.packsize("i") * 8 - 1) + checkerror("out of range", table.create, maxint1) + checkerror("out of range", table.create, 0, maxint1) + checkerror("table overflow", table.create, 0, maxint1 - 1) +end + + +print "testing unpack" + +local unpack = table.unpack + + checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) local x,y,z,a,n -a = {}; lim = _soft and 200 or 2000 +a = {}; local lim = _soft and 200 or 2000 for i=1, lim do a[i]=i end assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) x = unpack(a) @@ -169,7 +199,7 @@ do __index = function (_,k) pos1 = k end, __newindex = function (_,k) pos2 = k; error() end, }) local st, msg = pcall(table.move, a, f, e, t) - assert(not st and not msg and pos1 == x and pos2 == y) + assert(not st and pos1 == x and pos2 == y) end checkmove(1, maxI, 0, 1, 0) checkmove(0, maxI - 1, 1, maxI - 1, maxI) @@ -222,7 +252,7 @@ a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", table.sort(a) check(a) -function perm (s, n) +local function perm (s, n) n = n or #s if n == 1 then local t = {unpack(s)} @@ -248,7 +278,7 @@ perm{1,2,3,3,5} perm{1,2,3,4,5,6} perm{2,2,3,3,5,6} -function timesort (a, n, func, msg, pre) +local function timesort (a, n, func, msg, pre) local x = os.clock() table.sort(a, func) x = (os.clock() - x) * 1000 @@ -257,7 +287,7 @@ function timesort (a, n, func, msg, pre) check(a, func) end -limit = 50000 +local limit = 50000 if _soft then limit = 5000 end a = {} @@ -274,7 +304,7 @@ for i=1,limit do a[i] = math.random() end -x = os.clock(); i=0 +local x = os.clock(); local i = 0 table.sort(a, function(x,y) i=i+1; return y * print('testing strings and string library') -local maxi, mini = math.maxinteger, math.mininteger +local maxi = math.maxinteger +local mini = math.mininteger local function checkerror (msg, f, ...) @@ -51,17 +56,17 @@ assert(("\000123456789"):sub(8) == "789") -- testing string.find assert(string.find("123456789", "345") == 3) -a,b = string.find("123456789", "345") +local a,b = string.find("123456789", "345") assert(string.sub("123456789", a, b) == "345") assert(string.find("1234567890123456789", "345", 3) == 3) assert(string.find("1234567890123456789", "345", 4) == 13) -assert(string.find("1234567890123456789", "346", 4) == nil) +assert(not string.find("1234567890123456789", "346", 4)) assert(string.find("1234567890123456789", ".45", -9) == 13) -assert(string.find("abcdefg", "\0", 5, 1) == nil) +assert(not string.find("abcdefg", "\0", 5, 1)) assert(string.find("", "") == 1) assert(string.find("", "", 1) == 1) assert(not string.find("", "", 2)) -assert(string.find('', 'aaa', 1) == nil) +assert(not string.find('', 'aaa', 1)) assert(('alo(.)alo'):find('(.)', 1, 1) == 4) assert(string.len("") == 0) @@ -105,10 +110,9 @@ assert(string.rep('teste', 0) == '') assert(string.rep('tés\00tê', 2) == 'tés\0têtés\000tê') assert(string.rep('', 10) == '') -if string.packsize("i") == 4 then - -- result length would be 2^31 (int overflow) - checkerror("too large", string.rep, 'aa', (1 << 30)) - checkerror("too large", string.rep, 'a', (1 << 30), ',') +do + checkerror("too large", string.rep, 'aa', math.maxinteger); + checkerror("too large", string.rep, 'a', math.maxinteger, ',') end -- repetitions with separator @@ -153,24 +157,51 @@ else -- compatible coercion assert(tostring(-1203 + 0.0) == "-1203") end + +local function topointer (s) + return string.format("%p", s) +end + + do -- tests for '%p' format -- not much to test, as C does not specify what '%p' does. -- ("The value of the pointer is converted to a sequence of printing -- characters, in an implementation-defined manner.") - local null = string.format("%p", nil) - assert(string.format("%p", {}) ~= null) + local null = "(null)" -- nulls are formatted by Lua assert(string.format("%p", 4) == null) + assert(string.format("%p", true) == null) + assert(string.format("%p", nil) == null) + assert(string.format("%p", {}) ~= null) assert(string.format("%p", print) ~= null) assert(string.format("%p", coroutine.running()) ~= null) - assert(string.format("%p", {}) ~= string.format("%p", {})) - assert(string.format("%p", string.rep("a", 10)) == - string.format("%p", string.rep("a", 10))) -- short strings - assert(string.format("%p", string.rep("a", 300)) ~= - string.format("%p", string.rep("a", 300))) -- long strings + assert(string.format("%p", io.stdin) ~= null) + assert(string.format("%p", io.stdin) == string.format("%p", io.stdin)) + assert(string.format("%p", print) == string.format("%p", print)) + assert(string.format("%p", print) ~= string.format("%p", assert)) + assert(#string.format("%90p", {}) == 90) + assert(#string.format("%-60p", {}) == 60) + assert(string.format("%10p", false) == string.rep(" ", 10 - #null) .. null) + assert(string.format("%-12p", 1.5) == null .. string.rep(" ", 12 - #null)) + + do + local t1 = {}; local t2 = {} + assert(topointer(t1) ~= topointer(t2)) + end + + do -- short strings are internalized + local s1 = string.rep("a", 10) + local s2 = string.rep("aa", 5) + assert(topointer(s1) == topointer(s2)) + end + + do -- long strings aren't internalized + local s1 = string.rep("a", 300); local s2 = string.rep("a", 300) + assert(topointer(s1) ~= topointer(s2)) + end end -x = '"ílo"\n\\' +local x = '"ílo"\n\\' assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\') assert(string.format('%q', "\0") == [["\0"]]) assert(load(string.format('return %q', x))() == x) @@ -180,13 +211,11 @@ assert(string.format("\0%c\0%c%x\0", string.byte("\xe4"), string.byte("b"), 140) "\0\xe4\0b8c\0") assert(string.format('') == "") assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) == - string.format("%c%c%c%c", 34, 48, 90, 100)) + string.format("%1c%-c%-1c%c", 34, 48, 90, 100)) assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be') assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023") assert(tonumber(string.format("%f", 10.3)) == 10.3) -x = string.format('"%-50s"', 'a') -assert(#x == 52) -assert(string.sub(x, 1, 4) == '"a ') +assert(string.format('"%-50s"', 'a') == '"a' .. string.rep(' ', 49) .. '"') assert(string.format("-%.20s.20s", string.rep("%", 2000)) == "-"..string.rep("%", 20)..".20s") @@ -215,7 +244,6 @@ end assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0") checkerror("contains zeros", string.format, "%10s", "\0") -checkerror("cannot have modifiers", string.format, "%10q", "1") -- format x tostring assert(string.format("%s %s", nil, true) == "nil true") @@ -249,6 +277,12 @@ do -- longest number that can be formatted local s = string.format('%.99f', -(10^i)) assert(string.len(s) >= i + 101) assert(tonumber(s) == -(10^i)) + + -- limit for floats + assert(10^38 < math.huge) + local s = string.format('%.99f', -(10^38)) + assert(string.len(s) >= 38 + 101) + assert(tonumber(s) == -(10^38)) end @@ -294,8 +328,8 @@ do print("testing 'format %a %A'") matchhexa(n) end - assert(string.find(string.format("%A", 0.0), "^0X0%.?0?P%+?0$")) - assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0?p%+?0$")) + assert(string.find(string.format("%A", 0.0), "^0X0%.?0*P%+?0$")) + assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0*p%+?0$")) if not _port then -- test inf, -inf, NaN, and -0.0 assert(string.find(string.format("%a", 1/0), "^inf")) @@ -313,6 +347,26 @@ do print("testing 'format %a %A'") end +-- testing some flags (all these results are required by ISO C) +assert(string.format("%#12o", 10) == " 012") +assert(string.format("%#10x", 100) == " 0x64") +assert(string.format("%#-17X", 100) == "0X64 ") +assert(string.format("%013i", -100) == "-000000000100") +assert(string.format("%2.5d", -100) == "-00100") +assert(string.format("%.u", 0) == "") +assert(string.format("%+#014.0f", 100) == "+000000000100.") +assert(string.format("%-16c", 97) == "a ") +assert(string.format("%+.3G", 1.5) == "+1.5") +assert(string.format("%.0s", "alo") == "") +assert(string.format("%.s", "alo") == "") + +-- ISO C89 says that "The exponent always contains at least two digits", +-- but unlike ISO C99 it does not ensure that it contains "only as many +-- more digits as necessary". +assert(string.match(string.format("% 1.0E", 100), "^ 1E%+0+2$")) +assert(string.match(string.format("% .1g", 2^10), "^ 1e%+0+3$")) + + -- errors in format local function check (fmt, msg) @@ -320,19 +374,30 @@ local function check (fmt, msg) end local aux = string.rep('0', 600) -check("%100.3d", "too long") +check("%100.3d", "invalid conversion") check("%1"..aux..".3d", "too long") -check("%1.100d", "too long") +check("%1.100d", "invalid conversion") check("%10.1"..aux.."004d", "too long") check("%t", "invalid conversion") -check("%"..aux.."d", "repeated flags") +check("%"..aux.."d", "too long") check("%d %d", "no value") +check("%010c", "invalid conversion") +check("%.10c", "invalid conversion") +check("%0.34s", "invalid conversion") +check("%#i", "invalid conversion") +check("%3.1p", "invalid conversion") +check("%0.s", "invalid conversion") +check("%10q", "cannot have modifiers") +check("%F", "invalid conversion") -- useless and not in C89 assert(load("return 1\n--comment without ending EOL")() == 1) checkerror("table expected", table.concat, 3) +checkerror("at index " .. maxi, table.concat, {}, " ", maxi, maxi) +-- '%' escapes following minus signal +checkerror("at index %" .. mini, table.concat, {}, " ", mini, mini) assert(table.concat{} == "") assert(table.concat({}, 'x') == "") assert(table.concat({'\0', '\0\1', '\0\1\2'}, '.\0.') == "\0.\0.\0\1.\0.\0\1\2") @@ -396,9 +461,103 @@ end do local f = string.gmatch("1 2 3 4 5", "%d+") assert(f() == "1") - co = coroutine.wrap(f) + local co = coroutine.wrap(f) assert(co() == "2") end + +if T==nil then + (Message or print) + ("\n >>> testC not active: skipping 'pushfstring' tests <<<\n") +else + + print"testing 'pushfstring'" + + -- formats %U, %f, %I already tested elsewhere + + local blen = 200 -- internal buffer length in 'luaO_pushfstring' + + local function callpfs (op, fmt, n) + local x = {T.testC("pushfstring" .. op .. "; return *", fmt, n)} + -- stack has code, 'fmt', 'n', and result from operation + assert(#x == 4) -- make sure nothing else was left in the stack + return x[4] + end + + local function testpfs (op, fmt, n) + assert(callpfs(op, fmt, n) == string.format(fmt, n)) + end + + testpfs("I", "", 0) + testpfs("I", string.rep("a", blen - 1), 0) + testpfs("I", string.rep("a", blen), 0) + testpfs("I", string.rep("a", blen + 1), 0) + + local str = string.rep("ab", blen) .. "%d" .. string.rep("d", blen / 2) + testpfs("I", str, 2^14) + testpfs("I", str, -2^15) + + str = "%d" .. string.rep("cd", blen) + testpfs("I", str, 2^14) + testpfs("I", str, -2^15) + + str = string.rep("c", blen - 2) .. "%d" + testpfs("I", str, 2^14) + testpfs("I", str, -2^15) + + for l = 12, 14 do + local str1 = string.rep("a", l) + for i = 0, 500, 13 do + for j = 0, 500, 13 do + str = string.rep("a", i) .. "%s" .. string.rep("d", j) + testpfs("S", str, str1) + testpfs("S", str, str) + end + end + end + + str = "abc %c def" + testpfs("I", str, string.byte("A")) + testpfs("I", str, 255) + + str = string.rep("a", blen - 1) .. "%p" .. string.rep("cd", blen) + testpfs("P", str, {}) + + str = string.rep("%%", 3 * blen) .. "%p" .. string.rep("%%", 2 * blen) + testpfs("P", str, {}) +end + +if T == nil then + (Message or print)('\n >>> testC not active: skipping external strings tests <<<\n') +else + print("testing external strings") + local x = T.externKstr("hello") -- external fixed short string + assert(x == "hello") + local x = T.externstr("hello") -- external allocated short string + assert(x == "hello") + x = string.rep("a", 100) -- long string + local y = T.externKstr(x) -- external fixed long string + assert(y == x) + local z = T.externstr(x) -- external allocated long string + assert(z == y) + + local e = T.externstr("") -- empty external string + assert(e .. "x" == "x" and "x" .. e == "x") + assert(e .. e == "" and #e == 0) + + -- external string as the "n" key in vararg table + local n = T.externstr("n") + local n0 = T.externstr("n\0") + local function aux (...t) assert(t[n0] == nil); return t[n] end + assert(aux(10, 20, 30) == 3) + + -- external string as mode in weak table + local t = setmetatable({}, {__mode = T.externstr("kv")}) + t[{}] = {} + assert(next(t)) + collectgarbage() + assert(next(t) == nil) +end + print('OK') diff --git a/testes/tpack.lua b/testes/tpack.lua index 2b9953f831..70386178c4 100644 --- a/testes/tpack.lua +++ b/testes/tpack.lua @@ -1,5 +1,5 @@ -- $Id: testes/tpack.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h local pack = string.pack local packsize = string.packsize @@ -35,7 +35,7 @@ print("\talignment: " .. align) -- check errors in arguments -function checkerror (msg, f, ...) +local function checkerror (msg, f, ...) local status, err = pcall(f, ...) -- print(status, err, msg) assert(not status and string.find(err, msg)) @@ -135,15 +135,15 @@ checkerror("variable%-length format", packsize, "z") -- overflow in option size (error will be in digit after limit) checkerror("invalid format", packsize, "c1" .. string.rep("0", 40)) -if packsize("i") == 4 then - -- result would be 2^31 (2^3 repetitions of 2^28 strings) - local s = string.rep("c268435456", 2^3) - checkerror("too large", packsize, s) - -- one less is OK - s = string.rep("c268435456", 2^3 - 1) .. "c268435455" - assert(packsize(s) == 0x7fffffff) +do + local maxsize = (packsize("j") <= packsize("T")) and + math.maxinteger or (1 << (packsize("T") * 8)) + assert (packsize(string.format("c%d", maxsize - 9)) == maxsize - 9) + checkerror("too large", packsize, string.format("c%dc10", maxsize - 9)) + checkerror("too long", pack, string.format("xxxxxxxxxx c%d", maxsize - 9)) end + -- overflow in packing for i = 1, sizeLI - 1 do local umax = (1 << (i * 8)) - 1 @@ -229,8 +229,9 @@ do assert(pack("c3", "123") == "123") assert(pack("c0", "") == "") assert(pack("c8", "123456") == "123456\0\0") - assert(pack("c88", "") == string.rep("\0", 88)) - assert(pack("c188", "ab") == "ab" .. string.rep("\0", 188 - 2)) + assert(pack("c88 c1", "", "X") == string.rep("\0", 88) .. "X") + assert(pack("c188 c2", "ab", "X\1") == + "ab" .. string.rep("\0", 188 - 2) .. "X\1") local a, b, c = unpack("!4 z c3", "abcdefghi\0xyz") assert(a == "abcdefghi" and b == "xyz" and c == 14) checkerror("longer than", pack, "c3", "1234") diff --git a/testes/tracegc.lua b/testes/tracegc.lua new file mode 100644 index 0000000000..a8c929dffd --- /dev/null +++ b/testes/tracegc.lua @@ -0,0 +1,40 @@ +-- track collections + +local M = {} + +-- import list +local setmetatable, stderr, collectgarbage = + setmetatable, io.stderr, collectgarbage + +global none + +local active = false + + +-- each time a table is collected, remark it for finalization on next +-- cycle +local mt = {} +function mt.__gc (o) + stderr:write'.' -- mark progress + if active then + setmetatable(o, mt) -- remark object for finalization + end +end + + +function M.start () + if not active then + active = true + setmetatable({}, mt) -- create initial object + end +end + + +function M.stop () + if active then + active = false + collectgarbage() -- call finalizer for the last time + end +end + +return M diff --git a/testes/utf8.lua b/testes/utf8.lua index 86ec1b00f1..028995a478 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -1,5 +1,9 @@ -- $Id: testes/utf8.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h + +-- UTF-8 file + +global * print "testing UTF-8 library" @@ -30,8 +34,8 @@ local function checksyntax (s, t) assert(assert(load(ts))() == s) end -assert(utf8.offset("alo", 5) == nil) -assert(utf8.offset("alo", -4) == nil) +assert(not utf8.offset("alo", 5)) +assert(not utf8.offset("alo", -4)) -- 'check' makes several tests over the validity of string 's'. -- 't' is the list of codepoints of 's'. @@ -50,24 +54,34 @@ local function check (s, t, nonstrict) for i = 1, #t do assert(t[i] == t1[i]) end -- 't' is equal to 't1' for i = 1, l do -- for all codepoints - local pi = utf8.offset(s, i) -- position of i-th char + local pi, pie = utf8.offset(s, i) -- position of i-th char local pi1 = utf8.offset(s, 2, pi) -- position of next char + assert(pi1 == pie + 1) assert(string.find(string.sub(s, pi, pi1 - 1), justone)) assert(utf8.offset(s, -1, pi1) == pi) assert(utf8.offset(s, i - l - 1) == pi) assert(pi1 - pi == #utf8.char(utf8.codepoint(s, pi, pi, nonstrict))) for j = pi, pi1 - 1 do - assert(utf8.offset(s, 0, j) == pi) + local off1, off2 = utf8.offset(s, 0, j) + assert(off1 == pi and off2 == pi1 - 1) end for j = pi + 1, pi1 - 1 do assert(not utf8.len(s, j)) end - assert(utf8.len(s, pi, pi, nonstrict) == 1) - assert(utf8.len(s, pi, pi1 - 1, nonstrict) == 1) - assert(utf8.len(s, pi, -1, nonstrict) == l - i + 1) - assert(utf8.len(s, pi1, -1, nonstrict) == l - i) - assert(utf8.len(s, 1, pi, -1, nonstrict) == i) + assert(utf8.len(s, pi, pi, nonstrict) == 1) + assert(utf8.len(s, pi, pi1 - 1, nonstrict) == 1) + assert(utf8.len(s, pi, -1, nonstrict) == l - i + 1) + assert(utf8.len(s, pi1, -1, nonstrict) == l - i) + assert(utf8.len(s, 1, pi, nonstrict) == i) + end + + local expected = 1 -- expected position of "current" character + for i = 1, l + 1 do + local p, e = utf8.offset(s, i) + assert(p == expected) + expected = e + 1 end + assert(expected - 1 == #s + 1) local i = 0 for p, c in utf8.codes(s, nonstrict) do @@ -92,14 +106,20 @@ end do -- error indication in utf8.len - local function check (s, p) + local function checklen (s, p) local a, b = utf8.len(s) assert(not a and b == p) end - check("abc\xE3def", 4) - check("汉字\x80", #("汉字") + 1) - check("\xF4\x9F\xBF", 1) - check("\xF4\x9F\xBF\xBF", 1) + checklen("abc\xE3def", 4) + checklen("\xF4\x9F\xBF", 1) + checklen("\xF4\x9F\xBF\xBF", 1) + -- spurious continuation bytes + checklen("汉字\x80", #("汉字") + 1) + checklen("\x80hello", 1) + checklen("hel\x80lo", 4) + checklen("汉字\xBF", #("汉字") + 1) + checklen("\xBFhello", 1) + checklen("hel\xBFlo", 4) end -- errors in utf8.codes @@ -112,17 +132,39 @@ do end errorcodes("ab\xff") errorcodes("\u{110000}") + errorcodes("in\x80valid") + errorcodes("\xbfinvalid") + errorcodes("αλφ\xBFα") + + -- calling iteration function with invalid arguments + local f = utf8.codes("") + assert(f("", 2) == nil) + assert(f("", -1) == nil) + assert(f("", math.mininteger) == nil) + end -- error in initial position for offset -checkerror("position out of range", utf8.offset, "abc", 1, 5) -checkerror("position out of range", utf8.offset, "abc", 1, -4) -checkerror("position out of range", utf8.offset, "", 1, 2) -checkerror("position out of range", utf8.offset, "", 1, -1) +checkerror("position out of bounds", utf8.offset, "abc", 1, 5) +checkerror("position out of bounds", utf8.offset, "abc", 1, -4) +checkerror("position out of bounds", utf8.offset, "", 1, 2) +checkerror("position out of bounds", utf8.offset, "", 1, -1) checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) checkerror("continuation byte", utf8.offset, "\x80", 1) - +checkerror("continuation byte", utf8.offset, "\x9c", -1) + +-- error in indices for len +checkerror("out of bounds", utf8.len, "abc", 0, 2) +checkerror("out of bounds", utf8.len, "abc", 1, 4) + +do -- missing continuation bytes + -- get what is available + local p, e = utf8.offset("\xE0", 1) + assert(p == 1 and e == 1) + local p, e = utf8.offset("\xE0\x9e", -1) + assert(p == 1 and e == 2) +end local s = "hello World" @@ -137,11 +179,11 @@ do local t = {utf8.codepoint(s,1,#s - 1)} assert(#t == 3 and t[1] == 225 and t[2] == 233 and t[3] == 237) checkerror("invalid UTF%-8 code", utf8.codepoint, s, 1, #s) - checkerror("out of range", utf8.codepoint, s, #s + 1) + checkerror("out of bounds", utf8.codepoint, s, #s + 1) t = {utf8.codepoint(s, 4, 3)} assert(#t == 0) - checkerror("out of range", utf8.codepoint, s, -(#s + 1), 1) - checkerror("out of range", utf8.codepoint, s, 1, #s + 1) + checkerror("out of bounds", utf8.codepoint, s, -(#s + 1), 1) + checkerror("out of bounds", utf8.codepoint, s, 1, #s + 1) -- surrogates assert(utf8.codepoint("\u{D7FF}") == 0xD800 - 1) assert(utf8.codepoint("\u{E000}") == 0xDFFF + 1) @@ -211,7 +253,7 @@ do check(s, {0x10000, 0x1FFFFF}, true) end -x = "日本語a-4\0éó" +local x = "日本語a-4\0éó" check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243}) diff --git a/testes/vararg.lua b/testes/vararg.lua index 44848d254f..043fa7d47a 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua @@ -1,15 +1,18 @@ -- $Id: testes/vararg.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print('testing vararg') -function f(a, ...) +local function f (a, ...t) local x = {n = select('#', ...), ...} - for i = 1, x.n do assert(a[i] == x[i]) end + assert(x.n == t.n) + for i = 1, x.n do + assert(a[i] == x[i] and x[i] == t[i]) + end return x.n end -function c12 (...) +local function c12 (...) assert(arg == _G.arg) -- no local 'arg' local x = {...}; x.n = #x local res = (x.n==2 and x[1] == 1 and x[2] == 2) @@ -17,7 +20,7 @@ function c12 (...) return res, 2 end -function vararg (...) return {n = select('#', ...), ...} end +local function vararg (... t) return t end local call = function (f, args) return f(table.unpack(args, 1, args.n)) end @@ -29,7 +32,7 @@ assert(vararg().n == 0) assert(vararg(nil, nil).n == 2) assert(c12(1,2)==55) -a,b = assert(call(c12, {1,2})) +local a,b = assert(call(c12, {1,2})) assert(a == 55 and b == 2) a = call(c12, {1,2;n=2}) assert(a == 55 and b == 2) @@ -49,7 +52,7 @@ function t:f (...) local arg = {...}; return self[...]+#arg end assert(t:f(1,4) == 3 and t:f(2) == 11) print('+') -lim = 20 +local lim = 20 local i, a = 1, {} while i <= lim do a[i] = i+0.3; i=i+1 end @@ -59,7 +62,7 @@ function f(a, b, c, d, ...) more[lim-4] == lim+0.3 and not more[lim-3]) end -function g(a,b,c) +local function g (a,b,c) assert(a == 1.3 and b == 2.3 and c == 3.3) end @@ -76,7 +79,7 @@ print("+") -- new-style varargs -function oneless (a, ...) return ... end +local function oneless (a, ...) return ... end function f (n, a, ...) local b @@ -98,9 +101,41 @@ a,b,c,d,e = f(4) assert(a==nil and b==nil and c==nil and d==nil and e==nil) +do -- vararg expressions using unpack + local function aux (a, v, ...t) + for k, val in pairs(v) do t[k] = val end + return ... + end + + local t = table.pack(aux(10, {11, [5] = 24}, 1, 2, 3, nil, 4)) + assert(t.n == 5 and t[1] == 11 and t[2] == 2 and t[3] == 3 + and t[4] == nil and t[5] == 24) + + local t = table.pack(aux(nil, {1, [20] = "a", [30] = "b", n = 30})) + assert(t.n == 30 and t[1] == 1 and t[20] == "a" and t[30] == "b") + -- table has only those four elements + assert(next(t, next(t, next(t, next(t, next(t, nil))))) == nil) + + local a, b, c, d = aux(nil, {}, 10, 20, 30) + assert(a == 10 and b == 20 and c == 30 and d == nil) + + local function aux (a, b, n, ...t) t.n = n; return b, ... end + local t = table.pack(aux(10, 1, 10000)) + assert(t.n == 10001 and t[1] == 1 and #t == 1) + + local function checkerr (emsg, f, ...) + local st, msg = pcall(f, ...) + assert(not st and string.find(msg, emsg)) + end + checkerr("no proper 'n'", aux, 1, 1, -1) + checkerr("no proper 'n'", aux, 1, 1, math.maxinteger) + checkerr("no proper 'n'", aux, 1, 1, math.mininteger) + checkerr("no proper 'n'", aux, 1, 1, 1.0) +end + -- varargs for main chunks -f = load[[ return {...} ]] -x = f(2,3) +local f = assert(load[[ return {...} ]]) +local x = f(2,3) assert(x[1] == 2 and x[2] == 3 and x[3] == undef) @@ -147,5 +182,79 @@ do local a, b = g() assert(a == nil and b == 2) end + + +do -- vararg parameter used in nested functions + local function foo (...tab1) + return function (...tab2) + return {tab1, tab2} + end + end + local f = foo(10, 20, 30) + local t = f("a", "b") + assert(t[1].n == 3 and t[1][1] == 10) + assert(t[2].n == 2 and t[2][1] == "a") +end + +do -- vararg parameter is read-only + local st, msg = load("return function (... t) t = 10 end") + assert(string.find(msg, "const variable 't'")) + + local st, msg = load[[ + local function foo (...extra) + return function (...) extra = nil end + end + ]] + assert(string.find(msg, "const variable 'extra'")) +end + + +do -- _ENV as vararg parameter + local st, msg = load[[ + local function aux (... _ENV) + global a + a = 10 + end ]] + assert(string.find(msg, "const variable 'a'")) + + local function aux (..._ENV) + global a; a = 10 + return a + end + assert(aux() == 10) + + local function aux (... _ENV) + global a = 10 + return a + end + assert(aux() == 10) +end + + +do -- access to vararg parameter + local function notab (keys, t, ...v) + for _, k in pairs(keys) do + assert(t[k] == v[k]) + end + assert(t.n == v.n) + return ... + end + + local t = table.pack(10, 20, 30) + local keys = {-1, 0, 1, t.n, t.n + 1, 1.0, 1.1, "n", print, "k", "1"} + notab(keys, t, 10, 20, 30) -- ensure stack space + local m = collectgarbage"count" + notab(keys, t, 10, 20, 30) + -- 'notab' does not create any table/object + assert(m == collectgarbage"count") + + -- writing to the vararg table + local function foo (...t) + t[1] = t[1] + 10 + return t[1] + end + assert(foo(10, 30) == 20) +end + print('OK') diff --git a/testes/verybig.lua b/testes/verybig.lua index 8fb7b13e8a..8163802c1d 100644 --- a/testes/verybig.lua +++ b/testes/verybig.lua @@ -1,5 +1,5 @@ -- $Id: testes/verybig.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print "testing RK" @@ -52,7 +52,7 @@ if _soft then return 10 end print "testing large programs (>64k)" -- template to create a very big test file -prog = [[$ +local prog = [[$ local a,b @@ -85,7 +85,7 @@ function b:xxx (a,b) return a+b end assert(b:xxx(10, 12) == 22) -- pushself with non-constant index b["xxx"] = undef -s = 0; n=0 +local s = 0; local n=0 for a,b in pairs(b) do s=s+b; n=n+1 end -- with 32-bit floats, exact value of 's' depends on summation order assert(81800000.0 < s and s < 81860000 and n == 70001) @@ -93,7 +93,7 @@ assert(81800000.0 < s and s < 81860000 and n == 70001) a = nil; b = nil print'+' -function f(x) b=x end +local function f(x) b=x end a = f{$3$} or 10 @@ -118,7 +118,7 @@ local function sig (x) return (x % 2 == 0) and '' or '-' end -F = { +local F = { function () -- $1$ for i=10,50009 do io.write('a', i, ' = ', sig(i), 5+((i-10)/2), ',\n') @@ -138,14 +138,14 @@ function () -- $3$ end, } -file = os.tmpname() +local file = os.tmpname() io.output(file) for s in string.gmatch(prog, "$([^$]+)") do local n = tonumber(s) if not n then io.write(s) else F[n]() end end io.close() -result = dofile(file) +local result = dofile(file) assert(os.remove(file)) print'OK' return result