aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lapi.c97
-rw-r--r--lbaselib.c40
-rw-r--r--ldblib.c22
-rw-r--r--ldo.c4
-rw-r--r--lfunc.c8
-rw-r--r--lfunc.h6
-rw-r--r--lgc.c7
-rw-r--r--linit.c9
-rw-r--r--liolib.c8
-rw-r--r--lobject.h5
-rw-r--r--lstate.c7
-rw-r--r--lstate.h3
-rw-r--r--ltests.c10
-rw-r--r--lua.h9
-rw-r--r--luaconf.h9
-rw-r--r--lvm.c4
16 files changed, 78 insertions, 170 deletions
diff --git a/lapi.c b/lapi.c
index 3a0dbdae..adae47b8 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 2.115 2010/03/22 18:28:03 roberto Exp roberto $ 2** $Id: lapi.c,v 2.116 2010/03/25 19:37:23 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -39,16 +39,6 @@ const char lua_ident[] =
39 "invalid index") 39 "invalid index")
40 40
41 41
42static Table *getcurrenv (lua_State *L) {
43 if (L->ci->previous == NULL) /* no enclosing function? */
44 return G(L)->l_gt; /* use global table as environment */
45 else {
46 Closure *func = curr_func(L);
47 return func->c.env;
48 }
49}
50
51
52static TValue *index2addr (lua_State *L, int idx) { 42static TValue *index2addr (lua_State *L, int idx) {
53 CallInfo *ci = L->ci; 43 CallInfo *ci = L->ci;
54 if (idx > 0) { 44 if (idx > 0) {
@@ -61,20 +51,15 @@ static TValue *index2addr (lua_State *L, int idx) {
61 api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); 51 api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
62 return L->top + idx; 52 return L->top + idx;
63 } 53 }
64 else switch (idx) { /* pseudo-indices */ 54 else if (idx == LUA_REGISTRYINDEX)
65 case LUA_REGISTRYINDEX: return &G(L)->l_registry; 55 return &G(L)->l_registry;
66 case LUA_ENVIRONINDEX: { 56 else { /* upvalues */
67 sethvalue(L, &L->env, getcurrenv(L)); 57 Closure *func = curr_func(L);
68 return &L->env; 58 idx = LUA_REGISTRYINDEX - idx;
69 } 59 api_check(L, idx <= UCHAR_MAX + 1, "upvalue index too large");
70 default: { 60 return (idx <= func->c.nupvalues)
71 Closure *func = curr_func(L); 61 ? &func->c.upvalue[idx-1]
72 idx = LUA_ENVIRONINDEX - idx; 62 : cast(TValue *, luaO_nilobject);
73 api_check(L, idx <= UCHAR_MAX + 1, "upvalue index too large");
74 return (idx <= func->c.nupvalues)
75 ? &func->c.upvalue[idx-1]
76 : cast(TValue *, luaO_nilobject);
77 }
78 } 63 }
79} 64}
80 65
@@ -195,17 +180,9 @@ LUA_API void lua_insert (lua_State *L, int idx) {
195static void moveto (lua_State *L, TValue *fr, int idx) { 180static void moveto (lua_State *L, TValue *fr, int idx) {
196 TValue *to = index2addr(L, idx); 181 TValue *to = index2addr(L, idx);
197 api_checkvalidindex(L, to); 182 api_checkvalidindex(L, to);
198 if (idx == LUA_ENVIRONINDEX) { 183 setobj(L, to, fr);
199 Closure *func = curr_func(L); 184 if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
200 api_check(L, ttistable(fr), "table expected"); 185 luaC_barrier(L, curr_func(L), fr);
201 func->c.env = hvalue(fr);
202 luaC_barrier(L, func, fr);
203 }
204 else {
205 setobj(L, to, fr);
206 if (idx < LUA_ENVIRONINDEX) /* function upvalue? */
207 luaC_barrier(L, curr_func(L), fr);
208 }
209 /* LUA_REGISTRYINDEX does not need gc barrier 186 /* LUA_REGISTRYINDEX does not need gc barrier
210 (collector revisits it before finishing collection) */ 187 (collector revisits it before finishing collection) */
211} 188}
@@ -213,9 +190,6 @@ static void moveto (lua_State *L, TValue *fr, int idx) {
213 190
214LUA_API void lua_replace (lua_State *L, int idx) { 191LUA_API void lua_replace (lua_State *L, int idx) {
215 lua_lock(L); 192 lua_lock(L);
216 /* explicit test for incompatible code */
217 if (idx == LUA_ENVIRONINDEX && L->ci->previous == NULL)
218 luaG_runerror(L, "no calling environment");
219 api_checknelems(L, 1); 193 api_checknelems(L, 1);
220 moveto(L, L->top - 1, idx); 194 moveto(L, L->top - 1, idx);
221 L->top--; 195 L->top--;
@@ -503,7 +477,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
503 api_checknelems(L, n); 477 api_checknelems(L, n);
504 api_check(L, n <= UCHAR_MAX, "upvalue index too large"); 478 api_check(L, n <= UCHAR_MAX, "upvalue index too large");
505 luaC_checkGC(L); 479 luaC_checkGC(L);
506 cl = luaF_newCclosure(L, n, getcurrenv(L)); 480 cl = luaF_newCclosure(L, n);
507 cl->c.f = fn; 481 cl->c.f = fn;
508 L->top -= n; 482 L->top -= n;
509 while (n--) 483 while (n--)
@@ -631,22 +605,16 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) {
631} 605}
632 606
633 607
634LUA_API void lua_getfenv (lua_State *L, int idx) { 608LUA_API void lua_getenv (lua_State *L, int idx) {
635 StkId o; 609 StkId o;
636 lua_lock(L); 610 lua_lock(L);
637 o = index2addr(L, idx); 611 o = index2addr(L, idx);
638 api_checkvalidindex(L, o); 612 api_checkvalidindex(L, o);
639 switch (ttype(o)) { 613 api_check(L, ttisuserdata(o), "userdata expected");
640 case LUA_TFUNCTION: 614 if (uvalue(o)->env) {
641 sethvalue(L, L->top, clvalue(o)->c.env); 615 sethvalue(L, L->top, uvalue(o)->env);
642 break; 616 } else
643 case LUA_TUSERDATA: 617 setnilvalue(L->top);
644 sethvalue(L, L->top, uvalue(o)->env);
645 break;
646 default:
647 setnilvalue(L->top);
648 break;
649 }
650 api_incr_top(L); 618 api_incr_top(L);
651 lua_unlock(L); 619 lua_unlock(L);
652} 620}
@@ -747,29 +715,22 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
747} 715}
748 716
749 717
750LUA_API int lua_setfenv (lua_State *L, int idx) { 718LUA_API void lua_setenv (lua_State *L, int idx) {
751 StkId o; 719 StkId o;
752 int res = 1;
753 lua_lock(L); 720 lua_lock(L);
754 api_checknelems(L, 1); 721 api_checknelems(L, 1);
755 o = index2addr(L, idx); 722 o = index2addr(L, idx);
756 api_checkvalidindex(L, o); 723 api_checkvalidindex(L, o);
757 api_check(L, ttistable(L->top - 1), "table expected"); 724 api_check(L, ttisuserdata(o), "userdata expected");
758 switch (ttype(o)) { 725 if (ttisnil(L->top - 1))
759 case LUA_TFUNCTION: 726 uvalue(o)->env = NULL;
760 clvalue(o)->c.env = hvalue(L->top - 1); 727 else {
761 break; 728 api_check(L, ttistable(L->top - 1), "table expected");
762 case LUA_TUSERDATA: 729 uvalue(o)->env = hvalue(L->top - 1);
763 uvalue(o)->env = hvalue(L->top - 1); 730 luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
764 break;
765 default:
766 res = 0;
767 break;
768 } 731 }
769 if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
770 L->top--; 732 L->top--;
771 lua_unlock(L); 733 lua_unlock(L);
772 return res;
773} 734}
774 735
775 736
@@ -1072,7 +1033,7 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1072 Udata *u; 1033 Udata *u;
1073 lua_lock(L); 1034 lua_lock(L);
1074 luaC_checkGC(L); 1035 luaC_checkGC(L);
1075 u = luaS_newudata(L, size, getcurrenv(L)); 1036 u = luaS_newudata(L, size, NULL);
1076 setuvalue(L, L->top, u); 1037 setuvalue(L, L->top, u);
1077 api_incr_top(L); 1038 api_incr_top(L);
1078 lua_unlock(L); 1039 lua_unlock(L);
diff --git a/lbaselib.c b/lbaselib.c
index bba92816..0f74f53e 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.238 2010/03/19 15:52:48 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.239 2010/03/22 18:28:03 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -107,50 +107,12 @@ static int luaB_setmetatable (lua_State *L) {
107} 107}
108 108
109 109
110
111#if defined(LUA_COMPAT_FENV)
112
113static void getfunc (lua_State *L, int opt) {
114 if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
115 else {
116 lua_Debug ar;
117 int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
118 luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
119 if (lua_getstack(L, level, &ar) == 0)
120 luaL_argerror(L, 1, "invalid level");
121 lua_getinfo(L, "f", &ar);
122 }
123}
124
125static int luaB_getfenv (lua_State *L) {
126 getfunc(L, 1);
127 if (lua_iscfunction(L, -1)) /* is a C function? */
128 lua_pushglobaltable(L); /* return the global env. */
129 else
130 lua_getfenv(L, -1);
131 return 1;
132}
133
134static int luaB_setfenv (lua_State *L) {
135 luaL_checktype(L, 2, LUA_TTABLE);
136 getfunc(L, 0);
137 lua_pushvalue(L, 2);
138 if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
139 return luaL_error(L,
140 LUA_QL("setfenv") " cannot change environment of given object");
141 return 1;
142}
143
144#else
145
146static int luaB_getfenv (lua_State *L) { 110static int luaB_getfenv (lua_State *L) {
147 return luaL_error(L, "getfenv/setfenv deprecated"); 111 return luaL_error(L, "getfenv/setfenv deprecated");
148} 112}
149 113
150#define luaB_setfenv luaB_getfenv 114#define luaB_setfenv luaB_getfenv
151 115
152#endif
153
154 116
155static int luaB_rawequal (lua_State *L) { 117static int luaB_rawequal (lua_State *L) {
156 luaL_checkany(L, 1); 118 luaL_checkany(L, 1);
diff --git a/ldblib.c b/ldblib.c
index 1633a9e3..38bc40ac 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.119 2010/01/06 14:42:35 roberto Exp roberto $ 2** $Id: ldblib.c,v 1.120 2010/02/18 19:18:41 roberto Exp roberto $
3** Interface from Lua to its debug API 3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -44,19 +44,19 @@ static int db_setmetatable (lua_State *L) {
44} 44}
45 45
46 46
47static int db_getfenv (lua_State *L) { 47static int db_getenv (lua_State *L) {
48 luaL_checkany(L, 1); 48 luaL_checktype(L, 1, LUA_TUSERDATA);
49 lua_getfenv(L, 1); 49 lua_getenv(L, 1);
50 return 1; 50 return 1;
51} 51}
52 52
53 53
54static int db_setfenv (lua_State *L) { 54static int db_setenv (lua_State *L) {
55 luaL_checktype(L, 2, LUA_TTABLE); 55 luaL_checktype(L, 1, LUA_TUSERDATA);
56 if (!lua_isnoneornil(L, 2))
57 luaL_checktype(L, 2, LUA_TTABLE);
56 lua_settop(L, 2); 58 lua_settop(L, 2);
57 if (lua_setfenv(L, 1) == 0) 59 lua_setenv(L, 1);
58 luaL_error(L, LUA_QL("setfenv")
59 " cannot change environment of given object");
60 return 1; 60 return 1;
61} 61}
62 62
@@ -367,7 +367,7 @@ static int db_traceback (lua_State *L) {
367 367
368static const luaL_Reg dblib[] = { 368static const luaL_Reg dblib[] = {
369 {"debug", db_debug}, 369 {"debug", db_debug},
370 {"getfenv", db_getfenv}, 370 {"getenv", db_getenv},
371 {"gethook", db_gethook}, 371 {"gethook", db_gethook},
372 {"getinfo", db_getinfo}, 372 {"getinfo", db_getinfo},
373 {"getlocal", db_getlocal}, 373 {"getlocal", db_getlocal},
@@ -376,7 +376,7 @@ static const luaL_Reg dblib[] = {
376 {"getupvalue", db_getupvalue}, 376 {"getupvalue", db_getupvalue},
377 {"upvaluejoin", db_upvaluejoin}, 377 {"upvaluejoin", db_upvaluejoin},
378 {"upvalueid", db_upvalueid}, 378 {"upvalueid", db_upvalueid},
379 {"setfenv", db_setfenv}, 379 {"setenv", db_setenv},
380 {"sethook", db_sethook}, 380 {"sethook", db_sethook},
381 {"setlocal", db_setlocal}, 381 {"setlocal", db_setlocal},
382 {"setmetatable", db_setmetatable}, 382 {"setmetatable", db_setmetatable},
diff --git a/ldo.c b/ldo.c
index 0d540228..d00ed860 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 2.80 2010/01/13 16:17:32 roberto Exp roberto $ 2** $Id: ldo.c,v 2.81 2010/02/09 11:56:29 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -622,7 +622,7 @@ static void f_parser (lua_State *L, void *ud) {
622 : luaY_parser(L, p->z, &p->buff, &p->varl, p->name); 622 : luaY_parser(L, p->z, &p->buff, &p->varl, p->name);
623 setptvalue2s(L, L->top, tf); 623 setptvalue2s(L, L->top, tf);
624 incr_top(L); 624 incr_top(L);
625 cl = luaF_newLclosure(L, tf->sizeupvalues, G(L)->l_gt); 625 cl = luaF_newLclosure(L, tf->sizeupvalues);
626 cl->l.p = tf; 626 cl->l.p = tf;
627 setclvalue(L, L->top - 1, cl); 627 setclvalue(L, L->top - 1, cl);
628 for (i = 0; i < tf->sizeupvalues; i++) /* initialize upvalues */ 628 for (i = 0; i < tf->sizeupvalues; i++) /* initialize upvalues */
diff --git a/lfunc.c b/lfunc.c
index f4e30faa..daa430c6 100644
--- a/lfunc.c
+++ b/lfunc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lfunc.c,v 2.19 2009/12/16 16:42:58 roberto Exp roberto $ 2** $Id: lfunc.c,v 2.20 2010/03/12 19:14:06 roberto Exp roberto $
3** Auxiliary functions to manipulate prototypes and closures 3** Auxiliary functions to manipulate prototypes and closures
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -21,19 +21,17 @@
21 21
22 22
23 23
24Closure *luaF_newCclosure (lua_State *L, int n, Table *e) { 24Closure *luaF_newCclosure (lua_State *L, int n) {
25 Closure *c = &luaC_newobj(L, LUA_TFUNCTION, sizeCclosure(n), NULL, 0)->cl; 25 Closure *c = &luaC_newobj(L, LUA_TFUNCTION, sizeCclosure(n), NULL, 0)->cl;
26 c->c.isC = 1; 26 c->c.isC = 1;
27 c->c.env = e;
28 c->c.nupvalues = cast_byte(n); 27 c->c.nupvalues = cast_byte(n);
29 return c; 28 return c;
30} 29}
31 30
32 31
33Closure *luaF_newLclosure (lua_State *L, int n, Table *e) { 32Closure *luaF_newLclosure (lua_State *L, int n) {
34 Closure *c = &luaC_newobj(L, LUA_TFUNCTION, sizeLclosure(n), NULL, 0)->cl; 33 Closure *c = &luaC_newobj(L, LUA_TFUNCTION, sizeLclosure(n), NULL, 0)->cl;
35 c->l.isC = 0; 34 c->l.isC = 0;
36 c->l.env = e;
37 c->l.nupvalues = cast_byte(n); 35 c->l.nupvalues = cast_byte(n);
38 while (n--) c->l.upvals[n] = NULL; 36 while (n--) c->l.upvals[n] = NULL;
39 return c; 37 return c;
diff --git a/lfunc.h b/lfunc.h
index 6f19e2fa..0d52058d 100644
--- a/lfunc.h
+++ b/lfunc.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lfunc.h,v 2.3 2005/02/18 12:40:02 roberto Exp roberto $ 2** $Id: lfunc.h,v 2.4 2005/04/25 19:24:10 roberto Exp roberto $
3** Auxiliary functions to manipulate prototypes and closures 3** Auxiliary functions to manipulate prototypes and closures
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -19,8 +19,8 @@
19 19
20 20
21LUAI_FUNC Proto *luaF_newproto (lua_State *L); 21LUAI_FUNC Proto *luaF_newproto (lua_State *L);
22LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); 22LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems);
23LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); 23LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems);
24LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 24LUAI_FUNC UpVal *luaF_newupval (lua_State *L);
25LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 25LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
26LUAI_FUNC void luaF_close (lua_State *L, StkId level); 26LUAI_FUNC void luaF_close (lua_State *L, StkId level);
diff --git a/lgc.c b/lgc.c
index b8ab674f..0612f4a5 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 2.72 2010/03/25 13:06:36 roberto Exp roberto $ 2** $Id: lgc.c,v 2.73 2010/03/25 19:37:23 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -375,7 +375,6 @@ static void traverseproto (global_State *g, Proto *f) {
375 375
376 376
377static void traverseclosure (global_State *g, Closure *cl) { 377static void traverseclosure (global_State *g, Closure *cl) {
378 markobject(g, cl->c.env);
379 if (cl->c.isC) { 378 if (cl->c.isC) {
380 int i; 379 int i;
381 for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */ 380 for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
@@ -605,8 +604,6 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
605 604
606static void checkSizes (lua_State *L) { 605static void checkSizes (lua_State *L) {
607 global_State *g = G(L); 606 global_State *g = G(L);
608 if (g->gckind == KGC_EMERGENCY)
609 return; /* do not move buffers during emergency collection */
610 if (g->strt.nuse < cast(lu_int32, g->strt.size)) { 607 if (g->strt.nuse < cast(lu_int32, g->strt.size)) {
611 /* string-table size could be the smaller power of 2 larger than 'nuse' */ 608 /* string-table size could be the smaller power of 2 larger than 'nuse' */
612 int size = 1 << luaO_ceillog2(g->strt.nuse); 609 int size = 1 << luaO_ceillog2(g->strt.nuse);
@@ -787,7 +784,6 @@ static l_mem singlestep (lua_State *L) {
787 sweeplist(L, cast(GCObject **, &g->mainthread), 1); 784 sweeplist(L, cast(GCObject **, &g->mainthread), 1);
788 g->sweepgc = &g->udgc; /* prepare to sweep userdata */ 785 g->sweepgc = &g->udgc; /* prepare to sweep userdata */
789 g->gcstate = GCSsweepudata; 786 g->gcstate = GCSsweepudata;
790 checkSizes(L);
791 } 787 }
792 return GCSWEEPCOST; 788 return GCSWEEPCOST;
793 } 789 }
@@ -809,6 +805,7 @@ static l_mem singlestep (lua_State *L) {
809 return GCFINALIZECOST; 805 return GCFINALIZECOST;
810 } 806 }
811 else { 807 else {
808 checkSizes(L);
812 g->gcstate = GCSpause; /* end collection */ 809 g->gcstate = GCSpause; /* end collection */
813 return 0; 810 return 0;
814 } 811 }
diff --git a/linit.c b/linit.c
index 095a835a..79dec99f 100644
--- a/linit.c
+++ b/linit.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: linit.c,v 1.22 2009/12/17 12:26:09 roberto Exp roberto $ 2** $Id: linit.c,v 1.23 2009/12/22 15:32:50 roberto Exp roberto $
3** Initialization of libraries for lua.c and other clients 3** Initialization of libraries for lua.c and other clients
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -52,9 +52,9 @@ LUALIB_API void luaL_openlibs (lua_State *L) {
52 const luaL_Reg *lib; 52 const luaL_Reg *lib;
53 /* call open functions from 'loadedlibs' */ 53 /* call open functions from 'loadedlibs' */
54 for (lib = loadedlibs; lib->func; lib++) { 54 for (lib = loadedlibs; lib->func; lib++) {
55 lua_pushcfunction(L, lib->func); 55 lua_settop(L, 0);
56 lua_pushstring(L, lib->name); 56 lua_pushstring(L, lib->name);
57 lua_call(L, 1, 0); 57 (lib->func)(L);
58 } 58 }
59 /* add open functions from 'preloadedlibs' into 'package.preload' table */ 59 /* add open functions from 'preloadedlibs' into 'package.preload' table */
60 lua_pushglobaltable(L); 60 lua_pushglobaltable(L);
@@ -65,8 +65,7 @@ LUALIB_API void luaL_openlibs (lua_State *L) {
65 } 65 }
66 lua_pop(L, 1); /* remove package.preload table */ 66 lua_pop(L, 1); /* remove package.preload table */
67#if defined(LUA_COMPAT_DEBUGLIB) 67#if defined(LUA_COMPAT_DEBUGLIB)
68 lua_pushglobaltable(L); 68 lua_getglobal(L, "require");
69 lua_getfield(L, -1, "require");
70 lua_pushliteral(L, LUA_DBLIBNAME); 69 lua_pushliteral(L, LUA_DBLIBNAME);
71 lua_call(L, 1, 0); /* call 'require"debug"' */ 70 lua_call(L, 1, 0); /* call 'require"debug"' */
72 lua_pop(L, 1); /* remove global table */ 71 lua_pop(L, 1); /* remove global table */
diff --git a/liolib.c b/liolib.c
index fce6b69b..6a290098 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 2.86 2010/03/03 18:48:57 roberto Exp roberto $ 2** $Id: liolib.c,v 2.87 2010/03/17 21:37:37 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -120,7 +120,7 @@ static FILE **newprefile (lua_State *L) {
120static FILE **newfile (lua_State *L) { 120static FILE **newfile (lua_State *L) {
121 FILE **pf = newprefile(L); 121 FILE **pf = newprefile(L);
122 lua_pushvalue(L, lua_upvalueindex(1)); /* set upvalue... */ 122 lua_pushvalue(L, lua_upvalueindex(1)); /* set upvalue... */
123 lua_setfenv(L, -2); /* ... as environment for new file */ 123 lua_setenv(L, -2); /* ... as environment for new file */
124 return pf; 124 return pf;
125} 125}
126 126
@@ -163,7 +163,7 @@ static int io_fclose (lua_State *L) {
163 163
164 164
165static int aux_close (lua_State *L) { 165static int aux_close (lua_State *L) {
166 lua_getfenv(L, 1); 166 lua_getenv(L, 1);
167 lua_getfield(L, -1, "__close"); 167 lua_getfield(L, -1, "__close");
168 return (lua_tocfunction(L, -1))(L); 168 return (lua_tocfunction(L, -1))(L);
169} 169}
@@ -595,7 +595,7 @@ static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
595 lua_rawseti(L, 1, k); /* add it to common upvalue */ 595 lua_rawseti(L, 1, k); /* add it to common upvalue */
596 } 596 }
597 lua_pushvalue(L, 3); /* get environment for default files */ 597 lua_pushvalue(L, 3); /* get environment for default files */
598 lua_setfenv(L, -2); /* set it as environment for file */ 598 lua_setenv(L, -2); /* set it as environment for file */
599 lua_setfield(L, 2, fname); /* add file to module */ 599 lua_setfield(L, 2, fname); /* add file to module */
600} 600}
601 601
diff --git a/lobject.h b/lobject.h
index 4d28438f..8ea1c830 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 2.34 2010/01/08 20:00:20 roberto Exp roberto $ 2** $Id: lobject.h,v 2.35 2010/03/12 19:14:06 roberto Exp roberto $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -297,8 +297,7 @@ typedef struct UpVal {
297*/ 297*/
298 298
299#define ClosureHeader \ 299#define ClosureHeader \
300 CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \ 300 CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist
301 struct Table *env
302 301
303typedef struct CClosure { 302typedef struct CClosure {
304 ClosureHeader; 303 ClosureHeader;
diff --git a/lstate.c b/lstate.c
index b00b62a1..d47e01c8 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 2.73 2010/03/25 13:06:36 roberto Exp roberto $ 2** $Id: lstate.c,v 2.74 2010/03/25 19:37:23 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -118,9 +118,6 @@ static void freestack (lua_State *L) {
118static int ccall (lua_State *L) { 118static int ccall (lua_State *L) {
119 lua_CFunction f = *(lua_CFunction *)lua_touserdata(L, 1); 119 lua_CFunction f = *(lua_CFunction *)lua_touserdata(L, 1);
120 lua_remove(L, 1); /* remove f from stack */ 120 lua_remove(L, 1); /* remove f from stack */
121 /* restore original environment for 'ccall' */
122 lua_pushglobaltable(L);
123 lua_replace(L, LUA_ENVIRONINDEX);
124 return f(L); 121 return f(L);
125} 122}
126 123
@@ -139,7 +136,7 @@ static void init_registry (lua_State *L, global_State *g) {
139 setthvalue(L, &mt, L); 136 setthvalue(L, &mt, L);
140 setobj2t(L, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt); 137 setobj2t(L, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt);
141 /* registry[LUA_RIDX_CCALL] = ccall */ 138 /* registry[LUA_RIDX_CCALL] = ccall */
142 cp = luaF_newCclosure(L, 0, g->l_gt); 139 cp = luaF_newCclosure(L, 0);
143 cp->c.f = ccall; 140 cp->c.f = ccall;
144 setclvalue(L, &mt, cp); 141 setclvalue(L, &mt, cp);
145 setobj2t(L, luaH_setint(L, registry, LUA_RIDX_CCALL), &mt); 142 setobj2t(L, luaH_setint(L, registry, LUA_RIDX_CCALL), &mt);
diff --git a/lstate.h b/lstate.h
index 2377810a..17abe10b 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 2.56 2010/03/24 13:07:01 roberto Exp roberto $ 2** $Id: lstate.h,v 2.57 2010/03/25 19:37:23 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -168,7 +168,6 @@ struct lua_State {
168 int basehookcount; 168 int basehookcount;
169 int hookcount; 169 int hookcount;
170 lua_Hook hook; 170 lua_Hook hook;
171 TValue env; /* temporary place for environments */
172 GCObject *openupval; /* list of open upvalues in this stack */ 171 GCObject *openupval; /* list of open upvalues in this stack */
173 GCObject *gclist; 172 GCObject *gclist;
174 struct lua_longjmp *errorJmp; /* current error recover point */ 173 struct lua_longjmp *errorJmp; /* current error recover point */
diff --git a/ltests.c b/ltests.c
index d8c91947..15d02872 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 2.88 2010/03/24 13:07:01 roberto Exp roberto $ 2** $Id: ltests.c,v 2.89 2010/03/25 13:06:36 roberto Exp roberto $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -275,7 +275,6 @@ static void checkproto (global_State *g, Proto *f) {
275 275
276static void checkclosure (global_State *g, Closure *cl) { 276static void checkclosure (global_State *g, Closure *cl) {
277 GCObject *clgc = obj2gco(cl); 277 GCObject *clgc = obj2gco(cl);
278 checkobjref(g, clgc, cl->l.env);
279 if (cl->c.isC) { 278 if (cl->c.isC) {
280 int i; 279 int i;
281 for (i=0; i<cl->c.nupvalues; i++) 280 for (i=0; i<cl->c.nupvalues; i++)
@@ -875,7 +874,6 @@ static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) {
875 switch (*(*pc)++) { 874 switch (*(*pc)++) {
876 case 'R': return LUA_REGISTRYINDEX; 875 case 'R': return LUA_REGISTRYINDEX;
877 case 'G': return luaL_error(L, "deprecated index 'G'"); 876 case 'G': return luaL_error(L, "deprecated index 'G'");
878 case 'E': return LUA_ENVIRONINDEX;
879 case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc)); 877 case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc));
880 default: (*pc)--; return getnum_aux(L, L1, pc); 878 default: (*pc)--; return getnum_aux(L, L1, pc);
881 } 879 }
@@ -1021,6 +1019,9 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
1021 else if EQ("gettable") { 1019 else if EQ("gettable") {
1022 lua_gettable(L1, getindex); 1020 lua_gettable(L1, getindex);
1023 } 1021 }
1022 else if EQ("getglobal") {
1023 lua_getglobal(L1, getstring);
1024 }
1024 else if EQ("getfield") { 1025 else if EQ("getfield") {
1025 int t = getindex; 1026 int t = getindex;
1026 lua_getfield(L1, t, getstring); 1027 lua_getfield(L1, t, getstring);
@@ -1036,6 +1037,9 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
1036 else if EQ("settable") { 1037 else if EQ("settable") {
1037 lua_settable(L1, getindex); 1038 lua_settable(L1, getindex);
1038 } 1039 }
1040 else if EQ("setglobal") {
1041 lua_setglobal(L1, getstring);
1042 }
1039 else if EQ("next") { 1043 else if EQ("next") {
1040 lua_next(L1, -2); 1044 lua_next(L1, -2);
1041 } 1045 }
diff --git a/lua.h b/lua.h
index a0f909a5..0b3aa587 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.263 2010/03/19 21:04:17 roberto Exp roberto $ 2** $Id: lua.h,v 1.264 2010/03/22 18:28:03 roberto Exp roberto $
3** Lua - A Scripting Language 3** Lua - A Scripting Language
4** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 4** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
5** See Copyright Notice at the end of this file 5** See Copyright Notice at the end of this file
@@ -34,8 +34,7 @@
34** pseudo-indices 34** pseudo-indices
35*/ 35*/
36#define LUA_REGISTRYINDEX LUAI_FIRSTPSEUDOIDX 36#define LUA_REGISTRYINDEX LUAI_FIRSTPSEUDOIDX
37#define LUA_ENVIRONINDEX (LUA_REGISTRYINDEX - 1) 37#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i))
38#define lua_upvalueindex(i) (LUA_ENVIRONINDEX - (i))
39 38
40 39
41/* thread status */ 40/* thread status */
@@ -212,7 +211,7 @@ LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n);
212LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); 211LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
213LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); 212LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);
214LUA_API int (lua_getmetatable) (lua_State *L, int objindex); 213LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
215LUA_API void (lua_getfenv) (lua_State *L, int idx); 214LUA_API void (lua_getenv) (lua_State *L, int idx);
216 215
217 216
218/* 217/*
@@ -223,7 +222,7 @@ LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
223LUA_API void (lua_rawset) (lua_State *L, int idx); 222LUA_API void (lua_rawset) (lua_State *L, int idx);
224LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); 223LUA_API void (lua_rawseti) (lua_State *L, int idx, int n);
225LUA_API int (lua_setmetatable) (lua_State *L, int objindex); 224LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
226LUA_API int (lua_setfenv) (lua_State *L, int idx); 225LUA_API void (lua_setenv) (lua_State *L, int idx);
227 226
228 227
229/* 228/*
diff --git a/luaconf.h b/luaconf.h
index 582b02e3..a9a7e53f 100644
--- a/luaconf.h
+++ b/luaconf.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: luaconf.h,v 1.133 2010/02/18 19:18:41 roberto Exp roberto $ 2** $Id: luaconf.h,v 1.134 2010/03/03 18:53:02 roberto Exp roberto $
3** Configuration file for Lua 3** Configuration file for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -233,13 +233,6 @@
233#define lua_cpcall(L,f,u) \ 233#define lua_cpcall(L,f,u) \
234 (lua_pushlightuserdata(L,(u)), luaL_cpcall(L,(f),1,0)) 234 (lua_pushlightuserdata(L,(u)), luaL_cpcall(L,(f),1,0))
235 235
236/*
237@@ LUA_COMPAT_FENV controls the presence of functions 'setfenv/getfenv'.
238** You can replace them with lexical environments, 'loadin', or the
239** debug library.
240*/
241#define LUA_COMPAT_FENV
242
243 236
244/* 237/*
245@@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library. 238@@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library.
diff --git a/lvm.c b/lvm.c
index 3c0a8276..bcb4318f 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.105 2010/02/27 21:16:24 roberto Exp roberto $ 2** $Id: lvm.c,v 2.106 2010/03/12 19:14:06 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -771,7 +771,7 @@ void luaV_execute (lua_State *L) {
771 case OP_CLOSURE: { 771 case OP_CLOSURE: {
772 Proto *p = cl->p->p[GETARG_Bx(i)]; /* prototype for new closure */ 772 Proto *p = cl->p->p[GETARG_Bx(i)]; /* prototype for new closure */
773 int nup = p->sizeupvalues; 773 int nup = p->sizeupvalues;
774 Closure *ncl = luaF_newLclosure(L, nup, cl->env); 774 Closure *ncl = luaF_newLclosure(L, nup);
775 Upvaldesc *uv = p->upvalues; 775 Upvaldesc *uv = p->upvalues;
776 int j; 776 int j;
777 ncl->l.p = p; 777 ncl->l.p = p;