first version of _ENV; no more global variables

This commit is contained in:
Roberto Ierusalimschy 2010-03-12 16:14:06 -03:00
parent f292760f12
commit 25c557ec63
12 changed files with 52 additions and 124 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.234 2009/12/22 15:32:50 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.235 2009/12/28 16:30:31 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -376,7 +376,7 @@ static int luaB_loadin (lua_State *L) {
n = luaB_load_aux(L, 2); n = luaB_load_aux(L, 2);
if (n == 1) { /* success? */ if (n == 1) { /* success? */
lua_pushvalue(L, 1); /* environment for loaded function */ lua_pushvalue(L, 1); /* environment for loaded function */
lua_setfenv(L, -2); lua_setupvalue(L, -2, 1);
} }
return n; return n;
} }

14
lcode.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 2.43 2010/01/11 17:38:30 roberto Exp roberto $ ** $Id: lcode.c,v 2.44 2010/02/26 20:40:29 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -372,11 +372,6 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) {
e->k = VRELOCABLE; e->k = VRELOCABLE;
break; break;
} }
case VGLOBAL: {
e->u.s.info = luaK_codeABxX(fs, OP_GETGLOBAL, 0, e->u.s.info);
e->k = VRELOCABLE;
break;
}
case VINDEXED: { case VINDEXED: {
freereg(fs, e->u.s.aux); freereg(fs, e->u.s.aux);
freereg(fs, e->u.s.info); freereg(fs, e->u.s.info);
@ -555,11 +550,6 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0); luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0);
break; break;
} }
case VGLOBAL: {
int e = luaK_exp2anyreg(fs, ex);
luaK_codeABxX(fs, OP_SETGLOBAL, e, var->u.s.info);
break;
}
case VINDEXED: { case VINDEXED: {
int e = luaK_exp2RK(fs, ex); int e = luaK_exp2RK(fs, ex);
luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e); luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e);
@ -584,9 +574,9 @@ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
luaK_exp2anyreg(fs, e); luaK_exp2anyreg(fs, e);
freeexp(fs, e); freeexp(fs, e);
func = fs->freereg; func = fs->freereg;
luaK_reserveregs(fs, 2);
luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key)); luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key));
freeexp(fs, key); freeexp(fs, key);
luaK_reserveregs(fs, 2);
e->u.s.info = func; e->u.s.info = func;
e->k = VNONRELOC; e->k = VNONRELOC;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 2.64 2010/02/26 20:40:29 roberto Exp roberto $ ** $Id: ldebug.c,v 2.65 2010/03/05 14:01:29 roberto Exp roberto $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -260,11 +260,14 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
*/ */
static const char *kname (Proto *p, int c) { static void kname (Proto *p, int c, int reg, const char *what,
if (ISK(c) && ttisstring(&p->k[INDEXK(c)])) const char **name) {
return svalue(&p->k[INDEXK(c)]); if (c == reg && *what == 'c')
return; /* index is a constant; name already correct */
else if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
*name = svalue(&p->k[INDEXK(c)]);
else else
return "?"; *name = "?";
} }
@ -283,17 +286,6 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
OpCode op = GET_OPCODE(i); OpCode op = GET_OPCODE(i);
int a = GETARG_A(i); int a = GETARG_A(i);
switch (op) { switch (op) {
case OP_GETGLOBAL: {
if (reg == a) {
int g = GETARG_Bx(i);
if (g != 0) g--;
else g = GETARG_Ax(p->code[++pc]);
lua_assert(ttisstring(&p->k[g]));
*name = svalue(&p->k[g]);
what = "global";
}
break;
}
case OP_MOVE: { case OP_MOVE: {
if (reg == a) { if (reg == a) {
int b = GETARG_B(i); /* move from 'b' to 'a' */ int b = GETARG_B(i); /* move from 'b' to 'a' */
@ -307,8 +299,12 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
case OP_GETTABLE: { case OP_GETTABLE: {
if (reg == a) { if (reg == a) {
int k = GETARG_C(i); /* key index */ int k = GETARG_C(i); /* key index */
*name = kname(p, k); int t = GETARG_B(i);
what = "field"; const char *tabname = (op == OP_GETTABLE)
? luaF_getlocalname(p, t + 1, pc)
: getstr(p->upvalues[t].name);
kname(p, k, a, what, name);
what = (tabname && strcmp(tabname, "_ENV") == 0) ? "global" : "field";
} }
break; break;
} }
@ -321,6 +317,17 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
} }
break; break;
} }
case OP_LOADK: {
if (reg == a) {
int b = GETARG_Bx(i);
b = (b > 0) ? b - 1 : GETARG_Ax(p->code[pc + 1]);
if (ttisstring(&p->k[b])) {
what = "constant";
*name = svalue(&p->k[b]);
}
}
break;
}
case OP_LOADNIL: { case OP_LOADNIL: {
int b = GETARG_B(i); /* move from 'b' to 'a' */ int b = GETARG_B(i); /* move from 'b' to 'a' */
if (a <= reg && reg <= b) /* set registers from 'a' to 'b' */ if (a <= reg && reg <= b) /* set registers from 'a' to 'b' */
@ -330,7 +337,7 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
case OP_SELF: { case OP_SELF: {
if (reg == a) { if (reg == a) {
int k = GETARG_C(i); /* key index */ int k = GETARG_C(i); /* key index */
*name = kname(p, k); kname(p, k, a, what, name);
what = "method"; what = "method";
} }
break; break;
@ -378,11 +385,10 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
*name = "for iterator"; *name = "for iterator";
return "for iterator"; return "for iterator";
} }
case OP_GETGLOBAL:
case OP_SELF: case OP_SELF:
case OP_GETTABUP: case OP_GETTABUP:
case OP_GETTABLE: tm = TM_INDEX; break; case OP_GETTABLE: tm = TM_INDEX; break;
case OP_SETGLOBAL: case OP_SETTABUP:
case OP_SETTABLE: tm = TM_NEWINDEX; break; case OP_SETTABLE: tm = TM_NEWINDEX; break;
case OP_EQ: tm = TM_EQ; break; case OP_EQ: tm = TM_EQ; break;
case OP_ADD: tm = TM_ADD; break; case OP_ADD: tm = TM_ADD; break;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldump.c,v 2.11 2009/09/28 16:32:50 roberto Exp roberto $ ** $Id: ldump.c,v 2.12 2009/09/30 15:38:37 roberto Exp roberto $
** save precompiled Lua chunks ** save precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -145,7 +145,6 @@ static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
DumpChar(f->numparams,D); DumpChar(f->numparams,D);
DumpChar(f->is_vararg,D); DumpChar(f->is_vararg,D);
DumpChar(f->maxstacksize,D); DumpChar(f->maxstacksize,D);
DumpChar(f->envreg,D);
DumpCode(f,D); DumpCode(f,D);
DumpConstants(f,D); DumpConstants(f,D);
DumpUpvalues(f,D); DumpUpvalues(f,D);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lfunc.c,v 2.18 2009/12/11 13:39:34 roberto Exp roberto $ ** $Id: lfunc.c,v 2.19 2009/12/16 16:42:58 roberto Exp roberto $
** Auxiliary functions to manipulate prototypes and closures ** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -127,7 +127,6 @@ Proto *luaF_newproto (lua_State *L) {
f->linedefined = 0; f->linedefined = 0;
f->lastlinedefined = 0; f->lastlinedefined = 0;
f->source = NULL; f->source = NULL;
f->envreg = NO_REG;
return f; return f;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 2.33 2009/11/19 19:06:52 roberto Exp roberto $ ** $Id: lobject.h,v 2.34 2010/01/08 20:00:20 roberto Exp roberto $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -264,7 +264,6 @@ typedef struct Proto {
lu_byte numparams; lu_byte numparams;
lu_byte is_vararg; lu_byte is_vararg;
lu_byte maxstacksize; lu_byte maxstacksize;
lu_byte envreg; /* register in outer function with initial environment */
} Proto; } Proto;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.c,v 1.41 2009/11/19 19:06:52 roberto Exp roberto $ ** $Id: lopcodes.c,v 1.42 2010/02/26 20:40:29 roberto Exp roberto $
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -19,10 +19,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
"LOADBOOL", "LOADBOOL",
"LOADNIL", "LOADNIL",
"GETUPVAL", "GETUPVAL",
"GETGLOBAL",
"GETTABUP", "GETTABUP",
"GETTABLE", "GETTABLE",
"SETGLOBAL",
"SETTABUP", "SETTABUP",
"SETUPVAL", "SETUPVAL",
"SETTABLE", "SETTABLE",
@ -69,10 +67,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */ ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */
,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */
,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */
,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */
,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */
,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */
,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */
,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */
,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.h,v 1.133 2009/11/19 19:06:52 roberto Exp roberto $ ** $Id: lopcodes.h,v 1.134 2010/02/26 20:40:29 roberto Exp roberto $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -171,11 +171,9 @@ OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */ OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
OP_GETUPVAL,/* A B R(A) := UpValue[B] */ OP_GETUPVAL,/* A B R(A) := UpValue[B] */
OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx - 1)] */
OP_GETTABUP,/* A B C R(A) := UpValue[B][RK(C)] */ OP_GETTABUP,/* A B C R(A) := UpValue[B][RK(C)] */
OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */ OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */
OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx - 1)] := R(A) */
OP_SETTABUP,/* A B C UpValue[A][RK(B)] := RK(C) */ OP_SETTABUP,/* A B C UpValue[A][RK(B)] := RK(C) */
OP_SETUPVAL,/* A B UpValue[B] := R(A) */ OP_SETUPVAL,/* A B UpValue[B] := R(A) */
OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */ OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */
@ -245,8 +243,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
(*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next (*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next
'instruction' is EXTRAARG(real C). 'instruction' is EXTRAARG(real C).
(*) In OP_LOADK, OP_GETGLOBAL, and OP_SETGLOBAL, if (Bx == 0) then next (*) In OP_LOADK, if (Bx == 0) then next 'instruction' is EXTRAARG(real Bx).
'instruction' is EXTRAARG(real Bx).
(*) For comparisons, A specifies what condition the test should accept (*) For comparisons, A specifies what condition the test should accept
(true or false). (true or false).

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 2.77 2010/03/04 18:12:57 roberto Exp roberto $ ** $Id: lparser.c,v 2.78 2010/03/08 16:55:52 roberto Exp roberto $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -259,7 +259,7 @@ static void markupval (FuncState *fs, int level) {
*/ */
static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
if (fs == NULL) /* no more levels? */ if (fs == NULL) /* no more levels? */
return VGLOBAL; /* default is global variable */ return VVOID; /* default is global */
else { else {
int v = searchvar(fs, n); /* look up locals at current level */ int v = searchvar(fs, n); /* look up locals at current level */
if (v >= 0) { /* found? */ if (v >= 0) { /* found? */
@ -271,8 +271,8 @@ static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
else { /* not found as local at current level; try upvalues */ else { /* not found as local at current level; try upvalues */
int idx = searchupvalue(fs, n); /* try existing upvalues */ int idx = searchupvalue(fs, n); /* try existing upvalues */
if (idx < 0) { /* not found? */ if (idx < 0) { /* not found? */
if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL) /* try upper levels */ if (singlevaraux(fs->prev, n, var, 0) == VVOID) /* try upper levels */
return VGLOBAL; /* not found; is a global */ return VVOID; /* not found; is a global */
/* else was LOCAL or UPVAL */ /* else was LOCAL or UPVAL */
idx = newupvalue(fs, n, var); /* will be a new upvalue */ idx = newupvalue(fs, n, var); /* will be a new upvalue */
} }
@ -286,15 +286,12 @@ static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
static void singlevar (LexState *ls, expdesc *var) { static void singlevar (LexState *ls, expdesc *var) {
TString *varname = str_checkname(ls); TString *varname = str_checkname(ls);
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
if (singlevaraux(fs, varname, var, 1) == VGLOBAL) { if (singlevaraux(fs, varname, var, 1) == VVOID) { /* global name? */
if (fs->envreg == NO_REG) /* regular global? */ expdesc key;
init_exp(var, VGLOBAL, luaK_stringK(fs, varname)); singlevaraux(fs, ls->envn, var, 1); /* get _ENV variable */
else { /* "globals" are in current lexical environment */ lua_assert(var->k == VLOCAL || var->k == VUPVAL);
expdesc key; codestring(ls, &key, varname); /* key is variable name */
init_exp(var, VLOCAL, fs->envreg); /* current environment */ luaK_indexed(fs, var, &key); /* env[varname] */
codestring(ls, &key, varname); /* key is variable name */
luaK_indexed(fs, var, &key); /* env[varname] */
}
} }
} }
@ -363,7 +360,6 @@ static void pushclosure (LexState *ls, Proto *clp, expdesc *v) {
while (oldsize < f->sizep) f->p[oldsize++] = NULL; while (oldsize < f->sizep) f->p[oldsize++] = NULL;
f->p[fs->np++] = clp; f->p[fs->np++] = clp;
/* initial environment for new function is current lexical environment */ /* initial environment for new function is current lexical environment */
clp->envreg = fs->envreg;
luaC_objbarrier(ls->L, f, clp); luaC_objbarrier(ls->L, f, clp);
init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1)); init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
} }
@ -386,7 +382,6 @@ static void open_func (LexState *ls, FuncState *fs) {
fs->nlocvars = 0; fs->nlocvars = 0;
fs->nactvar = 0; fs->nactvar = 0;
fs->firstlocal = ls->varl->nactvar; fs->firstlocal = ls->varl->nactvar;
fs->envreg = NO_REG;
fs->bl = NULL; fs->bl = NULL;
f = luaF_newproto(L); f = luaF_newproto(L);
fs->f = f; fs->f = f;
@ -1304,24 +1299,6 @@ static void funcstat (LexState *ls, int line) {
} }
static void instat (LexState *ls, int line) {
/* instat -> IN exp DO block END */
FuncState *fs = ls->fs;
int oldenv = fs->envreg; /* save current environment */
BlockCnt bl;
luaX_next(ls); /* skip IN */
enterblock(fs, &bl, 0); /* scope for environment variable */
new_localvarliteral(ls, "(environment)");
fs->envreg = exp1(ls); /* new environment */
adjustlocalvars(ls, 1);
checknext(ls, TK_DO);
block(ls);
leaveblock(fs);
check_match(ls, TK_END, TK_IN, line);
fs->envreg = oldenv; /* restore outer environment */
}
static void exprstat (LexState *ls) { static void exprstat (LexState *ls) {
/* stat -> func | assignment */ /* stat -> func | assignment */
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
@ -1386,10 +1363,6 @@ static int statement (LexState *ls) {
check_match(ls, TK_END, TK_DO, line); check_match(ls, TK_END, TK_DO, line);
return 0; return 0;
} }
case TK_IN: {
instat(ls, line);
return 0;
}
case TK_FOR: { /* stat -> forstat */ case TK_FOR: { /* stat -> forstat */
forstat(ls, line); forstat(ls, line);
return 0; return 0;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.h,v 1.61 2009/10/11 20:02:19 roberto Exp roberto $ ** $Id: lparser.h,v 1.62 2010/02/26 20:40:29 roberto Exp roberto $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -25,7 +25,6 @@ typedef enum {
VKNUM, /* nval = numerical value */ VKNUM, /* nval = numerical value */
VLOCAL, /* info = local register */ VLOCAL, /* info = local register */
VUPVAL, /* info = index of upvalue in 'upvalues' */ VUPVAL, /* info = index of upvalue in 'upvalues' */
VGLOBAL, /* info = index of global name in 'k' */
VINDEXED, /* info = table R/K; aux = index R/K */ VINDEXED, /* info = table R/K; aux = index R/K */
VINDEXEDUP, /* info = table upvalue; aux = R/K */ VINDEXEDUP, /* info = table upvalue; aux = R/K */
VJMP, /* info = instruction pc */ VJMP, /* info = instruction pc */
@ -81,7 +80,6 @@ typedef struct FuncState {
short nlocvars; /* number of elements in `locvars' */ short nlocvars; /* number of elements in `locvars' */
lu_byte nactvar; /* number of active local variables */ lu_byte nactvar; /* number of active local variables */
lu_byte nups; /* number of upvalues */ lu_byte nups; /* number of upvalues */
lu_byte envreg; /* register holding current lexical environment */
} FuncState; } FuncState;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.c,v 2.11 2009/09/28 16:32:50 roberto Exp roberto $ ** $Id: lundump.c,v 2.12 2009/09/30 15:38:37 roberto Exp roberto $
** load precompiled Lua chunks ** load precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -180,7 +180,6 @@ static Proto* LoadFunction(LoadState* S, TString* p)
f->numparams=LoadByte(S); f->numparams=LoadByte(S);
f->is_vararg=LoadByte(S); f->is_vararg=LoadByte(S);
f->maxstacksize=LoadByte(S); f->maxstacksize=LoadByte(S);
f->envreg=LoadByte(S);
LoadCode(S,f); LoadCode(S,f);
LoadConstants(S,f); LoadConstants(S,f);
LoadUpvalues(S,f); LoadUpvalues(S,f);

34
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.104 2010/02/26 20:40:29 roberto Exp roberto $ ** $Id: lvm.c,v 2.105 2010/02/27 21:16:24 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -355,14 +355,10 @@ void luaV_finishOp (lua_State *L) {
StkId base = ci->u.l.base; StkId base = ci->u.l.base;
Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
OpCode op = GET_OPCODE(inst); OpCode op = GET_OPCODE(inst);
if (op == OP_EXTRAARG) { /* extra argument? */
inst = *(ci->u.l.savedpc - 2); /* get its 'main' instruction */
op = GET_OPCODE(inst);
}
switch (op) { /* finish its execution */ switch (op) { /* finish its execution */
case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV:
case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN: case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN:
case OP_GETGLOBAL: case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: { case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
setobjs2s(L, base + GETARG_A(inst), --L->top); setobjs2s(L, base + GETARG_A(inst), --L->top);
break; break;
} }
@ -403,7 +399,7 @@ void luaV_finishOp (lua_State *L) {
L->top = ci->top; /* adjust results */ L->top = ci->top; /* adjust results */
break; break;
} }
case OP_TAILCALL: case OP_SETGLOBAL: case OP_SETTABUP: case OP_SETTABLE: case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
break; break;
default: lua_assert(0); default: lua_assert(0);
} }
@ -501,14 +497,6 @@ void luaV_execute (lua_State *L) {
setobj2s(L, ra, cl->upvals[b]->v); setobj2s(L, ra, cl->upvals[b]->v);
break; break;
} }
case OP_GETGLOBAL: {
TValue g;
TValue *rb = KBx(i);
sethvalue(L, &g, cl->env);
lua_assert(ttisstring(rb));
Protect(luaV_gettable(L, &g, rb, ra));
break;
}
case OP_GETTABUP: { case OP_GETTABUP: {
int b = GETARG_B(i); int b = GETARG_B(i);
Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra)); Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
@ -518,14 +506,6 @@ void luaV_execute (lua_State *L) {
Protect(luaV_gettable(L, RB(i), RKC(i), ra)); Protect(luaV_gettable(L, RB(i), RKC(i), ra));
break; break;
} }
case OP_SETGLOBAL: {
TValue g;
TValue *rb = KBx(i);
sethvalue(L, &g, cl->env);
lua_assert(ttisstring(rb));
Protect(luaV_settable(L, &g, rb, ra));
break;
}
case OP_SETTABUP: { case OP_SETTABUP: {
int a = GETARG_A(i); int a = GETARG_A(i);
Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i))); Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
@ -796,14 +776,6 @@ void luaV_execute (lua_State *L) {
int j; int j;
ncl->l.p = p; ncl->l.p = p;
setclvalue(L, ra, ncl); /* anchor new closure in stack */ setclvalue(L, ra, ncl); /* anchor new closure in stack */
if (p->envreg != NO_REG) { /* lexical environment? */
StkId env = base + p->envreg;
if (!ttistable(env))
luaG_runerror(L, "environment is not a table: "
"cannot create closure");
else
ncl->l.env = hvalue(env);
}
for (j = 0; j < nup; j++) { /* fill in upvalues */ for (j = 0; j < nup; j++) { /* fill in upvalues */
if (uv[j].instack) /* upvalue refers to local variable? */ if (uv[j].instack) /* upvalue refers to local variable? */
ncl->l.upvals[j] = luaF_findupval(L, base + uv[j].idx); ncl->l.upvals[j] = luaF_findupval(L, base + uv[j].idx);