first implementation of centralized global state.

This commit is contained in:
Roberto Ierusalimschy 1997-11-19 15:29:23 -02:00
parent 9cdeb275e7
commit 592a3f289b
25 changed files with 782 additions and 829 deletions

179
lapi.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 1.3 1997/10/24 17:17:24 roberto Exp roberto $ ** $Id: lapi.c,v 1.4 1997/11/04 15:27:53 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -10,13 +10,12 @@
#include "lapi.h" #include "lapi.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lbuiltin.h"
#include "ldo.h" #include "ldo.h"
#include "lfunc.h" #include "lfunc.h"
#include "lgc.h" #include "lgc.h"
#include "llex.h"
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
#include "lstate.h"
#include "lstring.h" #include "lstring.h"
#include "ltable.h" #include "ltable.h"
#include "ltm.h" #include "ltm.h"
@ -38,41 +37,41 @@ TObject *luaA_Address (lua_Object o)
void luaA_packresults (void) void luaA_packresults (void)
{ {
luaV_pack(luaD_Cstack.lua2C, luaD_Cstack.num, luaD_stack.top); luaV_pack(L->Cstack.lua2C, L->Cstack.num, L->stack.top);
incr_top; incr_top;
} }
int luaA_passresults (void) int luaA_passresults (void)
{ {
luaD_checkstack(luaD_Cstack.num); luaD_checkstack(L->Cstack.num);
memcpy(luaD_stack.top, luaD_Cstack.lua2C+luaD_stack.stack, memcpy(L->stack.top, L->Cstack.lua2C+L->stack.stack,
luaD_Cstack.num*sizeof(TObject)); L->Cstack.num*sizeof(TObject));
luaD_stack.top += luaD_Cstack.num; L->stack.top += L->Cstack.num;
return luaD_Cstack.num; return L->Cstack.num;
} }
static void checkCparams (int nParams) static void checkCparams (int nParams)
{ {
if (luaD_stack.top-luaD_stack.stack < luaD_Cstack.base+nParams) if (L->stack.top-L->stack.stack < L->Cstack.base+nParams)
lua_error("API error - wrong number of arguments in C2lua stack"); lua_error("API error - wrong number of arguments in C2lua stack");
} }
static lua_Object put_luaObject (TObject *o) static lua_Object put_luaObject (TObject *o)
{ {
luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base); luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
luaD_stack.stack[luaD_Cstack.base++] = *o; L->stack.stack[L->Cstack.base++] = *o;
return luaD_Cstack.base; /* this is +1 real position (see Ref) */ return L->Cstack.base; /* this is +1 real position (see Ref) */
} }
static lua_Object put_luaObjectonTop (void) static lua_Object put_luaObjectonTop (void)
{ {
luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base); luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
luaD_stack.stack[luaD_Cstack.base++] = *(--luaD_stack.top); L->stack.stack[L->Cstack.base++] = *(--L->stack.top);
return luaD_Cstack.base; /* this is +1 real position (see Ref) */ return L->Cstack.base; /* this is +1 real position (see Ref) */
} }
@ -89,20 +88,20 @@ lua_Object lua_pop (void)
*/ */
lua_Object lua_lua2C (int number) lua_Object lua_lua2C (int number)
{ {
if (number <= 0 || number > luaD_Cstack.num) return LUA_NOOBJECT; if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
/* Ref(luaD_stack.stack+(luaD_Cstack.lua2C+number-1)) == /* Ref(L->stack.stack+(L->Cstack.lua2C+number-1)) ==
luaD_stack.stack+(luaD_Cstack.lua2C+number-1)-luaD_stack.stack+1 == */ L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */
return luaD_Cstack.lua2C+number; return L->Cstack.lua2C+number;
} }
lua_Object lua_upvalue (int n) lua_Object lua_upvalue (int n)
{ {
TObject *f = luaD_stack.stack+luaD_Cstack.lua2C-1; TObject *f = L->stack.stack+L->Cstack.lua2C-1;
if (ttype(f) != LUA_T_MARK || n <= 0 || n > clvalue(f)->nelems) if (ttype(f) != LUA_T_MARK || n <= 0 || n > clvalue(f)->nelems)
return LUA_NOOBJECT; return LUA_NOOBJECT;
if (ttype(protovalue(f)) != LUA_T_CPROTO) lua_error("BAD!!"); if (ttype(protovalue(f)) != LUA_T_CPROTO) lua_error("BAD!!");
*luaD_stack.top = clvalue(f)->consts[n]; *L->stack.top = clvalue(f)->consts[n];
incr_top; incr_top;
return put_luaObjectonTop(); return put_luaObjectonTop();
} }
@ -113,8 +112,8 @@ int lua_callfunction (lua_Object function)
if (function == LUA_NOOBJECT) if (function == LUA_NOOBJECT)
return 1; return 1;
else { else {
luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base); luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
luaD_stack.stack[luaD_Cstack.base] = *Address(function); L->stack.stack[L->Cstack.base] = *Address(function);
return luaD_protectedrun(MULT_RET); return luaD_protectedrun(MULT_RET);
} }
} }
@ -129,16 +128,16 @@ lua_Object lua_gettagmethod (int tag, char *event)
lua_Object lua_settagmethod (int tag, char *event) lua_Object lua_settagmethod (int tag, char *event)
{ {
checkCparams(1); checkCparams(1);
luaT_settagmethod(tag, event, luaD_stack.top-1); luaT_settagmethod(tag, event, L->stack.top-1);
return put_luaObjectonTop(); return put_luaObjectonTop();
} }
lua_Object lua_seterrormethod (void) lua_Object lua_seterrormethod (void)
{ {
TObject temp = luaD_errorim; TObject temp = L->errorim;
checkCparams(1); checkCparams(1);
luaD_errorim = *(--luaD_stack.top); L->errorim = *(--L->stack.top);
return put_luaObject(&temp); return put_luaObject(&temp);
} }
@ -154,15 +153,15 @@ lua_Object lua_gettable (void)
lua_Object lua_rawgettable (void) lua_Object lua_rawgettable (void)
{ {
checkCparams(2); checkCparams(2);
if (ttype(luaD_stack.top-2) != LUA_T_ARRAY) if (ttype(L->stack.top-2) != LUA_T_ARRAY)
lua_error("indexed expression not a table in raw gettable"); lua_error("indexed expression not a table in raw gettable");
else { else {
TObject *h = luaH_get(avalue(luaD_stack.top-2), luaD_stack.top-1); TObject *h = luaH_get(avalue(L->stack.top-2), L->stack.top-1);
--luaD_stack.top; --L->stack.top;
if (h != NULL) if (h != NULL)
*(luaD_stack.top-1) = *h; *(L->stack.top-1) = *h;
else else
ttype(luaD_stack.top-1) = LUA_T_NIL; ttype(L->stack.top-1) = LUA_T_NIL;
} }
return put_luaObjectonTop(); return put_luaObjectonTop();
} }
@ -171,14 +170,14 @@ lua_Object lua_rawgettable (void)
void lua_settable (void) void lua_settable (void)
{ {
checkCparams(3); checkCparams(3);
luaV_settable(luaD_stack.top-3, 1); luaV_settable(L->stack.top-3, 1);
} }
void lua_rawsettable (void) void lua_rawsettable (void)
{ {
checkCparams(3); checkCparams(3);
luaV_settable(luaD_stack.top-3, 0); luaV_settable(L->stack.top-3, 0);
} }
@ -192,6 +191,12 @@ lua_Object lua_createtable (void)
} }
lua_Object lua_globalbag (void)
{
return put_luaObject(&L->globalbag);
}
lua_Object lua_getglobal (char *name) lua_Object lua_getglobal (char *name)
{ {
luaD_checkstack(2); /* may need that to call T.M. */ luaD_checkstack(2); /* may need that to call T.M. */
@ -219,7 +224,7 @@ void lua_rawsetglobal (char *name)
{ {
TaggedString *ts = luaS_new(name); TaggedString *ts = luaS_new(name);
checkCparams(1); checkCparams(1);
luaS_rawsetglobal(ts, --luaD_stack.top); luaS_rawsetglobal(ts, --L->stack.top);
} }
@ -293,24 +298,24 @@ lua_CFunction lua_getcfunction (lua_Object object)
void lua_pushnil (void) void lua_pushnil (void)
{ {
ttype(luaD_stack.top) = LUA_T_NIL; ttype(L->stack.top) = LUA_T_NIL;
incr_top; incr_top;
} }
void lua_pushnumber (real n) void lua_pushnumber (real n)
{ {
ttype(luaD_stack.top) = LUA_T_NUMBER; ttype(L->stack.top) = LUA_T_NUMBER;
nvalue(luaD_stack.top) = n; nvalue(L->stack.top) = n;
incr_top; incr_top;
} }
void lua_pushstring (char *s) void lua_pushstring (char *s)
{ {
if (s == NULL) if (s == NULL)
ttype(luaD_stack.top) = LUA_T_NIL; ttype(L->stack.top) = LUA_T_NIL;
else { else {
tsvalue(luaD_stack.top) = luaS_new(s); tsvalue(L->stack.top) = luaS_new(s);
ttype(luaD_stack.top) = LUA_T_STRING; ttype(L->stack.top) = LUA_T_STRING;
} }
incr_top; incr_top;
luaC_checkGC(); luaC_checkGC();
@ -319,13 +324,13 @@ void lua_pushstring (char *s)
void lua_pushCclosure (lua_CFunction fn, int n) void lua_pushCclosure (lua_CFunction fn, int n)
{ {
if (fn == NULL) { if (fn == NULL) {
ttype(luaD_stack.top) = LUA_T_NIL; ttype(L->stack.top) = LUA_T_NIL;
incr_top; incr_top;
} }
else { else {
checkCparams(n); checkCparams(n);
ttype(luaD_stack.top) = LUA_T_CPROTO; ttype(L->stack.top) = LUA_T_CPROTO;
fvalue(luaD_stack.top) = fn; fvalue(L->stack.top) = fn;
incr_top; incr_top;
luaV_closure(n); luaV_closure(n);
} }
@ -335,15 +340,15 @@ void lua_pushusertag (void *u, int tag)
{ {
if (tag < 0 && tag != LUA_ANYTAG) if (tag < 0 && tag != LUA_ANYTAG)
luaT_realtag(tag); /* error if tag is not valid */ luaT_realtag(tag); /* error if tag is not valid */
tsvalue(luaD_stack.top) = luaS_createudata(u, tag); tsvalue(L->stack.top) = luaS_createudata(u, tag);
ttype(luaD_stack.top) = LUA_T_USERDATA; ttype(L->stack.top) = LUA_T_USERDATA;
incr_top; incr_top;
luaC_checkGC(); luaC_checkGC();
} }
void luaA_pushobject (TObject *o) void luaA_pushobject (TObject *o)
{ {
*luaD_stack.top = *o; *L->stack.top = *o;
incr_top; incr_top;
} }
@ -351,9 +356,9 @@ void lua_pushobject (lua_Object o)
{ {
if (o == LUA_NOOBJECT) if (o == LUA_NOOBJECT)
lua_error("API error - attempt to push a NOOBJECT"); lua_error("API error - attempt to push a NOOBJECT");
*luaD_stack.top = *Address(o); *L->stack.top = *Address(o);
if (ttype(luaD_stack.top) == LUA_T_MARK) if (ttype(L->stack.top) == LUA_T_MARK)
ttype(luaD_stack.top) = LUA_T_FUNCTION; ttype(L->stack.top) = LUA_T_FUNCTION;
incr_top; incr_top;
} }
@ -376,18 +381,18 @@ void lua_settag (int tag)
{ {
checkCparams(1); checkCparams(1);
luaT_realtag(tag); luaT_realtag(tag);
switch (ttype(luaD_stack.top-1)) { switch (ttype(L->stack.top-1)) {
case LUA_T_ARRAY: case LUA_T_ARRAY:
(luaD_stack.top-1)->value.a->htag = tag; (L->stack.top-1)->value.a->htag = tag;
break; break;
case LUA_T_USERDATA: case LUA_T_USERDATA:
(luaD_stack.top-1)->value.ts->u.d.tag = tag; (L->stack.top-1)->value.ts->u.d.tag = tag;
break; break;
default: default:
luaL_verror("cannot change the tag of a %s", luaL_verror("cannot change the tag of a %s",
luaO_typenames[-ttype((luaD_stack.top-1))]); luaO_typenames[-ttype((L->stack.top-1))]);
} }
luaD_stack.top--; L->stack.top--;
} }
@ -406,10 +411,10 @@ lua_LHFunction lua_linehook = NULL;
lua_Function lua_stackedfunction (int level) lua_Function lua_stackedfunction (int level)
{ {
StkId i; StkId i;
for (i = (luaD_stack.top-1)-luaD_stack.stack; i>=0; i--) for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
if (luaD_stack.stack[i].ttype == LUA_T_MARK) if (L->stack.stack[i].ttype == LUA_T_MARK)
if (level-- == 0) if (level-- == 0)
return Ref(luaD_stack.stack+i); return Ref(L->stack.stack+i);
return LUA_NOOBJECT; return LUA_NOOBJECT;
} }
@ -417,7 +422,7 @@ lua_Function lua_stackedfunction (int level)
int lua_currentline (lua_Function func) int lua_currentline (lua_Function func)
{ {
TObject *f = Address(func); TObject *f = Address(func);
return (f+1 < luaD_stack.top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1; return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
} }
@ -447,11 +452,11 @@ int lua_setlocal (lua_Function func, int local_number)
TProtoFunc *fp = protovalue(f)->value.tf; TProtoFunc *fp = protovalue(f)->value.tf;
char *name = luaF_getlocalname(fp, local_number, lua_currentline(func)); char *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
checkCparams(1); checkCparams(1);
--luaD_stack.top; --L->stack.top;
if (name) { if (name) {
/* if "name", there must be a LUA_T_LINE */ /* if "name", there must be a LUA_T_LINE */
/* therefore, f+2 points to function base */ /* therefore, f+2 points to function base */
*((f+2)+(local_number-1)) = *luaD_stack.top; *((f+2)+(local_number-1)) = *L->stack.top;
return 1; return 1;
} }
else else
@ -478,19 +483,18 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
} }
static TObject *functofind;
static int checkfunc (TObject *o) static int checkfunc (TObject *o)
{ {
return (o->ttype == LUA_T_FUNCTION) && return (o->ttype == LUA_T_FUNCTION) &&
((functofind->ttype == LUA_T_FUNCTION) || ((L->functofind->ttype == LUA_T_FUNCTION) ||
(functofind->ttype == LUA_T_MARK)) && (L->functofind->ttype == LUA_T_MARK)) &&
(functofind->value.cl == o->value.cl); (L->functofind->value.cl == o->value.cl);
} }
char *lua_getobjname (lua_Object o, char **name) char *lua_getobjname (lua_Object o, char **name)
{ /* try to find a name for given function */ { /* try to find a name for given function */
functofind = Address(o); L->functofind = Address(o);
if ((*name = luaT_travtagmethods(checkfunc)) != NULL) if ((*name = luaT_travtagmethods(checkfunc)) != NULL)
return "tag-method"; return "tag-method";
else if ((*name = luaS_travsymbol(checkfunc)) != NULL) else if ((*name = luaS_travsymbol(checkfunc)) != NULL)
@ -504,36 +508,30 @@ char *lua_getobjname (lua_Object o, char **name)
** ======================================================= ** =======================================================
*/ */
#define MAX_C_BLOCKS 10
static int numCblocks = 0;
static struct C_Lua_Stack Cblocks[MAX_C_BLOCKS];
void lua_beginblock (void) void lua_beginblock (void)
{ {
if (numCblocks >= MAX_C_BLOCKS) if (L->numCblocks >= MAX_C_BLOCKS)
lua_error("`lua_beginblock': too many nested blocks"); lua_error("`lua_beginblock': too many nested blocks");
Cblocks[numCblocks] = luaD_Cstack; L->Cblocks[L->numCblocks] = L->Cstack;
numCblocks++; L->numCblocks++;
} }
void lua_endblock (void) void lua_endblock (void)
{ {
--numCblocks; --L->numCblocks;
luaD_Cstack = Cblocks[numCblocks]; L->Cstack = L->Cblocks[L->numCblocks];
luaD_adjusttop(luaD_Cstack.base); luaD_adjusttop(L->Cstack.base);
} }
int lua_ref (int lock) int lua_ref (int lock)
{ {
int ref; int ref;
checkCparams(1); checkCparams(1);
ref = luaC_ref(luaD_stack.top-1, lock); ref = luaC_ref(L->stack.top-1, lock);
luaD_stack.top--; L->stack.top--;
return ref; return ref;
} }
@ -546,19 +544,6 @@ lua_Object lua_getref (int ref)
} }
void lua_open (void)
{
static int firsttime = 1;
if (!firsttime) return;
firsttime = 0;
luaS_init();
luaX_init();
luaT_init();
luaD_init();
luaB_predefine();
}
#if LUA_COMPAT2_5 #if LUA_COMPAT2_5
/* /*
@ -567,11 +552,11 @@ void lua_open (void)
static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults) static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
{ {
StkId base = (luaD_stack.top-luaD_stack.stack)-nParams; StkId base = (L->stack.top-L->stack.stack)-nParams;
luaD_openstack(nParams); luaD_openstack(nParams);
luaD_stack.stack[base].ttype = LUA_T_CPROTO; L->stack.stack[base].ttype = LUA_T_CPROTO;
luaD_stack.stack[base].value.f = f; L->stack.stack[base].value.f = f;
luaF_simpleclosure(luaD_stack.stack+base); luaF_simpleclosure(L->stack.stack+base);
luaD_call(base+1, nResults); luaD_call(base+1, nResults);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbuiltin.c,v 1.6 1997/11/04 15:27:53 roberto Exp roberto $ ** $Id: lbuiltin.c,v 1.7 1997/11/07 18:19:13 roberto Exp roberto $
** Built-in functions ** Built-in functions
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -15,6 +15,7 @@
#include "lfunc.h" #include "lfunc.h"
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
#include "lstate.h"
#include "lstring.h" #include "lstring.h"
#include "ltable.h" #include "ltable.h"
#include "ltm.h" #include "ltm.h"
@ -51,7 +52,7 @@ static void nextvar (void)
TObject *o = luaA_Address(luaL_nonnullarg(1)); TObject *o = luaA_Address(luaL_nonnullarg(1));
TaggedString *g; TaggedString *g;
if (ttype(o) == LUA_T_NIL) if (ttype(o) == LUA_T_NIL)
g = (TaggedString *)luaS_root.next; g = (TaggedString *)L->rootglobal.next;
else { else {
luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected"); luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
g = tsvalue(o); g = tsvalue(o);
@ -72,21 +73,21 @@ static void foreachvar (void)
{ {
TObject f = *luaA_Address(functionarg(1)); TObject f = *luaA_Address(functionarg(1));
GCnode *g; GCnode *g;
StkId name = luaD_Cstack.base++; /* place to keep var name (to avoid GC) */ StkId name = L->Cstack.base++; /* place to keep var name (to avoid GC) */
ttype(luaD_stack.stack+name) = LUA_T_NIL; ttype(L->stack.stack+name) = LUA_T_NIL;
luaD_stack.top++; L->stack.top++;
for (g = luaS_root.next; g; g = g->next) { for (g = L->rootglobal.next; g; g = g->next) {
TaggedString *s = (TaggedString *)g; TaggedString *s = (TaggedString *)g;
if (s->u.globalval.ttype != LUA_T_NIL) { if (s->u.globalval.ttype != LUA_T_NIL) {
ttype(luaD_stack.stack+name) = LUA_T_STRING; ttype(L->stack.stack+name) = LUA_T_STRING;
tsvalue(luaD_stack.stack+name) = s; /* keep s on stack to avoid GC */ tsvalue(L->stack.stack+name) = s; /* keep s on stack to avoid GC */
luaA_pushobject(&f); luaA_pushobject(&f);
pushstring(s); pushstring(s);
luaA_pushobject(&s->u.globalval); luaA_pushobject(&s->u.globalval);
luaD_call((luaD_stack.top-luaD_stack.stack)-2, 1); luaD_call((L->stack.top-L->stack.stack)-2, 1);
if (ttype(luaD_stack.top-1) != LUA_T_NIL) if (ttype(L->stack.top-1) != LUA_T_NIL)
return; return;
luaD_stack.top--; L->stack.top--;
} }
} }
} }
@ -115,10 +116,10 @@ static void foreach (void)
luaA_pushobject(&f); luaA_pushobject(&f);
luaA_pushobject(ref(nd)); luaA_pushobject(ref(nd));
luaA_pushobject(val(nd)); luaA_pushobject(val(nd));
luaD_call((luaD_stack.top-luaD_stack.stack)-2, 1); luaD_call((L->stack.top-L->stack.stack)-2, 1);
if (ttype(luaD_stack.top-1) != LUA_T_NIL) if (ttype(L->stack.top-1) != LUA_T_NIL)
return; return;
luaD_stack.top--; L->stack.top--;
} }
} }
} }

