Bläddra i källkod

all boxed types start with their tags

Roberto Ierusalimschy 24 år sedan
förälder
incheckning
63a822c8e1
7 ändrade filer med 36 tillägg och 13 borttagningar
  1. 2 1
      ldo.c
  2. 2 1
      lfunc.c
  3. 3 1
      lgc.c
  4. 3 2
      lobject.c
  5. 21 6
      lobject.h
  6. 3 1
      lstring.c
  7. 2 1
      ltable.c

+ 2 - 1
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 1.117 2001/01/26 11:45:51 roberto Exp roberto $
+** $Id: ldo.c,v 1.118 2001/01/29 15:35:17 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -172,6 +172,7 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
     setclvalue(func, tm);  /* tag method is the new function to be called */
   }
   cl = clvalue(func);
+  ci.v.ttype = LUA_TMARK;
   ci.func = cl;
   setivalue(func, &ci);
   callhook = L->callhook;

+ 2 - 1
lfunc.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lfunc.c,v 1.36 2000/12/28 12:55:41 roberto Exp roberto $
+** $Id: lfunc.c,v 1.37 2001/01/19 13:20:30 roberto Exp roberto $
 ** Auxiliary functions to manipulate prototypes and closures
 ** See Copyright Notice in lua.h
 */
@@ -19,6 +19,7 @@
 
 Closure *luaF_newclosure (lua_State *L, int nelems) {
   Closure *c = (Closure *)luaM_malloc(L, sizeclosure(nelems));
+  c->v.ttype = LUA_TFUNCTION;
   c->next = G(L)->rootcl;
   G(L)->rootcl = c;
   c->mark = c;

+ 3 - 1
lgc.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lgc.c,v 1.81 2001/01/26 14:16:24 roberto Exp roberto $
+** $Id: lgc.c,v 1.82 2001/01/29 17:16:58 roberto Exp roberto $
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 */
@@ -66,6 +66,8 @@ static void marktable (GCState *st, Hash *h) {
 
 
 static void markobject (GCState *st, TObject *o) {
+  lua_assert(ttype(o) == LUA_TNUMBER ||
+             ttype(o) == ((TValue *)(o->value.v))->ttype);
   switch (ttype(o)) {
     case LUA_TUSERDATA:  case LUA_TSTRING:
       strmark(tsvalue(o));

+ 3 - 2
lobject.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.c,v 1.61 2001/01/25 16:45:36 roberto Exp roberto $
+** $Id: lobject.c,v 1.62 2001/01/26 14:16:35 roberto Exp roberto $
 ** Some generic functions over Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -19,7 +19,8 @@
 
 
 
-const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
+const char luaO_ttnil = LUA_TNIL;
+const TObject luaO_nilobject = {LUA_TNIL, {(void *)&luaO_ttnil}};
 
 
 /*

+ 21 - 6
lobject.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.h,v 1.89 2001/01/26 15:58:50 roberto Exp roberto $
+** $Id: lobject.h,v 1.90 2001/01/29 17:16:58 roberto Exp roberto $
 ** Type definitions for Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -29,7 +29,7 @@
 #endif
 
 
-/* mark for closures active in the stack */
+/* ttype for closures active in the stack */
 #define LUA_TMARK	6
 
 
@@ -41,6 +41,14 @@
 #define is_T_MARK(t)	(ttype(t) == LUA_TMARK)
 
 
+/*
+** tag at the start of all "boxed" Lua values
+*/
+typedef struct TValue {
+  char ttype;
+} TValue;
+
+
 typedef union {
   void *v;
   lua_Number n;		/* LUA_TNUMBER */
@@ -87,7 +95,8 @@ typedef struct lua_TObject {
   { TObject *_o=(obj); struct CallInfo *_v=(x); \
     _o->tt=LUA_TMARK; _o->value.v=_v; }
 
-#define setnilvalue(obj) { (obj)->tt=LUA_TNIL; }
+#define setnilvalue(obj) \
+  { TObject *_o=(obj); _o->tt=LUA_TNIL; _o->value.v = (void *)&luaO_ttnil; }
 
 #define setobj(obj1,obj2) \
   { TObject *o1=(obj1); const TObject *o2=(obj2); \
@@ -107,6 +116,8 @@ typedef struct lua_TObject {
 #define TSPACK	((int)sizeof(int))
 
 typedef struct TString {
+  TValue v;
+  unsigned char marked;
   union {
     struct {  /* for strings */
       luint32 hash;
@@ -119,7 +130,6 @@ typedef struct TString {
   } u;
   size_t len;
   struct TString *nexthash;  /* chain for hash table */
-  int marked;
   char str[TSPACK];   /* variable length string!! must be the last field! */
 } TString;
 
@@ -128,6 +138,7 @@ typedef struct TString {
 ** Function Prototypes
 */
 typedef struct Proto {
+  TValue v;
   lua_Number *knum;  /* numbers used by the function */
   int sizeknum;  /* size of `knum' */
   struct TString **kstr;  /* strings used by the function */
@@ -162,14 +173,15 @@ typedef struct LocVar {
 ** Closures
 */
 typedef struct Closure {
+  TValue v;
+  char isC;  /* 0 for Lua functions, 1 for C functions */
+  short nupvalues;
   union {
     lua_CFunction c;  /* C functions */
     struct Proto *l;  /* Lua functions */
   } f;
   struct Closure *next;
   struct Closure *mark;  /* marked closures (point to itself when not marked) */
-  short isC;  /* 0 for Lua functions, 1 for C functions */
-  short nupvalues;
   TObject upvalue[1];
 } Closure;
 
@@ -186,6 +198,7 @@ typedef struct Node {
 
 
 typedef struct Hash {
+  TValue v;
   Node *node;
   int htag;
   int size;
@@ -210,6 +223,7 @@ typedef struct Hash {
 ** informations about a call (for debugging)
 */
 typedef struct CallInfo {
+  TValue v;
   struct Closure *func;  /* function being called */
   const Instruction **pc;  /* current pc of called function */
   int lastpc;  /* last pc traced */
@@ -218,6 +232,7 @@ typedef struct CallInfo {
 } CallInfo;
 
 
+extern const char luaO_ttnil;  /* "address" of the nil value */
 extern const TObject luaO_nilobject;
 
 

+ 3 - 1
lstring.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstring.c,v 1.51 2001/01/19 13:20:30 roberto Exp roberto $
+** $Id: lstring.c,v 1.52 2001/01/26 15:58:50 roberto Exp roberto $
 ** String table (keeps all strings handled by Lua)
 ** See Copyright Notice in lua.h
 */
@@ -76,6 +76,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
   }
   /* not found */
   ts = (TString *)luaM_malloc(L, sizestring(l));
+  ts->v.ttype = LUA_TSTRING;
   ts->marked = 0;
   ts->nexthash = NULL;
   ts->len = l;
@@ -91,6 +92,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
 TString *luaS_newudata (lua_State *L, size_t s, void *udata) {
   union L_UTString *uts = (union L_UTString *)luaM_malloc(L, sizeudata(s));
   TString *ts = &uts->ts;
+  ts->v.ttype = LUA_TUSERDATA;
   ts->marked = 0;
   ts->nexthash = NULL;
   ts->len = s;

+ 2 - 1
ltable.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltable.c,v 1.71 2001/01/29 13:02:20 roberto Exp roberto $
+** $Id: ltable.c,v 1.72 2001/01/29 17:16:58 roberto Exp roberto $
 ** Lua tables (hash)
 ** See Copyright Notice in lua.h
 */
@@ -98,6 +98,7 @@ static void setnodevector (lua_State *L, Hash *t, luint32 size) {
 
 Hash *luaH_new (lua_State *L, int size) {
   Hash *t = luaM_new(L, Hash);
+  t->v.ttype = LUA_TTABLE;
   t->htag = TagDefault;
   t->next = G(L)->roottable;
   G(L)->roottable = t;