Просмотр исходного кода

first implementation of centralized global state.

Roberto Ierusalimschy 28 лет назад
Родитель
Сommit
592a3f289b
25 измененных файлов с 781 добавлено и 828 удалено
  1. 82 97
      lapi.c
  2. 15 14
      lbuiltin.c
  3. 81 89
      ldo.c
  4. 7 25
      ldo.h
  5. 8 10
      lfunc.c
  6. 1 4
      lfunc.h
  7. 33 41
      lgc.c
  8. 1 3
      lgc.h
  9. 25 12
      liolib.c
  10. 178 194
      llex.c
  11. 30 2
      llex.h
  12. 8 10
      lmem.c
  13. 1 4
      lobject.c
  14. 1 2
      lobject.h
  15. 16 26
      lstring.c
  16. 1 3
      lstring.h
  17. 6 8
      ltable.c
  18. 1 4
      ltable.h
  19. 17 21
      ltm.c
  20. 5 4
      ltm.h
  21. 2 1
      lua.c
  22. 3 1
      lua.h
  23. 123 122
      lua.stx
  24. 113 111
      lvm.c
  25. 23 20
      makefile

+ 82 - 97
lapi.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -10,13 +10,12 @@
 
 #include "lapi.h"
 #include "lauxlib.h"
-#include "lbuiltin.h"
 #include "ldo.h"
 #include "lfunc.h"
 #include "lgc.h"
-#include "llex.h"
 #include "lmem.h"
 #include "lobject.h"
+#include "lstate.h"
 #include "lstring.h"
 #include "ltable.h"
 #include "ltm.h"
@@ -38,41 +37,41 @@ TObject *luaA_Address (lua_Object o)
 
 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;
 }
 
 
 int luaA_passresults (void)
 {
-  luaD_checkstack(luaD_Cstack.num);
-  memcpy(luaD_stack.top, luaD_Cstack.lua2C+luaD_stack.stack,
-         luaD_Cstack.num*sizeof(TObject));
-  luaD_stack.top += luaD_Cstack.num;
-  return luaD_Cstack.num;
+  luaD_checkstack(L->Cstack.num);
+  memcpy(L->stack.top, L->Cstack.lua2C+L->stack.stack,
+         L->Cstack.num*sizeof(TObject));
+  L->stack.top += L->Cstack.num;
+  return L->Cstack.num;
 }
 
 
 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");
 }
 
 
 static lua_Object put_luaObject (TObject *o)
 {
-  luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base);
-  luaD_stack.stack[luaD_Cstack.base++] = *o;
-  return luaD_Cstack.base;  /* this is +1 real position (see Ref) */
+  luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
+  L->stack.stack[L->Cstack.base++] = *o;
+  return L->Cstack.base;  /* this is +1 real position (see Ref) */
 }
 
 
 static lua_Object put_luaObjectonTop (void)
 {
-  luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base);
-  luaD_stack.stack[luaD_Cstack.base++] = *(--luaD_stack.top);
-  return luaD_Cstack.base;  /* this is +1 real position (see Ref) */
+  luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
+  L->stack.stack[L->Cstack.base++] = *(--L->stack.top);
+  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)
 {
-  if (number <= 0 || number > luaD_Cstack.num) return LUA_NOOBJECT;
-  /* Ref(luaD_stack.stack+(luaD_Cstack.lua2C+number-1)) ==
-     luaD_stack.stack+(luaD_Cstack.lua2C+number-1)-luaD_stack.stack+1 == */
-  return luaD_Cstack.lua2C+number;
+  if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
+  /* Ref(L->stack.stack+(L->Cstack.lua2C+number-1)) ==
+     L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */
+  return L->Cstack.lua2C+number;
 }
 
 
 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)
     return LUA_NOOBJECT;
 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;
   return put_luaObjectonTop();
 }
@@ -113,8 +112,8 @@ int lua_callfunction (lua_Object function)
   if (function == LUA_NOOBJECT)
     return 1;
   else {
-    luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base);
-    luaD_stack.stack[luaD_Cstack.base] = *Address(function);
+    luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
+    L->stack.stack[L->Cstack.base] = *Address(function);
     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)
 {
   checkCparams(1);
-  luaT_settagmethod(tag, event, luaD_stack.top-1);
+  luaT_settagmethod(tag, event, L->stack.top-1);
   return put_luaObjectonTop();
 }
 
 
 lua_Object lua_seterrormethod (void)
 {
-  TObject temp = luaD_errorim;
+  TObject temp = L->errorim;
   checkCparams(1);
-  luaD_errorim = *(--luaD_stack.top);
+  L->errorim = *(--L->stack.top);
   return put_luaObject(&temp);
 }
 
@@ -154,15 +153,15 @@ lua_Object lua_gettable (void)
 lua_Object lua_rawgettable (void)
 {
   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");
   else {
-    TObject *h = luaH_get(avalue(luaD_stack.top-2), luaD_stack.top-1);
-    --luaD_stack.top;
+    TObject *h = luaH_get(avalue(L->stack.top-2), L->stack.top-1);
+    --L->stack.top;
     if (h != NULL)
-      *(luaD_stack.top-1) = *h;
+      *(L->stack.top-1) = *h;
     else
-      ttype(luaD_stack.top-1) = LUA_T_NIL;
+      ttype(L->stack.top-1) = LUA_T_NIL;
   }
   return put_luaObjectonTop();
 }
