History

Lua 5.1-work5

Lua Team committed 21 years ago

Commit e684a4d1 parent 930912737 files · +938 −717Browse files

Unified · Split

37 files
  1. COPYRIGHT +1 −1
  2. HISTORY +19 −0
  3. MANIFEST +104 −104
  4. Makefile +1 −1
  5. src/Makefile +11 −8
  6. src/lapi.c +62 −27
  7. src/lauxlib.c +4 −3
  8. src/lauxlib.h +46 −43
  9. src/lbaselib.c +15 −14
  10. src/lcode.c +2 −2
  11. src/ldblib.c +46 −5
  12. src/ldo.c +2 −2
  13. src/lfunc.c +42 −11
  14. src/lfunc.h +4 −3
  15. src/lgc.c +91 −107
  16. src/lgc.h +14 −9
  17. src/linit.c +3 −1
  18. src/liolib.c +17 −17
  19. src/llimits.h +1 −3
  20. src/lmathlib.c +1 −3
  21. src/lmem.c +24 −8
  22. src/loadlib.c +55 −27
  23. src/lobject.c +2 −2
  24. src/lobject.h +14 −5
  25. src/loslib.c +3 −5
  26. src/lparser.c +2 −2
  27. src/lstate.c +16 −27
  28. src/lstate.h +9 −10
  29. src/lstring.c +6 −5
  30. src/lstring.h +3 −3
  31. src/ltable.c +125 −96
  32. src/ltable.h +4 −2
  33. src/lua.c +16 −4
  34. src/lua.h +78 −78
  35. src/luaconf.h +46 −33
  36. src/lualib.h +10 −10
  37. src/lvm.c +39 −36
