Roberto Ierusalimschy 24 jaren geleden
bovenliggende
commit
943b8f5b18
6 gewijzigde bestanden met toevoegingen van 44 en 32 verwijderingen
  1. 10 12
      ldo.c
  2. 5 2
      ldo.h
  3. 1 7
      lgc.c
  4. 5 2
      lgc.h
  5. 2 3
      lparser.c
  6. 21 6
      lvm.c

+ 10 - 12
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 1.133 2001/04/06 19:26:06 roberto Exp roberto $
+** $Id: ldo.c,v 1.134 2001/04/11 18:39:37 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -48,17 +48,15 @@ void luaD_init (lua_State *L, int stacksize) {
 }
 
 
-void luaD_checkstack (lua_State *L, int n) {
-  if (L->stack_last - L->top <= n) {  /* stack overflow? */
-    if (L->stack_last == L->stack+L->stacksize-1) {
-      /* overflow while handling overflow */
-      luaD_breakrun(L, LUA_ERRERR);  /* break run without error message */
-    }
-    else {
-      L->stack_last += EXTRA_STACK;  /* to be used by error message */
-      lua_assert(L->stack_last == L->stack+L->stacksize-1);
-      luaD_error(L, l_s("stack overflow"));
-    }
+void luaD_stackerror (lua_State *L) {
+  if (L->stack_last == L->stack+L->stacksize-1) {
+    /* overflow while handling overflow */
+    luaD_breakrun(L, LUA_ERRERR);  /* break run without error message */
+  }
+  else {
+    L->stack_last += EXTRA_STACK;  /* to be used by error message */
+    lua_assert(L->stack_last == L->stack+L->stacksize-1);
+    luaD_error(L, l_s("stack overflow"));
   }
 }
 

+ 5 - 2
ldo.h

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.h,v 1.31 2001/02/23 17:17:25 roberto Exp roberto $
+** $Id: ldo.h,v 1.32 2001/03/07 18:09:25 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -19,11 +19,14 @@
 #define incr_top {if (L->top == L->stack_last) luaD_checkstack(L, 1); L->top++;}
 
 
+#define luaD_checkstack(L,n) if (L->stack_last-(n)<=L->top) luaD_stackerror(L)
+
+
 void luaD_init (lua_State *L, int stacksize);
 void luaD_adjusttop (lua_State *L, StkId base, int extra);
 void luaD_lineHook (lua_State *L, int line, lua_Hook linehook);
 void luaD_call (lua_State *L, StkId func, int nResults);
-void luaD_checkstack (lua_State *L, int n);
+void luaD_stackerror (lua_State *L);
 
 void luaD_error (lua_State *L, const l_char *s);
 void luaD_breakrun (lua_State *L, int errcode);

+ 1 - 7
lgc.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lgc.c,v 1.97 2001/04/17 17:35:54 roberto Exp roberto $
+** $Id: lgc.c,v 1.98 2001/06/05 18:17:01 roberto Exp roberto $
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 */
@@ -389,9 +389,3 @@ void luaC_collectgarbage (lua_State *L) {
   callgcTM(L, &luaO_nilobject);
 }
 
-
-void luaC_checkGC (lua_State *L) {
-  if (G(L)->nblocks >= G(L)->GCthreshold)
-    luaC_collectgarbage(L);
-}
-

+ 5 - 2
lgc.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lgc.h,v 1.8 2000/10/02 14:47:43 roberto Exp roberto $
+** $Id: lgc.h,v 1.9 2001/02/02 16:23:20 roberto Exp roberto $
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 */
@@ -11,9 +11,12 @@
 #include "lobject.h"
 
 
+#define luaC_checkGC(L) if (G(L)->nblocks >= G(L)->GCthreshold) \
+			  luaC_collectgarbage(L)
+
+
 void luaC_collect (lua_State *L, int all);
 void luaC_collectgarbage (lua_State *L);
-void luaC_checkGC (lua_State *L);
 
 
 #endif

+ 2 - 3
lparser.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lparser.c,v 1.142 2001/04/06 18:25:00 roberto Exp roberto $
+** $Id: lparser.c,v 1.143 2001/06/05 18:17:01 roberto Exp roberto $
 ** LL(1) Parser and code generator for Lua
 ** See Copyright Notice in lua.h
 */
@@ -495,7 +495,6 @@ static void recfield (LexState *ls, expdesc *t) {
 static int recfields (LexState *ls, expdesc *t) {
   /* recfields -> recfield { `,' recfield } [`,'] */
   int n = 1;  /* at least one element */
-  luaK_exp2nextreg(ls->fs, t);
   recfield(ls, t);
   while (ls->t.token == l_c(',')) {
     next(ls);
@@ -513,7 +512,6 @@ static int listfields (LexState *ls, expdesc *t) {
   FuncState *fs = ls->fs;
   int n = 1;  /* at least one element */
   int reg;
-  luaK_exp2nextreg(ls->fs, t);
   reg = fs->freereg;
   expr(ls, &v);
   while (ls->t.token == l_c(',') &&
@@ -578,6 +576,7 @@ static void constructor (LexState *ls, expdesc *t) {
   Constdesc cd;
   pc = luaK_codeABc(fs, OP_NEWTABLE, 0, 0);
   init_exp(t, VRELOCABLE, pc);
+  luaK_exp2nextreg(ls->fs, t);  /* fix it at stack top (for gc) */
   check(ls, l_c('{'));
   constructor_part(ls, t, &cd);
   n = cd.n;

+ 21 - 6
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 1.178 2001/04/06 18:25:00 roberto Exp roberto $
+** $Id: lvm.c,v 1.179 2001/06/05 18:17:01 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -28,6 +28,16 @@
 
 
 
+static void luaV_checkGC (lua_State *L, StkId top) {
+  if (G(L)->nblocks >= G(L)->GCthreshold) {
+    StkId temp = L->top;
+    L->top = top;
+    luaC_collectgarbage(L);
+    L->top = temp;  /* restore old top position */
+  }
+}
+
+
 const TObject *luaV_tonumber (const TObject *obj, TObject *n) {
   if (ttype(obj) == LUA_TNUMBER) return obj;
   if (ttype(obj) == LUA_TSTRING && luaO_str2d(svalue(obj), &nvalue(n))) {
@@ -262,6 +272,7 @@ int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r) {
 
 
 void luaV_strconc (lua_State *L, int total, StkId top) {
+  luaV_checkGC(L, top);
   do {
     int n = 2;  /* number of elements handled in this pass (at least 2) */
     if (tostring(L, top-2) || tostring(L, top-1)) {
@@ -353,7 +364,11 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
   lua_Hook linehook;
   if (tf->is_vararg)  /* varargs? */
     adjust_varargs(L, base, tf->numparams);
-  luaD_adjusttop(L, base, tf->maxstacksize);
+  if (base > L->stack_last - tf->maxstacksize)
+    luaD_stackerror(L);
+  while (L->top < base+tf->maxstacksize)
+    setnilvalue(L->top++);
+  L->top = base+tf->maxstacksize;
   pc = tf->code;
   L->ci->pc = &pc;
   linehook = L->linehook;
@@ -406,8 +421,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
         break;
       }
       case OP_NEWTABLE: {
-        luaC_checkGC(L);
-        sethvalue(RA(i), luaH_new(L, GETARG_Bc(i)));
+        StkId ra = RA(i);
+        sethvalue(ra, luaH_new(L, GETARG_Bc(i)));
+        luaV_checkGC(L, ra+1);
         break;
       }
       case OP_SELF: {
@@ -463,7 +479,6 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
         StkId rb = RB(i);
         luaV_strconc(L, top-rb, top);
         setobj(RA(i), rb);
-        luaC_checkGC(L);
         break;
       }
       case OP_CJMP:
@@ -630,10 +645,10 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
         Proto *p = tf->kproto[GETARG_Bc(i)];
         int nup = p->nupvalues;
         StkId ra = RA(i);
+        luaV_checkGC(L, ra+nup);
         L->top = ra+nup;
         luaV_Lclosure(L, p, nup);
         L->top = base+tf->maxstacksize;
-        luaC_checkGC(L);
         break;
       }
     }