@@ -171,14 +170,14 @@ lua_Object lua_rawgettable (void)
 void lua_settable (void)
 {
   checkCparams(3);
-  luaV_settable(luaD_stack.top-3, 1);
+  luaV_settable(L->stack.top-3, 1);
 }
 
 
 void lua_rawsettable (void)
 {
   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)
 {
   luaD_checkstack(2);  /* may need that to call T.M. */
@@ -219,7 +224,7 @@ void lua_rawsetglobal (char *name)
 {
   TaggedString *ts = luaS_new(name);
   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)
 {
-  ttype(luaD_stack.top) = LUA_T_NIL;
+  ttype(L->stack.top) = LUA_T_NIL;
   incr_top;
 }
 
 void lua_pushnumber (real n)
 {
-  ttype(luaD_stack.top) = LUA_T_NUMBER;
-  nvalue(luaD_stack.top) = n;
+  ttype(L->stack.top) = LUA_T_NUMBER;
+  nvalue(L->stack.top) = n;
   incr_top;
 }
 
 void lua_pushstring (char *s)
 {
   if (s == NULL)
-    ttype(luaD_stack.top) = LUA_T_NIL;
+    ttype(L->stack.top) = LUA_T_NIL;
   else {
-    tsvalue(luaD_stack.top) = luaS_new(s);
-    ttype(luaD_stack.top) = LUA_T_STRING;
+    tsvalue(L->stack.top) = luaS_new(s);
+    ttype(L->stack.top) = LUA_T_STRING;
   }
   incr_top;
   luaC_checkGC();
@@ -319,13 +324,13 @@ void lua_pushstring (char *s)
 void lua_pushCclosure (lua_CFunction fn, int n)
 {
   if (fn == NULL) {
-    ttype(luaD_stack.top) = LUA_T_NIL;
+    ttype(L->stack.top) = LUA_T_NIL;
     incr_top;
   }
   else {
     checkCparams(n);
-    ttype(luaD_stack.top) = LUA_T_CPROTO;
-    fvalue(luaD_stack.top) = fn;
+    ttype(L->stack.top) = LUA_T_CPROTO;
+    fvalue(L->stack.top) = fn;
     incr_top;
     luaV_closure(n);
   }
@@ -335,15 +340,15 @@ void lua_pushusertag (void *u, int tag)
 {
   if (tag < 0 && tag != LUA_ANYTAG)
     luaT_realtag(tag);  /* error if tag is not valid */
-  tsvalue(luaD_stack.top) = luaS_createudata(u, tag);
-  ttype(luaD_stack.top) = LUA_T_USERDATA;
+  tsvalue(L->stack.top) = luaS_createudata(u, tag);
+  ttype(L->stack.top) = LUA_T_USERDATA;
   incr_top;
   luaC_checkGC();
 }
 
 void luaA_pushobject (TObject *o)
 {
-  *luaD_stack.top = *o;
+  *L->stack.top = *o;
   incr_top;
 }
 
@@ -351,9 +356,9 @@ void lua_pushobject (lua_Object o)
 {
   if (o == LUA_NOOBJECT)
     lua_error("API error - attempt to push a NOOBJECT");
-  *luaD_stack.top = *Address(o);
-  if (ttype(luaD_stack.top) == LUA_T_MARK)
-    ttype(luaD_stack.top) = LUA_T_FUNCTION;
+  *L->stack.top = *Address(o);
+  if (ttype(L->stack.top) == LUA_T_MARK)
+    ttype(L->stack.top) = LUA_T_FUNCTION;
   incr_top;
 }
 
@@ -376,18 +381,18 @@ void lua_settag (int tag)
 {
   checkCparams(1);
   luaT_realtag(tag);
-  switch (ttype(luaD_stack.top-1)) {
+  switch (ttype(L->stack.top-1)) {
     case LUA_T_ARRAY:
-      (luaD_stack.top-1)->value.a->htag = tag;
+      (L->stack.top-1)->value.a->htag = tag;
       break;
     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;
     default:
       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)
 {
   StkId i;
-  for (i = (luaD_stack.top-1)-luaD_stack.stack; i>=0; i--)
-    if (luaD_stack.stack[i].ttype == LUA_T_MARK)
+  for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
+    if (L->stack.stack[i].ttype == LUA_T_MARK)
       if (level-- == 0)
-        return Ref(luaD_stack.stack+i);
+        return Ref(L->stack.stack+i);
   return LUA_NOOBJECT;
 }
 
@@ -417,7 +422,7 @@ lua_Function lua_stackedfunction (int level)
 int lua_currentline (lua_Function 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;
   char *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
   checkCparams(1);
-  --luaD_stack.top;
+  --L->stack.top;
   if (name) {
     /* if "name", there must be a LUA_T_LINE */
     /* 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;
   }
   else
@@ -478,19 +483,18 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
 }
 
 
-static TObject *functofind;
 static int checkfunc (TObject *o)
 {
     return (o->ttype == LUA_T_FUNCTION) &&
-           ((functofind->ttype == LUA_T_FUNCTION) ||
-            (functofind->ttype == LUA_T_MARK)) &&
-           (functofind->value.cl == o->value.cl);
+           ((L->functofind->ttype == LUA_T_FUNCTION) ||
+            (L->functofind->ttype == LUA_T_MARK)) &&
+           (L->functofind->value.cl == o->value.cl);
 }
 
 
 char *lua_getobjname (lua_Object o, char **name)
 { /* try to find a name for given function */
-  functofind = Address(o);
+  L->functofind = Address(o);
   if ((*name = luaT_travtagmethods(checkfunc)) != NULL)
     return "tag-method";
   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)
 {
-  if (numCblocks >= MAX_C_BLOCKS)
+  if (L->numCblocks >= MAX_C_BLOCKS)
     lua_error("`lua_beginblock': too many nested blocks");
-  Cblocks[numCblocks] = luaD_Cstack;
-  numCblocks++;
+  L->Cblocks[L->numCblocks] = L->Cstack;
+  L->numCblocks++;
 }
 
 void lua_endblock (void)
 {
-  --numCblocks;
-  luaD_Cstack = Cblocks[numCblocks];
-  luaD_adjusttop(luaD_Cstack.base);
+  --L->numCblocks;
+  L->Cstack = L->Cblocks[L->numCblocks];
+  luaD_adjusttop(L->Cstack.base);
 }
 
 
 
-
-
 int lua_ref (int lock)
 {
   int ref;
   checkCparams(1);
-  ref = luaC_ref(luaD_stack.top-1, lock);
-  luaD_stack.top--;
+  ref = luaC_ref(L->stack.top-1, lock);
+  L->stack.top--;
   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
 /*
@@ -567,11 +552,11 @@ void lua_open (void)
 
 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_stack.stack[base].ttype = LUA_T_CPROTO;
-  luaD_stack.stack[base].value.f = f;
-  luaF_simpleclosure(luaD_stack.stack+base);
+  L->stack.stack[base].ttype = LUA_T_CPROTO;
+  L->stack.stack[base].value.f = f;
+  luaF_simpleclosure(L->stack.stack+base);
   luaD_call(base+1, nResults);
 }
 

+ 15 - 14
lbuiltin.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -15,6 +15,7 @@
 #include "lfunc.h"
 #include "lmem.h"
 #include "lobject.h"
+#include "lstate.h"
 #include "lstring.h"
 #include "ltable.h"
 #include "ltm.h"
@@ -51,7 +52,7 @@ static void nextvar (void)
   TObject *o = luaA_Address(luaL_nonnullarg(1));
   TaggedString *g;
   if (ttype(o) == LUA_T_NIL)
-    g = (TaggedString *)luaS_root.next;
+    g = (TaggedString *)L->rootglobal.next;
   else {
     luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
     g = tsvalue(o);
@@ -72,21 +73,21 @@ static void foreachvar (void)
 {
   TObject f = *luaA_Address(functionarg(1));
   GCnode *g;
-  StkId name = luaD_Cstack.base++;  /* place to keep var name (to avoid GC) */
-  ttype(luaD_stack.stack+name) = LUA_T_NIL;
-  luaD_stack.top++;
-  for (g = luaS_root.next; g; g = g->next) {
+  StkId name = L->Cstack.base++;  /* place to keep var name (to avoid GC) */
+  ttype(L->stack.stack+name) = LUA_T_NIL;
+  L->stack.top++;
+  for (g = L->rootglobal.next; g; g = g->next) {
     TaggedString *s = (TaggedString *)g;
     if (s->u.globalval.ttype != LUA_T_NIL) {
-      ttype(luaD_stack.stack+name) = LUA_T_STRING;
-      tsvalue(luaD_stack.stack+name) = s;  /* keep s on stack to avoid GC */
+      ttype(L->stack.stack+name) = LUA_T_STRING;
+      tsvalue(L->stack.stack+name) = s;  /* keep s on stack to avoid GC */
       luaA_pushobject(&f);
       pushstring(s);
       luaA_pushobject(&s->u.globalval);
-      luaD_call((luaD_stack.top-luaD_stack.stack)-2, 1);
-      if (ttype(luaD_stack.top-1) != LUA_T_NIL)
+      luaD_call((L->stack.top-L->stack.stack)-2, 1);
+      if (ttype(L->stack.top-1) != LUA_T_NIL)
         return;
-      luaD_stack.top--;
+      L->stack.top--;
     }
   }
 }
@@ -115,10 +116,10 @@ static void foreach (void)
       luaA_pushobject(&f);
       luaA_pushobject(ref(nd));
       luaA_pushobject(val(nd));
-      luaD_call((luaD_stack.top-luaD_stack.stack)-2, 1);
-      if (ttype(luaD_stack.top-1) != LUA_T_NIL)
+      luaD_call((L->stack.top-L->stack.stack)-2, 1);
+      if (ttype(L->stack.top-1) != LUA_T_NIL)
         return;
-      luaD_stack.top--;
+      L->stack.top--;
     }
   }
 }

+ 81 - 89
ldo.c

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

+ 7 - 25
ldo.h

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -9,43 +9,25 @@
 
 
 #include "lobject.h"
+#include "lstate.h"
 
 
-typedef int StkId;  /* index to luaD_stack.stack elements */
-
 #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.
-** 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); \
-                   luaD_stack.top++; }
+#define incr_top { if (L->stack.top >= L->stack.last) luaD_checkstack(1); \
+                   L->stack.top++; }
 
 
 /* macros to convert from lua_Object to (TObject *) and back */
 
-#define Address(lo)     ((lo)+luaD_stack.stack-1)
-#define Ref(st)         ((st)-luaD_stack.stack+1)
+#define Address(lo)     ((lo)+L->stack.stack-1)
+#define Ref(st)         ((st)-L->stack.stack+1)
 
 
 void luaD_init (void);

+ 8 - 10
lfunc.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -9,20 +9,18 @@
 
 #include "lfunc.h"
 #include "lmem.h"
+#include "lstate.h"
 
 #define gcsizeproto(p)	5
 #define gcsizeclosure(c) 1
 
-GCnode luaF_root = {NULL, 0};
-GCnode luaF_rootcl = {NULL, 0};
-
 
 
 Closure *luaF_newclosure (int nelems)
 {
   Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject));
-  luaO_insertlist(&luaF_rootcl, (GCnode *)c);
-  luaO_nblocks += gcsizeclosure(c);
+  luaO_insertlist(&(L->rootcl), (GCnode *)c);
+  L->nblocks += gcsizeclosure(c);
   c->nelems = nelems;
   return c;
 }
@@ -46,8 +44,8 @@ TProtoFunc *luaF_newproto (void)
   f->consts = NULL;
   f->nconsts = 0;
   f->locvars = NULL;
-  luaO_insertlist(&luaF_root, (GCnode *)f);
-  luaO_nblocks += gcsizeproto(f);
+  luaO_insertlist(&(L->rootproto), (GCnode *)f);
+  L->nblocks += gcsizeproto(f);
   return f;
 }
 