170
ldo.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 1.7 1997/11/04 15:27:53 roberto Exp roberto $ ** $Id: ldo.c,v 1.8 1997/11/07 15:09:49 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -16,6 +16,7 @@
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
#include "lparser.h" #include "lparser.h"
#include "lstate.h"
#include "ltm.h" #include "ltm.h"
#include "lua.h" #include "lua.h"
#include "luadebug.h" #include "luadebug.h"
@ -30,14 +31,6 @@
#endif #endif
struct Stack luaD_stack;
struct C_Lua_Stack luaD_Cstack = {0, 0, 0};
static jmp_buf *errorJmp = NULL; /* current error recover point */
/* /*
** Error messages ** Error messages
@ -50,8 +43,6 @@ static void stderrorim (void)
fprintf(stderr, "lua error: %s\n", lua_getstring(s)); fprintf(stderr, "lua error: %s\n", lua_getstring(s));
} }
TObject luaD_errorim;
static void initCfunc (TObject *o, lua_CFunction f) static void initCfunc (TObject *o, lua_CFunction f)
{ {
@ -67,24 +58,25 @@ static void initCfunc (TObject *o, lua_CFunction f)
void luaD_init (void) void luaD_init (void)
{ {
luaD_stack.stack = luaM_newvector(INIT_STACK_SIZE, TObject); L->stacklimit = STACK_LIMIT;
luaD_stack.top = luaD_stack.stack; L->stack.stack = luaM_newvector(INIT_STACK_SIZE, TObject);
luaD_stack.last = luaD_stack.stack+(INIT_STACK_SIZE-1); L->stack.top = L->stack.stack;
initCfunc(&luaD_errorim, stderrorim); L->stack.last = L->stack.stack+(INIT_STACK_SIZE-1);
initCfunc(&L->errorim, stderrorim);
} }
void luaD_checkstack (int n) void luaD_checkstack (int n)
{ {
if (luaD_stack.last-luaD_stack.top <= n) { if (L->stack.last-L->stack.top <= n) {
static int limit = STACK_LIMIT; StkId top = L->stack.top-L->stack.stack;
StkId top = luaD_stack.top-luaD_stack.stack; int stacksize = (L->stack.last-L->stack.stack)+1+STACK_EXTRA+n;
int stacksize = (luaD_stack.last-luaD_stack.stack)+1+STACK_EXTRA+n; L->stack.stack = luaM_reallocvector(L->stack.stack, stacksize,TObject);
luaD_stack.stack = luaM_reallocvector(luaD_stack.stack, stacksize,TObject); L->stack.last = L->stack.stack+(stacksize-1);
luaD_stack.last = luaD_stack.stack+(stacksize-1); L->stack.top = L->stack.stack + top;
luaD_stack.top = luaD_stack.stack + top; if (stacksize >= L->stacklimit) {
if (stacksize >= limit) { /* extra space to run error handler */
limit = stacksize+STACK_EXTRA; /* extra space to run error handler */ L->stacklimit = stacksize+STACK_EXTRA;
if (lua_stackedfunction(100) == LUA_NOOBJECT) { if (lua_stackedfunction(100) == LUA_NOOBJECT) {
/* less than 100 functions on the stack: cannot be recursive loop */ /* less than 100 functions on the stack: cannot be recursive loop */
lua_error("Lua2C - C2Lua overflow"); lua_error("Lua2C - C2Lua overflow");
@ -101,80 +93,80 @@ void luaD_checkstack (int n)
*/ */
void luaD_adjusttop (StkId newtop) void luaD_adjusttop (StkId newtop)
{ {
int diff = newtop-(luaD_stack.top-luaD_stack.stack); int diff = newtop-(L->stack.top-L->stack.stack);
if (diff <= 0) if (diff <= 0)
luaD_stack.top += diff; L->stack.top += diff;
else { else {
luaD_checkstack(diff); luaD_checkstack(diff);
while (diff--) while (diff--)
ttype(luaD_stack.top++) = LUA_T_NIL; ttype(L->stack.top++) = LUA_T_NIL;
} }
} }
/* /*
** Open a hole below "nelems" from the luaD_stack.top. ** Open a hole below "nelems" from the L->stack.top.
*/ */
void luaD_openstack (int nelems) void luaD_openstack (int nelems)
{ {
int i; int i;
for (i=0; i<nelems; i++) for (i=0; i<nelems; i++)
*(luaD_stack.top-i) = *(luaD_stack.top-i-1); *(L->stack.top-i) = *(L->stack.top-i-1);
incr_top; incr_top;
} }
void luaD_lineHook (int line) void luaD_lineHook (int line)
{ {
struct C_Lua_Stack oldCLS = luaD_Cstack; struct C_Lua_Stack oldCLS = L->Cstack;
StkId old_top = luaD_Cstack.lua2C = luaD_Cstack.base = luaD_stack.top-luaD_stack.stack; StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
luaD_Cstack.num = 0; L->Cstack.num = 0;
(*lua_linehook)(line); (*lua_linehook)(line);
luaD_stack.top = luaD_stack.stack+old_top; L->stack.top = L->stack.stack+old_top;
luaD_Cstack = oldCLS; L->Cstack = oldCLS;
} }
void luaD_callHook (StkId base, lua_Type type, int isreturn) void luaD_callHook (StkId base, lua_Type type, int isreturn)
{ {
struct C_Lua_Stack oldCLS = luaD_Cstack; struct C_Lua_Stack oldCLS = L->Cstack;
StkId old_top = luaD_Cstack.lua2C = luaD_Cstack.base = luaD_stack.top-luaD_stack.stack; StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
luaD_Cstack.num = 0; L->Cstack.num = 0;
if (isreturn) if (isreturn)
(*lua_callhook)(LUA_NOOBJECT, "(return)", 0); (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
else { else {
TObject *f = luaD_stack.stack+base-1; TObject *f = L->stack.stack+base-1;
if (type == LUA_T_PROTO) if (type == LUA_T_PROTO)
(*lua_callhook)(Ref(f), tfvalue(protovalue(f))->fileName->str, (*lua_callhook)(Ref(f), tfvalue(protovalue(f))->fileName->str,
tfvalue(protovalue(f))->lineDefined); tfvalue(protovalue(f))->lineDefined);
else else
(*lua_callhook)(Ref(f), "(C)", -1); (*lua_callhook)(Ref(f), "(C)", -1);
} }
luaD_stack.top = luaD_stack.stack+old_top; L->stack.top = L->stack.stack+old_top;
luaD_Cstack = oldCLS; L->Cstack = oldCLS;
} }
/* /*
** Call a C function. luaD_Cstack.base will point to the top of the stack, ** Call a C function. L->Cstack.base will point to the top of the stack,
** and luaD_Cstack.num is the number of parameters. Returns an index ** and L->Cstack.num is the number of parameters. Returns an index
** to the first result from C. ** to the first result from C.
*/ */
static StkId callC (lua_CFunction func, StkId base) static StkId callC (lua_CFunction func, StkId base)
{ {
struct C_Lua_Stack oldCLS = luaD_Cstack; struct C_Lua_Stack oldCLS = L->Cstack;
StkId firstResult; StkId firstResult;
luaD_Cstack.num = (luaD_stack.top-luaD_stack.stack) - base; L->Cstack.num = (L->stack.top-L->stack.stack) - base;
/* incorporate parameters on the luaD_stack.stack */ /* incorporate parameters on the L->stack.stack */
luaD_Cstack.lua2C = base; L->Cstack.lua2C = base;
luaD_Cstack.base = base+luaD_Cstack.num; /* == top-stack */ L->Cstack.base = base+L->Cstack.num; /* == top-stack */
if (lua_callhook) if (lua_callhook)
luaD_callHook(base, LUA_T_CPROTO, 0); luaD_callHook(base, LUA_T_CPROTO, 0);
(*func)(); (*func)();
if (lua_callhook) /* func may have changed lua_callhook */ if (lua_callhook) /* func may have changed lua_callhook */
luaD_callHook(base, LUA_T_CPROTO, 1); luaD_callHook(base, LUA_T_CPROTO, 1);
firstResult = luaD_Cstack.base; firstResult = L->Cstack.base;
luaD_Cstack = oldCLS; L->Cstack = oldCLS;
return firstResult; return firstResult;
} }
@ -182,21 +174,21 @@ static StkId callC (lua_CFunction func, StkId base)
void luaD_callTM (TObject *f, int nParams, int nResults) void luaD_callTM (TObject *f, int nParams, int nResults)
{ {
luaD_openstack(nParams); luaD_openstack(nParams);
*(luaD_stack.top-nParams-1) = *f; *(L->stack.top-nParams-1) = *f;
luaD_call((luaD_stack.top-luaD_stack.stack)-nParams, nResults); luaD_call((L->stack.top-L->stack.stack)-nParams, nResults);
} }
/* /*
** Call a function (C or Lua). The parameters must be on the luaD_stack.stack, ** Call a function (C or Lua). The parameters must be on the L->stack.stack,
** between [luaD_stack.stack+base,luaD_stack.top). The function to be called is at luaD_stack.stack+base-1. ** between [L->stack.stack+base,L->stack.top). The function to be called is at L->stack.stack+base-1.
** When returns, the results are on the luaD_stack.stack, between [luaD_stack.stack+base-1,luaD_stack.top). ** When returns, the results are on the L->stack.stack, between [L->stack.stack+base-1,L->stack.top).
** The number of results is nResults, unless nResults=MULT_RET. ** The number of results is nResults, unless nResults=MULT_RET.
*/ */
void luaD_call (StkId base, int nResults) void luaD_call (StkId base, int nResults)
{ {
StkId firstResult; StkId firstResult;
TObject *func = luaD_stack.stack+base-1; TObject *func = L->stack.stack+base-1;
int i; int i;
if (ttype(func) == LUA_T_FUNCTION) { if (ttype(func) == LUA_T_FUNCTION) {
TObject *proto = protovalue(func); TObject *proto = protovalue(func);
@ -209,7 +201,7 @@ void luaD_call (StkId base, int nResults)
TObject *im = luaT_getimbyObj(func, IM_FUNCTION); TObject *im = luaT_getimbyObj(func, IM_FUNCTION);
if (ttype(im) == LUA_T_NIL) if (ttype(im) == LUA_T_NIL)
lua_error("call expression not a function"); lua_error("call expression not a function");
luaD_callTM(im, (luaD_stack.top-luaD_stack.stack)-(base-1), nResults); luaD_callTM(im, (L->stack.top-L->stack.stack)-(base-1), nResults);
return; return;
} }
/* adjust the number of results */ /* adjust the number of results */
@ -217,29 +209,29 @@ void luaD_call (StkId base, int nResults)
luaD_adjusttop(firstResult+nResults); luaD_adjusttop(firstResult+nResults);
/* move results to base-1 (to erase parameters and function) */ /* move results to base-1 (to erase parameters and function) */
base--; base--;
nResults = luaD_stack.top - (luaD_stack.stack+firstResult); /* actual number of results */ nResults = L->stack.top - (L->stack.stack+firstResult); /* actual number of results */
for (i=0; i<nResults; i++) for (i=0; i<nResults; i++)
*(luaD_stack.stack+base+i) = *(luaD_stack.stack+firstResult+i); *(L->stack.stack+base+i) = *(L->stack.stack+firstResult+i);
luaD_stack.top -= firstResult-base; L->stack.top -= firstResult-base;
} }
/* /*
** Traverse all objects on luaD_stack.stack ** Traverse all objects on L->stack.stack
*/ */
void luaD_travstack (int (*fn)(TObject *)) void luaD_travstack (int (*fn)(TObject *))
{ {
StkId i; StkId i;
for (i = (luaD_stack.top-1)-luaD_stack.stack; i>=0; i--) for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
fn (luaD_stack.stack+i); fn (L->stack.stack+i);
} }
static void message (char *s) static void message (char *s)
{ {
TObject im = luaD_errorim; TObject im = L->errorim;
if (ttype(&im) != LUA_T_NIL) { if (ttype(&im) != LUA_T_NIL) {
lua_pushstring(s); lua_pushstring(s);
luaD_callTM(&im, 1, 0); luaD_callTM(&im, 1, 0);
@ -252,8 +244,8 @@ static void message (char *s)
void lua_error (char *s) void lua_error (char *s)
{ {
if (s) message(s); if (s) message(s);
if (errorJmp) if (L->errorJmp)
longjmp(*errorJmp, 1); longjmp(*((jmp_buf *)L->errorJmp), 1);
else { else {
fprintf (stderr, "lua: exit(1). Unable to recover\n"); fprintf (stderr, "lua: exit(1). Unable to recover\n");
exit(1); exit(1);
@ -261,40 +253,40 @@ void lua_error (char *s)
} }
/* /*
** Call the function at luaD_Cstack.base, and incorporate results on ** Call the function at L->Cstack.base, and incorporate results on
** the Lua2C structure. ** the Lua2C structure.
*/ */
static void do_callinc (int nResults) static void do_callinc (int nResults)
{ {
StkId base = luaD_Cstack.base; StkId base = L->Cstack.base;
luaD_call(base+1, nResults); luaD_call(base+1, nResults);
luaD_Cstack.lua2C = base; /* position of the luaM_new results */ L->Cstack.lua2C = base; /* position of the luaM_new results */
luaD_Cstack.num = (luaD_stack.top-luaD_stack.stack) - base; /* number of results */ L->Cstack.num = (L->stack.top-L->stack.stack) - base; /* number of results */
luaD_Cstack.base = base + luaD_Cstack.num; /* incorporate results on luaD_stack.stack */ L->Cstack.base = base + L->Cstack.num; /* incorporate results on L->stack.stack */
} }
/* /*
** Execute a protected call. Assumes that function is at luaD_Cstack.base and ** Execute a protected call. Assumes that function is at L->Cstack.base and
** parameters are on top of it. Leave nResults on the stack. ** parameters are on top of it. Leave nResults on the stack.
*/ */
int luaD_protectedrun (int nResults) int luaD_protectedrun (int nResults)
{ {
jmp_buf myErrorJmp; jmp_buf myErrorJmp;
int status; int status;
struct C_Lua_Stack oldCLS = luaD_Cstack; struct C_Lua_Stack oldCLS = L->Cstack;
jmp_buf *oldErr = errorJmp; jmp_buf *oldErr = L->errorJmp;
errorJmp = &myErrorJmp; L->errorJmp = &myErrorJmp;
if (setjmp(myErrorJmp) == 0) { if (setjmp(myErrorJmp) == 0) {
do_callinc(nResults); do_callinc(nResults);
status = 0; status = 0;
} }
else { /* an error occurred: restore luaD_Cstack and luaD_stack.top */ else { /* an error occurred: restore L->Cstack and L->stack.top */
luaD_Cstack = oldCLS; L->Cstack = oldCLS;
luaD_stack.top = luaD_stack.stack+luaD_Cstack.base; L->stack.top = L->stack.stack+L->Cstack.base;
status = 1; status = 1;
} }
errorJmp = oldErr; L->errorJmp = oldErr;
return status; return status;
} }
@ -307,8 +299,8 @@ static int protectedparser (ZIO *z, char *chunkname, int bin)
int status; int status;
TProtoFunc *tf; TProtoFunc *tf;
jmp_buf myErrorJmp; jmp_buf myErrorJmp;
jmp_buf *oldErr = errorJmp; jmp_buf *oldErr = L->errorJmp;
errorJmp = &myErrorJmp; L->errorJmp = &myErrorJmp;
if (setjmp(myErrorJmp) == 0) { if (setjmp(myErrorJmp) == 0) {
tf = bin ? luaU_undump1(z, chunkname) : luaY_parser(z, chunkname); tf = bin ? luaU_undump1(z, chunkname) : luaY_parser(z, chunkname);
status = 0; status = 0;
@ -317,12 +309,12 @@ static int protectedparser (ZIO *z, char *chunkname, int bin)
tf = NULL; tf = NULL;
status = 1; status = 1;
} }
errorJmp = oldErr; L->errorJmp = oldErr;
if (status) return 1; /* error code */ if (status) return 1; /* error code */
if (tf == NULL) return 2; /* 'natural' end */ if (tf == NULL) return 2; /* 'natural' end */
luaD_adjusttop(luaD_Cstack.base+1); /* one slot for the pseudo-function */ luaD_adjusttop(L->Cstack.base+1); /* one slot for the pseudo-function */
luaD_stack.stack[luaD_Cstack.base].ttype = LUA_T_PROTO; L->stack.stack[L->Cstack.base].ttype = LUA_T_PROTO;
luaD_stack.stack[luaD_Cstack.base].value.tf = tf; L->stack.stack[L->Cstack.base].value.tf = tf;
luaV_closure(0); luaV_closure(0);
return 0; return 0;
} }
@ -332,15 +324,15 @@ static int do_main (ZIO *z, char *chunkname, int bin)
{ {
int status; int status;
do { do {
long old_blocks = (luaC_checkGC(), luaO_nblocks); long old_blocks = (luaC_checkGC(), L->nblocks);
status = protectedparser(z, chunkname, bin); status = protectedparser(z, chunkname, bin);
if (status == 1) return 1; /* error */ if (status == 1) return 1; /* error */
else if (status == 2) return 0; /* 'natural' end */ else if (status == 2) return 0; /* 'natural' end */
else { else {
unsigned long newelems2 = 2*(luaO_nblocks-old_blocks); unsigned long newelems2 = 2*(L->nblocks-old_blocks);
luaC_threshold += newelems2; L->GCthreshold += newelems2;
status = luaD_protectedrun(MULT_RET); status = luaD_protectedrun(MULT_RET);
luaC_threshold -= newelems2; L->GCthreshold -= newelems2;
} }
} while (bin && status == 0); } while (bin && status == 0);
return status; return status;
@ -351,7 +343,7 @@ void luaD_gcIM (TObject *o)
{ {
TObject *im = luaT_getimbyObj(o, IM_GC); TObject *im = luaT_getimbyObj(o, IM_GC);
if (ttype(im) != LUA_T_NIL) { if (ttype(im) != LUA_T_NIL) {
*luaD_stack.top = *o; *L->stack.top = *o;
incr_top; incr_top;
luaD_callTM(im, 1, 0); luaD_callTM(im, 1, 0);
} }

32
ldo.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ ** $Id: ldo.h,v 1.2 1997/11/04 15:27:53 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -9,43 +9,25 @@
#include "lobject.h" #include "lobject.h"
#include "lstate.h"
typedef int StkId; /* index to luaD_stack.stack elements */
#define MULT_RET 255 #define MULT_RET 255
extern struct Stack {
TObject *last;
TObject *stack;
TObject *top;
} luaD_stack;
extern struct C_Lua_Stack {
StkId base; /* when Lua calls C or C calls Lua, points to */
/* the first slot after the last parameter. */
StkId lua2C; /* points to first element of "array" lua2C */
int num; /* size of "array" lua2C */
} luaD_Cstack;
extern TObject luaD_errorim;
/* /*
** macro to increment stack top. ** macro to increment stack top.
** There must be always an empty slot at the luaD_stack.top ** There must be always an empty slot at the L->stack.top
*/ */
#define incr_top { if (luaD_stack.top >= luaD_stack.last) luaD_checkstack(1); \ #define incr_top { if (L->stack.top >= L->stack.last) luaD_checkstack(1); \
luaD_stack.top++; } L->stack.top++; }
/* macros to convert from lua_Object to (TObject *) and back */ /* macros to convert from lua_Object to (TObject *) and back */
#define Address(lo) ((lo)+luaD_stack.stack-1) #define Address(lo) ((lo)+L->stack.stack-1)
#define Ref(st) ((st)-luaD_stack.stack+1) #define Ref(st) ((st)-L->stack.stack+1)
void luaD_init (void); void luaD_init (void);

18
lfunc.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lfunc.c,v 1.4 1997/10/23 16:26:37 roberto Exp roberto $ ** $Id: lfunc.c,v 1.5 1997/10/24 17:17:24 roberto Exp roberto $
** Lua Funcion auxiliar ** Lua Funcion auxiliar
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -9,20 +9,18 @@
#include "lfunc.h" #include "lfunc.h"
#include "lmem.h" #include "lmem.h"
#include "lstate.h"
#define gcsizeproto(p) 5 #define gcsizeproto(p) 5
#define gcsizeclosure(c) 1 #define gcsizeclosure(c) 1
GCnode luaF_root = {NULL, 0};
GCnode luaF_rootcl = {NULL, 0};
Closure *luaF_newclosure (int nelems) Closure *luaF_newclosure (int nelems)
{ {
Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject)); Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject));
luaO_insertlist(&luaF_rootcl, (GCnode *)c); luaO_insertlist(&(L->rootcl), (GCnode *)c);
luaO_nblocks += gcsizeclosure(c); L->nblocks += gcsizeclosure(c);
c->nelems = nelems; c->nelems = nelems;
return c; return c;
} }
@ -46,8 +44,8 @@ TProtoFunc *luaF_newproto (void)
f->consts = NULL; f->consts = NULL;
f->nconsts = 0; f->nconsts = 0;
f->locvars = NULL; f->locvars = NULL;
luaO_insertlist(&luaF_root, (GCnode *)f); luaO_insertlist(&(L->rootproto), (GCnode *)f);
luaO_nblocks += gcsizeproto(f); L->nblocks += gcsizeproto(f);
return f; return f;
} }
@ -66,7 +64,7 @@ void luaF_freeproto (TProtoFunc *l)
{ {
while (l) { while (l) {
TProtoFunc *next = (TProtoFunc *)l->head.next; TProtoFunc *next = (TProtoFunc *)l->head.next;
luaO_nblocks -= gcsizeproto(l); L->nblocks -= gcsizeproto(l);
freefunc(l); freefunc(l);
l = next; l = next;
} }
@ -77,7 +75,7 @@ void luaF_freeclosure (Closure *l)
{ {
while (l) { while (l) {
Closure *next = (Closure *)l->head.next; Closure *next = (Closure *)l->head.next;
luaO_nblocks -= gcsizeclosure(l); L->nblocks -= gcsizeclosure(l);
luaM_free(l); luaM_free(l);
l = next; l = next;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lfunc.h,v 1.2 1997/09/26 16:46:20 roberto Exp roberto $ ** $Id: lfunc.h,v 1.3 1997/10/24 17:17:24 roberto Exp roberto $
** Lua Function structures ** Lua Function structures
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -11,9 +11,6 @@
#include "lobject.h" #include "lobject.h"
extern GCnode luaF_root;
extern GCnode luaF_rootcl;
TProtoFunc *luaF_newproto (void); TProtoFunc *luaF_newproto (void);
Closure *luaF_newclosure (int nelems); Closure *luaF_newclosure (int nelems);

74
lgc.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 1.6 1997/10/24 17:17:24 roberto Exp roberto $ ** $Id: lgc.c,v 1.7 1997/11/03 20:45:23 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -10,6 +10,7 @@
#include "lgc.h" #include "lgc.h"
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
#include "lstate.h"
#include "lstring.h" #include "lstring.h"
#include "ltable.h" #include "ltable.h"
#include "ltm.h" #include "ltm.h"
@ -27,12 +28,6 @@ static int markobject (TObject *o);
** ======================================================= ** =======================================================
*/ */
static struct ref {
TObject o;
enum {LOCK, HOLD, FREE, COLLECTED} status;
} *refArray = NULL;
static int refSize = 0;
int luaC_ref (TObject *o, int lock) int luaC_ref (TObject *o, int lock)
{ {
@ -40,18 +35,19 @@ int luaC_ref (TObject *o, int lock)
if (ttype(o) == LUA_T_NIL) if (ttype(o) == LUA_T_NIL)
ref = -1; /* special ref for nil */ ref = -1; /* special ref for nil */
else { else {
for (ref=0; ref<refSize; ref++) for (ref=0; ref<L->refSize; ref++)
if (refArray[ref].status == FREE) if (L->refArray[ref].status == FREE)
goto found; goto found;
/* no more empty spaces */ { /* no more empty spaces */ {
int oldSize = refSize; int oldSize = L->refSize;
refSize = luaM_growvector(&refArray, refSize, struct ref, refEM, MAX_WORD); L->refSize = luaM_growvector(&L->refArray, L->refSize, struct ref,
for (ref=oldSize; ref<refSize; ref++) refEM, MAX_WORD);
refArray[ref].status = FREE; for (ref=oldSize; ref<L->refSize; ref++)
L->refArray[ref].status = FREE;
ref = oldSize; ref = oldSize;
} found: } found:
refArray[ref].o = *o; L->refArray[ref].o = *o;
refArray[ref].status = lock ? LOCK : HOLD; L->refArray[ref].status = lock ? LOCK : HOLD;
} }
return ref; return ref;
} }
@ -59,8 +55,8 @@ int luaC_ref (TObject *o, int lock)
void lua_unref (int ref) void lua_unref (int ref)
{ {
if (ref >= 0 && ref < refSize) if (ref >= 0 && ref < L->refSize)
refArray[ref].status = FREE; L->refArray[ref].status = FREE;
} }
@ -68,9 +64,9 @@ TObject* luaC_getref (int ref)
{ {
if (ref == -1) if (ref == -1)
return &luaO_nilobject; return &luaO_nilobject;
if (ref >= 0 && ref < refSize && if (ref >= 0 && ref < L->refSize &&
(refArray[ref].status == LOCK || refArray[ref].status == HOLD)) (L->refArray[ref].status == LOCK || L->refArray[ref].status == HOLD))
return &refArray[ref].o; return &L->refArray[ref].o;
else else
return NULL; return NULL;
} }
@ -79,9 +75,9 @@ TObject* luaC_getref (int ref)
static void travlock (void) static void travlock (void)
{ {
int i; int i;
for (i=0; i<refSize; i++) for (i=0; i<L->refSize; i++)
if (refArray[i].status == LOCK) if (L->refArray[i].status == LOCK)
markobject(&refArray[i].o); markobject(&L->refArray[i].o);
} }
@ -105,9 +101,9 @@ static int ismarked (TObject *o)
static void invalidaterefs (void) static void invalidaterefs (void)
{ {
int i; int i;
for (i=0; i<refSize; i++) for (i=0; i<L->refSize; i++)
if (refArray[i].status == HOLD && !ismarked(&refArray[i].o)) if (L->refArray[i].status == HOLD && !ismarked(&L->refArray[i].o))
refArray[i].status = COLLECTED; L->refArray[i].status = COLLECTED;
} }
@ -210,7 +206,7 @@ static void hashmark (Hash *h)
static void globalmark (void) static void globalmark (void)
{ {
TaggedString *g; TaggedString *g;
for (g=(TaggedString *)luaS_root.next; g; g=(TaggedString *)g->head.next) for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
if (g->u.globalval.ttype != LUA_T_NIL) { if (g->u.globalval.ttype != LUA_T_NIL) {
markobject(&g->u.globalval); markobject(&g->u.globalval);
strmark(g); /* cannot collect non nil global variables */ strmark(g); /* cannot collect non nil global variables */
@ -240,23 +236,19 @@ static int markobject (TObject *o)
#define GARBAGE_BLOCK 150
unsigned long luaC_threshold = GARBAGE_BLOCK;
static void markall (void) static void markall (void)
{ {
luaD_travstack(markobject); /* mark stack objects */ luaD_travstack(markobject); /* mark stack objects */
globalmark(); /* mark global variable values and names */ globalmark(); /* mark global variable values and names */
travlock(); /* mark locked objects */ travlock(); /* mark locked objects */
markobject(&L->globalbag); /* mark elements in global bag */
luaT_travtagmethods(markobject); /* mark fallbacks */ luaT_travtagmethods(markobject); /* mark fallbacks */
} }
long lua_collectgarbage (long limit) long lua_collectgarbage (long limit)
{ {
unsigned long recovered = luaO_nblocks; /* to subtract nblocks after gc */ unsigned long recovered = L->nblocks; /* to subtract nblocks after gc */
Hash *freetable; Hash *freetable;
TaggedString *freestr; TaggedString *freestr;
TProtoFunc *freefunc; TProtoFunc *freefunc;
@ -264,10 +256,10 @@ long lua_collectgarbage (long limit)
markall(); markall();
invalidaterefs(); invalidaterefs();
freestr = luaS_collector(); freestr = luaS_collector();
freetable = (Hash *)listcollect(&luaH_root); freetable = (Hash *)listcollect(&(L->roottable));
freefunc = (TProtoFunc *)listcollect(&luaF_root); freefunc = (TProtoFunc *)listcollect(&(L->rootproto));
freeclos = (Closure *)listcollect(&luaF_rootcl); freeclos = (Closure *)listcollect(&(L->rootcl));
luaC_threshold *= 4; /* to avoid GC during GC */ L->GCthreshold *= 4; /* to avoid GC during GC */
hashcallIM(freetable); /* GC tag methods for tables */ hashcallIM(freetable); /* GC tag methods for tables */
strcallIM(freestr); /* GC tag methods for userdata */ strcallIM(freestr); /* GC tag methods for userdata */
luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */ luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
@ -276,16 +268,16 @@ long lua_collectgarbage (long limit)
luaF_freeproto(freefunc); luaF_freeproto(freefunc);
luaF_freeclosure(freeclos); luaF_freeclosure(freeclos);
luaM_clearbuffer(); luaM_clearbuffer();
recovered = recovered-luaO_nblocks; recovered = recovered-L->nblocks;
/*printf("==total %ld coletados %ld\n", luaO_nblocks+recovered, recovered);*/ /*printf("==total %ld coletados %ld\n", L->nblocks+recovered, recovered);*/
luaC_threshold = (limit == 0) ? 2*luaO_nblocks : luaO_nblocks+limit; L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
return recovered; return recovered;
} }
void luaC_checkGC (void) void luaC_checkGC (void)
{ {
if (luaO_nblocks >= luaC_threshold) if (L->nblocks >= L->GCthreshold)
lua_collectgarbage(0); lua_collectgarbage(0);
} }

4
lgc.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ ** $Id: lgc.h,v 1.2 1997/10/23 16:26:37 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -11,8 +11,6 @@
#include "lobject.h" #include "lobject.h"
extern unsigned long luaC_threshold;
void luaC_checkGC (void); void luaC_checkGC (void);
TObject* luaC_getref (int ref); TObject* luaC_getref (int ref);
int luaC_ref (TObject *o, int lock); int luaC_ref (TObject *o, int lock);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 1.3 1997/10/30 20:29:09 roberto Exp roberto $ ** $Id: liolib.c,v 1.4 1997/11/04 15:27:53 roberto Exp roberto $
** Standard I/O (and system) library ** Standard I/O (and system) library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -41,8 +41,21 @@ int pclose();
#endif #endif
int lua_tagio; static void createtag (char *t)
static int closedtag; {
lua_pushobject(lua_globalbag());
lua_pushstring(t);
lua_pushnumber(lua_newtag());
lua_settable();
}
static int gettag (char *t)
{
lua_pushobject(lua_globalbag());
lua_pushstring(t);
return lua_getnumber(lua_gettable());
}
static void pushresult (int i) static void pushresult (int i)
@ -59,9 +72,9 @@ static void pushresult (int i)
static int ishandler (lua_Object f) static int ishandler (lua_Object f)
{ {
if (lua_isuserdata(f)) { if (lua_isuserdata(f)) {
if (lua_tag(f) == closedtag) if (lua_tag(f) == gettag("closedtag"))
lua_error("trying to access a closed file"); lua_error("trying to access a closed file");
return lua_tag(f) == lua_tagio; return lua_tag(f) == gettag("tagio");
} }
else return 0; else return 0;
} }
@ -94,13 +107,13 @@ static void closefile (char *name)
if (pclose(f) == -1) if (pclose(f) == -1)
fclose(f); fclose(f);
lua_pushobject(lua_getglobal(name)); lua_pushobject(lua_getglobal(name));
lua_settag(closedtag); lua_settag(gettag("closedtag"));
} }
static void setfile (FILE *f, char *name) static void setfile (FILE *f, char *name)
{ {
lua_pushusertag(f, lua_tagio); lua_pushusertag(f, gettag("tagio"));
lua_setglobal(name); lua_setglobal(name);
} }
@ -108,7 +121,7 @@ static void setfile (FILE *f, char *name)
static void setreturn (FILE *f, char *name) static void setreturn (FILE *f, char *name)
{ {
setfile(f, name); setfile(f, name);
lua_pushusertag(f, lua_tagio); lua_pushusertag(f, gettag("tagio"));
} }
@ -120,7 +133,7 @@ static void io_readfrom (void)
closefile("_INPUT"); closefile("_INPUT");
current = stdin; current = stdin;
} }
else if (lua_tag(f) == lua_tagio) else if (lua_tag(f) == gettag("tagio"))
current = lua_getuserdata(f); current = lua_getuserdata(f);
else { else {
char *s = luaL_check_string(1); char *s = luaL_check_string(1);
@ -142,7 +155,7 @@ static void io_writeto (void)
closefile("_OUTPUT"); closefile("_OUTPUT");
current = stdout; current = stdout;
} }
else if (lua_tag(f) == lua_tagio) else if (lua_tag(f) == gettag("tagio"))
current = lua_getuserdata(f); current = lua_getuserdata(f);
else { else {
char *s = luaL_check_string(1); char *s = luaL_check_string(1);
@ -373,8 +386,8 @@ static struct luaL_reg iolib[] = {
void lua_iolibopen (void) void lua_iolibopen (void)
{ {
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
lua_tagio = lua_newtag(); createtag("iotag");
closedtag = lua_newtag(); createtag("closedtag");
setfile(stdin, "_INPUT"); setfile(stdin, "_INPUT");
setfile(stdout, "_OUTPUT"); setfile(stdout, "_OUTPUT");
setfile(stdin, "_STDIN"); setfile(stdin, "_STDIN");

372
llex.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.c,v 1.4 1997/11/04 15:27:53 roberto Exp roberto $ ** $Id: llex.c,v 1.5 1997/11/07 15:09:49 roberto Exp roberto $
** Lexical Analizer ** Lexical Analizer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -12,35 +12,32 @@
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
#include "lparser.h" #include "lparser.h"
#include "lstate.h"
#include "lstring.h" #include "lstring.h"
#include "lstx.h" #include "lstx.h"
#include "luadebug.h" #include "luadebug.h"
#include "lzio.h" #include "lzio.h"
static int current; /* look ahead character */
static ZIO *lex_z;
int luaX_linenumber;
int lua_debug=0; int lua_debug=0;
#define next() (current = zgetc(lex_z)) #define next(LL) (LL->current = zgetc(LL->lex_z))
static struct {
char *name;
int token;
} reserved [] = {
{"and", AND}, {"do", DO}, {"else", ELSE}, {"elseif", ELSEIF},
{"end", END}, {"function", FUNCTION}, {"if", IF}, {"local", LOCAL},
{"nil", NIL}, {"not", NOT}, {"or", OR}, {"repeat", REPEAT},
{"return", RETURN}, {"then", THEN}, {"until", UNTIL}, {"while", WHILE}
};
void luaX_init (void) void luaX_init (void)
{ {
static struct {
char *name;
int token;
} reserved [] = {
{"and", AND}, {"do", DO}, {"else", ELSE}, {"elseif", ELSEIF},
{"end", END}, {"function", FUNCTION}, {"if", IF}, {"local", LOCAL},
{"nil", NIL}, {"not", NOT}, {"or", OR}, {"repeat", REPEAT},
{"return", RETURN}, {"then", THEN}, {"until", UNTIL}, {"while", WHILE}
};
int i; int i;
for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) { for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
TaggedString *ts = luaS_new(reserved[i].name); TaggedString *ts = luaS_new(reserved[i].name);
@ -49,47 +46,29 @@ void luaX_init (void)
} }
static void firstline (LexState *LL)
#define MAX_IFS 5
/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
static struct {
int elsepart; /* true if its in the $else part */
int condition; /* true if $if condition is true */
int skip; /* true if part must be skiped */
} ifstate[MAX_IFS];
static int iflevel; /* level of nested $if's */
static struct textbuff {
char *text;
int tokensize;
int buffsize;
} textbuff;
static void firstline (void)
{ {
int c = zgetc(lex_z); int c = zgetc(LL->lex_z);
if (c == '#') if (c == '#')
while((c=zgetc(lex_z)) != '\n' && c != EOZ) /* skip first line */; while((c=zgetc(LL->lex_z)) != '\n' && c != EOZ) /* skip first line */;
zungetc(lex_z); zungetc(LL->lex_z);
} }
void luaX_setinput (ZIO *z) void luaX_setinput (ZIO *z)
{ {
current = '\n'; LexState *LL = L->lexstate;
luaX_linenumber = 0; LL->current = '\n';
iflevel = 0; LL->linelasttoken = 0;
ifstate[0].skip = 0; LL->lastline = 0;
ifstate[0].elsepart = 1; /* to avoid a free $else */ LL->linenumber = 0;
lex_z = z; LL->iflevel = 0;
firstline(); LL->ifstate[0].skip = 0;
textbuff.buffsize = 20; LL->ifstate[0].elsepart = 1; /* to avoid a free $else */
textbuff.text = luaM_buffer(textbuff.buffsize); LL->lex_z = z;
firstline(LL);
LL->textbuff.buffsize = 20;
LL->textbuff.text = luaM_buffer(LL->textbuff.buffsize);
} }
@ -102,9 +81,9 @@ void luaX_setinput (ZIO *z)
#define PRAGMASIZE 20 #define PRAGMASIZE 20
static void skipspace (void) static void skipspace (LexState *LL)
{ {
while (current == ' ' || current == '\t') next(); while (LL->current == ' ' || LL->current == '\t') next(LL);
} }
@ -122,49 +101,49 @@ static int checkcond (char *buff)
} }
static void readname (char *buff) static void readname (LexState *LL, char *buff)
{ {
int i = 0; int i = 0;
skipspace(); skipspace(LL);
while (isalnum(current) || current == '_') { while (isalnum(LL->current) || LL->current == '_') {
if (i >= PRAGMASIZE) { if (i >= PRAGMASIZE) {
buff[PRAGMASIZE] = 0; buff[PRAGMASIZE] = 0;
luaY_syntaxerror("pragma too long", buff); luaY_syntaxerror("pragma too long", buff);
} }
buff[i++] = current; buff[i++] = LL->current;
next(); next(LL);
} }
buff[i] = 0; buff[i] = 0;
} }
static void inclinenumber (void); static void inclinenumber (LexState *LL);
static void ifskip (void) static void ifskip (LexState *LL)
{ {
while (ifstate[iflevel].skip) { while (LL->ifstate[LL->iflevel].skip) {
if (current == '\n') if (LL->current == '\n')
inclinenumber(); inclinenumber(LL);
else if (current == EOZ) else if (LL->current == EOZ)
luaY_syntaxerror("input ends inside a $if", ""); luaY_syntaxerror("input ends inside a $if", "");
else next(); else next(LL);
} }
} }
static void inclinenumber (void) static void inclinenumber (LexState *LL)
{ {
static char *pragmas [] = static char *pragmas [] =
{"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL}; {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
next(); /* skip '\n' */ next(LL); /* skip '\n' */
++luaX_linenumber; ++LL->linenumber;
if (current == '$') { /* is a pragma? */ if (LL->current == '$') { /* is a pragma? */
char buff[PRAGMASIZE+1]; char buff[PRAGMASIZE+1];
int ifnot = 0; int ifnot = 0;
int skip = ifstate[iflevel].skip; int skip = LL->ifstate[LL->iflevel].skip;
next(); /* skip $ */ next(LL); /* skip $ */
readname(buff); readname(LL, buff);
switch (luaO_findstring(buff, pragmas)) { switch (luaO_findstring(buff, pragmas)) {
case 0: /* debug */ case 0: /* debug */
if (!skip) lua_debug = 1; if (!skip) lua_debug = 1;
@ -174,42 +153,42 @@ static void inclinenumber (void)
break; break;
case 2: /* endinput */ case 2: /* endinput */
if (!skip) { if (!skip) {
current = EOZ; LL->current = EOZ;
iflevel = 0; /* to allow $endinput inside a $if */ LL->iflevel = 0; /* to allow $endinput inside a $if */
} }
break; break;
case 3: /* end */ case 3: /* end */
if (iflevel-- == 0) if (LL->iflevel-- == 0)
luaY_syntaxerror("unmatched $end", "$end"); luaY_syntaxerror("unmatched $end", "$end");
break; break;
case 4: /* ifnot */ case 4: /* ifnot */
ifnot = 1; ifnot = 1;
/* go through */ /* go through */
case 5: /* if */ case 5: /* if */
if (iflevel == MAX_IFS-1) if (LL->iflevel == MAX_IFS-1)
luaY_syntaxerror("too many nested `$ifs'", "$if"); luaY_syntaxerror("too many nested `$ifs'", "$if");
readname(buff); readname(LL, buff);
iflevel++; LL->iflevel++;
ifstate[iflevel].elsepart = 0; LL->ifstate[LL->iflevel].elsepart = 0;
ifstate[iflevel].condition = checkcond(buff) ? !ifnot : ifnot; LL->ifstate[LL->iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
ifstate[iflevel].skip = skip || !ifstate[iflevel].condition; LL->ifstate[LL->iflevel].skip = skip || !LL->ifstate[LL->iflevel].condition;
break; break;
case 6: /* else */ case 6: /* else */
if (ifstate[iflevel].elsepart) if (LL->ifstate[LL->iflevel].elsepart)
luaY_syntaxerror("unmatched $else", "$else"); luaY_syntaxerror("unmatched $else", "$else");
ifstate[iflevel].elsepart = 1; LL->ifstate[LL->iflevel].elsepart = 1;
ifstate[iflevel].skip = LL->ifstate[LL->iflevel].skip = LL->ifstate[LL->iflevel-1].skip ||
ifstate[iflevel-1].skip || ifstate[iflevel].condition; LL->ifstate[LL->iflevel].condition;
break; break;
default: default:
luaY_syntaxerror("invalid pragma", buff); luaY_syntaxerror("invalid pragma", buff);
} }
skipspace(); skipspace(LL);
if (current == '\n') /* pragma must end with a '\n' ... */ if (LL->current == '\n') /* pragma must end with a '\n' ... */
inclinenumber(); inclinenumber(LL);
else if (current != EOZ) /* or eof */ else if (LL->current != EOZ) /* or eof */
luaY_syntaxerror("invalid pragma format", buff); luaY_syntaxerror("invalid pragma format", buff);
ifskip(); ifskip(LL);
} }
} }
@ -222,162 +201,167 @@ static void inclinenumber (void)
static void save (int c) static void save (LexState *LL, int c)
{ {
if (textbuff.tokensize >= textbuff.buffsize) if (LL->textbuff.tokensize >= LL->textbuff.buffsize)
textbuff.text = luaM_buffer(textbuff.buffsize *= 2); LL->textbuff.text = luaM_buffer(LL->textbuff.buffsize *= 2);
textbuff.text[textbuff.tokensize++] = c; LL->textbuff.text[LL->textbuff.tokensize++] = c;
} }
char *luaX_lasttoken (void) char *luaX_lasttoken (void)
{ {
save(0); save(L->lexstate, 0);
return textbuff.text; return L->lexstate->textbuff.text;
} }
#define save_and_next() (save(current), next()) #define save_and_next(LL) (save(LL, LL->current), next(LL))
static int read_long_string (void) static int read_long_string (LexState *LL, YYSTYPE *l)
{ {
int cont = 0; int cont = 0;
while (1) { while (1) {
switch (current) { switch (LL->current) {
case EOZ: case EOZ:
save(0); save(LL, 0);
return WRONGTOKEN; return WRONGTOKEN;
case '[': case '[':
save_and_next(); save_and_next(LL);
if (current == '[') { if (LL->current == '[') {
cont++; cont++;
save_and_next(); save_and_next(LL);
} }
continue; continue;
case ']': case ']':
save_and_next(); save_and_next(LL);
if (current == ']') { if (LL->current == ']') {
if (cont == 0) goto endloop; if (cont == 0) goto endloop;
cont--; cont--;
save_and_next(); save_and_next(LL);
} }
continue; continue;
case '\n': case '\n':
save('\n'); save(LL, '\n');
inclinenumber(); inclinenumber(LL);
continue; continue;
default: default:
save_and_next(); save_and_next(LL);
} }
} endloop: } endloop:
save_and_next(); /* pass the second ']' */ save_and_next(LL); /* pass the second ']' */
textbuff.text[textbuff.tokensize-2] = 0; /* erases ']]' */ LL->textbuff.text[LL->textbuff.tokensize-2] = 0; /* erases ']]' */
luaY_lval.pTStr = luaS_new(textbuff.text+2); l->pTStr = luaS_new(LL->textbuff.text+2);
textbuff.text[textbuff.tokensize-2] = ']'; /* restores ']]' */ LL->textbuff.text[LL->textbuff.tokensize-2] = ']'; /* restores ']]' */
return STRING; return STRING;
} }
int luaY_lex (void) /* to avoid warnings; this declaration cannot be public since YYSTYPE
** cannot be visible in llex.h (otherwise there is an error, since
** the parser body redefines it!)
*/
int luaY_lex (YYSTYPE *l);
int luaY_lex (YYSTYPE *l)
{ {
static int linelasttoken = 0; LexState *LL = L->lexstate;
double a; double a;
textbuff.tokensize = 0; LL->textbuff.tokensize = 0;
if (lua_debug) if (lua_debug)
luaY_codedebugline(linelasttoken); luaY_codedebugline(LL->linelasttoken);
linelasttoken = luaX_linenumber; LL->linelasttoken = LL->linenumber;
while (1) { while (1) {
switch (current) { switch (LL->current) {
case '\n': case '\n':
inclinenumber(); inclinenumber(LL);
linelasttoken = luaX_linenumber; LL->linelasttoken = LL->linenumber;
continue; continue;
case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */ case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
next(); next(LL);
continue; continue;
case '-': case '-':
save_and_next(); save_and_next(LL);
if (current != '-') return '-'; if (LL->current != '-') return '-';
do { next(); } while (current != '\n' && current != EOZ); do { next(LL); } while (LL->current != '\n' && LL->current != EOZ);
textbuff.tokensize = 0; LL->textbuff.tokensize = 0;
continue; continue;
case '[': case '[':
save_and_next(); save_and_next(LL);
if (current != '[') return '['; if (LL->current != '[') return '[';
else { else {
save_and_next(); /* pass the second '[' */ save_and_next(LL); /* pass the second '[' */
return read_long_string(); return read_long_string(LL, l);
} }
case '=': case '=':
save_and_next(); save_and_next(LL);
if (current != '=') return '='; if (LL->current != '=') return '=';
else { save_and_next(); return EQ; } else { save_and_next(LL); return EQ; }
case '<': case '<':
save_and_next(); save_and_next(LL);
if (current != '=') return '<'; if (LL->current != '=') return '<';
else { save_and_next(); return LE; } else { save_and_next(LL); return LE; }
case '>': case '>':
save_and_next(); save_and_next(LL);
if (current != '=') return '>'; if (LL->current != '=') return '>';
else { save_and_next(); return GE; } else { save_and_next(LL); return GE; }
case '~': case '~':
save_and_next(); save_and_next(LL);
if (current != '=') return '~'; if (LL->current != '=') return '~';
else { save_and_next(); return NE; } else { save_and_next(LL); return NE; }
case '"': case '"':
case '\'': { case '\'': {
int del = current; int del = LL->current;
save_and_next(); save_and_next(LL);
while (current != del) { while (LL->current != del) {
switch (current) { switch (LL->current) {
case EOZ: case EOZ:
case '\n': case '\n':
save(0); save(LL, 0);
return WRONGTOKEN; return WRONGTOKEN;
case '\\': case '\\':
next(); /* do not save the '\' */ next(LL); /* do not save the '\' */
switch (current) { switch (LL->current) {
case 'n': save('\n'); next(); break; case 'n': save(LL, '\n'); next(LL); break;
case 't': save('\t'); next(); break; case 't': save(LL, '\t'); next(LL); break;
case 'r': save('\r'); next(); break; case 'r': save(LL, '\r'); next(LL); break;
case '\n': save('\n'); inclinenumber(); break; case '\n': save(LL, '\n'); inclinenumber(LL); break;
default : save_and_next(); break; default : save_and_next(LL); break;
} }
break; break;
default: default:
save_and_next(); save_and_next(LL);
} }
} }
next(); /* skip delimiter */ next(LL); /* skip delimiter */
save(0); save(LL, 0);
luaY_lval.pTStr = luaS_new(textbuff.text+1); l->pTStr = luaS_new(LL->textbuff.text+1);
textbuff.text[textbuff.tokensize-1] = del; /* restore delimiter */ LL->textbuff.text[LL->textbuff.tokensize-1] = del; /* restore delimiter */
return STRING; return STRING;
} }
case '.': case '.':
save_and_next(); save_and_next(LL);
if (current == '.') if (LL->current == '.')
{ {
save_and_next(); save_and_next(LL);
if (current == '.') if (LL->current == '.')
{ {
save_and_next(); save_and_next(LL);
return DOTS; /* ... */ return DOTS; /* ... */
} }
else return CONC; /* .. */ else return CONC; /* .. */
} }
else if (!isdigit(current)) return '.'; else if (!isdigit(LL->current)) return '.';
/* current is a digit: goes through to number */ /* LL->current is a digit: goes through to number */
a=0.0; a=0.0;
goto fraction; goto fraction;
@ -385,69 +369,69 @@ int luaY_lex (void)
case '5': case '6': case '7': case '8': case '9': case '5': case '6': case '7': case '8': case '9':
a=0.0; a=0.0;
do { do {
a=10.0*a+(current-'0'); a=10.0*a+(LL->current-'0');
save_and_next(); save_and_next(LL);
} while (isdigit(current)); } while (isdigit(LL->current));
if (current == '.') { if (LL->current == '.') {
save_and_next(); save_and_next(LL);
if (current == '.') { if (LL->current == '.') {
save(0); save(LL, 0);
luaY_error( luaY_error(
"ambiguous syntax (decimal point x string concatenation)"); "ambiguous syntax (decimal point x string concatenation)");
} }
} }
fraction: fraction:
{ double da=0.1; { double da=0.1;
while (isdigit(current)) while (isdigit(LL->current))
{ {
a+=(current-'0')*da; a+=(LL->current-'0')*da;
da/=10.0; da/=10.0;
save_and_next(); save_and_next(LL);
} }
if (toupper(current) == 'E') { if (toupper(LL->current) == 'E') {
int e=0; int e=0;
int neg; int neg;
double ea; double ea;
save_and_next(); save_and_next(LL);
neg=(current=='-'); neg=(LL->current=='-');
if (current == '+' || current == '-') save_and_next(); if (LL->current == '+' || LL->current == '-') save_and_next(LL);
if (!isdigit(current)) { if (!isdigit(LL->current)) {
save(0); return WRONGTOKEN; } save(LL, 0); return WRONGTOKEN; }
do { do {
e=10.0*e+(current-'0'); e=10.0*e+(LL->current-'0');
save_and_next(); save_and_next(LL);
} while (isdigit(current)); } while (isdigit(LL->current));
for (ea=neg?0.1:10.0; e>0; e>>=1) for (ea=neg?0.1:10.0; e>0; e>>=1)
{ {
if (e & 1) a*=ea; if (e & 1) a*=ea;
ea*=ea; ea*=ea;
} }
} }
luaY_lval.vReal = a; l->vReal = a;
return NUMBER; return NUMBER;
} }
case EOZ: case EOZ:
save(0); save(LL, 0);
if (iflevel > 0) if (LL->iflevel > 0)
luaY_error("missing $endif"); luaY_error("missing $endif");
return 0; return 0;
default: default:
if (current != '_' && !isalpha(current)) { if (LL->current != '_' && !isalpha(LL->current)) {
save_and_next(); save_and_next(LL);
return textbuff.text[0]; return LL->textbuff.text[0];
} }
else { /* identifier or reserved word */ else { /* identifier or reserved word */
TaggedString *ts; TaggedString *ts;
do { do {
save_and_next(); save_and_next(LL);
} while (isalnum(current) || current == '_'); } while (isalnum(LL->current) || LL->current == '_');
save(0); save(LL, 0);
ts = luaS_new(textbuff.text); ts = luaS_new(LL->textbuff.text);
if (ts->head.marked > 255) if (ts->head.marked > 255)
return ts->head.marked; /* reserved word */ return ts->head.marked; /* reserved word */
luaY_lval.pTStr = ts; l->pTStr = ts;
return NAME; return NAME;
} }
} }

32
llex.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ ** $Id: llex.h,v 1.2 1997/11/04 15:27:53 roberto Exp roberto $
** Lexical Analizer ** Lexical Analizer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -11,11 +11,39 @@
#include "lzio.h" #include "lzio.h"
#define MAX_IFS 5
/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
struct ifState {
int elsepart; /* true if its in the $else part */
int condition; /* true if $if condition is true */
int skip; /* true if part must be skiped */
};
struct textBuff {
char *text;
int tokensize;
int buffsize;
};
typedef struct LexState {
int current; /* look ahead character */
struct zio *lex_z;
int linenumber;
struct ifState ifstate[MAX_IFS];
int iflevel; /* level of nested $if's (for lexical analysis) */
struct textBuff textbuff;
int linelasttoken;
int lastline;
} LexState;
extern int luaX_linenumber; extern int luaX_linenumber;
void luaX_init (void); void luaX_init (void);
int luaY_lex (void);
void luaX_setinput (ZIO *z); void luaX_setinput (ZIO *z);
char *luaX_lasttoken (void); char *luaX_lasttoken (void);

18
lmem.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: $ ** $Id: lmem.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $
** Interface to Memory Manager ** Interface to Memory Manager
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -8,6 +8,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "lmem.h" #include "lmem.h"
#include "lstate.h"
#include "lua.h" #include "lua.h"
@ -25,24 +26,21 @@ int luaM_growaux (void **block, unsigned long nelems, int size,
} }
static unsigned long Mbuffsize = 0;
static char *Mbuffer = NULL;
void *luaM_buffer (unsigned long size) void *luaM_buffer (unsigned long size)
{ {
if (size > Mbuffsize) { if (size > L->Mbuffsize) {
Mbuffsize = size; L->Mbuffsize = size;
Mbuffer = luaM_realloc(Mbuffer, Mbuffsize); L->Mbuffer = luaM_realloc(L->Mbuffer, L->Mbuffsize);
} }
return Mbuffer; return L->Mbuffer;
} }
void luaM_clearbuffer (void) void luaM_clearbuffer (void)
{ {
Mbuffsize /= 2; L->Mbuffsize /= 2;
Mbuffer = luaM_realloc(Mbuffer, Mbuffsize); L->Mbuffer = luaM_realloc(L->Mbuffer, L->Mbuffsize);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.c,v 1.5 1997/10/24 17:17:24 roberto Exp roberto $ ** $Id: lobject.c,v 1.6 1997/11/03 20:45:23 roberto Exp roberto $
** Some generic functions over Lua objects ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -21,9 +21,6 @@ TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
unsigned long luaO_nblocks = 0;
/* hash dimensions values */ /* hash dimensions values */
static long dimensions[] = static long dimensions[] =
{5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L, {5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 1.7 1997/10/24 17:17:24 roberto Exp roberto $ ** $Id: lobject.h,v 1.8 1997/11/03 20:45:23 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
*/ */
@ -162,7 +162,6 @@ typedef struct Hash {
** a gross estimation of number of memory "blocks" allocated ** a gross estimation of number of memory "blocks" allocated
** (a block is *roughly* 32 bytes) ** (a block is *roughly* 32 bytes)
*/ */
extern unsigned long luaO_nblocks;
extern char *luaO_typenames[]; extern char *luaO_typenames[];

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstring.c,v 1.3 1997/10/23 16:26:37 roberto Exp roberto $ ** $Id: lstring.c,v 1.4 1997/11/04 15:27:53 roberto Exp roberto $
** String table (keep all strings handled by Lua) ** String table (keep all strings handled by Lua)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -9,6 +9,7 @@
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
#include "lstate.h"
#include "lstring.h" #include "lstring.h"
#include "lua.h" #include "lua.h"
@ -19,18 +20,6 @@
#define gcsizestring(l) (1+(l/64)) #define gcsizestring(l) (1+(l/64))
GCnode luaS_root = {NULL, 0}; /* list of global variables */
typedef struct {
int size;
int nuse; /* number of elements (including EMPTYs) */
TaggedString **hash;
} stringtable;
static stringtable string_root[NUM_HASHS];
static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}}; static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}};
@ -38,10 +27,11 @@ static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}};
void luaS_init (void) void luaS_init (void)
{ {
int i; int i;
L->string_root = luaM_newvector(NUM_HASHS, stringtable);
for (i=0; i<NUM_HASHS; i++) { for (i=0; i<NUM_HASHS; i++) {
string_root[i].size = 0; L->string_root[i].size = 0;
string_root[i].nuse = 0; L->string_root[i].nuse = 0;
string_root[i].hash = NULL; L->string_root[i].hash = NULL;
} }
} }
@ -93,14 +83,14 @@ static TaggedString *newone(char *buff, int tag, unsigned long h)
strcpy(ts->str, buff); strcpy(ts->str, buff);
ts->u.globalval.ttype = LUA_T_NIL; /* initialize global value */ ts->u.globalval.ttype = LUA_T_NIL; /* initialize global value */
ts->constindex = 0; ts->constindex = 0;
luaO_nblocks += gcsizestring(l); L->nblocks += gcsizestring(l);
} }
else { else {
ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)); ts = (TaggedString *)luaM_malloc(sizeof(TaggedString));
ts->u.d.v = buff; ts->u.d.v = buff;
ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag; ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag;
ts->constindex = -1; /* tag -> this is a userdata */ ts->constindex = -1; /* tag -> this is a userdata */
luaO_nblocks++; L->nblocks++;
} }
ts->head.marked = 0; ts->head.marked = 0;
ts->head.next = (GCnode *)ts; /* signal it is in no list */ ts->head.next = (GCnode *)ts; /* signal it is in no list */
@ -138,12 +128,12 @@ static TaggedString *insert (char *buff, int tag, stringtable *tb)
TaggedString *luaS_createudata (void *udata, int tag) TaggedString *luaS_createudata (void *udata, int tag)
{ {
return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]); return insert(udata, tag, &L->string_root[(unsigned)udata%NUM_HASHS]);
} }
TaggedString *luaS_new (char *str) TaggedString *luaS_new (char *str)
{ {
return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]); return insert(str, LUA_T_STRING, &L->string_root[(unsigned)str[0]%NUM_HASHS]);
} }
TaggedString *luaS_newfixedstring (char *str) TaggedString *luaS_newfixedstring (char *str)
@ -159,7 +149,7 @@ void luaS_free (TaggedString *l)
{ {
while (l) { while (l) {
TaggedString *next = (TaggedString *)l->head.next; TaggedString *next = (TaggedString *)l->head.next;
luaO_nblocks -= (l->constindex == -1) ? 1 : gcsizestring(strlen(l->str)); L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(strlen(l->str));
luaM_free(l); luaM_free(l);
l = next; l = next;
} }
@ -185,9 +175,9 @@ TaggedString *luaS_collector (void)
{ {
TaggedString *frees = NULL; TaggedString *frees = NULL;
int i; int i;
remove_from_list(&luaS_root); remove_from_list(&(L->rootglobal));
for (i=0; i<NUM_HASHS; i++) { for (i=0; i<NUM_HASHS; i++) {
stringtable *tb = &string_root[i]; stringtable *tb = &L->string_root[i];
int j; int j;
for (j=0; j<tb->size; j++) { for (j=0; j<tb->size; j++) {
TaggedString *t = tb->hash[j]; TaggedString *t = tb->hash[j];
@ -209,8 +199,8 @@ void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
{ {
ts->u.globalval = *newval; ts->u.globalval = *newval;
if (ts->head.next == (GCnode *)ts) { /* is not in list? */ if (ts->head.next == (GCnode *)ts) { /* is not in list? */
ts->head.next = luaS_root.next; ts->head.next = L->rootglobal.next;
luaS_root.next = (GCnode *)ts; L->rootglobal.next = (GCnode *)ts;
} }
} }
@ -218,7 +208,7 @@ void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
char *luaS_travsymbol (int (*fn)(TObject *)) char *luaS_travsymbol (int (*fn)(TObject *))
{ {
TaggedString *g; TaggedString *g;
for (g=(TaggedString *)luaS_root.next; g; g=(TaggedString *)g->head.next) for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
if (fn(&g->u.globalval)) if (fn(&g->u.globalval))
return g->str; return g->str;
return NULL; return NULL;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstring.h,v 1.2 1997/09/26 15:02:26 roberto Exp roberto $ ** $Id: lstring.h,v 1.3 1997/11/04 15:27:53 roberto Exp roberto $
** String table (keep all strings handled by Lua) ** String table (keep all strings handled by Lua)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -10,8 +10,6 @@
#include "lobject.h" #include "lobject.h"
extern GCnode luaS_root;
void luaS_init (void); void luaS_init (void);
TaggedString *luaS_createudata (void *udata, int tag); TaggedString *luaS_createudata (void *udata, int tag);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 1.4 1997/10/23 16:26:37 roberto Exp roberto $ ** $Id: ltable.c,v 1.5 1997/10/24 17:17:24 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -9,6 +9,7 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
#include "lstate.h"
#include "ltable.h" #include "ltable.h"
#include "lua.h" #include "lua.h"
@ -24,9 +25,6 @@
#define TagDefault LUA_T_ARRAY; #define TagDefault LUA_T_ARRAY;
GCnode luaH_root = {NULL, 0};
static long int hashindex (TObject *ref) static long int hashindex (TObject *ref)
{ {
@ -95,7 +93,7 @@ void luaH_free (Hash *frees)
{ {
while (frees) { while (frees) {
Hash *next = (Hash *)frees->head.next; Hash *next = (Hash *)frees->head.next;
luaO_nblocks -= gcsize(frees->nhash); L->nblocks -= gcsize(frees->nhash);
hashdelete(frees); hashdelete(frees);
frees = next; frees = next;
} }
@ -110,8 +108,8 @@ Hash *luaH_new (int nhash)
nhash(t) = nhash; nhash(t) = nhash;
nuse(t) = 0; nuse(t) = 0;
t->htag = TagDefault; t->htag = TagDefault;
luaO_insertlist(&luaH_root, (GCnode *)t); luaO_insertlist(&(L->roottable), (GCnode *)t);
luaO_nblocks += gcsize(nhash); L->nblocks += gcsize(nhash);
return t; return t;
} }
@ -145,7 +143,7 @@ static void rehash (Hash *t)
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
*node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */ *node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */
} }
luaO_nblocks += gcsize(t->nhash)-gcsize(nold); L->nblocks += gcsize(t->nhash)-gcsize(nold);
luaM_free(vold); luaM_free(vold);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.h,v 1.2 1997/09/26 16:46:20 roberto Exp roberto $ ** $Id: ltable.h,v 1.3 1997/10/18 16:29:15 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -10,9 +10,6 @@
#include "lobject.h" #include "lobject.h"
extern GCnode luaH_root;
#define node(t,i) (&(t)->node[i]) #define node(t,i) (&(t)->node[i])
#define ref(n) (&(n)->ref) #define ref(n) (&(n)->ref)
#define val(n) (&(n)->val) #define val(n) (&(n)->val)

38
ltm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltm.c,v 1.6 1997/11/04 15:27:53 roberto Exp roberto $ ** $Id: ltm.c,v 1.7 1997/11/10 17:47:01 roberto Exp roberto $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -9,9 +9,9 @@
#include <string.h> #include <string.h>
#include "lauxlib.h" #include "lauxlib.h"
#include "ldo.h"
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
#include "lstate.h"
#include "ltm.h" #include "ltm.h"
@ -31,10 +31,6 @@ static int luaI_checkevent (char *name, char *list[])
} }
struct IM *luaT_IMtable;
static int IMtable_size;
static int last_tag;
/* events in LUA_T_NIL are all allowed, since this is used as a /* events in LUA_T_NIL are all allowed, since this is used as a
* 'placeholder' for "default" fallbacks * 'placeholder' for "default" fallbacks
@ -67,34 +63,34 @@ static void init_entry (int tag)
void luaT_init (void) void luaT_init (void)
{ {
int t; int t;
IMtable_size = NUM_TAGS*2; L->IMtable_size = NUM_TAGS*2;
last_tag = -(NUM_TAGS-1); L->last_tag = -(NUM_TAGS-1);
luaT_IMtable = luaM_newvector(IMtable_size, struct IM); L->IMtable = luaM_newvector(L->IMtable_size, struct IM);
for (t=last_tag; t<=0; t++) for (t=L->last_tag; t<=0; t++)
init_entry(t); init_entry(t);
} }
int lua_newtag (void) int lua_newtag (void)
{ {
--last_tag; --L->last_tag;
if ((-last_tag) >= IMtable_size) if ((-L->last_tag) >= L->IMtable_size)
IMtable_size = luaM_growvector(&luaT_IMtable, IMtable_size, L->IMtable_size = luaM_growvector(&L->IMtable, L->IMtable_size,
struct IM, memEM, MAX_INT); struct IM, memEM, MAX_INT);
init_entry(last_tag); init_entry(L->last_tag);
return last_tag; return L->last_tag;
} }
static void checktag (int tag) static void checktag (int tag)
{ {
if (!(last_tag <= tag && tag <= 0)) if (!(L->last_tag <= tag && tag <= 0))
luaL_verror("%d is not a valid tag", tag); luaL_verror("%d is not a valid tag", tag);
} }
void luaT_realtag (int tag) void luaT_realtag (int tag)
{ {
if (!(last_tag <= tag && tag < LUA_T_NIL)) if (!(L->last_tag <= tag && tag < LUA_T_NIL))
luaL_verror("tag %d is not result of `newtag'", tag); luaL_verror("tag %d is not result of `newtag'", tag);
} }
@ -145,11 +141,11 @@ void luaT_settagmethod (int t, char *event, TObject *func)
char *luaT_travtagmethods (int (*fn)(TObject *)) char *luaT_travtagmethods (int (*fn)(TObject *))
{ {
int e; int e;
if (fn(&luaD_errorim)) if (fn(&L->errorim))
return "error"; return "error";
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */ for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
int t; int t;
for (t=0; t>=last_tag; t--) for (t=0; t>=L->last_tag; t--)
if (fn(luaT_getim(t,e))) if (fn(luaT_getim(t,e)))
return luaT_eventname[e]; return luaT_eventname[e];
} }
@ -203,8 +199,8 @@ void luaT_setfallback (void)
luaL_arg_check(lua_isfunction(func), 2, "function expected"); luaL_arg_check(lua_isfunction(func), 2, "function expected");
switch (luaO_findstring(name, oldnames)) { switch (luaO_findstring(name, oldnames)) {
case 0: /* old error fallback */ case 0: /* old error fallback */
oldfunc = luaD_errorim; oldfunc = L->errorim;
luaD_errorim = *luaA_Address(func); L->errorim = *luaA_Address(func);
replace = errorFB; replace = errorFB;
break; break;
case 1: /* old getglobal fallback */ case 1: /* old getglobal fallback */

9
ltm.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltm.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ ** $Id: ltm.h,v 1.2 1997/11/04 15:27:53 roberto Exp roberto $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -9,6 +9,7 @@
#include "lobject.h" #include "lobject.h"
#include "lstate.h"
/* /*
* WARNING: if you change the order of this enumeration, * WARNING: if you change the order of this enumeration,
@ -38,12 +39,12 @@ typedef enum {
#define IM_N 18 #define IM_N 18
extern struct IM { struct IM {
TObject int_method[IM_N]; TObject int_method[IM_N];
} *luaT_IMtable; };
#define luaT_getim(tag,event) (&luaT_IMtable[-(tag)].int_method[event]) #define luaT_getim(tag,event) (&L->IMtable[-(tag)].int_method[event])
#define luaT_getimbyObj(o,e) (luaT_getim(luaT_efectivetag(o),(e))) #define luaT_getimbyObj(o,e) (luaT_getim(luaT_efectivetag(o),(e)))
extern char *luaT_eventname[]; extern char *luaT_eventname[];

3
lua.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.2 1997/10/06 14:51:32 roberto Exp roberto $ ** $Id: lua.c,v 1.3 1997/10/16 18:35:59 roberto Exp roberto $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -82,6 +82,7 @@ int main (int argc, char *argv[])
} }
} }
} }
/* lua_close(); */
return 0; return 0;
} }

4
lua.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.h,v 1.2 1997/10/24 17:17:24 roberto Exp roberto $ ** $Id: lua.h,v 1.3 1997/11/04 15:27:53 roberto Exp roberto $
** LUA - An Extensible Extension Language ** LUA - An Extensible Extension Language
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: lua@tecgraf.puc-rio.br ** e-mail: lua@tecgraf.puc-rio.br
@ -117,6 +117,8 @@ int lua_ref (int lock); /* In: value */
lua_Object lua_getref (int ref); lua_Object lua_getref (int ref);
void lua_unref (int ref); void lua_unref (int ref);
lua_Object lua_globalbag (void);
lua_Object lua_createtable (void); lua_Object lua_createtable (void);
long lua_collectgarbage (long limit); long lua_collectgarbage (long limit);

247
lua.stx
View File

@ -1,6 +1,6 @@
%{ %{
/* /*
** $Id: lua.stx,v 1.16 1997/10/30 18:47:19 roberto Exp roberto $ ** $Id: lua.stx,v 1.17 1997/11/07 15:09:49 roberto Exp roberto $
** Syntax analizer and code generator ** Syntax analizer and code generator
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -16,13 +16,13 @@
#include "lmem.h" #include "lmem.h"
#include "lopcodes.h" #include "lopcodes.h"
#include "lparser.h" #include "lparser.h"
#include "lstate.h"
#include "lstring.h" #include "lstring.h"
#include "lua.h" #include "lua.h"
#include "luadebug.h" #include "luadebug.h"
#include "lzio.h" #include "lzio.h"
/* to avoid warnings generated by yacc */
int luaY_parse (void); int luaY_parse (void);
@ -61,7 +61,7 @@ typedef long vardesc;
#define dotindex(v) ((-(v))-1) #define dotindex(v) ((-(v))-1)
/* state needed to generate code for a given function */ /* state needed to generate code for a given function */
typedef struct State { typedef struct FuncState {
TProtoFunc *f; /* current function header */ TProtoFunc *f; /* current function header */
int pc; /* next position to code */ int pc; /* next position to code */
TaggedString *localvar[MAXLOCALS]; /* store local variable names */ TaggedString *localvar[MAXLOCALS]; /* store local variable names */
@ -75,19 +75,19 @@ typedef struct State {
int maxconsts; /* size of f->consts */ int maxconsts; /* size of f->consts */
vardesc varbuffer[MAXVAR]; /* variables in an assignment list */ vardesc varbuffer[MAXVAR]; /* variables in an assignment list */
vardesc upvalues[MAXUPVALUES]; /* upvalues */ vardesc upvalues[MAXUPVALUES]; /* upvalues */
} State; } FuncState;
static State *mainState, *currState;
#define YYPURE 1
void luaY_syntaxerror (char *s, char *token) void luaY_syntaxerror (char *s, char *token)
{ {
if (token[0] == 0) if (token[0] == 0)
token = "<eof>"; token = "<eof>";
luaL_verror("%.100s;\n> last token read: \"%.50s\" at line %d in file %.50s", luaL_verror("%.100s;\n> last token read: \"%.50s\" at line %d in file %.50s",
s, token, luaX_linenumber, mainState->f->fileName->str); s, token, L->lexstate->linenumber, L->mainState->f->fileName->str);
} }
@ -99,16 +99,16 @@ void luaY_error (char *s)
static void check_pc (int n) static void check_pc (int n)
{ {
if (currState->pc+n > currState->maxcode) if (L->currState->pc+n > L->currState->maxcode)
currState->maxcode = luaM_growvector(&currState->f->code, L->currState->maxcode = luaM_growvector(&L->currState->f->code,
currState->maxcode, Byte, codeEM, MAX_INT); L->currState->maxcode, Byte, codeEM, MAX_INT);
} }
static void movecode_up (int d, int s, int n) static void movecode_up (int d, int s, int n)
{ {
while (n--) while (n--)
currState->f->code[d+n] = currState->f->code[s+n]; L->currState->f->code[d+n] = L->currState->f->code[s+n];
} }
@ -116,24 +116,24 @@ static void movecode_down (int d, int s, int n)
{ {
int i; int i;
for (i=0; i<n; i++) for (i=0; i<n; i++)
currState->f->code[d+i] = currState->f->code[s+i]; L->currState->f->code[d+i] = L->currState->f->code[s+i];
} }
static void code_byte (Byte c) static void code_byte (Byte c)
{ {
check_pc(1); check_pc(1);
currState->f->code[currState->pc++] = c; L->currState->f->code[L->currState->pc++] = c;
} }
static void deltastack (int delta) static void deltastack (int delta)
{ {
currState->stacksize += delta; L->currState->stacksize += delta;
if (currState->stacksize > currState->maxstacksize) { if (L->currState->stacksize > L->currState->maxstacksize) {
if (currState->stacksize > 255) if (L->currState->stacksize > 255)
luaY_error("function/expression too complex (limit 256)"); luaY_error("function/expression too complex (limit 256)");
currState->maxstacksize = currState->stacksize; L->currState->maxstacksize = L->currState->stacksize;
} }
} }
@ -142,18 +142,18 @@ static int code_oparg_at (int pc, OpCode op, int builtin, int arg, int delta)
{ {
deltastack(delta); deltastack(delta);
if (arg < builtin) { if (arg < builtin) {
currState->f->code[pc] = op+1+arg; L->currState->f->code[pc] = op+1+arg;
return 1; return 1;
} }
else if (arg <= 255) { else if (arg <= 255) {
currState->f->code[pc] = op; L->currState->f->code[pc] = op;
currState->f->code[pc+1] = arg; L->currState->f->code[pc+1] = arg;
return 2; return 2;
} }
else if (arg <= MAX_WORD) { else if (arg <= MAX_WORD) {
currState->f->code[pc] = op+1+builtin; L->currState->f->code[pc] = op+1+builtin;
currState->f->code[pc+1] = arg&0xFF; L->currState->f->code[pc+1] = arg&0xFF;
currState->f->code[pc+2] = arg>>8; L->currState->f->code[pc+2] = arg>>8;
return 3; return 3;
} }
else luaY_error("code too long (limit 64K)"); else luaY_error("code too long (limit 64K)");
@ -164,13 +164,13 @@ static int code_oparg_at (int pc, OpCode op, int builtin, int arg, int delta)
static int fix_opcode (int pc, OpCode op, int builtin, int arg) static int fix_opcode (int pc, OpCode op, int builtin, int arg)
{ {
if (arg < builtin) { /* close space */ if (arg < builtin) { /* close space */
movecode_down(pc+1, pc+2, currState->pc-(pc+2)); movecode_down(pc+1, pc+2, L->currState->pc-(pc+2));
currState->pc--; L->currState->pc--;
} }
else if (arg > 255) { /* open space */ else if (arg > 255) { /* open space */
check_pc(1); check_pc(1);
movecode_up(pc+1, pc, currState->pc-pc); movecode_up(pc+1, pc, L->currState->pc-pc);
currState->pc++; L->currState->pc++;
} }
return code_oparg_at(pc, op, builtin, arg, 0) - 2; return code_oparg_at(pc, op, builtin, arg, 0) - 2;
} }
@ -179,7 +179,7 @@ static int fix_opcode (int pc, OpCode op, int builtin, int arg)
static void code_oparg (OpCode op, int builtin, int arg, int delta) static void code_oparg (OpCode op, int builtin, int arg, int delta)
{ {
check_pc(3); /* maximum code size */ check_pc(3); /* maximum code size */
currState->pc += code_oparg_at(currState->pc, op, builtin, arg, delta); L->currState->pc += code_oparg_at(L->currState->pc, op, builtin, arg, delta);
} }
@ -214,7 +214,7 @@ static void code_constant (int c)
} }
static int next_constant (State *cs) static int next_constant (FuncState *cs)
{ {
TProtoFunc *f = cs->f; TProtoFunc *f = cs->f;
if (f->nconsts >= cs->maxconsts) { if (f->nconsts >= cs->maxconsts) {
@ -225,7 +225,7 @@ static int next_constant (State *cs)
} }
static int string_constant (TaggedString *s, State *cs) static int string_constant (TaggedString *s, FuncState *cs)
{ {
TProtoFunc *f = cs->f; TProtoFunc *f = cs->f;
int c = s->constindex; int c = s->constindex;
@ -242,7 +242,7 @@ static int string_constant (TaggedString *s, State *cs)
static void code_string (TaggedString *s) static void code_string (TaggedString *s)
{ {
code_constant(string_constant(s, currState)); code_constant(string_constant(s, L->currState));
} }
@ -250,16 +250,16 @@ static void code_string (TaggedString *s)
static int real_constant (real r) static int real_constant (real r)
{ {
/* check whether 'r' has appeared within the last LIM entries */ /* check whether 'r' has appeared within the last LIM entries */
TObject *cnt = currState->f->consts; TObject *cnt = L->currState->f->consts;
int c = currState->f->nconsts; int c = L->currState->f->nconsts;
int lim = c < LIM ? 0 : c-LIM; int lim = c < LIM ? 0 : c-LIM;
while (--c >= lim) { while (--c >= lim) {
if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r) if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
return c; return c;
} }
/* not found; create a luaM_new entry */ /* not found; create a luaM_new entry */
c = next_constant(currState); c = next_constant(L->currState);
cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */ cnt = L->currState->f->consts; /* 'next_constant' may reallocate this vector */
ttype(&cnt[c]) = LUA_T_NUMBER; ttype(&cnt[c]) = LUA_T_NUMBER;
nvalue(&cnt[c]) = r; nvalue(&cnt[c]) = r;
return c; return c;
@ -292,13 +292,13 @@ static void flush_list (int m, int n)
static void luaI_registerlocalvar (TaggedString *varname, int line) static void luaI_registerlocalvar (TaggedString *varname, int line)
{ {
if (currState->maxvars != -1) { /* debug information? */ if (L->currState->maxvars != -1) { /* debug information? */
if (currState->nvars >= currState->maxvars) if (L->currState->nvars >= L->currState->maxvars)
currState->maxvars = luaM_growvector(&currState->f->locvars, L->currState->maxvars = luaM_growvector(&L->currState->f->locvars,
currState->maxvars, LocVar, "", MAX_WORD); L->currState->maxvars, LocVar, "", MAX_WORD);
currState->f->locvars[currState->nvars].varname = varname; L->currState->f->locvars[L->currState->nvars].varname = varname;
currState->f->locvars[currState->nvars].line = line; L->currState->f->locvars[L->currState->nvars].line = line;
currState->nvars++; L->currState->nvars++;
} }
} }
@ -311,17 +311,17 @@ static void luaI_unregisterlocalvar (int line)
static void store_localvar (TaggedString *name, int n) static void store_localvar (TaggedString *name, int n)
{ {
if (currState->nlocalvar+n < MAXLOCALS) if (L->currState->nlocalvar+n < MAXLOCALS)
currState->localvar[currState->nlocalvar+n] = name; L->currState->localvar[L->currState->nlocalvar+n] = name;
else else
luaY_error("too many local variables (limit 32)"); luaY_error("too many local variables (limit 32)");
luaI_registerlocalvar(name, luaX_linenumber); luaI_registerlocalvar(name, L->lexstate->linenumber);
} }
static void add_localvar (TaggedString *name) static void add_localvar (TaggedString *name)
{ {
store_localvar(name, 0); store_localvar(name, 0);
currState->nlocalvar++; L->currState->nlocalvar++;
} }
@ -342,11 +342,11 @@ static void add_varbuffer (vardesc var, int n)
{ {
if (n >= MAXVAR) if (n >= MAXVAR)
luaY_error("variable buffer overflow (limit 32)"); luaY_error("variable buffer overflow (limit 32)");
currState->varbuffer[n] = var2store(var); L->currState->varbuffer[n] = var2store(var);
} }
static int aux_localname (TaggedString *n, State *st) static int aux_localname (TaggedString *n, FuncState *st)
{ {
int i; int i;
for (i=st->nlocalvar-1; i >= 0; i--) for (i=st->nlocalvar-1; i >= 0; i--)
@ -355,12 +355,12 @@ static int aux_localname (TaggedString *n, State *st)
} }
static vardesc singlevar (TaggedString *n, State *st) static vardesc singlevar (TaggedString *n, FuncState *st)
{ {
int i = aux_localname(n, st); int i = aux_localname(n, st);
if (i == -1) { /* check shadowing */ if (i == -1) { /* check shadowing */
int l; int l;
for (l=1; l<=(st-mainState); l++) for (l=1; l<=(st-L->mainState); l++)
if (aux_localname(n, st-l) >= 0) if (aux_localname(n, st-l) >= 0)
luaY_syntaxerror("cannot access a variable in outer scope", n->str); luaY_syntaxerror("cannot access a variable in outer scope", n->str);
return string_constant(n, st)+MINGLOBAL; /* global value */ return string_constant(n, st)+MINGLOBAL; /* global value */
@ -371,16 +371,16 @@ static vardesc singlevar (TaggedString *n, State *st)
static int indexupvalue (TaggedString *n) static int indexupvalue (TaggedString *n)
{ {
vardesc v = singlevar(n, currState-1); vardesc v = singlevar(n, L->currState-1);
int i; int i;
for (i=0; i<currState->nupvalues; i++) { for (i=0; i<L->currState->nupvalues; i++) {
if (currState->upvalues[i] == v) if (L->currState->upvalues[i] == v)
return i; return i;
} }
/* new one */ /* new one */
if (++(currState->nupvalues) > MAXUPVALUES) if (++(L->currState->nupvalues) > MAXUPVALUES)
luaY_error("too many upvalues in a single function (limit 16)"); luaY_error("too many upvalues in a single function (limit 16)");
currState->upvalues[i] = v; /* i = currState->nupvalues - 1 */ L->currState->upvalues[i] = v; /* i = L->currState->nupvalues - 1 */
return i; return i;
} }
@ -388,9 +388,9 @@ static int indexupvalue (TaggedString *n)
static void pushupvalue (TaggedString *n) static void pushupvalue (TaggedString *n)
{ {
int i; int i;
if (currState == mainState) if (L->currState == L->mainState)
luaY_error("cannot access upvalue in main"); luaY_error("cannot access upvalue in main");
if (aux_localname(n, currState) >= 0) if (aux_localname(n, L->currState) >= 0)
luaY_syntaxerror("cannot access an upvalue in current scope", n->str); luaY_syntaxerror("cannot access an upvalue in current scope", n->str);
i = indexupvalue(n); i = indexupvalue(n);
code_oparg(PUSHUPVALUE, 2, i, 1); code_oparg(PUSHUPVALUE, 2, i, 1);
@ -399,10 +399,9 @@ static void pushupvalue (TaggedString *n)
void luaY_codedebugline (int line) void luaY_codedebugline (int line)
{ {
static int lastline = 0; if (lua_debug && line != L->lexstate->lastline) {
if (lua_debug && line != lastline) {
code_oparg(SETLINE, 0, line, 0); code_oparg(SETLINE, 0, line, 0);
lastline = line; L->lexstate->lastline = line;
} }
} }
@ -421,10 +420,10 @@ static long adjust_functioncall (long exp, int nresults)
if (exp <= 0) if (exp <= 0)
return -exp; /* exp is -list length */ return -exp; /* exp is -list length */
else { else {
int temp = currState->f->code[exp]; int temp = L->currState->f->code[exp];
int nparams = currState->f->code[exp-1]; int nparams = L->currState->f->code[exp-1];
exp += fix_opcode(exp-2, CALLFUNC, 2, nresults); exp += fix_opcode(exp-2, CALLFUNC, 2, nresults);
currState->f->code[exp] = nparams; L->currState->f->code[exp] = nparams;
if (nresults != MULT_RET) if (nresults != MULT_RET)
deltastack(nresults); deltastack(nresults);
deltastack(-(nparams+1)); deltastack(-(nparams+1));
@ -436,7 +435,7 @@ static long adjust_functioncall (long exp, int nresults)
static void adjust_mult_assign (int vars, long exps) static void adjust_mult_assign (int vars, long exps)
{ {
if (exps > 0) { /* must correct function call */ if (exps > 0) { /* must correct function call */
int diff = currState->f->code[exps] - vars; int diff = L->currState->f->code[exps] - vars;
if (diff < 0) if (diff < 0)
adjust_functioncall(exps, -diff); adjust_functioncall(exps, -diff);
else { else {
@ -450,11 +449,11 @@ static void adjust_mult_assign (int vars, long exps)
static void code_args (int nparams, int dots) static void code_args (int nparams, int dots)
{ {
currState->nlocalvar += nparams; L->currState->nlocalvar += nparams;
if (!dots) if (!dots)
code_oparg(ARGS, 0, currState->nlocalvar, currState->nlocalvar); code_oparg(ARGS, 0, L->currState->nlocalvar, L->currState->nlocalvar);
else { else {
code_oparg(VARARGS, 0, currState->nlocalvar, currState->nlocalvar+1); code_oparg(VARARGS, 0, L->currState->nlocalvar, L->currState->nlocalvar+1);
add_localvar(luaS_new("arg")); add_localvar(luaS_new("arg"));
} }
} }
@ -487,9 +486,9 @@ static void storevar (vardesc var)
/* returns how many elements are left as 'garbage' on the stack */ /* returns how many elements are left as 'garbage' on the stack */
static int lua_codestore (int i, int left) static int lua_codestore (int i, int left)
{ {
if (currState->varbuffer[i] != 0 || /* global or local var or */ if (L->currState->varbuffer[i] != 0 || /* global or local var or */
left+i == 0) { /* indexed var without values in between */ left+i == 0) { /* indexed var without values in between */
storevar(currState->varbuffer[i]); storevar(L->currState->varbuffer[i]);
return left; return left;
} }
else { /* indexed var with values in between*/ else { /* indexed var with values in between*/
@ -508,7 +507,7 @@ static int fix_jump (int pc, OpCode op, int n)
static void fix_upjmp (OpCode op, int pos) static void fix_upjmp (OpCode op, int pos)
{ {
int delta = currState->pc+JMPSIZE - pos; /* jump is relative */ int delta = L->currState->pc+JMPSIZE - pos; /* jump is relative */
if (delta > 255) delta++; if (delta > 255) delta++;
code_oparg(op, 0, delta, 0); code_oparg(op, 0, delta, 0);
} }
@ -517,38 +516,38 @@ static void fix_upjmp (OpCode op, int pos)
static void codeIf (int thenAdd, int elseAdd) static void codeIf (int thenAdd, int elseAdd)
{ {
int elseinit = elseAdd+JMPSIZE; int elseinit = elseAdd+JMPSIZE;
if (currState->pc == elseinit) { /* no else part */ if (L->currState->pc == elseinit) { /* no else part */
currState->pc -= JMPSIZE; L->currState->pc -= JMPSIZE;
elseinit = currState->pc; elseinit = L->currState->pc;
} }
else else
elseinit += fix_jump(elseAdd, JMP, currState->pc); elseinit += fix_jump(elseAdd, JMP, L->currState->pc);
fix_jump(thenAdd, IFFJMP, elseinit); fix_jump(thenAdd, IFFJMP, elseinit);
} }
static void code_shortcircuit (int pc, OpCode op) static void code_shortcircuit (int pc, OpCode op)
{ {
fix_jump(pc, op, currState->pc); fix_jump(pc, op, L->currState->pc);
} }
static void codereturn (void) static void codereturn (void)
{ {
code_oparg(RETCODE, 0, currState->nlocalvar, 0); code_oparg(RETCODE, 0, L->currState->nlocalvar, 0);
currState->stacksize = currState->nlocalvar; L->currState->stacksize = L->currState->nlocalvar;
} }
static void func_onstack (TProtoFunc *f) static void func_onstack (TProtoFunc *f)
{ {
int i; int i;
int nupvalues = (currState+1)->nupvalues; int nupvalues = (L->currState+1)->nupvalues;
int c = next_constant(currState); int c = next_constant(L->currState);
ttype(&currState->f->consts[c]) = LUA_T_PROTO; ttype(&L->currState->f->consts[c]) = LUA_T_PROTO;
currState->f->consts[c].value.tf = (currState+1)->f; L->currState->f->consts[c].value.tf = (L->currState+1)->f;
for (i=0; i<nupvalues; i++) for (i=0; i<nupvalues; i++)
lua_pushvar((currState+1)->upvalues[i]); lua_pushvar((L->currState+1)->upvalues[i]);
code_constant(c); code_constant(c);
code_oparg(CLOSURE, 2, nupvalues, -nupvalues); code_oparg(CLOSURE, 2, nupvalues, -nupvalues);
} }
@ -557,48 +556,48 @@ static void func_onstack (TProtoFunc *f)
static void init_state (TaggedString *filename) static void init_state (TaggedString *filename)
{ {
TProtoFunc *f = luaF_newproto(); TProtoFunc *f = luaF_newproto();
currState->stacksize = 0; L->currState->stacksize = 0;
currState->maxstacksize = 0; L->currState->maxstacksize = 0;
currState->nlocalvar = 0; L->currState->nlocalvar = 0;
currState->nupvalues = 0; L->currState->nupvalues = 0;
currState->f = f; L->currState->f = f;
f->fileName = filename; f->fileName = filename;
currState->pc = 0; L->currState->pc = 0;
currState->maxcode = 0; L->currState->maxcode = 0;
f->code = NULL; f->code = NULL;
currState->maxconsts = 0; L->currState->maxconsts = 0;
if (lua_debug) { if (lua_debug) {
currState->nvars = 0; L->currState->nvars = 0;
currState->maxvars = 0; L->currState->maxvars = 0;
} }
else else
currState->maxvars = -1; /* flag no debug information */ L->currState->maxvars = -1; /* flag no debug information */
code_byte(0); /* to be filled with stacksize */ code_byte(0); /* to be filled with stacksize */
} }
static void init_func (void) static void init_func (void)
{ {
if (currState-mainState >= MAXSTATES-1) if (L->currState-L->mainState >= MAXSTATES-1)
luaY_error("too many nested functions (limit 6)"); luaY_error("too many nested functions (limit 6)");
currState++; L->currState++;
init_state(mainState->f->fileName); init_state(L->mainState->f->fileName);
luaY_codedebugline(luaX_linenumber); luaY_codedebugline(L->lexstate->linenumber);
currState->f->lineDefined = luaX_linenumber; L->currState->f->lineDefined = L->lexstate->linenumber;
} }
static TProtoFunc *close_func (void) static TProtoFunc *close_func (void)
{ {
TProtoFunc *f = currState->f; TProtoFunc *f = L->currState->f;
code_neutralop(ENDCODE); code_neutralop(ENDCODE);
f->code[0] = currState->maxstacksize; f->code[0] = L->currState->maxstacksize;
f->code = luaM_reallocvector(f->code, currState->pc, Byte); f->code = luaM_reallocvector(f->code, L->currState->pc, Byte);
f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject); f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject);
if (currState->maxvars != -1) { /* debug information? */ if (L->currState->maxvars != -1) { /* debug information? */
luaI_registerlocalvar(NULL, -1); /* flag end of vector */ luaI_registerlocalvar(NULL, -1); /* flag end of vector */
f->locvars = luaM_reallocvector(f->locvars, currState->nvars, LocVar); f->locvars = luaM_reallocvector(f->locvars, L->currState->nvars, LocVar);
} }
currState--; L->currState--;
return f; return f;
} }
@ -608,8 +607,10 @@ static TProtoFunc *close_func (void)
*/ */
TProtoFunc *luaY_parser (ZIO *z, char *chunkname) TProtoFunc *luaY_parser (ZIO *z, char *chunkname)
{ {
State state[MAXSTATES]; struct LexState lexstate;
currState = mainState = &state[0]; FuncState state[MAXSTATES];
L->currState = L->mainState = &state[0];
L->lexstate = &lexstate;
luaX_setinput(z); luaX_setinput(z);
init_state(luaS_new(chunkname)); init_state(luaS_new(chunkname));
if (luaY_parse ()) lua_error("parse error"); if (luaY_parse ()) lua_error("parse error");
@ -685,10 +686,10 @@ stat : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
int expsize = $3-$2; int expsize = $3-$2;
int newpos = $2+JMPSIZE; int newpos = $2+JMPSIZE;
check_pc(expsize); check_pc(expsize);
memcpy(&currState->f->code[currState->pc], memcpy(&L->currState->f->code[L->currState->pc],
&currState->f->code[$2], expsize); &L->currState->f->code[$2], expsize);
movecode_down($2, $3, currState->pc-$2); movecode_down($2, $3, L->currState->pc-$2);
newpos += fix_jump($2, JMP, currState->pc-expsize); newpos += fix_jump($2, JMP, L->currState->pc-expsize);
fix_upjmp(IFTUPJMP, newpos); fix_upjmp(IFTUPJMP, newpos);
}} }}
@ -712,18 +713,18 @@ stat : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
| LOCAL localnamelist decinit | LOCAL localnamelist decinit
{ {
currState->nlocalvar += $2; L->currState->nlocalvar += $2;
adjust_mult_assign($2, $3); adjust_mult_assign($2, $3);
} }
| FUNCTION funcname body { func_onstack($3); storevar($2); } | FUNCTION funcname body { func_onstack($3); storevar($2); }
; ;
block : {$<vInt>$ = currState->nlocalvar;} chunk block : {$<vInt>$ = L->currState->nlocalvar;} chunk
{ {
adjuststack(currState->nlocalvar - $<vInt>1); adjuststack(L->currState->nlocalvar - $<vInt>1);
for (; currState->nlocalvar > $<vInt>1; currState->nlocalvar--) for (; L->currState->nlocalvar > $<vInt>1; L->currState->nlocalvar--)
luaI_unregisterlocalvar(luaX_linenumber); luaI_unregisterlocalvar(L->lexstate->linenumber);
} }
; ;
@ -753,13 +754,13 @@ ret : /* empty */
} }
; ;
GetPC : /* empty */ { $$ = currState->pc; } GetPC : /* empty */ { $$ = L->currState->pc; }
; ;
SaveWord : /* empty */ SaveWord : /* empty */
{ $$ = currState->pc; { $$ = L->currState->pc;
check_pc(JMPSIZE); check_pc(JMPSIZE);
currState->pc += JMPSIZE; /* open space */ L->currState->pc += JMPSIZE; /* open space */
} }
; ;
@ -808,7 +809,7 @@ functioncall : funcvalue funcParams
{ {
code_byte(0); /* save space for opcode */ code_byte(0); /* save space for opcode */
code_byte($1+$2); /* number of parameters */ code_byte($1+$2); /* number of parameters */
$$ = currState->pc; $$ = L->currState->pc;
code_byte(0); /* must be adjusted by other rules */ code_byte(0); /* must be adjusted by other rules */
} }
; ;
@ -816,7 +817,7 @@ functioncall : funcvalue funcParams
funcvalue : varexp { $$ = 0; } funcvalue : varexp { $$ = 0; }
| varexp ':' NAME | varexp ':' NAME
{ {
code_oparg(PUSHSELF, 0, string_constant($3, currState), 1); code_oparg(PUSHSELF, 0, string_constant($3, L->currState), 1);
$$ = 1; $$ = 1;
} }
; ;
@ -834,7 +835,7 @@ exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
{ {
if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */ if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
else { else {
currState->f->code[$4] = $<vLong>3; /* store list length */ L->currState->f->code[$4] = $<vLong>3; /* store list length */
$$ = $4; $$ = $4;
} }
} }
@ -899,9 +900,9 @@ varlist1 : var { $$ = 1; add_varbuffer($1, 0); }
| varlist1 ',' var { add_varbuffer($3, $1); $$ = $1+1; } | varlist1 ',' var { add_varbuffer($3, $1); $$ = $1+1; }
; ;
var : NAME { $$ = singlevar($1, currState); } var : NAME { $$ = singlevar($1, L->currState); }
| varexp '[' expr1 ']' { $$ = 0; } /* indexed variable */ | varexp '[' expr1 ']' { $$ = 0; } /* indexed variable */
| varexp '.' NAME { $$ = (-string_constant($3, currState))-1; } | varexp '.' NAME { $$ = (-string_constant($3, L->currState))-1; }
; ;
varexp : var { lua_pushvar($1); } varexp : var { lua_pushvar($1); }

224
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.12 1997/10/24 18:40:29 roberto Exp roberto $ ** $Id: lvm.c,v 1.13 1997/10/27 16:14:37 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -14,6 +14,7 @@
#include "lgc.h" #include "lgc.h"
#include "lmem.h" #include "lmem.h"
#include "lopcodes.h" #include "lopcodes.h"
#include "lstate.h"
#include "lstring.h" #include "lstring.h"
#include "ltable.h" #include "ltable.h"
#include "ltm.h" #include "ltm.h"
@ -79,11 +80,11 @@ int luaV_tostring (TObject *obj)
void luaV_closure (int nelems) void luaV_closure (int nelems)
{ {
Closure *c = luaF_newclosure(nelems); Closure *c = luaF_newclosure(nelems);
c->consts[0] = *(luaD_stack.top-1); c->consts[0] = *(L->stack.top-1);
memcpy(&c->consts[1], luaD_stack.top-(nelems+1), nelems*sizeof(TObject)); memcpy(&c->consts[1], L->stack.top-(nelems+1), nelems*sizeof(TObject));
luaD_stack.top -= nelems; L->stack.top -= nelems;
ttype(luaD_stack.top-1) = LUA_T_FUNCTION; ttype(L->stack.top-1) = LUA_T_FUNCTION;
(luaD_stack.top-1)->value.cl = c; (L->stack.top-1)->value.cl = c;
} }
@ -94,22 +95,22 @@ void luaV_closure (int nelems)
void luaV_gettable (void) void luaV_gettable (void)
{ {
TObject *im; TObject *im;
if (ttype(luaD_stack.top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */ if (ttype(L->stack.top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */
im = luaT_getimbyObj(luaD_stack.top-2, IM_GETTABLE); im = luaT_getimbyObj(L->stack.top-2, IM_GETTABLE);
else { /* object is a table... */ else { /* object is a table... */
int tg = (luaD_stack.top-2)->value.a->htag; int tg = (L->stack.top-2)->value.a->htag;
im = luaT_getim(tg, IM_GETTABLE); im = luaT_getim(tg, IM_GETTABLE);
if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */ if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */
TObject *h = luaH_get(avalue(luaD_stack.top-2), luaD_stack.top-1); TObject *h = luaH_get(avalue(L->stack.top-2), L->stack.top-1);
if (h != NULL && ttype(h) != LUA_T_NIL) { if (h != NULL && ttype(h) != LUA_T_NIL) {
--luaD_stack.top; --L->stack.top;
*(luaD_stack.top-1) = *h; *(L->stack.top-1) = *h;
} }
else if (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL) else if (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL)
luaD_callTM(im, 2, 1); luaD_callTM(im, 2, 1);
else { else {
--luaD_stack.top; --L->stack.top;
ttype(luaD_stack.top-1) = LUA_T_NIL; ttype(L->stack.top-1) = LUA_T_NIL;
} }
return; return;
} }
@ -124,26 +125,26 @@ void luaV_gettable (void)
/* /*
** Function to store indexed based on values at the luaD_stack.top ** Function to store indexed based on values at the L->stack.top
** mode = 0: raw store (without internal methods) ** mode = 0: raw store (without internal methods)
** mode = 1: normal store (with internal methods) ** mode = 1: normal store (with internal methods)
** mode = 2: "deep luaD_stack.stack" store (with internal methods) ** mode = 2: "deep L->stack.stack" store (with internal methods)
*/ */
void luaV_settable (TObject *t, int mode) void luaV_settable (TObject *t, int mode)
{ {
TObject *im = (mode == 0) ? NULL : luaT_getimbyObj(t, IM_SETTABLE); TObject *im = (mode == 0) ? NULL : luaT_getimbyObj(t, IM_SETTABLE);
if (ttype(t) == LUA_T_ARRAY && (im == NULL || ttype(im) == LUA_T_NIL)) { if (ttype(t) == LUA_T_ARRAY && (im == NULL || ttype(im) == LUA_T_NIL)) {
TObject *h = luaH_set(avalue(t), t+1); TObject *h = luaH_set(avalue(t), t+1);
*h = *(luaD_stack.top-1); *h = *(L->stack.top-1);
luaD_stack.top -= (mode == 2) ? 1 : 3; L->stack.top -= (mode == 2) ? 1 : 3;
} }
else { /* object is not a table, and/or has a specific "settable" method */ else { /* object is not a table, and/or has a specific "settable" method */
if (im && ttype(im) != LUA_T_NIL) { if (im && ttype(im) != LUA_T_NIL) {
if (mode == 2) { if (mode == 2) {
*(luaD_stack.top+1) = *(luaD_stack.top-1); *(L->stack.top+1) = *(L->stack.top-1);
*(luaD_stack.top) = *(t+1); *(L->stack.top) = *(t+1);
*(luaD_stack.top-1) = *t; *(L->stack.top-1) = *t;
luaD_stack.top += 2; /* WARNING: caller must assure stack space */ L->stack.top += 2; /* WARNING: caller must assure stack space */
} }
luaD_callTM(im, 3, 0); luaD_callTM(im, 3, 0);
} }
@ -159,13 +160,13 @@ void luaV_getglobal (TaggedString *ts)
TObject *value = &ts->u.globalval; TObject *value = &ts->u.globalval;
TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL); TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL);
if (ttype(im) == LUA_T_NIL) { /* default behavior */ if (ttype(im) == LUA_T_NIL) { /* default behavior */
*luaD_stack.top++ = *value; *L->stack.top++ = *value;
} }
else { else {
ttype(luaD_stack.top) = LUA_T_STRING; ttype(L->stack.top) = LUA_T_STRING;
tsvalue(luaD_stack.top) = ts; tsvalue(L->stack.top) = ts;
luaD_stack.top++; L->stack.top++;
*luaD_stack.top++ = *value; *L->stack.top++ = *value;
luaD_callTM(im, 2, 1); luaD_callTM(im, 2, 1);
} }
} }
@ -176,14 +177,14 @@ void luaV_setglobal (TaggedString *ts)
TObject *oldvalue = &ts->u.globalval; TObject *oldvalue = &ts->u.globalval;
TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL); TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL);
if (ttype(im) == LUA_T_NIL) /* default behavior */ if (ttype(im) == LUA_T_NIL) /* default behavior */
luaS_rawsetglobal(ts, --luaD_stack.top); luaS_rawsetglobal(ts, --L->stack.top);
else { else {
/* WARNING: caller must assure stack space */ /* WARNING: caller must assure stack space */
TObject newvalue = *(luaD_stack.top-1); TObject newvalue = *(L->stack.top-1);
ttype(luaD_stack.top-1) = LUA_T_STRING; ttype(L->stack.top-1) = LUA_T_STRING;
tsvalue(luaD_stack.top-1) = ts; tsvalue(L->stack.top-1) = ts;
*luaD_stack.top++ = *oldvalue; *L->stack.top++ = *oldvalue;
*luaD_stack.top++ = newvalue; *L->stack.top++ = newvalue;
luaD_callTM(im, 3, 0); luaD_callTM(im, 3, 0);
} }
} }
@ -191,9 +192,9 @@ void luaV_setglobal (TaggedString *ts)
static void call_binTM (IMS event, char *msg) static void call_binTM (IMS event, char *msg)
{ {
TObject *im = luaT_getimbyObj(luaD_stack.top-2, event);/* try first operand */ TObject *im = luaT_getimbyObj(L->stack.top-2, event);/* try first operand */
if (ttype(im) == LUA_T_NIL) { if (ttype(im) == LUA_T_NIL) {
im = luaT_getimbyObj(luaD_stack.top-1, event); /* try second operand */ im = luaT_getimbyObj(L->stack.top-1, event); /* try second operand */
if (ttype(im) == LUA_T_NIL) { if (ttype(im) == LUA_T_NIL) {
im = luaT_getim(0, event); /* try a 'global' i.m. */ im = luaT_getim(0, event); /* try a 'global' i.m. */
if (ttype(im) == LUA_T_NIL) if (ttype(im) == LUA_T_NIL)
@ -214,8 +215,8 @@ static void call_arith (IMS event)
static void comparison (lua_Type ttype_less, lua_Type ttype_equal, static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
lua_Type ttype_great, IMS op) lua_Type ttype_great, IMS op)
{ {
TObject *l = luaD_stack.top-2; TObject *l = L->stack.top-2;
TObject *r = luaD_stack.top-1; TObject *r = L->stack.top-1;
int result; int result;
if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER) if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1; result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
@ -225,16 +226,16 @@ static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
call_binTM(op, "unexpected type at comparison"); call_binTM(op, "unexpected type at comparison");
return; return;
} }
luaD_stack.top--; L->stack.top--;
nvalue(luaD_stack.top-1) = 1; nvalue(L->stack.top-1) = 1;
ttype(luaD_stack.top-1) = (result < 0) ? ttype_less : ttype(L->stack.top-1) = (result < 0) ? ttype_less :
(result == 0) ? ttype_equal : ttype_great; (result == 0) ? ttype_equal : ttype_great;
} }
void luaV_pack (StkId firstel, int nvararg, TObject *tab) void luaV_pack (StkId firstel, int nvararg, TObject *tab)
{ {
TObject *firstelem = luaD_stack.stack+firstel; TObject *firstelem = L->stack.stack+firstel;
int i; int i;
if (nvararg < 0) nvararg = 0; if (nvararg < 0) nvararg = 0;
avalue(tab) = luaH_new(nvararg+1); /* +1 for field 'n' */ avalue(tab) = luaH_new(nvararg+1); /* +1 for field 'n' */
@ -260,20 +261,21 @@ static void adjust_varargs (StkId first_extra_arg)
{ {
TObject arg; TObject arg;
luaV_pack(first_extra_arg, luaV_pack(first_extra_arg,
(luaD_stack.top-luaD_stack.stack)-first_extra_arg, &arg); (L->stack.top-L->stack.stack)-first_extra_arg, &arg);
luaD_adjusttop(first_extra_arg); luaD_adjusttop(first_extra_arg);
*luaD_stack.top++ = arg; *L->stack.top++ = arg;
} }
/* /*
** Execute the given opcode, until a RET. Parameters are between ** Execute the given opcode, until a RET. Parameters are between
** [luaD_stack.stack+base,luaD_stack.top). Returns n such that the the results are between ** [stack+base,top). Returns n such that the the results are between
** [luaD_stack.stack+n,luaD_stack.top). ** [stack+n,top).
*/ */
StkId luaV_execute (Closure *cl, StkId base) StkId luaV_execute (Closure *cl, StkId base)
{ {
LState *LL = L; /* to optimize */
Byte *pc = cl->consts[0].value.tf->code; Byte *pc = cl->consts[0].value.tf->code;
TObject *consts = cl->consts[0].value.tf->consts; TObject *consts = cl->consts[0].value.tf->consts;
if (lua_callhook) if (lua_callhook)
@ -284,13 +286,13 @@ StkId luaV_execute (Closure *cl, StkId base)
switch ((OpCode)(aux = *pc++)) { switch ((OpCode)(aux = *pc++)) {
case PUSHNIL0: case PUSHNIL0:
ttype(luaD_stack.top++) = LUA_T_NIL; ttype(LL->stack.top++) = LUA_T_NIL;
break; break;
case PUSHNIL: case PUSHNIL:
aux = *pc++; aux = *pc++;
do { do {
ttype(luaD_stack.top++) = LUA_T_NIL; ttype(LL->stack.top++) = LUA_T_NIL;
} while (aux--); } while (aux--);
break; break;
@ -303,9 +305,9 @@ StkId luaV_execute (Closure *cl, StkId base)
case PUSHNUMBER0: case PUSHNUMBER1: case PUSHNUMBER2: case PUSHNUMBER0: case PUSHNUMBER1: case PUSHNUMBER2:
aux -= PUSHNUMBER0; aux -= PUSHNUMBER0;
pushnumber: pushnumber:
ttype(luaD_stack.top) = LUA_T_NUMBER; ttype(LL->stack.top) = LUA_T_NUMBER;
nvalue(luaD_stack.top) = aux; nvalue(LL->stack.top) = aux;
luaD_stack.top++; LL->stack.top++;
break; break;
case PUSHLOCAL: case PUSHLOCAL:
@ -315,7 +317,7 @@ StkId luaV_execute (Closure *cl, StkId base)
case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
aux -= PUSHLOCAL0; aux -= PUSHLOCAL0;
pushlocal: pushlocal:
*luaD_stack.top++ = *((luaD_stack.stack+base) + aux); *LL->stack.top++ = *((LL->stack.stack+base) + aux);
break; break;
case GETGLOBALW: case GETGLOBALW:
@ -345,7 +347,7 @@ StkId luaV_execute (Closure *cl, StkId base)
case GETDOTTED4: case GETDOTTED5: case GETDOTTED6: case GETDOTTED7: case GETDOTTED4: case GETDOTTED5: case GETDOTTED6: case GETDOTTED7:
aux -= GETDOTTED0; aux -= GETDOTTED0;
getdotted: getdotted:
*luaD_stack.top++ = consts[aux]; *LL->stack.top++ = consts[aux];
luaV_gettable(); luaV_gettable();
break; break;
@ -355,10 +357,10 @@ StkId luaV_execute (Closure *cl, StkId base)
case PUSHSELF: case PUSHSELF:
aux = *pc++; aux = *pc++;
pushself: { pushself: {
TObject receiver = *(luaD_stack.top-1); TObject receiver = *(LL->stack.top-1);
*luaD_stack.top++ = consts[aux]; *LL->stack.top++ = consts[aux];
luaV_gettable(); luaV_gettable();
*luaD_stack.top++ = receiver; *LL->stack.top++ = receiver;
break; break;
} }
@ -373,7 +375,7 @@ StkId luaV_execute (Closure *cl, StkId base)
case PUSHCONSTANT6: case PUSHCONSTANT7: case PUSHCONSTANT6: case PUSHCONSTANT7:
aux -= PUSHCONSTANT0; aux -= PUSHCONSTANT0;
pushconstant: pushconstant:
*luaD_stack.top++ = consts[aux]; *LL->stack.top++ = consts[aux];
break; break;
case PUSHUPVALUE: case PUSHUPVALUE:
@ -382,7 +384,7 @@ StkId luaV_execute (Closure *cl, StkId base)
case PUSHUPVALUE0: case PUSHUPVALUE1: case PUSHUPVALUE0: case PUSHUPVALUE1:
aux -= PUSHUPVALUE0; aux -= PUSHUPVALUE0;
pushupvalue: pushupvalue:
*luaD_stack.top++ = cl->consts[aux+1]; *LL->stack.top++ = cl->consts[aux+1];
break; break;
case SETLOCAL: case SETLOCAL:
@ -392,7 +394,7 @@ StkId luaV_execute (Closure *cl, StkId base)
case SETLOCAL4: case SETLOCAL5: case SETLOCAL6: case SETLOCAL7: case SETLOCAL4: case SETLOCAL5: case SETLOCAL6: case SETLOCAL7:
aux -= SETLOCAL0; aux -= SETLOCAL0;
setlocal: setlocal:
*((luaD_stack.stack+base) + aux) = *(--luaD_stack.top); *((LL->stack.stack+base) + aux) = *(--LL->stack.top);
break; break;
case SETGLOBALW: case SETGLOBALW:
@ -409,11 +411,11 @@ StkId luaV_execute (Closure *cl, StkId base)
break; break;
case SETTABLE0: case SETTABLE0:
luaV_settable(luaD_stack.top-3, 1); luaV_settable(LL->stack.top-3, 1);
break; break;
case SETTABLE: case SETTABLE:
luaV_settable(luaD_stack.top-3-(*pc++), 2); luaV_settable(LL->stack.top-3-(*pc++), 2);
break; break;
case SETLISTW: case SETLISTW:
@ -426,12 +428,12 @@ StkId luaV_execute (Closure *cl, StkId base)
aux = 0; aux = 0;
setlist: { setlist: {
int n = *(pc++); int n = *(pc++);
TObject *arr = luaD_stack.top-n-1; TObject *arr = LL->stack.top-n-1;
for (; n; n--) { for (; n; n--) {
ttype(luaD_stack.top) = LUA_T_NUMBER; ttype(LL->stack.top) = LUA_T_NUMBER;
nvalue(luaD_stack.top) = n+aux; nvalue(LL->stack.top) = n+aux;
*(luaH_set (avalue(arr), luaD_stack.top)) = *(luaD_stack.top-1); *(luaH_set (avalue(arr), LL->stack.top)) = *(LL->stack.top-1);
luaD_stack.top--; LL->stack.top--;
} }
break; break;
} }
@ -442,10 +444,10 @@ StkId luaV_execute (Closure *cl, StkId base)
case SETMAP: case SETMAP:
aux = *pc++; aux = *pc++;
setmap: { setmap: {
TObject *arr = luaD_stack.top-(2*aux)-3; TObject *arr = LL->stack.top-(2*aux)-3;
do { do {
*(luaH_set (avalue(arr), luaD_stack.top-2)) = *(luaD_stack.top-1); *(luaH_set (avalue(arr), LL->stack.top-2)) = *(LL->stack.top-1);
luaD_stack.top-=2; LL->stack.top-=2;
} while (aux--); } while (aux--);
break; break;
} }
@ -456,7 +458,7 @@ StkId luaV_execute (Closure *cl, StkId base)
case POP0: case POP1: case POP0: case POP1:
aux -= POP0; aux -= POP0;
pop: pop:
luaD_stack.top -= (aux+1); LL->stack.top -= (aux+1);
break; break;
case ARGS: case ARGS:
@ -478,17 +480,17 @@ StkId luaV_execute (Closure *cl, StkId base)
aux = *pc++; aux = *pc++;
createarray: createarray:
luaC_checkGC(); luaC_checkGC();
avalue(luaD_stack.top) = luaH_new(aux); avalue(LL->stack.top) = luaH_new(aux);
ttype(luaD_stack.top) = LUA_T_ARRAY; ttype(LL->stack.top) = LUA_T_ARRAY;
luaD_stack.top++; LL->stack.top++;
break; break;
case EQOP: case NEQOP: { case EQOP: case NEQOP: {
int res = luaO_equalObj(luaD_stack.top-2, luaD_stack.top-1); int res = luaO_equalObj(LL->stack.top-2, LL->stack.top-1);
luaD_stack.top--; LL->stack.top--;
if (aux == NEQOP) res = !res; if (aux == NEQOP) res = !res;
ttype(luaD_stack.top-1) = res ? LUA_T_NUMBER : LUA_T_NIL; ttype(LL->stack.top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
nvalue(luaD_stack.top-1) = 1; nvalue(LL->stack.top-1) = 1;
break; break;
} }
@ -509,49 +511,49 @@ StkId luaV_execute (Closure *cl, StkId base)
break; break;
case ADDOP: { case ADDOP: {
TObject *l = luaD_stack.top-2; TObject *l = LL->stack.top-2;
TObject *r = luaD_stack.top-1; TObject *r = LL->stack.top-1;
if (tonumber(r) || tonumber(l)) if (tonumber(r) || tonumber(l))
call_arith(IM_ADD); call_arith(IM_ADD);
else { else {
nvalue(l) += nvalue(r); nvalue(l) += nvalue(r);
--luaD_stack.top; --LL->stack.top;
} }
break; break;
} }
case SUBOP: { case SUBOP: {
TObject *l = luaD_stack.top-2; TObject *l = LL->stack.top-2;
TObject *r = luaD_stack.top-1; TObject *r = LL->stack.top-1;
if (tonumber(r) || tonumber(l)) if (tonumber(r) || tonumber(l))
call_arith(IM_SUB); call_arith(IM_SUB);
else { else {
nvalue(l) -= nvalue(r); nvalue(l) -= nvalue(r);
--luaD_stack.top; --LL->stack.top;
} }
break; break;
} }
case MULTOP: { case MULTOP: {
TObject *l = luaD_stack.top-2; TObject *l = LL->stack.top-2;
TObject *r = luaD_stack.top-1; TObject *r = LL->stack.top-1;
if (tonumber(r) || tonumber(l)) if (tonumber(r) || tonumber(l))
call_arith(IM_MUL); call_arith(IM_MUL);
else { else {
nvalue(l) *= nvalue(r); nvalue(l) *= nvalue(r);
--luaD_stack.top; --LL->stack.top;
} }
break; break;
} }
case DIVOP: { case DIVOP: {
TObject *l = luaD_stack.top-2; TObject *l = LL->stack.top-2;
TObject *r = luaD_stack.top-1; TObject *r = LL->stack.top-1;
if (tonumber(r) || tonumber(l)) if (tonumber(r) || tonumber(l))
call_arith(IM_DIV); call_arith(IM_DIV);
else { else {
nvalue(l) /= nvalue(r); nvalue(l) /= nvalue(r);
--luaD_stack.top; --LL->stack.top;
} }
break; break;
} }
@ -561,32 +563,32 @@ StkId luaV_execute (Closure *cl, StkId base)
break; break;
case CONCOP: { case CONCOP: {
TObject *l = luaD_stack.top-2; TObject *l = LL->stack.top-2;
TObject *r = luaD_stack.top-1; TObject *r = LL->stack.top-1;
if (tostring(l) || tostring(r)) if (tostring(l) || tostring(r))
call_binTM(IM_CONCAT, "unexpected type for concatenation"); call_binTM(IM_CONCAT, "unexpected type for concatenation");
else { else {
tsvalue(l) = strconc(svalue(l), svalue(r)); tsvalue(l) = strconc(svalue(l), svalue(r));
--luaD_stack.top; --LL->stack.top;
} }
luaC_checkGC(); luaC_checkGC();
break; break;
} }
case MINUSOP: case MINUSOP:
if (tonumber(luaD_stack.top-1)) { if (tonumber(LL->stack.top-1)) {
ttype(luaD_stack.top) = LUA_T_NIL; ttype(LL->stack.top) = LUA_T_NIL;
luaD_stack.top++; LL->stack.top++;
call_arith(IM_UNM); call_arith(IM_UNM);
} }
else else
nvalue(luaD_stack.top-1) = - nvalue(luaD_stack.top-1); nvalue(LL->stack.top-1) = - nvalue(LL->stack.top-1);
break; break;
case NOTOP: case NOTOP:
ttype(luaD_stack.top-1) = ttype(LL->stack.top-1) =
(ttype(luaD_stack.top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL; (ttype(LL->stack.top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
nvalue(luaD_stack.top-1) = 1; nvalue(LL->stack.top-1) = 1;
break; break;
case ONTJMPW: case ONTJMPW:
@ -595,8 +597,8 @@ StkId luaV_execute (Closure *cl, StkId base)
case ONTJMP: case ONTJMP:
aux = *pc++; aux = *pc++;
ontjmp: ontjmp:
if (ttype(luaD_stack.top-1) != LUA_T_NIL) pc += aux; if (ttype(LL->stack.top-1) != LUA_T_NIL) pc += aux;
else luaD_stack.top--; else LL->stack.top--;
break; break;
case ONFJMPW: case ONFJMPW:
@ -605,8 +607,8 @@ StkId luaV_execute (Closure *cl, StkId base)
case ONFJMP: case ONFJMP:
aux = *pc++; aux = *pc++;
onfjmp: onfjmp:
if (ttype(luaD_stack.top-1) == LUA_T_NIL) pc += aux; if (ttype(LL->stack.top-1) == LUA_T_NIL) pc += aux;
else luaD_stack.top--; else LL->stack.top--;
break; break;
case JMPW: case JMPW:
@ -624,7 +626,7 @@ StkId luaV_execute (Closure *cl, StkId base)
case IFFJMP: case IFFJMP:
aux = *pc++; aux = *pc++;
iffjmp: iffjmp:
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc += aux; if (ttype(--LL->stack.top) == LUA_T_NIL) pc += aux;
break; break;
case IFTUPJMPW: case IFTUPJMPW:
@ -633,7 +635,7 @@ StkId luaV_execute (Closure *cl, StkId base)
case IFTUPJMP: case IFTUPJMP:
aux = *pc++; aux = *pc++;
iftupjmp: iftupjmp:
if (ttype(--luaD_stack.top) != LUA_T_NIL) pc -= aux; if (ttype(--LL->stack.top) != LUA_T_NIL) pc -= aux;
break; break;
case IFFUPJMPW: case IFFUPJMPW:
@ -642,7 +644,7 @@ StkId luaV_execute (Closure *cl, StkId base)
case IFFUPJMP: case IFFUPJMP:
aux = *pc++; aux = *pc++;
iffupjmp: iffupjmp:
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc -= aux; if (ttype(--LL->stack.top) == LUA_T_NIL) pc -= aux;
break; break;
case CLOSURE: case CLOSURE:
@ -661,13 +663,13 @@ StkId luaV_execute (Closure *cl, StkId base)
case CALLFUNC0: case CALLFUNC1: case CALLFUNC0: case CALLFUNC1:
aux -= CALLFUNC0; aux -= CALLFUNC0;
callfunc: { callfunc: {
StkId newBase = (luaD_stack.top-luaD_stack.stack)-(*pc++); StkId newBase = (LL->stack.top-LL->stack.stack)-(*pc++);
luaD_call(newBase, aux); luaD_call(newBase, aux);
break; break;
} }
case ENDCODE: case ENDCODE:
luaD_stack.top = luaD_stack.stack + base; LL->stack.top = LL->stack.stack + base;
/* goes through */ /* goes through */
case RETCODE: case RETCODE:
if (lua_callhook) if (lua_callhook)
@ -680,13 +682,13 @@ StkId luaV_execute (Closure *cl, StkId base)
case SETLINE: case SETLINE:
aux = *pc++; aux = *pc++;
setline: setline:
if ((luaD_stack.stack+base-1)->ttype != LUA_T_LINE) { if ((LL->stack.stack+base-1)->ttype != LUA_T_LINE) {
/* open space for LINE value */ /* open space for LINE value */
luaD_openstack((luaD_stack.top-luaD_stack.stack)-base); luaD_openstack((LL->stack.top-LL->stack.stack)-base);
base++; base++;
(luaD_stack.stack+base-1)->ttype = LUA_T_LINE; (LL->stack.stack+base-1)->ttype = LUA_T_LINE;
} }
(luaD_stack.stack+base-1)->value.i = aux; (LL->stack.stack+base-1)->value.i = aux;
if (lua_linehook) if (lua_linehook)
luaD_lineHook(aux); luaD_lineHook(aux);
break; break;

View File

@ -1,5 +1,5 @@
# #
## $Id: makefile,v 1.4 1997/10/24 17:17:24 roberto Exp roberto $ ## $Id: makefile,v 1.5 1997/11/07 15:09:49 roberto Exp roberto $
## Makefile ## Makefile
## See Copyright Notice in lua.h ## See Copyright Notice in lua.h
# #
@ -18,7 +18,7 @@
# define LUA_COMPAT2_5=0 if yous system does not need to be compatible with # define LUA_COMPAT2_5=0 if yous system does not need to be compatible with
# version 2.5 (or older) # version 2.5 (or older)
CONFIG = -DPOPEN -D_POSIX_SOURCE CONFIG = -DPOPEN -D_POSIX_SOURCE -DLUA_COMPAT2_5=0
#CONFIG = -DLUA_COMPAT2_5=0 -DOLD_ANSI -DDEBUG #CONFIG = -DLUA_COMPAT2_5=0 -DOLD_ANSI -DDEBUG
@ -43,6 +43,7 @@ LUAOBJS = \
llex.o \ llex.o \
lmem.o \ lmem.o \
lobject.o \ lobject.o \
lstate.o \
lstx.o \ lstx.o \
lstring.o \ lstring.o \
ltable.o \ ltable.o \
@ -90,32 +91,34 @@ clear :
%.c : RCS/%.c,v %.c : RCS/%.c,v
co $@ co $@
lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h ldo.h \
lfunc.h lgc.h llex.h lzio.h lmem.h lstring.h ltable.h ltm.h \ lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h ldo.h lstate.h lfunc.h \
luadebug.h lvm.h lgc.h lmem.h lstring.h ltable.h ltm.h luadebug.h lvm.h
lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h
lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \ lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \
ldo.h lfunc.h lmem.h lstring.h ltable.h ltm.h ldo.h lstate.h lfunc.h lmem.h lstring.h ltable.h ltm.h
ldo.o: ldo.c ldo.h lobject.h lua.h lfunc.h lgc.h lmem.h lparser.h \ ldo.o: ldo.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
lzio.h ltm.h luadebug.h lundump.h lvm.h lparser.h lzio.h ltm.h luadebug.h lundump.h lvm.h
lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h lstate.h
lgc.o: lgc.c ldo.h lobject.h lua.h lfunc.h lgc.h lmem.h lstring.h \ lgc.o: lgc.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
ltable.h ltm.h lstring.h ltable.h ltm.h
liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h
llex.o: llex.c llex.h lobject.h lua.h lzio.h lmem.h lparser.h \ llex.o: llex.c llex.h lobject.h lua.h lzio.h lmem.h lparser.h lstate.h \
lstring.h lstx.h luadebug.h lstring.h lstx.h luadebug.h
lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h
lmem.o: lmem.c lmem.h lua.h lmem.o: lmem.c lmem.h lstate.h lobject.h lua.h
lobject.o: lobject.c lobject.h lua.h lobject.o: lobject.c lobject.h lua.h
lstring.o: lstring.c lmem.h lobject.h lua.h lstring.h lstate.o: lstate.c lbuiltin.h ldo.h lobject.h lua.h lstate.h llex.h \
lzio.h lmem.h lstring.h ltable.h ltm.h
lstring.o: lstring.c lmem.h lobject.h lua.h lstate.h lstring.h
lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h
lstx.o: lstx.c lauxlib.h lua.h ldo.h lobject.h lfunc.h llex.h lzio.h \ lstx.o: lstx.c lauxlib.h lua.h ldo.h lobject.h lstate.h lfunc.h llex.h \
lmem.h lopcodes.h lparser.h lstring.h luadebug.h lzio.h lmem.h lopcodes.h lparser.h lstring.h luadebug.h
ltable.o: ltable.c lauxlib.h lua.h lmem.h lobject.h ltable.h ltable.o: ltable.c lauxlib.h lua.h lmem.h lobject.h lstate.h ltable.h
ltm.o: ltm.c lauxlib.h lua.h ldo.h lobject.h lmem.h ltm.h lapi.h ltm.o: ltm.c lauxlib.h lua.h lmem.h lobject.h lstate.h ltm.h lapi.h
lua.o: lua.c lua.h luadebug.h lualib.h lua.o: lua.c lua.h luadebug.h lualib.h
lundump.o: lundump.c lauxlib.h lua.h lfunc.h lobject.h lmem.h \ lundump.o: lundump.c lauxlib.h lua.h lfunc.h lobject.h lmem.h \
lstring.h lundump.h lzio.h lstring.h lundump.h lzio.h
lvm.o: lvm.c lauxlib.h lua.h ldo.h lobject.h lfunc.h lgc.h lmem.h \ lvm.o: lvm.c lauxlib.h lua.h ldo.h lobject.h lstate.h lfunc.h lgc.h \
lopcodes.h lstring.h ltable.h ltm.h luadebug.h lvm.h lmem.h lopcodes.h lstring.h ltable.h ltm.h luadebug.h lvm.h
lzio.o: lzio.c lzio.h lzio.o: lzio.c lzio.h