1 /*
2 ** $Id: lobject.h,v 1.1 2002/02/14 10:46:59 jcatki Exp $
3 ** Type definitions for Lua objects
4 ** See Copyright Notice in lua.h
5 */
6 
7 #ifndef lobject_h
8 #define lobject_h
9 
10 
11 #include "llimits.h"
12 #include "lua.h"
13 
14 
15 #ifdef LUA_DEBUG
16 #undef NDEBUG
17 #include <assert.h>
18 #define LUA_INTERNALERROR(s)	assert(((void)s,0))
19 #define LUA_ASSERT(c,s)		assert(((void)s,(c)))
20 #else
21 #define LUA_INTERNALERROR(s)	/* empty */
22 #define LUA_ASSERT(c,s)		/* empty */
23 #endif
24 
25 
26 #ifdef LUA_DEBUG
27 /* to avoid warnings, and make sure value is really unused */
28 #define UNUSED(x)	(x=0, (void)(x))
29 #else
30 #define UNUSED(x)	((void)(x))	/* to avoid warnings */
31 #endif
32 
33 
34 /* mark for closures active in the stack */
35 #define LUA_TMARK	6
36 
37 
38 /* tags for values visible from Lua == first user-created tag */
39 #define NUM_TAGS	6
40 
41 
42 /* check whether `t' is a mark */
43 #define is_T_MARK(t)	((t) == LUA_TMARK)
44 
45 
46 typedef union {
47   struct TString *ts;	/* LUA_TSTRING, LUA_TUSERDATA */
48   struct Closure *cl;	/* LUA_TFUNCTION */
49   struct Hash *a;	/* LUA_TTABLE */
50   struct CallInfo *i;	/* LUA_TLMARK */
51   Number n;		/* LUA_TNUMBER */
52 } Value;
53 
54 
55 /* Macros to access values */
56 #define ttype(o)        ((o)->ttype)
57 #define nvalue(o)       ((o)->value.n)
58 #define tsvalue(o)      ((o)->value.ts)
59 #define clvalue(o)      ((o)->value.cl)
60 #define hvalue(o)       ((o)->value.a)
61 #define infovalue(o)	((o)->value.i)
62 #define svalue(o)       (tsvalue(o)->str)
63 
64 
65 typedef struct lua_TObject {
66   int ttype;
67   Value value;
68 } TObject;
69 
70 
71 /*
72 ** String headers for string table
73 */
74 
75 /*
76 ** most `malloc' libraries allocate memory in blocks of 8 bytes. TSPACK
77 ** tries to make sizeof(TString) a multiple of this granularity, to reduce
78 ** waste of space.
79 */
80 #define TSPACK	((int)sizeof(int))
81 
82 typedef struct TString {
83   union {
84     struct {  /* for strings */
85       unsigned long hash;
86       int constindex;  /* hint to reuse constants */
87     } s;
88     struct {  /* for userdata */
89       int tag;
90       void *value;
91     } d;
92   } u;
93   size_t len;
94   struct TString *nexthash;  /* chain for hash table */
95   int marked;
96   char str[TSPACK];   /* variable length string!! must be the last field! */
97 } TString;
98 
99 
100 /*
101 ** Function Prototypes
102 */
103 typedef struct Proto {
104   Number *knum;  /* Number numbers used by the function */
105   int nknum;  /* size of `knum' */
106   struct TString **kstr;  /* strings used by the function */
107   int nkstr;  /* size of `kstr' */
108   struct Proto **kproto;  /* functions defined inside the function */
109   int nkproto;  /* size of `kproto' */
110   Instruction *code;
111   int ncode;  /* size of `code'; when 0 means an incomplete `Proto' */
112   short numparams;
113   short is_vararg;
114   short maxstacksize;
115   short marked;
116   struct Proto *next;
117   /* debug information */
118   int *lineinfo;  /* map from opcodes to source lines */
119   int nlineinfo;  /* size of `lineinfo' */
120   int nlocvars;
121   struct LocVar *locvars;  /* information about local variables */
122   int lineDefined;
123   TString  *source;
124 } Proto;
125 
126 
127 typedef struct LocVar {
128   TString *varname;
129   int startpc;  /* first point where variable is active */
130   int endpc;    /* first point where variable is dead */
131 } LocVar;
132 
133 
134 /*
135 ** Closures
136 */
137 typedef struct Closure {
138   union {
139     lua_CFunction c;  /* C functions */
140     struct Proto *l;  /* Lua functions */
141   } f;
142   struct Closure *next;
143   struct Closure *mark;  /* marked closures (point to itself when not marked) */
144   short isC;  /* 0 for Lua functions, 1 for C functions */
145   short nupvalues;
146   TObject upvalue[1];
147 } Closure;
148 
149 
150 #define iscfunction(o)	(ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
151 
152 
153 typedef struct Node {
154   TObject key;
155   TObject val;
156   struct Node *next;  /* for chaining */
157 } Node;
158 
159 typedef struct Hash {
160   Node *node;
161   int htag;
162   int size;
163   Node *firstfree;  /* this position is free; all positions after it are full */
164   struct Hash *next;
165   struct Hash *mark;  /* marked tables (point to itself when not marked) */
166 } Hash;
167 
168 
169 /* unmarked tables and closures are represented by pointing `mark' to
170 ** themselves
171 */
172 #define ismarked(x)	((x)->mark != (x))
173 
174 
175 /*
176 ** informations about a call (for debugging)
177 */
178 typedef struct CallInfo {
179   struct Closure *func;  /* function being called */
180   const Instruction **pc;  /* current pc of called function */
181   int lastpc;  /* last pc traced */
182   int line;  /* current line */
183   int refi;  /* current index in `lineinfo' */
184 } CallInfo;
185 
186 
187 extern const TObject luaO_nilobject;
188 extern const char *const luaO_typenames[];
189 
190 
191 #define luaO_typename(o)	(luaO_typenames[ttype(o)])
192 
193 
194 lint32 luaO_power2 (lint32 n);
195 char *luaO_openspace (lua_State *L, size_t n);
196 
197 int luaO_equalObj (const TObject *t1, const TObject *t2);
198 int luaO_str2d (const char *s, Number *result);
199 
200 void luaO_verror (lua_State *L, const char *fmt, ...);
201 void luaO_chunkid (char *out, const char *source, int len);
202 
203 
204 #endif
205