2 ** $Id: lobject.h,v 2.117 2016/08/01 19:51:24 roberto Exp $
3 ** Type definitions for Lua objects
4 ** See Copyright Notice in lua.h
20 ** Extra tags for non-values
22 #define LUA_TPROTO LUA_NUMTAGS /* function prototypes */
23 #define LUA_TDEADKEY (LUA_NUMTAGS+1) /* removed keys in tables */
26 ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
28 #define LUA_TOTALTAGS (LUA_TPROTO + 2)
32 ** tags for Tagged Values have the following use of bits:
33 ** bits 0-3: actual tag (a LUA_T* value)
34 ** bits 4-5: variant bits
35 ** bit 6: whether value is collectable
40 ** LUA_TFUNCTION variants:
42 ** 1 - light C function
43 ** 2 - regular C function (closure)
46 /* Variant tags for functions */
47 #define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */
48 #define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */
49 #define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
52 /* Variant tags for strings */
53 #define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */
54 #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
57 /* Variant tags for numbers */
58 #define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */
59 #define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */
62 /* Bit mark for collectable types */
63 #define BIT_ISCOLLECTABLE (1 << 6)
65 /* mark a tag as collectable */
66 #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
70 ** Common type for all collectable objects
72 typedef struct GCObject GCObject;
76 ** Common Header for all collectable objects (in macro form, to be
77 ** included in other objects)
79 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
83 ** Common type has only the common header
93 ** Tagged Values. This is the basic representation of values in Lua,
94 ** an actual value plus a tag with its type.
98 ** Union of all Lua values
100 typedef union Value {
101 GCObject *gc; /* collectable objects */
102 void *p; /* light userdata */
103 int b; /* booleans */
104 lua_CFunction f; /* light C functions */
105 lua_Integer i; /* integer numbers */
106 lua_Number n; /* float numbers */
110 #define TValuefields Value value_; int tt_
113 typedef struct lua_TValue {
119 /* macro defining a nil value */
120 #define NILCONSTANT {NULL}, LUA_TNIL
123 #define val_(o) ((o)->value_)
126 /* raw type tag of a TValue */
127 #define rttype(o) ((o)->tt_)
129 /* tag with no variants (bits 0-3) */
130 #define novariant(x) ((x) & 0x0F)
132 /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
133 #define ttype(o) (rttype(o) & 0x3F)
135 /* type tag of a TValue with no variants (bits 0-3) */
136 #define ttnov(o) (novariant(rttype(o)))
139 /* Macros to test type */
140 #define checktag(o,t) (rttype(o) == (t))
141 #define checktype(o,t) (ttnov(o) == (t))
142 #define ttisnumber(o) checktype((o), LUA_TNUMBER)
143 #define ttisfloat(o) checktag((o), LUA_TNUMFLT)
144 #define ttisinteger(o) checktag((o), LUA_TNUMINT)
145 #define ttisnil(o) checktag((o), LUA_TNIL)
146 #define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
147 #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
148 #define ttisstring(o) checktype((o), LUA_TSTRING)
149 #define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
150 #define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
151 #define ttistable(o) checktag((o), ctb(LUA_TTABLE))
152 #define ttisfunction(o) checktype(o, LUA_TFUNCTION)
153 #define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
154 #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
155 #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
156 #define ttislcf(o) checktag((o), LUA_TLCF)
157 #define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA))
158 #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
159 #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)
162 /* Macros to access values */
163 #define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
164 #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
165 #define nvalue(o) check_exp(ttisnumber(o), \
166 (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
167 #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
168 #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
169 #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
170 #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
171 #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
172 #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
173 #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
174 #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
175 #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
176 #define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
177 #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
178 /* a dead value may get the 'gc' field, but cannot access its contents */
179 #define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
181 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
184 #define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
187 /* Macros for internal tests */
188 #define righttt(obj) (ttype(obj) == gcvalue(obj)->tt)
190 #define checkliveness(L,obj) \
191 lua_longassert(!iscollectable(obj) || \
192 (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))
195 /* Macros to set values */
196 #define settt_(o,t) ((o)->tt_=(t))
198 #define setfltvalue(obj,x) \
199 { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
201 #define chgfltvalue(obj,x) \
202 { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
204 #define setivalue(obj,x) \
205 { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
207 #define chgivalue(obj,x) \
208 { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
210 #define setnilvalue(obj) settt_(obj, LUA_TNIL)
212 #define setfvalue(obj,x) \
213 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
215 #define setpvalue(obj,x) \
216 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
218 #define setbvalue(obj,x) \
219 { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
221 #define setgcovalue(L,obj,x) \
222 { TValue *io = (obj); GCObject *i_g=(x); \
223 val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
225 #define setsvalue(L,obj,x) \
226 { TValue *io = (obj); TString *x_ = (x); \
227 val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
228 checkliveness(L,io); }
230 #define setuvalue(L,obj,x) \
231 { TValue *io = (obj); Udata *x_ = (x); \
232 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
233 checkliveness(L,io); }
235 #define setthvalue(L,obj,x) \
236 { TValue *io = (obj); lua_State *x_ = (x); \
237 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
238 checkliveness(L,io); }
240 #define setclLvalue(L,obj,x) \
241 { TValue *io = (obj); LClosure *x_ = (x); \
242 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
243 checkliveness(L,io); }
245 #define setclCvalue(L,obj,x) \
246 { TValue *io = (obj); CClosure *x_ = (x); \
247 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
248 checkliveness(L,io); }
250 #define sethvalue(L,obj,x) \
251 { TValue *io = (obj); Table *x_ = (x); \
252 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \
253 checkliveness(L,io); }
255 #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
259 #define setobj(L,obj1,obj2) \
260 { TValue *io1=(obj1); *io1 = *(obj2); \
261 (void)L; checkliveness(L,io1); }
265 ** different types of assignments, according to destination
268 /* from stack to (same) stack */
269 #define setobjs2s setobj
270 /* to stack (not from same stack) */
271 #define setobj2s setobj
272 #define setsvalue2s setsvalue
273 #define sethvalue2s sethvalue
274 #define setptvalue2s setptvalue
275 /* from table to same table */
276 #define setobjt2t setobj
278 #define setobj2n setobj
279 #define setsvalue2n setsvalue
281 /* to table (define it as an expression to be used in macros) */
282 #define setobj2t(L,o1,o2) ((void)L, *(o1)=*(o2), checkliveness(L,(o1)))
288 ** {======================================================
289 ** types and prototypes
290 ** =======================================================
294 typedef TValue *StkId; /* index to stack elements */
300 ** Header for string value; string bytes follow the end of this structure
301 ** (aligned according to 'UTString'; see next).
303 typedef struct TString {
305 lu_byte extra; /* reserved words for short strings; "has hash" for longs */
306 lu_byte shrlen; /* length for short strings */
309 size_t lnglen; /* length for long strings */
310 struct TString *hnext; /* linked list for hash table */
316 ** Ensures that address after this type is always fully aligned.
318 typedef union UTString {
319 L_Umaxalign dummy; /* ensures maximum alignment for strings */
325 ** Get the actual string (array of bytes) from a 'TString'.
326 ** (Access to 'extra' ensures that value is really a 'TString'.)
329 check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString))
332 /* get the actual string (array of bytes) from a Lua value */
333 #define svalue(o) getstr(tsvalue(o))
335 /* get string length from 'TString *s' */
336 #define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen)
338 /* get string length from 'TValue *o' */
339 #define vslen(o) tsslen(tsvalue(o))
343 ** Header for userdata; memory area follows the end of this structure
344 ** (aligned according to 'UUdata'; see next).
346 typedef struct Udata {
348 lu_byte ttuv_; /* user value's tag */
349 struct Table *metatable;
350 size_t len; /* number of bytes */
351 union Value user_; /* user value */
356 ** Ensures that address after this type is always fully aligned.
358 typedef union UUdata {
359 L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */
365 ** Get the address of memory block inside 'Udata'.
366 ** (Access to 'ttuv_' ensures that value is really a 'Udata'.)
368 #define getudatamem(u) \
369 check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata)))
371 #define setuservalue(L,u,o) \
372 { const TValue *io=(o); Udata *iu = (u); \
373 iu->user_ = io->value_; iu->ttuv_ = rttype(io); \
374 checkliveness(L,io); }
377 #define getuservalue(L,u,o) \
378 { TValue *io=(o); const Udata *iu = (u); \
379 io->value_ = iu->user_; settt_(io, iu->ttuv_); \
380 checkliveness(L,io); }
384 ** Description of an upvalue for function prototypes
386 typedef struct Upvaldesc {
387 TString *name; /* upvalue name (for debug information) */
388 lu_byte instack; /* whether it is in stack (register) */
389 lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
394 ** Description of a local variable for function prototypes
395 ** (used for debug information)
397 typedef struct LocVar {
399 int startpc; /* first point where variable is active */
400 int endpc; /* first point where variable is dead */
405 ** Function Prototypes
407 typedef struct Proto {
409 lu_byte numparams; /* number of fixed parameters */
411 lu_byte maxstacksize; /* number of registers needed by this function */
412 int sizeupvalues; /* size of 'upvalues' */
413 int sizek; /* size of 'k' */
416 int sizep; /* size of 'p' */
418 int linedefined; /* debug information */
419 int lastlinedefined; /* debug information */
420 TValue *k; /* constants used by the function */
421 Instruction *code; /* opcodes */
422 struct Proto **p; /* functions defined inside the function */
423 int *lineinfo; /* map from opcodes to source lines (debug information) */
424 LocVar *locvars; /* information about local variables (debug information) */
425 Upvaldesc *upvalues; /* upvalue information */
426 struct LClosure *cache; /* last-created closure with this prototype */
427 TString *source; /* used for debug information */
436 typedef struct UpVal UpVal;
443 #define ClosureHeader \
444 CommonHeader; lu_byte nupvalues; GCObject *gclist
446 typedef struct CClosure {
449 TValue upvalue[1]; /* list of upvalues */
453 typedef struct LClosure {
456 UpVal *upvals[1]; /* list of upvalues */
460 typedef union Closure {
466 #define isLfunction(o) ttisLclosure(o)
468 #define getproto(o) (clLvalue(o)->p)
478 int next; /* for chaining (offset for next node) */
484 /* copy a value into a key without messing up field 'next' */
485 #define setnodekey(L,key,obj) \
486 { TKey *k_=(key); const TValue *io_=(obj); \
487 k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \
488 (void)L; checkliveness(L,io_); }
491 typedef struct Node {
497 typedef struct Table {
499 lu_byte flags; /* 1<<p means tagmethod(p) is not present */
500 lu_byte lsizenode; /* log2 of size of 'node' array */
501 unsigned int sizearray; /* size of 'array' array */
502 TValue *array; /* array part */
504 Node *lastfree; /* any free position is before this position */
505 struct Table *metatable;
512 ** 'module' operation for hashing (size is always a power of 2)
514 #define lmod(s,size) \
515 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
518 #define twoto(x) (1<<(x))
519 #define sizenode(t) (twoto((t)->lsizenode))
523 ** (address of) a fixed nil value
525 #define luaO_nilobject (&luaO_nilobject_)
528 LUAI_DDEC const TValue luaO_nilobject_;
530 /* size of buffer for 'luaO_utf8esc' function */
533 LUAI_FUNC int luaO_int2fb (unsigned int x);
534 LUAI_FUNC int luaO_fb2int (int x);
535 LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
536 LUAI_FUNC int luaO_ceillog2 (unsigned int x);
537 LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
538 const TValue *p2, TValue *res);
539 LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
540 LUAI_FUNC int luaO_hexavalue (int c);
541 LUAI_FUNC void luaO_tostring (lua_State *L, StkId obj);
542 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
544 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
545 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);