COPYRIGHT+1−1
@@ -9,7 +9,7 @@ For details and rationale, see http://www.lua.org/license.html . · 8 unmodified lines
99
1010 ===============================================================================
1111
12−Copyright (C) 2003-2004 Tecgraf, PUC-Rio.
12+Copyright (C) 1994-2005 Tecgraf, PUC-Rio.
1313
1414 Permission is hereby granted, free of charge, to any person obtaining a copy
1515 of this software and associated documentation files (the "Software"), to deal
HISTORY+19−0
@@ -1,5 +1,24 @@
11 This is Lua 5.1 (work).
22
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+
322 * Changes from version 4.0 to 5.0
423 -------------------------------
524 Language:
MANIFEST+104−104
@@ -1,105 +1,105 @@
1−MANIFEST contents of Lua 5.1 (work4) distribution on Thu Dec 30 12:00:49 BRST 2004
2−lua-5.1-work4
3−lua-5.1-work4/COPYRIGHT
4−lua-5.1-work4/HISTORY
5−lua-5.1-work4/INSTALL
6−lua-5.1-work4/MANIFEST
7−lua-5.1-work4/Makefile
8−lua-5.1-work4/README
9−lua-5.1-work4/doc
10−lua-5.1-work4/doc/contents.html
11−lua-5.1-work4/doc/logo.gif
12−lua-5.1-work4/doc/lua.1
13−lua-5.1-work4/doc/lua.html
14−lua-5.1-work4/doc/luac.1
15−lua-5.1-work4/doc/luac.html
16−lua-5.1-work4/doc/readme.html
17−lua-5.1-work4/etc
18−lua-5.1-work4/etc/Makefile
19−lua-5.1-work4/etc/README
20−lua-5.1-work4/etc/all.c
21−lua-5.1-work4/etc/lua.hpp
22−lua-5.1-work4/etc/lua.ico
23−lua-5.1-work4/etc/min.c
24−lua-5.1-work4/etc/noparser.c
25−lua-5.1-work4/etc/saconfig.c
26−lua-5.1-work4/src
27−lua-5.1-work4/src/Makefile
28−lua-5.1-work4/src/lapi.c
29−lua-5.1-work4/src/lapi.h
30−lua-5.1-work4/src/lauxlib.c
31−lua-5.1-work4/src/lauxlib.h
32−lua-5.1-work4/src/lbaselib.c
33−lua-5.1-work4/src/lcode.c
34−lua-5.1-work4/src/lcode.h
35−lua-5.1-work4/src/ldblib.c
36−lua-5.1-work4/src/ldebug.c
37−lua-5.1-work4/src/ldebug.h
38−lua-5.1-work4/src/ldo.c
39−lua-5.1-work4/src/ldo.h
40−lua-5.1-work4/src/ldump.c
41−lua-5.1-work4/src/lfunc.c
42−lua-5.1-work4/src/lfunc.h
43−lua-5.1-work4/src/lgc.c
44−lua-5.1-work4/src/lgc.h
45−lua-5.1-work4/src/linit.c
46−lua-5.1-work4/src/liolib.c
47−lua-5.1-work4/src/llex.c
48−lua-5.1-work4/src/llex.h
49−lua-5.1-work4/src/llimits.h
50−lua-5.1-work4/src/lmathlib.c
51−lua-5.1-work4/src/lmem.c
52−lua-5.1-work4/src/lmem.h
53−lua-5.1-work4/src/loadlib.c
54−lua-5.1-work4/src/lobject.c
55−lua-5.1-work4/src/lobject.h
56−lua-5.1-work4/src/lopcodes.c
57−lua-5.1-work4/src/lopcodes.h
58−lua-5.1-work4/src/loslib.c
59−lua-5.1-work4/src/lparser.c
60−lua-5.1-work4/src/lparser.h
61−lua-5.1-work4/src/lstate.c
62−lua-5.1-work4/src/lstate.h
63−lua-5.1-work4/src/lstring.c
64−lua-5.1-work4/src/lstring.h
65−lua-5.1-work4/src/lstrlib.c
66−lua-5.1-work4/src/ltable.c
67−lua-5.1-work4/src/ltable.h
68−lua-5.1-work4/src/ltablib.c
69−lua-5.1-work4/src/ltm.c
70−lua-5.1-work4/src/ltm.h
71−lua-5.1-work4/src/lua.c
72−lua-5.1-work4/src/lua.h
73−lua-5.1-work4/src/luac.c
74−lua-5.1-work4/src/luaconf.h
75−lua-5.1-work4/src/lualib.h
76−lua-5.1-work4/src/lundump.c
77−lua-5.1-work4/src/lundump.h
78−lua-5.1-work4/src/lvm.c
79−lua-5.1-work4/src/lvm.h
80−lua-5.1-work4/src/lzio.c
81−lua-5.1-work4/src/lzio.h
82−lua-5.1-work4/src/print.c
83−lua-5.1-work4/test
84−lua-5.1-work4/test/README
85−lua-5.1-work4/test/bisect.lua
86−lua-5.1-work4/test/cf.lua
87−lua-5.1-work4/test/echo.lua
88−lua-5.1-work4/test/env.lua
89−lua-5.1-work4/test/factorial.lua
90−lua-5.1-work4/test/fib.lua
91−lua-5.1-work4/test/fibfor.lua
92−lua-5.1-work4/test/globals.lua
93−lua-5.1-work4/test/hello.lua
94−lua-5.1-work4/test/life.lua
95−lua-5.1-work4/test/luac.lua
96−lua-5.1-work4/test/printf.lua
97−lua-5.1-work4/test/readonly.lua
98−lua-5.1-work4/test/sieve.lua
99−lua-5.1-work4/test/sort.lua
100−lua-5.1-work4/test/table.lua
101−lua-5.1-work4/test/trace-calls.lua
102−lua-5.1-work4/test/trace-globals.lua
103−lua-5.1-work4/test/undefined.lua
104−lua-5.1-work4/test/xd.lua
1+MANIFEST contents of Lua 5.1 (work5) distribution on Fri Mar 4 13:32:55 BRST 2005
2+lua-5.1-work5
3+lua-5.1-work5/COPYRIGHT
4+lua-5.1-work5/HISTORY
5+lua-5.1-work5/INSTALL
6+lua-5.1-work5/MANIFEST
7+lua-5.1-work5/Makefile
8+lua-5.1-work5/README
9+lua-5.1-work5/doc
10+lua-5.1-work5/doc/contents.html
11+lua-5.1-work5/doc/logo.gif
12+lua-5.1-work5/doc/lua.1
13+lua-5.1-work5/doc/lua.html
14+lua-5.1-work5/doc/luac.1
15+lua-5.1-work5/doc/luac.html
16+lua-5.1-work5/doc/readme.html
17+lua-5.1-work5/etc
18+lua-5.1-work5/etc/Makefile
19+lua-5.1-work5/etc/README
20+lua-5.1-work5/etc/all.c
21+lua-5.1-work5/etc/lua.hpp
22+lua-5.1-work5/etc/lua.ico
23+lua-5.1-work5/etc/min.c
24+lua-5.1-work5/etc/noparser.c
25+lua-5.1-work5/etc/saconfig.c
26+lua-5.1-work5/src
27+lua-5.1-work5/src/Makefile
28+lua-5.1-work5/src/lapi.c
29+lua-5.1-work5/src/lapi.h
30+lua-5.1-work5/src/lauxlib.c
31+lua-5.1-work5/src/lauxlib.h
32+lua-5.1-work5/src/lbaselib.c
33+lua-5.1-work5/src/lcode.c
34+lua-5.1-work5/src/lcode.h
35+lua-5.1-work5/src/ldblib.c
36+lua-5.1-work5/src/ldebug.c
37+lua-5.1-work5/src/ldebug.h
38+lua-5.1-work5/src/ldo.c
39+lua-5.1-work5/src/ldo.h
40+lua-5.1-work5/src/ldump.c
41+lua-5.1-work5/src/lfunc.c
42+lua-5.1-work5/src/lfunc.h
43+lua-5.1-work5/src/lgc.c
44+lua-5.1-work5/src/lgc.h
45+lua-5.1-work5/src/linit.c
46+lua-5.1-work5/src/liolib.c
47+lua-5.1-work5/src/llex.c
48+lua-5.1-work5/src/llex.h
49+lua-5.1-work5/src/llimits.h
50+lua-5.1-work5/src/lmathlib.c
51+lua-5.1-work5/src/lmem.c
52+lua-5.1-work5/src/lmem.h
53+lua-5.1-work5/src/loadlib.c
54+lua-5.1-work5/src/lobject.c
55+lua-5.1-work5/src/lobject.h
56+lua-5.1-work5/src/lopcodes.c
57+lua-5.1-work5/src/lopcodes.h
58+lua-5.1-work5/src/loslib.c
59+lua-5.1-work5/src/lparser.c
60+lua-5.1-work5/src/lparser.h
61+lua-5.1-work5/src/lstate.c
62+lua-5.1-work5/src/lstate.h
63+lua-5.1-work5/src/lstring.c
64+lua-5.1-work5/src/lstring.h
65+lua-5.1-work5/src/lstrlib.c
66+lua-5.1-work5/src/ltable.c
67+lua-5.1-work5/src/ltable.h
68+lua-5.1-work5/src/ltablib.c
69+lua-5.1-work5/src/ltm.c
70+lua-5.1-work5/src/ltm.h
71+lua-5.1-work5/src/lua.c
72+lua-5.1-work5/src/lua.h
73+lua-5.1-work5/src/luac.c
74+lua-5.1-work5/src/luaconf.h
75+lua-5.1-work5/src/lualib.h
76+lua-5.1-work5/src/lundump.c
77+lua-5.1-work5/src/lundump.h
78+lua-5.1-work5/src/lvm.c
79+lua-5.1-work5/src/lvm.h
80+lua-5.1-work5/src/lzio.c
81+lua-5.1-work5/src/lzio.h
82+lua-5.1-work5/src/print.c
83+lua-5.1-work5/test
84+lua-5.1-work5/test/README
85+lua-5.1-work5/test/bisect.lua
86+lua-5.1-work5/test/cf.lua
87+lua-5.1-work5/test/echo.lua
88+lua-5.1-work5/test/env.lua
89+lua-5.1-work5/test/factorial.lua
90+lua-5.1-work5/test/fib.lua
91+lua-5.1-work5/test/fibfor.lua
92+lua-5.1-work5/test/globals.lua
93+lua-5.1-work5/test/hello.lua
94+lua-5.1-work5/test/life.lua
95+lua-5.1-work5/test/luac.lua
96+lua-5.1-work5/test/printf.lua
97+lua-5.1-work5/test/readonly.lua
98+lua-5.1-work5/test/sieve.lua
99+lua-5.1-work5/test/sort.lua
100+lua-5.1-work5/test/table.lua
101+lua-5.1-work5/test/trace-calls.lua
102+lua-5.1-work5/test/trace-globals.lua
103+lua-5.1-work5/test/undefined.lua
104+lua-5.1-work5/test/xd.lua
105105 END OF MANIFEST
Makefile+1−1
@@ -44,7 +44,7 @@ install: all · 43 unmodified lines
4444 cd doc; $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN)
4545
4646 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"
4848
4949 # echo config parameters
5050 echo:
src/Makefile+11−8
@@ -6,13 +6,15 @@ · 5 unmodified lines
66
77 CC= gcc
88 CFLAGS= -O2 -Wall $(MYCFLAGS)
9−MYCFLAGS=
10−MYLDFLAGS= -Wl,-E # enable dynamic loading in Linux
11−MYLIBS= -lm -ldl # enable dynamic loading in Linux
129 AR= ar rcu
1310 RANLIB= ranlib
1411 RM= rm -f
1512
13+MYCFLAGS=
14+MYLDFLAGS=
15+MYLIBS= -lm
16+DL= -ldl -Wl,-E # enable dynamic loading in Linux
17+
1618 # == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE =========
1719
1820 CORE_T= liblua.a
@@ -49,10 +51,10 @@ $(LIB_T): $(LIB_O) · 30 unmodified lines
4951 $(RANLIB) $@
5052
5153 $(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)
5355
5456 $(LUAC_T): $(LUAC_O) $(CORE_T)
55− $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) -L. -llua
57+ $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) -L. -llua $(MYLIBS)
5658
5759 clean:
5860 $(RM) $(ALL_T) $(ALL_O)
@@ -63,12 +65,13 @@ depend: · 4 unmodified lines
6365 echo:
6466 @echo "CC = $(CC)"
6567 @echo "CFLAGS = $(CFLAGS)"
66− @echo "MYCFLAGS = $(MYCFLAGS)"
67− @echo "MYLDFLAGS = $(MYLDFLAGS)"
68− @echo "MYLIBS = $(MYLIBS)"
6968 @echo "AR = $(AR)"
7069 @echo "RANLIB = $(RANLIB)"
7170 @echo "RM = $(RM)"
71+ @echo "MYCFLAGS = $(MYCFLAGS)"
72+ @echo "MYLDFLAGS = $(MYLDFLAGS)"
73+ @echo "MYLIBS = $(MYLIBS)"
74+ @echo "DL = $(DL)"
7275
7376 # DO NOT DELETE
7477
src/lapi.c+62−27
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Lua API
44 ** See Copyright Notice in lua.h
55 */
@@ -58,6 +58,11 @@ static TValue *index2adr (lua_State *L, int idx) { · 52 unmodified lines
5858 }
5959 else switch (idx) { /* pseudo-indices */
6060 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+ }
6166 case LUA_GLOBALSINDEX: return gt(L);
6267 default: {
6368 Closure *func = curr_func(L);
@@ -70,6 +75,16 @@ static TValue *index2adr (lua_State *L, int idx) { · 6 unmodified lines
7075 }
7176
7277
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+
7388 void luaA_pushobject (lua_State *L, const TValue *o) {
7489 setobj2s(L, L->top, o);
7590 incr_top(L);
@@ -186,9 +201,17 @@ LUA_API void lua_replace (lua_State *L, int idx) { · 110 unmodified lines
186201 api_checknelems(L, 1);
187202 o = index2adr(L, idx);
188203 api_checkvalidindex(L, o);
189− setobj(L, o, L->top - 1);
190− if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
191− luaC_barrier(L, curr_func(L), L->top - 1);
204+ if (idx == LUA_ENVIRONINDEX) {
205+ Closure *func = curr_func(L);
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+ }
192215 L->top--;
193216 lua_unlock(L);
194217 }
@@ -452,7 +475,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { · 257 unmodified lines
452475 lua_lock(L);
453476 luaC_checkGC(L);
454477 api_checknelems(L, n);
455− cl = luaF_newCclosure(L, n);
478+ cl = luaF_newCclosure(L, n, getcurrenv(L));
456479 cl->c.f = fn;
457480 L->top -= n;
458481 while (n--)
@@ -542,7 +565,7 @@ LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { · 83 unmodified lines
542565 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
543566 lua_lock(L);
544567 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));
546569 api_incr_top(L);
547570 lua_unlock(L);
548571 }
@@ -579,7 +602,17 @@ LUA_API void lua_getfenv (lua_State *L, int idx) { · 30 unmodified lines
579602 lua_lock(L);
580603 o = index2adr(L, idx);
581604 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+ }
583616 api_incr_top(L);
584617 lua_unlock(L);
585618 }
@@ -682,17 +715,24 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { · 96 unmodified lines
682715
683716 LUA_API int lua_setfenv (lua_State *L, int idx) {
684717 StkId o;
685− int res = 0;
718+ int res = 1;
686719 lua_lock(L);
687720 api_checknelems(L, 1);
688721 o = index2adr(L, idx);
689722 api_checkvalidindex(L, o);
690723 api_check(L, ttistable(L->top - 1));
691− if (isLfunction(o)) {
692− res = 1;
693− clvalue(o)->l.g = *(L->top - 1);
694− luaC_objbarrier(L, clvalue(o), hvalue(L->top - 1));
724+ switch (ttype(o)) {
725+ case LUA_TFUNCTION:
726+ clvalue(o)->c.env = 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;
695734 }
735+ luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
696736 L->top--;
697737 lua_unlock(L);
698738 return res;
@@ -776,7 +816,7 @@ struct CCallS { /* data to `f_Ccall' */ · 77 unmodified lines
776816 static void f_Ccall (lua_State *L, void *ud) {
777817 struct CCallS *c = cast(struct CCallS *, ud);
778818 Closure *cl;
779− cl = luaF_newCclosure(L, 0);
819+ cl = luaF_newCclosure(L, 0, getcurrenv(L));
780820 cl->c.f = c->func;
781821 setclvalue(L, L->top, cl); /* push function */
782822 incr_top(L);
@@ -826,7 +866,7 @@ LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data) { · 43 unmodified lines
826866 }
827867
828868
829−LUA_API int lua_threadstatus (lua_State *L) {
869+LUA_API int lua_status (lua_State *L) {
830870 return L->status;
831871 }
832872
@@ -872,9 +912,9 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { · 39 unmodified lines
872912 g->gcpace = data;
873913 break;
874914 }
875− case LUA_GCSETINCMODE: {
876− res = g->incgc;
877− g->incgc = data;
915+ case LUA_GCSETSTEPMUL: {
916+ res = g->gcstepmul;
917+ g->gcstepmul = data;
878918 break;
879919 }
880920 default: res = -1; /* invalid option */
@@ -890,11 +930,6 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { · 9 unmodified lines
890930 */
891931
892932
893−LUA_API const char *lua_version (void) {
894− return LUA_VERSION;
895−}
896−
897−
898933 LUA_API int lua_error (lua_State *L) {
899934 lua_lock(L);
900935 api_checknelems(L, 1);
@@ -940,7 +975,7 @@ LUA_API void lua_concat (lua_State *L, int n) { · 39 unmodified lines
940975
941976 LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
942977 *ud = G(L)->ud;
943− return G(L)->realloc;
978+ return G(L)->frealloc;
944979 }
945980
946981
@@ -948,7 +983,7 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { · 1 unmodified line
948983 Udata *u;
949984 lua_lock(L);
950985 luaC_checkGC(L);
951− u = luaS_newudata(L, size);
986+ u = luaS_newudata(L, size, getcurrenv(L));
952987 setuvalue(L, L->top, u);
953988 api_incr_top(L);
954989 lua_unlock(L);
@@ -958,7 +993,7 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { · 3 unmodified lines
958993
959994
960995
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) {
962997 Closure *f;
963998 if (!ttisfunction(fi)) return NULL;
964999 f = clvalue(fi);
@@ -980,7 +1015,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { · 15 unmodified lines
9801015 const char *name;
9811016 TValue *val;
9821017 lua_lock(L);
983− name = aux_upvalue(L, index2adr(L, funcindex), n, &val);
1018+ name = aux_upvalue(index2adr(L, funcindex), n, &val);
9841019 if (name) {
9851020 setobj2s(L, L->top, val);
9861021 api_incr_top(L);
@@ -997,7 +1032,7 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { · 10 unmodified lines
9971032 lua_lock(L);
9981033 fi = index2adr(L, funcindex);
9991034 api_checknelems(L, 1);
1000− name = aux_upvalue(L, fi, n, &val);
1035+ name = aux_upvalue(fi, n, &val);
10011036 if (name) {
10021037 L->top--;
10031038 setobj(L, val, L->top);
src/lauxlib.c+4−3
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Auxiliary functions for building Lua libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -350,8 +350,8 @@ LUALIB_API int luaL_getn (lua_State *L, int t) { · 344 unmodified lines
350350 static const char *pushnexttemplate (lua_State *L, const char *path) {
351351 const char *l;
352352 if (*path == '\0') return NULL; /* no more templates */
353− if (*path == LUA_PATH_SEP) path++; /* skip separator */
354− l = strchr(path, LUA_PATH_SEP); /* find next separator */
353+ if (*path == LUA_PATHSEP) path++; /* skip separator */
354+ l = strchr(path, LUA_PATHSEP); /* find next separator */
355355 if (l == NULL) l = path+strlen(path);
356356 lua_pushlstring(L, path, l - path); /* template */
357357 return l;
@@ -704,6 +704,7 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { · 346 unmodified lines
704704
705705
706706 static int panic (lua_State *L) {
707+ (void)L; /* to avoid warnings */
707708 fprintf(stderr, "PANIC: unprotected error during Lua-API call\n");
708709 return 0;
709710 }
src/lauxlib.h+46−43
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Auxiliary functions for building Lua libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -25,55 +25,58 @@ typedef struct luaL_reg { · 19 unmodified lines
2525 } luaL_reg;
2626
2727
28−LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
29− const luaL_reg *l, int nup);
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);
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);
34−LUALIB_API const char *luaL_checklstring (lua_State *L, int numArg, size_t *l);
35−LUALIB_API const char *luaL_optlstring (lua_State *L, int numArg,
36− const char *def, size_t *l);
37−LUALIB_API lua_Number luaL_checknumber (lua_State *L, int numArg);
38−LUALIB_API lua_Number luaL_optnumber (lua_State *L, int nArg, lua_Number def);
28+LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname,
29+ const luaL_reg *l, int nup);
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);
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);
34+LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg,
35+ size_t *l);
36+LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg,
37+ const char *def, size_t *l);
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);
3940
40−LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int numArg);
41−LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int nArg,
42− lua_Integer def);
41+LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
42+LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
43+ lua_Integer def);
4344
44−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_checkany (lua_State *L, int narg);
45+LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
46+LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
47+LUALIB_API void (luaL_checkany) (lua_State *L, int narg);
4748
48−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_checkudata (lua_State *L, int ud, const char *tname);
49+LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname);
50+LUALIB_API void (luaL_getmetatable) (lua_State *L, const char *tname);
51+LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
5152
52−LUALIB_API void luaL_where (lua_State *L, int lvl);
53−LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...);
53+LUALIB_API void (luaL_where) (lua_State *L, int lvl);
54+LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
5455
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[]);
5657
57−LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
58− const char *path);
58+LUALIB_API const char *(luaL_searchpath) (lua_State *L, const char *name,
59+ const char *path);
5960
60−LUALIB_API int luaL_ref (lua_State *L, int t);
61−LUALIB_API void luaL_unref (lua_State *L, int t, int ref);
61+LUALIB_API int (luaL_ref) (lua_State *L, int t);
62+LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
6263
63−LUALIB_API int luaL_getn (lua_State *L, int t);
64−LUALIB_API void luaL_setn (lua_State *L, int t, int n);
64+LUALIB_API int (luaL_getn) (lua_State *L, int t);
65+LUALIB_API void (luaL_setn) (lua_State *L, int t, int n);
6566
66−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− const char *name);
67+LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
68+LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
69+ const char *name);
6970
7071 LUALIB_API lua_State *(luaL_newstate) (void);
7172
7273
73−LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
74− const char *r);
75−LUALIB_API const char *luaL_getfield (lua_State *L, int idx, const char *fname);
76−LUALIB_API const char *luaL_setfield (lua_State *L, int idx, const char *fname);
74+LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
75+ const char *r);
76+LUALIB_API const char *(luaL_getfield) (lua_State *L, int idx,
77+ const char *fname);
78+LUALIB_API const char *(luaL_setfield) (lua_State *L, int idx,
79+ const char *fname);
7780
7881
7982
@@ -115,12 +118,12 @@ typedef struct luaL_Buffer { · 35 unmodified lines
115118
116119 #define luaL_addsize(B,n) ((B)->p += (n))
117120
118−LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B);
119−LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B);
120−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);
122−LUALIB_API void luaL_addvalue (luaL_Buffer *B);
123−LUALIB_API void luaL_pushresult (luaL_Buffer *B);
121+LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
122+LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B);
123+LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
124+LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s);
125+LUALIB_API void (luaL_addvalue) (luaL_Buffer *B);
126+LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
124127
125128
126129 /* }====================================================== */
src/lbaselib.c+15−14
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Basic library
44 ** See Copyright Notice in lua.h
55 */
@@ -131,7 +131,10 @@ static void getfunc (lua_State *L) { · 125 unmodified lines
131131
132132 static int luaB_getfenv (lua_State *L) {
133133 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);
135138 return 1;
136139 }
137140
@@ -144,8 +147,8 @@ static int luaB_setfenv (lua_State *L) { · 6 unmodified lines
144147 lua_replace(L, LUA_GLOBALSINDEX);
145148 return 0;
146149 }
147− else if (lua_setfenv(L, -2) == 0)
148− luaL_error(L, "`setfenv' cannot change environment of given function");
150+ else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
151+ luaL_error(L, "`setfenv' cannot change environment of given object");
149152 return 1;
150153 }
151154
@@ -182,11 +185,11 @@ static int luaB_gcinfo (lua_State *L) { · 30 unmodified lines
182185
183186 static int luaB_collectgarbage (lua_State *L) {
184187 static const char *const opts[] = {"stop", "restart", "collect",
185− "count", "step", "setpace", "setincmode", NULL};
188+ "count", "step", "setpace", "setstepmul", NULL};
186189 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};
188191 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);
190193 luaL_argcheck(L, o >= 0, 1, "invalid option");
191194 lua_pushinteger(L, lua_gc(L, optsnum[o], ex));
192195 return 1;
@@ -280,6 +283,7 @@ static int luaB_loadfile (lua_State *L) { · 87 unmodified lines
280283 ** reserved slot inside the stack.
281284 */
282285 static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
286+ (void)ud; /* to avoid warnings */
283287 luaL_checkstack(L, 2, "too many nested functions");
284288 lua_pushvalue(L, 1); /* get function */
285289 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
469473 int status;
470474 if (!lua_checkstack(co, narg))
471475 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) {
473477 lua_pushliteral(L, "cannot resume dead coroutine");
474478 return -1; /* error flag */
475479 }
@@ -549,7 +553,7 @@ static int luaB_costatus (lua_State *L) { · 73 unmodified lines
549553 luaL_argcheck(L, co, 1, "coroutine expected");
550554 if (L == co) lua_pushliteral(L, "running");
551555 else {
552− switch (lua_threadstatus(co)) {
556+ switch (lua_status(co)) {
553557 case LUA_YIELD:
554558 lua_pushliteral(L, "suspended");
555559 break;
@@ -572,7 +576,7 @@ static int luaB_costatus (lua_State *L) { · 16 unmodified lines
572576 }
573577
574578
575−static int luaB_cocurrent (lua_State *L) {
579+static int luaB_corunning (lua_State *L) {
576580 if (lua_pushthread(L))
577581 return 0; /* main thread is not a coroutine */
578582 else
@@ -586,7 +590,7 @@ static const luaL_reg co_funcs[] = { · 7 unmodified lines
586590 {"resume", luaB_coresume},
587591 {"yield", luaB_yield},
588592 {"status", luaB_costatus},
589− {"current", luaB_cocurrent},
593+ {"running", luaB_corunning},
590594 {NULL, NULL}
591595 };
592596
@@ -620,9 +624,6 @@ static void base_open (lua_State *L) { · 27 unmodified lines
620624 /* create register._LOADED to track loaded modules */
621625 lua_newtable(L);
622626 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");
626627 /* set global _G */
627628 lua_pushvalue(L, LUA_GLOBALSINDEX);
628629 lua_setglobal(L, "_G");
src/lcode.c+2−2
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Code generator for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -606,7 +606,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { · 600 unmodified lines
606606 if (op == OPR_MINUS) {
607607 luaK_exp2val(fs, e);
608608 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])));
610610 else {
611611 luaK_exp2anyreg(fs, e);
612612 freeexp(fs, e);
src/ldblib.c+46−5
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Interface from Lua to its debug API
44 ** See Copyright Notice in lua.h
55 */
@@ -19,6 +19,40 @@ · 13 unmodified lines
1919
2020
2121
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+
2256 static void settabss (lua_State *L, const char *i, const char *v) {
2357 lua_pushstring(L, v);
2458 lua_setfield(L, -2, i);
@@ -275,12 +309,17 @@ static int debug (lua_State *L) { · 250 unmodified lines
275309 #define LEVELS2 10 /* size of the second part of the stack */
276310
277311 static int errorfb (lua_State *L) {
278− int level = 0;
312+ int level;
279313 int firstpart = 1; /* still before eventual `...' */
280314 int arg;
281315 lua_State *L1 = getthread(L, &arg);
282316 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 */
284323 if (lua_gettop(L) == arg)
285324 lua_pushliteral(L, "");
286325 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
323362
324363
325364 static const luaL_reg dblib[] = {
365+ {"getmetatable", getmetatable},
366+ {"setmetatable", setmetatable},
367+ {"getfenv", getfenv},
368+ {"setfenv", setfenv},
326369 {"getlocal", getlocal},
327370 {"getinfo", getinfo},
328371 {"gethook", gethook},
@@ -338,8 +381,6 @@ static const luaL_reg dblib[] = { · 9 unmodified lines
338381
339382 LUALIB_API int luaopen_debug (lua_State *L) {
340383 luaL_openlib(L, LUA_DBLIBNAME, dblib, 0);
341− lua_pushcfunction(L, errorfb);
342− lua_setglobal(L, "_TRACEBACK");
343384 return 1;
344385 }
345386
src/ldo.c+2−2
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Stack and Call structure of Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -474,7 +474,7 @@ static void f_parser (lua_State *L, void *ud) { · 468 unmodified lines
474474 luaC_checkGC(L);
475475 tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
476476 &p->buff, p->name);
477− cl = luaF_newLclosure(L, tf->nups, gt(L));
477+ cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
478478 cl->l.p = tf;
479479 for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */
480480 cl->l.upvals[i] = luaF_newupval(L);
src/lfunc.c+42−11
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Auxiliary functions to manipulate prototypes and closures
44 ** See Copyright Notice in lua.h
55 */
@@ -20,20 +20,21 @@ · 14 unmodified lines
2020
2121
2222
23−Closure *luaF_newCclosure (lua_State *L, int nelems) {
23+Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) {
2424 Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
2525 luaC_link(L, obj2gco(c), LUA_TFUNCTION);
2626 c->c.isC = 1;
27+ c->c.env = e;
2728 c->c.nupvalues = cast(lu_byte, nelems);
2829 return c;
2930 }
3031
3132
32−Closure *luaF_newLclosure (lua_State *L, int nelems, TValue *e) {
33+Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) {
3334 Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
3435 luaC_link(L, obj2gco(c), LUA_TFUNCTION);
3536 c->l.isC = 0;
36− c->l.g = *e;
37+ c->l.env = e;
3738 c->l.nupvalues = cast(lu_byte, nelems);
3839 while (nelems--) c->l.upvals[nelems] = NULL;
3940 return c;
@@ -43,42 +44,72 @@ Closure *luaF_newLclosure (lua_State *L, int nelems, TValue *e) { · 3 unmodified lines
4344 UpVal *luaF_newupval (lua_State *L) {
4445 UpVal *uv = luaM_new(L, UpVal);
4546 luaC_link(L, obj2gco(uv), LUA_TUPVAL);
46− uv->v = &uv->value;
47+ uv->v = &uv->u.value;
4748 setnilvalue(uv->v);
4849 return uv;
4950 }
5051
5152
5253 UpVal *luaF_findupval (lua_State *L, StkId level) {
54+ global_State *g = G(L);
5355 GCObject **pp = &L->openupval;
5456 UpVal *p;
5557 UpVal *uv;
5658 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+ }
5865 pp = &p->next;
5966 }
6067 uv = luaM_new(L, UpVal); /* not found: create a new one */
6168 uv->tt = LUA_TUPVAL;
62− uv->marked = luaC_white(G(L));
69+ uv->marked = luaC_white(g);
6370 uv->v = level; /* current value lives in the stack */
6471 uv->next = *pp; /* chain it in the proper position */
6572 *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);
6678 return uv;
6779 }
6880
6981
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+
7096 void luaF_close (lua_State *L, StkId level) {
7197 UpVal *uv;
7298 global_State *g = G(L);
7399 while ((uv = ngcotouv(L->openupval)) != NULL && uv->v >= level) {
74100 GCObject *o = obj2gco(uv);
75− lua_assert(!isblack(o));
101+ lua_assert(!isblack(o) && uv->v != &uv->u.value);
76102 L->openupval = uv->next; /* remove from `open' list */
77103 if (isdead(g, o))
78− luaM_free(L, uv); /* free upvalue */
104+ luaF_freeupval(L, uv); /* free upvalue */
79105 else {
80− setobj(L, &uv->value, uv->v);
81− uv->v = &uv->value; /* now current value lives here */
106+ unlinkupval(uv);
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 */
82113 luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */
83114 }
84115 }
src/lfunc.h+4−3
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Auxiliary functions to manipulate prototypes and closures
44 ** See Copyright Notice in lua.h
55 */
@@ -19,13 +19,14 @@ · 13 unmodified lines
1919
2020
2121 Proto *luaF_newproto (lua_State *L);
22−Closure *luaF_newCclosure (lua_State *L, int nelems);
23−Closure *luaF_newLclosure (lua_State *L, int nelems, TValue *e);
22+Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e);
23+Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e);
2424 UpVal *luaF_newupval (lua_State *L);
2525 UpVal *luaF_findupval (lua_State *L, StkId level);
2626 void luaF_close (lua_State *L, StkId level);
2727 void luaF_freeproto (lua_State *L, Proto *f);
2828 void luaF_freeclosure (lua_State *L, Closure *c);
29+void luaF_freeupval (lua_State *L, UpVal *uv);
2930
3031 const char *luaF_getlocalname (const Proto *func, int local_number, int pc);
3132
src/lgc.c+91−107
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Garbage Collector
44 ** See Copyright Notice in lua.h
55 */
@@ -23,30 +23,26 @@ · 17 unmodified lines
2323 #include "ltm.h"
2424
2525
26−#define GCSTEPSIZE 1000
27−#define GCSWEEPMAX 10
28−#define GCSWEEPCOST 30
26+#define GCSTEPSIZE 1024u
27+#define GCSWEEPMAX 40
28+#define GCSWEEPCOST 10
2929 #define GCFINALIZECOST 100
30−#define GCSTEPMUL 8
3130
3231
33−#define FIXEDMASK bitmask(FIXEDBIT)
34−
3532 #define maskmarks \
36− cast(lu_byte, ~(bitmask(BLACKBIT)|bit2mask(WHITE0BIT, WHITE1BIT)))
33+ cast(lu_byte, ~(bitmask(BLACKBIT)|WHITEBITS))
3734
3835 #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))
4037
4138 #define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
42−#define gray2black(x) setbit((x)->gch.marked, BLACKBIT)
4339 #define black2gray(x) resetbit((x)->gch.marked, BLACKBIT)
4440
4541 #define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT)
4642
4743
4844 #define isfinalized(u) testbit((u)->marked, FINALIZEDBIT)
49−#define markfinalized(u) setbit((u)->marked, FINALIZEDBIT)
45+#define markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT)
5046
5147
5248 #define KEYWEAK bitmask(KEYWEAKBIT)
@@ -61,9 +57,11 @@ · 8 unmodified lines
6157 reallymarkobject(g, obj2gco(t)); }
6258
6359
60+#define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpace)
61+
6462
6563 static void removeentry (Node *n) {
66− setnilvalue(gval(n)); /* remove corresponding value ... */
64+ lua_assert(ttisnil(gval(n)));
6765 if (iscollectable(gkey(n)))
6866 setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */
6967 }
@@ -80,14 +78,14 @@ static void reallymarkobject (global_State *g, GCObject *o) { · 10 unmodified lines
8078 Table *mt = gco2u(o)->metatable;
8179 gray2black(o); /* udata are never gray */
8280 if (mt) markobject(g, mt);
81+ markobject(g, gco2u(o)->env);
8382 return;
8483 }
8584 case LUA_TUPVAL: {
8685 UpVal *uv = gco2uv(o);
87− if (uv->v == &uv->value) { /* closed? */
88− markvalue(g, uv->v);
89− gray2black(o);
90− }
86+ markvalue(g, uv->v);
87+ if (uv->v == &uv->u.value) /* closed? */
88+ gray2black(o); /* open upvalues are never black */
9189 return;
9290 }
9391 case LUA_TFUNCTION: {
@@ -116,22 +114,24 @@ static void reallymarkobject (global_State *g, GCObject *o) { · 22 unmodified lines
116114
117115
118116 static void marktmu (global_State *g) {
119− GCObject *u;
120− for (u = g->tmudata; u; u = u->gch.next) {
121− makewhite(g, u); /* may be marked, if left from previous GC */
122− reallymarkobject(g, u);
117+ GCObject *u = g->tmudata;
118+ if (u) {
119+ do {
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);
123124 }
124125 }
125126
126127
127128 /* move `dead' udata that need finalization to list `tmudata' */
128129 size_t luaC_separateudata (lua_State *L, int all) {
130+ global_State *g = G(L);
129131 size_t deadmem = 0;
130− GCObject **p = &G(L)->firstudata;
132+ GCObject **p = &g->mainthread->next;
131133 GCObject *curr;
132− GCObject *collected = NULL; /* to collect udata with gc event */
133− GCObject **lastcollected = &collected;
134− while ((curr = *p)->gch.tt == LUA_TUSERDATA) {
134+ while ((curr = *p) != NULL) {
135135 if (!(iswhite(curr) || all) || isfinalized(gco2u(curr)))
136136 p = &curr->gch.next; /* don't bother with them */
137137 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
142142 deadmem += sizeudata(gco2u(curr));
143143 markfinalized(gco2u(curr));
144144 *p = curr->gch.next;
145− curr->gch.next = NULL; /* link `curr' at the end of `collected' list */
146− *lastcollected = curr;
147− lastcollected = &curr->gch.next;
145+ /* link `curr' at the end of `tmudata' list */
146+ if (g->tmudata == NULL) /* list is empty? */
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+ }
148153 }
149154 }
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;
154155 return deadmem;
155156 }
156157
@@ -162,7 +163,6 @@ static int traversetable (global_State *g, Table *h) { · 5 unmodified lines
162163 const TValue *mode;
163164 if (h->metatable)
164165 markobject(g, h->metatable);
165− lua_assert(h->lsizenode || h->node == g->dummynode);
166166 mode = gfasttm(g, h->metatable, TM_MODE);
167167 if (mode && ttisstring(mode)) { /* is there a weak mode? */
168168 weakkey = (strchr(svalue(mode), 'k') != NULL);
@@ -223,6 +223,7 @@ static void traverseproto (global_State *g, Proto *f) { · 54 unmodified lines
223223
224224
225225 static void traverseclosure (global_State *g, Closure *cl) {
226+ markobject(g, cl->c.env);
226227 if (cl->c.isC) {
227228 int i;
228229 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
231232 else {
232233 int i;
233234 lua_assert(cl->l.nupvalues == cl->l.p->nups);
234− markobject(g, hvalue(&cl->l.g));
235235 markobject(g, cl->l.p);
236236 for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */
237237 markobject(g, cl->l.upvals[i]);
@@ -368,8 +368,10 @@ static void cleartable (GCObject *l) { · 130 unmodified lines
368368 while (i--) {
369369 Node *n = gnode(h, i);
370370 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 ... */
372373 removeentry(n); /* remove entry from table */
374+ }
373375 }
374376 l = h->gclist;
375377 }
@@ -380,7 +382,7 @@ static void freeobj (lua_State *L, GCObject *o) { · 4 unmodified lines
380382 switch (o->gch.tt) {
381383 case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
382384 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;
384386 case LUA_TTABLE: luaH_free(L, gco2h(o)); break;
385387 case LUA_TTHREAD: {
386388 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
402404
403405
404406
405−#define sweepwholelist(L,p) sweeplist(L,p,LUA_MAXINT32)
407+#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM)
406408
407409
408−static GCObject **sweeplist (lua_State *L, GCObject **p, lu_int32 count) {
410+static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
409411 GCObject *curr;
410412 global_State *g = G(L);
411− int whitebit = otherwhite(g);
412− int deadmask = whitebit | FIXEDMASK;
413− int generational = g->gcgenerational;
413+ int deadmask = otherwhite(g);
414414 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? */
416418 lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT));
417− if (!generational || isdead(g, curr))
418− makewhite(g, curr);
419− if (curr->gch.tt == LUA_TTHREAD)
420− sweepwholelist(L, &gco2th(curr)->openupval);
419+ makewhite(g, curr); /* make it white (for next cycle) */
421420 p = &curr->gch.next;
422421 }
423− else {
424− lua_assert(isdead(g, curr));
422+ else { /* must erase `curr' */
423+ lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT));
425424 *p = curr->gch.next;
426425 if (curr == g->rootgc) /* is the first element of the list? */
427426 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
432431 }
433432
434433
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−
446434 static void checkSizes (lua_State *L) {
447435 global_State *g = G(L);
448436 /* 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) &&
450438 g->strt.size > MINSTRTABSIZE*2)
451439 luaS_resize(L, g->strt.size/2); /* table is too big */
452440 /* check size of buffer */
@@ -459,22 +447,29 @@ static void checkSizes (lua_State *L) { · 6 unmodified lines
459447
460448 static void GCTM (lua_State *L) {
461449 global_State *g = G(L);
462− GCObject *o = g->tmudata;
450+ GCObject *o = g->tmudata->gch.next; /* get first element */
463451 Udata *udata = rawgco2u(o);
464452 const TValue *tm;
465− g->tmudata = udata->uv.next; /* remove udata from `tmudata' */
466− udata->uv.next = g->firstudata->uv.next; /* return it to `root' list */
467− g->firstudata->uv.next = o;
453+ /* remove udata from `tmudata' */
454+ if (o == g->tmudata) /* last element? */
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;
468460 makewhite(g, o);
469461 tm = fasttm(L, udata->uv.metatable, TM_GC);
470462 if (tm != NULL) {
471463 lu_byte oldah = L->allowhook;
464+ lu_mem oldt = g->GCthreshold;
472465 L->allowhook = 0; /* stop debug hooks during GC tag method */
466+ g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */
473467 setobj2s(L, L->top, tm);
474468 setuvalue(L, L->top+1, udata);
475469 L->top += 2;
476470 luaD_call(L, L->top - 2, 0);
477471 L->allowhook = oldah; /* restore hooks */
472+ g->GCthreshold = oldt; /* restore threshold */
478473 }
479474 }
480475
@@ -491,9 +486,10 @@ void luaC_callGCTM (lua_State *L) { · 10 unmodified lines
491486 void luaC_freeall (lua_State *L) {
492487 global_State *g = G(L);
493488 int i;
494− freelist(L, &g->rootgc);
489+ g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* mask to collect all elements */
490+ sweepwholelist(L, &g->rootgc);
495491 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]);
497493 }
498494
499495
@@ -512,18 +508,11 @@ static void markroot (lua_State *L) { · 12 unmodified lines
512508
513509
514510 static void remarkupvals (global_State *g) {
515− GCObject *o;
516− for (o = obj2gco(g->mainthread); o; o = o->gch.next) {
517− lua_assert(!isblack(o));
518− if (iswhite(o)) {
519− GCObject *curr;
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− }
511+ UpVal *uv;
512+ for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
513+ lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
514+ if (isgray(obj2gco(uv)))
515+ markvalue(g, uv->v);
527516 }
528517 }
529518
@@ -531,7 +520,6 @@ static void remarkupvals (global_State *g) { · 1 unmodified line
531520 static void atomic (lua_State *L) {
532521 global_State *g = G(L);
533522 size_t udsize; /* total size of userdata to be finalized */
534− int aux;
535523 /* remark objects cautch by write barrier */
536524 propagateall(g);
537525 /* remark occasional upvalues of (maybe) dead threads */
@@ -555,10 +543,6 @@ static void atomic (lua_State *L) { · 17 unmodified lines
555543 g->sweepstrgc = 0;
556544 g->sweepgc = &g->rootgc;
557545 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 */
562546 g->estimate = g->totalbytes - udsize; /* first estimate */
563547 }
564548
@@ -568,11 +552,7 @@ static l_mem singlestep (lua_State *L) { · 3 unmodified lines
568552 /*lua_checkmemory(L);*/
569553 switch (g->gcstate) {
570554 case GCSpause: {
571− /* start a new collection */
572− if (g->gcgenerational)
573− atomic(L);
574− else
575− markroot(L);
555+ markroot(L); /* start a new collection */
576556 return 0;
577557 }
578558 case GCSpropagate: {
@@ -605,13 +585,12 @@ static l_mem singlestep (lua_State *L) { · 26 unmodified lines
605585 }
606586 case GCSfinalize: {
607587 if (g->tmudata) {
608− g->GCthreshold += GCFINALIZECOST; /* avoid GC steps inside method */
609588 GCTM(L);
610− g->GCthreshold -= GCFINALIZECOST; /* correct threshold */
611589 return GCFINALIZECOST;
612590 }
613591 else {
614592 g->gcstate = GCSpause; /* end collection */
593+ g->gcdept = 0;
615594 return 0;
616595 }
617596 }
@@ -622,25 +601,33 @@ static l_mem singlestep (lua_State *L) { · 4 unmodified lines
622601
623602 void luaC_step (lua_State *L) {
624603 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;
626608 do {
627609 lim -= singlestep(L);
628610 if (g->gcstate == GCSpause)
629611 break;
630− } while (lim > 0 || !g->incgc);
631− if (g->gcstate != GCSpause)
632− g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/STEPMUL; */
612+ } while (lim > 0);
613+ if (g->gcstate != GCSpause) {
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+ }
633621 else {
634622 lua_assert(g->totalbytes >= g->estimate);
635− g->GCthreshold = g->estimate + ((g->estimate/GCDIV) * g->gcpace);
623+ setthreshold(g);
636624 }
637625 }
638626
639627
640628 void luaC_fullgc (lua_State *L) {
641629 global_State *g = G(L);
642− if (g->gcstate <= GCSpropagate || g->gcgenerational) {
643− g->gcgenerational = 0;
630+ if (g->gcstate <= GCSpropagate) {
644631 /* reset sweep marks to sweep all elements (returning them to white) */
645632 g->sweepstrgc = 0;
646633 g->sweepgc = &g->rootgc;
@@ -650,40 +637,37 @@ void luaC_fullgc (lua_State *L) { · 3 unmodified lines
650637 g->weak = NULL;
651638 g->gcstate = GCSsweepstring;
652639 }
640+ lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate);
653641 /* finish any pending sweep phase */
654642 while (g->gcstate != GCSfinalize) {
655643 lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep);
656644 singlestep(L);
657645 }
658646 markroot(L);
659− lua_assert(!g->gcgenerational);
660647 while (g->gcstate != GCSpause) {
661648 singlestep(L);
662− g->gcgenerational = 0; /* keep it in this mode */
663649 }
664− g->GCthreshold = 2*g->estimate;
650+ setthreshold(g);
665651 }
666652
667653
668654 void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) {
669655 global_State *g = G(L);
670656 lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
671− lua_assert(g->gcgenerational ||
672− (g->gcstate != GCSfinalize && g->gcstate != GCSpause));
657+ lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
673658 lua_assert(ttype(&o->gch) != LUA_TTABLE);
674659 /* must keep invariant? */
675− if (g->gcstate == GCSpropagate || g->gcgenerational)
660+ if (g->gcstate == GCSpropagate)
676661 reallymarkobject(g, v); /* restore invariant */
677662 else /* don't mind */
678663 makewhite(g, o); /* mark as white just to avoid other barriers */
679664 }
680665
681666
682−void luaC_barrierback (lua_State *L, GCObject *o, GCObject *v) {
667+void luaC_barrierback (lua_State *L, GCObject *o) {
683668 global_State *g = G(L);
684− lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
685− lua_assert(g->gcgenerational ||
686− (g->gcstate != GCSfinalize && g->gcstate != GCSpause));
669+ lua_assert(isblack(o) && !isdead(g, o));
670+ lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
687671 black2gray(o); /* make table gray (again) */
688672 gco2h(o)->gclist = g->grayagain;
689673 g->grayagain = o;
@@ -705,7 +689,7 @@ void luaC_linkupval (lua_State *L, UpVal *uv) { · 15 unmodified lines
705689 o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */
706690 g->rootgc = o;
707691 if (isgray(o)) {
708− if (g->gcstate == GCSpropagate || g->gcgenerational) {
692+ if (g->gcstate == GCSpropagate) {
709693 gray2black(o); /* closed upvalues need barrier */
710694 luaC_barrier(L, uv, uv->v);
711695 }
src/lgc.h+14−9
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Garbage Collector
44 ** See Copyright Notice in lua.h
55 */
@@ -29,7 +29,7 @@ · 23 unmodified lines
2929 #define testbits(x,m) ((x) & (m))
3030 #define bitmask(b) (1<<(b))
3131 #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))
3333 #define resetbit(x,b) resetbits(x, bitmask(b))
3434 #define testbit(x,b) testbits(x, bitmask(b))
3535 #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2)))
@@ -47,8 +47,10 @@ · 11 unmodified lines
4747 ** bit 3 - for tables: has weak keys
4848 ** bit 4 - for tables: has weak values
4949 ** bit 5 - object is fixed (should not be collected)
50+** bit 6 - object is "super" fixed (only the main thread)
5051 */
5152
53+
5254 #define WHITE0BIT 0
5355 #define WHITE1BIT 1
5456 #define BLACKBIT 2
@@ -56,20 +58,23 @@ · 1 unmodified line
5658 #define KEYWEAKBIT 3
5759 #define VALUEWEAKBIT 4
5860 #define FIXEDBIT 5
61+#define SFIXEDBIT 6
62+#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
5963
6064
6165 #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
6266 #define isblack(x) testbit((x)->gch.marked, BLACKBIT)
6367 #define isgray(x) (!isblack(x) && !iswhite(x))
6468
65−#define otherwhite(g) (g->currentwhite ^ bit2mask(WHITE0BIT, WHITE1BIT))
66−#define isdead(g,v) ((v)->gch.marked & otherwhite(g))
69+#define otherwhite(g) (g->currentwhite ^ WHITEBITS)
70+#define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS)
6771
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)
6974
7075 #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
7176
72−#define luaC_white(g) cast(lu_byte, (g)->currentwhite)
77+#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
7378
7479
7580 #define luaC_checkGC(L) { if (G(L)->totalbytes >= G(L)->GCthreshold) \
@@ -80,7 +85,7 @@ · 4 unmodified lines
8085 luaC_barrierf(L,obj2gco(p),gcvalue(v)); }
8186
8287 #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)); }
8489
8590 #define luaC_objbarrier(L,p,o) \
8691 { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
@@ -88,7 +93,7 @@ · 1 unmodified line
8893
8994 #define luaC_objbarriert(L,p,o) \
9095 { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
91− luaC_barrierback(L,obj2gco(p),obj2gco(o)); }
96+ luaC_barrierback(L,obj2gco(p)); }
9297
9398 size_t luaC_separateudata (lua_State *L, int all);
9499 void luaC_callGCTM (lua_State *L);
@@ -98,7 +103,7 @@ void luaC_fullgc (lua_State *L); · 3 unmodified lines
98103 void luaC_link (lua_State *L, GCObject *o, lu_byte tt);
99104 void luaC_linkupval (lua_State *L, UpVal *uv);
100105 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);
102107
103108
104109 #endif
src/linit.c+3−1
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Initialization of libraries for lua.c
44 ** See Copyright Notice in lua.h
55 */
@@ -32,6 +32,8 @@ LUALIB_API int luaopen_stdlibs (lua_State *L) { · 26 unmodified lines
3232 for (; lib->func; lib++) {
3333 lib->func(L); /* open library */
3434 lua_settop(L, 0); /* discard any results */
35+ lua_pushvalue(L, LUA_GLOBALSINDEX);
36+ lua_replace(L, LUA_ENVIRONINDEX); /* restore environment */
3537 }
3638 return 0;
3739 }
src/liolib.c+17−17
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Standard I/O (and system) library
44 ** See Copyright Notice in lua.h
55 */
@@ -105,8 +105,8 @@ static int aux_close (lua_State *L) { · 99 unmodified lines
105105
106106
107107 static int io_close (lua_State *L) {
108− if (lua_isnone(L, 1) && lua_type(L, lua_upvalueindex(1)) == LUA_TTABLE)
109− lua_rawgeti(L, lua_upvalueindex(1), IO_OUTPUT);
108+ if (lua_isnone(L, 1))
109+ lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT);
110110 return pushresult(L, aux_close(L), NULL);
111111 }
112112
@@ -147,7 +147,7 @@ static int io_tmpfile (lua_State *L) { · 34 unmodified lines
147147
148148 static FILE *getiofile (lua_State *L, int findex) {
149149 FILE *f;
150− lua_rawgeti(L, lua_upvalueindex(1), findex);
150+ lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
151151 lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE));
152152 f = *(FILE **)lua_touserdata(L, -1);
153153 if (f == NULL)
@@ -170,10 +170,10 @@ static int g_iofile (lua_State *L, int f, const char *mode) { · 16 unmodified lines
170170 lua_pushvalue(L, 1);
171171 }
172172 lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE));
173− lua_rawseti(L, lua_upvalueindex(1), f);
173+ lua_rawseti(L, LUA_ENVIRONINDEX, f);
174174 }
175175 /* return current value */
176− lua_rawgeti(L, lua_upvalueindex(1), f);
176+ lua_rawgeti(L, LUA_ENVIRONINDEX, f);
177177 return 1;
178178 }
179179
@@ -192,10 +192,9 @@ static int io_readline (lua_State *L); · 12 unmodified lines
192192
193193
194194 static void aux_lines (lua_State *L, int idx, int close) {
195− lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
196195 lua_pushvalue(L, idx);
197196 lua_pushboolean(L, close); /* close/not close file when finished */
198− lua_pushcclosure(L, io_readline, 3);
197+ lua_pushcclosure(L, io_readline, 2);
199198 }
200199
201200
@@ -209,7 +208,7 @@ static int f_lines (lua_State *L) { · 7 unmodified lines
209208 static int io_lines (lua_State *L) {
210209 if (lua_isnoneornil(L, 1)) { /* no arguments? */
211210 /* will iterate over default input */
212− lua_rawgeti(L, lua_upvalueindex(1), IO_INPUT);
211+ lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT);
213212 return f_lines(L);
214213 }
215214 else {
@@ -349,7 +348,7 @@ static int f_read (lua_State *L) { · 133 unmodified lines
349348
350349
351350 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));
353352 int sucess;
354353 if (f == NULL) /* file is already closed? */
355354 luaL_error(L, "file is already closed");
@@ -358,9 +357,9 @@ static int io_readline (lua_State *L) { · 2 unmodified lines
358357 luaL_error(L, "%s", strerror(errno));
359358 if (sucess) return 1;
360359 else { /* EOF */
361− if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
360+ if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
362361 lua_settop(L, 0);
363− lua_pushvalue(L, lua_upvalueindex(2));
362+ lua_pushvalue(L, lua_upvalueindex(1));
364363 aux_close(L); /* close it */
365364 }
366365 return 0;
@@ -489,12 +488,13 @@ LUALIB_API int luaopen_io (lua_State *L) { · 122 unmodified lines
489488 createmeta(L);
490489 createupval(L);
491490 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);
493493 /* put predefined file handles into `io' table */
494− lua_rawgeti(L, -2, IO_INPUT); /* get current input from metatable */
495− lua_setfield(L, -2, "stdin"); /* io.stdin = metatable[IO_INPUT] */
496− lua_rawgeti(L, -2, IO_OUTPUT); /* get current output from metatable */
497− lua_setfield(L, -2, "stdout"); /* io.stdout = metatable[IO_OUTPUT] */
494+ lua_rawgeti(L, -2, IO_INPUT); /* get current input from upval */
495+ lua_setfield(L, -2, "stdin"); /* io.stdin = upval[IO_INPUT] */
496+ lua_rawgeti(L, -2, IO_OUTPUT); /* get current output from upval */
497+ lua_setfield(L, -2, "stdout"); /* io.stdout = upval[IO_OUTPUT] */
498498 *newfile(L) = stderr;
499499 lua_setfield(L, -2, "stderr"); /* io.stderr = newfile(stderr) */
500500 return 1;
src/llimits.h+1−3
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Limits, basic types, and some other `installation-dependent' definitions
44 ** See Copyright Notice in lua.h
55 */
@@ -73,8 +73,6 @@ typedef LUA_UACNUMBER l_uacNumber; · 67 unmodified lines
7373 typedef lu_int32 Instruction;
7474
7575
76−/* divisor for GC pace */
77−#define GCDIV 8
7876
7977 /* maximum stack for a Lua function */
8078 #define MAXSTACK 250
src/lmathlib.c+1−3
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Standard mathematical library
44 ** See Copyright Notice in lua.h
55 */
@@ -225,8 +225,6 @@ LUALIB_API int luaopen_math (lua_State *L) { · 219 unmodified lines
225225 luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0);
226226 lua_pushnumber(L, PI);
227227 lua_setfield(L, -2, "pi");
228− lua_pushcfunction(L, math_pow);
229− lua_setglobal(L, "__pow");
230228 return 1;
231229 }
232230
src/lmem.c+24−8
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Interface to Memory Manager
44 ** See Copyright Notice in lua.h
55 */
@@ -22,19 +22,19 @@ · 16 unmodified lines
2222
2323 /*
2424 ** 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);
2626 ** (`osize' is the old size, `nsize' is the new size)
2727 **
2828 ** Lua ensures that (ptr == NULL) iff (osize == 0).
2929 **
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'
3131 **
32−** * realloc(ud, p, x, 0) frees the block `p'
33−** (in this specific case, realloc must return NULL).
34−** particularly, realloc(ud, NULL, 0, 0) does nothing
32+** * frealloc(ud, p, x, 0) frees the block `p'
33+** (in this specific case, frealloc must return NULL).
34+** particularly, frealloc(ud, NULL, 0, 0) does nothing
3535 ** (which is equivalent to free(NULL) in ANSI C)
3636 **
37−** realloc returns NULL if it cannot create or reallocate the area
37+** frealloc returns NULL if it cannot create or reallocate the area
3838 ** (any reallocation to an equal or smaller size cannot fail!)
3939 */
4040
@@ -76,11 +76,27 @@ void *luaM_toobig (lua_State *L) { · 35 unmodified lines
7676 void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
7777 global_State *g = G(L);
7878 lua_assert((osize == 0) == (block == NULL));
79− block = (*g->realloc)(g->ud, block, osize, nsize);
79+ block = (*g->frealloc)(g->ud, block, osize, nsize);
8080 if (block == NULL && nsize > 0)
8181 luaD_throw(L, LUA_ERRMEM);
8282 lua_assert((nsize == 0) == (block == NULL));
8383 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
84100 return block;
85101 }
86102
src/loadlib.c+55−27
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Dynamic library loader for Lua
44 ** See Copyright Notice in lua.h
55 *
@@ -293,45 +293,60 @@ static int ll_loadlib (lua_State *L) { · 287 unmodified lines
293293 */
294294
295295
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);
297299 const char *path;
298300 /* try first `LUA_PATH' for compatibility */
299− lua_getglobal(L, "LUA_PATH");
301+ lua_pushstring(L, "LUA_PATH");
302+ lua_rawget(L, LUA_GLOBALSINDEX);
300303 path = lua_tostring(L, -1);
301304 if (!path) {
302305 lua_pop(L, 1);
303− luaL_getfield(L, LUA_GLOBALSINDEX, "package.path");
306+ lua_getfield(L, LUA_ENVIRONINDEX, "path");
304307 path = lua_tostring(L, -1);
305308 }
306309 if (path == NULL)
307310 luaL_error(L, "`package.path' must be a string");
308311 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 */
310313 if (luaL_loadfile(L, fname) != 0)
311314 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 */
313316 }
314317
315318
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);
317322 const char *path;
318323 const char *funcname;
319− luaL_getfield(L, LUA_GLOBALSINDEX, "package.cpath");
324+ lua_getfield(L, LUA_ENVIRONINDEX, "cpath");
320325 path = lua_tostring(L, -1);
321326 if (path == NULL)
322327 luaL_error(L, "`package.cpath' must be a string");
323328 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 */
325330 funcname = luaL_gsub(L, name, ".", LUA_OFSEP);
326331 funcname = lua_pushfstring(L, "%s%s", POF, funcname);
327332 if (ll_loadfunc(L, fname, funcname) != 1)
328333 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;
330344 }
331345
332346
333347 static int ll_require (lua_State *L) {
334348 const char *name = luaL_checkstring(L, 1);
349+ int i;
335350 lua_settop(L, 1);
336351 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
337352 lua_getfield(L, 2, name);
@@ -340,22 +355,18 @@ static int ll_require (lua_State *L) { · 2 unmodified lines
340355 /* else must load it; first mark it as loaded */
341356 lua_pushboolean(L, 1);
342357 lua_setfield(L, 2, name); /* _LOADED[name] = true */
343− /* check whether it is preloaded */
344− lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
345− lua_getfield(L, -1, name);
346− if (lua_isnil(L, -1)) { /* no preload function for that module? */
347− const char *fname = luaL_gsub(L, name, ".", LUA_DIRSEP);
348− const char *cpath = loadC(L, fname, name); /* try a C module */
349− if (cpath) { /* not found? */
350− const char *path = loadLua(L, fname, name); /* try a Lua module */
351− if (path) { /* yet not found? */
352− lua_pushnil(L);
353− lua_setfield(L, 2, name); /* unmark _LOADED[name] */
354− return luaL_error(L, "package `%s' not found\n"
355− " cpath: %s\n path: %s",
356− name, cpath, path);
357− }
358− }
358+ /* iterate over available loaders */
359+ lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
360+ if (!lua_istable(L, -1))
361+ luaL_error(L, "`package.loaders' must be a table");
362+ for (i=1;; i++) {
363+ lua_rawgeti(L, -1, i); /* get a loader */
364+ if (lua_isnil(L, -1))
365+ return luaL_error(L, "package `%s' not found", name);
366+ lua_pushstring(L, name);
367+ lua_call(L, 1, 1); /* call it */
368+ if (lua_isnil(L, -1)) lua_pop(L, 1);
369+ else break; /* module loaded successfully */
359370 }
360371 lua_pushvalue(L, 1); /* pass name as argument to module */
361372 lua_call(L, 1, 1); /* run loaded module */
@@ -430,9 +441,13 @@ static const luaL_reg ll_funcs[] = { · 68 unmodified lines
430441 };
431442
432443
444+static const lua_CFunction loaders[] =
445+ {loader_preload, loader_C, loader_Lua, NULL};
446+
433447
434448 LUALIB_API int luaopen_loadlib (lua_State *L) {
435449 const char *path;
450+ int i;
436451 /* create new type _LOADLIB */
437452 luaL_newmetatable(L, "_LOADLIB");
438453 lua_pushcfunction(L, gctm);
@@ -441,6 +456,19 @@ LUALIB_API int luaopen_loadlib (lua_State *L) { · 2 unmodified lines
441456 lua_newtable(L);
442457 lua_pushvalue(L, -1);
443458 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");
444472 /* set field `path' */
445473 path = getenv(LUA_PATH);
446474 if (path == NULL) path = LUA_PATH_DEFAULT;
@@ -455,7 +483,7 @@ LUALIB_API int luaopen_loadlib (lua_State *L) { · 8 unmodified lines
455483 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
456484 lua_setfield(L, -2, "loaded");
457485 /* set field `preload' */
458− lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
486+ lua_newtable(L);
459487 lua_setfield(L, -2, "preload");
460488 lua_pushvalue(L, LUA_GLOBALSINDEX);
461489 luaL_openlib(L, NULL, ll_funcs, 0); /* open lib into global table */
src/lobject.c+2−2
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Some generic functions over Lua objects
44 ** See Copyright Notice in lua.h
55 */
@@ -73,7 +73,7 @@ int luaO_rawequalObj (const TValue *t1, const TValue *t2) { · 67 unmodified lines
7373 case LUA_TNIL:
7474 return 1;
7575 case LUA_TNUMBER:
76− return nvalue(t1) == nvalue(t2);
76+ return num_eq(nvalue(t1), nvalue(t2));
7777 case LUA_TBOOLEAN:
7878 return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
7979 case LUA_TLIGHTUSERDATA:
src/lobject.h+14−5
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Type definitions for Lua objects
44 ** See Copyright Notice in lua.h
55 */
@@ -218,6 +218,7 @@ typedef union Udata { · 212 unmodified lines
218218 struct {
219219 CommonHeader;
220220 struct Table *metatable;
221+ struct Table *env;
221222 size_t len;
222223 } uv;
223224 } Udata;
@@ -271,7 +272,13 @@ typedef struct LocVar { · 47 unmodified lines
271272 typedef struct UpVal {
272273 CommonHeader;
273274 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;
275282 } UpVal;
276283
277284
@@ -280,7 +287,8 @@ typedef struct UpVal { · 2 unmodified lines
280287 */
281288
282289 #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
284292
285293 typedef struct CClosure {
286294 ClosureHeader;
@@ -292,7 +300,6 @@ typedef struct CClosure { · 5 unmodified lines
292300 typedef struct LClosure {
293301 ClosureHeader;
294302 struct Proto *p;
295− TValue g; /* global table for this closure */
296303 UpVal *upvals[1];
297304 } LClosure;
298305
@@ -330,7 +337,7 @@ typedef struct Table { · 31 unmodified lines
330337 struct Table *metatable;
331338 TValue *array; /* array part */
332339 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 */
334341 GCObject *gclist;
335342 int sizearray; /* size of `array' array */
336343 } Table;
@@ -351,6 +358,8 @@ typedef struct Table { · 14 unmodified lines
351358
352359 extern const TValue luaO_nilobject;
353360
361+#define ceillog2(x) (luaO_log2((x)-1) + 1)
362+
354363 int luaO_log2 (unsigned int x);
355364 int luaO_int2fb (unsigned int x);
356365 int luaO_fb2int (int x);
src/loslib.c+3−5
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Standard Operating System library
44 ** See Copyright Notice in lua.h
55 */
@@ -91,15 +91,13 @@ static int io_clock (lua_State *L) { · 85 unmodified lines
9191 */
9292
9393 static void setfield (lua_State *L, const char *key, int value) {
94− lua_pushstring(L, key);
9594 lua_pushinteger(L, value);
96− lua_rawset(L, -3);
95+ lua_setfield(L, -2, key);
9796 }
9897
9998 static void setboolfield (lua_State *L, const char *key, int value) {
100− lua_pushstring(L, key);
10199 lua_pushboolean(L, value);
102− lua_rawset(L, -3);
100+ lua_setfield(L, -2, key);
103101 }
104102
105103 static int getboolfield (lua_State *L, const char *key) {
src/lparser.c+2−2
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Lua Parser
44 ** See Copyright Notice in lua.h
55 */
@@ -547,7 +547,7 @@ static void constructor (LexState *ls, expdesc *t) { · 541 unmodified lines
547547 check_match(ls, '}', '{', line);
548548 lastlistfield(fs, &cc);
549549 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 */
551551 }
552552
553553 /* }====================================================================== */
src/lstate.c+16−27
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Global State
44 ** See Copyright Notice in lua.h
55 */
@@ -76,33 +76,22 @@ static void freestack (lua_State *L, lua_State *L1) { · 70 unmodified lines
7676 ** open parts that may cause memory-allocation errors
7777 */
7878 static void f_luaopen (lua_State *L, void *ud) {
79− Udata *u; /* head of udata list */
8079 global_State *g = G(L);
8180 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);
8981 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 */
9183 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 */
9385 luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
9486 luaT_init(L);
9587 luaX_init(L);
9688 luaS_fix(luaS_newliteral(L, MEMERRMSG));
9789 g->GCthreshold = 4*g->totalbytes;
98− g->prevestimate = g->estimate = g->totalbytes;
9990 }
10091
10192
10293 static void preinit_state (lua_State *L, global_State *g) {
10394 L->l_G = g;
104− L->tt = LUA_TTHREAD;
105− L->marked = luaC_white(g);
10695 L->stack = NULL;
10796 L->stacksize = 0;
10897 L->errorJmp = NULL;
@@ -125,20 +114,19 @@ static void close_state (lua_State *L) { · 16 unmodified lines
125114 global_State *g = G(L);
126115 luaF_close(L, L->stack); /* close all upvalues for this thread */
127116 luaC_freeall(L); /* collect all objects */
128− lua_assert(g->rootgc == NULL);
117+ lua_assert(g->rootgc == obj2gco(L));
129118 lua_assert(g->strt.nuse == 0);
130119 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
131120 luaZ_freebuffer(L, &g->buff);
132121 freestack(L, L);
133122 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);
135124 }
136125
137126
138127 lua_State *luaE_newthread (lua_State *L) {
139128 lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
140− L1->next = L->next; /* link new thread after `L' */
141− L->next = obj2gco(L1);
129+ luaC_link(L, obj2gco(L1), LUA_TTHREAD);
142130 preinit_state(L1, G(L));
143131 stack_init(L1, L); /* init stack */
144132 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
167155 L = tostate(l);
168156 g = &((LG *)L)->g;
169157 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);
171162 preinit_state(L, g);
172− g->realloc = f;
163+ g->frealloc = f;
173164 g->ud = ud;
174165 g->mainthread = L;
166+ g->uvhead.u.l.prev = &g->uvhead;
167+ g->uvhead.u.l.next = &g->uvhead;
175168 g->GCthreshold = 0; /* mark it as unfinished state */
176169 g->strt.size = 0;
177170 g->strt.nuse = 0;
@@ -180,21 +173,17 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { · 2 unmodified lines
180173 luaZ_initbuffer(L, &g->buff);
181174 g->panic = NULL;
182175 g->gcstate = GCSpause;
183− g->gcgenerational = 0;
184176 g->rootgc = obj2gco(L);
185177 g->sweepstrgc = 0;
186178 g->sweepgc = &g->rootgc;
187− g->firstudata = NULL;
188179 g->gray = NULL;
189180 g->grayagain = NULL;
190181 g->weak = NULL;
191182 g->tmudata = NULL;
192− setnilvalue(gkey(g->dummynode));
193− setnilvalue(gval(g->dummynode));
194− gnext(g->dummynode) = NULL;
195183 g->totalbytes = sizeof(LG);
196− g->gcpace = GCDIV;
197− g->incgc = 1;
184+ g->gcpace = 200; /* 200% (wait memory to double before next collection) */
185+ g->gcstepmul = 200; /* GC runs `twice the speed' of memory allocation */
186+ g->gcdept = 0;
198187 if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
199188 /* memory allocation error: free partial state */
200189 close_state(L);
src/lstate.h+9−10
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Global State
44 ** See Copyright Notice in lua.h
55 */
@@ -67,30 +67,28 @@ typedef struct CallInfo { · 61 unmodified lines
6767 */
6868 typedef struct global_State {
6969 stringtable strt; /* hash table for strings */
70− lua_Alloc realloc; /* function to reallocate memory */
71− void *ud; /* auxiliary data to `realloc' */
70+ lua_Alloc frealloc; /* function to reallocate memory */
71+ void *ud; /* auxiliary data to `frealloc' */
7272 lu_byte currentwhite;
7373 lu_byte gcstate; /* state of garbage collector */
74− lu_byte gcgenerational;
7574 GCObject *rootgc; /* list of all collectable objects */
76− GCObject *firstudata; /* udata go to the end of `rootgc' */
7775 GCObject **sweepgc; /* position of sweep in `rootgc' */
7876 int sweepstrgc; /* position of sweep in `strt' */
7977 GCObject *gray; /* list of gray objects */
8078 GCObject *grayagain; /* list of objects to be traversed atomically */
8179 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 */
8381 Mbuffer buff; /* temporary buffer for string concatentation */
8482 lu_mem GCthreshold;
8583 lu_mem totalbytes; /* number of bytes currently allocated */
8684 lu_mem estimate; /* an estimate of number of bytes actually in use */
87− lu_mem prevestimate; /* previous estimate */
88− int gcpace; /* relative `speed' of the GC */
89− int incgc; /* 0 if GC is done non-incrementally */
85+ lu_mem gcdept; /* how much GC is `behind schedule' */
86+ int gcpace; /* size of pause between successive GCs */
87+ int gcstepmul; /* GC `granularity' */
9088 lua_CFunction panic; /* to be called in unprotected errors */
9189 TValue _registry;
9290 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 */
9492 TString *tmname[TM_N]; /* array with tag-method names */
9593 } global_State;
9694
@@ -118,6 +116,7 @@ struct lua_State { · 21 unmodified lines
118116 int hookcount;
119117 lua_Hook hook;
120118 TValue _gt; /* table of globals */
119+ TValue env; /* temporary place for environments */
121120 GCObject *openupval; /* list of open upvalues in this stack */
122121 GCObject *gclist;
123122 struct lua_longjmp *errorJmp; /* current error recover point */
src/lstring.c+6−5
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** String table (keeps all strings handled by Lua)
44 ** See Copyright Notice in lua.h
55 */
@@ -93,7 +93,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { · 87 unmodified lines
9393 }
9494
9595
96−Udata *luaS_newudata (lua_State *L, size_t s) {
96+Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
9797 Udata *u;
9898 if (s > MAX_SIZET - sizeof(Udata))
9999 luaM_toobig(L);
@@ -102,9 +102,10 @@ Udata *luaS_newudata (lua_State *L, size_t s) { · 2 unmodified lines
102102 u->uv.tt = LUA_TUSERDATA;
103103 u->uv.len = s;
104104 u->uv.metatable = NULL;
105− /* chain it on udata list */
106− u->uv.next = G(L)->firstudata->uv.next;
107− G(L)->firstudata->uv.next = obj2gco(u);
105+ u->uv.env = e;
106+ /* chain it on udata list (after main thread) */
107+ u->uv.next = G(L)->mainthread->next;
108+ G(L)->mainthread->next = obj2gco(u);
108109 return u;
109110 }
110111
src/lstring.h+3−3
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** String table (keep all strings handled by Lua)
44 ** See Copyright Notice in lua.h
55 */
@@ -21,10 +21,10 @@ · 15 unmodified lines
2121 #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
2222 (sizeof(s)/sizeof(char))-1))
2323
24−#define luaS_fix(s) setbit((s)->tsv.marked, FIXEDBIT)
24+#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT)
2525
2626 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);
2828 TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
2929
3030
src/ltable.c+125−96
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Lua tables (hash)
44 ** See Copyright Notice in lua.h
55 */
@@ -68,6 +68,13 @@ · 62 unmodified lines
6868 #define numints cast(int, sizeof(lua_Number)/sizeof(int))
6969
7070
71+
72+const Node luaH_dummynode = {
73+ {{NULL}, LUA_TNIL}, /* value */
74+ {{NULL}, LUA_TNIL, NULL} /* key */
75+};
76+
77+
7178 /*
7279 ** hash for lua_Numbers
7380 */
@@ -112,7 +119,7 @@ static int arrayindex (const TValue *key) { · 38 unmodified lines
112119 lua_Number n = nvalue(key);
113120 int k;
114121 lua_number2int(k, n);
115− if (cast(lua_Number, k) == nvalue(key))
122+ if (num_eq(cast(lua_Number, k), nvalue(key)))
116123 return k;
117124 }
118125 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
176183 */
177184
178185
179−static void computesizes (int nums[], int ntotal, int *narray, int *nhash) {
186+static int computesizes (int nums[], int *narray) {
180187 int i;
181− int a = nums[0]; /* number of elements smaller than 2^i */
182− int na = a; /* number of elements to go to array part */
183− int n = (na == 0) ? -1 : 0; /* (log of) optimal size for array part */
184− for (i = 1; a < *narray && *narray >= twoto(i-1); i++) {
188+ int twotoi; /* 2^i */
189+ int a = 0; /* number of elements smaller than 2^i */
190+ int na = 0; /* number of elements to go to array part */
191+ int n = 0; /* optimal size for array part */
192+ for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
185193 if (nums[i] > 0) {
186194 a += nums[i];
187− if (a >= twoto(i-1)) { /* more than half elements in use? */
188− n = i;
189− na = a;
195+ if (a > twotoi/2) { /* more than half elements present? */
196+ n = twotoi; /* optimal size (till now) */
197+ na = a; /* all elements smaller than n will go to array part */
190198 }
191199 }
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;
192213 }
193− lua_assert(na <= *narray && *narray <= ntotal);
194− *nhash = ntotal - na;
195− *narray = (n == -1) ? 0 : twoto(n);
196− lua_assert(na <= *narray && na >= *narray/2);
214+ else
215+ return 0;
197216 }
198217
199218
200−static void numuse (const Table *t, int *narray, int *nhash) {
201− int nums[MAXBITS+1];
202− int i, lg;
203− int totaluse = 0;
204− /* count elements in array part */
205− for (i=0, lg=0; lg<=MAXBITS; lg++) { /* for each slice [2^(lg-1) to 2^lg) */
206− int ttlg = twoto(lg); /* 2^lg */
207− if (ttlg > t->sizearray) {
208− ttlg = t->sizearray;
209− if (i >= ttlg) break;
219+static int numusearray (const Table *t, int *nums) {
220+ int lg;
221+ int ttlg; /* 2^lg */
222+ int ause = 0; /* summation of `nums' */
223+ int i = 1; /* count to traverse all array keys */
224+ for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */
225+ int lc = 0; /* counter */
226+ int lim = ttlg;
227+ if (lim > t->sizearray) {
228+ lim = t->sizearray; /* adjust upper limit */
229+ if (i > lim)
230+ break; /* no more elements to count */
210231 }
211− nums[lg] = 0;
212− for (; i<ttlg; i++) {
213− if (!ttisnil(&t->array[i])) {
214− nums[lg]++;
215− totaluse++;
216− }
232+ /* count elements in range (2^(lg-1), 2^lg] */
233+ for (; i <= lim; i++) {
234+ if (!ttisnil(&t->array[i-1]))
235+ lc++;
217236 }
237+ nums[lg] += lc;
238+ ause += lc;
218239 }
219− for (; lg<=MAXBITS; lg++) nums[lg] = 0; /* reset other counts */
220− *narray = totaluse; /* all previous uses were in array part */
221− /* count elements in hash part */
222− i = sizenode(t);
240+ return ause;
241+}
242+
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);
223248 while (i--) {
224249 Node *n = &t->node[i];
225250 if (!ttisnil(gval(n))) {
226− int k = arrayindex(key2tval(n));
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− }
251+ ause += countint(key2tval(n), nums);
231252 totaluse++;
232253 }
233254 }
234− computesizes(nums, totaluse, narray, nhash);
255+ *pnasize += ause;
256+ return totaluse;
235257 }
236258
237259
@@ -244,18 +266,18 @@ static void setarrayvector (lua_State *L, Table *t, int size) { · 6 unmodified lines
244266 }
245267
246268
247−static void setnodevector (lua_State *L, Table *t, int lsize) {
248− int i;
249− int size = twoto(lsize);
250− if (lsize > MAXBITS)
251− luaG_runerror(L, "table overflow");
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) */
269+static void setnodevector (lua_State *L, Table *t, int size) {
270+ int lsize;
271+ if (size == 0) { /* no elements to hash part? */
272+ t->node = cast(Node *, &luaH_dummynode); /* use common `dummynode' */
273+ lsize = 0;
257274 }
258275 else {
276+ int i;
277+ lsize = ceillog2(size);
278+ if (lsize > MAXBITS)
279+ luaG_runerror(L, "table overflow");
280+ size = twoto(lsize);
259281 t->node = luaM_newvector(L, size, Node);
260282 for (i=0; i<size; i++) {
261283 gnext(&t->node[i]) = NULL;
@@ -264,31 +286,19 @@ static void setnodevector (lua_State *L, Table *t, int lsize) { · 2 unmodified lines
264286 }
265287 }
266288 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 */
268290 }
269291
270292
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) {
272294 int i;
273295 int oldasize = t->sizearray;
274296 int oldhsize = t->lsizenode;
275− Node *nold;
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− }
297+ Node *nold = t->node; /* save old hash ... */
287298 if (nasize > oldasize) /* array part must grow? */
288299 setarrayvector(L, t, nasize);
289300 /* create new hash part with appropriate size */
290301 setnodevector(L, t, nhsize);
291− /* re-insert elements */
292302 if (nasize < oldasize) { /* array part must shrink? */
293303 t->sizearray = nasize;
294304 /* 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
299309 /* shrink array */
300310 luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
301311 }
302− /* re-insert elements in hash part */
312+ /* re-insert elements from hash part */
303313 for (i = twoto(oldhsize) - 1; i >= 0; i--) {
304314 Node *old = nold+i;
305315 if (!ttisnil(gval(old)))
306316 setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
307317 }
308− if (oldhsize)
318+ if (nold != &luaH_dummynode)
309319 luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
310320 }
311321
312322
313−static void rehash (lua_State *L, Table *t) {
314− int nasize, nhsize;
315− numuse(t, &nasize, &nhsize); /* compute new sizes for array and hash parts */
316− luaH_resize(L, t, nasize, luaO_log2(nhsize)+1);
323+void luaH_resizearray (lua_State *L, Table *t, int nasize) {
324+ int nsize = (t->node == &luaH_dummynode) ? 0 : sizenode(t);
325+ resize(L, t, nasize, nsize);
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);
317345 }
318346
319347
@@ -323,7 +351,7 @@ static void rehash (lua_State *L, Table *t) { · 3 unmodified lines
323351 */
324352
325353
326−Table *luaH_new (lua_State *L, int narray, int lnhash) {
354+Table *luaH_new (lua_State *L, int narray, int nhash) {
327355 Table *t = luaM_new(L, Table);
328356 luaC_link(L, obj2gco(t), LUA_TTABLE);
329357 t->metatable = NULL;
@@ -332,21 +360,30 @@ Table *luaH_new (lua_State *L, int narray, int lnhash) { · 2 unmodified lines
332360 t->array = NULL;
333361 t->sizearray = 0;
334362 t->lsizenode = 0;
335− t->node = NULL;
363+ t->node = cast(Node *, &luaH_dummynode);
336364 setarrayvector(L, t, narray);
337− setnodevector(L, t, lnhash);
365+ setnodevector(L, t, nhash);
338366 return t;
339367 }
340368
341369
342370 void luaH_free (lua_State *L, Table *t) {
343− if (t->lsizenode)
371+ if (t->node != &luaH_dummynode)
344372 luaM_freearray(L, t->node, sizenode(t), Node);
345373 luaM_freearray(L, t->array, t->sizearray, TValue);
346374 luaM_free(L, t);
347375 }
348376
349377
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+
350387
351388 /*
352389 ** 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
356393 ** position), new key goes to an empty position.
357394 */
358395 static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
359− TValue *val;
360396 Node *mp = luaH_mainposition(t, key);
361− if (!ttisnil(gval(mp))) { /* main position is not free? */
362− /* `mp' of colliding node */
363− Node *othern = luaH_mainposition(t, key2tval(mp));
364− Node *n = t->firstfree; /* get a free place */
397+ if (!ttisnil(gval(mp)) || mp == &luaH_dummynode) {
398+ Node *othern;
399+ Node *n = getfreepos(L, t); /* 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));
365406 if (othern != mp) { /* is colliding node out of its main position? */
366407 /* yes; move colliding node into free position */
367408 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
380421 gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
381422 luaC_barriert(L, t, key);
382423 lua_assert(ttisnil(gval(mp)));
383− for (;;) { /* correct `firstfree' */
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;
424+ return gval(mp);
396425 }
397426
398427
@@ -407,7 +436,7 @@ const TValue *luaH_getnum (Table *t, int key) { · 8 unmodified lines
407436 lua_Number nk = cast(lua_Number, key);
408437 Node *n = hashnum(t, nk);
409438 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))
411440 return gval(n); /* that's it */
412441 else n = gnext(n);
413442 } while (n);
@@ -440,7 +469,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { · 26 unmodified lines
440469 case LUA_TNUMBER: {
441470 int k;
442471 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? */
444473 return luaH_getnum(t, k); /* use specialized version */
445474 /* else go through */
446475 }
@@ -464,7 +493,7 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { · 17 unmodified lines
464493 return cast(TValue *, p);
465494 else {
466495 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)))
468497 luaG_runerror(L, "table index is NaN");
469498 return newkey(L, t, key);
470499 }
src/ltable.h+4−2
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Lua tables (hash)
44 ** See Copyright Notice in lua.h
55 */
@@ -18,6 +18,8 @@ · 12 unmodified lines
1818 #define key2tval(n) (cast(const TValue *, gkey(n)))
1919
2020
21+extern const Node luaH_dummynode;
22+
2123 const TValue *luaH_getnum (Table *t, int key);
2224 TValue *luaH_setnum (lua_State *L, Table *t, int key);
2325 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
2527 const TValue *luaH_get (Table *t, const TValue *key);
2628 TValue *luaH_set (lua_State *L, Table *t, const TValue *key);
2729 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);
2931 void luaH_free (lua_State *L, Table *t);
3032 int luaH_next (lua_State *L, Table *t, StkId key);
3133
src/lua.c+16−4
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Lua stand-alone interpreter
44 ** See Copyright Notice in lua.h
55 */
@@ -79,11 +79,23 @@ static int report (lua_State *L, int status) { · 73 unmodified lines
7979 }
8080
8181
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+
8295 static int docall (lua_State *L, int narg, int clear) {
8396 int status;
8497 int base = lua_gettop(L) - narg; /* function index */
85− lua_pushliteral(L, "_TRACEBACK");
86− lua_rawget(L, LUA_GLOBALSINDEX); /* get traceback function */
98+ lua_pushcfunction(L, traceback); /* push traceback function */
8799 lua_insert(L, base); /* put it under chunk and args */
88100 signal(SIGINT, laction);
89101 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
@@ -359,7 +371,7 @@ static int pmain (lua_State *L) { · 269 unmodified lines
359371 int interactive = 1;
360372 if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];
361373 globalL = L;
362− lua_userinit(L); /* open libraries */
374+ luaopen_stdlibs(L); /* open libraries */
363375 status = handle_luainit(L);
364376 if (status == 0) {
365377 status = handle_argv(L, s->argv, &interactive);
src/lua.h+78−78
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Lua - An Extensible Extension Language
44 ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
55 ** http://www.lua.org mailto:info@lua.org
@@ -17,8 +17,9 @@ · 11 unmodified lines
1717 #include "luaconf.h"
1818
1919
20−#define LUA_VERSION "Lua 5.1 (work4)"
21−#define LUA_COPYRIGHT "Copyright (C) 1994-2004 Tecgraf, PUC-Rio"
20+#define LUA_VERSION "Lua 5.1 (work5)"
21+#define LUA_VERSION_NUM 501
22+#define LUA_COPYRIGHT "Copyright (C) 1994-2005 Tecgraf, PUC-Rio"
2223 #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
2324
2425
@@ -33,11 +34,12 @@ · 8 unmodified lines
3334 ** pseudo-indices
3435 */
3536 #define LUA_REGISTRYINDEX (-10000)
36−#define LUA_GLOBALSINDEX (-10001)
37+#define LUA_ENVIRONINDEX (-10001)
38+#define LUA_GLOBALSINDEX (-10002)
3739 #define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
3840
3941
40−/* return codes for `lua_pcall', `lua_resume', and `lua_threadstatus' */
42+/* return codes for `lua_pcall', `lua_resume', and `lua_status' */
4143 #define LUA_YIELD 1
4244 #define LUA_ERRRUN 2
4345 #define LUA_ERRSYNTAX 3
@@ -109,112 +111,112 @@ typedef LUA_INTEGER lua_Integer; · 65 unmodified lines
109111 /*
110112 ** state manipulation
111113 */
112−LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud);
113−LUA_API void lua_close (lua_State *L);
114−LUA_API lua_State *lua_newthread (lua_State *L);
114+LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
115+LUA_API void (lua_close) (lua_State *L);
116+LUA_API lua_State *(lua_newthread) (lua_State *L);
115117
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);
117119
118120
119121 /*
120122 ** basic stack manipulation
121123 */
122−LUA_API int lua_gettop (lua_State *L);
123−LUA_API void lua_settop (lua_State *L, int idx);
124−LUA_API void lua_pushvalue (lua_State *L, int idx);
125−LUA_API void lua_remove (lua_State *L, int idx);
126−LUA_API void lua_insert (lua_State *L, int idx);
127−LUA_API void lua_replace (lua_State *L, int idx);
128−LUA_API int lua_checkstack (lua_State *L, int sz);
124+LUA_API int (lua_gettop) (lua_State *L);
125+LUA_API void (lua_settop) (lua_State *L, int idx);
126+LUA_API void (lua_pushvalue) (lua_State *L, int idx);
127+LUA_API void (lua_remove) (lua_State *L, int idx);
128+LUA_API void (lua_insert) (lua_State *L, int idx);
129+LUA_API void (lua_replace) (lua_State *L, int idx);
130+LUA_API int (lua_checkstack) (lua_State *L, int sz);
129131
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);
131133
132134
133135 /*
134136 ** access functions (stack -> C)
135137 */
136138
137−LUA_API int lua_isnumber (lua_State *L, int idx);
138−LUA_API int lua_isstring (lua_State *L, int idx);
139−LUA_API int lua_iscfunction (lua_State *L, int idx);
140−LUA_API int lua_isuserdata (lua_State *L, int idx);
141−LUA_API int lua_type (lua_State *L, int idx);
142−LUA_API const char *lua_typename (lua_State *L, int tp);
139+LUA_API int (lua_isnumber) (lua_State *L, int idx);
140+LUA_API int (lua_isstring) (lua_State *L, int idx);
141+LUA_API int (lua_iscfunction) (lua_State *L, int idx);
142+LUA_API int (lua_isuserdata) (lua_State *L, int idx);
143+LUA_API int (lua_type) (lua_State *L, int idx);
144+LUA_API const char *(lua_typename) (lua_State *L, int tp);
143145
144−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);
146−LUA_API int lua_lessthan (lua_State *L, int idx1, int idx2);
146+LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2);
147+LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
148+LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2);
147149
148−LUA_API lua_Number lua_tonumber (lua_State *L, int idx);
149−LUA_API lua_Integer lua_tointeger (lua_State *L, int idx);
150−LUA_API int lua_toboolean (lua_State *L, int idx);
151−LUA_API const char *lua_tostring (lua_State *L, int idx);
152−LUA_API size_t lua_objsize (lua_State *L, int idx);
153−LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx);
154−LUA_API void *lua_touserdata (lua_State *L, int idx);
155−LUA_API lua_State *lua_tothread (lua_State *L, int idx);
156−LUA_API const void *lua_topointer (lua_State *L, int idx);
150+LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx);
151+LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx);
152+LUA_API int (lua_toboolean) (lua_State *L, int idx);
153+LUA_API const char *(lua_tostring) (lua_State *L, int idx);
154+LUA_API size_t (lua_objsize) (lua_State *L, int idx);
155+LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx);
156+LUA_API void *(lua_touserdata) (lua_State *L, int idx);
157+LUA_API lua_State *(lua_tothread) (lua_State *L, int idx);
158+LUA_API const void *(lua_topointer) (lua_State *L, int idx);
157159
158160
159161 /*
160162 ** push functions (C -> stack)
161163 */
162−LUA_API void lua_pushnil (lua_State *L);
163−LUA_API void lua_pushnumber (lua_State *L, lua_Number n);
164−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);
166−LUA_API void lua_pushstring (lua_State *L, const char *s);
167−LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
168− va_list argp);
169−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);
171−LUA_API void lua_pushboolean (lua_State *L, int b);
172−LUA_API void lua_pushlightuserdata (lua_State *L, void *p);
173−LUA_API int lua_pushthread (lua_State *L);
164+LUA_API void (lua_pushnil) (lua_State *L);
165+LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
166+LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
167+LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l);
168+LUA_API void (lua_pushstring) (lua_State *L, const char *s);
169+LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
170+ va_list argp);
171+LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);
172+LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
173+LUA_API void (lua_pushboolean) (lua_State *L, int b);
174+LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p);
175+LUA_API int (lua_pushthread) (lua_State *L);
174176
175177
176178 /*
177179 ** get functions (Lua -> stack)
178180 */
179−LUA_API void lua_gettable (lua_State *L, int idx);
180−LUA_API void lua_getfield (lua_State *L, int idx, const char *k);
181−LUA_API void lua_rawget (lua_State *L, int idx);
182−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);
184−LUA_API void *lua_newuserdata (lua_State *L, size_t sz);
185−LUA_API int lua_getmetatable (lua_State *L, int objindex);
186−LUA_API void lua_getfenv (lua_State *L, int idx);
181+LUA_API void (lua_gettable) (lua_State *L, int idx);
182+LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k);
183+LUA_API void (lua_rawget) (lua_State *L, int idx);
184+LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n);
185+LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
186+LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);
187+LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
188+LUA_API void (lua_getfenv) (lua_State *L, int idx);
187189
188190
189191 /*
190192 ** set functions (stack -> Lua)
191193 */
192−LUA_API void lua_settable (lua_State *L, int idx);
193−LUA_API void lua_setfield (lua_State *L, int idx, const char *k);
194−LUA_API void lua_rawset (lua_State *L, int idx);
195−LUA_API void lua_rawseti (lua_State *L, int idx, int n);
196−LUA_API int lua_setmetatable (lua_State *L, int objindex);
197−LUA_API int lua_setfenv (lua_State *L, int idx);
194+LUA_API void (lua_settable) (lua_State *L, int idx);
195+LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
196+LUA_API void (lua_rawset) (lua_State *L, int idx);
197+LUA_API void (lua_rawseti) (lua_State *L, int idx, int n);
198+LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
199+LUA_API int (lua_setfenv) (lua_State *L, int idx);
198200
199201
200202 /*
201203 ** `load' and `call' functions (load and run Lua code)
202204 */
203−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);
205−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,
207− const char *chunkname);
205+LUA_API void (lua_call) (lua_State *L, int nargs, int nresults);
206+LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
207+LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud);
208+LUA_API int (lua_load) (lua_State *L, lua_Chunkreader reader, void *dt,
209+ const char *chunkname);
208210
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);
210212
211213
212214 /*
213215 ** coroutine functions
214216 */
215−LUA_API int lua_yield (lua_State *L, int nresults);
216−LUA_API int lua_resume (lua_State *L, int narg);
217−LUA_API int lua_threadstatus (lua_State *L);
217+LUA_API int (lua_yield) (lua_State *L, int nresults);
218+LUA_API int (lua_resume) (lua_State *L, int narg);
219+LUA_API int (lua_status) (lua_State *L);
218220
219221 /*
220222 ** garbage-collection function and options
@@ -226,24 +228,22 @@ LUA_API int lua_threadstatus (lua_State *L); · 5 unmodified lines
226228 #define LUA_GCCOUNT 3
227229 #define LUA_GCSTEP 4
228230 #define LUA_GCSETPACE 5
229−#define LUA_GCSETINCMODE 6
231+#define LUA_GCSETSTEPMUL 6
230232
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);
232234
233235
234236 /*
235237 ** miscellaneous functions
236238 */
237239
238−LUA_API const char *lua_version (void);
240+LUA_API int (lua_error) (lua_State *L);
239241
240−LUA_API int lua_error (lua_State *L);
242+LUA_API int (lua_next) (lua_State *L, int idx);
241243
242−LUA_API int lua_next (lua_State *L, int idx);
244+LUA_API void (lua_concat) (lua_State *L, int n);
243245
244−LUA_API void lua_concat (lua_State *L, int n);
245−
246−LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud);
246+LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
247247
248248
249249
src/luaconf.h+46−33
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Configuration file for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -96,7 +96,7 @@ · 90 unmodified lines
9696 #define stdin_is_tty() isatty(0)
9797 #elif defined(_WIN32)
9898 #include <io.h>
99−#include <fcntl.h>
99+#include <stdio.h>
100100 #define stdin_is_tty() _isatty(_fileno(stdin))
101101 #else
102102 #define stdin_is_tty() 1 /* assume stdin is a tty */
@@ -108,16 +108,6 @@ · 5 unmodified lines
108108 #define PROGNAME "lua"
109109
110110
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−
121111
122112
123113 /*
@@ -217,8 +207,7 @@ · 93 unmodified lines
217207 #if defined(__GNUC__) && defined(__i386)
218208 #define lua_number2int(i,d) __asm__ ("fistpl %0":"=m"(i):"t"(d):"st")
219209
220−#elif defined(_WIN32) && defined(_M_IX86)
221−#include <math.h>
210+#elif defined(_MSC_VER) && defined(_M_IX86)
222211 #pragma warning(disable: 4514)
223212 __inline int l_lrint (double flt)
224213 { int i;
@@ -248,6 +237,8 @@ __inline int l_lrint (double flt) · 23 unmodified lines
248237 /* function to convert a lua_Number to a string */
249238 #include <stdio.h>
250239 #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) */
251242
252243 /* function to convert a string to a lua_Number */
253244 #define lua_str2number(s,p) strtod((s), (p))
@@ -258,12 +249,29 @@ __inline int l_lrint (double flt) · 4 unmodified lines
258249 #define LUA_UACNUMBER double
259250
260251
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+
261265
262266 /* type to ensure maximum alignment */
263267 #define LUSER_ALIGNMENT_T union { double u; void *s; long l; }
264268
265269
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+*/
267275 #ifndef __cplusplus
268276 /* default handling with long jumps */
269277 #include <setjmp.h>
@@ -282,18 +290,20 @@ __inline int l_lrint (double flt) · 12 unmodified lines
282290
283291
284292 /*
285−** macros for thread synchronization inside Lua core machine:
286−** all accesses to the global state and to global objects are synchronized.
287−** Because threads can read the stack of other threads
288−** (when running garbage collection),
289−** a thread must also synchronize any write-access to its own stack.
290−** Unsynchronized accesses are allowed only when reading its own stack,
291−** or when reading immutable fields from global objects
292−** (such as string values and udata values).
293+** macros for thread synchronization inside Lua core machine: This is
294+** an attempt to simplify the implementation of a multithreaded version
295+** of Lua. Do not change that unless you know what you are doing. all
296+** accesses to the global state and to global objects are synchronized.
297+** Because threads can read the stack of other threads (when running
298+** garbage collection), a thread must also synchronize any write-access
299+** to its own stack. Unsynchronized accesses are allowed only when
300+** reading its own stack, or when reading immutable fields from global
301+** objects (such as string values and udata values).
293302 */
294303 #define lua_lock(L) ((void) 0)
295304 #define lua_unlock(L) ((void) 0)
296305
306+
297307 /*
298308 ** this macro allows a thread switch in appropriate places in the Lua
299309 ** core
@@ -303,7 +313,7 @@ __inline int l_lrint (double flt) · 3 unmodified lines
303313
304314
305315 /* allows user-specific initialization on new threads */
306−#define lua_userstateopen(L) /* empty */
316+#define lua_userstateopen(L) ((void)0)
307317
308318
309319 #endif
@@ -321,9 +331,6 @@ __inline int l_lrint (double flt) · 11 unmodified lines
321331 #ifdef LUA_LIB
322332
323333
324−
325−/* `assert' options */
326−
327334 /* environment variables that hold the search path for packages */
328335 #define LUA_PATH "LUA_PATH"
329336 #define LUA_CPATH "LUA_CPATH"
@@ -332,7 +339,7 @@ __inline int l_lrint (double flt) · 2 unmodified lines
332339 #define LUA_POF "luaopen_"
333340
334341 /* separator for open functions in C libraries */
335−#define LUA_OFSEP ""
342+#define LUA_OFSEP "_"
336343
337344 /* directory separator (for submodules) */
338345 #if defined(_WIN32)
@@ -342,18 +349,20 @@ __inline int l_lrint (double flt) · 3 unmodified lines
342349 #endif
343350
344351 /* separator of templates in a path */
345−#define LUA_PATH_SEP ';'
352+#define LUA_PATHSEP ';'
346353
347354 /* wild char in each template */
348355 #define LUA_PATH_MARK "?"
349356
350357
351−/* maximum number of captures in pattern-matching */
352−#define MAX_CAPTURES 32 /* arbitrary limit */
358+/* maximum number of captures in pattern-matching (arbitrary limit) */
359+#define MAX_CAPTURES 32
353360
354361
355362 /*
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.
357366 */
358367 #ifdef __GNUC__
359368 #define USE_TMPNAME 0
@@ -363,7 +372,11 @@ __inline int l_lrint (double flt) · 3 unmodified lines
363372
364373
365374 /*
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.
367380 */
368381 #if defined(_WIN32)
369382 #define USE_DLL
src/lualib.h+10−10
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Lua standard libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -16,32 +16,32 @@ · 10 unmodified lines
1616
1717
1818 #define LUA_COLIBNAME "coroutine"
19−LUALIB_API int luaopen_base (lua_State *L);
19+LUALIB_API int (luaopen_base) (lua_State *L);
2020
2121 #define LUA_TABLIBNAME "table"
22−LUALIB_API int luaopen_table (lua_State *L);
22+LUALIB_API int (luaopen_table) (lua_State *L);
2323
2424 #define LUA_IOLIBNAME "io"
25−LUALIB_API int luaopen_io (lua_State *L);
25+LUALIB_API int (luaopen_io) (lua_State *L);
2626
2727 #define LUA_OSLIBNAME "os"
28−LUALIB_API int luaopen_os (lua_State *L);
28+LUALIB_API int (luaopen_os) (lua_State *L);
2929
3030 #define LUA_STRLIBNAME "string"
31−LUALIB_API int luaopen_string (lua_State *L);
31+LUALIB_API int (luaopen_string) (lua_State *L);
3232
3333 #define LUA_MATHLIBNAME "math"
34−LUALIB_API int luaopen_math (lua_State *L);
34+LUALIB_API int (luaopen_math) (lua_State *L);
3535
3636 #define LUA_DBLIBNAME "debug"
37−LUALIB_API int luaopen_debug (lua_State *L);
37+LUALIB_API int (luaopen_debug) (lua_State *L);
3838
3939
40−LUALIB_API int luaopen_loadlib (lua_State *L);
40+LUALIB_API int (luaopen_loadlib) (lua_State *L);
4141
4242
4343 /* open all previous libraries */
44−LUALIB_API int luaopen_stdlibs (lua_State *L);
44+LUALIB_API int (luaopen_stdlibs) (lua_State *L);
4545
4646
4747 #endif
src/lvm.c+39−36
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Lua virtual machine
44 ** See Copyright Notice in lua.h
55 */
@@ -47,7 +47,7 @@ int luaV_tostring (lua_State *L, StkId obj) { · 41 unmodified lines
4747 if (!ttisnumber(obj))
4848 return 0;
4949 else {
50− char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
50+ char s[MAX_NUMBER2STR];
5151 lua_number2str(s, nvalue(obj));
5252 setsvalue2s(L, obj, luaS_new(L, s));
5353 return 1;
@@ -113,7 +113,7 @@ StkId luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val, · 59 unmodified lines
113113 const TValue *tm;
114114 if (ttistable(t)) { /* `t' is a table? */
115115 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 */
117117 if (!ttisnil(res) || /* result is no nil? */
118118 (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
119119 setobj2s(L, val, res);
@@ -241,7 +241,7 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { · 121 unmodified lines
241241 if (ttype(l) != ttype(r))
242242 return luaG_ordererror(L, l, r);
243243 else if (ttisnumber(l))
244− return nvalue(l) < nvalue(r);
244+ return num_lt(nvalue(l), nvalue(r));
245245 else if (ttisstring(l))
246246 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
247247 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
255255 if (ttype(l) != ttype(r))
256256 return luaG_ordererror(L, l, r);
257257 else if (ttisnumber(l))
258− return nvalue(l) <= nvalue(r);
258+ return num_le(nvalue(l), nvalue(r));
259259 else if (ttisstring(l))
260260 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
261261 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
271271 lua_assert(ttype(t1) == ttype(t2));
272272 switch (ttype(t1)) {
273273 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));
275275 case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
276276 case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
277277 case LUA_TUSERDATA: {
@@ -335,18 +335,11 @@ static StkId Arith (lua_State *L, StkId ra, const TValue *rb, · 57 unmodified lines
335335 if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
336336 (c = luaV_tonumber(rc, &tempc)) != NULL) {
337337 switch (op) {
338− case TM_ADD: setnvalue(ra, nvalue(b) + nvalue(c)); break;
339− case TM_SUB: setnvalue(ra, nvalue(b) - nvalue(c)); break;
340− case TM_MUL: setnvalue(ra, nvalue(b) * nvalue(c)); break;
341− case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break;
342− case TM_POW: {
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− }
338+ case TM_ADD: setnvalue(ra, num_add(nvalue(b), nvalue(c))); break;
339+ case TM_SUB: setnvalue(ra, num_sub(nvalue(b), nvalue(c))); break;
340+ case TM_MUL: setnvalue(ra, num_mul(nvalue(b), nvalue(c))); break;
341+ case TM_DIV: setnvalue(ra, num_div(nvalue(b), nvalue(c))); break;
342+ case TM_POW: setnvalue(ra, num_pow(nvalue(b), nvalue(c))); break;
350343 default: lua_assert(0); break;
351344 }
352345 }
@@ -435,9 +428,11 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 82 unmodified lines
435428 continue;
436429 }
437430 case OP_GETGLOBAL: {
431+ TValue g;
438432 TValue *rb = KBx(i);
439− lua_assert(ttisstring(rb) && ttistable(&cl->g));
440− base = luaV_gettable(L, &cl->g, rb, ra, pc); /***/
433+ sethvalue(L, &g, cl->env);
434+ lua_assert(ttisstring(rb));
435+ base = luaV_gettable(L, &g, rb, ra, pc); /***/
441436 continue;
442437 }
443438 case OP_GETTABLE: {
@@ -445,8 +440,10 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 1 unmodified line
445440 continue;
446441 }
447442 case OP_SETGLOBAL: {
448− lua_assert(ttisstring(KBx(i)) && ttistable(&cl->g));
449− base = luaV_settable(L, &cl->g, KBx(i), ra, pc); /***/
443+ TValue g;
444+ sethvalue(L, &g, cl->env);
445+ lua_assert(ttisstring(KBx(i)));
446+ base = luaV_settable(L, &g, KBx(i), ra, pc); /***/
450447 continue;
451448 }
452449 case OP_SETUPVAL: {
@@ -461,8 +458,8 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 8 unmodified lines
461458 }
462459 case OP_NEWTABLE: {
463460 int b = GETARG_B(i);
464− b = luaO_fb2int(b);
465− sethvalue(L, ra, luaH_new(L, b, GETARG_C(i)));
461+ int c = GETARG_C(i);
462+ sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
466463 L->ci->savedpc = pc;
467464 luaC_checkGC(L); /***/
468465 base = L->base;
@@ -478,7 +475,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 9 unmodified lines
478475 TValue *rb = RKB(i);
479476 TValue *rc = RKC(i);
480477 if (ttisnumber(rb) && ttisnumber(rc)) {
481− setnvalue(ra, nvalue(rb) + nvalue(rc));
478+ setnvalue(ra, num_add(nvalue(rb), nvalue(rc)));
482479 }
483480 else
484481 base = Arith(L, ra, rb, rc, TM_ADD, pc); /***/
@@ -488,7 +485,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 3 unmodified lines
488485 TValue *rb = RKB(i);
489486 TValue *rc = RKC(i);
490487 if (ttisnumber(rb) && ttisnumber(rc)) {
491− setnvalue(ra, nvalue(rb) - nvalue(rc));
488+ setnvalue(ra, num_sub(nvalue(rb), nvalue(rc)));
492489 }
493490 else
494491 base = Arith(L, ra, rb, rc, TM_SUB, pc); /***/
@@ -498,7 +495,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 3 unmodified lines
498495 TValue *rb = RKB(i);
499496 TValue *rc = RKC(i);
500497 if (ttisnumber(rb) && ttisnumber(rc)) {
501− setnvalue(ra, nvalue(rb) * nvalue(rc));
498+ setnvalue(ra, num_mul(nvalue(rb), nvalue(rc)));
502499 }
503500 else
504501 base = Arith(L, ra, rb, rc, TM_MUL, pc); /***/
@@ -508,21 +505,27 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 3 unmodified lines
508505 TValue *rb = RKB(i);
509506 TValue *rc = RKC(i);
510507 if (ttisnumber(rb) && ttisnumber(rc)) {
511− setnvalue(ra, nvalue(rb) / nvalue(rc));
508+ setnvalue(ra, num_div(nvalue(rb), nvalue(rc)));
512509 }
513510 else
514511 base = Arith(L, ra, rb, rc, TM_DIV, pc); /***/
515512 continue;
516513 }
517514 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); /***/
519522 continue;
520523 }
521524 case OP_UNM: {
522525 const TValue *rb = RB(i);
523526 TValue temp;
524527 if (tonumber(rb, &temp)) {
525− setnvalue(ra, -nvalue(rb));
528+ setnvalue(ra, num_unm(nvalue(rb)));
526529 }
527530 else {
528531 setnilvalue(&temp);
@@ -617,9 +620,9 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 88 unmodified lines
617620 int aux;
618621 StkId func = ci->func;
619622 StkId pfunc = (ci+1)->func; /* previous function index */
623+ if (L->openupval) luaF_close(L, base);
620624 base = ci->base = ci->func + ((ci+1)->base - pfunc);
621625 L->base = base;
622− if (L->openupval) luaF_close(L, base);
623626 for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
624627 setobjs2s(L, func+aux, pfunc+aux);
625628 ci->top = L->top = func+aux; /* correct top */
@@ -658,9 +661,9 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 32 unmodified lines
658661 }
659662 case OP_FORLOOP: {
660663 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 */
662665 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)) {
664667 dojump(L, pc, GETARG_sBx(i)); /* jump back */
665668 setnvalue(ra, idx); /* update internal index... */
666669 setnvalue(ra+3, idx); /* ...and external index */
@@ -678,7 +681,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 11 unmodified lines
678681 luaG_runerror(L, "`for' limit must be a number");
679682 else if (!tonumber(pstep, ra+2))
680683 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)));
682685 dojump(L, pc, GETARG_sBx(i));
683686 continue;
684687 }
@@ -723,7 +726,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 38 unmodified lines
723726 if (c == 0) c = cast(int, *pc++);
724727 last = ((c-1)*LFIELDS_PER_FLUSH) + n + LUA_FIRSTINDEX - 1;
725728 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 */
727730 for (; n > 0; n--) {
728731 TValue *val = ra+n;
729732 setobj2t(L, luaH_setnum(L, h, last--), val);
@@ -741,7 +744,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { · 11 unmodified lines
741744 int nup, j;
742745 p = cl->p->p[GETARG_Bx(i)];
743746 nup = p->nups;
744− ncl = luaF_newLclosure(L, nup, &cl->g);
747+ ncl = luaF_newLclosure(L, nup, cl->env);
745748 ncl->l.p = p;
746749 for (j=0; j<nup; j++, pc++) {
747750 if (GET_OPCODE(*pc) == OP_GETUPVAL)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t