@@ -66,7 +64,7 @@ void luaF_freeproto (TProtoFunc *l)
 {
   while (l) {
     TProtoFunc *next = (TProtoFunc *)l->head.next;
-    luaO_nblocks -= gcsizeproto(l);
+    L->nblocks -= gcsizeproto(l);
     freefunc(l);
     l = next;
   }
@@ -77,7 +75,7 @@ void luaF_freeclosure (Closure *l)
 {
   while (l) {
     Closure *next = (Closure *)l->head.next;
-    luaO_nblocks -= gcsizeclosure(l);
+    L->nblocks -= gcsizeclosure(l);
     luaM_free(l);
     l = next;
   }

+ 1 - 4
lfunc.h

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -11,9 +11,6 @@
 #include "lobject.h"
 
 
-extern GCnode luaF_root;
-extern GCnode luaF_rootcl;
-
 
 TProtoFunc *luaF_newproto (void);
 Closure *luaF_newclosure (int nelems);

+ 33 - 41
lgc.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -10,6 +10,7 @@
 #include "lgc.h"
 #include "lmem.h"
 #include "lobject.h"
+#include "lstate.h"
 #include "lstring.h"
 #include "ltable.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)
 {
@@ -40,18 +35,19 @@ int luaC_ref (TObject *o, int lock)
   if (ttype(o) == LUA_T_NIL)
     ref = -1;   /* special ref for nil */
   else {
-    for (ref=0; ref<refSize; ref++)
-      if (refArray[ref].status == FREE)
+    for (ref=0; ref<L->refSize; ref++)
+      if (L->refArray[ref].status == FREE)
         goto found;
     /* no more empty spaces */ {
-      int oldSize = refSize;
-      refSize = luaM_growvector(&refArray, refSize, struct ref, refEM, MAX_WORD);
-      for (ref=oldSize; ref<refSize; ref++)
-        refArray[ref].status = FREE;
+      int oldSize = L->refSize;
+      L->refSize = luaM_growvector(&L->refArray, L->refSize, struct ref,
+                                   refEM, MAX_WORD);
+      for (ref=oldSize; ref<L->refSize; ref++)
+        L->refArray[ref].status = FREE;
       ref = oldSize;
     } found:
-    refArray[ref].o = *o;
-    refArray[ref].status = lock ? LOCK : HOLD;
+    L->refArray[ref].o = *o;
+    L->refArray[ref].status = lock ? LOCK : HOLD;
   }
   return ref;
 }
@@ -59,8 +55,8 @@ int luaC_ref (TObject *o, int lock)
 
 void lua_unref (int ref)
 {
-  if (ref >= 0 && ref < refSize)
-    refArray[ref].status = FREE;
+  if (ref >= 0 && ref < L->refSize)
+    L->refArray[ref].status = FREE;
 }
 
 
@@ -68,9 +64,9 @@ TObject* luaC_getref (int ref)
 {
   if (ref == -1)
     return &luaO_nilobject;
-  if (ref >= 0 && ref < refSize &&
-      (refArray[ref].status == LOCK || refArray[ref].status == HOLD))
-    return &refArray[ref].o;
+  if (ref >= 0 && ref < L->refSize &&
+      (L->refArray[ref].status == LOCK || L->refArray[ref].status == HOLD))
+    return &L->refArray[ref].o;
   else
     return NULL;
 }
@@ -79,9 +75,9 @@ TObject* luaC_getref (int ref)
 static void travlock (void)
 {
   int i;
-  for (i=0; i<refSize; i++)
-    if (refArray[i].status == LOCK)
-      markobject(&refArray[i].o);
+  for (i=0; i<L->refSize; i++)
+    if (L->refArray[i].status == LOCK)
+      markobject(&L->refArray[i].o);
 }
 
 
@@ -105,9 +101,9 @@ static int ismarked (TObject *o)
 static void invalidaterefs (void)
 {
   int i;
-  for (i=0; i<refSize; i++)
-    if (refArray[i].status == HOLD && !ismarked(&refArray[i].o))
-      refArray[i].status = COLLECTED;
+  for (i=0; i<L->refSize; i++)
+    if (L->refArray[i].status == HOLD && !ismarked(&L->refArray[i].o))
+      L->refArray[i].status = COLLECTED;
 }
 
 
@@ -210,7 +206,7 @@ static void hashmark (Hash *h)
 static void globalmark (void)
 {
   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) {
       markobject(&g->u.globalval);
       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)
 {
   luaD_travstack(markobject); /* mark stack objects */
   globalmark();  /* mark global variable values and names */
   travlock(); /* mark locked objects */
+  markobject(&L->globalbag);  /* mark elements in global bag */
   luaT_travtagmethods(markobject);  /* mark fallbacks */
 }
 
 
 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;
   TaggedString *freestr;
   TProtoFunc *freefunc;
@@ -264,10 +256,10 @@ long lua_collectgarbage (long limit)
   markall();
   invalidaterefs();
   freestr = luaS_collector();
-  freetable = (Hash *)listcollect(&luaH_root);
-  freefunc = (TProtoFunc *)listcollect(&luaF_root);
-  freeclos = (Closure *)listcollect(&luaF_rootcl);
-  luaC_threshold *= 4;  /* to avoid GC during GC */
+  freetable = (Hash *)listcollect(&(L->roottable));
+  freefunc = (TProtoFunc *)listcollect(&(L->rootproto));
+  freeclos = (Closure *)listcollect(&(L->rootcl));
+  L->GCthreshold *= 4;  /* to avoid GC during GC */
   hashcallIM(freetable);  /* GC tag methods for tables */
   strcallIM(freestr);  /* GC tag methods for userdata */
   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_freeclosure(freeclos);
   luaM_clearbuffer();
-  recovered = recovered-luaO_nblocks;
-/*printf("==total %ld  coletados %ld\n", luaO_nblocks+recovered, recovered);*/
-  luaC_threshold = (limit == 0) ? 2*luaO_nblocks : luaO_nblocks+limit;
+  recovered = recovered-L->nblocks;
+/*printf("==total %ld  coletados %ld\n", L->nblocks+recovered, recovered);*/
+  L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
   return recovered;
 }
 
 
 void luaC_checkGC (void)
 {
-  if (luaO_nblocks >= luaC_threshold)
+  if (L->nblocks >= L->GCthreshold)
     lua_collectgarbage(0);
 }
 

+ 1 - 3
lgc.h

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -11,8 +11,6 @@
 #include "lobject.h"
 
 
-extern unsigned long luaC_threshold;
-
 void luaC_checkGC (void);
 TObject* luaC_getref (int ref);
 int luaC_ref (TObject *o, int lock);

+ 25 - 12
liolib.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -41,8 +41,21 @@ int pclose();
 #endif
 
 
-int lua_tagio;
-static int closedtag;
+static void createtag (char *t)
+{
+  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)
@@ -59,9 +72,9 @@ static void pushresult (int i)
 static int ishandler (lua_Object f)
 {
   if (lua_isuserdata(f)) {
-    if (lua_tag(f) == closedtag)
+    if (lua_tag(f) == gettag("closedtag"))
       lua_error("trying to access a closed file");
-    return lua_tag(f) == lua_tagio;
+    return lua_tag(f) == gettag("tagio");
   }
   else return 0;
 }
@@ -94,13 +107,13 @@ static void closefile (char *name)
   if (pclose(f) == -1)
     fclose(f);
   lua_pushobject(lua_getglobal(name));
-  lua_settag(closedtag);
+  lua_settag(gettag("closedtag"));
 }
 
 
 static void setfile (FILE *f, char *name)
 {
-  lua_pushusertag(f, lua_tagio);
+  lua_pushusertag(f, gettag("tagio"));
   lua_setglobal(name);
 }
 
@@ -108,7 +121,7 @@ static void setfile (FILE *f, char *name)
 static void setreturn (FILE *f, char *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");
     current = stdin;
   }
-  else if (lua_tag(f) == lua_tagio)
+  else if (lua_tag(f) == gettag("tagio"))
     current = lua_getuserdata(f);
   else {
     char *s = luaL_check_string(1);
@@ -142,7 +155,7 @@ static void io_writeto (void)
     closefile("_OUTPUT");
     current = stdout;
   }
