Unified · Split
| @@ -9,7 +9,7 @@ For details and rationale, see http://www.lua.org/license.html . · 8 unmodified lines | |||
| 9 | 9 | ||
| 10 | =============================================================================== | 10 | =============================================================================== |
| 11 | 11 | ||
| 12 | Copyright (C) 2003-2004 Tecgraf, PUC-Rio. | 12 | Copyright (C) 1994-2005 Tecgraf, PUC-Rio. |
| 13 | 13 | ||
| 14 | Permission is hereby granted, free of charge, to any person obtaining a copy | 14 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 15 | of this software and associated documentation files (the "Software"), to deal | 15 | of this software and associated documentation files (the "Software"), to deal |
| @@ -1,5 +1,24 @@ | |||
| 1 | This is Lua 5.1 (work). | 1 | This is Lua 5.1 (work). |
| 2 | 2 | ||
| 3 | * Changes from version 5.0 to 5.1 | ||
| 4 | ------------------------------- | ||
| 5 | Language: | ||
| 6 | + new module system. | ||
| 7 | + new semantics for control variables of fors. | ||
| 8 | + new semantics for setn/getn. | ||
| 9 | + new syntax/semantics for varargs. | ||
| 10 | + new long strings. | ||
| 11 | API: | ||
| 12 | + new functions: lua_createtable, lua_get(set)field, lua_push(to)integer. | ||
| 13 | + user supplies memory allocator (lua_open becomes lua_newstate). | ||
| 14 | Implementation: | ||
| 15 | + incremental garbage collection. | ||
| 16 | + debug works over other threads. | ||
| 17 | + better handling of end-of-line in the lexer. | ||
| 18 | + fully reentrant parser (new Lua function `load') | ||
| 19 | + better support for 64-bit machines (special thanks to Mike Pall). | ||
| 20 | + native loadlib support for Mac OS X (thanks to Matthew Cox). | ||
| 21 | |||
| 3 | * Changes from version 4.0 to 5.0 | 22 | * Changes from version 4.0 to 5.0 |
| 4 | ------------------------------- | 23 | ------------------------------- |
| 5 | Language: | 24 | Language: |
| @@ -1,105 +1,105 @@ | |||
| 1 | MANIFEST contents of Lua 5.1 (work4) distribution on Thu Dec 30 12:00:49 BRST 2004 | 1 | MANIFEST contents of Lua 5.1 (work5) distribution on Fri Mar 4 13:32:55 BRST 2005 |
| 2 | lua-5.1-work4 | 2 | lua-5.1-work5 |
| 3 | lua-5.1-work4/COPYRIGHT | 3 | lua-5.1-work5/COPYRIGHT |
| 4 | lua-5.1-work4/HISTORY | 4 | lua-5.1-work5/HISTORY |
| 5 | lua-5.1-work4/INSTALL | 5 | lua-5.1-work5/INSTALL |
| 6 | lua-5.1-work4/MANIFEST | 6 | lua-5.1-work5/MANIFEST |
| 7 | lua-5.1-work4/Makefile | 7 | lua-5.1-work5/Makefile |
| 8 | lua-5.1-work4/README | 8 | lua-5.1-work5/README |
| 9 | lua-5.1-work4/doc | 9 | lua-5.1-work5/doc |
| 10 | lua-5.1-work4/doc/contents.html | 10 | lua-5.1-work5/doc/contents.html |
| 11 | lua-5.1-work4/doc/logo.gif | 11 | lua-5.1-work5/doc/logo.gif |
| 12 | lua-5.1-work4/doc/lua.1 | 12 | lua-5.1-work5/doc/lua.1 |
| 13 | lua-5.1-work4/doc/lua.html | 13 | lua-5.1-work5/doc/lua.html |
| 14 | lua-5.1-work4/doc/luac.1 | 14 | lua-5.1-work5/doc/luac.1 |
| 15 | lua-5.1-work4/doc/luac.html | 15 | lua-5.1-work5/doc/luac.html |
| 16 | lua-5.1-work4/doc/readme.html | 16 | lua-5.1-work5/doc/readme.html |
| 17 | lua-5.1-work4/etc | 17 | lua-5.1-work5/etc |
| 18 | lua-5.1-work4/etc/Makefile | 18 | lua-5.1-work5/etc/Makefile |
| 19 | lua-5.1-work4/etc/README | 19 | lua-5.1-work5/etc/README |
| 20 | lua-5.1-work4/etc/all.c | 20 | lua-5.1-work5/etc/all.c |
| 21 | lua-5.1-work4/etc/lua.hpp | 21 | lua-5.1-work5/etc/lua.hpp |
| 22 | lua-5.1-work4/etc/lua.ico | 22 | lua-5.1-work5/etc/lua.ico |
| 23 | lua-5.1-work4/etc/min.c | 23 | lua-5.1-work5/etc/min.c |
| 24 | lua-5.1-work4/etc/noparser.c | 24 | lua-5.1-work5/etc/noparser.c |
| 25 | lua-5.1-work4/etc/saconfig.c | 25 | lua-5.1-work5/etc/saconfig.c |
| 26 | lua-5.1-work4/src | 26 | lua-5.1-work5/src |
| 27 | lua-5.1-work4/src/Makefile | 27 | lua-5.1-work5/src/Makefile |
| 28 | lua-5.1-work4/src/lapi.c | 28 | lua-5.1-work5/src/lapi.c |
| 29 | lua-5.1-work4/src/lapi.h | 29 | lua-5.1-work5/src/lapi.h |
| 30 | lua-5.1-work4/src/lauxlib.c | 30 | lua-5.1-work5/src/lauxlib.c |
| 31 | lua-5.1-work4/src/lauxlib.h | 31 | lua-5.1-work5/src/lauxlib.h |
| 32 | lua-5.1-work4/src/lbaselib.c | 32 | lua-5.1-work5/src/lbaselib.c |
| 33 | lua-5.1-work4/src/lcode.c | 33 | lua-5.1-work5/src/lcode.c |
| 34 | lua-5.1-work4/src/lcode.h | 34 | lua-5.1-work5/src/lcode.h |
| 35 | lua-5.1-work4/src/ldblib.c | 35 | lua-5.1-work5/src/ldblib.c |
| 36 | lua-5.1-work4/src/ldebug.c | 36 | lua-5.1-work5/src/ldebug.c |
| 37 | lua-5.1-work4/src/ldebug.h | 37 | lua-5.1-work5/src/ldebug.h |
| 38 | lua-5.1-work4/src/ldo.c | 38 | lua-5.1-work5/src/ldo.c |
| 39 | lua-5.1-work4/src/ldo.h | 39 | lua-5.1-work5/src/ldo.h |
| 40 | lua-5.1-work4/src/ldump.c | 40 | lua-5.1-work5/src/ldump.c |
| 41 | lua-5.1-work4/src/lfunc.c | 41 | lua-5.1-work5/src/lfunc.c |
| 42 | lua-5.1-work4/src/lfunc.h | 42 | lua-5.1-work5/src/lfunc.h |
| 43 | lua-5.1-work4/src/lgc.c | 43 | lua-5.1-work5/src/lgc.c |
| 44 | lua-5.1-work4/src/lgc.h | 44 | lua-5.1-work5/src/lgc.h |
| 45 | lua-5.1-work4/src/linit.c | 45 | lua-5.1-work5/src/linit.c |
| 46 | lua-5.1-work4/src/liolib.c | 46 | lua-5.1-work5/src/liolib.c |
| 47 | lua-5.1-work4/src/llex.c | 47 | lua-5.1-work5/src/llex.c |
| 48 | lua-5.1-work4/src/llex.h | 48 | lua-5.1-work5/src/llex.h |
| 49 | lua-5.1-work4/src/llimits.h | 49 | lua-5.1-work5/src/llimits.h |
| 50 | lua-5.1-work4/src/lmathlib.c | 50 | lua-5.1-work5/src/lmathlib.c |
| 51 | lua-5.1-work4/src/lmem.c | 51 | lua-5.1-work5/src/lmem.c |
| 52 | lua-5.1-work4/src/lmem.h | 52 | lua-5.1-work5/src/lmem.h |
| 53 | lua-5.1-work4/src/loadlib.c | 53 | lua-5.1-work5/src/loadlib.c |
| 54 | lua-5.1-work4/src/lobject.c | 54 | lua-5.1-work5/src/lobject.c |
| 55 | lua-5.1-work4/src/lobject.h | 55 | lua-5.1-work5/src/lobject.h |
| 56 | lua-5.1-work4/src/lopcodes.c | 56 | lua-5.1-work5/src/lopcodes.c |
| 57 | lua-5.1-work4/src/lopcodes.h | 57 | lua-5.1-work5/src/lopcodes.h |
| 58 | lua-5.1-work4/src/loslib.c | 58 | lua-5.1-work5/src/loslib.c |
| 59 | lua-5.1-work4/src/lparser.c | 59 | lua-5.1-work5/src/lparser.c |
| 60 | lua-5.1-work4/src/lparser.h | 60 | lua-5.1-work5/src/lparser.h |
| 61 | lua-5.1-work4/src/lstate.c | 61 | lua-5.1-work5/src/lstate.c |
| 62 | lua-5.1-work4/src/lstate.h | 62 | lua-5.1-work5/src/lstate.h |
| 63 | lua-5.1-work4/src/lstring.c | 63 | lua-5.1-work5/src/lstring.c |
| 64 | lua-5.1-work4/src/lstring.h | 64 | lua-5.1-work5/src/lstring.h |
| 65 | lua-5.1-work4/src/lstrlib.c | 65 | lua-5.1-work5/src/lstrlib.c |
| 66 | lua-5.1-work4/src/ltable.c | 66 | lua-5.1-work5/src/ltable.c |
| 67 | lua-5.1-work4/src/ltable.h | 67 | lua-5.1-work5/src/ltable.h |
| 68 | lua-5.1-work4/src/ltablib.c | 68 | lua-5.1-work5/src/ltablib.c |
| 69 | lua-5.1-work4/src/ltm.c | 69 | lua-5.1-work5/src/ltm.c |
| 70 | lua-5.1-work4/src/ltm.h | 70 | lua-5.1-work5/src/ltm.h |
| 71 | lua-5.1-work4/src/lua.c | 71 | lua-5.1-work5/src/lua.c |
| 72 | lua-5.1-work4/src/lua.h | 72 | lua-5.1-work5/src/lua.h |
| 73 | lua-5.1-work4/src/luac.c | 73 | lua-5.1-work5/src/luac.c |
| 74 | lua-5.1-work4/src/luaconf.h | 74 | lua-5.1-work5/src/luaconf.h |
| 75 | lua-5.1-work4/src/lualib.h | 75 | lua-5.1-work5/src/lualib.h |
| 76 | lua-5.1-work4/src/lundump.c | 76 | lua-5.1-work5/src/lundump.c |
| 77 | lua-5.1-work4/src/lundump.h | 77 | lua-5.1-work5/src/lundump.h |
| 78 | lua-5.1-work4/src/lvm.c | 78 | lua-5.1-work5/src/lvm.c |
| 79 | lua-5.1-work4/src/lvm.h | 79 | lua-5.1-work5/src/lvm.h |
| 80 | lua-5.1-work4/src/lzio.c | 80 | lua-5.1-work5/src/lzio.c |
| 81 | lua-5.1-work4/src/lzio.h | 81 | lua-5.1-work5/src/lzio.h |
| 82 | lua-5.1-work4/src/print.c | 82 | lua-5.1-work5/src/print.c |
| 83 | lua-5.1-work4/test | 83 | lua-5.1-work5/test |
| 84 | lua-5.1-work4/test/README | 84 | lua-5.1-work5/test/README |
| 85 | lua-5.1-work4/test/bisect.lua | 85 | lua-5.1-work5/test/bisect.lua |
| 86 | lua-5.1-work4/test/cf.lua | 86 | lua-5.1-work5/test/cf.lua |
| 87 | lua-5.1-work4/test/echo.lua | 87 | lua-5.1-work5/test/echo.lua |
| 88 | lua-5.1-work4/test/env.lua | 88 | lua-5.1-work5/test/env.lua |
| 89 | lua-5.1-work4/test/factorial.lua | 89 | lua-5.1-work5/test/factorial.lua |
| 90 | lua-5.1-work4/test/fib.lua | 90 | lua-5.1-work5/test/fib.lua |
| 91 | lua-5.1-work4/test/fibfor.lua | 91 | lua-5.1-work5/test/fibfor.lua |
| 92 | lua-5.1-work4/test/globals.lua | 92 | lua-5.1-work5/test/globals.lua |
| 93 | lua-5.1-work4/test/hello.lua | 93 | lua-5.1-work5/test/hello.lua |
| 94 | lua-5.1-work4/test/life.lua | 94 | lua-5.1-work5/test/life.lua |
| 95 | lua-5.1-work4/test/luac.lua | 95 | lua-5.1-work5/test/luac.lua |
| 96 | lua-5.1-work4/test/printf.lua | 96 | lua-5.1-work5/test/printf.lua |
| 97 | lua-5.1-work4/test/readonly.lua | 97 | lua-5.1-work5/test/readonly.lua |
| 98 | lua-5.1-work4/test/sieve.lua | 98 | lua-5.1-work5/test/sieve.lua |
| 99 | lua-5.1-work4/test/sort.lua | 99 | lua-5.1-work5/test/sort.lua |
| 100 | lua-5.1-work4/test/table.lua | 100 | lua-5.1-work5/test/table.lua |
| 101 | lua-5.1-work4/test/trace-calls.lua | 101 | lua-5.1-work5/test/trace-calls.lua |
| 102 | lua-5.1-work4/test/trace-globals.lua | 102 | lua-5.1-work5/test/trace-globals.lua |
| 103 | lua-5.1-work4/test/undefined.lua | 103 | lua-5.1-work5/test/undefined.lua |
| 104 | lua-5.1-work4/test/xd.lua | 104 | lua-5.1-work5/test/xd.lua |
| 105 | END OF MANIFEST | 105 | END OF MANIFEST |
| @@ -44,7 +44,7 @@ install: all · 43 unmodified lines | |||
| 44 | cd doc; $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN) | 44 | cd doc; $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN) |
| 45 | 45 | ||
| 46 | local: | 46 | local: |
| 47 | $(MAKE) install INSTALL_TOP=.. INSTALL_EXEC="cp -up" INSTALL_DATA="cp -up" | 47 | $(MAKE) install INSTALL_TOP=.. INSTALL_EXEC="cp -p" INSTALL_DATA="cp -p" |
| 48 | 48 | ||
| 49 | # echo config parameters | 49 | # echo config parameters |
| 50 | echo: | 50 | echo: |
| @@ -6,13 +6,15 @@ · 5 unmodified lines | |||
| 6 | 6 | ||
| 7 | CC= gcc | 7 | CC= gcc |
| 8 | CFLAGS= -O2 -Wall $(MYCFLAGS) | 8 | CFLAGS= -O2 -Wall $(MYCFLAGS) |
| 9 | MYCFLAGS= | ||
| 10 | MYLDFLAGS= -Wl,-E # enable dynamic loading in Linux | ||
| 11 | MYLIBS= -lm -ldl # enable dynamic loading in Linux | ||
| 12 | AR= ar rcu | 9 | AR= ar rcu |
| 13 | RANLIB= ranlib | 10 | RANLIB= ranlib |
| 14 | RM= rm -f | 11 | RM= rm -f |
| 15 | 12 | ||
| 13 | MYCFLAGS= | ||
| 14 | MYLDFLAGS= | ||
| 15 | MYLIBS= -lm | ||
| 16 | DL= -ldl -Wl,-E # enable dynamic loading in Linux | ||
| 17 | |||
| 16 | # == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE ========= | 18 | # == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE ========= |
| 17 | 19 | ||
| 18 | CORE_T= liblua.a | 20 | CORE_T= liblua.a |
| @@ -49,10 +51,10 @@ $(LIB_T): $(LIB_O) · 30 unmodified lines | |||
| 49 | $(RANLIB) $@ | 51 | $(RANLIB) $@ |
| 50 | 52 | ||
| 51 | $(LUA_T): $(LUA_O) $(CORE_T) $(LIB_T) | 53 | $(LUA_T): $(LUA_O) $(CORE_T) $(LIB_T) |
| 52 | $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) -L. -llua -llualib $(MYLIBS) | 54 | $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) -L. -llua -llualib $(MYLIBS) $(DL) |
| 53 | 55 | ||
| 54 | $(LUAC_T): $(LUAC_O) $(CORE_T) | 56 | $(LUAC_T): $(LUAC_O) $(CORE_T) |
| 55 | $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) -L. -llua | 57 | $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) -L. -llua $(MYLIBS) |
| 56 | 58 | ||
| 57 | clean: | 59 | clean: |
| 58 | $(RM) $(ALL_T) $(ALL_O) | 60 | $(RM) $(ALL_T) $(ALL_O) |
| @@ -63,12 +65,13 @@ depend: · 4 unmodified lines | |||
| 63 | echo: | 65 | echo: |
| 64 | @echo "CC = $(CC)" | 66 | @echo "CC = $(CC)" |
| 65 | @echo "CFLAGS = $(CFLAGS)" | 67 | @echo "CFLAGS = $(CFLAGS)" |
| 66 | @echo "MYCFLAGS = $(MYCFLAGS)" | ||
| 67 | @echo "MYLDFLAGS = $(MYLDFLAGS)" | ||
| 68 | @echo "MYLIBS = $(MYLIBS)" | ||
| 69 | @echo "AR = $(AR)" | 68 | @echo "AR = $(AR)" |
| 70 | @echo "RANLIB = $(RANLIB)" | 69 | @echo "RANLIB = $(RANLIB)" |
| 71 | @echo "RM = $(RM)" | 70 | @echo "RM = $(RM)" |
| 71 | @echo "MYCFLAGS = $(MYCFLAGS)" | ||
| 72 | @echo "MYLDFLAGS = $(MYLDFLAGS)" | ||
| 73 | @echo "MYLIBS = $(MYLIBS)" | ||
| 74 | @echo "DL = $(DL)" | ||
| 72 | 75 | ||
| 73 | # DO NOT DELETE | 76 | # DO NOT DELETE |
| 74 | 77 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 2.23 2004/12/13 12:15:11 roberto Exp $ | 2 | ** $Id: lapi.c,v 2.28 2005/02/23 17:30:22 roberto Exp $ |
| 3 | ** Lua API | 3 | ** Lua API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -58,6 +58,11 @@ static TValue *index2adr (lua_State *L, int idx) { · 52 unmodified lines | |||
| 58 | } | 58 | } |
| 59 | else switch (idx) { /* pseudo-indices */ | 59 | else switch (idx) { /* pseudo-indices */ |
| 60 | case LUA_REGISTRYINDEX: return registry(L); | 60 | case LUA_REGISTRYINDEX: return registry(L); |
| 61 | case LUA_ENVIRONINDEX: { | ||
| 62 | Closure *func = curr_func(L); | ||
| 63 | sethvalue(L, &L->env, func->c.env); | ||
| 64 | return &L->env; | ||
| 65 | } | ||
| 61 | case LUA_GLOBALSINDEX: return gt(L); | 66 | case LUA_GLOBALSINDEX: return gt(L); |
| 62 | default: { | 67 | default: { |
| 63 | Closure *func = curr_func(L); | 68 | Closure *func = curr_func(L); |
| @@ -70,6 +75,16 @@ static TValue *index2adr (lua_State *L, int idx) { · 6 unmodified lines | |||
| 70 | } | 75 | } |
| 71 | 76 | ||
| 72 | 77 | ||
| 78 | static Table *getcurrenv (lua_State *L) { | ||
| 79 | if (L->ci == L->base_ci) /* no enclosing function? */ | ||
| 80 | return hvalue(gt(L)); /* use global table as environment */ | ||
| 81 | else { | ||
| 82 | Closure *func = curr_func(L); | ||
| 83 | return func->c.env; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 73 | void luaA_pushobject (lua_State *L, const TValue *o) { | 88 | void luaA_pushobject (lua_State *L, const TValue *o) { |
| 74 | setobj2s(L, L->top, o); | 89 | setobj2s(L, L->top, o); |
| 75 | incr_top(L); | 90 | incr_top(L); |
| @@ -186,9 +201,17 @@ LUA_API void lua_replace (lua_State *L, int idx) { · 110 unmodified lines | |||
| 186 | api_checknelems(L, 1); | 201 | api_checknelems(L, 1); |
| 187 | o = index2adr(L, idx); | 202 | o = index2adr(L, idx); |
| 188 | api_checkvalidindex(L, o); | 203 | api_checkvalidindex(L, o); |
| 189 | setobj(L, o, L->top - 1); | 204 | if (idx == LUA_ENVIRONINDEX) { |
| 190 | if (idx < LUA_GLOBALSINDEX) /* function upvalue? */ | 205 | Closure *func = curr_func(L); |
| 191 | luaC_barrier(L, curr_func(L), L->top - 1); | 206 | api_check(L, ttistable(L->top - 1)); |
| 207 | func->c.env = hvalue(L->top - 1); | ||
| 208 | luaC_barrier(L, func, L->top - 1); | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | setobj(L, o, L->top - 1); | ||
| 212 | if (idx < LUA_GLOBALSINDEX) /* function upvalue? */ | ||
| 213 | luaC_barrier(L, curr_func(L), L->top - 1); | ||
| 214 | } | ||
| 192 | L->top--; | 215 | L->top--; |
| 193 | lua_unlock(L); | 216 | lua_unlock(L); |
| 194 | } | 217 | } |
| @@ -452,7 +475,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { · 257 unmodified lines | |||
| 452 | lua_lock(L); | 475 | lua_lock(L); |
| 453 | luaC_checkGC(L); | 476 | luaC_checkGC(L); |
| 454 | api_checknelems(L, n); | 477 | api_checknelems(L, n); |
| 455 | cl = luaF_newCclosure(L, n); | 478 | cl = luaF_newCclosure(L, n, getcurrenv(L)); |
| 456 | cl->c.f = fn; | 479 | cl->c.f = fn; |
| 457 | L->top -= n; | 480 | L->top -= n; |
| 458 | while (n--) | 481 | while (n--) |
| @@ -542,7 +565,7 @@ LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { · 83 unmodified lines | |||
| 542 | LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { | 565 | LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { |
| 543 | lua_lock(L); | 566 | lua_lock(L); |
| 544 | luaC_checkGC(L); | 567 | luaC_checkGC(L); |
| 545 | sethvalue(L, L->top, luaH_new(L, narray, luaO_log2(nrec) + 1)); | 568 | sethvalue(L, L->top, luaH_new(L, narray, nrec)); |
| 546 | api_incr_top(L); | 569 | api_incr_top(L); |
| 547 | lua_unlock(L); | 570 | lua_unlock(L); |
| 548 | } | 571 | } |
| @@ -579,7 +602,17 @@ LUA_API void lua_getfenv (lua_State *L, int idx) { · 30 unmodified lines | |||
| 579 | lua_lock(L); | 602 | lua_lock(L); |
| 580 | o = index2adr(L, idx); | 603 | o = index2adr(L, idx); |
| 581 | api_checkvalidindex(L, o); | 604 | api_checkvalidindex(L, o); |
| 582 | setobj2s(L, L->top, isLfunction(o) ? &clvalue(o)->l.g : gt(L)); | 605 | switch (ttype(o)) { |
| 606 | case LUA_TFUNCTION: | ||
| 607 | sethvalue(L, L->top, clvalue(o)->c.env); | ||
| 608 | break; | ||
| 609 | case LUA_TUSERDATA: | ||
| 610 | sethvalue(L, L->top, uvalue(o)->env); | ||
| 611 | break; | ||
| 612 | default: | ||
| 613 | setnilvalue(L->top); | ||
| 614 | break; | ||
| 615 | } | ||
| 583 | api_incr_top(L); | 616 | api_incr_top(L); |
| 584 | lua_unlock(L); | 617 | lua_unlock(L); |
| 585 | } | 618 | } |
| @@ -682,17 +715,24 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { · 96 unmodified lines | |||
| 682 | 715 | ||
| 683 | LUA_API int lua_setfenv (lua_State *L, int idx) { | 716 | LUA_API int lua_setfenv (lua_State *L, int idx) { |
| 684 | StkId o; | 717 | StkId o; |
| 685 | int res = 0; | 718 | int res = 1; |
| 686 | lua_lock(L); | 719 | lua_lock(L); |
| 687 | api_checknelems(L, 1); | 720 | api_checknelems(L, 1); |
| 688 | o = index2adr(L, idx); | 721 | o = index2adr(L, idx); |
| 689 | api_checkvalidindex(L, o); | 722 | api_checkvalidindex(L, o); |
| 690 | api_check(L, ttistable(L->top - 1)); | 723 | api_check(L, ttistable(L->top - 1)); |
| 691 | if (isLfunction(o)) { | 724 | switch (ttype(o)) { |
| 692 | res = 1; | 725 | case LUA_TFUNCTION: |
| 693 | clvalue(o)->l.g = *(L->top - 1); | 726 | clvalue(o)->c.env = hvalue(L->top - 1); |
| 694 | luaC_objbarrier(L, clvalue(o), hvalue(L->top - 1)); | 727 | break; |
| 728 | case LUA_TUSERDATA: | ||
| 729 | uvalue(o)->env = hvalue(L->top - 1); | ||
| 730 | break; | ||
| 731 | default: | ||
| 732 | res = 0; | ||
| 733 | break; | ||
| 695 | } | 734 | } |
| 735 | luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); | ||
| 696 | L->top--; | 736 | L->top--; |
| 697 | lua_unlock(L); | 737 | lua_unlock(L); |
| 698 | return res; | 738 | return res; |
| @@ -776,7 +816,7 @@ struct CCallS { /* data to `f_Ccall' */ · 77 unmodified lines | |||
| 776 | static void f_Ccall (lua_State *L, void *ud) { | 816 | static void f_Ccall (lua_State *L, void *ud) { |
| 777 | struct CCallS *c = cast(struct CCallS *, ud); | 817 | struct CCallS *c = cast(struct CCallS *, ud); |
| 778 | Closure *cl; | 818 | Closure *cl; |
| 779 | cl = luaF_newCclosure(L, 0); | 819 | cl = luaF_newCclosure(L, 0, getcurrenv(L)); |
| 780 | cl->c.f = c->func; | 820 | cl->c.f = c->func; |
| 781 | setclvalue(L, L->top, cl); /* push function */ | 821 | setclvalue(L, L->top, cl); /* push function */ |
| 782 | incr_top(L); | 822 | incr_top(L); |
| @@ -826,7 +866,7 @@ LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data) { · 43 unmodified lines | |||
| 826 | } | 866 | } |
| 827 | 867 | ||
| 828 | 868 | ||
| 829 | LUA_API int lua_threadstatus (lua_State *L) { | 869 | LUA_API int lua_status (lua_State *L) { |
| 830 | return L->status; | 870 | return L->status; |
| 831 | } | 871 | } |
| 832 | 872 | ||
| @@ -872,9 +912,9 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { · 39 unmodified lines | |||
| 872 | g->gcpace = data; | 912 | g->gcpace = data; |
| 873 | break; | 913 | break; |
| 874 | } | 914 | } |
| 875 | case LUA_GCSETINCMODE: { | 915 | case LUA_GCSETSTEPMUL: { |
| 876 | res = g->incgc; | 916 | res = g->gcstepmul; |
| 877 | g->incgc = data; | 917 | g->gcstepmul = data; |
| 878 | break; | 918 | break; |
| 879 | } | 919 | } |
| 880 | default: res = -1; /* invalid option */ | 920 | default: res = -1; /* invalid option */ |
| @@ -890,11 +930,6 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { · 9 unmodified lines | |||
| 890 | */ | 930 | */ |
| 891 | 931 | ||
| 892 | 932 | ||
| 893 | LUA_API const char *lua_version (void) { | ||
| 894 | return LUA_VERSION; | ||
| 895 | } | ||
| 896 | |||
| 897 | |||
| 898 | LUA_API int lua_error (lua_State *L) { | 933 | LUA_API int lua_error (lua_State *L) { |
| 899 | lua_lock(L); | 934 | lua_lock(L); |
| 900 | api_checknelems(L, 1); | 935 | api_checknelems(L, 1); |
| @@ -940,7 +975,7 @@ LUA_API void lua_concat (lua_State *L, int n) { · 39 unmodified lines | |||
| 940 | 975 | ||
| 941 | LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { | 976 | LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { |
| 942 | *ud = G(L)->ud; | 977 | *ud = G(L)->ud; |
| 943 | return G(L)->realloc; | 978 | return G(L)->frealloc; |
| 944 | } | 979 | } |
| 945 | 980 | ||
| 946 | 981 | ||
| @@ -948,7 +983,7 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { · 1 unmodified line | |||
| 948 | Udata *u; | 983 | Udata *u; |
| 949 | lua_lock(L); | 984 | lua_lock(L); |
| 950 | luaC_checkGC(L); | 985 | luaC_checkGC(L); |
| 951 | u = luaS_newudata(L, size); | 986 | u = luaS_newudata(L, size, getcurrenv(L)); |
| 952 | setuvalue(L, L->top, u); | 987 | setuvalue(L, L->top, u); |
| 953 | api_incr_top(L); | 988 | api_incr_top(L); |
| 954 | lua_unlock(L); | 989 | lua_unlock(L); |
| @@ -958,7 +993,7 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { · 3 unmodified lines | |||
| 958 | 993 | ||
| 959 | 994 | ||
| 960 | 995 | ||
| 961 | static const char *aux_upvalue (lua_State *L, StkId fi, int n, TValue **val) { | 996 | static const char *aux_upvalue (StkId fi, int n, TValue **val) { |
| 962 | Closure *f; | 997 | Closure *f; |
| 963 | if (!ttisfunction(fi)) return NULL; | 998 | if (!ttisfunction(fi)) return NULL; |
| 964 | f = clvalue(fi); | 999 | f = clvalue(fi); |
| @@ -980,7 +1015,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { · 15 unmodified lines | |||
| 980 | const char *name; | 1015 | const char *name; |
| 981 | TValue *val; | 1016 | TValue *val; |
| 982 | lua_lock(L); | 1017 | lua_lock(L); |
| 983 | name = aux_upvalue(L, index2adr(L, funcindex), n, &val); | 1018 | name = aux_upvalue(index2adr(L, funcindex), n, &val); |
| 984 | if (name) { | 1019 | if (name) { |
| 985 | setobj2s(L, L->top, val); | 1020 | setobj2s(L, L->top, val); |
| 986 | api_incr_top(L); | 1021 | api_incr_top(L); |
| @@ -997,7 +1032,7 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { · 10 unmodified lines | |||
| 997 | lua_lock(L); | 1032 | lua_lock(L); |
| 998 | fi = index2adr(L, funcindex); | 1033 | fi = index2adr(L, funcindex); |
| 999 | api_checknelems(L, 1); | 1034 | api_checknelems(L, 1); |
| 1000 | name = aux_upvalue(L, fi, n, &val); | 1035 | name = aux_upvalue(fi, n, &val); |
| 1001 | if (name) { | 1036 | if (name) { |
| 1002 | L->top--; | 1037 | L->top--; |
| 1003 | setobj(L, val, L->top); | 1038 | setobj(L, val, L->top); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.127 2004/12/20 13:47:29 roberto Exp $ | 2 | ** $Id: lauxlib.c,v 1.129 2005/02/23 17:30:22 roberto Exp $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -350,8 +350,8 @@ LUALIB_API int luaL_getn (lua_State *L, int t) { · 344 unmodified lines | |||
| 350 | static const char *pushnexttemplate (lua_State *L, const char *path) { | 350 | static const char *pushnexttemplate (lua_State *L, const char *path) { |
| 351 | const char *l; | 351 | const char *l; |
| 352 | if (*path == '\0') return NULL; /* no more templates */ | 352 | if (*path == '\0') return NULL; /* no more templates */ |
| 353 | if (*path == LUA_PATH_SEP) path++; /* skip separator */ | 353 | if (*path == LUA_PATHSEP) path++; /* skip separator */ |
| 354 | l = strchr(path, LUA_PATH_SEP); /* find next separator */ | 354 | l = strchr(path, LUA_PATHSEP); /* find next separator */ |
| 355 | if (l == NULL) l = path+strlen(path); | 355 | if (l == NULL) l = path+strlen(path); |
| 356 | lua_pushlstring(L, path, l - path); /* template */ | 356 | lua_pushlstring(L, path, l - path); /* template */ |
| 357 | return l; | 357 | return l; |
| @@ -704,6 +704,7 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { · 346 unmodified lines | |||
| 704 | 704 | ||
| 705 | 705 | ||
| 706 | static int panic (lua_State *L) { | 706 | static int panic (lua_State *L) { |
| 707 | (void)L; /* to avoid warnings */ | ||
| 707 | fprintf(stderr, "PANIC: unprotected error during Lua-API call\n"); | 708 | fprintf(stderr, "PANIC: unprotected error during Lua-API call\n"); |
| 708 | return 0; | 709 | return 0; |
| 709 | } | 710 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.h,v 1.73 2004/10/18 12:51:44 roberto Exp $ | 2 | ** $Id: lauxlib.h,v 1.74 2005/01/10 17:31:50 roberto Exp $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -25,55 +25,58 @@ typedef struct luaL_reg { · 19 unmodified lines | |||
| 25 | } luaL_reg; | 25 | } luaL_reg; |
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | LUALIB_API void luaL_openlib (lua_State *L, const char *libname, | 28 | LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, |
| 29 | const luaL_reg *l, int nup); | 29 | const luaL_reg *l, int nup); |
| 30 | LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *e); | 30 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); |
| 31 | LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *e); | 31 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); |
| 32 | LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname); | 32 | LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); |
| 33 | LUALIB_API int luaL_argerror (lua_State *L, int numarg, const char *extramsg); | 33 | LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); |
| 34 | LUALIB_API const char *luaL_checklstring (lua_State *L, int numArg, size_t *l); | 34 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, |
| 35 | LUALIB_API const char *luaL_optlstring (lua_State *L, int numArg, | 35 | size_t *l); |
| 36 | const char *def, size_t *l); | 36 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, |
| 37 | LUALIB_API lua_Number luaL_checknumber (lua_State *L, int numArg); | 37 | const char *def, size_t *l); |
| 38 | LUALIB_API lua_Number luaL_optnumber (lua_State *L, int nArg, lua_Number def); | 38 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); |
| 39 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); | ||
| 39 | 40 | ||
| 40 | LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int numArg); | 41 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); |
| 41 | LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int nArg, | 42 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, |
| 42 | lua_Integer def); | 43 | lua_Integer def); |
| 43 | 44 | ||
| 44 | LUALIB_API void luaL_checkstack (lua_State *L, int sz, const char *msg); | 45 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); |
| 45 | LUALIB_API void luaL_checktype (lua_State *L, int narg, int t); | 46 | LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); |
| 46 | LUALIB_API void luaL_checkany (lua_State *L, int narg); | 47 | LUALIB_API void (luaL_checkany) (lua_State *L, int narg); |
| 47 | 48 | ||
| 48 | LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname); | 49 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); |
| 49 | LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname); | 50 | LUALIB_API void (luaL_getmetatable) (lua_State *L, const char *tname); |
| 50 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname); | 51 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); |
| 51 | 52 | ||
| 52 | LUALIB_API void luaL_where (lua_State *L, int lvl); | 53 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); |
| 53 | LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...); | 54 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); |
| 54 | 55 | ||
| 55 | LUALIB_API int luaL_findstring (const char *st, const char *const lst[]); | 56 | LUALIB_API int (luaL_findstring) (const char *st, const char *const lst[]); |
| 56 | 57 | ||
| 57 | LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, | 58 | LUALIB_API const char *(luaL_searchpath) (lua_State *L, const char *name, |
| 58 | const char *path); | 59 | const char *path); |
| 59 | 60 | ||
| 60 | LUALIB_API int luaL_ref (lua_State *L, int t); | 61 | LUALIB_API int (luaL_ref) (lua_State *L, int t); |
| 61 | LUALIB_API void luaL_unref (lua_State *L, int t, int ref); | 62 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); |
| 62 | 63 | ||
| 63 | LUALIB_API int luaL_getn (lua_State *L, int t); | 64 | LUALIB_API int (luaL_getn) (lua_State *L, int t); |
| 64 | LUALIB_API void luaL_setn (lua_State *L, int t, int n); | 65 | LUALIB_API void (luaL_setn) (lua_State *L, int t, int n); |
| 65 | 66 | ||
| 66 | LUALIB_API int luaL_loadfile (lua_State *L, const char *filename); | 67 | LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); |
| 67 | LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t sz, | 68 | LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, |
| 68 | const char *name); | 69 | const char *name); |
| 69 | 70 | ||
| 70 | LUALIB_API lua_State *(luaL_newstate) (void); | 71 | LUALIB_API lua_State *(luaL_newstate) (void); |
| 71 | 72 | ||
| 72 | 73 | ||
| 73 | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, | 74 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, |
| 74 | const char *r); | 75 | const char *r); |
| 75 | LUALIB_API const char *luaL_getfield (lua_State *L, int idx, const char *fname); | 76 | LUALIB_API const char *(luaL_getfield) (lua_State *L, int idx, |
| 76 | LUALIB_API const char *luaL_setfield (lua_State *L, int idx, const char *fname); | 77 | const char *fname); |
| 78 | LUALIB_API const char *(luaL_setfield) (lua_State *L, int idx, | ||
| 79 | const char *fname); | ||
| 77 | 80 | ||
| 78 | 81 | ||
| 79 | 82 | ||
| @@ -115,12 +118,12 @@ typedef struct luaL_Buffer { · 35 unmodified lines | |||
| 115 | 118 | ||
| 116 | #define luaL_addsize(B,n) ((B)->p += (n)) | 119 | #define luaL_addsize(B,n) ((B)->p += (n)) |
| 117 | 120 | ||
| 118 | LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B); | 121 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); |
| 119 | LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B); | 122 | LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); |
| 120 | LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l); | 123 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); |
| 121 | LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s); | 124 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); |
| 122 | LUALIB_API void luaL_addvalue (luaL_Buffer *B); | 125 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); |
| 123 | LUALIB_API void luaL_pushresult (luaL_Buffer *B); | 126 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); |
| 124 | 127 | ||
| 125 | 128 | ||
| 126 | /* }====================================================== */ | 129 | /* }====================================================== */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.163 2004/12/13 12:15:11 roberto Exp $ | 2 | ** $Id: lbaselib.c,v 1.169 2005/02/28 17:24:41 roberto Exp $ |
| 3 | ** Basic library | 3 | ** Basic library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -131,7 +131,10 @@ static void getfunc (lua_State *L) { · 125 unmodified lines | |||
| 131 | 131 | ||
| 132 | static int luaB_getfenv (lua_State *L) { | 132 | static int luaB_getfenv (lua_State *L) { |
| 133 | getfunc(L); | 133 | getfunc(L); |
| 134 | lua_getfenv(L, -1); | 134 | if (lua_iscfunction(L, -1)) /* is a C function? */ |
| 135 | lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the global env. */ | ||
| 136 | else | ||
| 137 | lua_getfenv(L, -1); | ||
| 135 | return 1; | 138 | return 1; |
| 136 | } | 139 | } |
| 137 | 140 | ||
| @@ -144,8 +147,8 @@ static int luaB_setfenv (lua_State *L) { · 6 unmodified lines | |||
| 144 | lua_replace(L, LUA_GLOBALSINDEX); | 147 | lua_replace(L, LUA_GLOBALSINDEX); |
| 145 | return 0; | 148 | return 0; |
| 146 | } | 149 | } |
| 147 | else if (lua_setfenv(L, -2) == 0) | 150 | else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0) |
| 148 | luaL_error(L, "`setfenv' cannot change environment of given function"); | 151 | luaL_error(L, "`setfenv' cannot change environment of given object"); |
| 149 | return 1; | 152 | return 1; |
| 150 | } | 153 | } |
| 151 | 154 | ||
| @@ -182,11 +185,11 @@ static int luaB_gcinfo (lua_State *L) { · 30 unmodified lines | |||
| 182 | 185 | ||
| 183 | static int luaB_collectgarbage (lua_State *L) { | 186 | static int luaB_collectgarbage (lua_State *L) { |
| 184 | static const char *const opts[] = {"stop", "restart", "collect", | 187 | static const char *const opts[] = {"stop", "restart", "collect", |
| 185 | "count", "step", "setpace", "setincmode", NULL}; | 188 | "count", "step", "setpace", "setstepmul", NULL}; |
| 186 | static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, | 189 | static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, |
| 187 | LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPACE, LUA_GCSETINCMODE}; | 190 | LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPACE, LUA_GCSETSTEPMUL}; |
| 188 | int o = luaL_findstring(luaL_optstring(L, 1, "collect"), opts); | 191 | int o = luaL_findstring(luaL_optstring(L, 1, "collect"), opts); |
| 189 | int ex = luaL_optint(L, 2, 0); | 192 | int ex = luaL_optinteger(L, 2, 0); |
| 190 | luaL_argcheck(L, o >= 0, 1, "invalid option"); | 193 | luaL_argcheck(L, o >= 0, 1, "invalid option"); |
| 191 | lua_pushinteger(L, lua_gc(L, optsnum[o], ex)); | 194 | lua_pushinteger(L, lua_gc(L, optsnum[o], ex)); |
| 192 | return 1; | 195 | return 1; |
| @@ -280,6 +283,7 @@ static int luaB_loadfile (lua_State *L) { · 87 unmodified lines | |||
| 280 | ** reserved slot inside the stack. | 283 | ** reserved slot inside the stack. |
| 281 | */ | 284 | */ |
| 282 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | 285 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { |
| 286 | (void)ud; /* to avoid warnings */ | ||
| 283 | luaL_checkstack(L, 2, "too many nested functions"); | 287 | luaL_checkstack(L, 2, "too many nested functions"); |
| 284 | lua_pushvalue(L, 1); /* get function */ | 288 | lua_pushvalue(L, 1); /* get function */ |
| 285 | lua_call(L, 0, 1); /* call it */ | 289 | lua_call(L, 0, 1); /* call it */ |
| @@ -469,7 +473,7 @@ static int auxresume (lua_State *L, lua_State *co, int narg) { · 183 unmodified lines | |||
| 469 | int status; | 473 | int status; |
| 470 | if (!lua_checkstack(co, narg)) | 474 | if (!lua_checkstack(co, narg)) |
| 471 | luaL_error(L, "too many arguments to resume"); | 475 | luaL_error(L, "too many arguments to resume"); |
| 472 | if (lua_threadstatus(co) == 0 && lua_gettop(co) == 0) { | 476 | if (lua_status(co) == 0 && lua_gettop(co) == 0) { |
| 473 | lua_pushliteral(L, "cannot resume dead coroutine"); | 477 | lua_pushliteral(L, "cannot resume dead coroutine"); |
| 474 | return -1; /* error flag */ | 478 | return -1; /* error flag */ |
| 475 | } | 479 | } |
| @@ -549,7 +553,7 @@ static int luaB_costatus (lua_State *L) { · 73 unmodified lines | |||
| 549 | luaL_argcheck(L, co, 1, "coroutine expected"); | 553 | luaL_argcheck(L, co, 1, "coroutine expected"); |
| 550 | if (L == co) lua_pushliteral(L, "running"); | 554 | if (L == co) lua_pushliteral(L, "running"); |
| 551 | else { | 555 | else { |
| 552 | switch (lua_threadstatus(co)) { | 556 | switch (lua_status(co)) { |
| 553 | case LUA_YIELD: | 557 | case LUA_YIELD: |
| 554 | lua_pushliteral(L, "suspended"); | 558 | lua_pushliteral(L, "suspended"); |
| 555 | break; | 559 | break; |
| @@ -572,7 +576,7 @@ static int luaB_costatus (lua_State *L) { · 16 unmodified lines | |||
| 572 | } | 576 | } |
| 573 | 577 | ||
| 574 | 578 | ||
| 575 | static int luaB_cocurrent (lua_State *L) { | 579 | static int luaB_corunning (lua_State *L) { |
| 576 | if (lua_pushthread(L)) | 580 | if (lua_pushthread(L)) |
| 577 | return 0; /* main thread is not a coroutine */ | 581 | return 0; /* main thread is not a coroutine */ |
| 578 | else | 582 | else |
| @@ -586,7 +590,7 @@ static const luaL_reg co_funcs[] = { · 7 unmodified lines | |||
| 586 | {"resume", luaB_coresume}, | 590 | {"resume", luaB_coresume}, |
| 587 | {"yield", luaB_yield}, | 591 | {"yield", luaB_yield}, |
| 588 | {"status", luaB_costatus}, | 592 | {"status", luaB_costatus}, |
| 589 | {"current", luaB_cocurrent}, | 593 | {"running", luaB_corunning}, |
| 590 | {NULL, NULL} | 594 | {NULL, NULL} |
| 591 | }; | 595 | }; |
| 592 | 596 | ||
| @@ -620,9 +624,6 @@ static void base_open (lua_State *L) { · 27 unmodified lines | |||
| 620 | /* create register._LOADED to track loaded modules */ | 624 | /* create register._LOADED to track loaded modules */ |
| 621 | lua_newtable(L); | 625 | lua_newtable(L); |
| 622 | lua_setfield(L, LUA_REGISTRYINDEX, "_LOADED"); | 626 | lua_setfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 623 | /* create register._PRELOAD to allow pre-loaded modules */ | ||
| 624 | lua_newtable(L); | ||
| 625 | lua_setfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); | ||
| 626 | /* set global _G */ | 627 | /* set global _G */ |
| 627 | lua_pushvalue(L, LUA_GLOBALSINDEX); | 628 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 628 | lua_setglobal(L, "_G"); | 629 | lua_setglobal(L, "_G"); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lcode.c,v 2.8 2004/12/03 20:35:33 roberto Exp $ | 2 | ** $Id: lcode.c,v 2.9 2005/01/10 18:17:39 roberto Exp $ |
| 3 | ** Code generator for Lua | 3 | ** Code generator for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -606,7 +606,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { · 600 unmodified lines | |||
| 606 | if (op == OPR_MINUS) { | 606 | if (op == OPR_MINUS) { |
| 607 | luaK_exp2val(fs, e); | 607 | luaK_exp2val(fs, e); |
| 608 | if (e->k == VK && ttisnumber(&fs->f->k[e->info])) | 608 | if (e->k == VK && ttisnumber(&fs->f->k[e->info])) |
| 609 | e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info])); | 609 | e->info = luaK_numberK(fs, num_unm(nvalue(&fs->f->k[e->info]))); |
| 610 | else { | 610 | else { |
| 611 | luaK_exp2anyreg(fs, e); | 611 | luaK_exp2anyreg(fs, e); |
| 612 | freeexp(fs, e); | 612 | freeexp(fs, e); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldblib.c,v 1.89 2004/11/17 12:02:41 roberto Exp $ | 2 | ** $Id: ldblib.c,v 1.93 2005/02/18 12:40:02 roberto Exp $ |
| 3 | ** Interface from Lua to its debug API | 3 | ** Interface from Lua to its debug API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -19,6 +19,40 @@ · 13 unmodified lines | |||
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | static int getmetatable (lua_State *L) { | ||
| 23 | luaL_checkany(L, 1); | ||
| 24 | if (!lua_getmetatable(L, 1)) { | ||
| 25 | lua_pushnil(L); /* no metatable */ | ||
| 26 | } | ||
| 27 | return 1; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | static int setmetatable (lua_State *L) { | ||
| 32 | int t = lua_type(L, 2); | ||
| 33 | luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, | ||
| 34 | "nil or table expected"); | ||
| 35 | lua_settop(L, 2); | ||
| 36 | lua_pushboolean(L, lua_setmetatable(L, 1)); | ||
| 37 | return 1; | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | static int getfenv (lua_State *L) { | ||
| 42 | lua_getfenv(L, 1); | ||
| 43 | return 1; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | static int setfenv (lua_State *L) { | ||
| 48 | luaL_checktype(L, 2, LUA_TTABLE); | ||
| 49 | lua_settop(L, 2); | ||
| 50 | if (lua_setfenv(L, 1) == 0) | ||
| 51 | luaL_error(L, "`setfenv' cannot change environment of given object"); | ||
| 52 | return 1; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 22 | static void settabss (lua_State *L, const char *i, const char *v) { | 56 | static void settabss (lua_State *L, const char *i, const char *v) { |
| 23 | lua_pushstring(L, v); | 57 | lua_pushstring(L, v); |
| 24 | lua_setfield(L, -2, i); | 58 | lua_setfield(L, -2, i); |
| @@ -275,12 +309,17 @@ static int debug (lua_State *L) { · 250 unmodified lines | |||
| 275 | #define LEVELS2 10 /* size of the second part of the stack */ | 309 | #define LEVELS2 10 /* size of the second part of the stack */ |
| 276 | 310 | ||
| 277 | static int errorfb (lua_State *L) { | 311 | static int errorfb (lua_State *L) { |
| 278 | int level = 0; | 312 | int level; |
| 279 | int firstpart = 1; /* still before eventual `...' */ | 313 | int firstpart = 1; /* still before eventual `...' */ |
| 280 | int arg; | 314 | int arg; |
| 281 | lua_State *L1 = getthread(L, &arg); | 315 | lua_State *L1 = getthread(L, &arg); |
| 282 | lua_Debug ar; | 316 | lua_Debug ar; |
| 283 | if (L == L1) level++; /* skip level 0 (it's this function) */ | 317 | if (lua_isnumber(L, arg+2)) { |
| 318 | level = lua_tointeger(L, arg+2); | ||
| 319 | lua_pop(L, 1); | ||
| 320 | } | ||
| 321 | else | ||
| 322 | level = (L == L1) ? 1 : 0; /* level 0 may be this own function */ | ||
| 284 | if (lua_gettop(L) == arg) | 323 | if (lua_gettop(L) == arg) |
| 285 | lua_pushliteral(L, ""); | 324 | lua_pushliteral(L, ""); |
| 286 | else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */ | 325 | else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */ |
| @@ -323,6 +362,10 @@ static int errorfb (lua_State *L) { · 36 unmodified lines | |||
| 323 | 362 | ||
| 324 | 363 | ||
| 325 | static const luaL_reg dblib[] = { | 364 | static const luaL_reg dblib[] = { |
| 365 | {"getmetatable", getmetatable}, | ||
| 366 | {"setmetatable", setmetatable}, | ||
| 367 | {"getfenv", getfenv}, | ||
| 368 | {"setfenv", setfenv}, | ||
| 326 | {"getlocal", getlocal}, | 369 | {"getlocal", getlocal}, |
| 327 | {"getinfo", getinfo}, | 370 | {"getinfo", getinfo}, |
| 328 | {"gethook", gethook}, | 371 | {"gethook", gethook}, |
| @@ -338,8 +381,6 @@ static const luaL_reg dblib[] = { · 9 unmodified lines | |||
| 338 | 381 | ||
| 339 | LUALIB_API int luaopen_debug (lua_State *L) { | 382 | LUALIB_API int luaopen_debug (lua_State *L) { |
| 340 | luaL_openlib(L, LUA_DBLIBNAME, dblib, 0); | 383 | luaL_openlib(L, LUA_DBLIBNAME, dblib, 0); |
| 341 | lua_pushcfunction(L, errorfb); | ||
| 342 | lua_setglobal(L, "_TRACEBACK"); | ||
| 343 | return 1; | 384 | return 1; |
| 344 | } | 385 | } |
| 345 | 386 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldo.c,v 2.13 2004/12/03 20:35:33 roberto Exp $ | 2 | ** $Id: ldo.c,v 2.14 2005/02/18 12:40:02 roberto Exp $ |
| 3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -474,7 +474,7 @@ static void f_parser (lua_State *L, void *ud) { · 468 unmodified lines | |||
| 474 | luaC_checkGC(L); | 474 | luaC_checkGC(L); |
| 475 | tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z, | 475 | tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z, |
| 476 | &p->buff, p->name); | 476 | &p->buff, p->name); |
| 477 | cl = luaF_newLclosure(L, tf->nups, gt(L)); | 477 | cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L))); |
| 478 | cl->l.p = tf; | 478 | cl->l.p = tf; |
| 479 | for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */ | 479 | for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */ |
| 480 | cl->l.upvals[i] = luaF_newupval(L); | 480 | cl->l.upvals[i] = luaF_newupval(L); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lfunc.c,v 2.5 2004/11/24 19:20:21 roberto Exp $ | 2 | ** $Id: lfunc.c,v 2.9 2005/02/18 12:40:02 roberto Exp $ |
| 3 | ** Auxiliary functions to manipulate prototypes and closures | 3 | ** Auxiliary functions to manipulate prototypes and closures |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -20,20 +20,21 @@ · 14 unmodified lines | |||
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | 22 | ||
| 23 | Closure *luaF_newCclosure (lua_State *L, int nelems) { | 23 | Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { |
| 24 | Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); | 24 | Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); |
| 25 | luaC_link(L, obj2gco(c), LUA_TFUNCTION); | 25 | luaC_link(L, obj2gco(c), LUA_TFUNCTION); |
| 26 | c->c.isC = 1; | 26 | c->c.isC = 1; |
| 27 | c->c.env = e; | ||
| 27 | c->c.nupvalues = cast(lu_byte, nelems); | 28 | c->c.nupvalues = cast(lu_byte, nelems); |
| 28 | return c; | 29 | return c; |
| 29 | } | 30 | } |
| 30 | 31 | ||
| 31 | 32 | ||
| 32 | Closure *luaF_newLclosure (lua_State *L, int nelems, TValue *e) { | 33 | Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { |
| 33 | Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); | 34 | Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); |
| 34 | luaC_link(L, obj2gco(c), LUA_TFUNCTION); | 35 | luaC_link(L, obj2gco(c), LUA_TFUNCTION); |
| 35 | c->l.isC = 0; | 36 | c->l.isC = 0; |
| 36 | c->l.g = *e; | 37 | c->l.env = e; |
| 37 | c->l.nupvalues = cast(lu_byte, nelems); | 38 | c->l.nupvalues = cast(lu_byte, nelems); |
| 38 | while (nelems--) c->l.upvals[nelems] = NULL; | 39 | while (nelems--) c->l.upvals[nelems] = NULL; |
| 39 | return c; | 40 | return c; |
| @@ -43,42 +44,72 @@ Closure *luaF_newLclosure (lua_State *L, int nelems, TValue *e) { · 3 unmodified lines | |||
| 43 | UpVal *luaF_newupval (lua_State *L) { | 44 | UpVal *luaF_newupval (lua_State *L) { |
| 44 | UpVal *uv = luaM_new(L, UpVal); | 45 | UpVal *uv = luaM_new(L, UpVal); |
| 45 | luaC_link(L, obj2gco(uv), LUA_TUPVAL); | 46 | luaC_link(L, obj2gco(uv), LUA_TUPVAL); |
| 46 | uv->v = &uv->value; | 47 | uv->v = &uv->u.value; |
| 47 | setnilvalue(uv->v); | 48 | setnilvalue(uv->v); |
| 48 | return uv; | 49 | return uv; |
| 49 | } | 50 | } |
| 50 | 51 | ||
| 51 | 52 | ||
| 52 | UpVal *luaF_findupval (lua_State *L, StkId level) { | 53 | UpVal *luaF_findupval (lua_State *L, StkId level) { |
| 54 | global_State *g = G(L); | ||
| 53 | GCObject **pp = &L->openupval; | 55 | GCObject **pp = &L->openupval; |
| 54 | UpVal *p; | 56 | UpVal *p; |
| 55 | UpVal *uv; | 57 | UpVal *uv; |
| 56 | while ((p = ngcotouv(*pp)) != NULL && p->v >= level) { | 58 | while ((p = ngcotouv(*pp)) != NULL && p->v >= level) { |
| 57 | if (p->v == level) return p; | 59 | lua_assert(p->v != &p->u.value); |
| 60 | if (p->v == level) { /* found a corresponding upvalue? */ | ||
| 61 | if (isdead(g, obj2gco(p))) /* is it dead? */ | ||
| 62 | changewhite(obj2gco(p)); /* ressurect it */ | ||
| 63 | return p; | ||
| 64 | } | ||
| 58 | pp = &p->next; | 65 | pp = &p->next; |
| 59 | } | 66 | } |
| 60 | uv = luaM_new(L, UpVal); /* not found: create a new one */ | 67 | uv = luaM_new(L, UpVal); /* not found: create a new one */ |
| 61 | uv->tt = LUA_TUPVAL; | 68 | uv->tt = LUA_TUPVAL; |
| 62 | uv->marked = luaC_white(G(L)); | 69 | uv->marked = luaC_white(g); |
| 63 | uv->v = level; /* current value lives in the stack */ | 70 | uv->v = level; /* current value lives in the stack */ |
| 64 | uv->next = *pp; /* chain it in the proper position */ | 71 | uv->next = *pp; /* chain it in the proper position */ |
| 65 | *pp = obj2gco(uv); | 72 | *pp = obj2gco(uv); |
| 73 | uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ | ||
| 74 | uv->u.l.next = g->uvhead.u.l.next; | ||
| 75 | uv->u.l.next->u.l.prev = uv; | ||
| 76 | g->uvhead.u.l.next = uv; | ||
| 77 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); | ||
| 66 | return uv; | 78 | return uv; |
| 67 | } | 79 | } |
| 68 | 80 | ||
| 69 | 81 | ||
| 82 | static void unlinkupval (UpVal *uv) { | ||
| 83 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); | ||
| 84 | uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ | ||
| 85 | uv->u.l.prev->u.l.next = uv->u.l.next; | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | void luaF_freeupval (lua_State *L, UpVal *uv) { | ||
| 90 | if (uv->v != &uv->u.value) /* is it open? */ | ||
| 91 | unlinkupval(uv); /* remove from open list */ | ||
| 92 | luaM_free(L, uv); /* free upvalue */ | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 70 | void luaF_close (lua_State *L, StkId level) { | 96 | void luaF_close (lua_State *L, StkId level) { |
| 71 | UpVal *uv; | 97 | UpVal *uv; |
| 72 | global_State *g = G(L); | 98 | global_State *g = G(L); |
| 73 | while ((uv = ngcotouv(L->openupval)) != NULL && uv->v >= level) { | 99 | while ((uv = ngcotouv(L->openupval)) != NULL && uv->v >= level) { |
| 74 | GCObject *o = obj2gco(uv); | 100 | GCObject *o = obj2gco(uv); |
| 75 | lua_assert(!isblack(o)); | 101 | lua_assert(!isblack(o) && uv->v != &uv->u.value); |
| 76 | L->openupval = uv->next; /* remove from `open' list */ | 102 | L->openupval = uv->next; /* remove from `open' list */ |
| 77 | if (isdead(g, o)) | 103 | if (isdead(g, o)) |
| 78 | luaM_free(L, uv); /* free upvalue */ | 104 | luaF_freeupval(L, uv); /* free upvalue */ |
| 79 | else { | 105 | else { |
| 80 | setobj(L, &uv->value, uv->v); | 106 | unlinkupval(uv); |
| 81 | uv->v = &uv->value; /* now current value lives here */ | 107 | setobj(L, &uv->u.value, uv->v); |
| 108 | if (isgray(o)) { | ||
| 109 | gray2black(o); /* closed upvalues are never gray */ | ||
| 110 | luaC_barrier(L, uv, &uv->u.value); | ||
| 111 | } | ||
| 112 | uv->v = &uv->u.value; /* now current value lives here */ | ||
| 82 | luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ | 113 | luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ |
| 83 | } | 114 | } |
| 84 | } | 115 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lfunc.h,v 2.1 2003/12/10 12:13:36 roberto Exp $ | 2 | ** $Id: lfunc.h,v 2.3 2005/02/18 12:40:02 roberto Exp $ |
| 3 | ** Auxiliary functions to manipulate prototypes and closures | 3 | ** Auxiliary functions to manipulate prototypes and closures |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -19,13 +19,14 @@ · 13 unmodified lines | |||
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | Proto *luaF_newproto (lua_State *L); | 21 | Proto *luaF_newproto (lua_State *L); |
| 22 | Closure *luaF_newCclosure (lua_State *L, int nelems); | 22 | Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); |
| 23 | Closure *luaF_newLclosure (lua_State *L, int nelems, TValue *e); | 23 | Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); |
| 24 | UpVal *luaF_newupval (lua_State *L); | 24 | UpVal *luaF_newupval (lua_State *L); |
| 25 | UpVal *luaF_findupval (lua_State *L, StkId level); | 25 | UpVal *luaF_findupval (lua_State *L, StkId level); |
| 26 | void luaF_close (lua_State *L, StkId level); | 26 | void luaF_close (lua_State *L, StkId level); |
| 27 | void luaF_freeproto (lua_State *L, Proto *f); | 27 | void luaF_freeproto (lua_State *L, Proto *f); |
| 28 | void luaF_freeclosure (lua_State *L, Closure *c); | 28 | void luaF_freeclosure (lua_State *L, Closure *c); |
| 29 | void luaF_freeupval (lua_State *L, UpVal *uv); | ||
| 29 | 30 | ||
| 30 | const char *luaF_getlocalname (const Proto *func, int local_number, int pc); | 31 | const char *luaF_getlocalname (const Proto *func, int local_number, int pc); |
| 31 | 32 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lgc.c,v 2.19 2004/12/13 12:15:11 roberto Exp $ | 2 | ** $Id: lgc.c,v 2.27 2005/02/23 17:30:22 roberto Exp $ |
| 3 | ** Garbage Collector | 3 | ** Garbage Collector |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -23,30 +23,26 @@ · 17 unmodified lines | |||
| 23 | #include "ltm.h" | 23 | #include "ltm.h" |
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | #define GCSTEPSIZE 1000 | 26 | #define GCSTEPSIZE 1024u |
| 27 | #define GCSWEEPMAX 10 | 27 | #define GCSWEEPMAX 40 |
| 28 | #define GCSWEEPCOST 30 | 28 | #define GCSWEEPCOST 10 |
| 29 | #define GCFINALIZECOST 100 | 29 | #define GCFINALIZECOST 100 |
| 30 | #define GCSTEPMUL 8 | ||
| 31 | 30 | ||
| 32 | 31 | ||
| 33 | #define FIXEDMASK bitmask(FIXEDBIT) | ||
| 34 | |||
| 35 | #define maskmarks \ | 32 | #define maskmarks \ |
| 36 | cast(lu_byte, ~(bitmask(BLACKBIT)|bit2mask(WHITE0BIT, WHITE1BIT))) | 33 | cast(lu_byte, ~(bitmask(BLACKBIT)|WHITEBITS)) |
| 37 | 34 | ||
| 38 | #define makewhite(g,x) \ | 35 | #define makewhite(g,x) \ |
| 39 | ((x)->gch.marked = ((x)->gch.marked & maskmarks) | g->currentwhite) | 36 | ((x)->gch.marked = ((x)->gch.marked & maskmarks) | luaC_white(g)) |
| 40 | 37 | ||
| 41 | #define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) | 38 | #define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) |
| 42 | #define gray2black(x) setbit((x)->gch.marked, BLACKBIT) | ||
| 43 | #define black2gray(x) resetbit((x)->gch.marked, BLACKBIT) | 39 | #define black2gray(x) resetbit((x)->gch.marked, BLACKBIT) |
| 44 | 40 | ||
| 45 | #define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT) | 41 | #define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT) |
| 46 | 42 | ||
| 47 | 43 | ||
| 48 | #define isfinalized(u) testbit((u)->marked, FINALIZEDBIT) | 44 | #define isfinalized(u) testbit((u)->marked, FINALIZEDBIT) |
| 49 | #define markfinalized(u) setbit((u)->marked, FINALIZEDBIT) | 45 | #define markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT) |
| 50 | 46 | ||
| 51 | 47 | ||
| 52 | #define KEYWEAK bitmask(KEYWEAKBIT) | 48 | #define KEYWEAK bitmask(KEYWEAKBIT) |
| @@ -61,9 +57,11 @@ · 8 unmodified lines | |||
| 61 | reallymarkobject(g, obj2gco(t)); } | 57 | reallymarkobject(g, obj2gco(t)); } |
| 62 | 58 | ||
| 63 | 59 | ||
| 60 | #define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpace) | ||
| 61 | |||
| 64 | 62 | ||
| 65 | static void removeentry (Node *n) { | 63 | static void removeentry (Node *n) { |
| 66 | setnilvalue(gval(n)); /* remove corresponding value ... */ | 64 | lua_assert(ttisnil(gval(n))); |
| 67 | if (iscollectable(gkey(n))) | 65 | if (iscollectable(gkey(n))) |
| 68 | setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */ | 66 | setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */ |
| 69 | } | 67 | } |
| @@ -80,14 +78,14 @@ static void reallymarkobject (global_State *g, GCObject *o) { · 10 unmodified lines | |||
| 80 | Table *mt = gco2u(o)->metatable; | 78 | Table *mt = gco2u(o)->metatable; |
| 81 | gray2black(o); /* udata are never gray */ | 79 | gray2black(o); /* udata are never gray */ |
| 82 | if (mt) markobject(g, mt); | 80 | if (mt) markobject(g, mt); |
| 81 | markobject(g, gco2u(o)->env); | ||
| 83 | return; | 82 | return; |
| 84 | } | 83 | } |
| 85 | case LUA_TUPVAL: { | 84 | case LUA_TUPVAL: { |
| 86 | UpVal *uv = gco2uv(o); | 85 | UpVal *uv = gco2uv(o); |
| 87 | if (uv->v == &uv->value) { /* closed? */ | 86 | markvalue(g, uv->v); |
| 88 | markvalue(g, uv->v); | 87 | if (uv->v == &uv->u.value) /* closed? */ |
| 89 | gray2black(o); | 88 | gray2black(o); /* open upvalues are never black */ |
| 90 | } | ||
| 91 | return; | 89 | return; |
| 92 | } | 90 | } |
| 93 | case LUA_TFUNCTION: { | 91 | case LUA_TFUNCTION: { |
| @@ -116,22 +114,24 @@ static void reallymarkobject (global_State *g, GCObject *o) { · 22 unmodified lines | |||
| 116 | 114 | ||
| 117 | 115 | ||
| 118 | static void marktmu (global_State *g) { | 116 | static void marktmu (global_State *g) { |
| 119 | GCObject *u; | 117 | GCObject *u = g->tmudata; |
| 120 | for (u = g->tmudata; u; u = u->gch.next) { | 118 | if (u) { |
| 121 | makewhite(g, u); /* may be marked, if left from previous GC */ | 119 | do { |
| 122 | reallymarkobject(g, u); | 120 | u = u->gch.next; |
| 121 | makewhite(g, u); /* may be marked, if left from previous GC */ | ||
| 122 | reallymarkobject(g, u); | ||
| 123 | } while (u != g->tmudata); | ||
| 123 | } | 124 | } |
| 124 | } | 125 | } |
| 125 | 126 | ||
| 126 | 127 | ||
| 127 | /* move `dead' udata that need finalization to list `tmudata' */ | 128 | /* move `dead' udata that need finalization to list `tmudata' */ |
| 128 | size_t luaC_separateudata (lua_State *L, int all) { | 129 | size_t luaC_separateudata (lua_State *L, int all) { |
| 130 | global_State *g = G(L); | ||
| 129 | size_t deadmem = 0; | 131 | size_t deadmem = 0; |
| 130 | GCObject **p = &G(L)->firstudata; | 132 | GCObject **p = &g->mainthread->next; |
| 131 | GCObject *curr; | 133 | GCObject *curr; |
| 132 | GCObject *collected = NULL; /* to collect udata with gc event */ | 134 | while ((curr = *p) != NULL) { |
| 133 | GCObject **lastcollected = &collected; | ||
| 134 | while ((curr = *p)->gch.tt == LUA_TUSERDATA) { | ||
| 135 | if (!(iswhite(curr) || all) || isfinalized(gco2u(curr))) | 135 | if (!(iswhite(curr) || all) || isfinalized(gco2u(curr))) |
| 136 | p = &curr->gch.next; /* don't bother with them */ | 136 | p = &curr->gch.next; /* don't bother with them */ |
| 137 | else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) { | 137 | else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) { |
| @@ -142,15 +142,16 @@ size_t luaC_separateudata (lua_State *L, int all) { · 4 unmodified lines | |||
| 142 | deadmem += sizeudata(gco2u(curr)); | 142 | deadmem += sizeudata(gco2u(curr)); |
| 143 | markfinalized(gco2u(curr)); | 143 | markfinalized(gco2u(curr)); |
| 144 | *p = curr->gch.next; | 144 | *p = curr->gch.next; |
| 145 | curr->gch.next = NULL; /* link `curr' at the end of `collected' list */ | 145 | /* link `curr' at the end of `tmudata' list */ |
| 146 | *lastcollected = curr; | 146 | if (g->tmudata == NULL) /* list is empty? */ |
| 147 | lastcollected = &curr->gch.next; | 147 | g->tmudata = curr->gch.next = curr; /* creates a circular list */ |
| 148 | else { | ||
| 149 | curr->gch.next = g->tmudata->gch.next; | ||
| 150 | g->tmudata->gch.next = curr; | ||
| 151 | g->tmudata = curr; | ||
| 152 | } | ||
| 148 | } | 153 | } |
| 149 | } | 154 | } |
| 150 | lua_assert(curr == obj2gco(G(L)->mainthread)); | ||
| 151 | /* insert collected udata with gc event into `tmudata' list */ | ||
| 152 | *lastcollected = G(L)->tmudata; | ||
| 153 | G(L)->tmudata = collected; | ||
| 154 | return deadmem; | 155 | return deadmem; |
| 155 | } | 156 | } |
| 156 | 157 | ||
| @@ -162,7 +163,6 @@ static int traversetable (global_State *g, Table *h) { · 5 unmodified lines | |||
| 162 | const TValue *mode; | 163 | const TValue *mode; |
| 163 | if (h->metatable) | 164 | if (h->metatable) |
| 164 | markobject(g, h->metatable); | 165 | markobject(g, h->metatable); |
| 165 | lua_assert(h->lsizenode || h->node == g->dummynode); | ||
| 166 | mode = gfasttm(g, h->metatable, TM_MODE); | 166 | mode = gfasttm(g, h->metatable, TM_MODE); |
| 167 | if (mode && ttisstring(mode)) { /* is there a weak mode? */ | 167 | if (mode && ttisstring(mode)) { /* is there a weak mode? */ |
| 168 | weakkey = (strchr(svalue(mode), 'k') != NULL); | 168 | weakkey = (strchr(svalue(mode), 'k') != NULL); |
| @@ -223,6 +223,7 @@ static void traverseproto (global_State *g, Proto *f) { · 54 unmodified lines | |||
| 223 | 223 | ||
| 224 | 224 | ||
| 225 | static void traverseclosure (global_State *g, Closure *cl) { | 225 | static void traverseclosure (global_State *g, Closure *cl) { |
| 226 | markobject(g, cl->c.env); | ||
| 226 | if (cl->c.isC) { | 227 | if (cl->c.isC) { |
| 227 | int i; | 228 | int i; |
| 228 | for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */ | 229 | for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */ |
| @@ -231,7 +232,6 @@ static void traverseclosure (global_State *g, Closure *cl) { · 2 unmodified lines | |||
| 231 | else { | 232 | else { |
| 232 | int i; | 233 | int i; |
| 233 | lua_assert(cl->l.nupvalues == cl->l.p->nups); | 234 | lua_assert(cl->l.nupvalues == cl->l.p->nups); |
| 234 | markobject(g, hvalue(&cl->l.g)); | ||
| 235 | markobject(g, cl->l.p); | 235 | markobject(g, cl->l.p); |
| 236 | for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */ | 236 | for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */ |
| 237 | markobject(g, cl->l.upvals[i]); | 237 | markobject(g, cl->l.upvals[i]); |
| @@ -368,8 +368,10 @@ static void cleartable (GCObject *l) { · 130 unmodified lines | |||
| 368 | while (i--) { | 368 | while (i--) { |
| 369 | Node *n = gnode(h, i); | 369 | Node *n = gnode(h, i); |
| 370 | if (!ttisnil(gval(n)) && /* non-empty entry? */ | 370 | if (!ttisnil(gval(n)) && /* non-empty entry? */ |
| 371 | (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) | 371 | (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) { |
| 372 | setnilvalue(gval(n)); /* remove value ... */ | ||
| 372 | removeentry(n); /* remove entry from table */ | 373 | removeentry(n); /* remove entry from table */ |
| 374 | } | ||
| 373 | } | 375 | } |
| 374 | l = h->gclist; | 376 | l = h->gclist; |
| 375 | } | 377 | } |
| @@ -380,7 +382,7 @@ static void freeobj (lua_State *L, GCObject *o) { · 4 unmodified lines | |||
| 380 | switch (o->gch.tt) { | 382 | switch (o->gch.tt) { |
| 381 | case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; | 383 | case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; |
| 382 | case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break; | 384 | case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break; |
| 383 | case LUA_TUPVAL: luaM_free(L, gco2uv(o)); break; | 385 | case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break; |
| 384 | case LUA_TTABLE: luaH_free(L, gco2h(o)); break; | 386 | case LUA_TTABLE: luaH_free(L, gco2h(o)); break; |
| 385 | case LUA_TTHREAD: { | 387 | case LUA_TTHREAD: { |
| 386 | lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread); | 388 | lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread); |
| @@ -402,26 +404,23 @@ static void freeobj (lua_State *L, GCObject *o) { · 15 unmodified lines | |||
| 402 | 404 | ||
| 403 | 405 | ||
| 404 | 406 | ||
| 405 | #define sweepwholelist(L,p) sweeplist(L,p,LUA_MAXINT32) | 407 | #define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM) |
| 406 | 408 | ||
| 407 | 409 | ||
| 408 | static GCObject **sweeplist (lua_State *L, GCObject **p, lu_int32 count) { | 410 | static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { |
| 409 | GCObject *curr; | 411 | GCObject *curr; |
| 410 | global_State *g = G(L); | 412 | global_State *g = G(L); |
| 411 | int whitebit = otherwhite(g); | 413 | int deadmask = otherwhite(g); |
| 412 | int deadmask = whitebit | FIXEDMASK; | ||
| 413 | int generational = g->gcgenerational; | ||
| 414 | while ((curr = *p) != NULL && count-- > 0) { | 414 | while ((curr = *p) != NULL && count-- > 0) { |
| 415 | if ((curr->gch.marked ^ whitebit) & deadmask) { | 415 | if (curr->gch.tt == LUA_TTHREAD) /* sweep open upvalues of each thread */ |
| 416 | sweepwholelist(L, &gco2th(curr)->openupval); | ||
| 417 | if ((curr->gch.marked ^ WHITEBITS) & deadmask) { /* not dead? */ | ||
| 416 | lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT)); | 418 | lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT)); |
| 417 | if (!generational || isdead(g, curr)) | 419 | makewhite(g, curr); /* make it white (for next cycle) */ |
| 418 | makewhite(g, curr); | ||
| 419 | if (curr->gch.tt == LUA_TTHREAD) | ||
| 420 | sweepwholelist(L, &gco2th(curr)->openupval); | ||
| 421 | p = &curr->gch.next; | 420 | p = &curr->gch.next; |
| 422 | } | 421 | } |
| 423 | else { | 422 | else { /* must erase `curr' */ |
| 424 | lua_assert(isdead(g, curr)); | 423 | lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT)); |
| 425 | *p = curr->gch.next; | 424 | *p = curr->gch.next; |
| 426 | if (curr == g->rootgc) /* is the first element of the list? */ | 425 | if (curr == g->rootgc) /* is the first element of the list? */ |
| 427 | g->rootgc = curr->gch.next; /* adjust first */ | 426 | g->rootgc = curr->gch.next; /* adjust first */ |
| @@ -432,21 +431,10 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_int32 count) { · 4 unmodified lines | |||
| 432 | } | 431 | } |
| 433 | 432 | ||
| 434 | 433 | ||
| 435 | |||
| 436 | static void freelist (lua_State *L, GCObject **p) { | ||
| 437 | while (*p) { | ||
| 438 | GCObject *curr = *p; | ||
| 439 | *p = (*p)->gch.next; | ||
| 440 | if (curr != obj2gco(L)) | ||
| 441 | freeobj(L, curr); | ||
| 442 | } | ||
| 443 | } | ||
| 444 | |||
| 445 | |||
| 446 | static void checkSizes (lua_State *L) { | 434 | static void checkSizes (lua_State *L) { |
| 447 | global_State *g = G(L); | 435 | global_State *g = G(L); |
| 448 | /* check size of string hash */ | 436 | /* check size of string hash */ |
| 449 | if (g->strt.nuse < cast(lu_int32, G(L)->strt.size/4) && | 437 | if (g->strt.nuse < cast(lu_int32, g->strt.size/4) && |
| 450 | g->strt.size > MINSTRTABSIZE*2) | 438 | g->strt.size > MINSTRTABSIZE*2) |
| 451 | luaS_resize(L, g->strt.size/2); /* table is too big */ | 439 | luaS_resize(L, g->strt.size/2); /* table is too big */ |
| 452 | /* check size of buffer */ | 440 | /* check size of buffer */ |
| @@ -459,22 +447,29 @@ static void checkSizes (lua_State *L) { · 6 unmodified lines | |||
| 459 | 447 | ||
| 460 | static void GCTM (lua_State *L) { | 448 | static void GCTM (lua_State *L) { |
| 461 | global_State *g = G(L); | 449 | global_State *g = G(L); |
| 462 | GCObject *o = g->tmudata; | 450 | GCObject *o = g->tmudata->gch.next; /* get first element */ |
| 463 | Udata *udata = rawgco2u(o); | 451 | Udata *udata = rawgco2u(o); |
| 464 | const TValue *tm; | 452 | const TValue *tm; |
| 465 | g->tmudata = udata->uv.next; /* remove udata from `tmudata' */ | 453 | /* remove udata from `tmudata' */ |
| 466 | udata->uv.next = g->firstudata->uv.next; /* return it to `root' list */ | 454 | if (o == g->tmudata) /* last element? */ |
| 467 | g->firstudata->uv.next = o; | 455 | g->tmudata = NULL; |
| 456 | else | ||
| 457 | g->tmudata->gch.next = udata->uv.next; | ||
| 458 | udata->uv.next = g->mainthread->next; /* return it to `root' list */ | ||
| 459 | g->mainthread->next = o; | ||
| 468 | makewhite(g, o); | 460 | makewhite(g, o); |
| 469 | tm = fasttm(L, udata->uv.metatable, TM_GC); | 461 | tm = fasttm(L, udata->uv.metatable, TM_GC); |
| 470 | if (tm != NULL) { | 462 | if (tm != NULL) { |
| 471 | lu_byte oldah = L->allowhook; | 463 | lu_byte oldah = L->allowhook; |
| 464 | lu_mem oldt = g->GCthreshold; | ||
| 472 | L->allowhook = 0; /* stop debug hooks during GC tag method */ | 465 | L->allowhook = 0; /* stop debug hooks during GC tag method */ |
| 466 | g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */ | ||
| 473 | setobj2s(L, L->top, tm); | 467 | setobj2s(L, L->top, tm); |
| 474 | setuvalue(L, L->top+1, udata); | 468 | setuvalue(L, L->top+1, udata); |
| 475 | L->top += 2; | 469 | L->top += 2; |
| 476 | luaD_call(L, L->top - 2, 0); | 470 | luaD_call(L, L->top - 2, 0); |
| 477 | L->allowhook = oldah; /* restore hooks */ | 471 | L->allowhook = oldah; /* restore hooks */ |
| 472 | g->GCthreshold = oldt; /* restore threshold */ | ||
| 478 | } | 473 | } |
| 479 | } | 474 | } |
| 480 | 475 | ||
| @@ -491,9 +486,10 @@ void luaC_callGCTM (lua_State *L) { · 10 unmodified lines | |||
| 491 | void luaC_freeall (lua_State *L) { | 486 | void luaC_freeall (lua_State *L) { |
| 492 | global_State *g = G(L); | 487 | global_State *g = G(L); |
| 493 | int i; | 488 | int i; |
| 494 | freelist(L, &g->rootgc); | 489 | g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* mask to collect all elements */ |
| 490 | sweepwholelist(L, &g->rootgc); | ||
| 495 | for (i = 0; i < g->strt.size; i++) /* free all string lists */ | 491 | for (i = 0; i < g->strt.size; i++) /* free all string lists */ |
| 496 | freelist(L, &G(L)->strt.hash[i]); | 492 | sweepwholelist(L, &g->strt.hash[i]); |
| 497 | } | 493 | } |
| 498 | 494 | ||
| 499 | 495 | ||
| @@ -512,18 +508,11 @@ static void markroot (lua_State *L) { · 12 unmodified lines | |||
| 512 | 508 | ||
| 513 | 509 | ||
| 514 | static void remarkupvals (global_State *g) { | 510 | static void remarkupvals (global_State *g) { |
| 515 | GCObject *o; | 511 | UpVal *uv; |
| 516 | for (o = obj2gco(g->mainthread); o; o = o->gch.next) { | 512 | for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) { |
| 517 | lua_assert(!isblack(o)); | 513 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); |
| 518 | if (iswhite(o)) { | 514 | if (isgray(obj2gco(uv))) |
| 519 | GCObject *curr; | 515 | markvalue(g, uv->v); |
| 520 | for (curr = gco2th(o)->openupval; curr != NULL; curr = curr->gch.next) { | ||
| 521 | if (isgray(curr)) { | ||
| 522 | UpVal *uv = gco2uv(curr); | ||
| 523 | markvalue(g, uv->v); | ||
| 524 | } | ||
| 525 | } | ||
| 526 | } | ||
| 527 | } | 516 | } |
| 528 | } | 517 | } |
| 529 | 518 | ||
| @@ -531,7 +520,6 @@ static void remarkupvals (global_State *g) { · 1 unmodified line | |||
| 531 | static void atomic (lua_State *L) { | 520 | static void atomic (lua_State *L) { |
| 532 | global_State *g = G(L); | 521 | global_State *g = G(L); |
| 533 | size_t udsize; /* total size of userdata to be finalized */ | 522 | size_t udsize; /* total size of userdata to be finalized */ |
| 534 | int aux; | ||
| 535 | /* remark objects cautch by write barrier */ | 523 | /* remark objects cautch by write barrier */ |
| 536 | propagateall(g); | 524 | propagateall(g); |
| 537 | /* remark occasional upvalues of (maybe) dead threads */ | 525 | /* remark occasional upvalues of (maybe) dead threads */ |
| @@ -555,10 +543,6 @@ static void atomic (lua_State *L) { · 17 unmodified lines | |||
| 555 | g->sweepstrgc = 0; | 543 | g->sweepstrgc = 0; |
| 556 | g->sweepgc = &g->rootgc; | 544 | g->sweepgc = &g->rootgc; |
| 557 | g->gcstate = GCSsweepstring; | 545 | g->gcstate = GCSsweepstring; |
| 558 | aux = g->gcgenerational; | ||
| 559 | g->gcgenerational = g->incgc && (g->estimate/2 <= g->prevestimate); | ||
| 560 | if (!aux) /* last collection was full? */ | ||
| 561 | g->prevestimate = g->estimate; /* keep estimate of last full collection */ | ||
| 562 | g->estimate = g->totalbytes - udsize; /* first estimate */ | 546 | g->estimate = g->totalbytes - udsize; /* first estimate */ |
| 563 | } | 547 | } |
| 564 | 548 | ||
| @@ -568,11 +552,7 @@ static l_mem singlestep (lua_State *L) { · 3 unmodified lines | |||
| 568 | /*lua_checkmemory(L);*/ | 552 | /*lua_checkmemory(L);*/ |
| 569 | switch (g->gcstate) { | 553 | switch (g->gcstate) { |
| 570 | case GCSpause: { | 554 | case GCSpause: { |
| 571 | /* start a new collection */ | 555 | markroot(L); /* start a new collection */ |
| 572 | if (g->gcgenerational) | ||
| 573 | atomic(L); | ||
| 574 | else | ||
| 575 | markroot(L); | ||
| 576 | return 0; | 556 | return 0; |
| 577 | } | 557 | } |
| 578 | case GCSpropagate: { | 558 | case GCSpropagate: { |
| @@ -605,13 +585,12 @@ static l_mem singlestep (lua_State *L) { · 26 unmodified lines | |||
| 605 | } | 585 | } |
| 606 | case GCSfinalize: { | 586 | case GCSfinalize: { |
| 607 | if (g->tmudata) { | 587 | if (g->tmudata) { |
| 608 | g->GCthreshold += GCFINALIZECOST; /* avoid GC steps inside method */ | ||
| 609 | GCTM(L); | 588 | GCTM(L); |
| 610 | g->GCthreshold -= GCFINALIZECOST; /* correct threshold */ | ||
| 611 | return GCFINALIZECOST; | 589 | return GCFINALIZECOST; |
| 612 | } | 590 | } |
| 613 | else { | 591 | else { |
| 614 | g->gcstate = GCSpause; /* end collection */ | 592 | g->gcstate = GCSpause; /* end collection */ |
| 593 | g->gcdept = 0; | ||
| 615 | return 0; | 594 | return 0; |
| 616 | } | 595 | } |
| 617 | } | 596 | } |
| @@ -622,25 +601,33 @@ static l_mem singlestep (lua_State *L) { · 4 unmodified lines | |||
| 622 | 601 | ||
| 623 | void luaC_step (lua_State *L) { | 602 | void luaC_step (lua_State *L) { |
| 624 | global_State *g = G(L); | 603 | global_State *g = G(L); |
| 625 | l_mem lim = (g->totalbytes - (g->GCthreshold - GCSTEPSIZE)) * GCSTEPMUL; | 604 | l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul; |
| 605 | if (lim == 0) | ||
| 606 | lim = (MAX_LUMEM-1)/2; /* no limit */ | ||
| 607 | g->gcdept += g->totalbytes - g->GCthreshold; | ||
| 626 | do { | 608 | do { |
| 627 | lim -= singlestep(L); | 609 | lim -= singlestep(L); |
| 628 | if (g->gcstate == GCSpause) | 610 | if (g->gcstate == GCSpause) |
| 629 | break; | 611 | break; |
| 630 | } while (lim > 0 || !g->incgc); | 612 | } while (lim > 0); |
| 631 | if (g->gcstate != GCSpause) | 613 | if (g->gcstate != GCSpause) { |
| 632 | g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/STEPMUL; */ | 614 | if (g->gcdept < GCSTEPSIZE) |
| 615 | g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/g->gcstepmul;*/ | ||
| 616 | else { | ||
| 617 | g->gcdept -= GCSTEPSIZE; | ||
| 618 | g->GCthreshold = g->totalbytes; | ||
| 619 | } | ||
| 620 | } | ||
| 633 | else { | 621 | else { |
| 634 | lua_assert(g->totalbytes >= g->estimate); | 622 | lua_assert(g->totalbytes >= g->estimate); |
| 635 | g->GCthreshold = g->estimate + ((g->estimate/GCDIV) * g->gcpace); | 623 | setthreshold(g); |
| 636 | } | 624 | } |
| 637 | } | 625 | } |
| 638 | 626 | ||
| 639 | 627 | ||
| 640 | void luaC_fullgc (lua_State *L) { | 628 | void luaC_fullgc (lua_State *L) { |
| 641 | global_State *g = G(L); | 629 | global_State *g = G(L); |
| 642 | if (g->gcstate <= GCSpropagate || g->gcgenerational) { | 630 | if (g->gcstate <= GCSpropagate) { |
| 643 | g->gcgenerational = 0; | ||
| 644 | /* reset sweep marks to sweep all elements (returning them to white) */ | 631 | /* reset sweep marks to sweep all elements (returning them to white) */ |
| 645 | g->sweepstrgc = 0; | 632 | g->sweepstrgc = 0; |
| 646 | g->sweepgc = &g->rootgc; | 633 | g->sweepgc = &g->rootgc; |
| @@ -650,40 +637,37 @@ void luaC_fullgc (lua_State *L) { · 3 unmodified lines | |||
| 650 | g->weak = NULL; | 637 | g->weak = NULL; |
| 651 | g->gcstate = GCSsweepstring; | 638 | g->gcstate = GCSsweepstring; |
| 652 | } | 639 | } |
| 640 | lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate); | ||
| 653 | /* finish any pending sweep phase */ | 641 | /* finish any pending sweep phase */ |
| 654 | while (g->gcstate != GCSfinalize) { | 642 | while (g->gcstate != GCSfinalize) { |
| 655 | lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep); | 643 | lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep); |
| 656 | singlestep(L); | 644 | singlestep(L); |
| 657 | } | 645 | } |
| 658 | markroot(L); | 646 | markroot(L); |
| 659 | lua_assert(!g->gcgenerational); | ||
| 660 | while (g->gcstate != GCSpause) { | 647 | while (g->gcstate != GCSpause) { |
| 661 | singlestep(L); | 648 | singlestep(L); |
| 662 | g->gcgenerational = 0; /* keep it in this mode */ | ||
| 663 | } | 649 | } |
| 664 | g->GCthreshold = 2*g->estimate; | 650 | setthreshold(g); |
| 665 | } | 651 | } |
| 666 | 652 | ||
| 667 | 653 | ||
| 668 | void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) { | 654 | void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) { |
| 669 | global_State *g = G(L); | 655 | global_State *g = G(L); |
| 670 | lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); | 656 | lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); |
| 671 | lua_assert(g->gcgenerational || | 657 | lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); |
| 672 | (g->gcstate != GCSfinalize && g->gcstate != GCSpause)); | ||
| 673 | lua_assert(ttype(&o->gch) != LUA_TTABLE); | 658 | lua_assert(ttype(&o->gch) != LUA_TTABLE); |
| 674 | /* must keep invariant? */ | 659 | /* must keep invariant? */ |
| 675 | if (g->gcstate == GCSpropagate || g->gcgenerational) | 660 | if (g->gcstate == GCSpropagate) |
| 676 | reallymarkobject(g, v); /* restore invariant */ | 661 | reallymarkobject(g, v); /* restore invariant */ |
| 677 | else /* don't mind */ | 662 | else /* don't mind */ |
| 678 | makewhite(g, o); /* mark as white just to avoid other barriers */ | 663 | makewhite(g, o); /* mark as white just to avoid other barriers */ |
| 679 | } | 664 | } |
| 680 | 665 | ||
| 681 | 666 | ||
| 682 | void luaC_barrierback (lua_State *L, GCObject *o, GCObject *v) { | 667 | void luaC_barrierback (lua_State *L, GCObject *o) { |
| 683 | global_State *g = G(L); | 668 | global_State *g = G(L); |
| 684 | lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); | 669 | lua_assert(isblack(o) && !isdead(g, o)); |
| 685 | lua_assert(g->gcgenerational || | 670 | lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); |
| 686 | (g->gcstate != GCSfinalize && g->gcstate != GCSpause)); | ||
| 687 | black2gray(o); /* make table gray (again) */ | 671 | black2gray(o); /* make table gray (again) */ |
| 688 | gco2h(o)->gclist = g->grayagain; | 672 | gco2h(o)->gclist = g->grayagain; |
| 689 | g->grayagain = o; | 673 | g->grayagain = o; |
| @@ -705,7 +689,7 @@ void luaC_linkupval (lua_State *L, UpVal *uv) { · 15 unmodified lines | |||
| 705 | o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */ | 689 | o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */ |
| 706 | g->rootgc = o; | 690 | g->rootgc = o; |
| 707 | if (isgray(o)) { | 691 | if (isgray(o)) { |
| 708 | if (g->gcstate == GCSpropagate || g->gcgenerational) { | 692 | if (g->gcstate == GCSpropagate) { |
| 709 | gray2black(o); /* closed upvalues need barrier */ | 693 | gray2black(o); /* closed upvalues need barrier */ |
| 710 | luaC_barrier(L, uv, uv->v); | 694 | luaC_barrier(L, uv, uv->v); |
| 711 | } | 695 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lgc.h,v 2.9 2004/09/15 20:38:15 roberto Exp $ | 2 | ** $Id: lgc.h,v 2.12 2005/02/23 17:30:22 roberto Exp $ |
| 3 | ** Garbage Collector | 3 | ** Garbage Collector |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -29,7 +29,7 @@ · 23 unmodified lines | |||
| 29 | #define testbits(x,m) ((x) & (m)) | 29 | #define testbits(x,m) ((x) & (m)) |
| 30 | #define bitmask(b) (1<<(b)) | 30 | #define bitmask(b) (1<<(b)) |
| 31 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) | 31 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) |
| 32 | #define setbit(x,b) setbits(x, bitmask(b)) | 32 | #define l_setbit(x,b) setbits(x, bitmask(b)) |
| 33 | #define resetbit(x,b) resetbits(x, bitmask(b)) | 33 | #define resetbit(x,b) resetbits(x, bitmask(b)) |
| 34 | #define testbit(x,b) testbits(x, bitmask(b)) | 34 | #define testbit(x,b) testbits(x, bitmask(b)) |
| 35 | #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) | 35 | #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) |
| @@ -47,8 +47,10 @@ · 11 unmodified lines | |||
| 47 | ** bit 3 - for tables: has weak keys | 47 | ** bit 3 - for tables: has weak keys |
| 48 | ** bit 4 - for tables: has weak values | 48 | ** bit 4 - for tables: has weak values |
| 49 | ** bit 5 - object is fixed (should not be collected) | 49 | ** bit 5 - object is fixed (should not be collected) |
| 50 | ** bit 6 - object is "super" fixed (only the main thread) | ||
| 50 | */ | 51 | */ |
| 51 | 52 | ||
| 53 | |||
| 52 | #define WHITE0BIT 0 | 54 | #define WHITE0BIT 0 |
| 53 | #define WHITE1BIT 1 | 55 | #define WHITE1BIT 1 |
| 54 | #define BLACKBIT 2 | 56 | #define BLACKBIT 2 |
| @@ -56,20 +58,23 @@ · 1 unmodified line | |||
| 56 | #define KEYWEAKBIT 3 | 58 | #define KEYWEAKBIT 3 |
| 57 | #define VALUEWEAKBIT 4 | 59 | #define VALUEWEAKBIT 4 |
| 58 | #define FIXEDBIT 5 | 60 | #define FIXEDBIT 5 |
| 61 | #define SFIXEDBIT 6 | ||
| 62 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) | ||
| 59 | 63 | ||
| 60 | 64 | ||
| 61 | #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) | 65 | #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) |
| 62 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) | 66 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) |
| 63 | #define isgray(x) (!isblack(x) && !iswhite(x)) | 67 | #define isgray(x) (!isblack(x) && !iswhite(x)) |
| 64 | 68 | ||
| 65 | #define otherwhite(g) (g->currentwhite ^ bit2mask(WHITE0BIT, WHITE1BIT)) | 69 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) |
| 66 | #define isdead(g,v) ((v)->gch.marked & otherwhite(g)) | 70 | #define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) |
| 67 | 71 | ||
| 68 | #define changewhite(x) ((x)->gch.marked ^= bit2mask(WHITE0BIT, WHITE1BIT)) | 72 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) |
| 73 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) | ||
| 69 | 74 | ||
| 70 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) | 75 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) |
| 71 | 76 | ||
| 72 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite) | 77 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) |
| 73 | 78 | ||
| 74 | 79 | ||
| 75 | #define luaC_checkGC(L) { if (G(L)->totalbytes >= G(L)->GCthreshold) \ | 80 | #define luaC_checkGC(L) { if (G(L)->totalbytes >= G(L)->GCthreshold) \ |
| @@ -80,7 +85,7 @@ · 4 unmodified lines | |||
| 80 | luaC_barrierf(L,obj2gco(p),gcvalue(v)); } | 85 | luaC_barrierf(L,obj2gco(p),gcvalue(v)); } |
| 81 | 86 | ||
| 82 | #define luaC_barriert(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ | 87 | #define luaC_barriert(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ |
| 83 | luaC_barrierback(L,obj2gco(p),gcvalue(v)); } | 88 | luaC_barrierback(L,obj2gco(p)); } |
| 84 | 89 | ||
| 85 | #define luaC_objbarrier(L,p,o) \ | 90 | #define luaC_objbarrier(L,p,o) \ |
| 86 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ | 91 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ |
| @@ -88,7 +93,7 @@ · 1 unmodified line | |||
| 88 | 93 | ||
| 89 | #define luaC_objbarriert(L,p,o) \ | 94 | #define luaC_objbarriert(L,p,o) \ |
| 90 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ | 95 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ |
| 91 | luaC_barrierback(L,obj2gco(p),obj2gco(o)); } | 96 | luaC_barrierback(L,obj2gco(p)); } |
| 92 | 97 | ||
| 93 | size_t luaC_separateudata (lua_State *L, int all); | 98 | size_t luaC_separateudata (lua_State *L, int all); |
| 94 | void luaC_callGCTM (lua_State *L); | 99 | void luaC_callGCTM (lua_State *L); |
| @@ -98,7 +103,7 @@ void luaC_fullgc (lua_State *L); · 3 unmodified lines | |||
| 98 | void luaC_link (lua_State *L, GCObject *o, lu_byte tt); | 103 | void luaC_link (lua_State *L, GCObject *o, lu_byte tt); |
| 99 | void luaC_linkupval (lua_State *L, UpVal *uv); | 104 | void luaC_linkupval (lua_State *L, UpVal *uv); |
| 100 | void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); | 105 | void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); |
| 101 | void luaC_barrierback (lua_State *L, GCObject *o, GCObject *v); | 106 | void luaC_barrierback (lua_State *L, GCObject *o); |
| 102 | 107 | ||
| 103 | 108 | ||
| 104 | #endif | 109 | #endif |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: linit.c,v 1.8 2004/07/09 15:47:48 roberto Exp $ | 2 | ** $Id: linit.c,v 1.9 2005/02/18 12:40:02 roberto Exp $ |
| 3 | ** Initialization of libraries for lua.c | 3 | ** Initialization of libraries for lua.c |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -32,6 +32,8 @@ LUALIB_API int luaopen_stdlibs (lua_State *L) { · 26 unmodified lines | |||
| 32 | for (; lib->func; lib++) { | 32 | for (; lib->func; lib++) { |
| 33 | lib->func(L); /* open library */ | 33 | lib->func(L); /* open library */ |
| 34 | lua_settop(L, 0); /* discard any results */ | 34 | lua_settop(L, 0); /* discard any results */ |
| 35 | lua_pushvalue(L, LUA_GLOBALSINDEX); | ||
| 36 | lua_replace(L, LUA_ENVIRONINDEX); /* restore environment */ | ||
| 35 | } | 37 | } |
| 36 | return 0; | 38 | return 0; |
| 37 | } | 39 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 2.57 2004/08/13 19:52:13 roberto Exp $ | 2 | ** $Id: liolib.c,v 2.58 2005/02/18 12:40:02 roberto Exp $ |
| 3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -105,8 +105,8 @@ static int aux_close (lua_State *L) { · 99 unmodified lines | |||
| 105 | 105 | ||
| 106 | 106 | ||
| 107 | static int io_close (lua_State *L) { | 107 | static int io_close (lua_State *L) { |
| 108 | if (lua_isnone(L, 1) && lua_type(L, lua_upvalueindex(1)) == LUA_TTABLE) | 108 | if (lua_isnone(L, 1)) |
| 109 | lua_rawgeti(L, lua_upvalueindex(1), IO_OUTPUT); | 109 | lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT); |
| 110 | return pushresult(L, aux_close(L), NULL); | 110 | return pushresult(L, aux_close(L), NULL); |
| 111 | } | 111 | } |
| 112 | 112 | ||
| @@ -147,7 +147,7 @@ static int io_tmpfile (lua_State *L) { · 34 unmodified lines | |||
| 147 | 147 | ||
| 148 | static FILE *getiofile (lua_State *L, int findex) { | 148 | static FILE *getiofile (lua_State *L, int findex) { |
| 149 | FILE *f; | 149 | FILE *f; |
| 150 | lua_rawgeti(L, lua_upvalueindex(1), findex); | 150 | lua_rawgeti(L, LUA_ENVIRONINDEX, findex); |
| 151 | lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE)); | 151 | lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE)); |
| 152 | f = *(FILE **)lua_touserdata(L, -1); | 152 | f = *(FILE **)lua_touserdata(L, -1); |
| 153 | if (f == NULL) | 153 | if (f == NULL) |
| @@ -170,10 +170,10 @@ static int g_iofile (lua_State *L, int f, const char *mode) { · 16 unmodified lines | |||
| 170 | lua_pushvalue(L, 1); | 170 | lua_pushvalue(L, 1); |
| 171 | } | 171 | } |
| 172 | lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE)); | 172 | lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE)); |
| 173 | lua_rawseti(L, lua_upvalueindex(1), f); | 173 | lua_rawseti(L, LUA_ENVIRONINDEX, f); |
| 174 | } | 174 | } |
| 175 | /* return current value */ | 175 | /* return current value */ |
| 176 | lua_rawgeti(L, lua_upvalueindex(1), f); | 176 | lua_rawgeti(L, LUA_ENVIRONINDEX, f); |
| 177 | return 1; | 177 | return 1; |
| 178 | } | 178 | } |
| 179 | 179 | ||
| @@ -192,10 +192,9 @@ static int io_readline (lua_State *L); · 12 unmodified lines | |||
| 192 | 192 | ||
| 193 | 193 | ||
| 194 | static void aux_lines (lua_State *L, int idx, int close) { | 194 | static void aux_lines (lua_State *L, int idx, int close) { |
| 195 | lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); | ||
| 196 | lua_pushvalue(L, idx); | 195 | lua_pushvalue(L, idx); |
| 197 | lua_pushboolean(L, close); /* close/not close file when finished */ | 196 | lua_pushboolean(L, close); /* close/not close file when finished */ |
| 198 | lua_pushcclosure(L, io_readline, 3); | 197 | lua_pushcclosure(L, io_readline, 2); |
| 199 | } | 198 | } |
| 200 | 199 | ||
| 201 | 200 | ||
| @@ -209,7 +208,7 @@ static int f_lines (lua_State *L) { · 7 unmodified lines | |||
| 209 | static int io_lines (lua_State *L) { | 208 | static int io_lines (lua_State *L) { |
| 210 | if (lua_isnoneornil(L, 1)) { /* no arguments? */ | 209 | if (lua_isnoneornil(L, 1)) { /* no arguments? */ |
| 211 | /* will iterate over default input */ | 210 | /* will iterate over default input */ |
| 212 | lua_rawgeti(L, lua_upvalueindex(1), IO_INPUT); | 211 | lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT); |
| 213 | return f_lines(L); | 212 | return f_lines(L); |
| 214 | } | 213 | } |
| 215 | else { | 214 | else { |
| @@ -349,7 +348,7 @@ static int f_read (lua_State *L) { · 133 unmodified lines | |||
| 349 | 348 | ||
| 350 | 349 | ||
| 351 | static int io_readline (lua_State *L) { | 350 | static int io_readline (lua_State *L) { |
| 352 | FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(2)); | 351 | FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1)); |
| 353 | int sucess; | 352 | int sucess; |
| 354 | if (f == NULL) /* file is already closed? */ | 353 | if (f == NULL) /* file is already closed? */ |
| 355 | luaL_error(L, "file is already closed"); | 354 | luaL_error(L, "file is already closed"); |
| @@ -358,9 +357,9 @@ static int io_readline (lua_State *L) { · 2 unmodified lines | |||
| 358 | luaL_error(L, "%s", strerror(errno)); | 357 | luaL_error(L, "%s", strerror(errno)); |
| 359 | if (sucess) return 1; | 358 | if (sucess) return 1; |
| 360 | else { /* EOF */ | 359 | else { /* EOF */ |
| 361 | if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ | 360 | if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */ |
| 362 | lua_settop(L, 0); | 361 | lua_settop(L, 0); |
| 363 | lua_pushvalue(L, lua_upvalueindex(2)); | 362 | lua_pushvalue(L, lua_upvalueindex(1)); |
| 364 | aux_close(L); /* close it */ | 363 | aux_close(L); /* close it */ |
| 365 | } | 364 | } |
| 366 | return 0; | 365 | return 0; |
| @@ -489,12 +488,13 @@ LUALIB_API int luaopen_io (lua_State *L) { · 122 unmodified lines | |||
| 489 | createmeta(L); | 488 | createmeta(L); |
| 490 | createupval(L); | 489 | createupval(L); |
| 491 | lua_pushvalue(L, -1); | 490 | lua_pushvalue(L, -1); |
| 492 | luaL_openlib(L, LUA_IOLIBNAME, iolib, 1); | 491 | lua_replace(L, LUA_ENVIRONINDEX); |
| 492 | luaL_openlib(L, LUA_IOLIBNAME, iolib, 0); | ||
| 493 | /* put predefined file handles into `io' table */ | 493 | /* put predefined file handles into `io' table */ |
| 494 | lua_rawgeti(L, -2, IO_INPUT); /* get current input from metatable */ | 494 | lua_rawgeti(L, -2, IO_INPUT); /* get current input from upval */ |
| 495 | lua_setfield(L, -2, "stdin"); /* io.stdin = metatable[IO_INPUT] */ | 495 | lua_setfield(L, -2, "stdin"); /* io.stdin = upval[IO_INPUT] */ |
| 496 | lua_rawgeti(L, -2, IO_OUTPUT); /* get current output from metatable */ | 496 | lua_rawgeti(L, -2, IO_OUTPUT); /* get current output from upval */ |
| 497 | lua_setfield(L, -2, "stdout"); /* io.stdout = metatable[IO_OUTPUT] */ | 497 | lua_setfield(L, -2, "stdout"); /* io.stdout = upval[IO_OUTPUT] */ |
| 498 | *newfile(L) = stderr; | 498 | *newfile(L) = stderr; |
| 499 | lua_setfield(L, -2, "stderr"); /* io.stderr = newfile(stderr) */ | 499 | lua_setfield(L, -2, "stderr"); /* io.stderr = newfile(stderr) */ |
| 500 | return 1; | 500 | return 1; |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: llimits.h,v 1.62 2004/12/13 12:15:11 roberto Exp $ | 2 | ** $Id: llimits.h,v 1.63 2005/01/14 14:19:42 roberto Exp $ |
| 3 | ** Limits, basic types, and some other `installation-dependent' definitions | 3 | ** Limits, basic types, and some other `installation-dependent' definitions |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -73,8 +73,6 @@ typedef LUA_UACNUMBER l_uacNumber; · 67 unmodified lines | |||
| 73 | typedef lu_int32 Instruction; | 73 | typedef lu_int32 Instruction; |
| 74 | 74 | ||
| 75 | 75 | ||
| 76 | /* divisor for GC pace */ | ||
| 77 | #define GCDIV 8 | ||
| 78 | 76 | ||
| 79 | /* maximum stack for a Lua function */ | 77 | /* maximum stack for a Lua function */ |
| 80 | #define MAXSTACK 250 | 78 | #define MAXSTACK 250 |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmathlib.c,v 1.61 2004/05/10 18:11:32 roberto Exp $ | 2 | ** $Id: lmathlib.c,v 1.62 2005/01/07 20:00:33 roberto Exp $ |
| 3 | ** Standard mathematical library | 3 | ** Standard mathematical library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -225,8 +225,6 @@ LUALIB_API int luaopen_math (lua_State *L) { · 219 unmodified lines | |||
| 225 | luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0); | 225 | luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0); |
| 226 | lua_pushnumber(L, PI); | 226 | lua_pushnumber(L, PI); |
| 227 | lua_setfield(L, -2, "pi"); | 227 | lua_setfield(L, -2, "pi"); |
| 228 | lua_pushcfunction(L, math_pow); | ||
| 229 | lua_setglobal(L, "__pow"); | ||
| 230 | return 1; | 228 | return 1; |
| 231 | } | 229 | } |
| 232 | 230 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmem.c,v 1.67 2004/12/01 15:46:18 roberto Exp $ | 2 | ** $Id: lmem.c,v 1.69 2005/02/23 17:30:22 roberto Exp $ |
| 3 | ** Interface to Memory Manager | 3 | ** Interface to Memory Manager |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -22,19 +22,19 @@ · 16 unmodified lines | |||
| 22 | 22 | ||
| 23 | /* | 23 | /* |
| 24 | ** About the realloc function: | 24 | ** About the realloc function: |
| 25 | ** void * realloc (void *ud, void *ptr, size_t osize, size_t nsize); | 25 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); |
| 26 | ** (`osize' is the old size, `nsize' is the new size) | 26 | ** (`osize' is the old size, `nsize' is the new size) |
| 27 | ** | 27 | ** |
| 28 | ** Lua ensures that (ptr == NULL) iff (osize == 0). | 28 | ** Lua ensures that (ptr == NULL) iff (osize == 0). |
| 29 | ** | 29 | ** |
| 30 | ** * realloc(ud, NULL, 0, x) creates a new block of size `x' | 30 | ** * frealloc(ud, NULL, 0, x) creates a new block of size `x' |
| 31 | ** | 31 | ** |
| 32 | ** * realloc(ud, p, x, 0) frees the block `p' | 32 | ** * frealloc(ud, p, x, 0) frees the block `p' |
| 33 | ** (in this specific case, realloc must return NULL). | 33 | ** (in this specific case, frealloc must return NULL). |
| 34 | ** particularly, realloc(ud, NULL, 0, 0) does nothing | 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing |
| 35 | ** (which is equivalent to free(NULL) in ANSI C) | 35 | ** (which is equivalent to free(NULL) in ANSI C) |
| 36 | ** | 36 | ** |
| 37 | ** realloc returns NULL if it cannot create or reallocate the area | 37 | ** frealloc returns NULL if it cannot create or reallocate the area |
| 38 | ** (any reallocation to an equal or smaller size cannot fail!) | 38 | ** (any reallocation to an equal or smaller size cannot fail!) |
| 39 | */ | 39 | */ |
| 40 | 40 | ||
| @@ -76,11 +76,27 @@ void *luaM_toobig (lua_State *L) { · 35 unmodified lines | |||
| 76 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { | 76 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { |
| 77 | global_State *g = G(L); | 77 | global_State *g = G(L); |
| 78 | lua_assert((osize == 0) == (block == NULL)); | 78 | lua_assert((osize == 0) == (block == NULL)); |
| 79 | block = (*g->realloc)(g->ud, block, osize, nsize); | 79 | block = (*g->frealloc)(g->ud, block, osize, nsize); |
| 80 | if (block == NULL && nsize > 0) | 80 | if (block == NULL && nsize > 0) |
| 81 | luaD_throw(L, LUA_ERRMEM); | 81 | luaD_throw(L, LUA_ERRMEM); |
| 82 | lua_assert((nsize == 0) == (block == NULL)); | 82 | lua_assert((nsize == 0) == (block == NULL)); |
| 83 | g->totalbytes = (g->totalbytes - osize) + nsize; | 83 | g->totalbytes = (g->totalbytes - osize) + nsize; |
| 84 | #if 0 | ||
| 85 | { /* auxiliar patch to monitor garbage collection */ | ||
| 86 | static unsigned long total = 0; /* our "time" */ | ||
| 87 | static lu_mem last = 0; /* last totalmem that generated an output */ | ||
| 88 | static FILE *f = NULL; /* output file */ | ||
| 89 | if (nsize <= osize) total += 1; /* "time" always grow */ | ||
| 90 | else total += (nsize - osize); | ||
| 91 | if ((int)g->totalbytes - (int)last > 1000 || | ||
| 92 | (int)g->totalbytes - (int)last < -1000) { | ||
| 93 | last = g->totalbytes; | ||
| 94 | if (f == NULL) f = fopen("trace", "w"); | ||
| 95 | fprintf(f, "%lu %u %u %u %d\n", total, g->totalbytes, g->GCthreshold, | ||
| 96 | g->estimate, g->gcstate); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | #endif | ||
| 84 | return block; | 100 | return block; |
| 85 | } | 101 | } |
| 86 | 102 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: loadlib.c,v 1.15 2004/12/29 18:56:34 roberto Exp $ | 2 | ** $Id: loadlib.c,v 1.18 2005/02/28 15:58:48 roberto Exp $ |
| 3 | ** Dynamic library loader for Lua | 3 | ** Dynamic library loader for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | * | 5 | * |
| @@ -293,45 +293,60 @@ static int ll_loadlib (lua_State *L) { · 287 unmodified lines | |||
| 293 | */ | 293 | */ |
| 294 | 294 | ||
| 295 | 295 | ||
| 296 | static const char *loadLua (lua_State *L, const char *fname, const char *name) { | 296 | static int loader_Lua (lua_State *L) { |
| 297 | const char *name = luaL_checkstring(L, 1); | ||
| 298 | const char *fname = luaL_gsub(L, name, ".", LUA_DIRSEP); | ||
| 297 | const char *path; | 299 | const char *path; |
| 298 | /* try first `LUA_PATH' for compatibility */ | 300 | /* try first `LUA_PATH' for compatibility */ |
| 299 | lua_getglobal(L, "LUA_PATH"); | 301 | lua_pushstring(L, "LUA_PATH"); |
| 302 | lua_rawget(L, LUA_GLOBALSINDEX); | ||
| 300 | path = lua_tostring(L, -1); | 303 | path = lua_tostring(L, -1); |
| 301 | if (!path) { | 304 | if (!path) { |
| 302 | lua_pop(L, 1); | 305 | lua_pop(L, 1); |
| 303 | luaL_getfield(L, LUA_GLOBALSINDEX, "package.path"); | 306 | lua_getfield(L, LUA_ENVIRONINDEX, "path"); |
| 304 | path = lua_tostring(L, -1); | 307 | path = lua_tostring(L, -1); |
| 305 | } | 308 | } |
| 306 | if (path == NULL) | 309 | if (path == NULL) |
| 307 | luaL_error(L, "`package.path' must be a string"); | 310 | luaL_error(L, "`package.path' must be a string"); |
| 308 | fname = luaL_searchpath(L, fname, path); | 311 | fname = luaL_searchpath(L, fname, path); |
| 309 | if (fname == NULL) return path; /* library not found in this path */ | 312 | if (fname == NULL) return 0; /* library not found in this path */ |
| 310 | if (luaL_loadfile(L, fname) != 0) | 313 | if (luaL_loadfile(L, fname) != 0) |
| 311 | luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -1)); | 314 | luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -1)); |
| 312 | return NULL; /* library loaded successfully */ | 315 | return 1; /* library loaded successfully */ |
| 313 | } | 316 | } |
| 314 | 317 | ||
| 315 | 318 | ||
| 316 | static const char *loadC (lua_State *L, const char *fname, const char *name) { | 319 | static int loader_C (lua_State *L) { |
| 320 | const char *name = luaL_checkstring(L, 1); | ||
| 321 | const char *fname = luaL_gsub(L, name, ".", LUA_DIRSEP); | ||
| 317 | const char *path; | 322 | const char *path; |
| 318 | const char *funcname; | 323 | const char *funcname; |
| 319 | luaL_getfield(L, LUA_GLOBALSINDEX, "package.cpath"); | 324 | lua_getfield(L, LUA_ENVIRONINDEX, "cpath"); |
| 320 | path = lua_tostring(L, -1); | 325 | path = lua_tostring(L, -1); |
| 321 | if (path == NULL) | 326 | if (path == NULL) |
| 322 | luaL_error(L, "`package.cpath' must be a string"); | 327 | luaL_error(L, "`package.cpath' must be a string"); |
| 323 | fname = luaL_searchpath(L, fname, path); | 328 | fname = luaL_searchpath(L, fname, path); |
| 324 | if (fname == NULL) return path; /* library not found in this path */ | 329 | if (fname == NULL) return 0; /* library not found in this path */ |
| 325 | funcname = luaL_gsub(L, name, ".", LUA_OFSEP); | 330 | funcname = luaL_gsub(L, name, ".", LUA_OFSEP); |
| 326 | funcname = lua_pushfstring(L, "%s%s", POF, funcname); | 331 | funcname = lua_pushfstring(L, "%s%s", POF, funcname); |
| 327 | if (ll_loadfunc(L, fname, funcname) != 1) | 332 | if (ll_loadfunc(L, fname, funcname) != 1) |
| 328 | luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -2)); | 333 | luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -2)); |
| 329 | return NULL; /* library loaded successfully */ | 334 | return 1; /* library loaded successfully */ |
| 335 | } | ||
| 336 | |||
| 337 | |||
| 338 | static int loader_preload (lua_State *L) { | ||
| 339 | lua_getfield(L, LUA_ENVIRONINDEX, "preload"); | ||
| 340 | if (!lua_istable(L, -1)) | ||
| 341 | luaL_error(L, "`package.preload' must be a table"); | ||
| 342 | lua_getfield(L, -1, luaL_checkstring(L, 1)); | ||
| 343 | return 1; | ||
| 330 | } | 344 | } |
| 331 | 345 | ||
| 332 | 346 | ||
| 333 | static int ll_require (lua_State *L) { | 347 | static int ll_require (lua_State *L) { |
| 334 | const char *name = luaL_checkstring(L, 1); | 348 | const char *name = luaL_checkstring(L, 1); |
| 349 | int i; | ||
| 335 | lua_settop(L, 1); | 350 | lua_settop(L, 1); |
| 336 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | 351 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 337 | lua_getfield(L, 2, name); | 352 | lua_getfield(L, 2, name); |
| @@ -340,22 +355,18 @@ static int ll_require (lua_State *L) { · 2 unmodified lines | |||
| 340 | /* else must load it; first mark it as loaded */ | 355 | /* else must load it; first mark it as loaded */ |
| 341 | lua_pushboolean(L, 1); | 356 | lua_pushboolean(L, 1); |
| 342 | lua_setfield(L, 2, name); /* _LOADED[name] = true */ | 357 | lua_setfield(L, 2, name); /* _LOADED[name] = true */ |
| 343 | /* check whether it is preloaded */ | 358 | /* iterate over available loaders */ |
| 344 | lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); | 359 | lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); |
| 345 | lua_getfield(L, -1, name); | 360 | if (!lua_istable(L, -1)) |
| 346 | if (lua_isnil(L, -1)) { /* no preload function for that module? */ | 361 | luaL_error(L, "`package.loaders' must be a table"); |
| 347 | const char *fname = luaL_gsub(L, name, ".", LUA_DIRSEP); | 362 | for (i=1;; i++) { |
| 348 | const char *cpath = loadC(L, fname, name); /* try a C module */ | 363 | lua_rawgeti(L, -1, i); /* get a loader */ |
| 349 | if (cpath) { /* not found? */ | 364 | if (lua_isnil(L, -1)) |
| 350 | const char *path = loadLua(L, fname, name); /* try a Lua module */ | 365 | return luaL_error(L, "package `%s' not found", name); |
| 351 | if (path) { /* yet not found? */ | 366 | lua_pushstring(L, name); |
| 352 | lua_pushnil(L); | 367 | lua_call(L, 1, 1); /* call it */ |
| 353 | lua_setfield(L, 2, name); /* unmark _LOADED[name] */ | 368 | if (lua_isnil(L, -1)) lua_pop(L, 1); |
| 354 | return luaL_error(L, "package `%s' not found\n" | 369 | else break; /* module loaded successfully */ |
| 355 | " cpath: %s\n path: %s", | ||
| 356 | name, cpath, path); | ||
| 357 | } | ||
| 358 | } | ||
| 359 | } | 370 | } |
| 360 | lua_pushvalue(L, 1); /* pass name as argument to module */ | 371 | lua_pushvalue(L, 1); /* pass name as argument to module */ |
| 361 | lua_call(L, 1, 1); /* run loaded module */ | 372 | lua_call(L, 1, 1); /* run loaded module */ |
| @@ -430,9 +441,13 @@ static const luaL_reg ll_funcs[] = { · 68 unmodified lines | |||
| 430 | }; | 441 | }; |
| 431 | 442 | ||
| 432 | 443 | ||
| 444 | static const lua_CFunction loaders[] = | ||
| 445 | {loader_preload, loader_C, loader_Lua, NULL}; | ||
| 446 | |||
| 433 | 447 | ||
| 434 | LUALIB_API int luaopen_loadlib (lua_State *L) { | 448 | LUALIB_API int luaopen_loadlib (lua_State *L) { |
| 435 | const char *path; | 449 | const char *path; |
| 450 | int i; | ||
| 436 | /* create new type _LOADLIB */ | 451 | /* create new type _LOADLIB */ |
| 437 | luaL_newmetatable(L, "_LOADLIB"); | 452 | luaL_newmetatable(L, "_LOADLIB"); |
| 438 | lua_pushcfunction(L, gctm); | 453 | lua_pushcfunction(L, gctm); |
| @@ -441,6 +456,19 @@ LUALIB_API int luaopen_loadlib (lua_State *L) { · 2 unmodified lines | |||
| 441 | lua_newtable(L); | 456 | lua_newtable(L); |
| 442 | lua_pushvalue(L, -1); | 457 | lua_pushvalue(L, -1); |
| 443 | lua_setglobal(L, "package"); | 458 | lua_setglobal(L, "package"); |
| 459 | lua_pushvalue(L, -1); | ||
| 460 | lua_setfield(L, LUA_REGISTRYINDEX, "_PACKAGE"); | ||
| 461 | lua_pushvalue(L, -1); | ||
| 462 | lua_replace(L, LUA_ENVIRONINDEX); | ||
| 463 | /* create `loaders' table */ | ||
| 464 | lua_newtable(L); | ||
| 465 | /* fill it with pre-defined loaders */ | ||
| 466 | for (i=0; loaders[i] != NULL; i++) { | ||
| 467 | lua_pushcfunction(L, loaders[i]); | ||
| 468 | lua_rawseti(L, -2, i+1); | ||
| 469 | } | ||
| 470 | /* put it in field `loaders' */ | ||
| 471 | lua_setfield(L, -2, "loaders"); | ||
| 444 | /* set field `path' */ | 472 | /* set field `path' */ |
| 445 | path = getenv(LUA_PATH); | 473 | path = getenv(LUA_PATH); |
| 446 | if (path == NULL) path = LUA_PATH_DEFAULT; | 474 | if (path == NULL) path = LUA_PATH_DEFAULT; |
| @@ -455,7 +483,7 @@ LUALIB_API int luaopen_loadlib (lua_State *L) { · 8 unmodified lines | |||
| 455 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | 483 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 456 | lua_setfield(L, -2, "loaded"); | 484 | lua_setfield(L, -2, "loaded"); |
| 457 | /* set field `preload' */ | 485 | /* set field `preload' */ |
| 458 | lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); | 486 | lua_newtable(L); |
| 459 | lua_setfield(L, -2, "preload"); | 487 | lua_setfield(L, -2, "preload"); |
| 460 | lua_pushvalue(L, LUA_GLOBALSINDEX); | 488 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 461 | luaL_openlib(L, NULL, ll_funcs, 0); /* open lib into global table */ | 489 | luaL_openlib(L, NULL, ll_funcs, 0); /* open lib into global table */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.c,v 2.7 2004/11/24 19:16:03 roberto Exp $ | 2 | ** $Id: lobject.c,v 2.8 2005/01/10 18:17:39 roberto Exp $ |
| 3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -73,7 +73,7 @@ int luaO_rawequalObj (const TValue *t1, const TValue *t2) { · 67 unmodified lines | |||
| 73 | case LUA_TNIL: | 73 | case LUA_TNIL: |
| 74 | return 1; | 74 | return 1; |
| 75 | case LUA_TNUMBER: | 75 | case LUA_TNUMBER: |
| 76 | return nvalue(t1) == nvalue(t2); | 76 | return num_eq(nvalue(t1), nvalue(t2)); |
| 77 | case LUA_TBOOLEAN: | 77 | case LUA_TBOOLEAN: |
| 78 | return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ | 78 | return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ |
| 79 | case LUA_TLIGHTUSERDATA: | 79 | case LUA_TLIGHTUSERDATA: |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.h,v 2.8 2004/12/04 18:10:22 roberto Exp $ | 2 | ** $Id: lobject.h,v 2.11 2005/02/18 12:40:02 roberto Exp $ |
| 3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -218,6 +218,7 @@ typedef union Udata { · 212 unmodified lines | |||
| 218 | struct { | 218 | struct { |
| 219 | CommonHeader; | 219 | CommonHeader; |
| 220 | struct Table *metatable; | 220 | struct Table *metatable; |
| 221 | struct Table *env; | ||
| 221 | size_t len; | 222 | size_t len; |
| 222 | } uv; | 223 | } uv; |
| 223 | } Udata; | 224 | } Udata; |
| @@ -271,7 +272,13 @@ typedef struct LocVar { · 47 unmodified lines | |||
| 271 | typedef struct UpVal { | 272 | typedef struct UpVal { |
| 272 | CommonHeader; | 273 | CommonHeader; |
| 273 | TValue *v; /* points to stack or to its own value */ | 274 | TValue *v; /* points to stack or to its own value */ |
| 274 | TValue value; /* the value (when closed) */ | 275 | union { |
| 276 | TValue value; /* the value (when closed) */ | ||
| 277 | struct { /* double linked list (when open) */ | ||
| 278 | struct UpVal *prev; | ||
| 279 | struct UpVal *next; | ||
| 280 | } l; | ||
| 281 | } u; | ||
| 275 | } UpVal; | 282 | } UpVal; |
| 276 | 283 | ||
| 277 | 284 | ||
| @@ -280,7 +287,8 @@ typedef struct UpVal { · 2 unmodified lines | |||
| 280 | */ | 287 | */ |
| 281 | 288 | ||
| 282 | #define ClosureHeader \ | 289 | #define ClosureHeader \ |
| 283 | CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist | 290 | CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \ |
| 291 | struct Table *env | ||
| 284 | 292 | ||
| 285 | typedef struct CClosure { | 293 | typedef struct CClosure { |
| 286 | ClosureHeader; | 294 | ClosureHeader; |
| @@ -292,7 +300,6 @@ typedef struct CClosure { · 5 unmodified lines | |||
| 292 | typedef struct LClosure { | 300 | typedef struct LClosure { |
| 293 | ClosureHeader; | 301 | ClosureHeader; |
| 294 | struct Proto *p; | 302 | struct Proto *p; |
| 295 | TValue g; /* global table for this closure */ | ||
| 296 | UpVal *upvals[1]; | 303 | UpVal *upvals[1]; |
| 297 | } LClosure; | 304 | } LClosure; |
| 298 | 305 | ||
| @@ -330,7 +337,7 @@ typedef struct Table { · 31 unmodified lines | |||
| 330 | struct Table *metatable; | 337 | struct Table *metatable; |
| 331 | TValue *array; /* array part */ | 338 | TValue *array; /* array part */ |
| 332 | Node *node; | 339 | Node *node; |
| 333 | Node *firstfree; /* this position is free; all positions after it are full */ | 340 | Node *lastfree; /* any free position is before this position */ |
| 334 | GCObject *gclist; | 341 | GCObject *gclist; |
| 335 | int sizearray; /* size of `array' array */ | 342 | int sizearray; /* size of `array' array */ |
| 336 | } Table; | 343 | } Table; |
| @@ -351,6 +358,8 @@ typedef struct Table { · 14 unmodified lines | |||
| 351 | 358 | ||
| 352 | extern const TValue luaO_nilobject; | 359 | extern const TValue luaO_nilobject; |
| 353 | 360 | ||
| 361 | #define ceillog2(x) (luaO_log2((x)-1) + 1) | ||
| 362 | |||
| 354 | int luaO_log2 (unsigned int x); | 363 | int luaO_log2 (unsigned int x); |
| 355 | int luaO_int2fb (unsigned int x); | 364 | int luaO_int2fb (unsigned int x); |
| 356 | int luaO_fb2int (int x); | 365 | int luaO_fb2int (int x); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: loslib.c,v 1.3 2004/10/08 18:57:16 roberto Exp $ | 2 | ** $Id: loslib.c,v 1.4 2005/01/10 19:16:29 roberto Exp $ |
| 3 | ** Standard Operating System library | 3 | ** Standard Operating System library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -91,15 +91,13 @@ static int io_clock (lua_State *L) { · 85 unmodified lines | |||
| 91 | */ | 91 | */ |
| 92 | 92 | ||
| 93 | static void setfield (lua_State *L, const char *key, int value) { | 93 | static void setfield (lua_State *L, const char *key, int value) { |
| 94 | lua_pushstring(L, key); | ||
| 95 | lua_pushinteger(L, value); | 94 | lua_pushinteger(L, value); |
| 96 | lua_rawset(L, -3); | 95 | lua_setfield(L, -2, key); |
| 97 | } | 96 | } |
| 98 | 97 | ||
| 99 | static void setboolfield (lua_State *L, const char *key, int value) { | 98 | static void setboolfield (lua_State *L, const char *key, int value) { |
| 100 | lua_pushstring(L, key); | ||
| 101 | lua_pushboolean(L, value); | 99 | lua_pushboolean(L, value); |
| 102 | lua_rawset(L, -3); | 100 | lua_setfield(L, -2, key); |
| 103 | } | 101 | } |
| 104 | 102 | ||
| 105 | static int getboolfield (lua_State *L, const char *key) { | 103 | static int getboolfield (lua_State *L, const char *key) { |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lparser.c,v 2.11 2004/12/07 18:31:16 roberto Exp $ | 2 | ** $Id: lparser.c,v 2.13 2005/01/05 18:20:51 roberto Exp $ |
| 3 | ** Lua Parser | 3 | ** Lua Parser |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -547,7 +547,7 @@ static void constructor (LexState *ls, expdesc *t) { · 541 unmodified lines | |||
| 547 | check_match(ls, '}', '{', line); | 547 | check_match(ls, '}', '{', line); |
| 548 | lastlistfield(fs, &cc); | 548 | lastlistfield(fs, &cc); |
| 549 | SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ | 549 | SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ |
| 550 | SETARG_C(fs->f->code[pc], luaO_log2(cc.nh)+1); /* set initial table size */ | 550 | SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ |
| 551 | } | 551 | } |
| 552 | 552 | ||
| 553 | /* }====================================================================== */ | 553 | /* }====================================================================== */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstate.c,v 2.19 2004/12/13 12:15:11 roberto Exp $ | 2 | ** $Id: lstate.c,v 2.25 2005/02/23 17:30:22 roberto Exp $ |
| 3 | ** Global State | 3 | ** Global State |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -76,33 +76,22 @@ static void freestack (lua_State *L, lua_State *L1) { · 70 unmodified lines | |||
| 76 | ** open parts that may cause memory-allocation errors | 76 | ** open parts that may cause memory-allocation errors |
| 77 | */ | 77 | */ |
| 78 | static void f_luaopen (lua_State *L, void *ud) { | 78 | static void f_luaopen (lua_State *L, void *ud) { |
| 79 | Udata *u; /* head of udata list */ | ||
| 80 | global_State *g = G(L); | 79 | global_State *g = G(L); |
| 81 | UNUSED(ud); | 80 | UNUSED(ud); |
| 82 | u = luaM_new(L, Udata); | ||
| 83 | u->uv.len = 0; | ||
| 84 | u->uv.metatable = NULL; | ||
| 85 | g->firstudata = obj2gco(u); | ||
| 86 | luaC_link(L, obj2gco(u), LUA_TUSERDATA); | ||
| 87 | setbit(u->uv.marked, FIXEDBIT); | ||
| 88 | setbit(L->marked, FIXEDBIT); | ||
| 89 | stack_init(L, L); /* init stack */ | 81 | stack_init(L, L); /* init stack */ |
| 90 | sethvalue(L, gt(L), luaH_new(L, 0, 4)); /* table of globals */ | 82 | sethvalue(L, gt(L), luaH_new(L, 0, 20)); /* table of globals */ |
| 91 | hvalue(gt(L))->metatable = luaH_new(L, 0, 0); /* globals metatable */ | 83 | hvalue(gt(L))->metatable = luaH_new(L, 0, 0); /* globals metatable */ |
| 92 | sethvalue(L, registry(L), luaH_new(L, 4, 4)); /* registry */ | 84 | sethvalue(L, registry(L), luaH_new(L, 6, 20)); /* registry */ |
| 93 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ | 85 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ |
| 94 | luaT_init(L); | 86 | luaT_init(L); |
| 95 | luaX_init(L); | 87 | luaX_init(L); |
| 96 | luaS_fix(luaS_newliteral(L, MEMERRMSG)); | 88 | luaS_fix(luaS_newliteral(L, MEMERRMSG)); |
| 97 | g->GCthreshold = 4*g->totalbytes; | 89 | g->GCthreshold = 4*g->totalbytes; |
| 98 | g->prevestimate = g->estimate = g->totalbytes; | ||
| 99 | } | 90 | } |
| 100 | 91 | ||
| 101 | 92 | ||
| 102 | static void preinit_state (lua_State *L, global_State *g) { | 93 | static void preinit_state (lua_State *L, global_State *g) { |
| 103 | L->l_G = g; | 94 | L->l_G = g; |
| 104 | L->tt = LUA_TTHREAD; | ||
| 105 | L->marked = luaC_white(g); | ||
| 106 | L->stack = NULL; | 95 | L->stack = NULL; |
| 107 | L->stacksize = 0; | 96 | L->stacksize = 0; |
| 108 | L->errorJmp = NULL; | 97 | L->errorJmp = NULL; |
| @@ -125,20 +114,19 @@ static void close_state (lua_State *L) { · 16 unmodified lines | |||
| 125 | global_State *g = G(L); | 114 | global_State *g = G(L); |
| 126 | luaF_close(L, L->stack); /* close all upvalues for this thread */ | 115 | luaF_close(L, L->stack); /* close all upvalues for this thread */ |
| 127 | luaC_freeall(L); /* collect all objects */ | 116 | luaC_freeall(L); /* collect all objects */ |
| 128 | lua_assert(g->rootgc == NULL); | 117 | lua_assert(g->rootgc == obj2gco(L)); |
| 129 | lua_assert(g->strt.nuse == 0); | 118 | lua_assert(g->strt.nuse == 0); |
| 130 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); | 119 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); |
| 131 | luaZ_freebuffer(L, &g->buff); | 120 | luaZ_freebuffer(L, &g->buff); |
| 132 | freestack(L, L); | 121 | freestack(L, L); |
| 133 | lua_assert(g->totalbytes == sizeof(LG)); | 122 | lua_assert(g->totalbytes == sizeof(LG)); |
| 134 | (*g->realloc)(g->ud, fromstate(L), state_size(LG), 0); | 123 | (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); |
| 135 | } | 124 | } |
| 136 | 125 | ||
| 137 | 126 | ||
| 138 | lua_State *luaE_newthread (lua_State *L) { | 127 | lua_State *luaE_newthread (lua_State *L) { |
| 139 | lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); | 128 | lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); |
| 140 | L1->next = L->next; /* link new thread after `L' */ | 129 | luaC_link(L, obj2gco(L1), LUA_TTHREAD); |
| 141 | L->next = obj2gco(L1); | ||
| 142 | preinit_state(L1, G(L)); | 130 | preinit_state(L1, G(L)); |
| 143 | stack_init(L1, L); /* init stack */ | 131 | stack_init(L1, L); /* init stack */ |
| 144 | setobj2n(L, gt(L1), gt(L)); /* share table of globals */ | 132 | setobj2n(L, gt(L1), gt(L)); /* share table of globals */ |
| @@ -167,11 +155,16 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { · 22 unmodified lines | |||
| 167 | L = tostate(l); | 155 | L = tostate(l); |
| 168 | g = &((LG *)L)->g; | 156 | g = &((LG *)L)->g; |
| 169 | L->next = NULL; | 157 | L->next = NULL; |
| 170 | g->currentwhite = bitmask(WHITE0BIT); | 158 | L->tt = LUA_TTHREAD; |
| 159 | g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); | ||
| 160 | L->marked = luaC_white(g); | ||
| 161 | set2bits(L->marked, FIXEDBIT, SFIXEDBIT); | ||
| 171 | preinit_state(L, g); | 162 | preinit_state(L, g); |
| 172 | g->realloc = f; | 163 | g->frealloc = f; |
| 173 | g->ud = ud; | 164 | g->ud = ud; |
| 174 | g->mainthread = L; | 165 | g->mainthread = L; |
| 166 | g->uvhead.u.l.prev = &g->uvhead; | ||
| 167 | g->uvhead.u.l.next = &g->uvhead; | ||
| 175 | g->GCthreshold = 0; /* mark it as unfinished state */ | 168 | g->GCthreshold = 0; /* mark it as unfinished state */ |
| 176 | g->strt.size = 0; | 169 | g->strt.size = 0; |
| 177 | g->strt.nuse = 0; | 170 | g->strt.nuse = 0; |
| @@ -180,21 +173,17 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { · 2 unmodified lines | |||
| 180 | luaZ_initbuffer(L, &g->buff); | 173 | luaZ_initbuffer(L, &g->buff); |
| 181 | g->panic = NULL; | 174 | g->panic = NULL; |
| 182 | g->gcstate = GCSpause; | 175 | g->gcstate = GCSpause; |
| 183 | g->gcgenerational = 0; | ||
| 184 | g->rootgc = obj2gco(L); | 176 | g->rootgc = obj2gco(L); |
| 185 | g->sweepstrgc = 0; | 177 | g->sweepstrgc = 0; |
| 186 | g->sweepgc = &g->rootgc; | 178 | g->sweepgc = &g->rootgc; |
| 187 | g->firstudata = NULL; | ||
| 188 | g->gray = NULL; | 179 | g->gray = NULL; |
| 189 | g->grayagain = NULL; | 180 | g->grayagain = NULL; |
| 190 | g->weak = NULL; | 181 | g->weak = NULL; |
| 191 | g->tmudata = NULL; | 182 | g->tmudata = NULL; |
| 192 | setnilvalue(gkey(g->dummynode)); | ||
| 193 | setnilvalue(gval(g->dummynode)); | ||
| 194 | gnext(g->dummynode) = NULL; | ||
| 195 | g->totalbytes = sizeof(LG); | 183 | g->totalbytes = sizeof(LG); |
| 196 | g->gcpace = GCDIV; | 184 | g->gcpace = 200; /* 200% (wait memory to double before next collection) */ |
| 197 | g->incgc = 1; | 185 | g->gcstepmul = 200; /* GC runs `twice the speed' of memory allocation */ |
| 186 | g->gcdept = 0; | ||
| 198 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { | 187 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { |
| 199 | /* memory allocation error: free partial state */ | 188 | /* memory allocation error: free partial state */ |
| 200 | close_state(L); | 189 | close_state(L); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstate.h,v 2.10 2004/12/13 12:15:11 roberto Exp $ | 2 | ** $Id: lstate.h,v 2.16 2005/02/23 17:30:22 roberto Exp $ |
| 3 | ** Global State | 3 | ** Global State |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -67,30 +67,28 @@ typedef struct CallInfo { · 61 unmodified lines | |||
| 67 | */ | 67 | */ |
| 68 | typedef struct global_State { | 68 | typedef struct global_State { |
| 69 | stringtable strt; /* hash table for strings */ | 69 | stringtable strt; /* hash table for strings */ |
| 70 | lua_Alloc realloc; /* function to reallocate memory */ | 70 | lua_Alloc frealloc; /* function to reallocate memory */ |
| 71 | void *ud; /* auxiliary data to `realloc' */ | 71 | void *ud; /* auxiliary data to `frealloc' */ |
| 72 | lu_byte currentwhite; | 72 | lu_byte currentwhite; |
| 73 | lu_byte gcstate; /* state of garbage collector */ | 73 | lu_byte gcstate; /* state of garbage collector */ |
| 74 | lu_byte gcgenerational; | ||
| 75 | GCObject *rootgc; /* list of all collectable objects */ | 74 | GCObject *rootgc; /* list of all collectable objects */ |
| 76 | GCObject *firstudata; /* udata go to the end of `rootgc' */ | ||
| 77 | GCObject **sweepgc; /* position of sweep in `rootgc' */ | 75 | GCObject **sweepgc; /* position of sweep in `rootgc' */ |
| 78 | int sweepstrgc; /* position of sweep in `strt' */ | 76 | int sweepstrgc; /* position of sweep in `strt' */ |
| 79 | GCObject *gray; /* list of gray objects */ | 77 | GCObject *gray; /* list of gray objects */ |
| 80 | GCObject *grayagain; /* list of objects to be traversed atomically */ | 78 | GCObject *grayagain; /* list of objects to be traversed atomically */ |
| 81 | GCObject *weak; /* list of weak tables (to be cleared) */ | 79 | GCObject *weak; /* list of weak tables (to be cleared) */ |
| 82 | GCObject *tmudata; /* list of userdata to be GC */ | 80 | GCObject *tmudata; /* last element of list of userdata to be GC */ |
| 83 | Mbuffer buff; /* temporary buffer for string concatentation */ | 81 | Mbuffer buff; /* temporary buffer for string concatentation */ |
| 84 | lu_mem GCthreshold; | 82 | lu_mem GCthreshold; |
| 85 | lu_mem totalbytes; /* number of bytes currently allocated */ | 83 | lu_mem totalbytes; /* number of bytes currently allocated */ |
| 86 | lu_mem estimate; /* an estimate of number of bytes actually in use */ | 84 | lu_mem estimate; /* an estimate of number of bytes actually in use */ |
| 87 | lu_mem prevestimate; /* previous estimate */ | 85 | lu_mem gcdept; /* how much GC is `behind schedule' */ |
| 88 | int gcpace; /* relative `speed' of the GC */ | 86 | int gcpace; /* size of pause between successive GCs */ |
| 89 | int incgc; /* 0 if GC is done non-incrementally */ | 87 | int gcstepmul; /* GC `granularity' */ |
| 90 | lua_CFunction panic; /* to be called in unprotected errors */ | 88 | lua_CFunction panic; /* to be called in unprotected errors */ |
| 91 | TValue _registry; | 89 | TValue _registry; |
| 92 | struct lua_State *mainthread; | 90 | struct lua_State *mainthread; |
| 93 | Node dummynode[1]; /* common node array for all empty tables */ | 91 | UpVal uvhead; /* head of double-linked list of all open upvalues */ |
| 94 | TString *tmname[TM_N]; /* array with tag-method names */ | 92 | TString *tmname[TM_N]; /* array with tag-method names */ |
| 95 | } global_State; | 93 | } global_State; |
| 96 | 94 | ||
| @@ -118,6 +116,7 @@ struct lua_State { · 21 unmodified lines | |||
| 118 | int hookcount; | 116 | int hookcount; |
| 119 | lua_Hook hook; | 117 | lua_Hook hook; |
| 120 | TValue _gt; /* table of globals */ | 118 | TValue _gt; /* table of globals */ |
| 119 | TValue env; /* temporary place for environments */ | ||
| 121 | GCObject *openupval; /* list of open upvalues in this stack */ | 120 | GCObject *openupval; /* list of open upvalues in this stack */ |
| 122 | GCObject *gclist; | 121 | GCObject *gclist; |
| 123 | struct lua_longjmp *errorJmp; /* current error recover point */ | 122 | struct lua_longjmp *errorJmp; /* current error recover point */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstring.c,v 2.5 2004/11/24 19:16:03 roberto Exp $ | 2 | ** $Id: lstring.c,v 2.7 2005/02/18 12:40:02 roberto Exp $ |
| 3 | ** String table (keeps all strings handled by Lua) | 3 | ** String table (keeps all strings handled by Lua) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -93,7 +93,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { · 87 unmodified lines | |||
| 93 | } | 93 | } |
| 94 | 94 | ||
| 95 | 95 | ||
| 96 | Udata *luaS_newudata (lua_State *L, size_t s) { | 96 | Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { |
| 97 | Udata *u; | 97 | Udata *u; |
| 98 | if (s > MAX_SIZET - sizeof(Udata)) | 98 | if (s > MAX_SIZET - sizeof(Udata)) |
| 99 | luaM_toobig(L); | 99 | luaM_toobig(L); |
| @@ -102,9 +102,10 @@ Udata *luaS_newudata (lua_State *L, size_t s) { · 2 unmodified lines | |||
| 102 | u->uv.tt = LUA_TUSERDATA; | 102 | u->uv.tt = LUA_TUSERDATA; |
| 103 | u->uv.len = s; | 103 | u->uv.len = s; |
| 104 | u->uv.metatable = NULL; | 104 | u->uv.metatable = NULL; |
| 105 | /* chain it on udata list */ | 105 | u->uv.env = e; |
| 106 | u->uv.next = G(L)->firstudata->uv.next; | 106 | /* chain it on udata list (after main thread) */ |
| 107 | G(L)->firstudata->uv.next = obj2gco(u); | 107 | u->uv.next = G(L)->mainthread->next; |
| 108 | G(L)->mainthread->next = obj2gco(u); | ||
| 108 | return u; | 109 | return u; |
| 109 | } | 110 | } |
| 110 | 111 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstring.h,v 1.40 2004/11/19 15:52:40 roberto Exp $ | 2 | ** $Id: lstring.h,v 1.42 2005/02/23 17:30:22 roberto Exp $ |
| 3 | ** String table (keep all strings handled by Lua) | 3 | ** String table (keep all strings handled by Lua) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -21,10 +21,10 @@ · 15 unmodified lines | |||
| 21 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ | 21 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ |
| 22 | (sizeof(s)/sizeof(char))-1)) | 22 | (sizeof(s)/sizeof(char))-1)) |
| 23 | 23 | ||
| 24 | #define luaS_fix(s) setbit((s)->tsv.marked, FIXEDBIT) | 24 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) |
| 25 | 25 | ||
| 26 | void luaS_resize (lua_State *L, int newsize); | 26 | void luaS_resize (lua_State *L, int newsize); |
| 27 | Udata *luaS_newudata (lua_State *L, size_t s); | 27 | Udata *luaS_newudata (lua_State *L, size_t s, Table *e); |
| 28 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l); | 28 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l); |
| 29 | 29 | ||
| 30 | 30 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 2.12 2004/12/04 18:10:22 roberto Exp $ | 2 | ** $Id: ltable.c,v 2.15 2005/01/10 18:17:39 roberto Exp $ |
| 3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -68,6 +68,13 @@ · 62 unmodified lines | |||
| 68 | #define numints cast(int, sizeof(lua_Number)/sizeof(int)) | 68 | #define numints cast(int, sizeof(lua_Number)/sizeof(int)) |
| 69 | 69 | ||
| 70 | 70 | ||
| 71 | |||
| 72 | const Node luaH_dummynode = { | ||
| 73 | {{NULL}, LUA_TNIL}, /* value */ | ||
| 74 | {{NULL}, LUA_TNIL, NULL} /* key */ | ||
| 75 | }; | ||
| 76 | |||
| 77 | |||
| 71 | /* | 78 | /* |
| 72 | ** hash for lua_Numbers | 79 | ** hash for lua_Numbers |
| 73 | */ | 80 | */ |
| @@ -112,7 +119,7 @@ static int arrayindex (const TValue *key) { · 38 unmodified lines | |||
| 112 | lua_Number n = nvalue(key); | 119 | lua_Number n = nvalue(key); |
| 113 | int k; | 120 | int k; |
| 114 | lua_number2int(k, n); | 121 | lua_number2int(k, n); |
| 115 | if (cast(lua_Number, k) == nvalue(key)) | 122 | if (num_eq(cast(lua_Number, k), nvalue(key))) |
| 116 | return k; | 123 | return k; |
| 117 | } | 124 | } |
| 118 | return -1; /* `key' did not match some condition */ | 125 | return -1; /* `key' did not match some condition */ |
| @@ -176,62 +183,77 @@ int luaH_next (lua_State *L, Table *t, StkId key) { · 57 unmodified lines | |||
| 176 | */ | 183 | */ |
| 177 | 184 | ||
| 178 | 185 | ||
| 179 | static void computesizes (int nums[], int ntotal, int *narray, int *nhash) { | 186 | static int computesizes (int nums[], int *narray) { |
| 180 | int i; | 187 | int i; |
| 181 | int a = nums[0]; /* number of elements smaller than 2^i */ | 188 | int twotoi; /* 2^i */ |
| 182 | int na = a; /* number of elements to go to array part */ | 189 | int a = 0; /* number of elements smaller than 2^i */ |
| 183 | int n = (na == 0) ? -1 : 0; /* (log of) optimal size for array part */ | 190 | int na = 0; /* number of elements to go to array part */ |
| 184 | for (i = 1; a < *narray && *narray >= twoto(i-1); i++) { | 191 | int n = 0; /* optimal size for array part */ |
| 192 | for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { | ||
| 185 | if (nums[i] > 0) { | 193 | if (nums[i] > 0) { |
| 186 | a += nums[i]; | 194 | a += nums[i]; |
| 187 | if (a >= twoto(i-1)) { /* more than half elements in use? */ | 195 | if (a > twotoi/2) { /* more than half elements present? */ |
| 188 | n = i; | 196 | n = twotoi; /* optimal size (till now) */ |
| 189 | na = a; | 197 | na = a; /* all elements smaller than n will go to array part */ |
| 190 | } | 198 | } |
| 191 | } | 199 | } |
| 200 | if (a == *narray) break; /* all elements already counted */ | ||
| 201 | } | ||
| 202 | *narray = n; | ||
| 203 | lua_assert(*narray/2 <= na && na <= *narray); | ||
| 204 | return na; | ||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | static int countint (const TValue *key, int *nums) { | ||
| 209 | int k = arrayindex(key); | ||
| 210 | if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ | ||
| 211 | nums[ceillog2(k)]++; /* count as such */ | ||
| 212 | return 1; | ||
| 192 | } | 213 | } |
| 193 | lua_assert(na <= *narray && *narray <= ntotal); | 214 | else |
| 194 | *nhash = ntotal - na; | 215 | return 0; |
| 195 | *narray = (n == -1) ? 0 : twoto(n); | ||
| 196 | lua_assert(na <= *narray && na >= *narray/2); | ||
| 197 | } | 216 | } |
| 198 | 217 | ||
| 199 | 218 | ||
| 200 | static void numuse (const Table *t, int *narray, int *nhash) { | 219 | static int numusearray (const Table *t, int *nums) { |
| 201 | int nums[MAXBITS+1]; | 220 | int lg; |
| 202 | int i, lg; | 221 | int ttlg; /* 2^lg */ |
| 203 | int totaluse = 0; | 222 | int ause = 0; /* summation of `nums' */ |
| 204 | /* count elements in array part */ | 223 | int i = 1; /* count to traverse all array keys */ |
| 205 | for (i=0, lg=0; lg<=MAXBITS; lg++) { /* for each slice [2^(lg-1) to 2^lg) */ | 224 | for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */ |
| 206 | int ttlg = twoto(lg); /* 2^lg */ | 225 | int lc = 0; /* counter */ |
| 207 | if (ttlg > t->sizearray) { | 226 | int lim = ttlg; |
| 208 | ttlg = t->sizearray; | 227 | if (lim > t->sizearray) { |
| 209 | if (i >= ttlg) break; | 228 | lim = t->sizearray; /* adjust upper limit */ |
| 229 | if (i > lim) | ||
| 230 | break; /* no more elements to count */ | ||
| 210 | } | 231 | } |
| 211 | nums[lg] = 0; | 232 | /* count elements in range (2^(lg-1), 2^lg] */ |
| 212 | for (; i<ttlg; i++) { | 233 | for (; i <= lim; i++) { |
| 213 | if (!ttisnil(&t->array[i])) { | 234 | if (!ttisnil(&t->array[i-1])) |
| 214 | nums[lg]++; | 235 | lc++; |
| 215 | totaluse++; | ||
| 216 | } | ||
| 217 | } | 236 | } |
| 237 | nums[lg] += lc; | ||
| 238 | ause += lc; | ||
| 218 | } | 239 | } |
| 219 | for (; lg<=MAXBITS; lg++) nums[lg] = 0; /* reset other counts */ | 240 | return ause; |
| 220 | *narray = totaluse; /* all previous uses were in array part */ | 241 | } |
| 221 | /* count elements in hash part */ | 242 | |
| 222 | i = sizenode(t); | 243 | |
| 244 | static int numusehash (const Table *t, int *nums, int *pnasize) { | ||
| 245 | int totaluse = 0; /* total number of elements */ | ||
| 246 | int ause = 0; /* summation of `nums' */ | ||
| 247 | int i = sizenode(t); | ||
| 223 | while (i--) { | 248 | while (i--) { |
| 224 | Node *n = &t->node[i]; | 249 | Node *n = &t->node[i]; |
| 225 | if (!ttisnil(gval(n))) { | 250 | if (!ttisnil(gval(n))) { |
| 226 | int k = arrayindex(key2tval(n)); | 251 | ause += countint(key2tval(n), nums); |
| 227 | if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ | ||
| 228 | nums[luaO_log2(k-1)+1]++; /* count as such */ | ||
| 229 | (*narray)++; | ||
| 230 | } | ||
| 231 | totaluse++; | 252 | totaluse++; |
| 232 | } | 253 | } |
| 233 | } | 254 | } |
| 234 | computesizes(nums, totaluse, narray, nhash); | 255 | *pnasize += ause; |
| 256 | return totaluse; | ||
| 235 | } | 257 | } |
| 236 | 258 | ||
| 237 | 259 | ||
| @@ -244,18 +266,18 @@ static void setarrayvector (lua_State *L, Table *t, int size) { · 6 unmodified lines | |||
| 244 | } | 266 | } |
| 245 | 267 | ||
| 246 | 268 | ||
| 247 | static void setnodevector (lua_State *L, Table *t, int lsize) { | 269 | static void setnodevector (lua_State *L, Table *t, int size) { |
| 248 | int i; | 270 | int lsize; |
| 249 | int size = twoto(lsize); | 271 | if (size == 0) { /* no elements to hash part? */ |
| 250 | if (lsize > MAXBITS) | 272 | t->node = cast(Node *, &luaH_dummynode); /* use common `dummynode' */ |
| 251 | luaG_runerror(L, "table overflow"); | 273 | lsize = 0; |
| 252 | if (lsize == 0) { /* no elements to hash part? */ | ||
| 253 | t->node = G(L)->dummynode; /* use common `dummynode' */ | ||
| 254 | lua_assert(ttisnil(gkey(t->node))); /* assert invariants: */ | ||
| 255 | lua_assert(ttisnil(gval(t->node))); | ||
| 256 | lua_assert(gnext(t->node) == NULL); /* (`dummynode' must be empty) */ | ||
| 257 | } | 274 | } |
| 258 | else { | 275 | else { |
| 276 | int i; | ||
| 277 | lsize = ceillog2(size); | ||
| 278 | if (lsize > MAXBITS) | ||
| 279 | luaG_runerror(L, "table overflow"); | ||
| 280 | size = twoto(lsize); | ||
| 259 | t->node = luaM_newvector(L, size, Node); | 281 | t->node = luaM_newvector(L, size, Node); |
| 260 | for (i=0; i<size; i++) { | 282 | for (i=0; i<size; i++) { |
| 261 | gnext(&t->node[i]) = NULL; | 283 | gnext(&t->node[i]) = NULL; |
| @@ -264,31 +286,19 @@ static void setnodevector (lua_State *L, Table *t, int lsize) { · 2 unmodified lines | |||
| 264 | } | 286 | } |
| 265 | } | 287 | } |
| 266 | t->lsizenode = cast(lu_byte, lsize); | 288 | t->lsizenode = cast(lu_byte, lsize); |
| 267 | t->firstfree = gnode(t, size-1); /* first free position to be used */ | 289 | t->lastfree = gnode(t, size); /* all positions are free */ |
| 268 | } | 290 | } |
| 269 | 291 | ||
| 270 | 292 | ||
| 271 | void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { | 293 | static void resize (lua_State *L, Table *t, int nasize, int nhsize) { |
| 272 | int i; | 294 | int i; |
| 273 | int oldasize = t->sizearray; | 295 | int oldasize = t->sizearray; |
| 274 | int oldhsize = t->lsizenode; | 296 | int oldhsize = t->lsizenode; |
| 275 | Node *nold; | 297 | Node *nold = t->node; /* save old hash ... */ |
| 276 | Node temp[1]; | ||
| 277 | if (oldhsize) | ||
| 278 | nold = t->node; /* save old hash ... */ | ||
| 279 | else { /* old hash is `dummynode' */ | ||
| 280 | lua_assert(t->node == G(L)->dummynode); | ||
| 281 | temp[0] = t->node[0]; /* copy it to `temp' */ | ||
| 282 | nold = temp; | ||
| 283 | setnilvalue(gkey(G(L)->dummynode)); /* restate invariant */ | ||
| 284 | setnilvalue(gval(G(L)->dummynode)); | ||
| 285 | lua_assert(gnext(G(L)->dummynode) == NULL); | ||
| 286 | } | ||
| 287 | if (nasize > oldasize) /* array part must grow? */ | 298 | if (nasize > oldasize) /* array part must grow? */ |
| 288 | setarrayvector(L, t, nasize); | 299 | setarrayvector(L, t, nasize); |
| 289 | /* create new hash part with appropriate size */ | 300 | /* create new hash part with appropriate size */ |
| 290 | setnodevector(L, t, nhsize); | 301 | setnodevector(L, t, nhsize); |
| 291 | /* re-insert elements */ | ||
| 292 | if (nasize < oldasize) { /* array part must shrink? */ | 302 | if (nasize < oldasize) { /* array part must shrink? */ |
| 293 | t->sizearray = nasize; | 303 | t->sizearray = nasize; |
| 294 | /* re-insert elements from vanishing slice */ | 304 | /* re-insert elements from vanishing slice */ |
| @@ -299,21 +309,39 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { · 4 unmodified lines | |||
| 299 | /* shrink array */ | 309 | /* shrink array */ |
| 300 | luaM_reallocvector(L, t->array, oldasize, nasize, TValue); | 310 | luaM_reallocvector(L, t->array, oldasize, nasize, TValue); |
| 301 | } | 311 | } |
| 302 | /* re-insert elements in hash part */ | 312 | /* re-insert elements from hash part */ |
| 303 | for (i = twoto(oldhsize) - 1; i >= 0; i--) { | 313 | for (i = twoto(oldhsize) - 1; i >= 0; i--) { |
| 304 | Node *old = nold+i; | 314 | Node *old = nold+i; |
| 305 | if (!ttisnil(gval(old))) | 315 | if (!ttisnil(gval(old))) |
| 306 | setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); | 316 | setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); |
| 307 | } | 317 | } |
| 308 | if (oldhsize) | 318 | if (nold != &luaH_dummynode) |
| 309 | luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ | 319 | luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ |
| 310 | } | 320 | } |
| 311 | 321 | ||
| 312 | 322 | ||
| 313 | static void rehash (lua_State *L, Table *t) { | 323 | void luaH_resizearray (lua_State *L, Table *t, int nasize) { |
| 314 | int nasize, nhsize; | 324 | int nsize = (t->node == &luaH_dummynode) ? 0 : sizenode(t); |
| 315 | numuse(t, &nasize, &nhsize); /* compute new sizes for array and hash parts */ | 325 | resize(L, t, nasize, nsize); |
| 316 | luaH_resize(L, t, nasize, luaO_log2(nhsize)+1); | 326 | } |
| 327 | |||
| 328 | |||
| 329 | static void rehash (lua_State *L, Table *t, const TValue *ek) { | ||
| 330 | int nasize, na; | ||
| 331 | int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */ | ||
| 332 | int i; | ||
| 333 | int totaluse; | ||
| 334 | for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ | ||
| 335 | nasize = numusearray(t, nums); /* count keys in array part */ | ||
| 336 | totaluse = nasize; /* all those keys are integer keys */ | ||
| 337 | totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */ | ||
| 338 | /* count extra key */ | ||
| 339 | nasize += countint(ek, nums); | ||
| 340 | totaluse++; | ||
| 341 | /* compute new size for array part */ | ||
| 342 | na = computesizes(nums, &nasize); | ||
| 343 | /* resize the table to new computed sizes */ | ||
| 344 | resize(L, t, nasize, totaluse - na); | ||
| 317 | } | 345 | } |
| 318 | 346 | ||
| 319 | 347 | ||
| @@ -323,7 +351,7 @@ static void rehash (lua_State *L, Table *t) { · 3 unmodified lines | |||
| 323 | */ | 351 | */ |
| 324 | 352 | ||
| 325 | 353 | ||
| 326 | Table *luaH_new (lua_State *L, int narray, int lnhash) { | 354 | Table *luaH_new (lua_State *L, int narray, int nhash) { |
| 327 | Table *t = luaM_new(L, Table); | 355 | Table *t = luaM_new(L, Table); |
| 328 | luaC_link(L, obj2gco(t), LUA_TTABLE); | 356 | luaC_link(L, obj2gco(t), LUA_TTABLE); |
| 329 | t->metatable = NULL; | 357 | t->metatable = NULL; |
| @@ -332,21 +360,30 @@ Table *luaH_new (lua_State *L, int narray, int lnhash) { · 2 unmodified lines | |||
| 332 | t->array = NULL; | 360 | t->array = NULL; |
| 333 | t->sizearray = 0; | 361 | t->sizearray = 0; |
| 334 | t->lsizenode = 0; | 362 | t->lsizenode = 0; |
| 335 | t->node = NULL; | 363 | t->node = cast(Node *, &luaH_dummynode); |
| 336 | setarrayvector(L, t, narray); | 364 | setarrayvector(L, t, narray); |
| 337 | setnodevector(L, t, lnhash); | 365 | setnodevector(L, t, nhash); |
| 338 | return t; | 366 | return t; |
| 339 | } | 367 | } |
| 340 | 368 | ||
| 341 | 369 | ||
| 342 | void luaH_free (lua_State *L, Table *t) { | 370 | void luaH_free (lua_State *L, Table *t) { |
| 343 | if (t->lsizenode) | 371 | if (t->node != &luaH_dummynode) |
| 344 | luaM_freearray(L, t->node, sizenode(t), Node); | 372 | luaM_freearray(L, t->node, sizenode(t), Node); |
| 345 | luaM_freearray(L, t->array, t->sizearray, TValue); | 373 | luaM_freearray(L, t->array, t->sizearray, TValue); |
| 346 | luaM_free(L, t); | 374 | luaM_free(L, t); |
| 347 | } | 375 | } |
| 348 | 376 | ||
| 349 | 377 | ||
| 378 | static Node *getfreepos (lua_State *L, Table *t) { | ||
| 379 | while (t->lastfree-- > t->node) { | ||
| 380 | if (ttisnil(gkey(t->lastfree))) | ||
| 381 | return t->lastfree; | ||
| 382 | } | ||
| 383 | return NULL; /* could not find a free place */ | ||
| 384 | } | ||
| 385 | |||
| 386 | |||
| 350 | 387 | ||
| 351 | /* | 388 | /* |
| 352 | ** inserts a new key into a hash table; first, check whether key's main | 389 | ** inserts a new key into a hash table; first, check whether key's main |
| @@ -356,12 +393,16 @@ void luaH_free (lua_State *L, Table *t) { · 3 unmodified lines | |||
| 356 | ** position), new key goes to an empty position. | 393 | ** position), new key goes to an empty position. |
| 357 | */ | 394 | */ |
| 358 | static TValue *newkey (lua_State *L, Table *t, const TValue *key) { | 395 | static TValue *newkey (lua_State *L, Table *t, const TValue *key) { |
| 359 | TValue *val; | ||
| 360 | Node *mp = luaH_mainposition(t, key); | 396 | Node *mp = luaH_mainposition(t, key); |
| 361 | if (!ttisnil(gval(mp))) { /* main position is not free? */ | 397 | if (!ttisnil(gval(mp)) || mp == &luaH_dummynode) { |
| 362 | /* `mp' of colliding node */ | 398 | Node *othern; |
| 363 | Node *othern = luaH_mainposition(t, key2tval(mp)); | 399 | Node *n = getfreepos(L, t); /* get a free place */ |
| 364 | Node *n = t->firstfree; /* get a free place */ | 400 | if (n == NULL) { /* cannot find a free place? */ |
| 401 | rehash(L, t, key); /* grow table */ | ||
| 402 | return luaH_set(L, t, key); /* re-insert key into grown table */ | ||
| 403 | } | ||
| 404 | lua_assert(n != &luaH_dummynode); | ||
| 405 | othern = luaH_mainposition(t, key2tval(mp)); | ||
| 365 | if (othern != mp) { /* is colliding node out of its main position? */ | 406 | if (othern != mp) { /* is colliding node out of its main position? */ |
| 366 | /* yes; move colliding node into free position */ | 407 | /* yes; move colliding node into free position */ |
| 367 | while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ | 408 | while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ |
| @@ -380,19 +421,7 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) { · 12 unmodified lines | |||
| 380 | gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; | 421 | gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; |
| 381 | luaC_barriert(L, t, key); | 422 | luaC_barriert(L, t, key); |
| 382 | lua_assert(ttisnil(gval(mp))); | 423 | lua_assert(ttisnil(gval(mp))); |
| 383 | for (;;) { /* correct `firstfree' */ | 424 | return gval(mp); |
| 384 | if (ttisnil(gkey(t->firstfree))) | ||
| 385 | return gval(mp); /* OK; table still has a free place */ | ||
| 386 | else if (t->firstfree == t->node) break; /* cannot decrement from here */ | ||
| 387 | else (t->firstfree)--; | ||
| 388 | } | ||
| 389 | /* no more free places; must create one */ | ||
| 390 | setbvalue(gval(mp), 0); /* avoid new key being removed */ | ||
| 391 | rehash(L, t); /* grow table */ | ||
| 392 | val = cast(TValue *, luaH_get(t, key)); /* get new position */ | ||
| 393 | lua_assert(ttisboolean(val)); | ||
| 394 | setnilvalue(val); | ||
| 395 | return val; | ||
| 396 | } | 425 | } |
| 397 | 426 | ||
| 398 | 427 | ||
| @@ -407,7 +436,7 @@ const TValue *luaH_getnum (Table *t, int key) { · 8 unmodified lines | |||
| 407 | lua_Number nk = cast(lua_Number, key); | 436 | lua_Number nk = cast(lua_Number, key); |
| 408 | Node *n = hashnum(t, nk); | 437 | Node *n = hashnum(t, nk); |
| 409 | do { /* check whether `key' is somewhere in the chain */ | 438 | do { /* check whether `key' is somewhere in the chain */ |
| 410 | if (ttisnumber(gkey(n)) && nvalue(gkey(n)) == nk) | 439 | if (ttisnumber(gkey(n)) && num_eq(nvalue(gkey(n)), nk)) |
| 411 | return gval(n); /* that's it */ | 440 | return gval(n); /* that's it */ |
| 412 | else n = gnext(n); | 441 | else n = gnext(n); |
| 413 | } while (n); | 442 | } while (n); |
| @@ -440,7 +469,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { · 26 unmodified lines | |||
| 440 | case LUA_TNUMBER: { | 469 | case LUA_TNUMBER: { |
| 441 | int k; | 470 | int k; |
| 442 | lua_number2int(k, (nvalue(key))); | 471 | lua_number2int(k, (nvalue(key))); |
| 443 | if (cast(lua_Number, k) == nvalue(key)) /* is an integer index? */ | 472 | if (num_eq(cast(lua_Number, k), nvalue(key))) /* is an integer index? */ |
| 444 | return luaH_getnum(t, k); /* use specialized version */ | 473 | return luaH_getnum(t, k); /* use specialized version */ |
| 445 | /* else go through */ | 474 | /* else go through */ |
| 446 | } | 475 | } |
| @@ -464,7 +493,7 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { · 17 unmodified lines | |||
| 464 | return cast(TValue *, p); | 493 | return cast(TValue *, p); |
| 465 | else { | 494 | else { |
| 466 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); | 495 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); |
| 467 | else if (ttisnumber(key) && nvalue(key) != nvalue(key)) | 496 | else if (ttisnumber(key) && !num_eq(nvalue(key), nvalue(key))) |
| 468 | luaG_runerror(L, "table index is NaN"); | 497 | luaG_runerror(L, "table index is NaN"); |
| 469 | return newkey(L, t, key); | 498 | return newkey(L, t, key); |
| 470 | } | 499 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.h,v 2.3 2004/10/06 18:34:16 roberto Exp $ | 2 | ** $Id: ltable.h,v 2.5 2005/01/05 18:20:51 roberto Exp $ |
| 3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -18,6 +18,8 @@ · 12 unmodified lines | |||
| 18 | #define key2tval(n) (cast(const TValue *, gkey(n))) | 18 | #define key2tval(n) (cast(const TValue *, gkey(n))) |
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | extern const Node luaH_dummynode; | ||
| 22 | |||
| 21 | const TValue *luaH_getnum (Table *t, int key); | 23 | const TValue *luaH_getnum (Table *t, int key); |
| 22 | TValue *luaH_setnum (lua_State *L, Table *t, int key); | 24 | TValue *luaH_setnum (lua_State *L, Table *t, int key); |
| 23 | const TValue *luaH_getstr (Table *t, TString *key); | 25 | const TValue *luaH_getstr (Table *t, TString *key); |
| @@ -25,7 +27,7 @@ TValue *luaH_setstr (lua_State *L, Table *t, TString *key); · 1 unmodified line | |||
| 25 | const TValue *luaH_get (Table *t, const TValue *key); | 27 | const TValue *luaH_get (Table *t, const TValue *key); |
| 26 | TValue *luaH_set (lua_State *L, Table *t, const TValue *key); | 28 | TValue *luaH_set (lua_State *L, Table *t, const TValue *key); |
| 27 | Table *luaH_new (lua_State *L, int narray, int lnhash); | 29 | Table *luaH_new (lua_State *L, int narray, int lnhash); |
| 28 | void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); | 30 | void luaH_resizearray (lua_State *L, Table *t, int nasize); |
| 29 | void luaH_free (lua_State *L, Table *t); | 31 | void luaH_free (lua_State *L, Table *t); |
| 30 | int luaH_next (lua_State *L, Table *t, StkId key); | 32 | int luaH_next (lua_State *L, Table *t, StkId key); |
| 31 | 33 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.133 2004/11/18 19:53:49 roberto Exp $ | 2 | ** $Id: lua.c,v 1.135 2005/01/10 17:21:10 roberto Exp $ |
| 3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -79,11 +79,23 @@ static int report (lua_State *L, int status) { · 73 unmodified lines | |||
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | 81 | ||
| 82 | static int traceback (lua_State *L) { | ||
| 83 | luaL_getfield(L, LUA_GLOBALSINDEX, "debug.traceback"); | ||
| 84 | if (!lua_isfunction(L, -1)) | ||
| 85 | lua_pop(L, 1); | ||
| 86 | else { | ||
| 87 | lua_pushvalue(L, 1); /* pass error message */ | ||
| 88 | lua_pushinteger(L, 2); /* skip this function and traceback */ | ||
| 89 | lua_call(L, 2, 1); /* call debug.traceback */ | ||
| 90 | } | ||
| 91 | return 1; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 82 | static int docall (lua_State *L, int narg, int clear) { | 95 | static int docall (lua_State *L, int narg, int clear) { |
| 83 | int status; | 96 | int status; |
| 84 | int base = lua_gettop(L) - narg; /* function index */ | 97 | int base = lua_gettop(L) - narg; /* function index */ |
| 85 | lua_pushliteral(L, "_TRACEBACK"); | 98 | lua_pushcfunction(L, traceback); /* push traceback function */ |
| 86 | lua_rawget(L, LUA_GLOBALSINDEX); /* get traceback function */ | ||
| 87 | lua_insert(L, base); /* put it under chunk and args */ | 99 | lua_insert(L, base); /* put it under chunk and args */ |
| 88 | signal(SIGINT, laction); | 100 | signal(SIGINT, laction); |
| 89 | status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); | 101 | status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); |
| @@ -359,7 +371,7 @@ static int pmain (lua_State *L) { · 269 unmodified lines | |||
| 359 | int interactive = 1; | 371 | int interactive = 1; |
| 360 | if (s->argv[0] && s->argv[0][0]) progname = s->argv[0]; | 372 | if (s->argv[0] && s->argv[0][0]) progname = s->argv[0]; |
| 361 | globalL = L; | 373 | globalL = L; |
| 362 | lua_userinit(L); /* open libraries */ | 374 | luaopen_stdlibs(L); /* open libraries */ |
| 363 | status = handle_luainit(L); | 375 | status = handle_luainit(L); |
| 364 | if (status == 0) { | 376 | if (status == 0) { |
| 365 | status = handle_argv(L, s->argv, &interactive); | 377 | status = handle_argv(L, s->argv, &interactive); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.h,v 1.197 2004/12/13 12:15:11 roberto Exp $ | 2 | ** $Id: lua.h,v 1.202 2005/02/18 12:40:02 roberto Exp $ |
| 3 | ** Lua - An Extensible Extension Language | 3 | ** Lua - An Extensible Extension Language |
| 4 | ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil | 4 | ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil |
| 5 | ** http://www.lua.org mailto:info@lua.org | 5 | ** http://www.lua.org mailto:info@lua.org |
| @@ -17,8 +17,9 @@ · 11 unmodified lines | |||
| 17 | #include "luaconf.h" | 17 | #include "luaconf.h" |
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | #define LUA_VERSION "Lua 5.1 (work4)" | 20 | #define LUA_VERSION "Lua 5.1 (work5)" |
| 21 | #define LUA_COPYRIGHT "Copyright (C) 1994-2004 Tecgraf, PUC-Rio" | 21 | #define LUA_VERSION_NUM 501 |
| 22 | #define LUA_COPYRIGHT "Copyright (C) 1994-2005 Tecgraf, PUC-Rio" | ||
| 22 | #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" | 23 | #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" |
| 23 | 24 | ||
| 24 | 25 | ||
| @@ -33,11 +34,12 @@ · 8 unmodified lines | |||
| 33 | ** pseudo-indices | 34 | ** pseudo-indices |
| 34 | */ | 35 | */ |
| 35 | #define LUA_REGISTRYINDEX (-10000) | 36 | #define LUA_REGISTRYINDEX (-10000) |
| 36 | #define LUA_GLOBALSINDEX (-10001) | 37 | #define LUA_ENVIRONINDEX (-10001) |
| 38 | #define LUA_GLOBALSINDEX (-10002) | ||
| 37 | #define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) | 39 | #define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) |
| 38 | 40 | ||
| 39 | 41 | ||
| 40 | /* return codes for `lua_pcall', `lua_resume', and `lua_threadstatus' */ | 42 | /* return codes for `lua_pcall', `lua_resume', and `lua_status' */ |
| 41 | #define LUA_YIELD 1 | 43 | #define LUA_YIELD 1 |
| 42 | #define LUA_ERRRUN 2 | 44 | #define LUA_ERRRUN 2 |
| 43 | #define LUA_ERRSYNTAX 3 | 45 | #define LUA_ERRSYNTAX 3 |
| @@ -109,112 +111,112 @@ typedef LUA_INTEGER lua_Integer; · 65 unmodified lines | |||
| 109 | /* | 111 | /* |
| 110 | ** state manipulation | 112 | ** state manipulation |
| 111 | */ | 113 | */ |
| 112 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud); | 114 | LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); |
| 113 | LUA_API void lua_close (lua_State *L); | 115 | LUA_API void (lua_close) (lua_State *L); |
| 114 | LUA_API lua_State *lua_newthread (lua_State *L); | 116 | LUA_API lua_State *(lua_newthread) (lua_State *L); |
| 115 | 117 | ||
| 116 | LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf); | 118 | LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); |
| 117 | 119 | ||
| 118 | 120 | ||
| 119 | /* | 121 | /* |
| 120 | ** basic stack manipulation | 122 | ** basic stack manipulation |
| 121 | */ | 123 | */ |
| 122 | LUA_API int lua_gettop (lua_State *L); | 124 | LUA_API int (lua_gettop) (lua_State *L); |
| 123 | LUA_API void lua_settop (lua_State *L, int idx); | 125 | LUA_API void (lua_settop) (lua_State *L, int idx); |
| 124 | LUA_API void lua_pushvalue (lua_State *L, int idx); | 126 | LUA_API void (lua_pushvalue) (lua_State *L, int idx); |
| 125 | LUA_API void lua_remove (lua_State *L, int idx); | 127 | LUA_API void (lua_remove) (lua_State *L, int idx); |
| 126 | LUA_API void lua_insert (lua_State *L, int idx); | 128 | LUA_API void (lua_insert) (lua_State *L, int idx); |
| 127 | LUA_API void lua_replace (lua_State *L, int idx); | 129 | LUA_API void (lua_replace) (lua_State *L, int idx); |
| 128 | LUA_API int lua_checkstack (lua_State *L, int sz); | 130 | LUA_API int (lua_checkstack) (lua_State *L, int sz); |
| 129 | 131 | ||
| 130 | LUA_API void lua_xmove (lua_State *from, lua_State *to, int n); | 132 | LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); |
| 131 | 133 | ||
| 132 | 134 | ||
| 133 | /* | 135 | /* |
| 134 | ** access functions (stack -> C) | 136 | ** access functions (stack -> C) |
| 135 | */ | 137 | */ |
| 136 | 138 | ||
| 137 | LUA_API int lua_isnumber (lua_State *L, int idx); | 139 | LUA_API int (lua_isnumber) (lua_State *L, int idx); |
| 138 | LUA_API int lua_isstring (lua_State *L, int idx); | 140 | LUA_API int (lua_isstring) (lua_State *L, int idx); |
| 139 | LUA_API int lua_iscfunction (lua_State *L, int idx); | 141 | LUA_API int (lua_iscfunction) (lua_State *L, int idx); |
| 140 | LUA_API int lua_isuserdata (lua_State *L, int idx); | 142 | LUA_API int (lua_isuserdata) (lua_State *L, int idx); |
| 141 | LUA_API int lua_type (lua_State *L, int idx); | 143 | LUA_API int (lua_type) (lua_State *L, int idx); |
| 142 | LUA_API const char *lua_typename (lua_State *L, int tp); | 144 | LUA_API const char *(lua_typename) (lua_State *L, int tp); |
| 143 | 145 | ||
| 144 | LUA_API int lua_equal (lua_State *L, int idx1, int idx2); | 146 | LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2); |
| 145 | LUA_API int lua_rawequal (lua_State *L, int idx1, int idx2); | 147 | LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); |
| 146 | LUA_API int lua_lessthan (lua_State *L, int idx1, int idx2); | 148 | LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2); |
| 147 | 149 | ||
| 148 | LUA_API lua_Number lua_tonumber (lua_State *L, int idx); | 150 | LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); |
| 149 | LUA_API lua_Integer lua_tointeger (lua_State *L, int idx); | 151 | LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); |
| 150 | LUA_API int lua_toboolean (lua_State *L, int idx); | 152 | LUA_API int (lua_toboolean) (lua_State *L, int idx); |
| 151 | LUA_API const char *lua_tostring (lua_State *L, int idx); | 153 | LUA_API const char *(lua_tostring) (lua_State *L, int idx); |
| 152 | LUA_API size_t lua_objsize (lua_State *L, int idx); | 154 | LUA_API size_t (lua_objsize) (lua_State *L, int idx); |
| 153 | LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx); | 155 | LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); |
| 154 | LUA_API void *lua_touserdata (lua_State *L, int idx); | 156 | LUA_API void *(lua_touserdata) (lua_State *L, int idx); |
| 155 | LUA_API lua_State *lua_tothread (lua_State *L, int idx); | 157 | LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); |
| 156 | LUA_API const void *lua_topointer (lua_State *L, int idx); | 158 | LUA_API const void *(lua_topointer) (lua_State *L, int idx); |
| 157 | 159 | ||
| 158 | 160 | ||
| 159 | /* | 161 | /* |
| 160 | ** push functions (C -> stack) | 162 | ** push functions (C -> stack) |
| 161 | */ | 163 | */ |
| 162 | LUA_API void lua_pushnil (lua_State *L); | 164 | LUA_API void (lua_pushnil) (lua_State *L); |
| 163 | LUA_API void lua_pushnumber (lua_State *L, lua_Number n); | 165 | LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); |
| 164 | LUA_API void lua_pushinteger (lua_State *L, lua_Integer n); | 166 | LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); |
| 165 | LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t l); | 167 | LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); |
| 166 | LUA_API void lua_pushstring (lua_State *L, const char *s); | 168 | LUA_API void (lua_pushstring) (lua_State *L, const char *s); |
| 167 | LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, | 169 | LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, |
| 168 | va_list argp); | 170 | va_list argp); |
| 169 | LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...); | 171 | LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); |
| 170 | LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n); | 172 | LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); |
| 171 | LUA_API void lua_pushboolean (lua_State *L, int b); | 173 | LUA_API void (lua_pushboolean) (lua_State *L, int b); |
| 172 | LUA_API void lua_pushlightuserdata (lua_State *L, void *p); | 174 | LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); |
| 173 | LUA_API int lua_pushthread (lua_State *L); | 175 | LUA_API int (lua_pushthread) (lua_State *L); |
| 174 | 176 | ||
| 175 | 177 | ||
| 176 | /* | 178 | /* |
| 177 | ** get functions (Lua -> stack) | 179 | ** get functions (Lua -> stack) |
| 178 | */ | 180 | */ |
| 179 | LUA_API void lua_gettable (lua_State *L, int idx); | 181 | LUA_API void (lua_gettable) (lua_State *L, int idx); |
| 180 | LUA_API void lua_getfield (lua_State *L, int idx, const char *k); | 182 | LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); |
| 181 | LUA_API void lua_rawget (lua_State *L, int idx); | 183 | LUA_API void (lua_rawget) (lua_State *L, int idx); |
| 182 | LUA_API void lua_rawgeti (lua_State *L, int idx, int n); | 184 | LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); |
| 183 | LUA_API void lua_createtable (lua_State *L, int narr, int nrec); | 185 | LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); |
| 184 | LUA_API void *lua_newuserdata (lua_State *L, size_t sz); | 186 | LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); |
| 185 | LUA_API int lua_getmetatable (lua_State *L, int objindex); | 187 | LUA_API int (lua_getmetatable) (lua_State *L, int objindex); |
| 186 | LUA_API void lua_getfenv (lua_State *L, int idx); | 188 | LUA_API void (lua_getfenv) (lua_State *L, int idx); |
| 187 | 189 | ||
| 188 | 190 | ||
| 189 | /* | 191 | /* |
| 190 | ** set functions (stack -> Lua) | 192 | ** set functions (stack -> Lua) |
| 191 | */ | 193 | */ |
| 192 | LUA_API void lua_settable (lua_State *L, int idx); | 194 | LUA_API void (lua_settable) (lua_State *L, int idx); |
| 193 | LUA_API void lua_setfield (lua_State *L, int idx, const char *k); | 195 | LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); |
| 194 | LUA_API void lua_rawset (lua_State *L, int idx); | 196 | LUA_API void (lua_rawset) (lua_State *L, int idx); |
| 195 | LUA_API void lua_rawseti (lua_State *L, int idx, int n); | 197 | LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); |
| 196 | LUA_API int lua_setmetatable (lua_State *L, int objindex); | 198 | LUA_API int (lua_setmetatable) (lua_State *L, int objindex); |
| 197 | LUA_API int lua_setfenv (lua_State *L, int idx); | 199 | LUA_API int (lua_setfenv) (lua_State *L, int idx); |
| 198 | 200 | ||
| 199 | 201 | ||
| 200 | /* | 202 | /* |
| 201 | ** `load' and `call' functions (load and run Lua code) | 203 | ** `load' and `call' functions (load and run Lua code) |
| 202 | */ | 204 | */ |
| 203 | LUA_API void lua_call (lua_State *L, int nargs, int nresults); | 205 | LUA_API void (lua_call) (lua_State *L, int nargs, int nresults); |
| 204 | LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc); | 206 | LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); |
| 205 | LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud); | 207 | LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); |
| 206 | LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *dt, | 208 | LUA_API int (lua_load) (lua_State *L, lua_Chunkreader reader, void *dt, |
| 207 | const char *chunkname); | 209 | const char *chunkname); |
| 208 | 210 | ||
| 209 | LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data); | 211 | LUA_API int (lua_dump) (lua_State *L, lua_Chunkwriter writer, void *data); |
| 210 | 212 | ||
| 211 | 213 | ||
| 212 | /* | 214 | /* |
| 213 | ** coroutine functions | 215 | ** coroutine functions |
| 214 | */ | 216 | */ |
| 215 | LUA_API int lua_yield (lua_State *L, int nresults); | 217 | LUA_API int (lua_yield) (lua_State *L, int nresults); |
| 216 | LUA_API int lua_resume (lua_State *L, int narg); | 218 | LUA_API int (lua_resume) (lua_State *L, int narg); |
| 217 | LUA_API int lua_threadstatus (lua_State *L); | 219 | LUA_API int (lua_status) (lua_State *L); |
| 218 | 220 | ||
| 219 | /* | 221 | /* |
| 220 | ** garbage-collection function and options | 222 | ** garbage-collection function and options |
| @@ -226,24 +228,22 @@ LUA_API int lua_threadstatus (lua_State *L); · 5 unmodified lines | |||
| 226 | #define LUA_GCCOUNT 3 | 228 | #define LUA_GCCOUNT 3 |
| 227 | #define LUA_GCSTEP 4 | 229 | #define LUA_GCSTEP 4 |
| 228 | #define LUA_GCSETPACE 5 | 230 | #define LUA_GCSETPACE 5 |
| 229 | #define LUA_GCSETINCMODE 6 | 231 | #define LUA_GCSETSTEPMUL 6 |
| 230 | 232 | ||
| 231 | LUA_API int lua_gc (lua_State *L, int what, int data); | 233 | LUA_API int (lua_gc) (lua_State *L, int what, int data); |
| 232 | 234 | ||
| 233 | 235 | ||
| 234 | /* | 236 | /* |
| 235 | ** miscellaneous functions | 237 | ** miscellaneous functions |
| 236 | */ | 238 | */ |
| 237 | 239 | ||
| 238 | LUA_API const char *lua_version (void); | 240 | LUA_API int (lua_error) (lua_State *L); |
| 239 | 241 | ||
| 240 | LUA_API int lua_error (lua_State *L); | 242 | LUA_API int (lua_next) (lua_State *L, int idx); |
| 241 | 243 | ||
| 242 | LUA_API int lua_next (lua_State *L, int idx); | 244 | LUA_API void (lua_concat) (lua_State *L, int n); |
| 243 | 245 | ||
| 244 | LUA_API void lua_concat (lua_State *L, int n); | 246 | LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); |
| 245 | |||
| 246 | LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud); | ||
| 247 | 247 | ||
| 248 | 248 | ||
| 249 | 249 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: luaconf.h,v 1.22 2004/12/27 15:58:15 roberto Exp $ | 2 | ** $Id: luaconf.h,v 1.30 2005/02/28 15:59:11 roberto Exp $ |
| 3 | ** Configuration file for Lua | 3 | ** Configuration file for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -96,7 +96,7 @@ · 90 unmodified lines | |||
| 96 | #define stdin_is_tty() isatty(0) | 96 | #define stdin_is_tty() isatty(0) |
| 97 | #elif defined(_WIN32) | 97 | #elif defined(_WIN32) |
| 98 | #include <io.h> | 98 | #include <io.h> |
| 99 | #include <fcntl.h> | 99 | #include <stdio.h> |
| 100 | #define stdin_is_tty() _isatty(_fileno(stdin)) | 100 | #define stdin_is_tty() _isatty(_fileno(stdin)) |
| 101 | #else | 101 | #else |
| 102 | #define stdin_is_tty() 1 /* assume stdin is a tty */ | 102 | #define stdin_is_tty() 1 /* assume stdin is a tty */ |
| @@ -108,16 +108,6 @@ · 5 unmodified lines | |||
| 108 | #define PROGNAME "lua" | 108 | #define PROGNAME "lua" |
| 109 | 109 | ||
| 110 | 110 | ||
| 111 | /* | ||
| 112 | ** this macro allows you to open other libraries when starting the | ||
| 113 | ** stand-alone interpreter | ||
| 114 | */ | ||
| 115 | #define lua_userinit(L) luaopen_stdlibs(L) | ||
| 116 | /* | ||
| 117 | ** #define lua_userinit(L) { int luaopen_mylibs(lua_State *L); \ | ||
| 118 | ** luaopen_stdlibs(L); luaopen_mylibs(L); } | ||
| 119 | */ | ||
| 120 | |||
| 121 | 111 | ||
| 122 | 112 | ||
| 123 | /* | 113 | /* |
| @@ -217,8 +207,7 @@ · 93 unmodified lines | |||
| 217 | #if defined(__GNUC__) && defined(__i386) | 207 | #if defined(__GNUC__) && defined(__i386) |
| 218 | #define lua_number2int(i,d) __asm__ ("fistpl %0":"=m"(i):"t"(d):"st") | 208 | #define lua_number2int(i,d) __asm__ ("fistpl %0":"=m"(i):"t"(d):"st") |
| 219 | 209 | ||
| 220 | #elif defined(_WIN32) && defined(_M_IX86) | 210 | #elif defined(_MSC_VER) && defined(_M_IX86) |
| 221 | #include <math.h> | ||
| 222 | #pragma warning(disable: 4514) | 211 | #pragma warning(disable: 4514) |
| 223 | __inline int l_lrint (double flt) | 212 | __inline int l_lrint (double flt) |
| 224 | { int i; | 213 | { int i; |
| @@ -248,6 +237,8 @@ __inline int l_lrint (double flt) · 23 unmodified lines | |||
| 248 | /* function to convert a lua_Number to a string */ | 237 | /* function to convert a lua_Number to a string */ |
| 249 | #include <stdio.h> | 238 | #include <stdio.h> |
| 250 | #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) | 239 | #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) |
| 240 | /* maximum size of previous conversion */ | ||
| 241 | #define MAX_NUMBER2STR 32 /* 16 digits, sign, point and \0 (+ some extra) */ | ||
| 251 | 242 | ||
| 252 | /* function to convert a string to a lua_Number */ | 243 | /* function to convert a string to a lua_Number */ |
| 253 | #define lua_str2number(s,p) strtod((s), (p)) | 244 | #define lua_str2number(s,p) strtod((s), (p)) |
| @@ -258,12 +249,29 @@ __inline int l_lrint (double flt) · 4 unmodified lines | |||
| 258 | #define LUA_UACNUMBER double | 249 | #define LUA_UACNUMBER double |
| 259 | 250 | ||
| 260 | 251 | ||
| 252 | /* primitive operators for numbers */ | ||
| 253 | #define num_add(a,b) ((a)+(b)) | ||
| 254 | #define num_sub(a,b) ((a)-(b)) | ||
| 255 | #define num_mul(a,b) ((a)*(b)) | ||
| 256 | #define num_div(a,b) ((a)/(b)) | ||
| 257 | #define num_unm(a) (-(a)) | ||
| 258 | #define num_eq(a,b) ((a)==(b)) | ||
| 259 | #define num_lt(a,b) ((a)<(b)) | ||
| 260 | #define num_le(a,b) ((a)<=(b)) | ||
| 261 | #include <math.h> | ||
| 262 | #define num_pow(a,b) pow(a,b) | ||
| 263 | |||
| 264 | |||
| 261 | 265 | ||
| 262 | /* type to ensure maximum alignment */ | 266 | /* type to ensure maximum alignment */ |
| 263 | #define LUSER_ALIGNMENT_T union { double u; void *s; long l; } | 267 | #define LUSER_ALIGNMENT_T union { double u; void *s; long l; } |
| 264 | 268 | ||
| 265 | 269 | ||
| 266 | /* exception handling */ | 270 | /* |
| 271 | ** exception handling: by default, Lua handles errors with longjmp/setjmp | ||
| 272 | ** when compiling as C code and with exceptions when compiling as C++ code. | ||
| 273 | ** Change that if you prefer to use longjmp/setjmp even with C++. | ||
| 274 | */ | ||
| 267 | #ifndef __cplusplus | 275 | #ifndef __cplusplus |
| 268 | /* default handling with long jumps */ | 276 | /* default handling with long jumps */ |
| 269 | #include <setjmp.h> | 277 | #include <setjmp.h> |
| @@ -282,18 +290,20 @@ __inline int l_lrint (double flt) · 12 unmodified lines | |||
| 282 | 290 | ||
| 283 | 291 | ||
| 284 | /* | 292 | /* |
| 285 | ** macros for thread synchronization inside Lua core machine: | 293 | ** macros for thread synchronization inside Lua core machine: This is |
| 286 | ** all accesses to the global state and to global objects are synchronized. | 294 | ** an attempt to simplify the implementation of a multithreaded version |
| 287 | ** Because threads can read the stack of other threads | 295 | ** of Lua. Do not change that unless you know what you are doing. all |
| 288 | ** (when running garbage collection), | 296 | ** accesses to the global state and to global objects are synchronized. |
| 289 | ** a thread must also synchronize any write-access to its own stack. | 297 | ** Because threads can read the stack of other threads (when running |
| 290 | ** Unsynchronized accesses are allowed only when reading its own stack, | 298 | ** garbage collection), a thread must also synchronize any write-access |
| 291 | ** or when reading immutable fields from global objects | 299 | ** to its own stack. Unsynchronized accesses are allowed only when |
| 292 | ** (such as string values and udata values). | 300 | ** reading its own stack, or when reading immutable fields from global |
| 301 | ** objects (such as string values and udata values). | ||
| 293 | */ | 302 | */ |
| 294 | #define lua_lock(L) ((void) 0) | 303 | #define lua_lock(L) ((void) 0) |
| 295 | #define lua_unlock(L) ((void) 0) | 304 | #define lua_unlock(L) ((void) 0) |
| 296 | 305 | ||
| 306 | |||
| 297 | /* | 307 | /* |
| 298 | ** this macro allows a thread switch in appropriate places in the Lua | 308 | ** this macro allows a thread switch in appropriate places in the Lua |
| 299 | ** core | 309 | ** core |
| @@ -303,7 +313,7 @@ __inline int l_lrint (double flt) · 3 unmodified lines | |||
| 303 | 313 | ||
| 304 | 314 | ||
| 305 | /* allows user-specific initialization on new threads */ | 315 | /* allows user-specific initialization on new threads */ |
| 306 | #define lua_userstateopen(L) /* empty */ | 316 | #define lua_userstateopen(L) ((void)0) |
| 307 | 317 | ||
| 308 | 318 | ||
| 309 | #endif | 319 | #endif |
| @@ -321,9 +331,6 @@ __inline int l_lrint (double flt) · 11 unmodified lines | |||
| 321 | #ifdef LUA_LIB | 331 | #ifdef LUA_LIB |
| 322 | 332 | ||
| 323 | 333 | ||
| 324 | |||
| 325 | /* `assert' options */ | ||
| 326 | |||
| 327 | /* environment variables that hold the search path for packages */ | 334 | /* environment variables that hold the search path for packages */ |
| 328 | #define LUA_PATH "LUA_PATH" | 335 | #define LUA_PATH "LUA_PATH" |
| 329 | #define LUA_CPATH "LUA_CPATH" | 336 | #define LUA_CPATH "LUA_CPATH" |
| @@ -332,7 +339,7 @@ __inline int l_lrint (double flt) · 2 unmodified lines | |||
| 332 | #define LUA_POF "luaopen_" | 339 | #define LUA_POF "luaopen_" |
| 333 | 340 | ||
| 334 | /* separator for open functions in C libraries */ | 341 | /* separator for open functions in C libraries */ |
| 335 | #define LUA_OFSEP "" | 342 | #define LUA_OFSEP "_" |
| 336 | 343 | ||
| 337 | /* directory separator (for submodules) */ | 344 | /* directory separator (for submodules) */ |
| 338 | #if defined(_WIN32) | 345 | #if defined(_WIN32) |
| @@ -342,18 +349,20 @@ __inline int l_lrint (double flt) · 3 unmodified lines | |||
| 342 | #endif | 349 | #endif |
| 343 | 350 | ||
| 344 | /* separator of templates in a path */ | 351 | /* separator of templates in a path */ |
| 345 | #define LUA_PATH_SEP ';' | 352 | #define LUA_PATHSEP ';' |
| 346 | 353 | ||
| 347 | /* wild char in each template */ | 354 | /* wild char in each template */ |
| 348 | #define LUA_PATH_MARK "?" | 355 | #define LUA_PATH_MARK "?" |
| 349 | 356 | ||
| 350 | 357 | ||
| 351 | /* maximum number of captures in pattern-matching */ | 358 | /* maximum number of captures in pattern-matching (arbitrary limit) */ |
| 352 | #define MAX_CAPTURES 32 /* arbitrary limit */ | 359 | #define MAX_CAPTURES 32 |
| 353 | 360 | ||
| 354 | 361 | ||
| 355 | /* | 362 | /* |
| 356 | ** by default, gcc does not get `tmpname' | 363 | ** by default, gcc does not get `os.tmpname', because it generates a warning |
| 364 | ** when using `tmpname'. Change that if you really want (or do not want) | ||
| 365 | ** `os.tmpname' available. | ||
| 357 | */ | 366 | */ |
| 358 | #ifdef __GNUC__ | 367 | #ifdef __GNUC__ |
| 359 | #define USE_TMPNAME 0 | 368 | #define USE_TMPNAME 0 |
| @@ -363,7 +372,11 @@ __inline int l_lrint (double flt) · 3 unmodified lines | |||
| 363 | 372 | ||
| 364 | 373 | ||
| 365 | /* | 374 | /* |
| 366 | ** Configuration for loadlib | 375 | ** Configuration for loadlib: Lua tries to guess the dynamic-library |
| 376 | ** system that your platform uses (either Windows' DLL, Mac's dyld, or | ||
| 377 | ** dlopen). If your system is some kind of Unix, there is a good chance | ||
| 378 | ** that USE_DLOPEN will work for it. You may need to adapt also the | ||
| 379 | ** makefile. | ||
| 367 | */ | 380 | */ |
| 368 | #if defined(_WIN32) | 381 | #if defined(_WIN32) |
| 369 | #define USE_DLL | 382 | #define USE_DLL |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lualib.h,v 1.32 2004/07/09 15:47:48 roberto Exp $ | 2 | ** $Id: lualib.h,v 1.33 2005/01/10 16:31:30 roberto Exp $ |
| 3 | ** Lua standard libraries | 3 | ** Lua standard libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -16,32 +16,32 @@ · 10 unmodified lines | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | #define LUA_COLIBNAME "coroutine" | 18 | #define LUA_COLIBNAME "coroutine" |
| 19 | LUALIB_API int luaopen_base (lua_State *L); | 19 | LUALIB_API int (luaopen_base) (lua_State *L); |
| 20 | 20 | ||
| 21 | #define LUA_TABLIBNAME "table" | 21 | #define LUA_TABLIBNAME "table" |
| 22 | LUALIB_API int luaopen_table (lua_State *L); | 22 | LUALIB_API int (luaopen_table) (lua_State *L); |
| 23 | 23 | ||
| 24 | #define LUA_IOLIBNAME "io" | 24 | #define LUA_IOLIBNAME "io" |
| 25 | LUALIB_API int luaopen_io (lua_State *L); | 25 | LUALIB_API int (luaopen_io) (lua_State *L); |
| 26 | 26 | ||
| 27 | #define LUA_OSLIBNAME "os" | 27 | #define LUA_OSLIBNAME "os" |
| 28 | LUALIB_API int luaopen_os (lua_State *L); | 28 | LUALIB_API int (luaopen_os) (lua_State *L); |
| 29 | 29 | ||
| 30 | #define LUA_STRLIBNAME "string" | 30 | #define LUA_STRLIBNAME "string" |
| 31 | LUALIB_API int luaopen_string (lua_State *L); | 31 | LUALIB_API int (luaopen_string) (lua_State *L); |
| 32 | 32 | ||
| 33 | #define LUA_MATHLIBNAME "math" | 33 | #define LUA_MATHLIBNAME "math" |
| 34 | LUALIB_API int luaopen_math (lua_State *L); | 34 | LUALIB_API int (luaopen_math) (lua_State *L); |
| 35 | 35 | ||
| 36 | #define LUA_DBLIBNAME "debug" | 36 | #define LUA_DBLIBNAME "debug" |
| 37 | LUALIB_API int luaopen_debug (lua_State *L); | 37 | LUALIB_API int (luaopen_debug) (lua_State *L); |
| 38 | 38 | ||
| 39 | 39 | ||
| 40 | LUALIB_API int luaopen_loadlib (lua_State *L); | 40 | LUALIB_API int (luaopen_loadlib) (lua_State *L); |
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | /* open all previous libraries */ | 43 | /* open all previous libraries */ |
| 44 | LUALIB_API int luaopen_stdlibs (lua_State *L); | 44 | LUALIB_API int (luaopen_stdlibs) (lua_State *L); |
| 45 | 45 | ||
| 46 | 46 | ||
| 47 | #endif | 47 | #endif |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 2.18 2004/12/03 20:35:33 roberto Exp $ | 2 | ** $Id: lvm.c,v 2.26 2005/02/23 17:30:22 roberto Exp $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -47,7 +47,7 @@ int luaV_tostring (lua_State *L, StkId obj) { · 41 unmodified lines | |||
| 47 | if (!ttisnumber(obj)) | 47 | if (!ttisnumber(obj)) |
| 48 | return 0; | 48 | return 0; |
| 49 | else { | 49 | else { |
| 50 | char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */ | 50 | char s[MAX_NUMBER2STR]; |
| 51 | lua_number2str(s, nvalue(obj)); | 51 | lua_number2str(s, nvalue(obj)); |
| 52 | setsvalue2s(L, obj, luaS_new(L, s)); | 52 | setsvalue2s(L, obj, luaS_new(L, s)); |
| 53 | return 1; | 53 | return 1; |
| @@ -113,7 +113,7 @@ StkId luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val, · 59 unmodified lines | |||
| 113 | const TValue *tm; | 113 | const TValue *tm; |
| 114 | if (ttistable(t)) { /* `t' is a table? */ | 114 | if (ttistable(t)) { /* `t' is a table? */ |
| 115 | Table *h = hvalue(t); | 115 | Table *h = hvalue(t); |
| 116 | const TValue *res = luaH_get(h, key); /* do a primitive set */ | 116 | const TValue *res = luaH_get(h, key); /* do a primitive get */ |
| 117 | if (!ttisnil(res) || /* result is no nil? */ | 117 | if (!ttisnil(res) || /* result is no nil? */ |
| 118 | (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ | 118 | (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ |
| 119 | setobj2s(L, val, res); | 119 | setobj2s(L, val, res); |
| @@ -241,7 +241,7 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { · 121 unmodified lines | |||
| 241 | if (ttype(l) != ttype(r)) | 241 | if (ttype(l) != ttype(r)) |
| 242 | return luaG_ordererror(L, l, r); | 242 | return luaG_ordererror(L, l, r); |
| 243 | else if (ttisnumber(l)) | 243 | else if (ttisnumber(l)) |
| 244 | return nvalue(l) < nvalue(r); | 244 | return num_lt(nvalue(l), nvalue(r)); |
| 245 | else if (ttisstring(l)) | 245 | else if (ttisstring(l)) |
| 246 | return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; | 246 | return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; |
| 247 | else if ((res = call_orderTM(L, l, r, TM_LT)) != -1) | 247 | else if ((res = call_orderTM(L, l, r, TM_LT)) != -1) |
| @@ -255,7 +255,7 @@ static int lessequal (lua_State *L, const TValue *l, const TValue *r) { · 7 unmodified lines | |||
| 255 | if (ttype(l) != ttype(r)) | 255 | if (ttype(l) != ttype(r)) |
| 256 | return luaG_ordererror(L, l, r); | 256 | return luaG_ordererror(L, l, r); |
| 257 | else if (ttisnumber(l)) | 257 | else if (ttisnumber(l)) |
| 258 | return nvalue(l) <= nvalue(r); | 258 | return num_le(nvalue(l), nvalue(r)); |
| 259 | else if (ttisstring(l)) | 259 | else if (ttisstring(l)) |
| 260 | return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; | 260 | return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; |
| 261 | else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */ | 261 | else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */ |
| @@ -271,7 +271,7 @@ int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) { · 9 unmodified lines | |||
| 271 | lua_assert(ttype(t1) == ttype(t2)); | 271 | lua_assert(ttype(t1) == ttype(t2)); |
| 272 | switch (ttype(t1)) { | 272 | switch (ttype(t1)) { |
| 273 | case LUA_TNIL: return 1; | 273 | case LUA_TNIL: return 1; |
| 274 | case LUA_TNUMBER: return nvalue(t1) == nvalue(t2); | 274 | case LUA_TNUMBER: return num_eq(nvalue(t1), nvalue(t2)); |
| 275 | case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ | 275 | case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ |
| 276 | case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); | 276 | case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); |
| 277 | case LUA_TUSERDATA: { | 277 | case LUA_TUSERDATA: { |
| @@ -335,18 +335,11 @@ static StkId Arith (lua_State *L, StkId ra, const TValue *rb, · 57 unmodified lines | |||
| 335 | if ((b = luaV_tonumber(rb, &tempb)) != NULL && | 335 | if ((b = luaV_tonumber(rb, &tempb)) != NULL && |
| 336 | (c = luaV_tonumber(rc, &tempc)) != NULL) { | 336 | (c = luaV_tonumber(rc, &tempc)) != NULL) { |
| 337 | switch (op) { | 337 | switch (op) { |
| 338 | case TM_ADD: setnvalue(ra, nvalue(b) + nvalue(c)); break; | 338 | case TM_ADD: setnvalue(ra, num_add(nvalue(b), nvalue(c))); break; |
| 339 | case TM_SUB: setnvalue(ra, nvalue(b) - nvalue(c)); break; | 339 | case TM_SUB: setnvalue(ra, num_sub(nvalue(b), nvalue(c))); break; |
| 340 | case TM_MUL: setnvalue(ra, nvalue(b) * nvalue(c)); break; | 340 | case TM_MUL: setnvalue(ra, num_mul(nvalue(b), nvalue(c))); break; |
| 341 | case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break; | 341 | case TM_DIV: setnvalue(ra, num_div(nvalue(b), nvalue(c))); break; |
| 342 | case TM_POW: { | 342 | case TM_POW: setnvalue(ra, num_pow(nvalue(b), nvalue(c))); break; |
| 343 | const TValue *f = luaH_getstr(hvalue(gt(L)), G(L)->tmname[TM_POW]); | ||
| 344 | if (!ttisfunction(f)) | ||
| 345 | luaG_runerror(L, "`__pow' (`^' operator) is not a function"); | ||
| 346 | prepTMcall(L, f, b, c); | ||
| 347 | callTMres(L, ra); | ||
| 348 | break; | ||
| 349 | } | ||
| 350 | default: lua_assert(0); break; | 343 | default: lua_assert(0); break; |
| 351 | } | 344 | } |
| 352 | } | 345 | } |
| @@ -435,9 +428,11 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 82 unmodified lines | |||
| 435 | continue; | 428 | continue; |
| 436 | } | 429 | } |
| 437 | case OP_GETGLOBAL: { | 430 | case OP_GETGLOBAL: { |
| 431 | TValue g; | ||
| 438 | TValue *rb = KBx(i); | 432 | TValue *rb = KBx(i); |
| 439 | lua_assert(ttisstring(rb) && ttistable(&cl->g)); | 433 | sethvalue(L, &g, cl->env); |
| 440 | base = luaV_gettable(L, &cl->g, rb, ra, pc); /***/ | 434 | lua_assert(ttisstring(rb)); |
| 435 | base = luaV_gettable(L, &g, rb, ra, pc); /***/ | ||
| 441 | continue; | 436 | continue; |
| 442 | } | 437 | } |
| 443 | case OP_GETTABLE: { | 438 | case OP_GETTABLE: { |
| @@ -445,8 +440,10 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 1 unmodified line | |||
| 445 | continue; | 440 | continue; |
| 446 | } | 441 | } |
| 447 | case OP_SETGLOBAL: { | 442 | case OP_SETGLOBAL: { |
| 448 | lua_assert(ttisstring(KBx(i)) && ttistable(&cl->g)); | 443 | TValue g; |
| 449 | base = luaV_settable(L, &cl->g, KBx(i), ra, pc); /***/ | 444 | sethvalue(L, &g, cl->env); |
| 445 | lua_assert(ttisstring(KBx(i))); | ||
| 446 | base = luaV_settable(L, &g, KBx(i), ra, pc); /***/ | ||
| 450 | continue; | 447 | continue; |
| 451 | } | 448 | } |
| 452 | case OP_SETUPVAL: { | 449 | case OP_SETUPVAL: { |
| @@ -461,8 +458,8 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 8 unmodified lines | |||
| 461 | } | 458 | } |
| 462 | case OP_NEWTABLE: { | 459 | case OP_NEWTABLE: { |
| 463 | int b = GETARG_B(i); | 460 | int b = GETARG_B(i); |
| 464 | b = luaO_fb2int(b); | 461 | int c = GETARG_C(i); |
| 465 | sethvalue(L, ra, luaH_new(L, b, GETARG_C(i))); | 462 | sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); |
| 466 | L->ci->savedpc = pc; | 463 | L->ci->savedpc = pc; |
| 467 | luaC_checkGC(L); /***/ | 464 | luaC_checkGC(L); /***/ |
| 468 | base = L->base; | 465 | base = L->base; |
| @@ -478,7 +475,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 9 unmodified lines | |||
| 478 | TValue *rb = RKB(i); | 475 | TValue *rb = RKB(i); |
| 479 | TValue *rc = RKC(i); | 476 | TValue *rc = RKC(i); |
| 480 | if (ttisnumber(rb) && ttisnumber(rc)) { | 477 | if (ttisnumber(rb) && ttisnumber(rc)) { |
| 481 | setnvalue(ra, nvalue(rb) + nvalue(rc)); | 478 | setnvalue(ra, num_add(nvalue(rb), nvalue(rc))); |
| 482 | } | 479 | } |
| 483 | else | 480 | else |
| 484 | base = Arith(L, ra, rb, rc, TM_ADD, pc); /***/ | 481 | base = Arith(L, ra, rb, rc, TM_ADD, pc); /***/ |
| @@ -488,7 +485,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 3 unmodified lines | |||
| 488 | TValue *rb = RKB(i); | 485 | TValue *rb = RKB(i); |
| 489 | TValue *rc = RKC(i); | 486 | TValue *rc = RKC(i); |
| 490 | if (ttisnumber(rb) && ttisnumber(rc)) { | 487 | if (ttisnumber(rb) && ttisnumber(rc)) { |
| 491 | setnvalue(ra, nvalue(rb) - nvalue(rc)); | 488 | setnvalue(ra, num_sub(nvalue(rb), nvalue(rc))); |
| 492 | } | 489 | } |
| 493 | else | 490 | else |
| 494 | base = Arith(L, ra, rb, rc, TM_SUB, pc); /***/ | 491 | base = Arith(L, ra, rb, rc, TM_SUB, pc); /***/ |
| @@ -498,7 +495,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 3 unmodified lines | |||
| 498 | TValue *rb = RKB(i); | 495 | TValue *rb = RKB(i); |
| 499 | TValue *rc = RKC(i); | 496 | TValue *rc = RKC(i); |
| 500 | if (ttisnumber(rb) && ttisnumber(rc)) { | 497 | if (ttisnumber(rb) && ttisnumber(rc)) { |
| 501 | setnvalue(ra, nvalue(rb) * nvalue(rc)); | 498 | setnvalue(ra, num_mul(nvalue(rb), nvalue(rc))); |
| 502 | } | 499 | } |
| 503 | else | 500 | else |
| 504 | base = Arith(L, ra, rb, rc, TM_MUL, pc); /***/ | 501 | base = Arith(L, ra, rb, rc, TM_MUL, pc); /***/ |
| @@ -508,21 +505,27 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 3 unmodified lines | |||
| 508 | TValue *rb = RKB(i); | 505 | TValue *rb = RKB(i); |
| 509 | TValue *rc = RKC(i); | 506 | TValue *rc = RKC(i); |
| 510 | if (ttisnumber(rb) && ttisnumber(rc)) { | 507 | if (ttisnumber(rb) && ttisnumber(rc)) { |
| 511 | setnvalue(ra, nvalue(rb) / nvalue(rc)); | 508 | setnvalue(ra, num_div(nvalue(rb), nvalue(rc))); |
| 512 | } | 509 | } |
| 513 | else | 510 | else |
| 514 | base = Arith(L, ra, rb, rc, TM_DIV, pc); /***/ | 511 | base = Arith(L, ra, rb, rc, TM_DIV, pc); /***/ |
| 515 | continue; | 512 | continue; |
| 516 | } | 513 | } |
| 517 | case OP_POW: { | 514 | case OP_POW: { |
| 518 | base = Arith(L, ra, RKB(i), RKC(i), TM_POW, pc); /***/ | 515 | TValue *rb = RKB(i); |
| 516 | TValue *rc = RKC(i); | ||
| 517 | if (ttisnumber(rb) && ttisnumber(rc)) { | ||
| 518 | setnvalue(ra, num_pow(nvalue(rb), nvalue(rc))); | ||
| 519 | } | ||
| 520 | else | ||
| 521 | base = Arith(L, ra, rb, rc, TM_POW, pc); /***/ | ||
| 519 | continue; | 522 | continue; |
| 520 | } | 523 | } |
| 521 | case OP_UNM: { | 524 | case OP_UNM: { |
| 522 | const TValue *rb = RB(i); | 525 | const TValue *rb = RB(i); |
| 523 | TValue temp; | 526 | TValue temp; |
| 524 | if (tonumber(rb, &temp)) { | 527 | if (tonumber(rb, &temp)) { |
| 525 | setnvalue(ra, -nvalue(rb)); | 528 | setnvalue(ra, num_unm(nvalue(rb))); |
| 526 | } | 529 | } |
| 527 | else { | 530 | else { |
| 528 | setnilvalue(&temp); | 531 | setnilvalue(&temp); |
| @@ -617,9 +620,9 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 88 unmodified lines | |||
| 617 | int aux; | 620 | int aux; |
| 618 | StkId func = ci->func; | 621 | StkId func = ci->func; |
| 619 | StkId pfunc = (ci+1)->func; /* previous function index */ | 622 | StkId pfunc = (ci+1)->func; /* previous function index */ |
| 623 | if (L->openupval) luaF_close(L, base); | ||
| 620 | base = ci->base = ci->func + ((ci+1)->base - pfunc); | 624 | base = ci->base = ci->func + ((ci+1)->base - pfunc); |
| 621 | L->base = base; | 625 | L->base = base; |
| 622 | if (L->openupval) luaF_close(L, base); | ||
| 623 | for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */ | 626 | for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */ |
| 624 | setobjs2s(L, func+aux, pfunc+aux); | 627 | setobjs2s(L, func+aux, pfunc+aux); |
| 625 | ci->top = L->top = func+aux; /* correct top */ | 628 | ci->top = L->top = func+aux; /* correct top */ |
| @@ -658,9 +661,9 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 32 unmodified lines | |||
| 658 | } | 661 | } |
| 659 | case OP_FORLOOP: { | 662 | case OP_FORLOOP: { |
| 660 | lua_Number step = nvalue(ra+2); | 663 | lua_Number step = nvalue(ra+2); |
| 661 | lua_Number idx = nvalue(ra) + step; /* increment index */ | 664 | lua_Number idx = num_add(nvalue(ra), step); /* increment index */ |
| 662 | lua_Number limit = nvalue(ra+1); | 665 | lua_Number limit = nvalue(ra+1); |
| 663 | if (step > 0 ? idx <= limit : idx >= limit) { | 666 | if (step > 0 ? num_le(idx, limit) : num_le(limit, idx)) { |
| 664 | dojump(L, pc, GETARG_sBx(i)); /* jump back */ | 667 | dojump(L, pc, GETARG_sBx(i)); /* jump back */ |
| 665 | setnvalue(ra, idx); /* update internal index... */ | 668 | setnvalue(ra, idx); /* update internal index... */ |
| 666 | setnvalue(ra+3, idx); /* ...and external index */ | 669 | setnvalue(ra+3, idx); /* ...and external index */ |
| @@ -678,7 +681,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 11 unmodified lines | |||
| 678 | luaG_runerror(L, "`for' limit must be a number"); | 681 | luaG_runerror(L, "`for' limit must be a number"); |
| 679 | else if (!tonumber(pstep, ra+2)) | 682 | else if (!tonumber(pstep, ra+2)) |
| 680 | luaG_runerror(L, "`for' step must be a number"); | 683 | luaG_runerror(L, "`for' step must be a number"); |
| 681 | setnvalue(ra, nvalue(ra) - nvalue(pstep)); | 684 | setnvalue(ra, num_sub(nvalue(ra), nvalue(pstep))); |
| 682 | dojump(L, pc, GETARG_sBx(i)); | 685 | dojump(L, pc, GETARG_sBx(i)); |
| 683 | continue; | 686 | continue; |
| 684 | } | 687 | } |
| @@ -723,7 +726,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 38 unmodified lines | |||
| 723 | if (c == 0) c = cast(int, *pc++); | 726 | if (c == 0) c = cast(int, *pc++); |
| 724 | last = ((c-1)*LFIELDS_PER_FLUSH) + n + LUA_FIRSTINDEX - 1; | 727 | last = ((c-1)*LFIELDS_PER_FLUSH) + n + LUA_FIRSTINDEX - 1; |
| 725 | if (last > h->sizearray) /* needs more space? */ | 728 | if (last > h->sizearray) /* needs more space? */ |
| 726 | luaH_resize(L, h, last, h->lsizenode); /* pre-alloc it at once */ | 729 | luaH_resizearray(L, h, last); /* pre-alloc it at once */ |
| 727 | for (; n > 0; n--) { | 730 | for (; n > 0; n--) { |
| 728 | TValue *val = ra+n; | 731 | TValue *val = ra+n; |
| 729 | setobj2t(L, luaH_setnum(L, h, last--), val); | 732 | setobj2t(L, luaH_setnum(L, h, last--), val); |
| @@ -741,7 +744,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 11 unmodified lines | |||
| 741 | int nup, j; | 744 | int nup, j; |
| 742 | p = cl->p->p[GETARG_Bx(i)]; | 745 | p = cl->p->p[GETARG_Bx(i)]; |
| 743 | nup = p->nups; | 746 | nup = p->nups; |
| 744 | ncl = luaF_newLclosure(L, nup, &cl->g); | 747 | ncl = luaF_newLclosure(L, nup, cl->env); |
| 745 | ncl->l.p = p; | 748 | ncl->l.p = p; |
| 746 | for (j=0; j<nup; j++, pc++) { | 749 | for (j=0; j<nup; j++, pc++) { |
| 747 | if (GET_OPCODE(*pc) == OP_GETUPVAL) | 750 | if (GET_OPCODE(*pc) == OP_GETUPVAL) |