1 /*
2 ** $Id: lobject.h $
3 ** Type definitions for Lua objects
4 ** See Copyright Notice in lua.h
5 */
6 
7 
8 #ifndef lobject_h
9 #define lobject_h
10 
11 
12 #include <stdarg.h>
13 
14 
15 #include "llimits.h"
16 #include "lua.h"
17 
18 
19 /*
20 ** Extra types for collectable non-values
21 */
22 #define LUA_TUPVAL	LUA_NUMTYPES  /* upvalues */
23 #define LUA_TPROTO	(LUA_NUMTYPES+1)  /* function prototypes */
24 
25 
26 /*
27 ** number of all possible types (including LUA_TNONE)
28 */
29 #define LUA_TOTALTYPES		(LUA_TPROTO + 2)
30 
31 
32 /*
33 ** tags for Tagged Values have the following use of bits:
34 ** bits 0-3: actual tag (a LUA_T* constant)
35 ** bits 4-5: variant bits
36 ** bit 6: whether value is collectable
37 */
38 
39 /* add variant bits to a type */
40 #define makevariant(t,v)	((t) | ((v) << 4))
41 
42 
43 
44 /*
45 ** Union of all Lua values
46 */
47 typedef union Value {
48   struct GCObject *gc;    /* collectable objects */
49   void *p;         /* light userdata */
50   lua_CFunction f; /* light C functions */
51   lua_Integer i;   /* integer numbers */
52   lua_Number n;    /* float numbers */
53 } Value;
54 
55 
56 /*
57 ** Tagged Values. This is the basic representation of values in Lua:
58 ** an actual value plus a tag with its type.
59 */
60 
61 #define TValuefields	Value value_; lu_byte tt_
62 
63 typedef struct TValue {
64   TValuefields;
65 } TValue;
66 
67 
68 #define val_(o)		((o)->value_)
69 #define valraw(o)	(&val_(o))
70 
71 
72 /* raw type tag of a TValue */
73 #define rawtt(o)	((o)->tt_)
74 
75 /* tag with no variants (bits 0-3) */
76 #define novariant(t)	((t) & 0x0F)
77 
78 /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
79 #define withvariant(t)	((t) & 0x3F)
80 #define ttypetag(o)	withvariant(rawtt(o))
81 
82 /* type of a TValue */
83 #define ttype(o)	(novariant(rawtt(o)))
84 
85 
86 /* Macros to test type */
87 #define checktag(o,t)		(rawtt(o) == (t))
88 #define checktype(o,t)		(ttype(o) == (t))
89 
90 
91 /* Macros for internal tests */
92 
93 /* collectable object has the same tag as the original value */
94 #define righttt(obj)		(ttypetag(obj) == gcvalue(obj)->tt)
95 
96 /*
97 ** Any value being manipulated by the program either is non
98 ** collectable, or the collectable object has the right tag
99 ** and it is not dead. The option 'L == NULL' allows other
100 ** macros using this one to be used where L is not available.
101 */
102 #define checkliveness(L,obj) \
103 	((void)L, lua_longassert(!iscollectable(obj) || \
104 		(righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))))
105 
106 
107 /* Macros to set values */
108 
109 /* set a value's tag */
110 #define settt_(o,t)	((o)->tt_=(t))
111 
112 
113 /* main macro to copy values (from 'obj1' to 'obj2') */
114 #define setobj(L,obj1,obj2) \
115 	{ TValue *io1=(obj1); const TValue *io2=(obj2); \
116           io1->value_ = io2->value_; settt_(io1, io2->tt_); \
117 	  checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); }
118 
119 /*
120 ** Different types of assignments, according to source and destination.
121 ** (They are mostly equal now, but may be different in the future.)
122 */
123 
124 /* from stack to stack */
125 #define setobjs2s(L,o1,o2)	setobj(L,s2v(o1),s2v(o2))
126 /* to stack (not from same stack) */
127 #define setobj2s(L,o1,o2)	setobj(L,s2v(o1),o2)
128 /* from table to same table */
129 #define setobjt2t	setobj
130 /* to new object */
131 #define setobj2n	setobj
132 /* to table */
133 #define setobj2t	setobj
134 
135 
136 /*
137 ** Entries in the Lua stack
138 */
139 typedef union StackValue {
140   TValue val;
141 } StackValue;
142 
143 
144 /* index to stack elements */
145 typedef StackValue *StkId;
146 
147 /* convert a 'StackValue' to a 'TValue' */
148 #define s2v(o)	(&(o)->val)
149 
150 
151 
152 /*
153 ** {==================================================================
154 ** Nil
155 ** ===================================================================
156 */
157 
158 /* Standard nil */
159 #define LUA_VNIL	makevariant(LUA_TNIL, 0)
160 
161 /* Empty slot (which might be different from a slot containing nil) */
162 #define LUA_VEMPTY	makevariant(LUA_TNIL, 1)
163 
164 /* Value returned for a key not found in a table (absent key) */
165 #define LUA_VABSTKEY	makevariant(LUA_TNIL, 2)
166 
167 
168 /* macro to test for (any kind of) nil */
169 #define ttisnil(v)		checktype((v), LUA_TNIL)
170 
171 
172 /* macro to test for a standard nil */
173 #define ttisstrictnil(o)	checktag((o), LUA_VNIL)
174 
175 
176 #define setnilvalue(obj) settt_(obj, LUA_VNIL)
177 
178 
179 #define isabstkey(v)		checktag((v), LUA_VABSTKEY)
180 
181 
182 /*
183 ** macro to detect non-standard nils (used only in assertions)
184 */
185 #define isnonstrictnil(v)	(ttisnil(v) && !ttisstrictnil(v))
186 
187 
188 /*
189 ** By default, entries with any kind of nil are considered empty.
190 ** (In any definition, values associated with absent keys must also
191 ** be accepted as empty.)
192 */
193 #define isempty(v)		ttisnil(v)
194 
195 
196 /* macro defining a value corresponding to an absent key */
197 #define ABSTKEYCONSTANT		{NULL}, LUA_VABSTKEY
198 
199 
200 /* mark an entry as empty */
201 #define setempty(v)		settt_(v, LUA_VEMPTY)
202 
203 
204 
205 /* }================================================================== */
206 
207 
208 /*
209 ** {==================================================================
210 ** Booleans
211 ** ===================================================================
212 */
213 
214 
215 #define LUA_VFALSE	makevariant(LUA_TBOOLEAN, 0)
216 #define LUA_VTRUE	makevariant(LUA_TBOOLEAN, 1)
217 
218 #define ttisboolean(o)		checktype((o), LUA_TBOOLEAN)
219 #define ttisfalse(o)		checktag((o), LUA_VFALSE)
220 #define ttistrue(o)		checktag((o), LUA_VTRUE)
221 
222 
223 #define l_isfalse(o)	(ttisfalse(o) || ttisnil(o))
224 
225 
226 #define setbfvalue(obj)		settt_(obj, LUA_VFALSE)
227 #define setbtvalue(obj)		settt_(obj, LUA_VTRUE)
228 
229 /* }================================================================== */
230 
231 
232 /*
233 ** {==================================================================
234 ** Threads
235 ** ===================================================================
236 */
237 
238 #define LUA_VTHREAD		makevariant(LUA_TTHREAD, 0)
239 
240 #define ttisthread(o)		checktag((o), ctb(LUA_VTHREAD))
241 
242 #define thvalue(o)	check_exp(ttisthread(o), gco2th(val_(o).gc))
243 
244 #define setthvalue(L,obj,x) \
245   { TValue *io = (obj); lua_State *x_ = (x); \
246     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \
247     checkliveness(L,io); }
248 
249 #define setthvalue2s(L,o,t)	setthvalue(L,s2v(o),t)
250 
251 /* }================================================================== */
252 
253 
254 /*
255 ** {==================================================================
256 ** Collectable Objects
257 ** ===================================================================
258 */
259 
260 /*
261 ** Common Header for all collectable objects (in macro form, to be
262 ** included in other objects)
263 */
264 #define CommonHeader	struct GCObject *next; lu_byte tt; lu_byte marked
265 
266 
267 /* Common type for all collectable objects */
268 typedef struct GCObject {
269   CommonHeader;
270 } GCObject;
271 
272 
273 /* Bit mark for collectable types */
274 #define BIT_ISCOLLECTABLE	(1 << 6)
275 
276 #define iscollectable(o)	(rawtt(o) & BIT_ISCOLLECTABLE)
277 
278 /* mark a tag as collectable */
279 #define ctb(t)			((t) | BIT_ISCOLLECTABLE)
280 
281 #define gcvalue(o)	check_exp(iscollectable(o), val_(o).gc)
282 
283 #define gcvalueraw(v)	((v).gc)
284 
285 #define setgcovalue(L,obj,x) \
286   { TValue *io = (obj); GCObject *i_g=(x); \
287     val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
288 
289 /* }================================================================== */
290 
291 
292 /*
293 ** {==================================================================
294 ** Numbers
295 ** ===================================================================
296 */
297 
298 /* Variant tags for numbers */
299 #define LUA_VNUMINT	makevariant(LUA_TNUMBER, 0)  /* integer numbers */
300 #define LUA_VNUMFLT	makevariant(LUA_TNUMBER, 1)  /* float numbers */
301 
302 #define ttisnumber(o)		checktype((o), LUA_TNUMBER)
303 #define ttisfloat(o)		checktag((o), LUA_VNUMFLT)
304 #define ttisinteger(o)		checktag((o), LUA_VNUMINT)
305 
306 #define nvalue(o)	check_exp(ttisnumber(o), \
307 	(ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
308 #define fltvalue(o)	check_exp(ttisfloat(o), val_(o).n)
309 #define ivalue(o)	check_exp(ttisinteger(o), val_(o).i)
310 
311 #define fltvalueraw(v)	((v).n)
312 #define ivalueraw(v)	((v).i)
313 
314 #define setfltvalue(obj,x) \
315   { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); }
316 
317 #define chgfltvalue(obj,x) \
318   { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
319 
320 #define setivalue(obj,x) \
321   { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); }
322 
323 #define chgivalue(obj,x) \
324   { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
325 
326 /* }================================================================== */
327 
328 
329 /*
330 ** {==================================================================
331 ** Strings
332 ** ===================================================================
333 */
334 
335 /* Variant tags for strings */
336 #define LUA_VSHRSTR	makevariant(LUA_TSTRING, 0)  /* short strings */
337 #define LUA_VLNGSTR	makevariant(LUA_TSTRING, 1)  /* long strings */
338 
339 #define ttisstring(o)		checktype((o), LUA_TSTRING)
340 #define ttisshrstring(o)	checktag((o), ctb(LUA_VSHRSTR))
341 #define ttislngstring(o)	checktag((o), ctb(LUA_VLNGSTR))
342 
343 #define tsvalueraw(v)	(gco2ts((v).gc))
344 
345 #define tsvalue(o)	check_exp(ttisstring(o), gco2ts(val_(o).gc))
346 
347 #define setsvalue(L,obj,x) \
348   { TValue *io = (obj); TString *x_ = (x); \
349     val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
350     checkliveness(L,io); }
351 
352 /* set a string to the stack */
353 #define setsvalue2s(L,o,s)	setsvalue(L,s2v(o),s)
354 
355 /* set a string to a new object */
356 #define setsvalue2n	setsvalue
357 
358 
359 /*
360 ** Header for a string value.
361 */
362 typedef struct TString {
363   CommonHeader;
364   lu_byte extra;  /* reserved words for short strings; "has hash" for longs */
365   lu_byte shrlen;  /* length for short strings */
366   unsigned int hash;
367   union {
368     size_t lnglen;  /* length for long strings */
369     struct TString *hnext;  /* linked list for hash table */
370   } u;
371   char contents[1];
372 } TString;
373 
374 
375 
376 /*
377 ** Get the actual string (array of bytes) from a 'TString'.
378 */
379 #define getstr(ts)  ((ts)->contents)
380 
381 
382 /* get the actual string (array of bytes) from a Lua value */
383 #define svalue(o)       getstr(tsvalue(o))
384 
385 /* get string length from 'TString *s' */
386 #define tsslen(s)	((s)->tt == LUA_VSHRSTR ? (s)->shrlen : (s)->u.lnglen)
387 
388 /* get string length from 'TValue *o' */
389 #define vslen(o)	tsslen(tsvalue(o))
390 
391 /* }================================================================== */
392 
393 
394 /*
395 ** {==================================================================
396 ** Userdata
397 ** ===================================================================
398 */
399 
400 
401 /*
402 ** Light userdata should be a variant of userdata, but for compatibility
403 ** reasons they are also different types.
404 */
405 #define LUA_VLIGHTUSERDATA	makevariant(LUA_TLIGHTUSERDATA, 0)
406 
407 #define LUA_VUSERDATA		makevariant(LUA_TUSERDATA, 0)
408 
409 #define ttislightuserdata(o)	checktag((o), LUA_VLIGHTUSERDATA)
410 #define ttisfulluserdata(o)	checktag((o), ctb(LUA_VUSERDATA))
411 
412 #define pvalue(o)	check_exp(ttislightuserdata(o), val_(o).p)
413 #define uvalue(o)	check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
414 
415 #define pvalueraw(v)	((v).p)
416 
417 #define setpvalue(obj,x) \
418   { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); }
419 
420 #define setuvalue(L,obj,x) \
421   { TValue *io = (obj); Udata *x_ = (x); \
422     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \
423     checkliveness(L,io); }
424 
425 
426 /* Ensures that addresses after this type are always fully aligned. */
427 typedef union UValue {
428   TValue uv;
429   LUAI_MAXALIGN;  /* ensures maximum alignment for udata bytes */
430 } UValue;
431 
432 
433 /*
434 ** Header for userdata with user values;
435 ** memory area follows the end of this structure.
436 */
437 typedef struct Udata {
438   CommonHeader;
439   unsigned short nuvalue;  /* number of user values */
440   size_t len;  /* number of bytes */
441   struct Table *metatable;
442   GCObject *gclist;
443   UValue uv[1];  /* user values */
444 } Udata;
445 
446 
447 /*
448 ** Header for userdata with no user values. These userdata do not need
449 ** to be gray during GC, and therefore do not need a 'gclist' field.
450 ** To simplify, the code always use 'Udata' for both kinds of userdata,
451 ** making sure it never accesses 'gclist' on userdata with no user values.
452 ** This structure here is used only to compute the correct size for
453 ** this representation. (The 'bindata' field in its end ensures correct
454 ** alignment for binary data following this header.)
455 */
456 typedef struct Udata0 {
457   CommonHeader;
458   unsigned short nuvalue;  /* number of user values */
459   size_t len;  /* number of bytes */
460   struct Table *metatable;
461   union {LUAI_MAXALIGN;} bindata;
462 } Udata0;
463 
464 
465 /* compute the offset of the memory area of a userdata */
466 #define udatamemoffset(nuv) \
467 	((nuv) == 0 ? offsetof(Udata0, bindata)  \
468                     : offsetof(Udata, uv) + (sizeof(UValue) * (nuv)))
469 
470 /* get the address of the memory block inside 'Udata' */
471 #define getudatamem(u)	(cast_charp(u) + udatamemoffset((u)->nuvalue))
472 
473 /* compute the size of a userdata */
474 #define sizeudata(nuv,nb)	(udatamemoffset(nuv) + (nb))
475 
476 /* }================================================================== */
477 
478 
479 /*
480 ** {==================================================================
481 ** Prototypes
482 ** ===================================================================
483 */
484 
485 #define LUA_VPROTO	makevariant(LUA_TPROTO, 0)
486 
487 
488 /*
489 ** Description of an upvalue for function prototypes
490 */
491 typedef struct Upvaldesc {
492   TString *name;  /* upvalue name (for debug information) */
493   lu_byte instack;  /* whether it is in stack (register) */
494   lu_byte idx;  /* index of upvalue (in stack or in outer function's list) */
495   lu_byte kind;  /* kind of corresponding variable */
496 } Upvaldesc;
497 
498 
499 /*
500 ** Description of a local variable for function prototypes
501 ** (used for debug information)
502 */
503 typedef struct LocVar {
504   TString *varname;
505   int startpc;  /* first point where variable is active */
506   int endpc;    /* first point where variable is dead */
507 } LocVar;
508 
509 
510 /*
511 ** Associates the absolute line source for a given instruction ('pc').
512 ** The array 'lineinfo' gives, for each instruction, the difference in
513 ** lines from the previous instruction. When that difference does not
514 ** fit into a byte, Lua saves the absolute line for that instruction.
515 ** (Lua also saves the absolute line periodically, to speed up the
516 ** computation of a line number: we can use binary search in the
517 ** absolute-line array, but we must traverse the 'lineinfo' array
518 ** linearly to compute a line.)
519 */
520 typedef struct AbsLineInfo {
521   int pc;
522   int line;
523 } AbsLineInfo;
524 
525 /*
526 ** Function Prototypes
527 */
528 typedef struct Proto {
529   CommonHeader;
530   lu_byte numparams;  /* number of fixed (named) parameters */
531   lu_byte is_vararg;
532   lu_byte maxstacksize;  /* number of registers needed by this function */
533   int sizeupvalues;  /* size of 'upvalues' */
534   int sizek;  /* size of 'k' */
535   int sizecode;
536   int sizelineinfo;
537   int sizep;  /* size of 'p' */
538   int sizelocvars;
539   int sizeabslineinfo;  /* size of 'abslineinfo' */
540   int linedefined;  /* debug information  */
541   int lastlinedefined;  /* debug information  */
542   TValue *k;  /* constants used by the function */
543   Instruction *code;  /* opcodes */
544   struct Proto **p;  /* functions defined inside the function */
545   Upvaldesc *upvalues;  /* upvalue information */
546   ls_byte *lineinfo;  /* information about source lines (debug information) */
547   AbsLineInfo *abslineinfo;  /* idem */
548   LocVar *locvars;  /* information about local variables (debug information) */
549   TString  *source;  /* used for debug information */
550   GCObject *gclist;
551 } Proto;
552 
553 /* }================================================================== */
554 
555 
556 /*
557 ** {==================================================================
558 ** Closures
559 ** ===================================================================
560 */
561 
562 #define LUA_VUPVAL	makevariant(LUA_TUPVAL, 0)
563 
564 
565 /* Variant tags for functions */
566 #define LUA_VLCL	makevariant(LUA_TFUNCTION, 0)  /* Lua closure */
567 #define LUA_VLCF	makevariant(LUA_TFUNCTION, 1)  /* light C function */
568 #define LUA_VCCL	makevariant(LUA_TFUNCTION, 2)  /* C closure */
569 
570 #define ttisfunction(o)		checktype(o, LUA_TFUNCTION)
571 #define ttisclosure(o)		((rawtt(o) & 0x1F) == LUA_VLCL)
572 #define ttisLclosure(o)		checktag((o), ctb(LUA_VLCL))
573 #define ttislcf(o)		checktag((o), LUA_VLCF)
574 #define ttisCclosure(o)		checktag((o), ctb(LUA_VCCL))
575 
576 #define isLfunction(o)	ttisLclosure(o)
577 
578 #define clvalue(o)	check_exp(ttisclosure(o), gco2cl(val_(o).gc))
579 #define clLvalue(o)	check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
580 #define fvalue(o)	check_exp(ttislcf(o), val_(o).f)
581 #define clCvalue(o)	check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
582 
583 #define fvalueraw(v)	((v).f)
584 
585 #define setclLvalue(L,obj,x) \
586   { TValue *io = (obj); LClosure *x_ = (x); \
587     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \
588     checkliveness(L,io); }
589 
590 #define setclLvalue2s(L,o,cl)	setclLvalue(L,s2v(o),cl)
591 
592 #define setfvalue(obj,x) \
593   { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); }
594 
595 #define setclCvalue(L,obj,x) \
596   { TValue *io = (obj); CClosure *x_ = (x); \
597     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \
598     checkliveness(L,io); }
599 
600 
601 /*
602 ** Upvalues for Lua closures
603 */
604 typedef struct UpVal {
605   CommonHeader;
606   lu_byte tbc;  /* true if it represents a to-be-closed variable */
607   TValue *v;  /* points to stack or to its own value */
608   union {
609     struct {  /* (when open) */
610       struct UpVal *next;  /* linked list */
611       struct UpVal **previous;
612     } open;
613     TValue value;  /* the value (when closed) */
614   } u;
615 } UpVal;
616 
617 
618 
619 #define ClosureHeader \
620 	CommonHeader; lu_byte nupvalues; GCObject *gclist
621 
622 typedef struct CClosure {
623   ClosureHeader;
624   lua_CFunction f;
625   TValue upvalue[1];  /* list of upvalues */
626 } CClosure;
627 
628 
629 typedef struct LClosure {
630   ClosureHeader;
631   struct Proto *p;
632   UpVal *upvals[1];  /* list of upvalues */
633 } LClosure;
634 
635 
636 typedef union Closure {
637   CClosure c;
638   LClosure l;
639 } Closure;
640 
641 
642 #define getproto(o)	(clLvalue(o)->p)
643 
644 /* }================================================================== */
645 
646 
647 /*
648 ** {==================================================================
649 ** Tables
650 ** ===================================================================
651 */
652 
653 #define LUA_VTABLE	makevariant(LUA_TTABLE, 0)
654 
655 #define ttistable(o)		checktag((o), ctb(LUA_VTABLE))
656 
657 #define hvalue(o)	check_exp(ttistable(o), gco2t(val_(o).gc))
658 
659 #define sethvalue(L,obj,x) \
660   { TValue *io = (obj); Table *x_ = (x); \
661     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \
662     checkliveness(L,io); }
663 
664 #define sethvalue2s(L,o,h)	sethvalue(L,s2v(o),h)
665 
666 
667 /*
668 ** Nodes for Hash tables: A pack of two TValue's (key-value pairs)
669 ** plus a 'next' field to link colliding entries. The distribution
670 ** of the key's fields ('key_tt' and 'key_val') not forming a proper
671 ** 'TValue' allows for a smaller size for 'Node' both in 4-byte
672 ** and 8-byte alignments.
673 */
674 typedef union Node {
675   struct NodeKey {
676     TValuefields;  /* fields for value */
677     lu_byte key_tt;  /* key type */
678     int next;  /* for chaining */
679     Value key_val;  /* key value */
680   } u;
681   TValue i_val;  /* direct access to node's value as a proper 'TValue' */
682 } Node;
683 
684 
685 /* copy a value into a key */
686 #define setnodekey(L,node,obj) \
687 	{ Node *n_=(node); const TValue *io_=(obj); \
688 	  n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \
689 	  checkliveness(L,io_); }
690 
691 
692 /* copy a value from a key */
693 #define getnodekey(L,obj,node) \
694 	{ TValue *io_=(obj); const Node *n_=(node); \
695 	  io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \
696 	  checkliveness(L,io_); }
697 
698 
699 /*
700 ** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the
701 ** real size of 'array'. Otherwise, the real size of 'array' is the
702 ** smallest power of two not smaller than 'alimit' (or zero iff 'alimit'
703 ** is zero); 'alimit' is then used as a hint for #t.
704 */
705 
706 #define BITRAS		(1 << 7)
707 #define isrealasize(t)		(!((t)->flags & BITRAS))
708 #define setrealasize(t)		((t)->flags &= cast_byte(~BITRAS))
709 #define setnorealasize(t)	((t)->flags |= BITRAS)
710 
711 
712 typedef struct Table {
713   CommonHeader;
714   lu_byte flags;  /* 1<<p means tagmethod(p) is not present */
715   lu_byte lsizenode;  /* log2 of size of 'node' array */
716   unsigned int alimit;  /* "limit" of 'array' array */
717   TValue *array;  /* array part */
718   Node *node;
719   Node *lastfree;  /* any free position is before this position */
720   struct Table *metatable;
721   GCObject *gclist;
722 } Table;
723 
724 
725 /*
726 ** Macros to manipulate keys inserted in nodes
727 */
728 #define keytt(node)		((node)->u.key_tt)
729 #define keyval(node)		((node)->u.key_val)
730 
731 #define keyisnil(node)		(keytt(node) == LUA_TNIL)
732 #define keyisinteger(node)	(keytt(node) == LUA_VNUMINT)
733 #define keyival(node)		(keyval(node).i)
734 #define keyisshrstr(node)	(keytt(node) == ctb(LUA_VSHRSTR))
735 #define keystrval(node)		(gco2ts(keyval(node).gc))
736 
737 #define setnilkey(node)		(keytt(node) = LUA_TNIL)
738 
739 #define keyiscollectable(n)	(keytt(n) & BIT_ISCOLLECTABLE)
740 
741 #define gckey(n)	(keyval(n).gc)
742 #define gckeyN(n)	(keyiscollectable(n) ? gckey(n) : NULL)
743 
744 
745 /*
746 ** Use a "nil table" to mark dead keys in a table. Those keys serve
747 ** to keep space for removed entries, which may still be part of
748 ** chains. Note that the 'keytt' does not have the BIT_ISCOLLECTABLE
749 ** set, so these values are considered not collectable and are different
750 ** from any valid value.
751 */
752 #define setdeadkey(n)	(keytt(n) = LUA_TTABLE, gckey(n) = NULL)
753 
754 /* }================================================================== */
755 
756 
757 
758 /*
759 ** 'module' operation for hashing (size is always a power of 2)
760 */
761 #define lmod(s,size) \
762 	(check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1)))))
763 
764 
765 #define twoto(x)	(1<<(x))
766 #define sizenode(t)	(twoto((t)->lsizenode))
767 
768 
769 /* size of buffer for 'luaO_utf8esc' function */
770 #define UTF8BUFFSZ	8
771 
772 LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
773 LUAI_FUNC int luaO_ceillog2 (unsigned int x);
774 LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
775                              const TValue *p2, TValue *res);
776 LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
777                            const TValue *p2, StkId res);
778 LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
779 LUAI_FUNC int luaO_hexavalue (int c);
780 LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj);
781 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
782                                                        va_list argp);
783 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
784 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen);
785 
786 
787 #endif
788 
789