-  else if (lua_tag(f) == lua_tagio)
+  else if (lua_tag(f) == gettag("tagio"))
     current = lua_getuserdata(f);
   else {
     char *s = luaL_check_string(1);
@@ -373,8 +386,8 @@ static struct luaL_reg iolib[] = {
 void lua_iolibopen (void)
 {
   luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
-  lua_tagio = lua_newtag();
-  closedtag = lua_newtag();
+  createtag("iotag");
+  createtag("closedtag");
   setfile(stdin, "_INPUT");
   setfile(stdout, "_OUTPUT");
   setfile(stdin, "_STDIN");

+ 178 - 194
llex.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -12,35 +12,32 @@
 #include "lmem.h"
 #include "lobject.h"
 #include "lparser.h"
+#include "lstate.h"
 #include "lstring.h"
 #include "lstx.h"
 #include "luadebug.h"
 #include "lzio.h"
 
 
-static int current;  /* look ahead character */
-static ZIO *lex_z;
 
-
-int luaX_linenumber;
 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)
 {
-  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;
   for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
     TaggedString *ts = luaS_new(reserved[i].name);
@@ -49,47 +46,29 @@ void luaX_init (void)
 }
 
 
-
-#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)
+static void firstline (LexState *LL)
 {
-  int c = zgetc(lex_z);
+  int c = zgetc(LL->lex_z);
   if (c == '#')
-    while((c=zgetc(lex_z)) != '\n' && c != EOZ) /* skip first line */;
-  zungetc(lex_z);
+    while((c=zgetc(LL->lex_z)) != '\n' && c != EOZ) /* skip first line */;
+  zungetc(LL->lex_z);
 }
 
 
 void luaX_setinput (ZIO *z)
 {
-  current = '\n';
-  luaX_linenumber = 0;
-  iflevel = 0;
-  ifstate[0].skip = 0;
-  ifstate[0].elsepart = 1;  /* to avoid a free $else */
-  lex_z = z;
-  firstline();
-  textbuff.buffsize = 20;
-  textbuff.text = luaM_buffer(textbuff.buffsize);
+  LexState *LL = L->lexstate;
+  LL->current = '\n';
+  LL->linelasttoken = 0;
+  LL->lastline = 0;
+  LL->linenumber = 0;
+  LL->iflevel = 0;
+  LL->ifstate[0].skip = 0;
+  LL->ifstate[0].elsepart = 1;  /* to avoid a free $else */
+  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
 
-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;
-  skipspace();
-  while (isalnum(current) || current == '_') {
+  skipspace(LL);
+  while (isalnum(LL->current) || LL->current == '_') {
     if (i >= PRAGMASIZE) {
       buff[PRAGMASIZE] = 0;
       luaY_syntaxerror("pragma too long", buff);
     }
-    buff[i++] = current;
-    next();
+    buff[i++] = LL->current;
+    next(LL);
   }
   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) {
-    if (current == '\n')
-      inclinenumber();
-    else if (current == EOZ)
+  while (LL->ifstate[LL->iflevel].skip) {
+    if (LL->current == '\n')
+      inclinenumber(LL);
+    else if (LL->current == EOZ)
       luaY_syntaxerror("input ends inside a $if", "");
-    else next();
+    else next(LL);
   }
 }
 
 
-static void inclinenumber (void)
+static void inclinenumber (LexState *LL)
 {
   static char *pragmas [] =
     {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
-  next();  /* skip '\n' */
-  ++luaX_linenumber;
-  if (current == '$') {  /* is a pragma? */
+  next(LL);  /* skip '\n' */
+  ++LL->linenumber;
+  if (LL->current == '$') {  /* is a pragma? */
     char buff[PRAGMASIZE+1];
     int ifnot = 0;
-    int skip = ifstate[iflevel].skip;
-    next();  /* skip $ */
-    readname(buff);
+    int skip = LL->ifstate[LL->iflevel].skip;
+    next(LL);  /* skip $ */
+    readname(LL, buff);
     switch (luaO_findstring(buff, pragmas)) {
       case 0:  /* debug */
         if (!skip) lua_debug = 1;
@@ -174,42 +153,42 @@ static void inclinenumber (void)
         break;
       case 2:  /* endinput */
         if (!skip) {
-          current = EOZ;
-          iflevel = 0;  /* to allow $endinput inside a $if */
+          LL->current = EOZ;
+          LL->iflevel = 0;  /* to allow $endinput inside a $if */
         }
         break;
       case 3:  /* end */
-        if (iflevel-- == 0)
+        if (LL->iflevel-- == 0)
           luaY_syntaxerror("unmatched $end", "$end");
         break;
       case 4:  /* ifnot */
         ifnot = 1;
         /* go through */
       case 5:  /* if */
-        if (iflevel == MAX_IFS-1)
+        if (LL->iflevel == MAX_IFS-1)
           luaY_syntaxerror("too many nested `$ifs'", "$if");
-        readname(buff);
-        iflevel++;
-        ifstate[iflevel].elsepart = 0;
-        ifstate[iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
-        ifstate[iflevel].skip = skip || !ifstate[iflevel].condition;
+        readname(LL, buff);
+        LL->iflevel++;
+        LL->ifstate[LL->iflevel].elsepart = 0;
+        LL->ifstate[LL->iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
+        LL->ifstate[LL->iflevel].skip = skip || !LL->ifstate[LL->iflevel].condition;
         break;
       case 6:  /* else */
-        if (ifstate[iflevel].elsepart)
+        if (LL->ifstate[LL->iflevel].elsepart)
           luaY_syntaxerror("unmatched $else", "$else");
-        ifstate[iflevel].elsepart = 1;
-        ifstate[iflevel].skip =
-                    ifstate[iflevel-1].skip || ifstate[iflevel].condition;
+        LL->ifstate[LL->iflevel].elsepart = 1;
+        LL->ifstate[LL->iflevel].skip = LL->ifstate[LL->iflevel-1].skip ||
+                                      LL->ifstate[LL->iflevel].condition;
         break;
       default:
         luaY_syntaxerror("invalid pragma", buff);
     }
-    skipspace();
-    if (current == '\n')  /* pragma must end with a '\n' ... */
-      inclinenumber();
-    else if (current != EOZ)  /* or eof */
+    skipspace(LL);
+    if (LL->current == '\n')  /* pragma must end with a '\n' ... */
+      inclinenumber(LL);
+    else if (LL->current != EOZ)  /* or eof */
       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)
-    textbuff.text = luaM_buffer(textbuff.buffsize *= 2);
-  textbuff.text[textbuff.tokensize++] = c;
+  if (LL->textbuff.tokensize >= LL->textbuff.buffsize)
+    LL->textbuff.text = luaM_buffer(LL->textbuff.buffsize *= 2);
+  LL->textbuff.text[LL->textbuff.tokensize++] = c;
 }
 
 
 char *luaX_lasttoken (void)
 {
-  save(0);
-  return textbuff.text;
+  save(L->lexstate, 0);
+  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;
   while (1) {
-    switch (current) {
+    switch (LL->current) {
       case EOZ:
-        save(0);
+        save(LL, 0);
         return WRONGTOKEN;
       case '[':
-        save_and_next();
-        if (current == '[') {
+        save_and_next(LL);
+        if (LL->current == '[') {
           cont++;
-          save_and_next();
+          save_and_next(LL);
         }
         continue;
       case ']':
-        save_and_next();
-        if (current == ']') {
+        save_and_next(LL);
+        if (LL->current == ']') {
           if (cont == 0) goto endloop;
           cont--;
-          save_and_next();
+          save_and_next(LL);
         }
         continue;
       case '\n':
-        save('\n');
-        inclinenumber();
+        save(LL, '\n');
+        inclinenumber(LL);
         continue;
       default:
-        save_and_next();
+        save_and_next(LL);
     }
   } endloop:
-  save_and_next();  /* pass the second ']' */
-  textbuff.text[textbuff.tokensize-2] = 0;  /* erases ']]' */
-  luaY_lval.pTStr = luaS_new(textbuff.text+2);
-  textbuff.text[textbuff.tokensize-2] = ']';  /* restores ']]' */
+  save_and_next(LL);  /* pass the second ']' */
+  LL->textbuff.text[LL->textbuff.tokensize-2] = 0;  /* erases ']]' */
+  l->pTStr = luaS_new(LL->textbuff.text+2);
+  LL->textbuff.text[LL->textbuff.tokensize-2] = ']';  /* restores ']]' */
   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;
-  textbuff.tokensize = 0;
+  LL->textbuff.tokensize = 0;
   if (lua_debug)
-    luaY_codedebugline(linelasttoken);
-  linelasttoken = luaX_linenumber;
+    luaY_codedebugline(LL->linelasttoken);
+  LL->linelasttoken = LL->linenumber;
   while (1) {
-    switch (current) {
+    switch (LL->current) {
       case '\n':
-        inclinenumber();
-        linelasttoken = luaX_linenumber;
+        inclinenumber(LL);
+        LL->linelasttoken = LL->linenumber;
         continue;
 
       case ' ': case '\t': case '\r':  /* CR: to avoid problems with DOS */
-        next();
+        next(LL);
         continue;
 
       case '-':
-        save_and_next();
-        if (current != '-') return '-';
-        do { next(); } while (current != '\n' && current != EOZ);
-        textbuff.tokensize = 0;
+        save_and_next(LL);
+        if (LL->current != '-') return '-';
+        do { next(LL); } while (LL->current != '\n' && LL->current != EOZ);
+        LL->textbuff.tokensize = 0;
         continue;
 
       case '[':
-        save_and_next();
-        if (current != '[') return '[';
+        save_and_next(LL);
+        if (LL->current != '[') return '[';
         else {
-          save_and_next();  /* pass the second '[' */
-          return read_long_string();
+          save_and_next(LL);  /* pass the second '[' */
+          return read_long_string(LL, l);
         }
 
       case '=':
-        save_and_next();
-        if (current != '=') return '=';
-        else { save_and_next(); return EQ; }
+        save_and_next(LL);
+        if (LL->current != '=') return '=';
+        else { save_and_next(LL); return EQ; }
 
       case '<':
-        save_and_next();
-        if (current != '=') return '<';
-        else { save_and_next(); return LE; }
+        save_and_next(LL);
+        if (LL->current != '=') return '<';
+        else { save_and_next(LL); return LE; }
 
       case '>':
-        save_and_next();
-        if (current != '=') return '>';
-        else { save_and_next(); return GE; }
+        save_and_next(LL);
+        if (LL->current != '=') return '>';
+        else { save_and_next(LL); return GE; }
 
       case '~':
-        save_and_next();
-        if (current != '=') return '~';
-        else { save_and_next(); return NE; }
+        save_and_next(LL);
+        if (LL->current != '=') return '~';
+        else { save_and_next(LL); return NE; }
 
       case '"':
       case '\'': {
-        int del = current;
-        save_and_next();
-        while (current != del) {
-          switch (current) {
+        int del = LL->current;
+        save_and_next(LL);
+        while (LL->current != del) {
+          switch (LL->current) {
             case EOZ:
             case '\n':
-              save(0);
+              save(LL, 0);
               return WRONGTOKEN;
             case '\\':
-              next();  /* do not save the '\' */
-              switch (current) {
-                case 'n': save('\n'); next(); break;
-                case 't': save('\t'); next(); break;
-                case 'r': save('\r'); next(); break;
-                case '\n': save('\n'); inclinenumber(); break;
-                default : save_and_next(); break;
+              next(LL);  /* do not save the '\' */
+              switch (LL->current) {
+                case 'n': save(LL, '\n'); next(LL); break;
+                case 't': save(LL, '\t'); next(LL); break;
+                case 'r': save(LL, '\r'); next(LL); break;
+                case '\n': save(LL, '\n'); inclinenumber(LL); break;
+                default : save_and_next(LL); break;
               }
               break;
             default:
-              save_and_next();
+              save_and_next(LL);
           }
         }
-        next();  /* skip delimiter */
-        save(0);
-        luaY_lval.pTStr = luaS_new(textbuff.text+1);
-        textbuff.text[textbuff.tokensize-1] = del;  /* restore delimiter */
+        next(LL);  /* skip delimiter */
+        save(LL, 0);
+        l->pTStr = luaS_new(LL->textbuff.text+1);
+        LL->textbuff.text[LL->textbuff.tokensize-1] = del;  /* restore delimiter */
         return STRING;
       }
 
       case '.':
-        save_and_next();
-        if (current == '.')
+        save_and_next(LL);
+        if (LL->current == '.')
         {
-          save_and_next();
-          if (current == '.')
+          save_and_next(LL);
+          if (LL->current == '.')
           {
-            save_and_next();
+            save_and_next(LL);
             return DOTS;   /* ... */
           }
           else return CONC;   /* .. */
         }
-        else if (!isdigit(current)) return '.';
-        /* current is a digit: goes through to number */
+        else if (!isdigit(LL->current)) return '.';
+        /* LL->current is a digit: goes through to number */
 	a=0.0;
         goto fraction;
 
@@ -385,69 +369,69 @@ int luaY_lex (void)
       case '5': case '6': case '7': case '8': case '9':
 	a=0.0;
         do {
-          a=10.0*a+(current-'0');
-          save_and_next();
-        } while (isdigit(current));
-        if (current == '.') {
-          save_and_next();
-          if (current == '.') {
-            save(0);
+          a=10.0*a+(LL->current-'0');
+          save_and_next(LL);
+        } while (isdigit(LL->current));
+        if (LL->current == '.') {
+          save_and_next(LL);
+          if (LL->current == '.') {
+            save(LL, 0);
             luaY_error(
               "ambiguous syntax (decimal point x string concatenation)");
           }
         }
       fraction:
 	{ double da=0.1;
-	  while (isdigit(current))
+	  while (isdigit(LL->current))
 	  {
-            a+=(current-'0')*da;
+            a+=(LL->current-'0')*da;
             da/=10.0;
-            save_and_next();
+            save_and_next(LL);
           }
-          if (toupper(current) == 'E') {
+          if (toupper(LL->current) == 'E') {
 	    int e=0;
 	    int neg;
 	    double ea;
-            save_and_next();
-	    neg=(current=='-');
-            if (current == '+' || current == '-') save_and_next();
-            if (!isdigit(current)) {
-              save(0); return WRONGTOKEN; }
+            save_and_next(LL);
+	    neg=(LL->current=='-');
+            if (LL->current == '+' || LL->current == '-') save_and_next(LL);
+            if (!isdigit(LL->current)) {
+              save(LL, 0); return WRONGTOKEN; }
             do {
-              e=10.0*e+(current-'0');
-              save_and_next();
-            } while (isdigit(current));
+              e=10.0*e+(LL->current-'0');
+              save_and_next(LL);
+            } while (isdigit(LL->current));
 	    for (ea=neg?0.1:10.0; e>0; e>>=1)
 	    {
 	      if (e & 1) a*=ea;
 	      ea*=ea;
 	    }
           }
-          luaY_lval.vReal = a;
+          l->vReal = a;
           return NUMBER;
         }
 
       case EOZ:
-        save(0);
-        if (iflevel > 0)
+        save(LL, 0);
+        if (LL->iflevel > 0)
           luaY_error("missing $endif");
         return 0;
 
       default:
-        if (current != '_' && !isalpha(current)) {
-          save_and_next();
-          return textbuff.text[0];
+        if (LL->current != '_' && !isalpha(LL->current)) {
+          save_and_next(LL);
+          return LL->textbuff.text[0];
         }
         else {  /* identifier or reserved word */
           TaggedString *ts;
           do {
-            save_and_next();
-          } while (isalnum(current) || current == '_');
-          save(0);
-          ts = luaS_new(textbuff.text);
+            save_and_next(LL);
+          } while (isalnum(LL->current) || LL->current == '_');
+          save(LL, 0);
+          ts = luaS_new(LL->textbuff.text);
           if (ts->head.marked > 255)
             return ts->head.marked;  /* reserved word */
-          luaY_lval.pTStr = ts;
+          l->pTStr = ts;
           return NAME;
         }
     }

+ 30 - 2
llex.h

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -11,11 +11,39 @@
 #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;
 
 
 void luaX_init (void);
-int  luaY_lex (void);
 void luaX_setinput (ZIO *z);
 char *luaX_lasttoken (void);
 

+ 8 - 10
lmem.c

@@ -1,5 +1,5 @@
 /*
-** $Id: $
+** $Id: lmem.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $
 ** Interface to Memory Manager
 ** See Copyright Notice in lua.h
 */
@@ -8,6 +8,7 @@
 #include <stdlib.h>
 
 #include "lmem.h"
+#include "lstate.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)
 {
-  if (size > Mbuffsize) {
-    Mbuffsize = size;
-    Mbuffer = luaM_realloc(Mbuffer, Mbuffsize);
+  if (size > L->Mbuffsize) {
+    L->Mbuffsize = size;
+    L->Mbuffer = luaM_realloc(L->Mbuffer, L->Mbuffsize);
   }
-  return Mbuffer;
+  return L->Mbuffer;
 }
 
 
 void luaM_clearbuffer (void)
 {
-  Mbuffsize /= 2;
-  Mbuffer = luaM_realloc(Mbuffer, Mbuffsize);
+  L->Mbuffsize /= 2;
+  L->Mbuffer = luaM_realloc(L->Mbuffer, L->Mbuffsize);
 }
 
 

+ 1 - 4
lobject.c

@@ -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
 ** 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 */
 static long dimensions[] =
  {5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,

+ 1 - 2
lobject.h

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -162,7 +162,6 @@ typedef struct Hash {
 ** a gross estimation of number of memory "blocks" allocated
 ** (a block is *roughly* 32 bytes)
 */
-extern unsigned long luaO_nblocks;
 
 extern char *luaO_typenames[];
 

+ 16 - 26
lstring.c

@@ -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)
 ** See Copyright Notice in lua.h
 */
@@ -9,6 +9,7 @@
 
 #include "lmem.h"
 #include "lobject.h"
+#include "lstate.h"
 #include "lstring.h"
 #include "lua.h"
 
@@ -19,18 +20,6 @@
 #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}};
 
@@ -38,10 +27,11 @@ static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}};
 void luaS_init (void)
 {
   int i;
+  L->string_root = luaM_newvector(NUM_HASHS, stringtable);
   for (i=0; i<NUM_HASHS; i++) {
-    string_root[i].size = 0;
-    string_root[i].nuse = 0;
-    string_root[i].hash = NULL;
+    L->string_root[i].size = 0;
+    L->string_root[i].nuse = 0;
+    L->string_root[i].hash = NULL;
   }
 }
 
@@ -93,14 +83,14 @@ static TaggedString *newone(char *buff, int tag, unsigned long h)
     strcpy(ts->str, buff);
     ts->u.globalval.ttype = LUA_T_NIL;  /* initialize global value */
     ts->constindex = 0;
-    luaO_nblocks += gcsizestring(l);
+    L->nblocks += gcsizestring(l);
   }
   else {
     ts = (TaggedString *)luaM_malloc(sizeof(TaggedString));
     ts->u.d.v = buff;
     ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag;
     ts->constindex = -1;  /* tag -> this is a userdata */
-    luaO_nblocks++;
+    L->nblocks++;
   }
   ts->head.marked = 0;
   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)
 {
-  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)
 {
-  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)
@@ -159,7 +149,7 @@ void luaS_free (TaggedString *l)
 {
   while (l) {
     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);
     l = next;
   }
@@ -185,9 +175,9 @@ TaggedString *luaS_collector (void)
 {
   TaggedString *frees = NULL;
   int i;
-  remove_from_list(&luaS_root);
+  remove_from_list(&(L->rootglobal));
   for (i=0; i<NUM_HASHS; i++) {
-    stringtable *tb = &string_root[i];
+    stringtable *tb = &L->string_root[i];
     int j;
     for (j=0; j<tb->size; j++) {
       TaggedString *t = tb->hash[j];
@@ -209,8 +199,8 @@ void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
 {
   ts->u.globalval = *newval;
   if (ts->head.next == (GCnode *)ts) {  /* is not in list? */
-    ts->head.next = luaS_root.next;
-    luaS_root.next = (GCnode *)ts;
+    ts->head.next = L->rootglobal.next;
+    L->rootglobal.next = (GCnode *)ts;
   }
 }
 
@@ -218,7 +208,7 @@ void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
 char *luaS_travsymbol (int (*fn)(TObject *))
 {
   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))
       return g->str;
   return NULL;

+ 1 - 3
lstring.h

@@ -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)
 ** See Copyright Notice in lua.h
 */
@@ -10,8 +10,6 @@
 
 #include "lobject.h"
 
-extern GCnode luaS_root;
-
 
 void luaS_init (void);
 TaggedString *luaS_createudata (void *udata, int tag);

+ 6 - 8
ltable.c

@@ -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)
 ** See Copyright Notice in lua.h
 */
@@ -9,6 +9,7 @@
 #include "lauxlib.h"
 #include "lmem.h"
 #include "lobject.h"
+#include "lstate.h"
 #include "ltable.h"
 #include "lua.h"
 
@@ -24,9 +25,6 @@
 #define TagDefault LUA_T_ARRAY;
 
 
-GCnode luaH_root = {NULL, 0};
-
-
 
 static long int hashindex (TObject *ref)
 {
@@ -95,7 +93,7 @@ void luaH_free (Hash *frees)
 {
   while (frees) {
     Hash *next = (Hash *)frees->head.next;
-    luaO_nblocks -= gcsize(frees->nhash);
+    L->nblocks -= gcsize(frees->nhash);
     hashdelete(frees);
     frees = next;
   }
@@ -110,8 +108,8 @@ Hash *luaH_new (int nhash)
   nhash(t) = nhash;
   nuse(t) = 0;
   t->htag = TagDefault;
-  luaO_insertlist(&luaH_root, (GCnode *)t);
-  luaO_nblocks += gcsize(nhash);
+  luaO_insertlist(&(L->roottable), (GCnode *)t);
+  L->nblocks += gcsize(nhash);
   return t;
 }
 
@@ -145,7 +143,7 @@ static void rehash (Hash *t)
     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 */
   }
-  luaO_nblocks += gcsize(t->nhash)-gcsize(nold);
+  L->nblocks += gcsize(t->nhash)-gcsize(nold);
   luaM_free(vold);
 }
 

+ 1 - 4
ltable.h

@@ -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)
 ** See Copyright Notice in lua.h
 */
@@ -10,9 +10,6 @@
 #include "lobject.h"
 
 
-extern GCnode luaH_root;
-
-
 #define node(t,i)       (&(t)->node[i])
 #define ref(n)		(&(n)->ref)
 #define val(n)		(&(n)->val)

+ 17 - 21
ltm.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -9,9 +9,9 @@
 #include <string.h>
 
 #include "lauxlib.h"
-#include "ldo.h"
 #include "lmem.h"
 #include "lobject.h"
+#include "lstate.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
 *  'placeholder' for "default" fallbacks
@@ -67,34 +63,34 @@ static void init_entry (int tag)
 void luaT_init (void)
 {
   int t;
-  IMtable_size = NUM_TAGS*2;
-  last_tag = -(NUM_TAGS-1);
-  luaT_IMtable = luaM_newvector(IMtable_size, struct IM);
-  for (t=last_tag; t<=0; t++)
+  L->IMtable_size = NUM_TAGS*2;
+  L->last_tag = -(NUM_TAGS-1);
+  L->IMtable = luaM_newvector(L->IMtable_size, struct IM);
+  for (t=L->last_tag; t<=0; t++)
     init_entry(t);
 }
 
 
 int lua_newtag (void)
 {
-  --last_tag;
-  if ((-last_tag) >= IMtable_size)
-    IMtable_size = luaM_growvector(&luaT_IMtable, IMtable_size,
+  --L->last_tag;
+  if ((-L->last_tag) >= L->IMtable_size)
+    L->IMtable_size = luaM_growvector(&L->IMtable, L->IMtable_size,
                                    struct IM, memEM, MAX_INT);
-  init_entry(last_tag);
-  return last_tag;
+  init_entry(L->last_tag);
+  return L->last_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);
 }
 
 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);
 }
 
@@ -145,11 +141,11 @@ void luaT_settagmethod (int t, char *event, TObject *func)
 char *luaT_travtagmethods (int (*fn)(TObject *))
 {
   int e;
-  if (fn(&luaD_errorim))
+  if (fn(&L->errorim))
     return "error";
   for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {  /* ORDER IM */
     int t;
-    for (t=0; t>=last_tag; t--)
+    for (t=0; t>=L->last_tag; t--)
       if (fn(luaT_getim(t,e)))
         return luaT_eventname[e];
   }
@@ -203,8 +199,8 @@ void luaT_setfallback (void)
   luaL_arg_check(lua_isfunction(func), 2, "function expected");
   switch (luaO_findstring(name, oldnames)) {
     case 0:  /* old error fallback */
-      oldfunc = luaD_errorim;
-      luaD_errorim = *luaA_Address(func);
+      oldfunc = L->errorim;
+      L->errorim = *luaA_Address(func);
       replace = errorFB;
       break;
     case 1:  /* old getglobal fallback */

+ 5 - 4
ltm.h

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -9,6 +9,7 @@
 
 
 #include "lobject.h"
+#include "lstate.h"
 
 /*
 * WARNING: if you change the order of this enumeration,
@@ -38,12 +39,12 @@ typedef enum {
 #define IM_N 18
 
 
-extern struct IM {
+struct IM {
   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)))
 
 extern char *luaT_eventname[];

+ 2 - 1
lua.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -82,6 +82,7 @@ int main (int argc, char *argv[])
       }
     }
   }
+/*  lua_close(); */
   return 0;
 }
 

+ 3 - 1
lua.h

@@ -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
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** e-mail: [email protected]
@@ -117,6 +117,8 @@ int            lua_ref			(int lock); /* In: value */
 lua_Object     lua_getref		(int ref);
 void	       lua_unref		(int ref);
 
+lua_Object     lua_globalbag		(void);
+
 lua_Object     lua_createtable		(void);
 
 long	       lua_collectgarbage	(long limit);

+ 123 - 122
lua.stx

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -16,13 +16,13 @@
 #include "lmem.h"
 #include "lopcodes.h"
 #include "lparser.h"
+#include "lstate.h"
 #include "lstring.h"
 #include "lua.h"
 #include "luadebug.h"
 #include "lzio.h"
 
 
-/* to avoid warnings generated by yacc */
 int luaY_parse (void);
 
 
@@ -61,7 +61,7 @@ typedef long vardesc;
 #define dotindex(v)	((-(v))-1)
 
 /* state needed to generate code for a given function */
-typedef struct State {
+typedef struct FuncState {
   TProtoFunc *f;  /* current function header */
   int pc;  /* next position to code */
   TaggedString *localvar[MAXLOCALS];  /* store local variable names */
@@ -75,11 +75,11 @@ typedef struct State {
   int maxconsts;  /* size of f->consts */
   vardesc varbuffer[MAXVAR];  /* variables in an assignment list */
   vardesc upvalues[MAXUPVALUES];  /* upvalues */
-} State;
+} FuncState;
 
-static State *mainState, *currState;
 
 
+#define YYPURE	1
 
 
 void luaY_syntaxerror (char *s, char *token)
@@ -87,7 +87,7 @@ void luaY_syntaxerror (char *s, char *token)
   if (token[0] == 0)
     token = "<eof>";
   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)
 {
-  if (currState->pc+n > currState->maxcode)
-    currState->maxcode = luaM_growvector(&currState->f->code,
-                                currState->maxcode, Byte, codeEM, MAX_INT);
+  if (L->currState->pc+n > L->currState->maxcode)
+    L->currState->maxcode = luaM_growvector(&L->currState->f->code,
+                                L->currState->maxcode, Byte, codeEM, MAX_INT);
 }
 
 
 static void movecode_up (int d, int s, int 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;
   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)
 {
   check_pc(1);
-  currState->f->code[currState->pc++] = c;
+  L->currState->f->code[L->currState->pc++] = c;
 }
 
 
 static void deltastack (int delta)
 {
-  currState->stacksize += delta;
-  if (currState->stacksize > currState->maxstacksize) {
-    if (currState->stacksize > 255)
+  L->currState->stacksize += delta;
+  if (L->currState->stacksize > L->currState->maxstacksize) {
+    if (L->currState->stacksize > 255)
       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);
   if (arg < builtin) {
-    currState->f->code[pc] = op+1+arg;
+    L->currState->f->code[pc] = op+1+arg;
     return 1;
   }
   else if (arg <= 255) {
-    currState->f->code[pc] = op;
-    currState->f->code[pc+1] = arg;
+    L->currState->f->code[pc] = op;
+    L->currState->f->code[pc+1] = arg;
     return 2;
   }
   else if (arg <= MAX_WORD) {
-    currState->f->code[pc] = op+1+builtin;
-    currState->f->code[pc+1] = arg&0xFF;
-    currState->f->code[pc+2] = arg>>8;
+    L->currState->f->code[pc] = op+1+builtin;
+    L->currState->f->code[pc+1] = arg&0xFF;
+    L->currState->f->code[pc+2] = arg>>8;
     return 3;
   }
   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)
 {
   if (arg < builtin) {  /* close space */
-    movecode_down(pc+1, pc+2, currState->pc-(pc+2));
-    currState->pc--;
+    movecode_down(pc+1, pc+2, L->currState->pc-(pc+2));
+    L->currState->pc--;
   }
   else if (arg > 255) {  /* open space */
     check_pc(1);
-    movecode_up(pc+1, pc, currState->pc-pc);
-    currState->pc++;
+    movecode_up(pc+1, pc, L->currState->pc-pc);
+    L->currState->pc++;
   }
   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)
 {
   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;
   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;
   int c = s->constindex;
@@ -242,7 +242,7 @@ static int string_constant (TaggedString *s, State *cs)
 
 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)
 {
   /* check whether 'r' has appeared within the last LIM entries */
-  TObject *cnt = currState->f->consts;
-  int c = currState->f->nconsts;
+  TObject *cnt = L->currState->f->consts;
+  int c = L->currState->f->nconsts;
   int lim = c < LIM ? 0 : c-LIM;
   while (--c >= lim) {
     if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
       return c;
   }
   /* not found; create a luaM_new entry */
-  c = next_constant(currState);
-  cnt = currState->f->consts;  /* 'next_constant' may reallocate this vector */
+  c = next_constant(L->currState);
+  cnt = L->currState->f->consts;  /* 'next_constant' may reallocate this vector */
   ttype(&cnt[c]) = LUA_T_NUMBER;
   nvalue(&cnt[c]) = r;
   return c;
@@ -292,13 +292,13 @@ static void flush_list (int m, int n)
 
 static void luaI_registerlocalvar (TaggedString *varname, int line)
 {
-  if (currState->maxvars != -1) {  /* debug information? */
-    if (currState->nvars >= currState->maxvars)
-      currState->maxvars = luaM_growvector(&currState->f->locvars,
-                                      currState->maxvars, LocVar, "", MAX_WORD);
-    currState->f->locvars[currState->nvars].varname = varname;
-    currState->f->locvars[currState->nvars].line = line;
-    currState->nvars++;
+  if (L->currState->maxvars != -1) {  /* debug information? */
+    if (L->currState->nvars >= L->currState->maxvars)
+      L->currState->maxvars = luaM_growvector(&L->currState->f->locvars,
+                                      L->currState->maxvars, LocVar, "", MAX_WORD);
+    L->currState->f->locvars[L->currState->nvars].varname = varname;
+    L->currState->f->locvars[L->currState->nvars].line = line;
+    L->currState->nvars++;
   }
 }
 
@@ -311,17 +311,17 @@ static void luaI_unregisterlocalvar (int line)
 
 static void store_localvar (TaggedString *name, int n)
 {
-  if (currState->nlocalvar+n < MAXLOCALS)
-    currState->localvar[currState->nlocalvar+n] = name;
+  if (L->currState->nlocalvar+n < MAXLOCALS)
+    L->currState->localvar[L->currState->nlocalvar+n] = name;
   else
     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)
 {
   store_localvar(name, 0);
-  currState->nlocalvar++;
+  L->currState->nlocalvar++;
 }
 
 
@@ -342,11 +342,11 @@ static void add_varbuffer (vardesc var, int n)
 {
   if (n >= MAXVAR)
     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;
   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);
   if (i == -1) {  /* check shadowing */
     int l;
-    for (l=1; l<=(st-mainState); l++)
+    for (l=1; l<=(st-L->mainState); l++)
       if (aux_localname(n, st-l) >= 0)
         luaY_syntaxerror("cannot access a variable in outer scope", n->str);
     return string_constant(n, st)+MINGLOBAL;  /* global value */
@@ -371,16 +371,16 @@ static vardesc singlevar (TaggedString *n, State *st)
 
 static int indexupvalue (TaggedString *n)
 {
-  vardesc v = singlevar(n, currState-1);
+  vardesc v = singlevar(n, L->currState-1);
   int i;
-  for (i=0; i<currState->nupvalues; i++) {
-    if (currState->upvalues[i] == v)
+  for (i=0; i<L->currState->nupvalues; i++) {
+    if (L->currState->upvalues[i] == v)
       return i;
   }
   /* new one */
-  if (++(currState->nupvalues) > MAXUPVALUES)
+  if (++(L->currState->nupvalues) > MAXUPVALUES)
     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;
 }
 
@@ -388,9 +388,9 @@ static int indexupvalue (TaggedString *n)
 static void pushupvalue (TaggedString *n)
 {
   int i;
-  if (currState == mainState)
+  if (L->currState == L->mainState)
     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);
   i = indexupvalue(n);
   code_oparg(PUSHUPVALUE, 2, i, 1);
@@ -399,10 +399,9 @@ static void pushupvalue (TaggedString *n)
 
 void luaY_codedebugline (int line)
 {
-  static int lastline = 0;
-  if (lua_debug && line != lastline) {
+  if (lua_debug && line != L->lexstate->lastline) {
     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)
     return -exp; /* exp is -list length */
   else {
-    int temp = currState->f->code[exp];
-    int nparams = currState->f->code[exp-1];
+    int temp = L->currState->f->code[exp];
+    int nparams = L->currState->f->code[exp-1];
     exp += fix_opcode(exp-2, CALLFUNC, 2, nresults);
-    currState->f->code[exp] = nparams;
+    L->currState->f->code[exp] = nparams;
     if (nresults != MULT_RET)
       deltastack(nresults);
     deltastack(-(nparams+1));
@@ -436,7 +435,7 @@ static long adjust_functioncall (long exp, int nresults)
 static void adjust_mult_assign (int vars, long exps)
 {
   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)
       adjust_functioncall(exps, -diff);
     else {
@@ -450,11 +449,11 @@ static void adjust_mult_assign (int vars, long exps)
 
 static void code_args (int nparams, int dots)
 {
-  currState->nlocalvar += nparams;
+  L->currState->nlocalvar += nparams;
   if (!dots)
-    code_oparg(ARGS, 0, currState->nlocalvar, currState->nlocalvar);
+    code_oparg(ARGS, 0, L->currState->nlocalvar, L->currState->nlocalvar);
   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"));
   }
 }
@@ -487,9 +486,9 @@ static void storevar (vardesc var)
 /* returns how many elements are left as 'garbage' on the stack */
 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 */
-    storevar(currState->varbuffer[i]);
+    storevar(L->currState->varbuffer[i]);
     return left;
   }
   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)
 {
-  int delta = currState->pc+JMPSIZE - pos;  /* jump is relative */
+  int delta = L->currState->pc+JMPSIZE - pos;  /* jump is relative */
   if (delta > 255) delta++;
   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)
 {
   int elseinit = elseAdd+JMPSIZE;
-  if (currState->pc == elseinit) {  /* no else part */
-    currState->pc -= JMPSIZE;
-    elseinit = currState->pc;
+  if (L->currState->pc == elseinit) {  /* no else part */
+    L->currState->pc -= JMPSIZE;
+    elseinit = L->currState->pc;
   }
   else
-    elseinit += fix_jump(elseAdd, JMP, currState->pc);
+    elseinit += fix_jump(elseAdd, JMP, L->currState->pc);
   fix_jump(thenAdd, IFFJMP, elseinit);
 }
 
 
 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)
 {
-  code_oparg(RETCODE, 0, currState->nlocalvar, 0);
-  currState->stacksize = currState->nlocalvar;
+  code_oparg(RETCODE, 0, L->currState->nlocalvar, 0);
+  L->currState->stacksize = L->currState->nlocalvar;
 }
 
 
 static void func_onstack (TProtoFunc *f)
 {
   int i;
-  int nupvalues = (currState+1)->nupvalues;
-  int c = next_constant(currState);
-  ttype(&currState->f->consts[c]) = LUA_T_PROTO;
-  currState->f->consts[c].value.tf = (currState+1)->f;
+  int nupvalues = (L->currState+1)->nupvalues;
+  int c = next_constant(L->currState);
+  ttype(&L->currState->f->consts[c]) = LUA_T_PROTO;
+  L->currState->f->consts[c].value.tf = (L->currState+1)->f;
   for (i=0; i<nupvalues; i++)
-    lua_pushvar((currState+1)->upvalues[i]);
+    lua_pushvar((L->currState+1)->upvalues[i]);
   code_constant(c);
   code_oparg(CLOSURE, 2, nupvalues, -nupvalues);
 }
@@ -557,48 +556,48 @@ static void func_onstack (TProtoFunc *f)
 static void init_state (TaggedString *filename)
 {
   TProtoFunc *f = luaF_newproto();
-  currState->stacksize = 0;
-  currState->maxstacksize = 0;
-  currState->nlocalvar = 0;
-  currState->nupvalues = 0;
-  currState->f = f;
+  L->currState->stacksize = 0;
+  L->currState->maxstacksize = 0;
+  L->currState->nlocalvar = 0;
+  L->currState->nupvalues = 0;
+  L->currState->f = f;
   f->fileName = filename;
-  currState->pc = 0;
-  currState->maxcode = 0;
+  L->currState->pc = 0;
+  L->currState->maxcode = 0;
   f->code = NULL;
-  currState->maxconsts = 0;
+  L->currState->maxconsts = 0;
   if (lua_debug) {
-    currState->nvars = 0;
-    currState->maxvars = 0;
+    L->currState->nvars = 0;
+    L->currState->maxvars = 0;
   }
   else
-    currState->maxvars = -1;  /* flag no debug information */
+    L->currState->maxvars = -1;  /* flag no debug information */
   code_byte(0);  /* to be filled with stacksize */
 }
 
 
 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)");
-  currState++;
-  init_state(mainState->f->fileName);
-  luaY_codedebugline(luaX_linenumber);
-  currState->f->lineDefined = luaX_linenumber;
+  L->currState++;
+  init_state(L->mainState->f->fileName);
+  luaY_codedebugline(L->lexstate->linenumber);
+  L->currState->f->lineDefined = L->lexstate->linenumber;
 }
 
 static TProtoFunc *close_func (void)
 {
-  TProtoFunc *f = currState->f;
+  TProtoFunc *f = L->currState->f;
   code_neutralop(ENDCODE);
-  f->code[0] = currState->maxstacksize;
-  f->code = luaM_reallocvector(f->code, currState->pc, Byte);
+  f->code[0] = L->currState->maxstacksize;
+  f->code = luaM_reallocvector(f->code, L->currState->pc, Byte);
   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 */
-    f->locvars = luaM_reallocvector(f->locvars, currState->nvars, LocVar);
+    f->locvars = luaM_reallocvector(f->locvars, L->currState->nvars, LocVar);
   }
-  currState--;
+  L->currState--;
   return f;
 }
 
@@ -608,8 +607,10 @@ static TProtoFunc *close_func (void)
 */
 TProtoFunc *luaY_parser (ZIO *z, char *chunkname)
 {
-  State state[MAXSTATES];
-  currState = mainState = &state[0];
+  struct LexState lexstate;
+  FuncState state[MAXSTATES];
+  L->currState = L->mainState = &state[0];
+  L->lexstate = &lexstate;
   luaX_setinput(z);
   init_state(luaS_new(chunkname));
   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 newpos = $2+JMPSIZE;
 	 check_pc(expsize);
-	 memcpy(&currState->f->code[currState->pc],
-                &currState->f->code[$2], expsize);
-	 movecode_down($2, $3, currState->pc-$2);
-	 newpos += fix_jump($2, JMP, currState->pc-expsize);
+	 memcpy(&L->currState->f->code[L->currState->pc],
+                &L->currState->f->code[$2], expsize);
+	 movecode_down($2, $3, L->currState->pc-$2);
+	 newpos += fix_jump($2, JMP, L->currState->pc-expsize);
 	 fix_upjmp(IFTUPJMP, newpos);
        }}
 
@@ -712,18 +713,18 @@ stat   : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
 
        | LOCAL localnamelist decinit
        {
-         currState->nlocalvar += $2;
+         L->currState->nlocalvar += $2;
          adjust_mult_assign($2, $3);
        }
 
        | FUNCTION funcname body { func_onstack($3); storevar($2); }
        ;
 
-block    : {$<vInt>$ = currState->nlocalvar;} chunk
+block    : {$<vInt>$ = L->currState->nlocalvar;} chunk
          {
-           adjuststack(currState->nlocalvar - $<vInt>1);
-	   for (; currState->nlocalvar > $<vInt>1; currState->nlocalvar--)
-	     luaI_unregisterlocalvar(luaX_linenumber);
+           adjuststack(L->currState->nlocalvar - $<vInt>1);
+	   for (; L->currState->nlocalvar > $<vInt>1; L->currState->nlocalvar--)
+	     luaI_unregisterlocalvar(L->lexstate->linenumber);
          }
          ;
 
@@ -753,13 +754,13 @@ ret	: /* empty */
           }
 	;
 
-GetPC	: /* empty */ { $$ = currState->pc; }
+GetPC	: /* empty */ { $$ = L->currState->pc; }
 	;
 
 SaveWord : /* empty */
-	{ $$ = currState->pc;
+	{ $$ = L->currState->pc;
 	  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($1+$2);  /* number of parameters */
-	  $$ = currState->pc;
+	  $$ = L->currState->pc;
 	  code_byte(0);  /* must be adjusted by other rules */
 	}
 	     ;
@@ -816,7 +817,7 @@ functioncall : funcvalue funcParams
 funcvalue    : varexp { $$ = 0; }
 	     | varexp ':' NAME
 	     {
-               code_oparg(PUSHSELF, 0, string_constant($3, currState), 1);
+               code_oparg(PUSHSELF, 0, string_constant($3, L->currState), 1);
                $$ = 1;
 	     }
 	     ;
@@ -834,7 +835,7 @@ exprlist1 :  expr	{ if ($1 != 0) $$ = $1; else $$ = -1; }
 	{
 	  if ($4 == 0) $$ = -($<vLong>3 + 1);  /* -length */
 	  else {
-            currState->f->code[$4] = $<vLong>3;  /* store list length */
+            L->currState->f->code[$4] = $<vLong>3;  /* store list length */
 	    $$ = $4;
 	  }
 	}
@@ -899,9 +900,9 @@ varlist1  : var { $$ = 1; add_varbuffer($1, 0); }
 	  | 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 '.' NAME { $$ = (-string_constant($3, currState))-1; }
+	  |	varexp '.' NAME { $$ = (-string_constant($3, L->currState))-1; }
 	  ;
 		
 varexp	: var { lua_pushvar($1); }

+ 113 - 111
lvm.c

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

+ 23 - 20
makefile

@@ -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
 ## 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
 # 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
 
 
@@ -43,6 +43,7 @@ LUAOBJS = \
 	llex.o \
 	lmem.o \
 	lobject.o \
+	lstate.o \
 	lstx.o \
 	lstring.o \
 	ltable.o \
@@ -90,32 +91,34 @@ clear	:
 %.c : RCS/%.c,v
 	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 \
- luadebug.h lvm.h
+
+lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h ldo.h lstate.h lfunc.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
 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.o: ldo.c ldo.h lobject.h lua.h lfunc.h lgc.h lmem.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
-lgc.o: lgc.c ldo.h lobject.h lua.h lfunc.h lgc.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 lstate.h lfunc.h lgc.h lmem.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 lstate.h
+lgc.o: lgc.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
+ lstring.h ltable.h ltm.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
 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
-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
-lstx.o: lstx.c lauxlib.h lua.h ldo.h lobject.h lfunc.h llex.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
-ltm.o: ltm.c lauxlib.h lua.h ldo.h lobject.h lmem.h ltm.h lapi.h
+lstx.o: lstx.c lauxlib.h lua.h ldo.h lobject.h lstate.h lfunc.h llex.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 lstate.h ltable.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
 lundump.o: lundump.c lauxlib.h lua.h lfunc.h lobject.h lmem.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 \
- lopcodes.h lstring.h ltable.h ltm.h luadebug.h lvm.h
+lvm.o: lvm.c lauxlib.h lua.h ldo.h lobject.h lstate.h lfunc.h lgc.h \
+ lmem.h lopcodes.h lstring.h ltable.h ltm.h luadebug.h lvm.h
 lzio.o: lzio.c lzio.h