From 888f91fa24d7561471acb87ddafc156408dd3617 Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Tue, 20 Feb 2001 15:28:11 -0300
Subject: code check for upvalues

---
 ldebug.c  |  7 ++++---
 lfunc.c   |  3 ++-
 lgc.c     |  6 ++++--
 lobject.h |  3 ++-
 lparser.c | 17 ++++++++---------
 lparser.h |  3 +--
 6 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/ldebug.c b/ldebug.c
index f99dfd75..c9b045bc 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
 /*
-** $Id: ldebug.c,v 1.64 2001/02/16 17:58:27 roberto Exp roberto $
+** $Id: ldebug.c,v 1.65 2001/02/20 18:15:33 roberto Exp roberto $
 ** Debug Interface
 ** See Copyright Notice in lua.h
 */
@@ -433,7 +433,7 @@ static Instruction luaG_symbexec (lua_State *L, const Proto *pt,
         break;
       }
       case OP_PUSHUPVALUE: {
-        /* ?? */
+        check(arg1 < pt->nupvalues);
         break;
       }
       case OP_GETLOCAL:
@@ -462,7 +462,8 @@ static Instruction luaG_symbexec (lua_State *L, const Proto *pt,
         break;
       }
       case OP_CLOSURE: {
-        /* ?? */
+        check(arg1 < pt->sizekproto);
+        check(arg2 == pt->kproto[arg1]->nupvalues);
         pop = arg2;
         break;
       }
diff --git a/lfunc.c b/lfunc.c
index e42bb64e..3b8dfd21 100644
--- a/lfunc.c
+++ b/lfunc.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lfunc.c,v 1.39 2001/02/01 17:40:48 roberto Exp roberto $
+** $Id: lfunc.c,v 1.40 2001/02/09 20:22:29 roberto Exp roberto $
 ** Auxiliary functions to manipulate prototypes and closures
 ** See Copyright Notice in lua.h
 */
@@ -37,6 +37,7 @@ Proto *luaF_newproto (lua_State *L) {
   f->sizekproto = 0;
   f->code = NULL;
   f->sizecode = 0;
+  f->nupvalues = 0;
   f->numparams = 0;
   f->is_vararg = 0;
   f->maxstacksize = 0;
diff --git a/lgc.c b/lgc.c
index d500549a..9b4c4eaa 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lgc.c,v 1.88 2001/02/07 18:13:49 roberto Exp roberto $
+** $Id: lgc.c,v 1.89 2001/02/20 18:15:33 roberto Exp roberto $
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 */
@@ -62,8 +62,10 @@ static void protomark (Proto *f) {
 
 static void markclosure (GCState *st, Closure *cl) {
   if (!ismarked(cl)) {
-    if (!cl->isC)
+    if (!cl->isC) {
+      lua_assert(cl->nupvalues == cl->f.l->nupvalues);
       protomark(cl->f.l);
+    }
     cl->mark = st->cmark;  /* chain it for later traversal */
     st->cmark = cl;
   }
diff --git a/lobject.h b/lobject.h
index 5fb142a2..74f3c616 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.h,v 1.95 2001/02/09 20:22:29 roberto Exp roberto $
+** $Id: lobject.h,v 1.96 2001/02/20 18:15:33 roberto Exp roberto $
 ** Type definitions for Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -133,6 +133,7 @@ typedef struct Proto {
   int sizekproto;  /* size of `kproto' */
   Instruction *code;
   int sizecode;
+  short nupvalues;
   short numparams;
   short is_vararg;
   short maxstacksize;
diff --git a/lparser.c b/lparser.c
index be917924..aea4b621 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lparser.c,v 1.134 2001/02/14 17:38:45 roberto Exp roberto $
+** $Id: lparser.c,v 1.135 2001/02/20 18:15:33 roberto Exp roberto $
 ** LL(1) Parser and code generator for Lua
 ** See Copyright Notice in lua.h
 */
@@ -219,14 +219,14 @@ static void singlevar (LexState *ls, TString *n, expdesc *var) {
 static int indexupvalue (LexState *ls, expdesc *v) {
   FuncState *fs = ls->fs;
   int i;
-  for (i=0; i<fs->nupvalues; i++) {
+  for (i=0; i<fs->f->nupvalues; i++) {
     if (fs->upvalues[i].k == v->k && fs->upvalues[i].u.index == v->u.index)
       return i;
   }
   /* new one */
-  luaX_checklimit(ls, fs->nupvalues+1, MAXUPVALUES, "upvalues");
-  fs->upvalues[fs->nupvalues] = *v;
-  return fs->nupvalues++;
+  luaX_checklimit(ls, fs->f->nupvalues+1, MAXUPVALUES, "upvalues");
+  fs->upvalues[fs->f->nupvalues] = *v;
+  return fs->f->nupvalues++;
 }
 
 
@@ -297,12 +297,12 @@ static void pushclosure (LexState *ls, FuncState *func) {
   FuncState *fs = ls->fs;
   Proto *f = fs->f;
   int i;
-  for (i=0; i<func->nupvalues; i++)
+  for (i=0; i<func->f->nupvalues; i++)
     luaK_tostack(ls, &func->upvalues[i], 1);
   luaM_growvector(ls->L, f->kproto, fs->nkproto, f->sizekproto, Proto *,
                   MAXARG_A, "constant table overflow");
   f->kproto[fs->nkproto++] = func->f;
-  luaK_code2(fs, OP_CLOSURE, fs->nkproto-1, func->nupvalues);
+  luaK_code2(fs, OP_CLOSURE, fs->nkproto-1, func->f->nupvalues);
 }
 
 
@@ -323,7 +323,6 @@ static void open_func (LexState *ls, FuncState *fs) {
   fs->nlineinfo = 0;
   fs->nlocvars = 0;
   fs->nactloc = 0;
-  fs->nupvalues = 0;
   fs->lastline = 0;
   fs->bl = NULL;
   f->code = NULL;
@@ -370,7 +369,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z) {
   check_condition(&lexstate, (lexstate.t.token == TK_EOS), "<eof> expected");
   close_func(&lexstate);
   lua_assert(funcstate.prev == NULL);
-  lua_assert(funcstate.nupvalues == 0);
+  lua_assert(funcstate.f->nupvalues == 0);
   return funcstate.f;
 }
 
diff --git a/lparser.h b/lparser.h
index 933934c9..a754b3be 100644
--- a/lparser.h
+++ b/lparser.h
@@ -1,5 +1,5 @@
 /*
-** $Id: lparser.h,v 1.28 2000/12/26 18:46:09 roberto Exp roberto $
+** $Id: lparser.h,v 1.29 2000/12/28 12:55:41 roberto Exp roberto $
 ** LL(1) Parser and code generator for Lua
 ** See Copyright Notice in lua.h
 */
@@ -51,7 +51,6 @@ typedef struct FuncState {
   int nlineinfo;  /* number of elements in `lineinfo' */
   int nlocvars;  /* number of elements in `locvars' */
   int nactloc;  /* number of active local variables */
-  int nupvalues;  /* number of upvalues */
   int lastline;  /* line where last `lineinfo' was generated */
   struct Breaklabel *bl;  /* chain of breakable blocks */
   expdesc upvalues[MAXUPVALUES];  /* upvalues */
-- 
cgit v1.2.3-55-g6feb