1 /* $NetBSD: lobject.h,v 1.1.1.2 2012/03/15 00:08:07 alnsn Exp $ */
4 ** $Id: lobject.h,v 1.1.1.2 2012/03/15 00:08:07 alnsn Exp $
5 ** Type definitions for Lua objects
6 ** See Copyright Notice in lua.h
21 /* tags for values visible from Lua */
22 #define LAST_TAG LUA_TTHREAD
24 #define NUM_TAGS (LAST_TAG+1)
28 ** Extra tags for non-values
30 #define LUA_TPROTO (LAST_TAG+1)
31 #define LUA_TUPVAL (LAST_TAG+2)
32 #define LUA_TDEADKEY (LAST_TAG+3)
36 ** Union of all collectable objects
38 typedef union GCObject GCObject
;
42 ** Common Header for all collectable objects (in macro form, to be
43 ** included in other objects)
45 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
49 ** Common header in struct form
51 typedef struct GCheader
{
59 ** Union of all Lua values
73 #define TValuefields Value value; int tt
75 typedef struct lua_TValue
{
80 /* Macros to test type */
81 #define ttisnil(o) (ttype(o) == LUA_TNIL)
82 #define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
83 #define ttisstring(o) (ttype(o) == LUA_TSTRING)
84 #define ttistable(o) (ttype(o) == LUA_TTABLE)
85 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
86 #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
87 #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
88 #define ttisthread(o) (ttype(o) == LUA_TTHREAD)
89 #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
91 /* Macros to access values */
92 #define ttype(o) ((o)->tt)
93 #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
94 #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
95 #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
96 #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
97 #define tsvalue(o) (&rawtsvalue(o)->tsv)
98 #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
99 #define uvalue(o) (&rawuvalue(o)->uv)
100 #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
101 #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
102 #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
103 #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
105 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
108 ** for internal debug only
110 #define checkconsistency(obj) \
111 lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
113 #define checkliveness(g,obj) \
114 lua_assert(!iscollectable(obj) || \
115 ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
118 /* Macros to set values */
119 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
121 #define setnvalue(obj,x) \
122 { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
124 #define setpvalue(obj,x) \
125 { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
127 #define setbvalue(obj,x) \
128 { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
130 #define setsvalue(L,obj,x) \
131 { TValue *i_o=(obj); \
132 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
133 checkliveness(G(L),i_o); }
135 #define setuvalue(L,obj,x) \
136 { TValue *i_o=(obj); \
137 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
138 checkliveness(G(L),i_o); }
140 #define setthvalue(L,obj,x) \
141 { TValue *i_o=(obj); \
142 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
143 checkliveness(G(L),i_o); }
145 #define setclvalue(L,obj,x) \
146 { TValue *i_o=(obj); \
147 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
148 checkliveness(G(L),i_o); }
150 #define sethvalue(L,obj,x) \
151 { TValue *i_o=(obj); \
152 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
153 checkliveness(G(L),i_o); }
155 #define setptvalue(L,obj,x) \
156 { TValue *i_o=(obj); \
157 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
158 checkliveness(G(L),i_o); }
163 #define setobj(L,obj1,obj2) \
164 { const TValue *o2=(obj2); TValue *o1=(obj1); \
165 o1->value = o2->value; o1->tt=o2->tt; \
166 checkliveness(G(L),o1); }
170 ** different types of sets, according to destination
173 /* from stack to (same) stack */
174 #define setobjs2s setobj
175 /* to stack (not from same stack) */
176 #define setobj2s setobj
177 #define setsvalue2s setsvalue
178 #define sethvalue2s sethvalue
179 #define setptvalue2s setptvalue
180 /* from table to same table */
181 #define setobjt2t setobj
183 #define setobj2t setobj
185 #define setobj2n setobj
186 #define setsvalue2n setsvalue
188 #define setttype(obj, tt) (ttype(obj) = (tt))
191 #define iscollectable(o) (ttype(o) >= LUA_TSTRING)
195 typedef TValue
*StkId
; /* index to stack elements */
199 ** String headers for string table
201 typedef union TString
{
202 L_Umaxalign dummy
; /* ensures maximum alignment for strings */
212 #define getstr(ts) cast(const char *, (ts) + 1)
213 #define svalue(o) getstr(rawtsvalue(o))
217 typedef union Udata
{
218 L_Umaxalign dummy
; /* ensures maximum alignment for `local' udata */
221 struct Table
*metatable
;
231 ** Function Prototypes
233 typedef struct Proto
{
235 TValue
*k
; /* constants used by the function */
237 struct Proto
**p
; /* functions defined inside the function */
238 int *lineinfo
; /* map from opcodes to source lines */
239 struct LocVar
*locvars
; /* information about local variables */
240 TString
**upvalues
; /* upvalue names */
243 int sizek
; /* size of `k' */
246 int sizep
; /* size of `p' */
251 lu_byte nups
; /* number of upvalues */
254 lu_byte maxstacksize
;
258 /* masks for new-style vararg */
259 #define VARARG_HASARG 1
260 #define VARARG_ISVARARG 2
261 #define VARARG_NEEDSARG 4
264 typedef struct LocVar
{
266 int startpc
; /* first point where variable is active */
267 int endpc
; /* first point where variable is dead */
276 typedef struct UpVal
{
278 TValue
*v
; /* points to stack or to its own value */
280 TValue value
; /* the value (when closed) */
281 struct { /* double linked list (when open) */
293 #define ClosureHeader \
294 CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
297 typedef struct CClosure
{
304 typedef struct LClosure
{
311 typedef union Closure
{
317 #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
318 #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
328 struct Node
*next
; /* for chaining */
334 typedef struct Node
{
340 typedef struct Table
{
342 lu_byte flags
; /* 1<<p means tagmethod(p) is not present */
343 lu_byte lsizenode
; /* log2 of size of `node' array */
344 struct Table
*metatable
;
345 TValue
*array
; /* array part */
347 Node
*lastfree
; /* any free position is before this position */
349 int sizearray
; /* size of `array' array */
355 ** `module' operation for hashing (size is always a power of 2)
357 #define lmod(s,size) \
358 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
361 #define twoto(x) (1<<(x))
362 #define sizenode(t) (twoto((t)->lsizenode))
365 #define luaO_nilobject (&luaO_nilobject_)
367 LUAI_DATA
const TValue luaO_nilobject_
;
369 #define ceillog2(x) (luaO_log2((x)-1) + 1)
371 LUAI_FUNC
int luaO_log2 (unsigned int x
);
372 LUAI_FUNC
int luaO_int2fb (unsigned int x
);
373 LUAI_FUNC
int luaO_fb2int (int x
);
374 LUAI_FUNC
int luaO_rawequalObj (const TValue
*t1
, const TValue
*t2
);
375 LUAI_FUNC
int luaO_str2d (const char *s
, lua_Number
*result
);
376 LUAI_FUNC
const char *luaO_pushvfstring (lua_State
*L
, const char *fmt
,
378 LUAI_FUNC
const char *luaO_pushfstring (lua_State
*L
, const char *fmt
, ...);
379 LUAI_FUNC
void luaO_chunkid (char *out
, const char *source
, size_t len
);